2019-10-27 15:35:49 +03:00
|
|
|
import spacy.language
|
2020-07-22 14:42:59 +03:00
|
|
|
from spacy.language import Language
|
2020-05-22 18:42:06 +03:00
|
|
|
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
|
2020-07-22 14:42:59 +03:00
|
|
|
from mock import Mock
|
2019-10-27 15:35:49 +03:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
|
|
def test_component_decorator_assigns():
|
|
|
|
spacy.language.ENABLE_PIPELINE_ANALYSIS = True
|
|
|
|
|
2020-07-22 14:42:59 +03:00
|
|
|
@Language.component("c1", assigns=["token.tag", "doc.tensor"])
|
2019-10-27 15:35:49 +03:00
|
|
|
def test_component1(doc):
|
|
|
|
return doc
|
|
|
|
|
2020-07-22 14:42:59 +03:00
|
|
|
@Language.component(
|
2019-10-27 15:35:49 +03:00
|
|
|
"c2", requires=["token.tag", "token.pos"], assigns=["token.lemma", "doc.tensor"]
|
|
|
|
)
|
|
|
|
def test_component2(doc):
|
|
|
|
return doc
|
|
|
|
|
2020-07-22 14:42:59 +03:00
|
|
|
@Language.component(
|
|
|
|
"c3", requires=["token.lemma"], assigns=["token._.custom_lemma"]
|
|
|
|
)
|
2019-10-27 15:35:49 +03:00
|
|
|
def test_component3(doc):
|
|
|
|
return doc
|
|
|
|
|
2020-07-22 14:42:59 +03:00
|
|
|
assert Language.has_factory("c1")
|
|
|
|
assert Language.has_factory("c2")
|
|
|
|
assert Language.has_factory("c3")
|
2019-10-27 15:35:49 +03:00
|
|
|
|
|
|
|
nlp = Language()
|
2020-07-22 14:42:59 +03:00
|
|
|
nlp.add_pipe("c1")
|
2019-10-27 15:35:49 +03:00
|
|
|
with pytest.warns(UserWarning):
|
2020-07-22 14:42:59 +03:00
|
|
|
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"
|
2019-10-27 15:35:49 +03:00
|
|
|
assert nlp.pipe_names == ["c1", "c2", "c3", "c4"]
|
2020-07-22 14:42:59 +03:00
|
|
|
assert not Language.has_factory("c4")
|
2019-10-27 15:35:49 +03:00
|
|
|
assert nlp.pipe_factories["c1"] == "c1"
|
|
|
|
assert nlp.pipe_factories["c4"] == "c1"
|
2020-07-22 14:42:59 +03:00
|
|
|
assert get_assigns_for_attr(nlp, "doc.tensor") == ["c1", "c2", "c4"]
|
|
|
|
assert get_requires_for_attr(nlp, "token.pos") == ["c2"]
|
2019-10-27 15:35:49 +03:00
|
|
|
assert print_summary(nlp, no_print=True)
|
|
|
|
assert nlp("hello world")
|
|
|
|
|
|
|
|
|
2020-07-22 14:42:59 +03:00
|
|
|
def test_component_factories_class_func():
|
2019-10-27 15:35:49 +03:00
|
|
|
"""Test that class components can implement a from_nlp classmethod that
|
|
|
|
gives them access to the nlp object and config via the factory."""
|
|
|
|
|
2020-07-12 15:03:23 +03:00
|
|
|
class TestComponent5:
|
2019-10-27 15:35:49 +03:00
|
|
|
def __call__(self, doc):
|
|
|
|
return doc
|
|
|
|
|
|
|
|
mock = Mock()
|
|
|
|
mock.return_value = TestComponent5()
|
|
|
|
|
2020-07-22 14:42:59 +03:00
|
|
|
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")
|
2019-10-27 15:35:49 +03:00
|
|
|
nlp = Language()
|
2020-07-22 14:42:59 +03:00
|
|
|
nlp.add_pipe("c5", config={"foo": "bar"})
|
2019-10-27 15:35:49 +03:00
|
|
|
assert nlp("hello world")
|
2020-07-22 14:42:59 +03:00
|
|
|
mock.assert_called_once_with(nlp, foo="bar")
|
2019-10-27 15:35:49 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_analysis_validate_attrs_valid():
|
2019-10-30 19:19:36 +03:00
|
|
|
attrs = ["doc.sents", "doc.ents", "token.tag", "token._.xyz", "span._.xyz"]
|
2019-10-27 15:35:49 +03:00
|
|
|
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",
|
2019-10-30 19:19:36 +03:00
|
|
|
"span.label",
|
2019-10-27 15:35:49 +03:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_analysis_validate_attrs_invalid(attr):
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
validate_attrs([attr])
|
2019-10-30 21:04:17 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_analysis_validate_attrs_remove_pipe():
|
|
|
|
"""Test that attributes are validated correctly on remove."""
|
|
|
|
spacy.language.ENABLE_PIPELINE_ANALYSIS = True
|
|
|
|
|
2020-07-22 14:42:59 +03:00
|
|
|
@Language.component("pipe_analysis_c6", assigns=["token.tag"])
|
2019-10-30 21:04:17 +03:00
|
|
|
def c1(doc):
|
|
|
|
return doc
|
|
|
|
|
2020-07-22 14:42:59 +03:00
|
|
|
@Language.component("pipe_analysis_c7", requires=["token.pos"])
|
2019-10-30 21:04:17 +03:00
|
|
|
def c2(doc):
|
|
|
|
return doc
|
|
|
|
|
|
|
|
nlp = Language()
|
2020-07-22 14:42:59 +03:00
|
|
|
nlp.add_pipe("pipe_analysis_c6")
|
2019-10-30 21:04:17 +03:00
|
|
|
with pytest.warns(UserWarning):
|
2020-07-22 14:42:59 +03:00
|
|
|
nlp.add_pipe("pipe_analysis_c7")
|
2019-10-30 21:04:17 +03:00
|
|
|
with pytest.warns(None) as record:
|
2020-07-22 14:42:59 +03:00
|
|
|
nlp.remove_pipe("pipe_analysis_c7")
|
2019-10-30 21:04:17 +03:00
|
|
|
assert not record.list
|
2020-05-22 17:43:18 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_pipe_interdependencies():
|
2020-07-22 14:42:59 +03:00
|
|
|
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)
|
2020-05-22 17:43:18 +03:00
|
|
|
assert counts == [1, 0]
|