- Notifications
You must be signed in to change notification settings - Fork1
License
ku-nlp/pyknp-eventgraph
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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.
- Python 3.6 or later
- pyknp
- graphviz
To install pyknp-eventgraph, usepip
.
$ pip install pyknp-eventgraph
An EventGraph is built on language analysis given in a KNP format.
# Add imports.frompyknpimportKNPfrompyknp_eventgraphimportEventGraph# Parse a document.document= ['彼女は海外勤務が長いので、英語がうまいに違いない。','私はそう確信していた。']knp=KNP()analysis= [knp.parse(sentence)forsentenceindocument]# Create an EventGraph.evg=EventGraph.build(analysis)print(evg)# <EventGraph, #sentences: 2, #events: 3, #relations: 1>
Users can obtain various information about language analysis via a simple interface.
# Extract sentences.sentences=evg.sentencesprint(sentences)# [# <Sentence, sid: 1, ssid: 0, surf: 彼女は海外勤務が長いので、英語がうまいに違いない。>,# <Sentence, sid: 2, ssid: 1, surf: 私はそう確信していた。># ]# Convert a sentence into various forms.sentence=evg.sentences[0]print(sentence.surf)# 彼女は海外勤務が長いので、英語がうまいに違いない。print(sentence.mrphs)# 彼女 は 海外 勤務 が 長い ので 、 英語 が うまい に 違いない 。print(sentence.reps)# 彼女/かのじょ は/は 海外/かいがい 勤務/きんむ が/が 長い/ながい ので/ので 、/、 英語/えいご が/が 上手い/うまい に/に 違い無い/ちがいない 。/。
# Extract events.events=evg.eventsprint(events)# [# <Event, evid: 0, surf: 海外勤務が長いので、>,# <Event, evid: 1, surf: 彼女は英語がうまいに違いない。>,# <Event, evid: 2, surf: 私はそう確信していた。># ]# Convert an event into various 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)# ['海外/かいがい', '勤務/きんむ', '長い/ながい']# Extract an event's PAS.pas=event.pasprint(pas)# <PAS, predicate: 長い/ながい, arguments: {ガ: 勤務/きんむ}>print(pas.predicate)# <Predicate, type: 形, surf: 長い>print(pas.arguments)# defaultdict(<class 'list'>, {'ガ': [<Argument, case: ガ, surf: 勤務が>]})# Extract an event's features.features=event.featuresprint(features)# <Features, modality: None, tense: 非過去, negation: False, state: 状態述語, complement: False>
# Extract event-to-event relations.relations=evg.relationsprint(relations)# [<Relation, label: 原因・理由, modifier_evid: 0, head_evid: 1>]# Take a closer look at an event-to-event relationrelation=relations[0]print(relation.label)# 原因・理由print(relation.surf)# のでprint(relation.modifier)# <Event, evid: 0, surf: 海外勤務が長いので、>print(relation.head)# <Event, evid: 1, surf: 彼女は英語がうまいに違いない。>
Users can save and load an EventGraph by serializing it as a JSON object.
# Save an EventGraph as a JSON file.evg.save('evg.json')# Load an EventGraph from a JSON file.withopen('evg.json')asf:evg=EventGraph.load(f)
Users can visualize an EventGraph usinggraphviz.
frompyknp_eventgraphimportmake_imagemake_image(evg,'evg.svg')# Currently, only supports 'svg'.
By merging a modifier event to the modifiee, users can construct a larger information unit.
frompyknpimportKNPfrompyknp_eventgraphimportEventGraphdocument= ['もっととろみが持続する作り方をして欲しい。']knp=KNP()analysis= [knp.parse(sentence)forsentenceindocument]evg=EventGraph.build(analysis)print(evg)# <EventGraph, #sentences: 1, #events: 2, #relations: 1># Investigate the relation.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: 作り方をして欲しい。># To merge modifier events, enable `include_modifiers`.print(relation.head.surf)# 作り方をして欲しい。print(relation.head.surf_(include_modifiers=True))# もっととろみが持続する作り方をして欲しい。# Other formats also support `include_modifiers`.print(relation.head.mrphs_(include_modifiers=True))# もっと とろみ が 持続 する 作り 方 を して 欲しい 。print(relation.head.normalized_mrphs_(include_modifiers=True))# もっと とろみ が 持続 する 作り 方 を して 欲しい
When an EventGraph is serialized in a JSON format, it will lose some functionality, including access to KNP objects and 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.withopen('evg.pkl','rb')asf:evg_=EventGraph.load(f,binary=True)
$ echo '彼女は海外勤務が長いので、英語がうまいに違いない。' | jumanpp | knp -tab | evg -o example-eventgraph.json
$ evgviz example-eventgraph.json example-eventgraph.svg
https://pyknp-eventgraph.readthedocs.io/en/latest/
- Kurohashi-Kawahara Lab, Kyoto University.
- contact@nlp.ist.i.kyoto-u.ac.jp
About
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.