Skip to main content

Yet another Python binding for Juman++/KNP/KWJA

Project description

rhoknp logo

rhoknp: Yet another Python binding for Juman++/KNP/KWJA

Test Codecov CodeFactor PyPI PyPI - Python Version Documentation Ruff


Documentation: https://rhoknp.readthedocs.io/en/latest/

Source Code: https://github.com/ku-nlp/rhoknp


rhoknp is a Python binding for Juman++, KNP, and KWJA.[^1]

[^1]: The logo was generated by OpenAI DALL·E 2.

import rhoknp

# Perform morphological analysis by Juman++
jumanpp = rhoknp.Jumanpp()
sentence = jumanpp.apply_to_sentence(
    "電気抵抗率は電気の通しにくさを表す物性値である。"
)

# Access to the result
for morpheme in sentence.morphemes:  # a.k.a. keitai-so
    ...

# Save the result
with open("result.jumanpp", "wt") as f:
    f.write(sentence.to_jumanpp())

# Load the result
with open("result.jumanpp", "rt") as f:
    sentence = rhoknp.Sentence.from_jumanpp(f.read())

Requirements

  • Python 3.10+
  • (Optional) Juman++ v2.0.0-rc3+
  • (Optional) KNP 5.0+
  • (Optional) KWJA 1.0.0+

Installation

pip install rhoknp

Quick tour

Let's begin by using Juman++ with rhoknp. Here, we present a simple example demonstrating how Juman++ can be used to analyze a sentence.

# Perform morphological analysis by Juman++
jumanpp = rhoknp.Jumanpp()
sentence = jumanpp.apply_to_sentence("電気抵抗率は電気の通しにくさを表す物性値である。")

You can easily access the individual morphemes that make up the sentence.

for morpheme in sentence.morphemes:  # a.k.a. keitai-so
    ...

Sentence objects can be saved in the JUMAN format.

# Save the sentence in the JUMAN format
with open("sentence.jumanpp", "wt") as f:
    f.write(sentence.to_jumanpp())

# Load the sentence
with open("sentence.jumanpp", "rt") as f:
    sentence = rhoknp.Sentence.from_jumanpp(f.read())

Almost the same APIs are available for KNP.

# Perform language analysis by KNP
knp = rhoknp.KNP()
sentence = knp.apply_to_sentence("電気抵抗率は電気の通しにくさを表す物性値である。")

KNP performs language analysis at multiple levels.

for clause in sentence.clauses:  # a.k.a., setsu
    ...
for phrase in sentence.phrases:  # a.k.a. bunsetsu
    ...
for base_phrase in sentence.base_phrases:  # a.k.a. kihon-ku
    ...
for morpheme in sentence.morphemes:  # a.k.a. keitai-so
    ...

Sentence objects can be saved in the KNP format.

# Save the sentence in the KNP format
with open("sentence.knp", "wt") as f:
    f.write(sentence.to_knp())

# Load the sentence
with open("sentence.knp", "rt") as f:
    sentence = rhoknp.Sentence.from_knp(f.read())

Furthermore, rhoknp provides convenient APIs for document-level language analysis.

document = rhoknp.Document.from_raw_text(
    "電気抵抗率は電気の通しにくさを表す物性値である。単に抵抗率とも呼ばれる。"
)
# If you know sentence boundaries, you can use `Document.from_sentences` instead.
document = rhoknp.Document.from_sentences(
    [
        "電気抵抗率は電気の通しにくさを表す物性値である。",
        "単に抵抗率とも呼ばれる。",
    ]
)

Document objects can be handled in a similar manner as Sentence objects.

# Perform morphological analysis by Juman++
document = jumanpp.apply_to_document(document)

# Access language units in the document
for sentence in document.sentences:
    ...
for morpheme in document.morphemes:
    ...

# Save language analysis by Juman++
with open("document.jumanpp", "wt") as f:
    f.write(document.to_jumanpp())

# Load language analysis by Juman++
with open("document.jumanpp", "rt") as f:
    document = rhoknp.Document.from_jumanpp(f.read())

For more information, please refer to the examples and documentation.

Main differences from pyknp

pyknp serves as the official Python binding for Juman++ and KNP. In the development of rhoknp, we redesigned the API, considering the current use cases of pyknp. The key differences between the two are as follows:

  • Support for document-level language analysis: rhoknp allows you to load and instantiate the results of document-level language analysis, including cohesion analysis and discourse relation analysis.
  • Strict type-awareness: rhoknp has been thoroughly annotated with type annotations, ensuring strict type checking and improved code clarity.
  • Comprehensive test suite: rhoknp is extensively tested with a comprehensive test suite. You can view the code coverage report on Codecov.

License

MIT

Contributing

We warmly welcome contributions to rhoknp. You can get started by reading the contribution guide.

Reference

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rhoknp-1.8.0.tar.gz (66.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rhoknp-1.8.0-py3-none-any.whl (94.8 kB view details)

Uploaded Python 3

File details

Details for the file rhoknp-1.8.0.tar.gz.

File metadata

  • Download URL: rhoknp-1.8.0.tar.gz
  • Upload date:
  • Size: 66.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rhoknp-1.8.0.tar.gz
Algorithm Hash digest
SHA256 083a0c75718a1b192dccbff1669267924b39af30c2445656b5e08adbd6b3be7f
MD5 83e2b76ef9faf675a54cc5e60bccb143
BLAKE2b-256 9d274100dcabfb721ff3bafdb57b77169e6410dab4ba94e93dcaa5095b3bb818

See more details on using hashes here.

Provenance

The following attestation bundles were made for rhoknp-1.8.0.tar.gz:

Publisher: publish.yml on ku-nlp/rhoknp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rhoknp-1.8.0-py3-none-any.whl.

File metadata

  • Download URL: rhoknp-1.8.0-py3-none-any.whl
  • Upload date:
  • Size: 94.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for rhoknp-1.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e8ec14f18d946d44a3db489765760f871a2179a464ee93eacdc14bb1e0df0893
MD5 4df5e3e1b4e5cfcd1cef962f830a26f9
BLAKE2b-256 c7cee853e8a67514dcec61da84ac3b1e6a8e509bf1406d07eb7f0184b49b7743

See more details on using hashes here.

Provenance

The following attestation bundles were made for rhoknp-1.8.0-py3-none-any.whl:

Publisher: publish.yml on ku-nlp/rhoknp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page