2020-07-22 11:42:59 +00:00
|
|
|
from thinc.api import Config
|
|
|
|
|
2018-03-29 10:19:51 +00:00
|
|
|
from ...language import Language
|
|
|
|
from ...tokens import Doc
|
|
|
|
from .stop_words import STOP_WORDS
|
2020-07-22 11:42:59 +00:00
|
|
|
from ...util import DummyTokenizer, registry
|
2018-03-29 10:19:51 +00:00
|
|
|
from .lex_attrs import LEX_ATTRS
|
|
|
|
|
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
DEFAULT_CONFIG = """
|
|
|
|
[nlp]
|
2018-03-29 10:19:51 +00:00
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
[nlp.tokenizer]
|
2020-07-24 12:50:26 +00:00
|
|
|
@tokenizers = "spacy.vi.VietnameseTokenizer"
|
2020-07-22 11:42:59 +00:00
|
|
|
use_pyvi = true
|
|
|
|
"""
|
💫 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
|
|
|
|
2018-03-29 10:19:51 +00:00
|
|
|
|
2020-07-24 12:50:26 +00:00
|
|
|
@registry.tokenizers("spacy.vi.VietnameseTokenizer")
|
2020-07-22 11:42:59 +00:00
|
|
|
def create_vietnamese_tokenizer(use_pyvi: bool = True,):
|
|
|
|
def vietnamese_tokenizer_factory(nlp):
|
|
|
|
return VietnameseTokenizer(nlp, use_pyvi=use_pyvi)
|
|
|
|
|
|
|
|
return vietnamese_tokenizer_factory
|
|
|
|
|
|
|
|
|
|
|
|
class VietnameseTokenizer(DummyTokenizer):
|
|
|
|
def __init__(self, nlp: Language, use_pyvi: bool = False):
|
|
|
|
self.vocab = nlp.vocab
|
|
|
|
self.use_pyvi = use_pyvi
|
|
|
|
if self.use_pyvi:
|
2018-03-29 10:19:51 +00:00
|
|
|
try:
|
|
|
|
from pyvi import ViTokenizer
|
2020-07-22 11:42:59 +00:00
|
|
|
|
|
|
|
self.ViTokenizer = ViTokenizer
|
2018-03-29 10:19:51 +00:00
|
|
|
except ImportError:
|
💫 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
|
|
|
msg = (
|
2020-07-22 11:42:59 +00:00
|
|
|
"Pyvi not installed. Either set use_pyvi = False, "
|
💫 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
|
|
|
"or install it https://pypi.python.org/pypi/pyvi"
|
|
|
|
)
|
2020-08-05 21:53:21 +00:00
|
|
|
raise ImportError(msg) from None
|
2020-07-22 11:42:59 +00:00
|
|
|
|
|
|
|
def __call__(self, text: str) -> Doc:
|
|
|
|
if self.use_pyvi:
|
|
|
|
words, spaces = self.ViTokenizer.spacy_tokenize(text)
|
2018-03-29 10:19:51 +00:00
|
|
|
return Doc(self.vocab, words=words, spaces=spaces)
|
|
|
|
else:
|
|
|
|
words = []
|
|
|
|
spaces = []
|
|
|
|
for token in self.tokenizer(text):
|
|
|
|
words.extend(list(token.text))
|
💫 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
|
|
|
spaces.extend([False] * len(token.text))
|
2018-03-29 10:19:51 +00:00
|
|
|
spaces[-1] = bool(token.whitespace_)
|
|
|
|
return Doc(self.vocab, words=words, spaces=spaces)
|
|
|
|
|
💫 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
|
|
|
|
2020-07-24 12:50:26 +00:00
|
|
|
class VietnameseDefaults(Language.Defaults):
|
|
|
|
config = Config().from_str(DEFAULT_CONFIG)
|
|
|
|
lex_attr_getters = LEX_ATTRS
|
|
|
|
stop_words = STOP_WORDS
|
|
|
|
|
|
|
|
|
2020-07-22 11:42:59 +00:00
|
|
|
class Vietnamese(Language):
|
|
|
|
lang = "vi"
|
2020-07-24 12:50:26 +00:00
|
|
|
Defaults = VietnameseDefaults
|
2020-07-22 11:42:59 +00:00
|
|
|
|
|
|
|
|
💫 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
|
|
|
__all__ = ["Vietnamese"]
|