2017-05-03 07:56:21 +03:00
|
|
|
# encoding: utf8
|
|
|
|
from __future__ import unicode_literals, print_function
|
|
|
|
|
2017-05-08 23:29:04 +03:00
|
|
|
from ...language import Language
|
|
|
|
from ...attrs import LANG
|
|
|
|
from ...tokens import Doc
|
2017-10-14 14:11:39 +03:00
|
|
|
from ...tokenizer import Tokenizer
|
|
|
|
|
|
|
|
|
|
|
|
class JapaneseTokenizer(object):
|
|
|
|
def __init__(self, cls, nlp=None):
|
|
|
|
self.vocab = nlp.vocab if nlp is not None else cls.create_vocab(nlp)
|
|
|
|
try:
|
|
|
|
from janome.tokenizer import Tokenizer
|
|
|
|
except ImportError:
|
|
|
|
raise ImportError("The Japanese tokenizer requires the Janome "
|
|
|
|
"library: https://github.com/mocobeta/janome")
|
|
|
|
self.tokenizer = Tokenizer()
|
|
|
|
|
|
|
|
def __call__(self, text):
|
|
|
|
words = [x.surface for x in self.tokenizer.tokenize(text)]
|
|
|
|
return Doc(self.vocab, words=words, spaces=[False]*len(words))
|
|
|
|
|
2017-11-15 14:44:02 +03:00
|
|
|
# add dummy methods for to_bytes, from_bytes, to_disk and from_disk to
|
|
|
|
# allow serialization (see #1557)
|
|
|
|
def to_bytes(self, **exclude):
|
|
|
|
return b''
|
|
|
|
|
|
|
|
def from_bytes(self, bytes_data, **exclude):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def to_disk(self, path, **exclude):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def from_disk(self, path, **exclude):
|
|
|
|
return self
|
|
|
|
|
2017-10-14 14:11:39 +03:00
|
|
|
|
|
|
|
class JapaneseDefaults(Language.Defaults):
|
2017-11-15 14:44:02 +03:00
|
|
|
lex_attr_getters = dict(Language.Defaults.lex_attr_getters)
|
|
|
|
lex_attr_getters[LANG] = lambda text: 'ja'
|
|
|
|
|
2017-10-14 14:11:39 +03:00
|
|
|
@classmethod
|
|
|
|
def create_tokenizer(cls, nlp=None):
|
|
|
|
return JapaneseTokenizer(cls, nlp)
|
2017-05-03 07:56:21 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Japanese(Language):
|
|
|
|
lang = 'ja'
|
2017-10-14 14:11:39 +03:00
|
|
|
Defaults = JapaneseDefaults
|
2017-05-03 07:56:21 +03:00
|
|
|
|
|
|
|
def make_doc(self, text):
|
2017-10-24 14:02:19 +03:00
|
|
|
return self.tokenizer(text)
|
2017-05-03 12:07:29 +03:00
|
|
|
|
2017-05-08 16:50:46 +03:00
|
|
|
|
|
|
|
__all__ = ['Japanese']
|