spaCy/spacy/lang/th/__init__.py

55 lines
1.4 KiB
Python
Raw Normal View History

2017-09-26 14:36:27 +00:00
# coding: utf8
from __future__ import unicode_literals
from .tokenizer_exceptions import TOKENIZER_EXCEPTIONS
from .tag_map import TAG_MAP
from .stop_words import STOP_WORDS
from ...attrs import LANG
2017-09-26 14:36:27 +00:00
from ...language import Language
from ...tokens import Doc
from ...util import DummyTokenizer
class ThaiTokenizer(DummyTokenizer):
def __init__(self, cls, nlp=None):
try:
from pythainlp.tokenize import word_tokenize
except ImportError:
raise ImportError(
"The Thai tokenizer requires the PyThaiNLP library: "
2019-02-07 19:54:07 +00:00
"https://github.com/PyThaiNLP/pythainlp"
)
self.word_tokenize = word_tokenize
self.vocab = nlp.vocab if nlp is not None else cls.create_vocab(nlp)
def __call__(self, text):
words = list(self.word_tokenize(text, "newmm"))
spaces = [False] * len(words)
return Doc(self.vocab, words=words, spaces=spaces)
2017-09-26 14:36:27 +00:00
2017-10-11 00:22:49 +00:00
2017-09-26 14:36:27 +00:00
class ThaiDefaults(Language.Defaults):
lex_attr_getters = dict(Language.Defaults.lex_attr_getters)
2019-02-07 19:54:07 +00:00
lex_attr_getters[LANG] = lambda _text: "th"
2017-10-11 00:22:49 +00:00
tokenizer_exceptions = dict(TOKENIZER_EXCEPTIONS)
tag_map = TAG_MAP
stop_words = STOP_WORDS
2017-09-26 14:36:27 +00:00
@classmethod
def create_tokenizer(cls, nlp=None):
return ThaiTokenizer(cls, nlp)
2017-09-26 14:36:27 +00:00
class Thai(Language):
lang = "th"
2017-10-11 00:22:49 +00:00
Defaults = ThaiDefaults
def make_doc(self, text):
return self.tokenizer(text)
2017-10-11 00:22:49 +00:00
2017-09-26 14:36:27 +00:00
__all__ = ["Thai"]