2020-08-07 16:27:13 +03:00
|
|
|
import pytest
|
|
|
|
from spacy import registry
|
|
|
|
from spacy.lookups import Lookups
|
|
|
|
from spacy.util import get_lang_class
|
|
|
|
|
|
|
|
|
|
|
|
# fmt: off
|
|
|
|
# Only include languages with no external dependencies
|
|
|
|
# excluded: ru, uk
|
|
|
|
# excluded for custom tables: pl
|
2020-09-16 18:37:29 +03:00
|
|
|
LANGUAGES = ["bn", "el", "en", "fa", "fr", "nb", "nl", "sv"]
|
2020-08-07 16:27:13 +03:00
|
|
|
# fmt: on
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("lang", LANGUAGES)
|
|
|
|
def test_lemmatizer_initialize(lang, capfd):
|
2020-09-03 18:31:14 +03:00
|
|
|
@registry.misc("lemmatizer_init_lookups")
|
2020-08-07 16:27:13 +03:00
|
|
|
def lemmatizer_init_lookups():
|
|
|
|
lookups = Lookups()
|
2020-10-02 16:42:36 +03:00
|
|
|
lookups.add_table("lemma_lookup", {"cope": "cope", "x": "y"})
|
2020-08-07 16:27:13 +03:00
|
|
|
lookups.add_table("lemma_index", {"verb": ("cope", "cop")})
|
|
|
|
lookups.add_table("lemma_exc", {"verb": {"coping": ("cope",)}})
|
|
|
|
lookups.add_table("lemma_rules", {"verb": [["ing", ""]]})
|
|
|
|
return lookups
|
|
|
|
|
2020-10-03 18:16:10 +03:00
|
|
|
lang_cls = get_lang_class(lang)
|
2020-10-02 16:42:36 +03:00
|
|
|
# Test that languages can be initialized
|
2020-10-03 18:16:10 +03:00
|
|
|
nlp = lang_cls()
|
2020-10-02 16:42:36 +03:00
|
|
|
lemmatizer = nlp.add_pipe("lemmatizer", config={"mode": "lookup"})
|
|
|
|
assert not lemmatizer.lookups.tables
|
|
|
|
nlp.config["initialize"]["components"]["lemmatizer"] = {
|
|
|
|
"lookups": {"@misc": "lemmatizer_init_lookups"}
|
|
|
|
}
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
nlp("x")
|
|
|
|
nlp.initialize()
|
|
|
|
assert lemmatizer.lookups.tables
|
|
|
|
doc = nlp("x")
|
2020-08-07 16:27:13 +03:00
|
|
|
# Check for stray print statements (see #3342)
|
|
|
|
captured = capfd.readouterr()
|
|
|
|
assert not captured.out
|
2020-10-02 16:42:36 +03:00
|
|
|
assert doc[0].lemma_ == "y"
|
|
|
|
|
|
|
|
# Test initialization by calling .initialize() directly
|
2020-10-03 18:16:10 +03:00
|
|
|
nlp = lang_cls()
|
2020-10-02 16:42:36 +03:00
|
|
|
lemmatizer = nlp.add_pipe("lemmatizer", config={"mode": "lookup"})
|
|
|
|
lemmatizer.initialize(lookups=lemmatizer_init_lookups())
|
|
|
|
assert nlp("x")[0].lemma_ == "y"
|
2020-10-03 18:16:10 +03:00
|
|
|
|
|
|
|
# Test lookups config format
|
|
|
|
for mode in ("rule", "lookup", "pos_lookup"):
|
|
|
|
required, optional = lemmatizer.get_lookups_config(mode)
|
|
|
|
assert isinstance(required, list)
|
|
|
|
assert isinstance(optional, list)
|