mirror of
https://github.com/explosion/spaCy.git
synced 2024-12-27 10:26:35 +03:00
43b960c01b
* Update with WIP * Update with WIP * Update with pipeline serialization * Update types and pipe factories * Add deep merge, tidy up and add tests * Fix pipe creation from config * Don't validate default configs on load * Update spacy/language.py Co-authored-by: Ines Montani <ines@ines.io> * Adjust factory/component meta error * Clean up factory args and remove defaults * Add test for failing empty dict defaults * Update pipeline handling and methods * provide KB as registry function instead of as object * small change in test to make functionality more clear * update example script for EL configuration * Fix typo * Simplify test * Simplify test * splitting pipes.pyx into separate files * moving default configs to each component file * fix batch_size type * removing default values from component constructors where possible (TODO: test 4725) * skip instead of xfail * Add test for config -> nlp with multiple instances * pipeline.pipes -> pipeline.pipe * Tidy up, document, remove kwargs * small cleanup/generalization for Tok2VecListener * use DEFAULT_UPSTREAM field * revert to avoid circular imports * Fix tests * Replace deprecated arg * Make model dirs require config * fix pickling of keyword-only arguments in constructor * WIP: clean up and integrate full config * Add helper to handle function args more reliably Now also includes keyword-only args * Fix config composition and serialization * Improve config debugging and add visual diff * Remove unused defaults and fix type * Remove pipeline and factories from meta * Update spacy/default_config.cfg Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com> * Update spacy/default_config.cfg * small UX edits * avoid printing stack trace for debug CLI commands * Add support for language-specific factories * specify the section of the config which holds the model to debug * WIP: add Language.from_config * Update with language data refactor WIP * Auto-format * Add backwards-compat handling for Language.factories * Update morphologizer.pyx * Fix morphologizer * Update and simplify lemmatizers * Fix Japanese tests * Port over tagger changes * Fix Chinese and tests * Update to latest Thinc * WIP: xfail first Russian lemmatizer test * Fix component-specific overrides * fix nO for output layers in debug_model * Fix default value * Fix tests and don't pass objects in config * Fix deep merging * Fix lemma lookup data registry Only load the lookups if an entry is available in the registry (and if spacy-lookups-data is installed) * Add types * Add Vocab.from_config * Fix typo * Fix tests * Make config copying more elegant * Fix pipe analysis * Fix lemmatizers and is_base_form * WIP: move language defaults to config * Fix morphology type * Fix vocab * Remove comment * Update to latest Thinc * Add morph rules to config * Tidy up * Remove set_morphology option from tagger factory * Hack use_gpu * Move [pipeline] to top-level block and make [nlp.pipeline] list Allows separating component blocks from component order – otherwise, ordering the config would mean a changed component order, which is bad. Also allows initial config to define more components and not use all of them * Fix use_gpu and resume in CLI * Auto-format * Remove resume from config * Fix formatting and error * [pipeline] -> [components] * Fix types * Fix tagger test: requires set_morphology? Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com> Co-authored-by: svlandeg <sofie.vanlandeghem@gmail.com> Co-authored-by: Matthew Honnibal <honnibal+gh@gmail.com>
138 lines
4.0 KiB
Python
138 lines
4.0 KiB
Python
import spacy.language
|
|
from spacy.language import Language
|
|
from spacy.pipe_analysis import print_summary, validate_attrs
|
|
from spacy.pipe_analysis import get_assigns_for_attr, get_requires_for_attr
|
|
from spacy.pipe_analysis import count_pipeline_interdependencies
|
|
from mock import Mock
|
|
import pytest
|
|
|
|
|
|
def test_component_decorator_assigns():
|
|
spacy.language.ENABLE_PIPELINE_ANALYSIS = True
|
|
|
|
@Language.component("c1", assigns=["token.tag", "doc.tensor"])
|
|
def test_component1(doc):
|
|
return doc
|
|
|
|
@Language.component(
|
|
"c2", requires=["token.tag", "token.pos"], assigns=["token.lemma", "doc.tensor"]
|
|
)
|
|
def test_component2(doc):
|
|
return doc
|
|
|
|
@Language.component(
|
|
"c3", requires=["token.lemma"], assigns=["token._.custom_lemma"]
|
|
)
|
|
def test_component3(doc):
|
|
return doc
|
|
|
|
assert Language.has_factory("c1")
|
|
assert Language.has_factory("c2")
|
|
assert Language.has_factory("c3")
|
|
|
|
nlp = Language()
|
|
nlp.add_pipe("c1")
|
|
with pytest.warns(UserWarning):
|
|
nlp.add_pipe("c2")
|
|
nlp.add_pipe("c3")
|
|
assert get_assigns_for_attr(nlp, "doc.tensor") == ["c1", "c2"]
|
|
nlp.add_pipe("c1", name="c4")
|
|
test_component4_meta = nlp.get_pipe_meta("c1")
|
|
assert test_component4_meta.factory == "c1"
|
|
assert nlp.pipe_names == ["c1", "c2", "c3", "c4"]
|
|
assert not Language.has_factory("c4")
|
|
assert nlp.pipe_factories["c1"] == "c1"
|
|
assert nlp.pipe_factories["c4"] == "c1"
|
|
assert get_assigns_for_attr(nlp, "doc.tensor") == ["c1", "c2", "c4"]
|
|
assert get_requires_for_attr(nlp, "token.pos") == ["c2"]
|
|
assert print_summary(nlp, no_print=True)
|
|
assert nlp("hello world")
|
|
|
|
|
|
def test_component_factories_class_func():
|
|
"""Test that class components can implement a from_nlp classmethod that
|
|
gives them access to the nlp object and config via the factory."""
|
|
|
|
class TestComponent5:
|
|
def __call__(self, doc):
|
|
return doc
|
|
|
|
mock = Mock()
|
|
mock.return_value = TestComponent5()
|
|
|
|
def test_componen5_factory(nlp, foo: str = "bar", name="c5"):
|
|
return mock(nlp, foo=foo)
|
|
|
|
Language.factory("c5", func=test_componen5_factory)
|
|
assert Language.has_factory("c5")
|
|
nlp = Language()
|
|
nlp.add_pipe("c5", config={"foo": "bar"})
|
|
assert nlp("hello world")
|
|
mock.assert_called_once_with(nlp, foo="bar")
|
|
|
|
|
|
def test_analysis_validate_attrs_valid():
|
|
attrs = ["doc.sents", "doc.ents", "token.tag", "token._.xyz", "span._.xyz"]
|
|
assert validate_attrs(attrs)
|
|
for attr in attrs:
|
|
assert validate_attrs([attr])
|
|
with pytest.raises(ValueError):
|
|
validate_attrs(["doc.sents", "doc.xyz"])
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"attr",
|
|
[
|
|
"doc",
|
|
"doc_ents",
|
|
"doc.xyz",
|
|
"token.xyz",
|
|
"token.tag_",
|
|
"token.tag.xyz",
|
|
"token._.xyz.abc",
|
|
"span.label",
|
|
],
|
|
)
|
|
def test_analysis_validate_attrs_invalid(attr):
|
|
with pytest.raises(ValueError):
|
|
validate_attrs([attr])
|
|
|
|
|
|
def test_analysis_validate_attrs_remove_pipe():
|
|
"""Test that attributes are validated correctly on remove."""
|
|
spacy.language.ENABLE_PIPELINE_ANALYSIS = True
|
|
|
|
@Language.component("pipe_analysis_c6", assigns=["token.tag"])
|
|
def c1(doc):
|
|
return doc
|
|
|
|
@Language.component("pipe_analysis_c7", requires=["token.pos"])
|
|
def c2(doc):
|
|
return doc
|
|
|
|
nlp = Language()
|
|
nlp.add_pipe("pipe_analysis_c6")
|
|
with pytest.warns(UserWarning):
|
|
nlp.add_pipe("pipe_analysis_c7")
|
|
with pytest.warns(None) as record:
|
|
nlp.remove_pipe("pipe_analysis_c7")
|
|
assert not record.list
|
|
|
|
|
|
def test_pipe_interdependencies():
|
|
prefix = "test_pipe_interdependencies"
|
|
|
|
@Language.component(f"{prefix}.fancifier", assigns=("doc._.fancy",))
|
|
def fancifier(doc):
|
|
return doc
|
|
|
|
@Language.component(f"{prefix}.needer", requires=("doc._.fancy",))
|
|
def needer(doc):
|
|
return doc
|
|
|
|
nlp = Language()
|
|
nlp.add_pipe(f"{prefix}.fancifier")
|
|
nlp.add_pipe(f"{prefix}.needer")
|
|
counts = count_pipeline_interdependencies(nlp)
|
|
assert counts == [1, 0]
|