2017-04-15 13:05:47 +03:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import absolute_import, unicode_literals
|
2015-08-27 10:16:11 +03:00
|
|
|
|
2017-05-25 04:10:54 +03:00
|
|
|
import random
|
2017-07-25 19:57:59 +03:00
|
|
|
import itertools
|
2020-04-28 14:37:37 +03:00
|
|
|
import warnings
|
2020-03-03 15:58:22 +03:00
|
|
|
|
|
|
|
from thinc.extra import load_nlp
|
|
|
|
|
2019-10-08 13:20:55 +03:00
|
|
|
from spacy.util import minibatch
|
2017-10-16 20:22:40 +03:00
|
|
|
import weakref
|
2017-10-17 19:18:10 +03:00
|
|
|
import functools
|
2017-10-27 22:07:59 +03:00
|
|
|
from collections import OrderedDict
|
|
|
|
from contextlib import contextmanager
|
💫 Better support for semi-supervised learning (#3035)
The new spacy pretrain command implemented BERT/ULMFit/etc-like transfer learning, using our Language Modelling with Approximate Outputs version of BERT's cloze task. Pretraining is convenient, but in some ways it's a bit of a strange solution. All we're doing is initialising the weights. At the same time, we're putting a lot of work into our optimisation so that it's less sensitive to initial conditions, and more likely to find good optima. I discuss this a bit in the pseudo-rehearsal blog post: https://explosion.ai/blog/pseudo-rehearsal-catastrophic-forgetting
Support semi-supervised learning in spacy train
One obvious way to improve these pretraining methods is to do multi-task learning, instead of just transfer learning. This has been shown to work very well: https://arxiv.org/pdf/1809.08370.pdf . This patch makes it easy to do this sort of thing.
Add a new argument to spacy train, --raw-text. This takes a jsonl file with unlabelled data that can be used in arbitrary ways to do semi-supervised learning.
Add a new method to the Language class and to pipeline components, .rehearse(). This is like .update(), but doesn't expect GoldParse objects. It takes a batch of Doc objects, and performs an update on some semi-supervised objective.
Move the BERT-LMAO objective out from spacy/cli/pretrain.py into spacy/_ml.py, so we can create a new pipeline component, ClozeMultitask. This can be specified as a parser or NER multitask in the spacy train command. Example usage:
python -m spacy train en ./tmp ~/data/en-core-web/train/nw.json ~/data/en-core-web/dev/nw.json --pipeline parser --raw-textt ~/data/unlabelled/reddit-100k.jsonl --vectors en_vectors_web_lg --parser-multitasks cloze
Implement rehearsal methods for pipeline components
The new --raw-text argument and nlp.rehearse() method also gives us a good place to implement the the idea in the pseudo-rehearsal blog post in the parser. This works as follows:
Add a new nlp.resume_training() method. This allocates copies of pre-trained models in the pipeline, setting things up for the rehearsal updates. It also returns an optimizer object. This also greatly reduces confusion around the nlp.begin_training() method, which randomises the weights, making it not suitable for adding new labels or otherwise fine-tuning a pre-trained model.
Implement rehearsal updates on the Parser class, making it available for the dependency parser and NER. During rehearsal, the initial model is used to supervise the model being trained. The current model is asked to match the predictions of the initial model on some data. This minimises catastrophic forgetting, by keeping the model's predictions close to the original. See the blog post for details.
Implement rehearsal updates for tagger
Implement rehearsal updates for text categoriz
2018-12-10 18:25:33 +03:00
|
|
|
from copy import copy, deepcopy
|
2017-10-27 22:07:59 +03:00
|
|
|
from thinc.neural import Model
|
💫 Replace ujson, msgpack and dill/pickle/cloudpickle with srsly (#3003)
Remove hacks and wrappers, keep code in sync across our libraries and move spaCy a few steps closer to only depending on packages with binary wheels 🎉
See here: https://github.com/explosion/srsly
Serialization is hard, especially across Python versions and multiple platforms. After dealing with many subtle bugs over the years (encodings, locales, large files) our libraries like spaCy and Prodigy have steadily grown a number of utility functions to wrap the multiple serialization formats we need to support (especially json, msgpack and pickle). These wrapping functions ended up duplicated across our codebases, so we wanted to put them in one place.
At the same time, we noticed that having a lot of small dependencies was making maintainence harder, and making installation slower. To solve this, we've made srsly standalone, by including the component packages directly within it. This way we can provide all the serialization utilities we need in a single binary wheel.
srsly currently includes forks of the following packages:
ujson
msgpack
msgpack-numpy
cloudpickle
* WIP: replace json/ujson with srsly
* Replace ujson in examples
Use regular json instead of srsly to make code easier to read and follow
* Update requirements
* Fix imports
* Fix typos
* Replace msgpack with srsly
* Fix warning
2018-12-03 03:28:22 +03:00
|
|
|
import srsly
|
2019-10-08 13:20:55 +03:00
|
|
|
import multiprocessing as mp
|
|
|
|
from itertools import chain, cycle
|
2017-05-18 12:25:19 +03:00
|
|
|
|
2015-08-26 20:16:09 +03:00
|
|
|
from .tokenizer import Tokenizer
|
2020-02-12 13:50:42 +03:00
|
|
|
from .tokens.underscore import Underscore
|
2015-08-26 20:16:09 +03:00
|
|
|
from .vocab import Vocab
|
2016-09-25 16:37:33 +03:00
|
|
|
from .lemmatizer import Lemmatizer
|
2019-08-22 15:21:32 +03:00
|
|
|
from .lookups import Lookups
|
2019-10-27 15:35:49 +03:00
|
|
|
from .analysis import analyze_pipes, analyze_all_pipes, validate_attrs
|
|
|
|
from .compat import izip, basestring_, is_python2, class_types
|
2017-11-07 00:07:38 +03:00
|
|
|
from .gold import GoldParse
|
2017-10-07 01:26:05 +03:00
|
|
|
from .scorer import Scorer
|
2017-11-06 17:06:27 +03:00
|
|
|
from ._ml import link_vectors_to_models, create_default_optimizer
|
2020-05-19 16:59:14 +03:00
|
|
|
from .attrs import IS_STOP, LANG, NORM
|
2017-10-27 15:40:14 +03:00
|
|
|
from .lang.punctuation import TOKENIZER_PREFIXES, TOKENIZER_SUFFIXES
|
|
|
|
from .lang.punctuation import TOKENIZER_INFIXES
|
2017-05-09 00:58:31 +03:00
|
|
|
from .lang.tokenizer_exceptions import TOKEN_MATCH
|
2020-05-19 16:59:14 +03:00
|
|
|
from .lang.norm_exceptions import BASE_NORMS
|
2017-05-09 00:58:31 +03:00
|
|
|
from .lang.tag_map import TAG_MAP
|
2019-10-08 13:20:55 +03:00
|
|
|
from .tokens import Doc
|
2017-10-17 19:18:10 +03:00
|
|
|
from .lang.lex_attrs import LEX_ATTRS, is_stop
|
2020-04-28 14:37:37 +03:00
|
|
|
from .errors import Errors, Warnings
|
2017-04-15 13:05:47 +03:00
|
|
|
from . import util
|
2017-10-07 01:26:05 +03:00
|
|
|
from . import about
|
2016-10-09 13:24:24 +03:00
|
|
|
|
2015-08-27 10:16:11 +03:00
|
|
|
|
2019-10-27 15:35:49 +03:00
|
|
|
ENABLE_PIPELINE_ANALYSIS = False
|
|
|
|
|
|
|
|
|
2016-09-24 21:26:17 +03:00
|
|
|
class BaseDefaults(object):
|
2016-10-18 17:18:25 +03:00
|
|
|
@classmethod
|
2019-08-22 15:21:32 +03:00
|
|
|
def create_lemmatizer(cls, nlp=None, lookups=None):
|
2019-09-08 19:08:09 +03:00
|
|
|
if lookups is None:
|
|
|
|
lookups = cls.create_lookups(nlp=nlp)
|
2019-10-01 22:36:04 +03:00
|
|
|
return Lemmatizer(lookups=lookups)
|
2019-08-22 15:21:32 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_lookups(cls, nlp=None):
|
2019-10-01 01:01:27 +03:00
|
|
|
root = util.get_module_path(cls)
|
|
|
|
filenames = {name: root / filename for name, filename in cls.resources}
|
|
|
|
if LANG in cls.lex_attr_getters:
|
|
|
|
lang = cls.lex_attr_getters[LANG](None)
|
2019-11-07 13:45:22 +03:00
|
|
|
if lang in util.registry.lookups:
|
|
|
|
filenames.update(util.registry.lookups.get(lang))
|
2019-08-22 15:21:32 +03:00
|
|
|
lookups = Lookups()
|
2019-10-01 01:01:27 +03:00
|
|
|
for name, filename in filenames.items():
|
|
|
|
data = util.load_language_data(filename)
|
2019-08-22 15:21:32 +03:00
|
|
|
lookups.add_table(name, data)
|
|
|
|
return lookups
|
2016-10-18 17:18:25 +03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create_vocab(cls, nlp=None):
|
2019-08-22 15:21:32 +03:00
|
|
|
lookups = cls.create_lookups(nlp)
|
|
|
|
lemmatizer = cls.create_lemmatizer(nlp, lookups=lookups)
|
2017-05-16 12:21:59 +03:00
|
|
|
lex_attr_getters = dict(cls.lex_attr_getters)
|
|
|
|
# This is messy, but it's the minimal working fix to Issue #639.
|
💫 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 19:03:03 +03:00
|
|
|
lex_attr_getters[IS_STOP] = functools.partial(is_stop, stops=cls.stop_words)
|
|
|
|
vocab = Vocab(
|
|
|
|
lex_attr_getters=lex_attr_getters,
|
|
|
|
tag_map=cls.tag_map,
|
|
|
|
lemmatizer=lemmatizer,
|
2019-08-22 15:21:32 +03:00
|
|
|
lookups=lookups,
|
💫 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 19:03:03 +03:00
|
|
|
)
|
2020-05-19 16:59:14 +03:00
|
|
|
vocab.lex_attr_getters[NORM] = util.add_lookups(
|
|
|
|
vocab.lex_attr_getters.get(NORM, LEX_ATTRS[NORM]), BASE_NORMS, vocab.lookups.get_table("lexeme_norm")
|
|
|
|
)
|
2017-03-15 17:24:40 +03:00
|
|
|
for tag_str, exc in cls.morph_rules.items():
|
|
|
|
for orth_str, attrs in exc.items():
|
|
|
|
vocab.morphology.add_special_case(tag_str, orth_str, attrs)
|
|
|
|
return vocab
|
2016-12-18 18:54:52 +03:00
|
|
|
|
2016-10-18 17:18:25 +03:00
|
|
|
@classmethod
|
|
|
|
def create_tokenizer(cls, nlp=None):
|
|
|
|
rules = cls.tokenizer_exceptions
|
2017-05-16 12:21:59 +03:00
|
|
|
token_match = cls.token_match
|
💫 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 19:03:03 +03:00
|
|
|
prefix_search = (
|
|
|
|
util.compile_prefix_regex(cls.prefixes).search if cls.prefixes else None
|
|
|
|
)
|
|
|
|
suffix_search = (
|
|
|
|
util.compile_suffix_regex(cls.suffixes).search if cls.suffixes else None
|
|
|
|
)
|
|
|
|
infix_finditer = (
|
|
|
|
util.compile_infix_regex(cls.infixes).finditer if cls.infixes else None
|
|
|
|
)
|
2016-10-18 17:18:25 +03:00
|
|
|
vocab = nlp.vocab if nlp is not None else cls.create_vocab(nlp)
|
💫 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 19:03:03 +03:00
|
|
|
return Tokenizer(
|
|
|
|
vocab,
|
|
|
|
rules=rules,
|
|
|
|
prefix_search=prefix_search,
|
|
|
|
suffix_search=suffix_search,
|
|
|
|
infix_finditer=infix_finditer,
|
|
|
|
token_match=token_match,
|
|
|
|
)
|
|
|
|
|
|
|
|
pipe_names = ["tagger", "parser", "ner"]
|
2017-05-09 00:58:31 +03:00
|
|
|
token_match = TOKEN_MATCH
|
|
|
|
prefixes = tuple(TOKENIZER_PREFIXES)
|
|
|
|
suffixes = tuple(TOKENIZER_SUFFIXES)
|
|
|
|
infixes = tuple(TOKENIZER_INFIXES)
|
|
|
|
tag_map = dict(TAG_MAP)
|
2016-10-09 13:24:24 +03:00
|
|
|
tokenizer_exceptions = {}
|
2016-09-24 21:26:17 +03:00
|
|
|
stop_words = set()
|
2017-03-15 17:24:40 +03:00
|
|
|
morph_rules = {}
|
2017-05-09 01:58:10 +03:00
|
|
|
lex_attr_getters = LEX_ATTRS
|
2017-06-04 22:53:39 +03:00
|
|
|
syntax_iterators = {}
|
2019-08-22 15:21:32 +03:00
|
|
|
resources = {}
|
2019-03-11 17:23:20 +03:00
|
|
|
writing_system = {"direction": "ltr", "has_case": True, "has_letters": True}
|
2019-08-28 10:14:20 +03:00
|
|
|
single_orth_variants = []
|
|
|
|
paired_orth_variants = []
|
2015-09-14 10:48:51 +03:00
|
|
|
|
2015-08-26 20:16:09 +03:00
|
|
|
|
2016-09-24 15:08:53 +03:00
|
|
|
class Language(object):
|
2017-05-19 00:57:38 +03:00
|
|
|
"""A text-processing pipeline. Usually you'll load this once per process,
|
|
|
|
and pass the instance around your application.
|
2017-05-19 19:47:24 +03:00
|
|
|
|
|
|
|
Defaults (class): Settings, data and factory methods for creating the `nlp`
|
|
|
|
object and processing pipeline.
|
|
|
|
lang (unicode): Two-letter language ID, i.e. ISO code.
|
💫 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 19:03:03 +03:00
|
|
|
|
2019-03-08 13:42:26 +03:00
|
|
|
DOCS: https://spacy.io/api/language
|
|
|
|
"""
|
2019-03-11 01:36:47 +03:00
|
|
|
|
2016-09-24 21:26:17 +03:00
|
|
|
Defaults = BaseDefaults
|
2016-09-24 15:08:53 +03:00
|
|
|
lang = None
|
2015-08-25 16:37:17 +03:00
|
|
|
|
2019-10-28 14:43:55 +03:00
|
|
|
factories = {"tokenizer": lambda nlp: nlp.Defaults.create_tokenizer(nlp)}
|
2017-10-07 01:25:54 +03: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 19:03:03 +03:00
|
|
|
def __init__(
|
|
|
|
self, vocab=True, make_doc=True, max_length=10 ** 6, meta={}, **kwargs
|
|
|
|
):
|
2017-05-19 00:57:38 +03:00
|
|
|
"""Initialise a Language object.
|
|
|
|
|
|
|
|
vocab (Vocab): A `Vocab` object. If `True`, a vocab is created via
|
|
|
|
`Language.Defaults.create_vocab`.
|
2017-05-21 14:17:40 +03:00
|
|
|
make_doc (callable): A function that takes text and returns a `Doc`
|
2017-05-19 00:57:38 +03:00
|
|
|
object. Usually a `Tokenizer`.
|
|
|
|
meta (dict): Custom meta data for the Language class. Is written to by
|
|
|
|
models to add model meta data.
|
2018-03-29 22:45:26 +03:00
|
|
|
max_length (int) :
|
|
|
|
Maximum number of characters in a single text. The current v2 models
|
|
|
|
may run out memory on extremely long texts, due to large internal
|
|
|
|
allocations. You should segment these texts into meaningful units,
|
|
|
|
e.g. paragraphs, subsections etc, before passing them to spaCy.
|
|
|
|
Default maximum length is 1,000,000 characters (1mb). As a rule of
|
|
|
|
thumb, if all pipeline components are enabled, spaCy's default
|
|
|
|
models currently requires roughly 1GB of temporary memory per
|
|
|
|
100,000 characters in one text.
|
2017-05-19 00:57:38 +03:00
|
|
|
RETURNS (Language): The newly constructed object.
|
|
|
|
"""
|
2019-11-07 13:45:22 +03:00
|
|
|
user_factories = util.registry.factories.get_all()
|
2018-05-22 19:29:45 +03:00
|
|
|
self.factories.update(user_factories)
|
2017-07-23 01:50:18 +03:00
|
|
|
self._meta = dict(meta)
|
2017-10-25 12:57:43 +03:00
|
|
|
self._path = None
|
2017-05-16 12:21:59 +03:00
|
|
|
if vocab is True:
|
|
|
|
factory = self.Defaults.create_vocab
|
💫 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 19:03:03 +03:00
|
|
|
vocab = factory(self, **meta.get("vocab", {}))
|
2018-03-28 17:02:59 +03:00
|
|
|
if vocab.vectors.name is None:
|
💫 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 19:03:03 +03:00
|
|
|
vocab.vectors.name = meta.get("vectors", {}).get("name")
|
2019-08-01 18:13:01 +03:00
|
|
|
else:
|
|
|
|
if (self.lang and vocab.lang) and (self.lang != vocab.lang):
|
|
|
|
raise ValueError(Errors.E150.format(nlp=self.lang, vocab=vocab.lang))
|
2017-05-16 12:21:59 +03:00
|
|
|
self.vocab = vocab
|
|
|
|
if make_doc is True:
|
|
|
|
factory = self.Defaults.create_tokenizer
|
💫 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 19:03:03 +03:00
|
|
|
make_doc = factory(self, **meta.get("tokenizer", {}))
|
2017-05-29 16:40:45 +03:00
|
|
|
self.tokenizer = make_doc
|
2017-10-07 01:25:54 +03:00
|
|
|
self.pipeline = []
|
2018-03-29 22:45:26 +03:00
|
|
|
self.max_length = max_length
|
2017-08-20 15:42:07 +03:00
|
|
|
self._optimizer = None
|
2015-10-12 11:33:11 +03:00
|
|
|
|
2017-10-25 12:57:43 +03:00
|
|
|
@property
|
|
|
|
def path(self):
|
|
|
|
return self._path
|
|
|
|
|
2017-07-23 01:50:18 +03:00
|
|
|
@property
|
|
|
|
def meta(self):
|
2019-08-01 18:13:01 +03:00
|
|
|
if self.vocab.lang:
|
|
|
|
self._meta.setdefault("lang", self.vocab.lang)
|
|
|
|
else:
|
|
|
|
self._meta.setdefault("lang", self.lang)
|
💫 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 19:03:03 +03:00
|
|
|
self._meta.setdefault("name", "model")
|
|
|
|
self._meta.setdefault("version", "0.0.0")
|
|
|
|
self._meta.setdefault("spacy_version", ">={}".format(about.__version__))
|
|
|
|
self._meta.setdefault("description", "")
|
|
|
|
self._meta.setdefault("author", "")
|
|
|
|
self._meta.setdefault("email", "")
|
|
|
|
self._meta.setdefault("url", "")
|
|
|
|
self._meta.setdefault("license", "")
|
|
|
|
self._meta["vectors"] = {
|
|
|
|
"width": self.vocab.vectors_length,
|
|
|
|
"vectors": len(self.vocab.vectors),
|
|
|
|
"keys": self.vocab.vectors.n_keys,
|
|
|
|
"name": self.vocab.vectors.name,
|
|
|
|
}
|
|
|
|
self._meta["pipeline"] = self.pipe_names
|
2019-10-27 15:35:49 +03:00
|
|
|
self._meta["factories"] = self.pipe_factories
|
2019-09-12 12:34:25 +03:00
|
|
|
self._meta["labels"] = self.pipe_labels
|
2017-07-23 01:50:18 +03:00
|
|
|
return self._meta
|
|
|
|
|
|
|
|
@meta.setter
|
|
|
|
def meta(self, value):
|
|
|
|
self._meta = value
|
|
|
|
|
2017-06-04 23:52:09 +03:00
|
|
|
# Conveniences to access pipeline components
|
2019-03-15 18:23:17 +03:00
|
|
|
# Shouldn't be used anymore!
|
2017-06-04 23:52:09 +03:00
|
|
|
@property
|
|
|
|
def tensorizer(self):
|
💫 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 19:03:03 +03:00
|
|
|
return self.get_pipe("tensorizer")
|
2017-06-04 23:52:09 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def tagger(self):
|
💫 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 19:03:03 +03:00
|
|
|
return self.get_pipe("tagger")
|
2017-06-04 23:52:09 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def parser(self):
|
💫 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 19:03:03 +03:00
|
|
|
return self.get_pipe("parser")
|
2017-06-04 23:52:09 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def entity(self):
|
💫 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 19:03:03 +03:00
|
|
|
return self.get_pipe("ner")
|
2017-06-04 23:52:09 +03:00
|
|
|
|
2019-03-21 19:33:25 +03:00
|
|
|
@property
|
|
|
|
def linker(self):
|
2019-03-22 15:55:10 +03:00
|
|
|
return self.get_pipe("entity_linker")
|
2019-03-21 19:33:25 +03:00
|
|
|
|
2017-06-04 23:52:09 +03:00
|
|
|
@property
|
|
|
|
def matcher(self):
|
💫 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 19:03:03 +03:00
|
|
|
return self.get_pipe("matcher")
|
2017-10-07 01:25:54 +03:00
|
|
|
|
|
|
|
@property
|
|
|
|
def pipe_names(self):
|
|
|
|
"""Get names of available pipeline components.
|
|
|
|
|
|
|
|
RETURNS (list): List of component name strings, in order.
|
|
|
|
"""
|
|
|
|
return [pipe_name for pipe_name, _ in self.pipeline]
|
|
|
|
|
2019-10-27 15:35:49 +03:00
|
|
|
@property
|
|
|
|
def pipe_factories(self):
|
|
|
|
"""Get the component factories for the available pipeline components.
|
|
|
|
|
|
|
|
RETURNS (dict): Factory names, keyed by component names.
|
|
|
|
"""
|
|
|
|
factories = {}
|
|
|
|
for pipe_name, pipe in self.pipeline:
|
|
|
|
factories[pipe_name] = getattr(pipe, "factory", pipe_name)
|
|
|
|
return factories
|
|
|
|
|
2019-09-12 11:56:28 +03:00
|
|
|
@property
|
|
|
|
def pipe_labels(self):
|
2019-09-12 14:03:38 +03:00
|
|
|
"""Get the labels set by the pipeline components, if available (if
|
|
|
|
the component exposes a labels property).
|
2019-09-12 11:56:28 +03:00
|
|
|
|
|
|
|
RETURNS (dict): Labels keyed by component name.
|
|
|
|
"""
|
|
|
|
labels = OrderedDict()
|
|
|
|
for name, pipe in self.pipeline:
|
|
|
|
if hasattr(pipe, "labels"):
|
|
|
|
labels[name] = list(pipe.labels)
|
|
|
|
return labels
|
|
|
|
|
2017-10-07 01:25:54 +03:00
|
|
|
def get_pipe(self, name):
|
|
|
|
"""Get a pipeline component for a given component name.
|
|
|
|
|
|
|
|
name (unicode): Name of pipeline component to get.
|
|
|
|
RETURNS (callable): The pipeline component.
|
2019-03-15 18:23:17 +03:00
|
|
|
|
|
|
|
DOCS: https://spacy.io/api/language#get_pipe
|
2017-10-07 01:25:54 +03:00
|
|
|
"""
|
|
|
|
for pipe_name, component in self.pipeline:
|
|
|
|
if pipe_name == name:
|
|
|
|
return component
|
2018-04-03 16:50:31 +03:00
|
|
|
raise KeyError(Errors.E001.format(name=name, opts=self.pipe_names))
|
2017-10-07 01:25:54 +03:00
|
|
|
|
|
|
|
def create_pipe(self, name, config=dict()):
|
|
|
|
"""Create a pipeline component from a factory.
|
|
|
|
|
|
|
|
name (unicode): Factory name to look up in `Language.factories`.
|
2017-10-07 02:04:50 +03:00
|
|
|
config (dict): Configuration parameters to initialise component.
|
2017-10-07 01:25:54 +03:00
|
|
|
RETURNS (callable): Pipeline component.
|
2019-03-15 18:23:17 +03:00
|
|
|
|
|
|
|
DOCS: https://spacy.io/api/language#create_pipe
|
2017-10-07 01:25:54 +03:00
|
|
|
"""
|
|
|
|
if name not in self.factories:
|
2018-11-30 23:22:40 +03:00
|
|
|
if name == "sbd":
|
|
|
|
raise KeyError(Errors.E108.format(name=name))
|
|
|
|
else:
|
|
|
|
raise KeyError(Errors.E002.format(name=name))
|
2017-10-07 01:25:54 +03:00
|
|
|
factory = self.factories[name]
|
|
|
|
return factory(self, **config)
|
|
|
|
|
💫 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 19:03:03 +03:00
|
|
|
def add_pipe(
|
|
|
|
self, component, name=None, before=None, after=None, first=None, last=None
|
|
|
|
):
|
2017-10-07 01:25:54 +03:00
|
|
|
"""Add a component to the processing pipeline. Valid components are
|
2017-10-27 15:40:14 +03:00
|
|
|
callables that take a `Doc` object, modify it and return it. Only one
|
|
|
|
of before/after/first/last can be set. Default behaviour is "last".
|
2017-10-07 01:25:54 +03:00
|
|
|
|
|
|
|
component (callable): The pipeline component.
|
|
|
|
name (unicode): Name of pipeline component. Overwrites existing
|
|
|
|
component.name attribute if available. If no name is set and
|
|
|
|
the component exposes no name attribute, component.__name__ is
|
2017-10-27 15:40:14 +03:00
|
|
|
used. An error is raised if a name already exists in the pipeline.
|
2017-10-07 01:25:54 +03:00
|
|
|
before (unicode): Component name to insert component directly before.
|
|
|
|
after (unicode): Component name to insert component directly after.
|
|
|
|
first (bool): Insert component first / not first in the pipeline.
|
|
|
|
last (bool): Insert component last / not last in the pipeline.
|
|
|
|
|
2019-03-15 18:23:17 +03:00
|
|
|
DOCS: https://spacy.io/api/language#add_pipe
|
2017-10-07 01:25:54 +03: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 19:03:03 +03:00
|
|
|
if not hasattr(component, "__call__"):
|
2018-04-03 16:50:31 +03:00
|
|
|
msg = Errors.E003.format(component=repr(component), name=name)
|
2018-01-30 18:29:07 +03:00
|
|
|
if isinstance(component, basestring_) and component in self.factories:
|
2018-04-03 16:50:31 +03:00
|
|
|
msg += Errors.E004.format(component=component)
|
2018-01-30 17:43:03 +03:00
|
|
|
raise ValueError(msg)
|
2017-10-07 01:25:54 +03:00
|
|
|
if name is None:
|
2019-10-27 15:35:49 +03:00
|
|
|
name = util.get_component_name(component)
|
2017-10-07 01:25:54 +03:00
|
|
|
if name in self.pipe_names:
|
2018-04-03 16:50:31 +03:00
|
|
|
raise ValueError(Errors.E007.format(name=name, opts=self.pipe_names))
|
2017-10-07 01:25:54 +03:00
|
|
|
if sum([bool(before), bool(after), bool(first), bool(last)]) >= 2:
|
2018-04-03 16:50:31 +03:00
|
|
|
raise ValueError(Errors.E006)
|
2019-10-27 15:35:49 +03:00
|
|
|
pipe_index = 0
|
2017-10-07 01:25:54 +03:00
|
|
|
pipe = (name, component)
|
|
|
|
if last or not any([first, before, after]):
|
2019-10-27 15:35:49 +03:00
|
|
|
pipe_index = len(self.pipeline)
|
2017-10-07 01:25:54 +03:00
|
|
|
self.pipeline.append(pipe)
|
|
|
|
elif first:
|
|
|
|
self.pipeline.insert(0, pipe)
|
|
|
|
elif before and before in self.pipe_names:
|
2019-10-27 15:35:49 +03:00
|
|
|
pipe_index = self.pipe_names.index(before)
|
2017-10-07 01:25:54 +03:00
|
|
|
self.pipeline.insert(self.pipe_names.index(before), pipe)
|
|
|
|
elif after and after in self.pipe_names:
|
2019-10-27 15:35:49 +03:00
|
|
|
pipe_index = self.pipe_names.index(after) + 1
|
2017-11-28 22:37:55 +03:00
|
|
|
self.pipeline.insert(self.pipe_names.index(after) + 1, pipe)
|
2017-10-07 01:25:54 +03:00
|
|
|
else:
|
💫 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 19:03:03 +03:00
|
|
|
raise ValueError(
|
|
|
|
Errors.E001.format(name=before or after, opts=self.pipe_names)
|
|
|
|
)
|
2019-10-27 15:35:49 +03:00
|
|
|
if ENABLE_PIPELINE_ANALYSIS:
|
|
|
|
analyze_pipes(self.pipeline, name, component, pipe_index)
|
2017-06-04 23:52:09 +03:00
|
|
|
|
2017-10-17 12:20:07 +03:00
|
|
|
def has_pipe(self, name):
|
|
|
|
"""Check if a component name is present in the pipeline. Equivalent to
|
|
|
|
`name in nlp.pipe_names`.
|
|
|
|
|
|
|
|
name (unicode): Name of the component.
|
2017-10-27 15:40:14 +03:00
|
|
|
RETURNS (bool): Whether a component of the name exists in the pipeline.
|
2019-03-15 18:23:17 +03:00
|
|
|
|
|
|
|
DOCS: https://spacy.io/api/language#has_pipe
|
2017-10-17 12:20:07 +03:00
|
|
|
"""
|
|
|
|
return name in self.pipe_names
|
|
|
|
|
2017-10-07 01:25:54 +03:00
|
|
|
def replace_pipe(self, name, component):
|
|
|
|
"""Replace a component in the pipeline.
|
|
|
|
|
|
|
|
name (unicode): Name of the component to replace.
|
|
|
|
component (callable): Pipeline component.
|
2019-03-15 18:23:17 +03:00
|
|
|
|
|
|
|
DOCS: https://spacy.io/api/language#replace_pipe
|
2017-10-07 01:25:54 +03:00
|
|
|
"""
|
|
|
|
if name not in self.pipe_names:
|
2018-04-03 16:50:31 +03:00
|
|
|
raise ValueError(Errors.E001.format(name=name, opts=self.pipe_names))
|
2019-05-14 17:59:31 +03:00
|
|
|
if not hasattr(component, "__call__"):
|
|
|
|
msg = Errors.E003.format(component=repr(component), name=name)
|
|
|
|
if isinstance(component, basestring_) and component in self.factories:
|
|
|
|
msg += Errors.E135.format(name=name)
|
|
|
|
raise ValueError(msg)
|
2017-10-07 01:25:54 +03:00
|
|
|
self.pipeline[self.pipe_names.index(name)] = (name, component)
|
2019-10-27 15:35:49 +03:00
|
|
|
if ENABLE_PIPELINE_ANALYSIS:
|
|
|
|
analyze_all_pipes(self.pipeline)
|
2017-10-07 01:25:54 +03:00
|
|
|
|
|
|
|
def rename_pipe(self, old_name, new_name):
|
|
|
|
"""Rename a pipeline component.
|
|
|
|
|
|
|
|
old_name (unicode): Name of the component to rename.
|
|
|
|
new_name (unicode): New name of the component.
|
2019-03-15 18:23:17 +03:00
|
|
|
|
|
|
|
DOCS: https://spacy.io/api/language#rename_pipe
|
2017-10-07 01:25:54 +03:00
|
|
|
"""
|
|
|
|
if old_name not in self.pipe_names:
|
2018-04-03 16:50:31 +03:00
|
|
|
raise ValueError(Errors.E001.format(name=old_name, opts=self.pipe_names))
|
2017-10-07 01:25:54 +03:00
|
|
|
if new_name in self.pipe_names:
|
2018-04-03 16:50:31 +03:00
|
|
|
raise ValueError(Errors.E007.format(name=new_name, opts=self.pipe_names))
|
2017-10-07 01:25:54 +03:00
|
|
|
i = self.pipe_names.index(old_name)
|
|
|
|
self.pipeline[i] = (new_name, self.pipeline[i][1])
|
|
|
|
|
|
|
|
def remove_pipe(self, name):
|
|
|
|
"""Remove a component from the pipeline.
|
|
|
|
|
|
|
|
name (unicode): Name of the component to remove.
|
2017-10-07 02:04:50 +03:00
|
|
|
RETURNS (tuple): A `(name, component)` tuple of the removed component.
|
2019-03-15 18:23:17 +03:00
|
|
|
|
|
|
|
DOCS: https://spacy.io/api/language#remove_pipe
|
2017-10-07 01:25:54 +03:00
|
|
|
"""
|
|
|
|
if name not in self.pipe_names:
|
2018-04-03 16:50:31 +03:00
|
|
|
raise ValueError(Errors.E001.format(name=name, opts=self.pipe_names))
|
2019-10-30 21:04:17 +03:00
|
|
|
removed = self.pipeline.pop(self.pipe_names.index(name))
|
2019-10-27 15:35:49 +03:00
|
|
|
if ENABLE_PIPELINE_ANALYSIS:
|
|
|
|
analyze_all_pipes(self.pipeline)
|
2019-10-30 21:04:17 +03:00
|
|
|
return removed
|
2017-06-04 23:52:09 +03:00
|
|
|
|
2019-03-11 01:36:47 +03:00
|
|
|
def __call__(self, text, disable=[], component_cfg=None):
|
2017-10-07 01:26:05 +03:00
|
|
|
"""Apply the pipeline to some text. The text can span multiple sentences,
|
2017-05-19 00:57:38 +03:00
|
|
|
and can contain arbtrary whitespace. Alignment into the original string
|
2015-08-25 16:37:17 +03:00
|
|
|
is preserved.
|
2016-12-18 18:54:52 +03:00
|
|
|
|
2017-05-19 00:57:38 +03:00
|
|
|
text (unicode): The text to be processed.
|
2017-05-26 13:33:54 +03:00
|
|
|
disable (list): Names of the pipeline components to disable.
|
2019-03-11 01:36:47 +03:00
|
|
|
component_cfg (dict): An optional dictionary with extra keyword arguments
|
|
|
|
for specific components.
|
2017-05-19 00:57:38 +03:00
|
|
|
RETURNS (Doc): A container for accessing the annotations.
|
2016-11-01 14:25:36 +03:00
|
|
|
|
2019-03-15 18:23:17 +03:00
|
|
|
DOCS: https://spacy.io/api/language#call
|
2015-08-25 16:37:17 +03:00
|
|
|
"""
|
💫 Port master changes over to develop (#2979)
* Create aryaprabhudesai.md (#2681)
* Update _install.jade (#2688)
Typo fix: "models" -> "model"
* Add FAC to spacy.explain (resolves #2706)
* Remove docstrings for deprecated arguments (see #2703)
* When calling getoption() in conftest.py, pass a default option (#2709)
* When calling getoption() in conftest.py, pass a default option
This is necessary to allow testing an installed spacy by running:
pytest --pyargs spacy
* Add contributor agreement
* update bengali token rules for hyphen and digits (#2731)
* Less norm computations in token similarity (#2730)
* Less norm computations in token similarity
* Contributor agreement
* Remove ')' for clarity (#2737)
Sorry, don't mean to be nitpicky, I just noticed this when going through the CLI and thought it was a quick fix. That said, if this was intention than please let me know.
* added contributor agreement for mbkupfer (#2738)
* Basic support for Telugu language (#2751)
* Lex _attrs for polish language (#2750)
* Signed spaCy contributor agreement
* Added polish version of english lex_attrs
* Introduces a bulk merge function, in order to solve issue #653 (#2696)
* Fix comment
* Introduce bulk merge to increase performance on many span merges
* Sign contributor agreement
* Implement pull request suggestions
* Describe converters more explicitly (see #2643)
* Add multi-threading note to Language.pipe (resolves #2582) [ci skip]
* Fix formatting
* Fix dependency scheme docs (closes #2705) [ci skip]
* Don't set stop word in example (closes #2657) [ci skip]
* Add words to portuguese language _num_words (#2759)
* Add words to portuguese language _num_words
* Add words to portuguese language _num_words
* Update Indonesian model (#2752)
* adding e-KTP in tokenizer exceptions list
* add exception token
* removing lines with containing space as it won't matter since we use .split() method in the end, added new tokens in exception
* add tokenizer exceptions list
* combining base_norms with norm_exceptions
* adding norm_exception
* fix double key in lemmatizer
* remove unused import on punctuation.py
* reformat stop_words to reduce number of lines, improve readibility
* updating tokenizer exception
* implement is_currency for lang/id
* adding orth_first_upper in tokenizer_exceptions
* update the norm_exception list
* remove bunch of abbreviations
* adding contributors file
* Fixed spaCy+Keras example (#2763)
* bug fixes in keras example
* created contributor agreement
* Adding French hyphenated first name (#2786)
* Fix typo (closes #2784)
* Fix typo (#2795) [ci skip]
Fixed typo on line 6 "regcognizer --> recognizer"
* Adding basic support for Sinhala language. (#2788)
* adding Sinhala language package, stop words, examples and lex_attrs.
* Adding contributor agreement
* Updating contributor agreement
* Also include lowercase norm exceptions
* Fix error (#2802)
* Fix error
ValueError: cannot resize an array that references or is referenced
by another array in this way. Use the resize function
* added spaCy Contributor Agreement
* Add charlax's contributor agreement (#2805)
* agreement of contributor, may I introduce a tiny pl languge contribution (#2799)
* Contributors agreement
* Contributors agreement
* Contributors agreement
* Add jupyter=True to displacy.render in documentation (#2806)
* Revert "Also include lowercase norm exceptions"
This reverts commit 70f4e8adf37cfcfab60be2b97d6deae949b30e9e.
* Remove deprecated encoding argument to msgpack
* Set up dependency tree pattern matching skeleton (#2732)
* Fix bug when too many entity types. Fixes #2800
* Fix Python 2 test failure
* Require older msgpack-numpy
* Restore encoding arg on msgpack-numpy
* Try to fix version pin for msgpack-numpy
* Update Portuguese Language (#2790)
* Add words to portuguese language _num_words
* Add words to portuguese language _num_words
* Portuguese - Add/remove stopwords, fix tokenizer, add currency symbols
* Extended punctuation and norm_exceptions in the Portuguese language
* Correct error in spacy universe docs concerning spacy-lookup (#2814)
* Update Keras Example for (Parikh et al, 2016) implementation (#2803)
* bug fixes in keras example
* created contributor agreement
* baseline for Parikh model
* initial version of parikh 2016 implemented
* tested asymmetric models
* fixed grevious error in normalization
* use standard SNLI test file
* begin to rework parikh example
* initial version of running example
* start to document the new version
* start to document the new version
* Update Decompositional Attention.ipynb
* fixed calls to similarity
* updated the README
* import sys package duh
* simplified indexing on mapping word to IDs
* stupid python indent error
* added code from https://github.com/tensorflow/tensorflow/issues/3388 for tf bug workaround
* Fix typo (closes #2815) [ci skip]
* Update regex version dependency
* Set version to 2.0.13.dev3
* Skip seemingly problematic test
* Remove problematic test
* Try previous version of regex
* Revert "Remove problematic test"
This reverts commit bdebbef45552d698d390aa430b527ee27830f11b.
* Unskip test
* Try older version of regex
* 💫 Update training examples and use minibatching (#2830)
<!--- Provide a general summary of your changes in the title. -->
## Description
Update the training examples in `/examples/training` to show usage of spaCy's `minibatch` and `compounding` helpers ([see here](https://spacy.io/usage/training#tips-batch-size) for details). The lack of batching in the examples has caused some confusion in the past, especially for beginners who would copy-paste the examples, update them with large training sets and experienced slow and unsatisfying results.
### Types of change
enhancements
## 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.
* Visual C++ link updated (#2842) (closes #2841) [ci skip]
* New landing page
* Add contribution agreement
* Correcting lang/ru/examples.py (#2845)
* Correct some grammatical inaccuracies in lang\ru\examples.py; filled Contributor Agreement
* Correct some grammatical inaccuracies in lang\ru\examples.py
* Move contributor agreement to separate file
* Set version to 2.0.13.dev4
* Add Persian(Farsi) language support (#2797)
* Also include lowercase norm exceptions
* Remove in favour of https://github.com/explosion/spaCy/graphs/contributors
* Rule-based French Lemmatizer (#2818)
<!--- Provide a general summary of your changes in the title. -->
## Description
<!--- Use this section to describe your changes. If your changes required
testing, include information about the testing environment and the tests you
ran. If your test fixes a bug reported in an issue, don't forget to include the
issue number. If your PR is still a work in progress, that's totally fine – just
include a note to let us know. -->
Add a rule-based French Lemmatizer following the english one and the excellent PR for [greek language optimizations](https://github.com/explosion/spaCy/pull/2558) to adapt the Lemmatizer class.
### Types of change
<!-- What type of change does your PR cover? Is it a bug fix, an enhancement
or new feature, or a change to the documentation? -->
- Lemma dictionary used can be found [here](http://infolingu.univ-mlv.fr/DonneesLinguistiques/Dictionnaires/telechargement.html), I used the XML version.
- Add several files containing exhaustive list of words for each part of speech
- Add some lemma rules
- Add POS that are not checked in the standard Lemmatizer, i.e PRON, DET, ADV and AUX
- Modify the Lemmatizer class to check in lookup table as a last resort if POS not mentionned
- Modify the lemmatize function to check in lookup table as a last resort
- Init files are updated so the model can support all the functionalities mentioned above
- Add words to tokenizer_exceptions_list.py in respect to regex used in tokenizer_exceptions.py
## 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.
* Set version to 2.0.13
* Fix formatting and consistency
* Update docs for new version [ci skip]
* Increment version [ci skip]
* Add info on wheels [ci skip]
* Adding "This is a sentence" example to Sinhala (#2846)
* Add wheels badge
* Update badge [ci skip]
* Update README.rst [ci skip]
* Update murmurhash pin
* Increment version to 2.0.14.dev0
* Update GPU docs for v2.0.14
* Add wheel to setup_requires
* Import prefer_gpu and require_gpu functions from Thinc
* Add tests for prefer_gpu() and require_gpu()
* Update requirements and setup.py
* Workaround bug in thinc require_gpu
* Set version to v2.0.14
* Update push-tag script
* Unhack prefer_gpu
* Require thinc 6.10.6
* Update prefer_gpu and require_gpu docs [ci skip]
* Fix specifiers for GPU
* Set version to 2.0.14.dev1
* Set version to 2.0.14
* Update Thinc version pin
* Increment version
* Fix msgpack-numpy version pin
* Increment version
* Update version to 2.0.16
* Update version [ci skip]
* Redundant ')' in the Stop words' example (#2856)
<!--- Provide a general summary of your changes in the title. -->
## Description
<!--- Use this section to describe your changes. If your changes required
testing, include information about the testing environment and the tests you
ran. If your test fixes a bug reported in an issue, don't forget to include the
issue number. If your PR is still a work in progress, that's totally fine – just
include a note to let us know. -->
### Types of change
<!-- What type of change does your PR cover? Is it a bug fix, an enhancement
or new feature, or a change to the documentation? -->
## Checklist
<!--- Before you submit the PR, go over this checklist and make sure you can
tick off all the boxes. [] -> [x] -->
- [ ] I have submitted the spaCy Contributor Agreement.
- [ ] I ran the tests, and all new and existing tests passed.
- [ ] My changes don't require a change to the documentation, or if they do, I've added all required information.
* Documentation improvement regarding joblib and SO (#2867)
Some documentation improvements
## Description
1. Fixed the dead URL to joblib
2. Fixed Stack Overflow brand name (with space)
### Types of change
Documentation
## 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.
* raise error when setting overlapping entities as doc.ents (#2880)
* Fix out-of-bounds access in NER training
The helper method state.B(1) gets the index of the first token of the
buffer, or -1 if no such token exists. Normally this is safe because we
pass this to functions like state.safe_get(), which returns an empty
token. Here we used it directly as an array index, which is not okay!
This error may have been the cause of out-of-bounds access errors during
training. Similar errors may still be around, so much be hunted down.
Hunting this one down took a long time...I printed out values across
training runs and diffed, looking for points of divergence between
runs, when no randomness should be allowed.
* Change PyThaiNLP Url (#2876)
* Fix missing comma
* Add example showing a fix-up rule for space entities
* Set version to 2.0.17.dev0
* Update regex version
* Revert "Update regex version"
This reverts commit 62358dd867d15bc6a475942dff34effba69dd70a.
* Try setting older regex version, to align with conda
* Set version to 2.0.17
* Add spacy-js to universe [ci-skip]
* Add spacy-raspberry to universe (closes #2889)
* Add script to validate universe json [ci skip]
* Removed space in docs + added contributor indo (#2909)
* - removed unneeded space in documentation
* - added contributor info
* Allow input text of length up to max_length, inclusive (#2922)
* Include universe spec for spacy-wordnet component (#2919)
* feat: include universe spec for spacy-wordnet component
* chore: include spaCy contributor agreement
* Minor formatting changes [ci skip]
* Fix image [ci skip]
Twitter URL doesn't work on live site
* Check if the word is in one of the regular lists specific to each POS (#2886)
* 💫 Create random IDs for SVGs to prevent ID clashes (#2927)
Resolves #2924.
## Description
Fixes problem where multiple visualizations in Jupyter notebooks would have clashing arc IDs, resulting in weirdly positioned arc labels. Generating a random ID prefix so even identical parses won't receive the same IDs for consistency (even if effect of ID clash isn't noticable here.)
### Types of change
bug fix
## 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.
* Fix typo [ci skip]
* fixes symbolic link on py3 and windows (#2949)
* fixes symbolic link on py3 and windows
during setup of spacy using command
python -m spacy link en_core_web_sm en
closes #2948
* Update spacy/compat.py
Co-Authored-By: cicorias <cicorias@users.noreply.github.com>
* Fix formatting
* Update universe [ci skip]
* Catalan Language Support (#2940)
* Catalan language Support
* Ddding Catalan to documentation
* Sort languages alphabetically [ci skip]
* Update tests for pytest 4.x (#2965)
<!--- Provide a general summary of your changes in the title. -->
## Description
- [x] Replace marks in params for pytest 4.0 compat ([see here](https://docs.pytest.org/en/latest/deprecations.html#marks-in-pytest-mark-parametrize))
- [x] Un-xfail passing tests (some fixes in a recent update resolved a bunch of issues, but tests were apparently never updated here)
### Types of change
<!-- What type of change does your PR cover? Is it a bug fix, an enhancement
or new feature, or a change to the documentation? -->
## 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.
* Fix regex pin to harmonize with conda (#2964)
* Update README.rst
* Fix bug where Vocab.prune_vector did not use 'batch_size' (#2977)
Fixes #2976
* Fix typo
* Fix typo
* Remove duplicate file
* Require thinc 7.0.0.dev2
Fixes bug in gpu_ops that would use cupy instead of numpy on CPU
* Add missing import
* Fix error IDs
* Fix tests
2018-11-29 18:30:29 +03:00
|
|
|
if len(text) > self.max_length:
|
💫 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 19:03:03 +03:00
|
|
|
raise ValueError(
|
|
|
|
Errors.E088.format(length=len(text), max_length=self.max_length)
|
|
|
|
)
|
2016-10-14 18:38:29 +03:00
|
|
|
doc = self.make_doc(text)
|
2019-03-11 01:36:47 +03:00
|
|
|
if component_cfg is None:
|
|
|
|
component_cfg = {}
|
2017-10-07 01:25:54 +03:00
|
|
|
for name, proc in self.pipeline:
|
2017-05-26 13:33:54 +03:00
|
|
|
if name in disable:
|
2017-05-16 12:21:59 +03:00
|
|
|
continue
|
💫 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 19:03:03 +03:00
|
|
|
if not hasattr(proc, "__call__"):
|
2018-04-03 16:50:31 +03:00
|
|
|
raise ValueError(Errors.E003.format(component=type(proc), name=name))
|
2019-03-11 01:36:47 +03:00
|
|
|
doc = proc(doc, **component_cfg.get(name, {}))
|
2018-04-03 16:50:31 +03:00
|
|
|
if doc is None:
|
|
|
|
raise ValueError(Errors.E005.format(name=name))
|
2016-05-17 17:55:42 +03:00
|
|
|
return doc
|
2015-08-25 16:37:17 +03:00
|
|
|
|
2017-10-25 14:46:41 +03:00
|
|
|
def disable_pipes(self, *names):
|
2017-10-27 15:40:14 +03:00
|
|
|
"""Disable one or more pipeline components. If used as a context
|
|
|
|
manager, the pipeline will be restored to the initial state at the end
|
|
|
|
of the block. Otherwise, a DisabledPipes object is returned, that has
|
|
|
|
a `.restore()` method you can use to undo your changes.
|
2017-10-25 14:46:41 +03:00
|
|
|
|
2019-03-15 18:23:17 +03:00
|
|
|
DOCS: https://spacy.io/api/language#disable_pipes
|
2017-10-27 15:40:14 +03:00
|
|
|
"""
|
2019-10-25 17:19:08 +03:00
|
|
|
if len(names) == 1 and isinstance(names[0], (list, tuple)):
|
|
|
|
names = names[0] # support list of names instead of spread
|
2017-10-25 14:46:41 +03:00
|
|
|
return DisabledPipes(self, *names)
|
|
|
|
|
2017-05-29 16:40:45 +03:00
|
|
|
def make_doc(self, text):
|
|
|
|
return self.tokenizer(text)
|
|
|
|
|
2019-09-27 17:20:21 +03:00
|
|
|
def _format_docs_and_golds(self, docs, golds):
|
|
|
|
"""Format golds and docs before update models."""
|
|
|
|
expected_keys = ("words", "tags", "heads", "deps", "entities", "cats", "links")
|
|
|
|
gold_objs = []
|
|
|
|
doc_objs = []
|
|
|
|
for doc, gold in zip(docs, golds):
|
|
|
|
if isinstance(doc, basestring_):
|
|
|
|
doc = self.make_doc(doc)
|
|
|
|
if not isinstance(gold, GoldParse):
|
|
|
|
unexpected = [k for k in gold if k not in expected_keys]
|
|
|
|
if unexpected:
|
|
|
|
err = Errors.E151.format(unexp=unexpected, exp=expected_keys)
|
|
|
|
raise ValueError(err)
|
|
|
|
gold = GoldParse(doc, **gold)
|
|
|
|
doc_objs.append(doc)
|
|
|
|
gold_objs.append(gold)
|
|
|
|
|
|
|
|
return doc_objs, gold_objs
|
|
|
|
|
2019-03-11 01:36:47 +03:00
|
|
|
def update(self, docs, golds, drop=0.0, sgd=None, losses=None, component_cfg=None):
|
2017-05-19 00:57:38 +03:00
|
|
|
"""Update the models in the pipeline.
|
|
|
|
|
|
|
|
docs (iterable): A batch of `Doc` objects.
|
|
|
|
golds (iterable): A batch of `GoldParse` objects.
|
2019-10-14 13:28:53 +03:00
|
|
|
drop (float): The dropout rate.
|
2017-05-21 14:17:40 +03:00
|
|
|
sgd (callable): An optimizer.
|
2019-05-24 15:06:26 +03:00
|
|
|
losses (dict): Dictionary to update with the loss, keyed by component.
|
|
|
|
component_cfg (dict): Config parameters for specific pipeline
|
|
|
|
components, keyed by component name.
|
2017-05-19 00:57:38 +03:00
|
|
|
|
2019-03-15 18:23:17 +03:00
|
|
|
DOCS: https://spacy.io/api/language#update
|
2017-05-19 00:57:38 +03:00
|
|
|
"""
|
2017-08-01 23:10:17 +03:00
|
|
|
if len(docs) != len(golds):
|
2018-04-03 16:50:31 +03:00
|
|
|
raise IndexError(Errors.E009.format(n_docs=len(docs), n_golds=len(golds)))
|
2017-08-01 23:10:17 +03:00
|
|
|
if len(docs) == 0:
|
|
|
|
return
|
2017-08-20 15:42:07 +03:00
|
|
|
if sgd is None:
|
|
|
|
if self._optimizer is None:
|
2017-11-07 00:07:38 +03:00
|
|
|
self._optimizer = create_default_optimizer(Model.ops)
|
2017-08-20 15:42:07 +03:00
|
|
|
sgd = self._optimizer
|
2017-11-07 00:07:38 +03:00
|
|
|
# Allow dict of args to GoldParse, instead of GoldParse objects.
|
2019-09-27 17:20:21 +03:00
|
|
|
docs, golds = self._format_docs_and_golds(docs, golds)
|
2017-05-25 04:10:54 +03:00
|
|
|
grads = {}
|
2017-10-27 15:40:14 +03:00
|
|
|
|
2017-05-25 04:10:54 +03:00
|
|
|
def get_grads(W, dW, key=None):
|
|
|
|
grads[key] = (W, dW)
|
2017-10-27 15:40:14 +03:00
|
|
|
|
2018-09-14 01:51:52 +03:00
|
|
|
get_grads.alpha = sgd.alpha
|
|
|
|
get_grads.b1 = sgd.b1
|
|
|
|
get_grads.b2 = sgd.b2
|
2017-09-21 15:59:48 +03:00
|
|
|
pipes = list(self.pipeline)
|
2017-05-28 02:32:21 +03:00
|
|
|
random.shuffle(pipes)
|
2019-03-11 01:36:47 +03:00
|
|
|
if component_cfg is None:
|
|
|
|
component_cfg = {}
|
2017-10-07 01:25:54 +03:00
|
|
|
for name, proc in pipes:
|
💫 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 19:03:03 +03:00
|
|
|
if not hasattr(proc, "update"):
|
2017-05-22 02:43:31 +03:00
|
|
|
continue
|
2017-11-03 22:20:01 +03:00
|
|
|
grads = {}
|
2019-03-11 01:36:47 +03:00
|
|
|
kwargs = component_cfg.get(name, {})
|
|
|
|
kwargs.setdefault("drop", drop)
|
|
|
|
proc.update(docs, golds, sgd=get_grads, losses=losses, **kwargs)
|
2017-11-03 22:20:01 +03:00
|
|
|
for key, (W, dW) in grads.items():
|
|
|
|
sgd(W, dW, key=key)
|
2017-05-16 17:17:30 +03:00
|
|
|
|
💫 Better support for semi-supervised learning (#3035)
The new spacy pretrain command implemented BERT/ULMFit/etc-like transfer learning, using our Language Modelling with Approximate Outputs version of BERT's cloze task. Pretraining is convenient, but in some ways it's a bit of a strange solution. All we're doing is initialising the weights. At the same time, we're putting a lot of work into our optimisation so that it's less sensitive to initial conditions, and more likely to find good optima. I discuss this a bit in the pseudo-rehearsal blog post: https://explosion.ai/blog/pseudo-rehearsal-catastrophic-forgetting
Support semi-supervised learning in spacy train
One obvious way to improve these pretraining methods is to do multi-task learning, instead of just transfer learning. This has been shown to work very well: https://arxiv.org/pdf/1809.08370.pdf . This patch makes it easy to do this sort of thing.
Add a new argument to spacy train, --raw-text. This takes a jsonl file with unlabelled data that can be used in arbitrary ways to do semi-supervised learning.
Add a new method to the Language class and to pipeline components, .rehearse(). This is like .update(), but doesn't expect GoldParse objects. It takes a batch of Doc objects, and performs an update on some semi-supervised objective.
Move the BERT-LMAO objective out from spacy/cli/pretrain.py into spacy/_ml.py, so we can create a new pipeline component, ClozeMultitask. This can be specified as a parser or NER multitask in the spacy train command. Example usage:
python -m spacy train en ./tmp ~/data/en-core-web/train/nw.json ~/data/en-core-web/dev/nw.json --pipeline parser --raw-textt ~/data/unlabelled/reddit-100k.jsonl --vectors en_vectors_web_lg --parser-multitasks cloze
Implement rehearsal methods for pipeline components
The new --raw-text argument and nlp.rehearse() method also gives us a good place to implement the the idea in the pseudo-rehearsal blog post in the parser. This works as follows:
Add a new nlp.resume_training() method. This allocates copies of pre-trained models in the pipeline, setting things up for the rehearsal updates. It also returns an optimizer object. This also greatly reduces confusion around the nlp.begin_training() method, which randomises the weights, making it not suitable for adding new labels or otherwise fine-tuning a pre-trained model.
Implement rehearsal updates on the Parser class, making it available for the dependency parser and NER. During rehearsal, the initial model is used to supervise the model being trained. The current model is asked to match the predictions of the initial model on some data. This minimises catastrophic forgetting, by keeping the model's predictions close to the original. See the blog post for details.
Implement rehearsal updates for tagger
Implement rehearsal updates for text categoriz
2018-12-10 18:25:33 +03:00
|
|
|
def rehearse(self, docs, sgd=None, losses=None, config=None):
|
|
|
|
"""Make a "rehearsal" update to the models in the pipeline, to prevent
|
|
|
|
forgetting. Rehearsal updates run an initial copy of the model over some
|
|
|
|
data, and update the model so its current predictions are more like the
|
2019-10-02 11:37:39 +03:00
|
|
|
initial ones. This is useful for keeping a pretrained model on-track,
|
💫 Better support for semi-supervised learning (#3035)
The new spacy pretrain command implemented BERT/ULMFit/etc-like transfer learning, using our Language Modelling with Approximate Outputs version of BERT's cloze task. Pretraining is convenient, but in some ways it's a bit of a strange solution. All we're doing is initialising the weights. At the same time, we're putting a lot of work into our optimisation so that it's less sensitive to initial conditions, and more likely to find good optima. I discuss this a bit in the pseudo-rehearsal blog post: https://explosion.ai/blog/pseudo-rehearsal-catastrophic-forgetting
Support semi-supervised learning in spacy train
One obvious way to improve these pretraining methods is to do multi-task learning, instead of just transfer learning. This has been shown to work very well: https://arxiv.org/pdf/1809.08370.pdf . This patch makes it easy to do this sort of thing.
Add a new argument to spacy train, --raw-text. This takes a jsonl file with unlabelled data that can be used in arbitrary ways to do semi-supervised learning.
Add a new method to the Language class and to pipeline components, .rehearse(). This is like .update(), but doesn't expect GoldParse objects. It takes a batch of Doc objects, and performs an update on some semi-supervised objective.
Move the BERT-LMAO objective out from spacy/cli/pretrain.py into spacy/_ml.py, so we can create a new pipeline component, ClozeMultitask. This can be specified as a parser or NER multitask in the spacy train command. Example usage:
python -m spacy train en ./tmp ~/data/en-core-web/train/nw.json ~/data/en-core-web/dev/nw.json --pipeline parser --raw-textt ~/data/unlabelled/reddit-100k.jsonl --vectors en_vectors_web_lg --parser-multitasks cloze
Implement rehearsal methods for pipeline components
The new --raw-text argument and nlp.rehearse() method also gives us a good place to implement the the idea in the pseudo-rehearsal blog post in the parser. This works as follows:
Add a new nlp.resume_training() method. This allocates copies of pre-trained models in the pipeline, setting things up for the rehearsal updates. It also returns an optimizer object. This also greatly reduces confusion around the nlp.begin_training() method, which randomises the weights, making it not suitable for adding new labels or otherwise fine-tuning a pre-trained model.
Implement rehearsal updates on the Parser class, making it available for the dependency parser and NER. During rehearsal, the initial model is used to supervise the model being trained. The current model is asked to match the predictions of the initial model on some data. This minimises catastrophic forgetting, by keeping the model's predictions close to the original. See the blog post for details.
Implement rehearsal updates for tagger
Implement rehearsal updates for text categoriz
2018-12-10 18:25:33 +03:00
|
|
|
even if you're updating it with a smaller set of examples.
|
|
|
|
|
|
|
|
docs (iterable): A batch of `Doc` objects.
|
2019-10-14 13:28:53 +03:00
|
|
|
drop (float): The dropout rate.
|
💫 Better support for semi-supervised learning (#3035)
The new spacy pretrain command implemented BERT/ULMFit/etc-like transfer learning, using our Language Modelling with Approximate Outputs version of BERT's cloze task. Pretraining is convenient, but in some ways it's a bit of a strange solution. All we're doing is initialising the weights. At the same time, we're putting a lot of work into our optimisation so that it's less sensitive to initial conditions, and more likely to find good optima. I discuss this a bit in the pseudo-rehearsal blog post: https://explosion.ai/blog/pseudo-rehearsal-catastrophic-forgetting
Support semi-supervised learning in spacy train
One obvious way to improve these pretraining methods is to do multi-task learning, instead of just transfer learning. This has been shown to work very well: https://arxiv.org/pdf/1809.08370.pdf . This patch makes it easy to do this sort of thing.
Add a new argument to spacy train, --raw-text. This takes a jsonl file with unlabelled data that can be used in arbitrary ways to do semi-supervised learning.
Add a new method to the Language class and to pipeline components, .rehearse(). This is like .update(), but doesn't expect GoldParse objects. It takes a batch of Doc objects, and performs an update on some semi-supervised objective.
Move the BERT-LMAO objective out from spacy/cli/pretrain.py into spacy/_ml.py, so we can create a new pipeline component, ClozeMultitask. This can be specified as a parser or NER multitask in the spacy train command. Example usage:
python -m spacy train en ./tmp ~/data/en-core-web/train/nw.json ~/data/en-core-web/dev/nw.json --pipeline parser --raw-textt ~/data/unlabelled/reddit-100k.jsonl --vectors en_vectors_web_lg --parser-multitasks cloze
Implement rehearsal methods for pipeline components
The new --raw-text argument and nlp.rehearse() method also gives us a good place to implement the the idea in the pseudo-rehearsal blog post in the parser. This works as follows:
Add a new nlp.resume_training() method. This allocates copies of pre-trained models in the pipeline, setting things up for the rehearsal updates. It also returns an optimizer object. This also greatly reduces confusion around the nlp.begin_training() method, which randomises the weights, making it not suitable for adding new labels or otherwise fine-tuning a pre-trained model.
Implement rehearsal updates on the Parser class, making it available for the dependency parser and NER. During rehearsal, the initial model is used to supervise the model being trained. The current model is asked to match the predictions of the initial model on some data. This minimises catastrophic forgetting, by keeping the model's predictions close to the original. See the blog post for details.
Implement rehearsal updates for tagger
Implement rehearsal updates for text categoriz
2018-12-10 18:25:33 +03:00
|
|
|
sgd (callable): An optimizer.
|
|
|
|
RETURNS (dict): Results from the update.
|
|
|
|
|
|
|
|
EXAMPLE:
|
|
|
|
>>> raw_text_batches = minibatch(raw_texts)
|
|
|
|
>>> for labelled_batch in minibatch(zip(train_docs, train_golds)):
|
2018-12-18 15:48:10 +03:00
|
|
|
>>> docs, golds = zip(*train_docs)
|
💫 Better support for semi-supervised learning (#3035)
The new spacy pretrain command implemented BERT/ULMFit/etc-like transfer learning, using our Language Modelling with Approximate Outputs version of BERT's cloze task. Pretraining is convenient, but in some ways it's a bit of a strange solution. All we're doing is initialising the weights. At the same time, we're putting a lot of work into our optimisation so that it's less sensitive to initial conditions, and more likely to find good optima. I discuss this a bit in the pseudo-rehearsal blog post: https://explosion.ai/blog/pseudo-rehearsal-catastrophic-forgetting
Support semi-supervised learning in spacy train
One obvious way to improve these pretraining methods is to do multi-task learning, instead of just transfer learning. This has been shown to work very well: https://arxiv.org/pdf/1809.08370.pdf . This patch makes it easy to do this sort of thing.
Add a new argument to spacy train, --raw-text. This takes a jsonl file with unlabelled data that can be used in arbitrary ways to do semi-supervised learning.
Add a new method to the Language class and to pipeline components, .rehearse(). This is like .update(), but doesn't expect GoldParse objects. It takes a batch of Doc objects, and performs an update on some semi-supervised objective.
Move the BERT-LMAO objective out from spacy/cli/pretrain.py into spacy/_ml.py, so we can create a new pipeline component, ClozeMultitask. This can be specified as a parser or NER multitask in the spacy train command. Example usage:
python -m spacy train en ./tmp ~/data/en-core-web/train/nw.json ~/data/en-core-web/dev/nw.json --pipeline parser --raw-textt ~/data/unlabelled/reddit-100k.jsonl --vectors en_vectors_web_lg --parser-multitasks cloze
Implement rehearsal methods for pipeline components
The new --raw-text argument and nlp.rehearse() method also gives us a good place to implement the the idea in the pseudo-rehearsal blog post in the parser. This works as follows:
Add a new nlp.resume_training() method. This allocates copies of pre-trained models in the pipeline, setting things up for the rehearsal updates. It also returns an optimizer object. This also greatly reduces confusion around the nlp.begin_training() method, which randomises the weights, making it not suitable for adding new labels or otherwise fine-tuning a pre-trained model.
Implement rehearsal updates on the Parser class, making it available for the dependency parser and NER. During rehearsal, the initial model is used to supervise the model being trained. The current model is asked to match the predictions of the initial model on some data. This minimises catastrophic forgetting, by keeping the model's predictions close to the original. See the blog post for details.
Implement rehearsal updates for tagger
Implement rehearsal updates for text categoriz
2018-12-10 18:25:33 +03:00
|
|
|
>>> nlp.update(docs, golds)
|
|
|
|
>>> raw_batch = [nlp.make_doc(text) for text in next(raw_text_batches)]
|
|
|
|
>>> nlp.rehearse(raw_batch)
|
|
|
|
"""
|
2019-03-15 18:23:17 +03:00
|
|
|
# TODO: document
|
💫 Better support for semi-supervised learning (#3035)
The new spacy pretrain command implemented BERT/ULMFit/etc-like transfer learning, using our Language Modelling with Approximate Outputs version of BERT's cloze task. Pretraining is convenient, but in some ways it's a bit of a strange solution. All we're doing is initialising the weights. At the same time, we're putting a lot of work into our optimisation so that it's less sensitive to initial conditions, and more likely to find good optima. I discuss this a bit in the pseudo-rehearsal blog post: https://explosion.ai/blog/pseudo-rehearsal-catastrophic-forgetting
Support semi-supervised learning in spacy train
One obvious way to improve these pretraining methods is to do multi-task learning, instead of just transfer learning. This has been shown to work very well: https://arxiv.org/pdf/1809.08370.pdf . This patch makes it easy to do this sort of thing.
Add a new argument to spacy train, --raw-text. This takes a jsonl file with unlabelled data that can be used in arbitrary ways to do semi-supervised learning.
Add a new method to the Language class and to pipeline components, .rehearse(). This is like .update(), but doesn't expect GoldParse objects. It takes a batch of Doc objects, and performs an update on some semi-supervised objective.
Move the BERT-LMAO objective out from spacy/cli/pretrain.py into spacy/_ml.py, so we can create a new pipeline component, ClozeMultitask. This can be specified as a parser or NER multitask in the spacy train command. Example usage:
python -m spacy train en ./tmp ~/data/en-core-web/train/nw.json ~/data/en-core-web/dev/nw.json --pipeline parser --raw-textt ~/data/unlabelled/reddit-100k.jsonl --vectors en_vectors_web_lg --parser-multitasks cloze
Implement rehearsal methods for pipeline components
The new --raw-text argument and nlp.rehearse() method also gives us a good place to implement the the idea in the pseudo-rehearsal blog post in the parser. This works as follows:
Add a new nlp.resume_training() method. This allocates copies of pre-trained models in the pipeline, setting things up for the rehearsal updates. It also returns an optimizer object. This also greatly reduces confusion around the nlp.begin_training() method, which randomises the weights, making it not suitable for adding new labels or otherwise fine-tuning a pre-trained model.
Implement rehearsal updates on the Parser class, making it available for the dependency parser and NER. During rehearsal, the initial model is used to supervise the model being trained. The current model is asked to match the predictions of the initial model on some data. This minimises catastrophic forgetting, by keeping the model's predictions close to the original. See the blog post for details.
Implement rehearsal updates for tagger
Implement rehearsal updates for text categoriz
2018-12-10 18:25:33 +03:00
|
|
|
if len(docs) == 0:
|
|
|
|
return
|
|
|
|
if sgd is None:
|
|
|
|
if self._optimizer is None:
|
|
|
|
self._optimizer = create_default_optimizer(Model.ops)
|
|
|
|
sgd = self._optimizer
|
|
|
|
docs = list(docs)
|
|
|
|
for i, doc in enumerate(docs):
|
|
|
|
if isinstance(doc, basestring_):
|
|
|
|
docs[i] = self.make_doc(doc)
|
|
|
|
pipes = list(self.pipeline)
|
|
|
|
random.shuffle(pipes)
|
|
|
|
if config is None:
|
|
|
|
config = {}
|
|
|
|
grads = {}
|
|
|
|
|
|
|
|
def get_grads(W, dW, key=None):
|
|
|
|
grads[key] = (W, dW)
|
|
|
|
|
|
|
|
get_grads.alpha = sgd.alpha
|
|
|
|
get_grads.b1 = sgd.b1
|
|
|
|
get_grads.b2 = sgd.b2
|
|
|
|
for name, proc in pipes:
|
|
|
|
if not hasattr(proc, "rehearse"):
|
|
|
|
continue
|
|
|
|
grads = {}
|
|
|
|
proc.rehearse(docs, sgd=get_grads, losses=losses, **config.get(name, {}))
|
|
|
|
for key, (W, dW) in grads.items():
|
|
|
|
sgd(W, dW, key=key)
|
|
|
|
return losses
|
|
|
|
|
2017-05-21 17:07:06 +03:00
|
|
|
def preprocess_gold(self, docs_golds):
|
2017-05-22 13:29:30 +03:00
|
|
|
"""Can be called before training to pre-process gold data. By default,
|
|
|
|
it handles nonprojectivity and adds missing tags to the tag map.
|
|
|
|
|
|
|
|
docs_golds (iterable): Tuples of `Doc` and `GoldParse` objects.
|
|
|
|
YIELDS (tuple): Tuples of preprocessed `Doc` and `GoldParse` objects.
|
|
|
|
"""
|
2017-10-07 01:25:54 +03:00
|
|
|
for name, proc in self.pipeline:
|
💫 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 19:03:03 +03:00
|
|
|
if hasattr(proc, "preprocess_gold"):
|
2017-05-21 17:07:06 +03:00
|
|
|
docs_golds = proc.preprocess_gold(docs_golds)
|
|
|
|
for doc, gold in docs_golds:
|
|
|
|
yield doc, gold
|
|
|
|
|
2019-03-11 01:36:47 +03:00
|
|
|
def begin_training(self, get_gold_tuples=None, sgd=None, component_cfg=None, **cfg):
|
2017-05-19 00:57:38 +03:00
|
|
|
"""Allocate models, pre-process training data and acquire a trainer and
|
|
|
|
optimizer. Used as a contextmanager.
|
|
|
|
|
2017-09-14 17:18:30 +03:00
|
|
|
get_gold_tuples (function): Function returning gold data
|
2019-03-11 01:36:47 +03:00
|
|
|
component_cfg (dict): Config parameters for specific components.
|
2017-05-19 00:57:38 +03:00
|
|
|
**cfg: Config parameters.
|
2019-03-15 18:23:17 +03:00
|
|
|
RETURNS: An optimizer.
|
|
|
|
|
|
|
|
DOCS: https://spacy.io/api/language#begin_training
|
2017-05-19 00:57:38 +03:00
|
|
|
"""
|
2017-11-01 15:14:31 +03:00
|
|
|
if get_gold_tuples is None:
|
|
|
|
get_gold_tuples = lambda: []
|
2017-05-17 13:04:50 +03:00
|
|
|
# Populate vocab
|
2017-11-01 15:14:31 +03:00
|
|
|
else:
|
2017-09-21 03:15:20 +03:00
|
|
|
for _, annots_brackets in get_gold_tuples():
|
2019-10-27 23:58:50 +03:00
|
|
|
_ = annots_brackets.pop()
|
2017-09-21 03:15:20 +03:00
|
|
|
for annots, _ in annots_brackets:
|
|
|
|
for word in annots[1]:
|
2018-11-30 19:43:08 +03:00
|
|
|
_ = self.vocab[word] # noqa: F841
|
💫 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 19:03:03 +03:00
|
|
|
if cfg.get("device", -1) >= 0:
|
2018-11-30 19:43:08 +03:00
|
|
|
util.use_gpu(cfg["device"])
|
2017-09-19 02:04:16 +03:00
|
|
|
if self.vocab.vectors.data.shape[1] >= 1:
|
💫 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 19:03:03 +03:00
|
|
|
self.vocab.vectors.data = Model.ops.asarray(self.vocab.vectors.data)
|
2017-09-23 04:11:52 +03:00
|
|
|
link_vectors_to_models(self.vocab)
|
2018-03-28 17:02:59 +03:00
|
|
|
if self.vocab.vectors.data.shape[1]:
|
💫 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 19:03:03 +03:00
|
|
|
cfg["pretrained_vectors"] = self.vocab.vectors.name
|
2020-03-25 14:28:12 +03:00
|
|
|
cfg["pretrained_dims"] = self.vocab.vectors.data.shape[1]
|
2017-11-06 16:26:00 +03:00
|
|
|
if sgd is None:
|
|
|
|
sgd = create_default_optimizer(Model.ops)
|
|
|
|
self._optimizer = sgd
|
2019-03-11 01:36:47 +03:00
|
|
|
if component_cfg is None:
|
|
|
|
component_cfg = {}
|
2017-10-07 01:25:54 +03:00
|
|
|
for name, proc in self.pipeline:
|
💫 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 19:03:03 +03:00
|
|
|
if hasattr(proc, "begin_training"):
|
2019-03-11 01:36:47 +03:00
|
|
|
kwargs = component_cfg.get(name, {})
|
|
|
|
kwargs.update(cfg)
|
💫 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 19:03:03 +03:00
|
|
|
proc.begin_training(
|
2019-03-11 01:36:47 +03:00
|
|
|
get_gold_tuples,
|
|
|
|
pipeline=self.pipeline,
|
|
|
|
sgd=self._optimizer,
|
|
|
|
**kwargs
|
💫 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 19:03:03 +03:00
|
|
|
)
|
2017-08-20 15:42:07 +03:00
|
|
|
return self._optimizer
|
2017-05-21 17:07:06 +03:00
|
|
|
|
💫 Better support for semi-supervised learning (#3035)
The new spacy pretrain command implemented BERT/ULMFit/etc-like transfer learning, using our Language Modelling with Approximate Outputs version of BERT's cloze task. Pretraining is convenient, but in some ways it's a bit of a strange solution. All we're doing is initialising the weights. At the same time, we're putting a lot of work into our optimisation so that it's less sensitive to initial conditions, and more likely to find good optima. I discuss this a bit in the pseudo-rehearsal blog post: https://explosion.ai/blog/pseudo-rehearsal-catastrophic-forgetting
Support semi-supervised learning in spacy train
One obvious way to improve these pretraining methods is to do multi-task learning, instead of just transfer learning. This has been shown to work very well: https://arxiv.org/pdf/1809.08370.pdf . This patch makes it easy to do this sort of thing.
Add a new argument to spacy train, --raw-text. This takes a jsonl file with unlabelled data that can be used in arbitrary ways to do semi-supervised learning.
Add a new method to the Language class and to pipeline components, .rehearse(). This is like .update(), but doesn't expect GoldParse objects. It takes a batch of Doc objects, and performs an update on some semi-supervised objective.
Move the BERT-LMAO objective out from spacy/cli/pretrain.py into spacy/_ml.py, so we can create a new pipeline component, ClozeMultitask. This can be specified as a parser or NER multitask in the spacy train command. Example usage:
python -m spacy train en ./tmp ~/data/en-core-web/train/nw.json ~/data/en-core-web/dev/nw.json --pipeline parser --raw-textt ~/data/unlabelled/reddit-100k.jsonl --vectors en_vectors_web_lg --parser-multitasks cloze
Implement rehearsal methods for pipeline components
The new --raw-text argument and nlp.rehearse() method also gives us a good place to implement the the idea in the pseudo-rehearsal blog post in the parser. This works as follows:
Add a new nlp.resume_training() method. This allocates copies of pre-trained models in the pipeline, setting things up for the rehearsal updates. It also returns an optimizer object. This also greatly reduces confusion around the nlp.begin_training() method, which randomises the weights, making it not suitable for adding new labels or otherwise fine-tuning a pre-trained model.
Implement rehearsal updates on the Parser class, making it available for the dependency parser and NER. During rehearsal, the initial model is used to supervise the model being trained. The current model is asked to match the predictions of the initial model on some data. This minimises catastrophic forgetting, by keeping the model's predictions close to the original. See the blog post for details.
Implement rehearsal updates for tagger
Implement rehearsal updates for text categoriz
2018-12-10 18:25:33 +03:00
|
|
|
def resume_training(self, sgd=None, **cfg):
|
2019-10-02 11:37:39 +03:00
|
|
|
"""Continue training a pretrained model.
|
2018-12-18 15:48:10 +03:00
|
|
|
|
💫 Better support for semi-supervised learning (#3035)
The new spacy pretrain command implemented BERT/ULMFit/etc-like transfer learning, using our Language Modelling with Approximate Outputs version of BERT's cloze task. Pretraining is convenient, but in some ways it's a bit of a strange solution. All we're doing is initialising the weights. At the same time, we're putting a lot of work into our optimisation so that it's less sensitive to initial conditions, and more likely to find good optima. I discuss this a bit in the pseudo-rehearsal blog post: https://explosion.ai/blog/pseudo-rehearsal-catastrophic-forgetting
Support semi-supervised learning in spacy train
One obvious way to improve these pretraining methods is to do multi-task learning, instead of just transfer learning. This has been shown to work very well: https://arxiv.org/pdf/1809.08370.pdf . This patch makes it easy to do this sort of thing.
Add a new argument to spacy train, --raw-text. This takes a jsonl file with unlabelled data that can be used in arbitrary ways to do semi-supervised learning.
Add a new method to the Language class and to pipeline components, .rehearse(). This is like .update(), but doesn't expect GoldParse objects. It takes a batch of Doc objects, and performs an update on some semi-supervised objective.
Move the BERT-LMAO objective out from spacy/cli/pretrain.py into spacy/_ml.py, so we can create a new pipeline component, ClozeMultitask. This can be specified as a parser or NER multitask in the spacy train command. Example usage:
python -m spacy train en ./tmp ~/data/en-core-web/train/nw.json ~/data/en-core-web/dev/nw.json --pipeline parser --raw-textt ~/data/unlabelled/reddit-100k.jsonl --vectors en_vectors_web_lg --parser-multitasks cloze
Implement rehearsal methods for pipeline components
The new --raw-text argument and nlp.rehearse() method also gives us a good place to implement the the idea in the pseudo-rehearsal blog post in the parser. This works as follows:
Add a new nlp.resume_training() method. This allocates copies of pre-trained models in the pipeline, setting things up for the rehearsal updates. It also returns an optimizer object. This also greatly reduces confusion around the nlp.begin_training() method, which randomises the weights, making it not suitable for adding new labels or otherwise fine-tuning a pre-trained model.
Implement rehearsal updates on the Parser class, making it available for the dependency parser and NER. During rehearsal, the initial model is used to supervise the model being trained. The current model is asked to match the predictions of the initial model on some data. This minimises catastrophic forgetting, by keeping the model's predictions close to the original. See the blog post for details.
Implement rehearsal updates for tagger
Implement rehearsal updates for text categoriz
2018-12-10 18:25:33 +03:00
|
|
|
Create and return an optimizer, and initialize "rehearsal" for any pipeline
|
|
|
|
component that has a .rehearse() method. Rehearsal is used to prevent
|
|
|
|
models from "forgetting" their initialised "knowledge". To perform
|
|
|
|
rehearsal, collect samples of text you want the models to retain performance
|
|
|
|
on, and call nlp.rehearse() with a batch of Doc objects.
|
|
|
|
"""
|
|
|
|
if cfg.get("device", -1) >= 0:
|
|
|
|
util.use_gpu(cfg["device"])
|
|
|
|
if self.vocab.vectors.data.shape[1] >= 1:
|
|
|
|
self.vocab.vectors.data = Model.ops.asarray(self.vocab.vectors.data)
|
|
|
|
link_vectors_to_models(self.vocab)
|
|
|
|
if self.vocab.vectors.data.shape[1]:
|
|
|
|
cfg["pretrained_vectors"] = self.vocab.vectors.name
|
|
|
|
if sgd is None:
|
|
|
|
sgd = create_default_optimizer(Model.ops)
|
|
|
|
self._optimizer = sgd
|
|
|
|
for name, proc in self.pipeline:
|
|
|
|
if hasattr(proc, "_rehearsal_model"):
|
|
|
|
proc._rehearsal_model = deepcopy(proc.model)
|
|
|
|
return self._optimizer
|
|
|
|
|
2019-03-11 01:36:47 +03:00
|
|
|
def evaluate(
|
|
|
|
self, docs_golds, verbose=False, batch_size=256, scorer=None, component_cfg=None
|
|
|
|
):
|
2019-05-24 15:06:36 +03:00
|
|
|
"""Evaluate a model's pipeline components.
|
|
|
|
|
|
|
|
docs_golds (iterable): Tuples of `Doc` and `GoldParse` objects.
|
|
|
|
verbose (bool): Print debugging information.
|
|
|
|
batch_size (int): Batch size to use.
|
|
|
|
scorer (Scorer): Optional `Scorer` to use. If not passed in, a new one
|
|
|
|
will be created.
|
|
|
|
component_cfg (dict): An optional dictionary with extra keyword
|
|
|
|
arguments for specific components.
|
|
|
|
RETURNS (Scorer): The scorer containing the evaluation results.
|
|
|
|
|
|
|
|
DOCS: https://spacy.io/api/language#evaluate
|
|
|
|
"""
|
2019-03-11 01:36:47 +03:00
|
|
|
if scorer is None:
|
2019-09-15 23:31:31 +03:00
|
|
|
scorer = Scorer(pipeline=self.pipeline)
|
2019-03-15 17:20:09 +03:00
|
|
|
if component_cfg is None:
|
|
|
|
component_cfg = {}
|
2017-08-18 23:26:12 +03:00
|
|
|
docs, golds = zip(*docs_golds)
|
2019-08-01 18:13:01 +03:00
|
|
|
docs = [
|
|
|
|
self.make_doc(doc) if isinstance(doc, basestring_) else doc for doc in docs
|
|
|
|
]
|
2017-08-18 23:26:12 +03:00
|
|
|
golds = list(golds)
|
2017-10-07 01:25:54 +03:00
|
|
|
for name, pipe in self.pipeline:
|
2019-03-11 01:36:47 +03:00
|
|
|
kwargs = component_cfg.get(name, {})
|
|
|
|
kwargs.setdefault("batch_size", batch_size)
|
💫 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 19:03:03 +03:00
|
|
|
if not hasattr(pipe, "pipe"):
|
2019-11-16 22:20:37 +03:00
|
|
|
docs = _pipe(docs, pipe, kwargs)
|
2017-08-18 23:26:12 +03:00
|
|
|
else:
|
2019-03-11 01:36:47 +03:00
|
|
|
docs = pipe.pipe(docs, **kwargs)
|
2017-08-18 23:26:12 +03:00
|
|
|
for doc, gold in zip(docs, golds):
|
2019-07-27 18:30:18 +03:00
|
|
|
if not isinstance(gold, GoldParse):
|
|
|
|
gold = GoldParse(doc, **gold)
|
2017-10-03 17:14:57 +03:00
|
|
|
if verbose:
|
|
|
|
print(doc)
|
2019-03-11 01:36:47 +03:00
|
|
|
kwargs = component_cfg.get("scorer", {})
|
|
|
|
kwargs.setdefault("verbose", verbose)
|
|
|
|
scorer.score(doc, gold, **kwargs)
|
2017-05-21 17:07:06 +03:00
|
|
|
return scorer
|
2017-05-16 12:21:59 +03:00
|
|
|
|
2017-05-18 12:25:19 +03:00
|
|
|
@contextmanager
|
|
|
|
def use_params(self, params, **cfg):
|
2017-05-19 00:57:38 +03:00
|
|
|
"""Replace weights of models in the pipeline with those provided in the
|
|
|
|
params dictionary. Can be used as a contextmanager, in which case,
|
|
|
|
models go back to their original weights after the block.
|
|
|
|
|
|
|
|
params (dict): A dictionary of parameters keyed by model ID.
|
|
|
|
**cfg: Config parameters.
|
|
|
|
|
|
|
|
EXAMPLE:
|
|
|
|
>>> with nlp.use_params(optimizer.averages):
|
|
|
|
>>> nlp.to_disk('/tmp/checkpoint')
|
|
|
|
"""
|
💫 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 19:03:03 +03:00
|
|
|
contexts = [
|
|
|
|
pipe.use_params(params)
|
|
|
|
for name, pipe in self.pipeline
|
|
|
|
if hasattr(pipe, "use_params")
|
|
|
|
]
|
2017-05-18 16:30:59 +03:00
|
|
|
# TODO: Having trouble with contextlib
|
|
|
|
# Workaround: these aren't actually context managers atm.
|
|
|
|
for context in contexts:
|
|
|
|
try:
|
|
|
|
next(context)
|
|
|
|
except StopIteration:
|
|
|
|
pass
|
2017-05-18 12:25:19 +03:00
|
|
|
yield
|
|
|
|
for context in contexts:
|
|
|
|
try:
|
2017-05-18 16:30:59 +03:00
|
|
|
next(context)
|
2017-05-18 12:25:19 +03:00
|
|
|
except StopIteration:
|
|
|
|
pass
|
|
|
|
|
💫 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 19:03:03 +03:00
|
|
|
def pipe(
|
|
|
|
self,
|
|
|
|
texts,
|
|
|
|
as_tuples=False,
|
2019-03-15 18:24:26 +03:00
|
|
|
n_threads=-1,
|
💫 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 19:03:03 +03:00
|
|
|
batch_size=1000,
|
|
|
|
disable=[],
|
|
|
|
cleanup=False,
|
2019-03-11 01:36:47 +03:00
|
|
|
component_cfg=None,
|
2019-10-08 13:20:55 +03:00
|
|
|
n_process=1,
|
💫 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 19:03:03 +03:00
|
|
|
):
|
2017-10-27 15:40:14 +03:00
|
|
|
"""Process texts as a stream, and yield `Doc` objects in order.
|
2017-05-19 00:57:38 +03:00
|
|
|
|
|
|
|
texts (iterator): A sequence of texts to process.
|
2019-03-15 18:23:17 +03:00
|
|
|
as_tuples (bool): If set to True, inputs should be a sequence of
|
2017-08-19 13:21:33 +03:00
|
|
|
(text, context) tuples. Output will then be a sequence of
|
|
|
|
(doc, context) tuples. Defaults to False.
|
2017-05-19 00:57:38 +03:00
|
|
|
batch_size (int): The number of texts to buffer.
|
2017-05-26 13:33:54 +03:00
|
|
|
disable (list): Names of the pipeline components to disable.
|
2019-03-15 18:23:17 +03:00
|
|
|
cleanup (bool): If True, unneeded strings are freed to control memory
|
|
|
|
use. Experimental.
|
|
|
|
component_cfg (dict): An optional dictionary with extra keyword
|
|
|
|
arguments for specific components.
|
2019-10-18 12:33:38 +03:00
|
|
|
n_process (int): Number of processors to process texts, only supported
|
|
|
|
in Python3. If -1, set `multiprocessing.cpu_count()`.
|
2017-05-19 00:57:38 +03:00
|
|
|
YIELDS (Doc): Documents in the order of the original text.
|
|
|
|
|
2019-03-15 18:23:17 +03:00
|
|
|
DOCS: https://spacy.io/api/language#pipe
|
2017-04-15 12:59:21 +03:00
|
|
|
"""
|
2019-10-08 13:20:55 +03:00
|
|
|
if is_python2 and n_process != 1:
|
2020-04-28 14:37:37 +03:00
|
|
|
warnings.warn(Warnings.W023)
|
2019-10-08 13:20:55 +03:00
|
|
|
n_process = 1
|
2019-03-15 18:38:44 +03:00
|
|
|
if n_threads != -1:
|
2020-04-28 14:37:37 +03:00
|
|
|
warnings.warn(Warnings.W016, DeprecationWarning)
|
2019-10-08 13:20:55 +03:00
|
|
|
if n_process == -1:
|
|
|
|
n_process = mp.cpu_count()
|
2017-08-19 13:21:33 +03:00
|
|
|
if as_tuples:
|
2017-07-25 19:57:59 +03:00
|
|
|
text_context1, text_context2 = itertools.tee(texts)
|
|
|
|
texts = (tc[0] for tc in text_context1)
|
|
|
|
contexts = (tc[1] for tc in text_context2)
|
💫 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 19:03:03 +03:00
|
|
|
docs = self.pipe(
|
2019-03-11 01:36:47 +03:00
|
|
|
texts,
|
|
|
|
batch_size=batch_size,
|
|
|
|
disable=disable,
|
2019-11-04 22:29:03 +03:00
|
|
|
n_process=n_process,
|
2019-03-11 01:36:47 +03:00
|
|
|
component_cfg=component_cfg,
|
💫 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 19:03:03 +03:00
|
|
|
)
|
2017-07-25 19:57:59 +03:00
|
|
|
for doc, context in izip(docs, contexts):
|
|
|
|
yield (doc, context)
|
|
|
|
return
|
2019-03-11 01:36:47 +03:00
|
|
|
if component_cfg is None:
|
|
|
|
component_cfg = {}
|
2019-10-08 13:20:55 +03:00
|
|
|
|
|
|
|
pipes = (
|
|
|
|
[]
|
2020-01-06 16:57:34 +03:00
|
|
|
) # contains functools.partial objects to easily create multiprocess worker.
|
2017-10-07 01:25:54 +03:00
|
|
|
for name, proc in self.pipeline:
|
2017-05-26 13:33:54 +03:00
|
|
|
if name in disable:
|
2017-05-16 12:21:59 +03:00
|
|
|
continue
|
2019-03-11 01:36:47 +03:00
|
|
|
kwargs = component_cfg.get(name, {})
|
|
|
|
# Allow component_cfg to overwrite the top-level kwargs.
|
|
|
|
kwargs.setdefault("batch_size", batch_size)
|
💫 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 19:03:03 +03:00
|
|
|
if hasattr(proc, "pipe"):
|
2019-10-08 13:20:55 +03:00
|
|
|
f = functools.partial(proc.pipe, **kwargs)
|
2017-05-16 12:21:59 +03:00
|
|
|
else:
|
2017-05-22 02:43:31 +03:00
|
|
|
# Apply the function, but yield the doc
|
2019-10-08 13:20:55 +03:00
|
|
|
f = functools.partial(_pipe, proc=proc, kwargs=kwargs)
|
|
|
|
pipes.append(f)
|
|
|
|
|
|
|
|
if n_process != 1:
|
|
|
|
docs = self._multiprocessing_pipe(texts, pipes, n_process, batch_size)
|
|
|
|
else:
|
|
|
|
# if n_process == 1, no processes are forked.
|
|
|
|
docs = (self.make_doc(text) for text in texts)
|
|
|
|
for pipe in pipes:
|
|
|
|
docs = pipe(docs)
|
|
|
|
|
2017-10-16 20:22:40 +03:00
|
|
|
# Track weakrefs of "recent" documents, so that we can see when they
|
|
|
|
# expire from memory. When they do, we know we don't need old strings.
|
|
|
|
# This way, we avoid maintaining an unbounded growth in string entries
|
|
|
|
# in the string store.
|
|
|
|
recent_refs = weakref.WeakSet()
|
|
|
|
old_refs = weakref.WeakSet()
|
2017-11-23 15:19:18 +03:00
|
|
|
# Keep track of the original string data, so that if we flush old strings,
|
|
|
|
# we can recover the original ones. However, we only want to do this if we're
|
|
|
|
# really adding strings, to save up-front costs.
|
|
|
|
original_strings_data = None
|
2017-10-16 20:22:40 +03:00
|
|
|
nr_seen = 0
|
2017-05-19 21:25:42 +03:00
|
|
|
for doc in docs:
|
2016-02-03 04:04:55 +03:00
|
|
|
yield doc
|
2017-11-23 15:19:18 +03:00
|
|
|
if cleanup:
|
|
|
|
recent_refs.add(doc)
|
|
|
|
if nr_seen < 10000:
|
|
|
|
old_refs.add(doc)
|
|
|
|
nr_seen += 1
|
|
|
|
elif len(old_refs) == 0:
|
|
|
|
old_refs, recent_refs = recent_refs, old_refs
|
|
|
|
if original_strings_data is None:
|
|
|
|
original_strings_data = list(self.vocab.strings)
|
|
|
|
else:
|
💫 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 19:03:03 +03:00
|
|
|
keys, strings = self.vocab.strings._cleanup_stale_strings(
|
|
|
|
original_strings_data
|
|
|
|
)
|
2017-11-23 15:19:18 +03:00
|
|
|
self.vocab._reset_cache(keys, strings)
|
|
|
|
self.tokenizer._reset_cache(keys)
|
|
|
|
nr_seen = 0
|
2016-02-01 11:01:13 +03:00
|
|
|
|
2019-10-08 13:20:55 +03:00
|
|
|
def _multiprocessing_pipe(self, texts, pipes, n_process, batch_size):
|
|
|
|
# raw_texts is used later to stop iteration.
|
|
|
|
texts, raw_texts = itertools.tee(texts)
|
|
|
|
# for sending texts to worker
|
|
|
|
texts_q = [mp.Queue() for _ in range(n_process)]
|
2020-01-06 16:57:34 +03:00
|
|
|
# for receiving byte-encoded docs from worker
|
2019-10-08 13:20:55 +03:00
|
|
|
bytedocs_recv_ch, bytedocs_send_ch = zip(
|
|
|
|
*[mp.Pipe(False) for _ in range(n_process)]
|
|
|
|
)
|
|
|
|
|
|
|
|
batch_texts = minibatch(texts, batch_size)
|
|
|
|
# Sender sends texts to the workers.
|
|
|
|
# This is necessary to properly handle infinite length of texts.
|
|
|
|
# (In this case, all data cannot be sent to the workers at once)
|
|
|
|
sender = _Sender(batch_texts, texts_q, chunk_size=n_process)
|
2020-01-06 16:57:34 +03:00
|
|
|
# send twice to make process busy
|
2019-10-08 13:20:55 +03:00
|
|
|
sender.send()
|
|
|
|
sender.send()
|
|
|
|
|
|
|
|
procs = [
|
2020-02-12 13:50:42 +03:00
|
|
|
mp.Process(
|
|
|
|
target=_apply_pipes,
|
2020-03-25 14:28:12 +03:00
|
|
|
args=(
|
|
|
|
self.make_doc,
|
|
|
|
pipes,
|
|
|
|
rch,
|
|
|
|
sch,
|
|
|
|
Underscore.get_state(),
|
|
|
|
load_nlp.VECTORS,
|
|
|
|
),
|
2020-02-12 13:50:42 +03:00
|
|
|
)
|
2019-10-08 13:20:55 +03:00
|
|
|
for rch, sch in zip(texts_q, bytedocs_send_ch)
|
|
|
|
]
|
|
|
|
for proc in procs:
|
|
|
|
proc.start()
|
|
|
|
|
|
|
|
# Cycle channels not to break the order of docs.
|
2020-01-06 16:57:34 +03:00
|
|
|
# The received object is a batch of byte-encoded docs, so flatten them with chain.from_iterable.
|
2019-10-08 13:20:55 +03:00
|
|
|
byte_docs = chain.from_iterable(recv.recv() for recv in cycle(bytedocs_recv_ch))
|
|
|
|
docs = (Doc(self.vocab).from_bytes(byte_doc) for byte_doc in byte_docs)
|
|
|
|
try:
|
|
|
|
for i, (_, doc) in enumerate(zip(raw_texts, docs), 1):
|
|
|
|
yield doc
|
|
|
|
if i % batch_size == 0:
|
|
|
|
# tell `sender` that one batch was consumed.
|
|
|
|
sender.step()
|
|
|
|
finally:
|
|
|
|
for proc in procs:
|
|
|
|
proc.terminate()
|
|
|
|
|
2019-03-10 21:16:45 +03:00
|
|
|
def to_disk(self, path, exclude=tuple(), disable=None):
|
2017-05-26 13:33:54 +03:00
|
|
|
"""Save the current state to a directory. If a model is loaded, this
|
|
|
|
will include the model.
|
2017-04-17 02:40:26 +03:00
|
|
|
|
2019-03-10 21:16:45 +03:00
|
|
|
path (unicode or Path): Path to a directory, which will be created if
|
|
|
|
it doesn't exist.
|
|
|
|
exclude (list): Names of components or serialization fields to exclude.
|
2017-05-19 00:57:38 +03:00
|
|
|
|
2019-03-10 21:16:45 +03:00
|
|
|
DOCS: https://spacy.io/api/language#to_disk
|
2017-05-17 13:04:50 +03:00
|
|
|
"""
|
2019-03-10 21:16:45 +03:00
|
|
|
if disable is not None:
|
2020-04-28 14:37:37 +03:00
|
|
|
warnings.warn(Warnings.W014, DeprecationWarning)
|
2019-03-10 21:16:45 +03:00
|
|
|
exclude = disable
|
2017-05-17 13:04:50 +03:00
|
|
|
path = util.ensure_path(path)
|
2019-03-10 21:16:45 +03:00
|
|
|
serializers = OrderedDict()
|
2019-08-01 18:13:01 +03:00
|
|
|
serializers["tokenizer"] = lambda p: self.tokenizer.to_disk(
|
|
|
|
p, exclude=["vocab"]
|
|
|
|
)
|
|
|
|
serializers["meta.json"] = lambda p: p.open("w").write(
|
|
|
|
srsly.json_dumps(self.meta)
|
|
|
|
)
|
2017-10-07 01:25:54 +03:00
|
|
|
for name, proc in self.pipeline:
|
💫 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 19:03:03 +03:00
|
|
|
if not hasattr(proc, "name"):
|
2017-05-31 14:42:39 +03:00
|
|
|
continue
|
2019-03-10 21:16:45 +03:00
|
|
|
if name in exclude:
|
2017-05-31 14:42:39 +03:00
|
|
|
continue
|
💫 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 19:03:03 +03:00
|
|
|
if not hasattr(proc, "to_disk"):
|
2017-05-31 14:42:39 +03:00
|
|
|
continue
|
2019-03-10 21:16:45 +03:00
|
|
|
serializers[name] = lambda p, proc=proc: proc.to_disk(p, exclude=["vocab"])
|
💫 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 19:03:03 +03:00
|
|
|
serializers["vocab"] = lambda p: self.vocab.to_disk(p)
|
2019-03-10 21:16:45 +03:00
|
|
|
util.to_disk(path, serializers, exclude)
|
2017-05-31 14:42:39 +03:00
|
|
|
|
2019-03-10 21:16:45 +03:00
|
|
|
def from_disk(self, path, exclude=tuple(), disable=None):
|
2017-05-19 00:57:38 +03:00
|
|
|
"""Loads state from a directory. Modifies the object in place and
|
2017-05-26 13:33:54 +03:00
|
|
|
returns it. If the saved `Language` object contains a model, the
|
|
|
|
model will be loaded.
|
2017-05-17 13:04:50 +03:00
|
|
|
|
2019-03-10 21:16:45 +03:00
|
|
|
path (unicode or Path): A path to a directory.
|
|
|
|
exclude (list): Names of components or serialization fields to exclude.
|
2017-05-19 00:57:38 +03:00
|
|
|
RETURNS (Language): The modified `Language` object.
|
2017-05-17 13:04:50 +03:00
|
|
|
|
2019-03-10 21:16:45 +03:00
|
|
|
DOCS: https://spacy.io/api/language#from_disk
|
2017-05-17 13:04:50 +03:00
|
|
|
"""
|
2019-03-10 21:16:45 +03:00
|
|
|
if disable is not None:
|
2020-04-28 14:37:37 +03:00
|
|
|
warnings.warn(Warnings.W014, DeprecationWarning)
|
2019-03-10 21:16:45 +03:00
|
|
|
exclude = disable
|
2017-05-17 13:04:50 +03:00
|
|
|
path = util.ensure_path(path)
|
2019-03-10 21:16:45 +03:00
|
|
|
deserializers = OrderedDict()
|
|
|
|
deserializers["meta.json"] = lambda p: self.meta.update(srsly.read_json(p))
|
2019-08-01 18:13:01 +03:00
|
|
|
deserializers["vocab"] = lambda p: self.vocab.from_disk(
|
|
|
|
p
|
|
|
|
) and _fix_pretrained_vectors_name(self)
|
|
|
|
deserializers["tokenizer"] = lambda p: self.tokenizer.from_disk(
|
|
|
|
p, exclude=["vocab"]
|
|
|
|
)
|
2017-10-07 01:25:54 +03:00
|
|
|
for name, proc in self.pipeline:
|
2019-03-10 21:16:45 +03:00
|
|
|
if name in exclude:
|
2017-05-31 14:42:39 +03:00
|
|
|
continue
|
💫 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 19:03:03 +03:00
|
|
|
if not hasattr(proc, "from_disk"):
|
2017-05-31 14:42:39 +03:00
|
|
|
continue
|
2019-08-01 18:13:01 +03:00
|
|
|
deserializers[name] = lambda p, proc=proc: proc.from_disk(
|
|
|
|
p, exclude=["vocab"]
|
|
|
|
)
|
2019-03-10 21:16:45 +03:00
|
|
|
if not (path / "vocab").exists() and "vocab" not in exclude:
|
|
|
|
# Convert to list here in case exclude is (default) tuple
|
|
|
|
exclude = list(exclude) + ["vocab"]
|
2017-06-01 15:38:35 +03:00
|
|
|
util.from_disk(path, deserializers, exclude)
|
2017-10-25 12:57:43 +03:00
|
|
|
self._path = path
|
2017-05-31 14:42:39 +03:00
|
|
|
return self
|
2017-05-17 13:04:50 +03:00
|
|
|
|
2019-03-10 21:16:45 +03:00
|
|
|
def to_bytes(self, exclude=tuple(), disable=None, **kwargs):
|
2017-05-17 13:04:50 +03:00
|
|
|
"""Serialize the current state to a binary string.
|
2016-12-18 18:54:52 +03:00
|
|
|
|
2019-03-10 21:16:45 +03:00
|
|
|
exclude (list): Names of components or serialization fields to exclude.
|
2017-05-19 00:57:38 +03:00
|
|
|
RETURNS (bytes): The serialized form of the `Language` object.
|
2019-03-10 21:16:45 +03:00
|
|
|
|
|
|
|
DOCS: https://spacy.io/api/language#to_bytes
|
2017-05-17 13:04:50 +03:00
|
|
|
"""
|
2019-03-10 21:16:45 +03:00
|
|
|
if disable is not None:
|
2020-04-28 14:37:37 +03:00
|
|
|
warnings.warn(Warnings.W014, DeprecationWarning)
|
2019-03-10 21:16:45 +03:00
|
|
|
exclude = disable
|
|
|
|
serializers = OrderedDict()
|
|
|
|
serializers["vocab"] = lambda: self.vocab.to_bytes()
|
|
|
|
serializers["tokenizer"] = lambda: self.tokenizer.to_bytes(exclude=["vocab"])
|
2020-04-18 18:01:53 +03:00
|
|
|
serializers["meta.json"] = lambda: srsly.json_dumps(OrderedDict(sorted(self.meta.items())))
|
2019-03-10 21:16:45 +03:00
|
|
|
for name, proc in self.pipeline:
|
|
|
|
if name in exclude:
|
2017-05-29 12:45:45 +03:00
|
|
|
continue
|
💫 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 19:03:03 +03:00
|
|
|
if not hasattr(proc, "to_bytes"):
|
2017-05-29 12:45:45 +03:00
|
|
|
continue
|
2019-03-10 21:16:45 +03:00
|
|
|
serializers[name] = lambda proc=proc: proc.to_bytes(exclude=["vocab"])
|
|
|
|
exclude = util.get_serialization_exclude(serializers, exclude, kwargs)
|
2017-10-17 19:18:10 +03:00
|
|
|
return util.to_bytes(serializers, exclude)
|
2017-04-15 13:05:47 +03:00
|
|
|
|
2019-03-10 21:16:45 +03:00
|
|
|
def from_bytes(self, bytes_data, exclude=tuple(), disable=None, **kwargs):
|
2017-05-17 13:04:50 +03:00
|
|
|
"""Load state from a binary string.
|
|
|
|
|
2017-05-19 00:57:38 +03:00
|
|
|
bytes_data (bytes): The data to load from.
|
2019-03-10 21:16:45 +03:00
|
|
|
exclude (list): Names of components or serialization fields to exclude.
|
2017-05-19 00:57:38 +03:00
|
|
|
RETURNS (Language): The `Language` object.
|
2019-03-10 21:16:45 +03:00
|
|
|
|
|
|
|
DOCS: https://spacy.io/api/language#from_bytes
|
2017-05-17 13:04:50 +03:00
|
|
|
"""
|
2019-03-10 21:16:45 +03:00
|
|
|
if disable is not None:
|
2020-04-28 14:37:37 +03:00
|
|
|
warnings.warn(Warnings.W014, DeprecationWarning)
|
2019-03-10 21:16:45 +03:00
|
|
|
exclude = disable
|
|
|
|
deserializers = OrderedDict()
|
|
|
|
deserializers["meta.json"] = lambda b: self.meta.update(srsly.json_loads(b))
|
2019-08-01 18:13:01 +03:00
|
|
|
deserializers["vocab"] = lambda b: self.vocab.from_bytes(
|
|
|
|
b
|
|
|
|
) and _fix_pretrained_vectors_name(self)
|
|
|
|
deserializers["tokenizer"] = lambda b: self.tokenizer.from_bytes(
|
|
|
|
b, exclude=["vocab"]
|
|
|
|
)
|
2019-03-10 21:16:45 +03:00
|
|
|
for name, proc in self.pipeline:
|
|
|
|
if name in exclude:
|
2017-05-29 12:45:45 +03:00
|
|
|
continue
|
💫 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 19:03:03 +03:00
|
|
|
if not hasattr(proc, "from_bytes"):
|
2017-05-29 12:45:45 +03:00
|
|
|
continue
|
2019-08-01 18:13:01 +03:00
|
|
|
deserializers[name] = lambda b, proc=proc: proc.from_bytes(
|
|
|
|
b, exclude=["vocab"]
|
|
|
|
)
|
2019-03-10 21:16:45 +03:00
|
|
|
exclude = util.get_serialization_exclude(deserializers, exclude, kwargs)
|
|
|
|
util.from_bytes(bytes_data, deserializers, exclude)
|
2017-05-17 13:04:50 +03:00
|
|
|
return self
|
2017-05-22 02:43:31 +03:00
|
|
|
|
2017-05-29 12:45:45 +03:00
|
|
|
|
2019-10-27 15:35:49 +03:00
|
|
|
class component(object):
|
|
|
|
"""Decorator for pipeline components. Can decorate both function components
|
|
|
|
and class components and will automatically register components in the
|
|
|
|
Language.factories. If the component is a class and needs access to the
|
|
|
|
nlp object or config parameters, it can expose a from_nlp classmethod
|
|
|
|
that takes the nlp object and **cfg arguments and returns the initialized
|
|
|
|
component.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# NB: This decorator needs to live here, because it needs to write to
|
|
|
|
# Language.factories. All other solutions would cause circular import.
|
|
|
|
|
|
|
|
def __init__(self, name=None, assigns=tuple(), requires=tuple(), retokenizes=False):
|
|
|
|
"""Decorate a pipeline component.
|
|
|
|
|
|
|
|
name (unicode): Default component and factory name.
|
|
|
|
assigns (list): Attributes assigned by component, e.g. `["token.pos"]`.
|
|
|
|
requires (list): Attributes required by component, e.g. `["token.dep"]`.
|
|
|
|
retokenizes (bool): Whether the component changes the tokenization.
|
|
|
|
"""
|
|
|
|
self.name = name
|
|
|
|
self.assigns = validate_attrs(assigns)
|
|
|
|
self.requires = validate_attrs(requires)
|
|
|
|
self.retokenizes = retokenizes
|
|
|
|
|
|
|
|
def __call__(self, *args, **kwargs):
|
|
|
|
obj = args[0]
|
|
|
|
args = args[1:]
|
|
|
|
factory_name = self.name or util.get_component_name(obj)
|
|
|
|
obj.name = factory_name
|
|
|
|
obj.factory = factory_name
|
|
|
|
obj.assigns = self.assigns
|
|
|
|
obj.requires = self.requires
|
|
|
|
obj.retokenizes = self.retokenizes
|
|
|
|
|
|
|
|
def factory(nlp, **cfg):
|
|
|
|
if hasattr(obj, "from_nlp"):
|
|
|
|
return obj.from_nlp(nlp, **cfg)
|
|
|
|
elif isinstance(obj, class_types):
|
|
|
|
return obj()
|
|
|
|
return obj
|
|
|
|
|
|
|
|
Language.factories[obj.factory] = factory
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
2018-03-28 17:02:59 +03:00
|
|
|
def _fix_pretrained_vectors_name(nlp):
|
|
|
|
# TODO: Replace this once we handle vectors consistently as static
|
|
|
|
# data
|
💫 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 19:03:03 +03:00
|
|
|
if "vectors" in nlp.meta and nlp.meta["vectors"].get("name"):
|
|
|
|
nlp.vocab.vectors.name = nlp.meta["vectors"]["name"]
|
2018-03-28 22:08:58 +03:00
|
|
|
elif not nlp.vocab.vectors.size:
|
|
|
|
nlp.vocab.vectors.name = None
|
💫 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 19:03:03 +03:00
|
|
|
elif "name" in nlp.meta and "lang" in nlp.meta:
|
|
|
|
vectors_name = "%s_%s.vectors" % (nlp.meta["lang"], nlp.meta["name"])
|
2018-03-28 17:02:59 +03:00
|
|
|
nlp.vocab.vectors.name = vectors_name
|
|
|
|
else:
|
2018-04-03 22:40:29 +03:00
|
|
|
raise ValueError(Errors.E092)
|
2018-04-04 02:31:25 +03:00
|
|
|
if nlp.vocab.vectors.size != 0:
|
2020-05-14 19:26:12 +03:00
|
|
|
link_vectors_to_models(nlp.vocab, skip_rank=True)
|
2018-03-28 17:02:59 +03:00
|
|
|
for name, proc in nlp.pipeline:
|
💫 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 19:03:03 +03:00
|
|
|
if not hasattr(proc, "cfg"):
|
2018-03-28 17:02:59 +03:00
|
|
|
continue
|
💫 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 19:03:03 +03:00
|
|
|
proc.cfg.setdefault("deprecation_fixes", {})
|
|
|
|
proc.cfg["deprecation_fixes"]["vectors_name"] = nlp.vocab.vectors.name
|
2018-03-28 17:02:59 +03:00
|
|
|
|
2017-05-29 12:45:45 +03:00
|
|
|
|
2017-10-25 14:46:41 +03:00
|
|
|
class DisabledPipes(list):
|
2017-10-27 15:40:14 +03:00
|
|
|
"""Manager for temporary pipeline disabling."""
|
💫 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 19:03:03 +03:00
|
|
|
|
2017-10-25 14:46:41 +03:00
|
|
|
def __init__(self, nlp, *names):
|
|
|
|
self.nlp = nlp
|
|
|
|
self.names = names
|
|
|
|
# Important! Not deep copy -- we just want the container (but we also
|
|
|
|
# want to support people providing arbitrarily typed nlp.pipeline
|
|
|
|
# objects.)
|
2017-10-27 22:07:59 +03:00
|
|
|
self.original_pipeline = copy(nlp.pipeline)
|
2017-10-25 14:46:41 +03:00
|
|
|
list.__init__(self)
|
|
|
|
self.extend(nlp.remove_pipe(name) for name in names)
|
|
|
|
|
|
|
|
def __enter__(self):
|
2017-10-25 15:56:16 +03:00
|
|
|
return self
|
2017-10-25 14:46:41 +03:00
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
self.restore()
|
|
|
|
|
|
|
|
def restore(self):
|
💫 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 19:03:03 +03:00
|
|
|
"""Restore the pipeline to its state when DisabledPipes was created."""
|
2017-10-25 14:46:41 +03:00
|
|
|
current, self.nlp.pipeline = self.nlp.pipeline, self.original_pipeline
|
💫 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 19:03:03 +03:00
|
|
|
unexpected = [name for name, pipe in current if not self.nlp.has_pipe(name)]
|
2017-10-25 14:46:41 +03:00
|
|
|
if unexpected:
|
|
|
|
# Don't change the pipeline if we're raising an error.
|
|
|
|
self.nlp.pipeline = current
|
2018-04-03 16:50:31 +03:00
|
|
|
raise ValueError(Errors.E008.format(names=unexpected))
|
2017-10-25 14:46:41 +03:00
|
|
|
self[:] = []
|
|
|
|
|
|
|
|
|
2019-10-08 13:20:55 +03:00
|
|
|
def _pipe(docs, proc, kwargs):
|
2019-03-12 15:32:56 +03:00
|
|
|
# We added some args for pipe that __call__ doesn't expect.
|
|
|
|
kwargs = dict(kwargs)
|
|
|
|
for arg in ["n_threads", "batch_size"]:
|
|
|
|
if arg in kwargs:
|
|
|
|
kwargs.pop(arg)
|
2017-05-22 02:43:31 +03:00
|
|
|
for doc in docs:
|
2019-10-08 13:20:55 +03:00
|
|
|
doc = proc(doc, **kwargs)
|
2017-05-22 02:43:31 +03:00
|
|
|
yield doc
|
2019-10-08 13:20:55 +03:00
|
|
|
|
|
|
|
|
2020-03-03 15:58:22 +03:00
|
|
|
def _apply_pipes(make_doc, pipes, receiver, sender, underscore_state, vectors):
|
2019-10-08 13:20:55 +03:00
|
|
|
"""Worker for Language.pipe
|
|
|
|
|
2019-10-18 12:33:38 +03:00
|
|
|
receiver (multiprocessing.Connection): Pipe to receive text. Usually
|
|
|
|
created by `multiprocessing.Pipe()`
|
|
|
|
sender (multiprocessing.Connection): Pipe to send doc. Usually created by
|
|
|
|
`multiprocessing.Pipe()`
|
2020-02-12 14:06:27 +03:00
|
|
|
underscore_state (tuple): The data in the Underscore class of the parent
|
2020-03-03 15:58:22 +03:00
|
|
|
vectors (dict): The global vectors data, copied from the parent
|
2019-10-08 13:20:55 +03:00
|
|
|
"""
|
2020-02-12 13:50:42 +03:00
|
|
|
Underscore.load_state(underscore_state)
|
2020-03-03 15:58:22 +03:00
|
|
|
load_nlp.VECTORS = vectors
|
2019-10-08 13:20:55 +03:00
|
|
|
while True:
|
2020-02-12 13:50:42 +03:00
|
|
|
texts = receiver.get()
|
2019-10-08 13:20:55 +03:00
|
|
|
docs = (make_doc(text) for text in texts)
|
|
|
|
for pipe in pipes:
|
|
|
|
docs = pipe(docs)
|
|
|
|
# Connection does not accept unpickable objects, so send list.
|
|
|
|
sender.send([doc.to_bytes() for doc in docs])
|
|
|
|
|
|
|
|
|
|
|
|
class _Sender:
|
|
|
|
"""Util for sending data to multiprocessing workers in Language.pipe"""
|
|
|
|
|
|
|
|
def __init__(self, data, queues, chunk_size):
|
|
|
|
self.data = iter(data)
|
|
|
|
self.queues = iter(cycle(queues))
|
|
|
|
self.chunk_size = chunk_size
|
|
|
|
self.count = 0
|
|
|
|
|
|
|
|
def send(self):
|
|
|
|
"""Send chunk_size items from self.data to channels."""
|
|
|
|
for item, q in itertools.islice(
|
|
|
|
zip(self.data, cycle(self.queues)), self.chunk_size
|
|
|
|
):
|
|
|
|
# cycle channels so that distribute the texts evenly
|
|
|
|
q.put(item)
|
|
|
|
|
|
|
|
def step(self):
|
2019-10-10 18:08:39 +03:00
|
|
|
"""Tell sender that comsumed one item.
|
2019-10-08 13:20:55 +03:00
|
|
|
|
|
|
|
Data is sent to the workers after every chunk_size calls."""
|
|
|
|
self.count += 1
|
|
|
|
if self.count >= self.chunk_size:
|
|
|
|
self.count = 0
|
|
|
|
self.send()
|