spaCy/spacy/lang/ja/__init__.py

41 lines
1.2 KiB
Python
Raw Normal View History

2017-05-03 04:56:21 +00:00
# encoding: utf8
from __future__ import unicode_literals, print_function
2017-05-08 20:29:04 +00:00
from ...language import Language
from ...attrs import LANG
from ...tokens import Doc
2017-10-14 11:11:39 +00: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))
class JapaneseDefaults(Language.Defaults):
@classmethod
def create_tokenizer(cls, nlp=None):
return JapaneseTokenizer(cls, nlp)
2017-05-03 04:56:21 +00:00
class Japanese(Language):
lang = 'ja'
2017-10-14 11:11:39 +00:00
Defaults = JapaneseDefaults
2017-05-03 04:56:21 +00:00
def make_doc(self, text):
2017-10-14 11:11:39 +00:00
words = self.tokenizer(text)
2017-05-03 04:56:21 +00:00
return Doc(self.vocab, words=words, spaces=[False]*len(words))
2017-05-03 09:07:29 +00:00
2017-05-08 13:50:46 +00:00
__all__ = ['Japanese']