File size: 1,917 Bytes
f69d4c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""The main model on one text: the whole pipeline in one call, then one function per task: python example.py"""
import json

from openthai_nlp_core_lite import NLPCore

text = "นายกรัฐมนตรีเดินทางไปเชียงใหม่เมื่อวานนี้เพื่อประชุมกับผู้ว่าราชการจังหวัด ฝนตกหนักทั้งวันแต่การประชุมก็ผ่านไปด้วยดี"
nlp = NLPCore()

doc = nlp(text)  # every task: one Document (nlp.analyze(text) is the same)
for s in doc.sentences:
    print("sentence:", s.text)
    for w in s.words:
        print("   ", w.id, w.text, w.upos, w.xpos, w.head, w.deprel, (w.start, w.end))
    print("    tree:", s.tree)
print("clauses:", [c.text for c in doc.clauses])
print("entities:", doc.entities)
print("only POS and NER:", json.dumps(nlp(text, tasks=["pos", "ner"]).to_dict(), ensure_ascii=False)[:160], "...")
print(doc.to_conllu())

# the same results, one function per task
for unit in ("long", "short", "minimal"):
    print(f"word_tokenize({unit}):", " | ".join(nlp.word_tokenize(text, unit)))
print("sent_tokenize:", nlp.sent_tokenize(text))
print("sent_spans:", nlp.sent_spans(text))
print("clause_tokenize:", nlp.clause_tokenize(text))
print("pos_tag (ud):", nlp.pos_tag(text))
print("pos_tag (orchid, long):", nlp.pos_tag(text, unit="long", tagset="orchid"))
print("pos_tag (a list of words):", nlp.pos_tag(["ฝน", "ตก", "หนัก"]))
for sent in nlp.dependency_parse(text):
    print("dependency_parse:", [(w.text, w.head, w.deprel) for w in sent])
for tree in nlp.constituency_parse(text):
    print("constituency_parse:", tree)
print("constituency_parse(inside_words):", nlp.constituency_parse(text, inside_words=True)[0])
print("word_trees:", nlp.word_trees(text))
print("ner:", nlp.ner(text))