Skip to main content

KorAP web service client package for Python

Project Status: Active – The project has reached a stable, usable state and is being actively developed. CI check Last commit GitHub closed issues GitHub issues GitHub license PyPI - Python Version PyPI - Downloads

Description

Python client wrapper package to access the web service API of the KorAP Corpus Analysis Platform developed at IDS Mannheim. Currently, this is no native Python package. Internally, it uses KorAP's client package for R via rpy2. The latter also automatically translates between R data frames (or tibbles) and pandas DataFrames.

Installation

1. Install latest R version from CRAN

or, alternatively, on some recent Linux distributions:

#### Debian / Ubuntu
sudo apt-get install -y r-base r-base-dev r-cran-tidyverse r-cran-r.utils r-cran-pixmap r-cran-webshot r-cran-ade4 r-cran-segmented r-cran-purrr r-cran-dygraphs r-cran-cvst r-cran-quantmod r-cran-graphlayouts r-cran-rappdirs r-cran-ggdendro r-cran-seqinr r-cran-heatmaply r-cran-igraph r-cran-plotly libcurl4-gnutls-dev libssl-dev libfontconfig1-dev libsecret-1-dev libxml2-dev libsodium-dev python3-pip python3-rpy2 python3-pandas

#### Fedora / CentOS / RHEL
sudo yum install -y R R-devel libcurl-devel openssl-devel libxml2-devel libsodium-devel python3-pandas

2. Install the RKorAPClient package

Start R and run:

install.packages('RKorAPClient', repos='https://cloud.r-project.org/')

or install RKorAPClient from the package installation menu entry.

3. Install the Python package

On Linux an MacOs:

python3 -m pip install KorAPClient

On Windows:

py -m pip install KorAPClient

Documentation

The core classes and methods to access the KorAP API are documented in the KorAPClient API documentation. For additional, mostly static helper functions, please refer to the Reference Manual of RKorAPClient for now. For translating R syntax to Python and vice versa, refer to the rpy2 Documentation.

Please note that some arguments in the original RKorAPClient functions use characters that are not allowed in Python keyword argument names. For these cases, you can however use Python's **kwargs syntax. For example, to let frequencyQuery interpret queries as queries for alternative variants and make it return their proportions instead of relative frequencies, you can write:

from KorAPClient import KorAPConnection
KorAPConnection(verbose=True) \
    .frequencyQuery(['"Wissenschaftler.*"', '"Wissenschafter.*"'],\
                    **{"as.alternatives": True})
query totalResults vc webUIRequestUrl total f conf.low conf.high
1 "Wissenschaftler.*" 942053 https://korap.ids-mannheim.de/?q=%22Wissenschaftler.%2a%22&ql=poliqarp 1080268 0.872055 0.871423 0.872684
2 "Wissenschafter.*" 138215 https://korap.ids-mannheim.de/?q=%22Wissenschafter.%2a%22&ql=poliqarp 1080268 0.127945 0.127316 0.128577

Examples

Frequencies of "Hello World" over years and countries

from KorAPClient import KorAPClient, KorAPConnection
import altair as alt
import pandas as pd

QUERY = "Hello World"
df = pd.DataFrame(range(2010, 2019), columns=["Year"], dtype=str) \
    .merge(pd.DataFrame(["DE", "CH"], columns=["Country"]), how="cross")
df["vc"] = "textType=/Zeit.*/ & pubPlaceKey = " + df.Country + " & pubDate in " + df.Year
df = KorAPClient.ipm(KorAPConnection().frequencyQuery(QUERY, df.vc)).merge(df)

alt.Chart(df).mark_line(point=True).encode(y="ipm", x="Year:T", color="Country", href="webUIRequestUrl") \
    .properties(title=QUERY).show()

Frequency per million words of “Hello World“ in DE vs. CH from 2010 to 2018 in newspapers and magazines

Identify in … setzen light verb constructions by the collocationAnalysis method

Lifecycle:experimental

from KorAPClient import KorAPConnection

kcon = KorAPConnection(verbose=True)
results = kcon.collocationAnalysis("focus(in [tt/p=NN] {[tt/l=setzen]})",
                                   leftContextSize=1,
                                   rightContextSize=0,
                                   exactFrequencies=False,
                                   searchHitsSampleLimit=1000,
                                   topCollocatesLimit=20)
results['collocate'] = "[" + results['collocate'] +"](" + results['webUIRequestUrl'] +")"
print(results[['collocate', 'logDice', 'pmi', 'll']].head(10).round(2).to_markdown(floatfmt=".2f"))
collocate logDice pmi ll
1 Szene 10.37 11.54 824928.58
2 Gang 9.65 10.99 366993.93
3 Verbindung 9.20 10.34 347644.75
4 Kenntnis 9.15 10.67 206902.89
5 Bewegung 8.80 9.91 264577.07
6 Brand 8.76 9.97 210654.43
7 Anführungszeichen 8.06 12.52 54148.31
8 Kraft 7.94 8.91 189399.70
9 Beziehung 6.92 8.29 37723.54
10 Relation 6.64 10.24 17105.84

Command Line Invocation

The Python KorAP client can also be called from the command line and shell scripts:

$ korapclient -h
usage: python -m KorAPClient [-h] [-v] [-l QUERY_LANGUAGE] [-u API_URL] [-c VC [VC ...]] [-q QUERY [QUERY ...]]

Send a query to the KorAP API and print results as tsv.

optional arguments:
  -h, --help            show this help message and exit
  -v, --verbose
  -l QUERY_LANGUAGE, --query-language QUERY_LANGUAGE
  -u API_URL, --api-url API_URL
                        Specify this to access a corpus other that DeReKo.
  -c VC [VC ...], --vc VC [VC ...]
                        virtual corpus definition[s]
  -q QUERY [QUERY ...], --query QUERY [QUERY ...]
                        If not specified only the size of the virtual corpus will be queried.

example:
  python -m KorAPClient -v --query "Hello World" "Hallo Welt" --vc "pubDate in 2017" "pubDate in 2018" "pubDate in 2019"

Accessed API Services

By using the KorAPClient you agree to the respective terms of use of the accessed KorAP API services which will be printed upon opening a connection.

Development and License

Author: Marc Kupietz

Copyright (c) 2021, Leibniz Institute for the German Language, Mannheim, Germany

This package is developed as part of the KorAP Corpus Analysis Platform at the Leibniz Institute for German Language (IDS).

It is published under the BSD-2 License.

Contributors: Ines Pisetta, Nils Diewald

To cite this work, please refer to: Kupietz et al. (2020, 2022), below.

Contributions

Contributions are very welcome!

Your contributions should ideally be committed via our Gerrit server to facilitate reviewing (see Gerrit Code Review - A Quick Introduction if you are not familiar with Gerrit). However, we are also happy to accept comments and pull requests via GitHub.

Please note that unless you explicitly state otherwise any contribution intentionally submitted for inclusion into this software shall – as this software itself – be under the BSD-2 License.

References

Metadata

Release files for KorAPClient 0.7.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for KorAPClient 0.7.5
File Size Uploaded Provenance Actions
KorAPClient-0.7.5.tar.gz 11.6 kB Without provenance Details for KorAPClient-0.7.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for KorAPClient 0.7.5
File Interpreter ABI Platform Actions
KorAPClient-0.7.5-py3-none-any.whl Python 3 none any Details for KorAPClient-0.7.5-py3-none-any.whl

Total release size: 24.2 kB

Release files / KorAPClient-0.7.5.tar.gz

Download URL KorAPClient-0.7.5.tar.gz
Size 11.6 kB
Tags Source
SHA-256 checksum
How to use checksums
9baa46fe0b9702fbca4de94c0b7f0c4af9baf935c4ac2cb237f301899ad8e3af
BLAKE2b-256 checksum
How to use checksums
e42a6866197e22d408df9cb9c90fec825015567a61e0d0a42a46a2788731eb49
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Release files / KorAPClient-0.7.5-py3-none-any.whl

Download URL KorAPClient-0.7.5-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
53796b6ab52c4617b48af0709f82a1630379adac5b440492a69bb3266f24b4d5
BLAKE2b-256 checksum
How to use checksums
f8656d433efc9d392d82b904c768aac057e12c2e3cb155889decf789f0106b13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.6

Security for KorAPClient 0.7.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page