2017-05-08 13:54:36 +00:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-12-28 09:13:58 +00:00
|
|
|
from ...attrs import LANG
|
2017-05-08 20:29:04 +00:00
|
|
|
from ...language import Language
|
|
|
|
from ...tokens import Doc
|
2019-11-11 13:23:21 +00:00
|
|
|
from ...util import DummyTokenizer
|
2018-08-07 09:26:31 +00:00
|
|
|
from ..tokenizer_exceptions import BASE_EXCEPTIONS
|
2019-11-11 13:23:21 +00:00
|
|
|
from .lex_attrs import LEX_ATTRS
|
💫 Tidy up and auto-format .py files (#2983)
<!--- Provide a general summary of your changes in the title. -->
## Description
- [x] Use [`black`](https://github.com/ambv/black) to auto-format all `.py` files.
- [x] Update flake8 config to exclude very large files (lemmatization tables etc.)
- [x] Update code to be compatible with flake8 rules
- [x] Fix various small bugs, inconsistencies and messy stuff in the language data
- [x] Update docs to explain new code style (`black`, `flake8`, when to use `# fmt: off` and `# fmt: on` and what `# noqa` means)
Once #2932 is merged, which auto-formats and tidies up the CLI, we'll be able to run `flake8 spacy` actually get meaningful results.
At the moment, the code style and linting isn't applied automatically, but I'm hoping that the new [GitHub Actions](https://github.com/features/actions) will let us auto-format pull requests and post comments with relevant linting information.
### Types of change
enhancement, code style
## Checklist
<!--- Before you submit the PR, go over this checklist and make sure you can
tick off all the boxes. [] -> [x] -->
- [x] I have submitted the spaCy Contributor Agreement.
- [x] I ran the tests, and all new and existing tests passed.
- [x] My changes don't require a change to the documentation, or if they do, I've added all required information.
2018-11-30 16:03:03 +00:00
|
|
|
from .stop_words import STOP_WORDS
|
2019-08-12 08:37:48 +00:00
|
|
|
from .tag_map import TAG_MAP
|
2016-04-24 16:44:24 +00:00
|
|
|
|
2019-08-18 13:09:16 +00:00
|
|
|
|
2019-11-11 13:23:21 +00:00
|
|
|
def try_jieba_import(use_jieba):
|
|
|
|
try:
|
|
|
|
import jieba
|
2019-11-20 12:15:24 +00:00
|
|
|
|
2019-11-11 13:23:21 +00:00
|
|
|
return jieba
|
|
|
|
except ImportError:
|
|
|
|
if use_jieba:
|
|
|
|
msg = (
|
|
|
|
"Jieba not installed. Either set Chinese.use_jieba = False, "
|
|
|
|
"or install it https://github.com/fxsjy/jieba"
|
|
|
|
)
|
|
|
|
raise ImportError(msg)
|
|
|
|
|
|
|
|
|
|
|
|
class ChineseTokenizer(DummyTokenizer):
|
|
|
|
def __init__(self, cls, nlp=None):
|
|
|
|
self.vocab = nlp.vocab if nlp is not None else cls.create_vocab(nlp)
|
|
|
|
self.use_jieba = cls.use_jieba
|
|
|
|
self.jieba_seg = try_jieba_import(self.use_jieba)
|
|
|
|
self.tokenizer = Language.Defaults().create_tokenizer(nlp)
|
|
|
|
|
|
|
|
def __call__(self, text):
|
|
|
|
# use jieba
|
|
|
|
if self.use_jieba:
|
2019-11-20 12:15:24 +00:00
|
|
|
jieba_words = list(
|
|
|
|
[x for x in self.jieba_seg.cut(text, cut_all=False) if x]
|
|
|
|
)
|
2019-11-11 13:23:21 +00:00
|
|
|
words = [jieba_words[0]]
|
|
|
|
spaces = [False]
|
|
|
|
for i in range(1, len(jieba_words)):
|
|
|
|
word = jieba_words[i]
|
|
|
|
if word.isspace():
|
|
|
|
# second token in adjacent whitespace following a
|
|
|
|
# non-space token
|
|
|
|
if spaces[-1]:
|
|
|
|
words.append(word)
|
|
|
|
spaces.append(False)
|
|
|
|
# first space token following non-space token
|
|
|
|
elif word == " " and not words[-1].isspace():
|
|
|
|
spaces[-1] = True
|
|
|
|
# token is non-space whitespace or any whitespace following
|
|
|
|
# a whitespace token
|
|
|
|
else:
|
|
|
|
# extend previous whitespace token with more whitespace
|
|
|
|
if words[-1].isspace():
|
|
|
|
words[-1] += word
|
|
|
|
# otherwise it's a new whitespace token
|
|
|
|
else:
|
|
|
|
words.append(word)
|
|
|
|
spaces.append(False)
|
|
|
|
else:
|
|
|
|
words.append(word)
|
|
|
|
spaces.append(False)
|
|
|
|
return Doc(self.vocab, words=words, spaces=spaces)
|
|
|
|
|
|
|
|
# split into individual characters
|
|
|
|
words = []
|
|
|
|
spaces = []
|
|
|
|
for token in self.tokenizer(text):
|
|
|
|
if token.text.isspace():
|
|
|
|
words.append(token.text)
|
|
|
|
spaces.append(False)
|
|
|
|
else:
|
|
|
|
words.extend(list(token.text))
|
|
|
|
spaces.extend([False] * len(token.text))
|
|
|
|
spaces[-1] = bool(token.whitespace_)
|
|
|
|
return Doc(self.vocab, words=words, spaces=spaces)
|
|
|
|
|
|
|
|
|
2017-12-28 09:13:58 +00:00
|
|
|
class ChineseDefaults(Language.Defaults):
|
|
|
|
lex_attr_getters = dict(Language.Defaults.lex_attr_getters)
|
2019-11-11 13:23:21 +00:00
|
|
|
lex_attr_getters.update(LEX_ATTRS)
|
💫 Tidy up and auto-format .py files (#2983)
<!--- Provide a general summary of your changes in the title. -->
## Description
- [x] Use [`black`](https://github.com/ambv/black) to auto-format all `.py` files.
- [x] Update flake8 config to exclude very large files (lemmatization tables etc.)
- [x] Update code to be compatible with flake8 rules
- [x] Fix various small bugs, inconsistencies and messy stuff in the language data
- [x] Update docs to explain new code style (`black`, `flake8`, when to use `# fmt: off` and `# fmt: on` and what `# noqa` means)
Once #2932 is merged, which auto-formats and tidies up the CLI, we'll be able to run `flake8 spacy` actually get meaningful results.
At the moment, the code style and linting isn't applied automatically, but I'm hoping that the new [GitHub Actions](https://github.com/features/actions) will let us auto-format pull requests and post comments with relevant linting information.
### Types of change
enhancement, code style
## Checklist
<!--- Before you submit the PR, go over this checklist and make sure you can
tick off all the boxes. [] -> [x] -->
- [x] I have submitted the spaCy Contributor Agreement.
- [x] I ran the tests, and all new and existing tests passed.
- [x] My changes don't require a change to the documentation, or if they do, I've added all required information.
2018-11-30 16:03:03 +00:00
|
|
|
lex_attr_getters[LANG] = lambda text: "zh"
|
|
|
|
tokenizer_exceptions = BASE_EXCEPTIONS
|
2018-07-25 00:47:23 +00:00
|
|
|
stop_words = STOP_WORDS
|
2019-08-12 08:37:48 +00:00
|
|
|
tag_map = TAG_MAP
|
2019-03-11 14:23:20 +00:00
|
|
|
writing_system = {"direction": "ltr", "has_case": False, "has_letters": False}
|
2019-11-11 13:23:21 +00:00
|
|
|
use_jieba = True
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_tokenizer(cls, nlp=None):
|
|
|
|
return ChineseTokenizer(cls, nlp)
|
2017-12-28 09:13:58 +00:00
|
|
|
|
2019-03-11 16:10:50 +00:00
|
|
|
|
2016-04-24 16:44:24 +00:00
|
|
|
class Chinese(Language):
|
💫 Tidy up and auto-format .py files (#2983)
<!--- Provide a general summary of your changes in the title. -->
## Description
- [x] Use [`black`](https://github.com/ambv/black) to auto-format all `.py` files.
- [x] Update flake8 config to exclude very large files (lemmatization tables etc.)
- [x] Update code to be compatible with flake8 rules
- [x] Fix various small bugs, inconsistencies and messy stuff in the language data
- [x] Update docs to explain new code style (`black`, `flake8`, when to use `# fmt: off` and `# fmt: on` and what `# noqa` means)
Once #2932 is merged, which auto-formats and tidies up the CLI, we'll be able to run `flake8 spacy` actually get meaningful results.
At the moment, the code style and linting isn't applied automatically, but I'm hoping that the new [GitHub Actions](https://github.com/features/actions) will let us auto-format pull requests and post comments with relevant linting information.
### Types of change
enhancement, code style
## Checklist
<!--- Before you submit the PR, go over this checklist and make sure you can
tick off all the boxes. [] -> [x] -->
- [x] I have submitted the spaCy Contributor Agreement.
- [x] I ran the tests, and all new and existing tests passed.
- [x] My changes don't require a change to the documentation, or if they do, I've added all required information.
2018-11-30 16:03:03 +00:00
|
|
|
lang = "zh"
|
2017-12-28 09:13:58 +00:00
|
|
|
Defaults = ChineseDefaults # override defaults
|
2016-05-05 09:39:12 +00:00
|
|
|
|
2016-11-02 18:57:38 +00:00
|
|
|
def make_doc(self, text):
|
2019-11-11 13:23:21 +00:00
|
|
|
return self.tokenizer(text)
|
2017-05-03 09:01:42 +00:00
|
|
|
|
|
|
|
|
2019-08-18 13:09:16 +00:00
|
|
|
__all__ = ["Chinese"]
|