Skip to main content

A a development platform for high-level NLP applications in Japanese

Project description

pyknp-eventgraph

EventGraph is a development platform for high-level NLP applications in Japanese. The core concept of EventGraph is event, a language information unit that is closely related to predicate-argument structure but more application-oriented. Events are linked to each other based on their syntactic and semantic relations.

Requirements

  • Python 3.6
  • pyknp: 0.4.1
  • graphviz: 0.10.1

Installation

To install pyknp-eventgraph, use pip.

$ pip install pyknp-eventgraph

or

$ python setup.py install

Basic Usage

# Add imports.
from pyknp import KNP
from pyknp_eventgraph import EventGraph

# Parse a document.
document = ['彼女は海外勤務が長いので、英語がうまいに違いない。', '私はそう確信していた。']
knp = KNP()
analysis = [knp.parse(sentence) for sentence in document]

# Create an EventGraph.
evg = EventGraph.build(analysis)

# Print the EventGraph.
print(evg)  # EventGraph(#sentences: 2, #events: 3, #relations: 1)

# Print sentences.
print(evg.sentences[0])  # Sentence(sid: 1, ssid: 0, surf: 彼女は海外勤務が長いので、英語がうまいに違いない。)
print(evg.sentences[1])  # Sentence(sid: 2, ssid: 1, surf: 私はそう確信していた。)

# Sentences are iterable.
for sentence in evg.sentences:
    pass

# Print events.
print(evg.events[0])  # Event(evid: 0, surf: 海外勤務が長いので、)
print(evg.events[1])  # Event(evid: 1, surf: 彼女は英語がうまいに違いない。)
print(evg.events[2])  # Event(evid: 2, surf: 私はそう確信していた。)

# Events are also iterable.
for event in evg.events:
    pass

# Print an event in different forms.
event = evg.events[0]
print(event.surf)              # 海外勤務が長いので、
print(event.mrphs)             # 海外 勤務 が 長い ので 、
print(event.normalized_mrphs)  # 海外 勤務 が 長い
print(event.reps)              # 海外/かいがい 勤務/きんむ が/が 長い/ながい ので/ので 、/、
print(event.normalized_reps)   # 海外/かいがい 勤務/きんむ が/が 長い/ながい
print(event.content_rep_list)  # ['海外/かいがい', '勤務/きんむ', '長い/ながい']

# Print an event's PAS information.
print(event.predicate)        # Predicate(type: 形, surf: 長い)
print(event.arguments['ガ'])  # [Argument(case: ガ, surf: 勤務が)]

# Print an event's features.
print(event.features)  # Features(modality: None, tense: 非過去, negation: False, state: 状態述語, complement: False)

# Print relations between events.
print(evg.relations[0])           # Relation(label: 原因・理由, modifier_evid: 0, head_evid: 1)
print(evg.relations[0].modifier)  # Event(evid: 0, surf: 海外勤務が長いので、)
print(evg.relations[0].head)      # Event(evid: 1, surf: 彼女は英語がうまいに違いない。)

# Relations are iterable, too.
for relation in evg.relations:
    pass

# Convert an EventGraph into a dictionary.
dct = evg.to_dict()  # {"sentences": ..., "events": ...}

# Save an EventGraph.
evg.save('evg.json')

# Load an EventGraph.
with open('evg.json') as f:
    evg_ = EventGraph.load(f)

# Visualize an EventGraph.
from pyknp_eventgraph import make_image
make_image(evg, 'evg.svg')  # Currently, only supports 'svg'.

Advanced Usage: Merging modifiers

By merging a modifier event to the modifiee, users can construct a larger information unit.

from pyknp import KNP
from pyknp_eventgraph import EventGraph

document = ['もっととろみが持続する作り方をして欲しい。']
knp = KNP()
analysis = [knp.parse(sentence) for sentence in document]

# Print some information.
evg = EventGraph.build(analysis)
print(evg)  # EventGraph(#sentences: 1, #events: 2, #relations: 1)

# Investigate what the relation is.
relation = evg.relations[0]
print(relation)           # Relation(label: 連体修飾, modifier_evid: 0, head_evid: 1)
print(relation.modifier)  # Event(evid: 0, surf: もっととろみが持続する)
print(relation.head)      # Event(evid: 1, surf: 作り方をして欲しい。)
print(relation.label)     # 連体修飾

# To merge modifiers' tokens, enable `include_modifiers`.
print(relation.head.surf)                           # 作り方をして欲しい。
print(relation.head.surf_(include_modifiers=True))  # もっととろみが持続する作り方をして欲しい。

print(relation.head.mrphs)                           # 作り 方 を して 欲しい 。
print(relation.head.mrphs_(include_modifiers=True))  # もっと とろみ が 持続 する 作り 方 を して 欲しい 。

print(relation.head.normalized_mrphs)                           # 作り 方 を して 欲しい
print(relation.head.normalized_mrphs_(include_modifiers=True))  # もっと とろみ が 持続 する 作り 方 を して 欲しい

Advanced Usage: Binary serialization

When an EventGraph is serialized in a JSON format, it will lose some functionality, including modifier merging. To keep full functionality, use Python's pickle utility for serialization.

# Save an EventGraph using Python's pickle utility.
evg.save('evg.pkl', binary=True)

# Load an EventGraph using Python's pickle utility.
with open('evg.pkl', 'rb') as f:
    evg_ = EventGraph.load(f, binary=True)

CLI

EventGraph Construction

$ echo '彼女は海外勤務が長いので、英語がうまいに違いない。' | jumanpp | knp -tab | evg -o example-eventgraph.json

EventGraph Visualization

$ evgviz example-eventgraph.json example-eventgraph.svg

Documents

https://pyknp-eventgraph.readthedocs.io/en/latest/

Authors

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

pyknp-eventgraph-6.0.0.tar.gz (24.7 kB view details)

Uploaded Source

Built Distribution

pyknp_eventgraph-6.0.0-py3-none-any.whl (41.5 kB view details)

Uploaded Python 3

File details

Details for the file pyknp-eventgraph-6.0.0.tar.gz.

File metadata

  • Download URL: pyknp-eventgraph-6.0.0.tar.gz
  • Upload date:
  • Size: 24.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.6.9

File hashes

Hashes for pyknp-eventgraph-6.0.0.tar.gz
Algorithm Hash digest
SHA256 235a600ee0e989e61c46a9cdd5c15c91444644fa8d0ff1f2d3d791433f20e8f2
MD5 d16e524a3a1869470d3c20115e1c182d
BLAKE2b-256 f1c588cea181479903a1ad109ce6b7d47b524c9832303c34e1d496fc74162548

See more details on using hashes here.

File details

Details for the file pyknp_eventgraph-6.0.0-py3-none-any.whl.

File metadata

  • Download URL: pyknp_eventgraph-6.0.0-py3-none-any.whl
  • Upload date:
  • Size: 41.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.6.9

File hashes

Hashes for pyknp_eventgraph-6.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b0623f7bfd40d229c7f65c775e3ba8bf486693312664c328694c19fccd9bf8ad
MD5 ed0760c122c6ba0359a9cc29328c573b
BLAKE2b-256 2d7a628a97332cb243ed11dd18c5cad8f3f5a16cb6761c47b03fa42ab116b7d7

See more details on using hashes here.

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