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 Code style - black

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

[^1]: The logo was originally generated using OpenAI DALL·E 2

import rhoknp

# Perform language 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 language analysis by Juman++
with open("result.jumanpp", "wt") as f:
    f.write(sentence.to_jumanpp())

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

Requirements

  • Python 3.7+

Optional requirements for language analysis

Installation

pip install rhoknp

Documentation

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

Quick tour

Let's start with using Juman++ with rhoknp. Here is a simple example of using Juman++ to analyze a sentence.

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

You can easily access the 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())

rhoknp also provides 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 almost the same way as Sentence objects.

# Perform language 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, explore the examples and documentation.

Main differences from pyknp

pyknp has been developed as the official Python binding for Juman++ and KNP. In rhoknp, we redesigned the API from the top-down, taking into account the current use cases of pyknp. The main differences are as follows:

  • Support for document-level language analysis: rhoknp can load and instantiate the result of document-level language analysis (i.e., cohesion analysis and discourse relation analysis).
  • Strictly type-aware: rhoknp is thoroughly annotated with type annotations.
  • Extensive test suite: rhoknp is tested with an extensive test suite. See the code coverage at Codecov.

License

MIT

Contributing

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

Reference

Project details


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.2.2.tar.gz (43.2 kB view hashes)

Uploaded Source

Built Distribution

rhoknp-1.2.2-py3-none-any.whl (60.9 kB view hashes)

Uploaded Python 3

Supported by

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