spaCy/spacy/lang/ja/__init__.py

126 lines
4.0 KiB
Python
Raw Normal View History

import re
from collections import namedtuple
from .stop_words import STOP_WORDS
from .tag_map import TAG_MAP
from ...attrs import LANG
from ...language import Language
from ...tokens import Doc
from ...compat import copy_reg
from ...util import DummyTokenizer
# Handling for multiple spaces in a row is somewhat awkward, this simplifies
# the flow by creating a dummy with the same interface.
DummyNode = namedtuple("DummyNode", ["surface", "pos", "feature"])
DummyNodeFeatures = namedtuple("DummyNodeFeatures", ["lemma"])
2019-12-21 21:04:17 +03:00
DummySpace = DummyNode(" ", " ", DummyNodeFeatures(" "))
def try_fugashi_import():
"""Fugashi is required for Japanese support, so check for it.
It it's not available blow up and explain how to fix it."""
try:
import fugashi
return fugashi
except ImportError:
raise ImportError(
2019-12-21 21:04:17 +03:00
"Japanese support requires Fugashi: " "https://github.com/polm/fugashi"
)
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
in the sentence. This function adds information to the POS tag to
resolve ambiguous mappings.
"""
# this is only used for consecutive ascii spaces
if token.surface == " ":
2019-09-14 13:58:06 +03:00
return "空白"
2019-02-08 16:14:49 +03:00
# TODO: 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-02-07 22:54:07 +03:00
if re.match(r"[こそあど此其彼]の", token.surface):
return token.pos + ",DET"
2019-02-07 22:54:07 +03:00
if re.match(r"[こそあど此其彼]", token.surface):
return token.pos + ",PRON"
2019-02-07 22:54:07 +03:00
return token.pos + ",ADJ"
return token.pos
2019-12-21 21:04:17 +03:00
def get_words_and_spaces(tokenizer, text):
"""Get the individual tokens that make up the sentence and handle white space.
Japanese doesn't usually use white space, and MeCab's handling of it for
multiple spaces in a row is somewhat awkward.
"""
2019-12-21 21:04:17 +03:00
tokens = tokenizer.parseToNodeList(text)
words = []
spaces = []
for token in tokens:
# If there's more than one space, spaces after the first become tokens
for ii in range(len(token.white_space) - 1):
words.append(DummySpace)
spaces.append(False)
words.append(token)
spaces.append(bool(token.white_space))
return words, spaces
2017-10-14 14:11:39 +03:00
2019-12-21 21:04:17 +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)
self.tokenizer = try_fugashi_import().Tagger()
self.tokenizer.parseToNodeList("") # see #2901
2017-10-14 14:11:39 +03:00
def __call__(self, text):
dtokens, spaces = get_words_and_spaces(self.tokenizer, text)
words = [x.surface for x in dtokens]
doc = Doc(self.vocab, words=words, spaces=spaces)
unidic_tags = []
for token, dtoken in zip(doc, dtokens):
unidic_tags.append(dtoken.pos)
token.tag_ = resolve_pos(dtoken)
# if there's no lemma info (it's an unk) just use the surface
token.lemma_ = dtoken.feature.lemma or dtoken.surface
doc.user_data["unidic_tags"] = unidic_tags
return doc
2017-10-14 14:11:39 +03:00
class JapaneseDefaults(Language.Defaults):
lex_attr_getters = dict(Language.Defaults.lex_attr_getters)
2019-02-07 22:54:07 +03:00
lex_attr_getters[LANG] = lambda _text: "ja"
stop_words = STOP_WORDS
tag_map = TAG_MAP
writing_system = {"direction": "ltr", "has_case": False, "has_letters": False}
2017-10-14 14:11:39 +03:00
@classmethod
def create_tokenizer(cls, nlp=None):
2019-02-08 16:14:49 +03:00
return JapaneseTokenizer(cls, nlp)
2017-05-03 07:56:21 +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):
return self.tokenizer(text)
2017-05-03 12:07:29 +03:00
def pickle_japanese(instance):
return Japanese, tuple()
copy_reg.pickle(Japanese, pickle_japanese)
__all__ = ["Japanese"]