File size: 1,664 Bytes
e4b9a7b | 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | # -*- coding: utf-8 -*-
"""
spacy_thai: Tokenizer, POS tagger, and dependency parser for the Thai language using Universal Dependencies.
GitHub: https://github.com/KoichiYasuoka/spacy-thai
"""
from typing import List, Union
import spacy_thai
class Parse:
def __init__(self, model: str = "th") -> None:
self.nlp = spacy_thai.load()
def __call__(
self, text: str, tag: str = "str"
) -> Union[List[List[str]], str]:
doc = self.nlp(text)
_text = []
if tag == "list":
_tag_data = []
for t in doc:
_tag_data.append(
[
str(t.i + 1),
t.orth_,
t.lemma_,
t.pos_,
t.tag_,
"_",
str(0 if t.head == t else t.head.i + 1),
t.dep_,
"_",
"_" if t.whitespace_ else "SpaceAfter=No",
]
)
return _tag_data
for t in doc:
_text.append(
"\t".join(
[
str(t.i + 1),
t.orth_,
t.lemma_,
t.pos_,
t.tag_,
"_",
str(0 if t.head == t else t.head.i + 1),
t.dep_,
"_",
"_" if t.whitespace_ else "SpaceAfter=No",
]
)
)
return "\n".join(_text)
|