Download runtime/example.py from iapp/OpenThai2.0-NLP-Core-Lite: direct link, hf CLI and curl.
- Browser
- Download file 1.92 kB
-
https://huggingface.co/iapp/OpenThai2.0-NLP-Core-Lite/resolve/main/runtime/example.py
- Command line
-
hf download hf://iapp/OpenThai2.0-NLP-Core-Lite/runtime/example.py
-
curl -L -o example.py https://huggingface.co/iapp/OpenThai2.0-NLP-Core-Lite/resolve/main/runtime/example.py
1.92 kB
| """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)) | |