2017-05-03 07:56:21 +03:00
|
|
|
# encoding: utf8
|
|
|
|
from __future__ import unicode_literals, print_function
|
|
|
|
|
2018-05-03 19:38:26 +03:00
|
|
|
import re
|
|
|
|
from collections import namedtuple
|
|
|
|
|
2019-01-10 17:40:37 +03:00
|
|
|
from .tag_map import TAG_MAP
|
|
|
|
|
|
|
|
from ...attrs import LANG
|
|
|
|
from ...language import Language
|
|
|
|
from ...tokens import Doc, Token
|
|
|
|
from ...util import DummyTokenizer
|
|
|
|
|
2018-05-03 19:38:26 +03:00
|
|
|
ShortUnitWord = namedtuple('ShortUnitWord', ['surface', 'lemma', 'pos'])
|
|
|
|
|
|
|
|
# XXX Is this the right place for this?
|
|
|
|
Token.set_extension('mecab_tag', default=None)
|
|
|
|
|
2019-01-10 17:40:37 +03:00
|
|
|
|
2018-05-03 19:38:26 +03:00
|
|
|
def try_mecab_import():
|
|
|
|
"""Mecab is required for Japanese support, so check for it.
|
|
|
|
|
|
|
|
It it's not available blow up and explain how to fix it."""
|
|
|
|
try:
|
|
|
|
import MeCab
|
|
|
|
return MeCab
|
|
|
|
except ImportError:
|
|
|
|
raise ImportError("Japanese support requires MeCab: "
|
|
|
|
"https://github.com/SamuraiT/mecab-python3")
|
|
|
|
|
2019-01-10 17:40:37 +03:00
|
|
|
|
2018-05-03 19:38:26 +03:00
|
|
|
def resolve_pos(token):
|
|
|
|
"""If necessary, add a field to the POS tag for UD mapping.
|
|
|
|
|
|
|
|
Under Universal Dependencies, sometimes the same Unidic POS tag can
|
|
|
|
be mapped differently depending on the literal token or its context
|
2018-12-18 16:53:50 +03:00
|
|
|
in the sentence. This function adds information to the POS tag to
|
2018-05-03 19:38:26 +03:00
|
|
|
resolve ambiguous mappings.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# NOTE: This is a first take. The rules here are crude approximations.
|
|
|
|
# For many of these, full dependencies are needed to properly resolve
|
|
|
|
# PoS mappings.
|
|
|
|
|
|
|
|
if token.pos == '連体詞,*,*,*':
|
2019-01-10 17:40:37 +03:00
|
|
|
if re.match(r'[こそあど此其彼]の', token.surface):
|
2018-05-03 19:38:26 +03:00
|
|
|
return token.pos + ',DET'
|
2019-01-10 17:40:37 +03:00
|
|
|
if re.match(r'[こそあど此其彼]', token.surface):
|
2018-05-03 19:38:26 +03:00
|
|
|
return token.pos + ',PRON'
|
2019-01-10 17:40:37 +03:00
|
|
|
return token.pos + ',ADJ'
|
|
|
|
|
2018-05-03 19:38:26 +03:00
|
|
|
return token.pos
|
|
|
|
|
2019-01-10 17:40:37 +03:00
|
|
|
|
2018-05-03 19:38:26 +03:00
|
|
|
def detailed_tokens(tokenizer, text):
|
|
|
|
"""Format Mecab output into a nice data structure, based on Janome."""
|
|
|
|
|
|
|
|
node = tokenizer.parseToNode(text)
|
2019-01-10 17:40:37 +03:00
|
|
|
node = node.next # first node is beginning of sentence and empty, skip it
|
2018-05-03 19:38:26 +03:00
|
|
|
words = []
|
|
|
|
while node.posid != 0:
|
|
|
|
surface = node.surface
|
2019-01-10 17:40:37 +03:00
|
|
|
base = surface # a default value. Updated if available later.
|
2018-05-03 19:38:26 +03:00
|
|
|
parts = node.feature.split(',')
|
|
|
|
pos = ','.join(parts[0:4])
|
|
|
|
|
2018-06-19 11:20:57 +03:00
|
|
|
if len(parts) > 7:
|
2019-01-10 17:40:37 +03:00
|
|
|
# this information is only available for words in the tokenizer
|
|
|
|
# dictionary
|
2018-05-03 19:38:26 +03:00
|
|
|
base = parts[7]
|
|
|
|
|
2019-01-10 17:40:37 +03:00
|
|
|
words.append(ShortUnitWord(surface, base, pos))
|
2018-05-03 19:38:26 +03:00
|
|
|
node = node.next
|
|
|
|
return words
|
2017-10-14 14:11:39 +03:00
|
|
|
|
2019-01-10 17:40:37 +03:00
|
|
|
|
|
|
|
class JapaneseTokenizer(DummyTokenizer):
|
2017-10-14 14:11:39 +03:00
|
|
|
def __init__(self, cls, nlp=None):
|
|
|
|
self.vocab = nlp.vocab if nlp is not None else cls.create_vocab(nlp)
|
2018-05-03 19:38:26 +03:00
|
|
|
|
2019-01-10 17:40:37 +03:00
|
|
|
self.tokenizer = try_mecab_import().Tagger()
|
2018-12-18 16:53:50 +03:00
|
|
|
self.tokenizer.parseToNode('') # see #2901
|
2017-10-14 14:11:39 +03:00
|
|
|
|
|
|
|
def __call__(self, text):
|
2018-05-03 19:38:26 +03:00
|
|
|
dtokens = detailed_tokens(self.tokenizer, text)
|
2019-01-10 17:40:37 +03:00
|
|
|
|
2018-05-03 19:38:26 +03:00
|
|
|
words = [x.surface for x in dtokens]
|
2019-01-10 17:40:37 +03:00
|
|
|
spaces = [False] * len(words)
|
|
|
|
doc = Doc(self.vocab, words=words, spaces=spaces)
|
|
|
|
|
2018-05-03 19:38:26 +03:00
|
|
|
for token, dtoken in zip(doc, dtokens):
|
|
|
|
token._.mecab_tag = dtoken.pos
|
|
|
|
token.tag_ = resolve_pos(dtoken)
|
2018-07-13 11:55:14 +03:00
|
|
|
token.lemma_ = dtoken.lemma
|
2017-10-14 14:11:39 +03:00
|
|
|
|
2019-01-10 17:40:37 +03:00
|
|
|
return doc
|
2017-11-15 14:44:02 +03:00
|
|
|
|
|
|
|
|
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)
|
2019-01-10 17:40:37 +03:00
|
|
|
lex_attr_getters[LANG] = lambda _text: 'ja'
|
|
|
|
|
2018-05-03 19:38:26 +03:00
|
|
|
tag_map = TAG_MAP
|
2017-11-15 14:44:02 +03:00
|
|
|
|
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
|
|
|
|
2019-01-10 17:40:37 +03:00
|
|
|
|
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
|
|
|
|
2019-01-10 17:40:37 +03:00
|
|
|
|
2017-05-08 16:50:46 +03:00
|
|
|
__all__ = ['Japanese']
|