2020-08-07 13:27:13 +00:00
|
|
|
import pytest
|
|
|
|
from spacy import util, registry
|
|
|
|
from spacy.lang.en import English
|
2020-08-09 20:36:23 +00:00
|
|
|
from spacy.lookups import Lookups
|
2020-08-07 13:27:13 +00:00
|
|
|
|
|
|
|
from ..util import make_tempdir
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def nlp():
|
|
|
|
return English()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def lemmatizer(nlp):
|
2020-09-03 15:31:14 +00:00
|
|
|
@registry.misc("cope_lookups")
|
2020-08-07 13:27:13 +00:00
|
|
|
def cope_lookups():
|
|
|
|
lookups = Lookups()
|
|
|
|
lookups.add_table("lemma_lookup", {"cope": "cope"})
|
|
|
|
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
|
|
|
|
|
|
|
|
lemmatizer = nlp.add_pipe(
|
2020-09-03 15:31:14 +00:00
|
|
|
"lemmatizer", config={"mode": "rule", "lookups": {"@misc": "cope_lookups"}}
|
2020-08-07 13:27:13 +00:00
|
|
|
)
|
|
|
|
return lemmatizer
|
|
|
|
|
|
|
|
|
|
|
|
def test_lemmatizer_init(nlp):
|
2020-09-03 15:31:14 +00:00
|
|
|
@registry.misc("cope_lookups")
|
2020-08-07 13:27:13 +00:00
|
|
|
def cope_lookups():
|
|
|
|
lookups = Lookups()
|
|
|
|
lookups.add_table("lemma_lookup", {"cope": "cope"})
|
|
|
|
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
|
|
|
|
|
|
|
|
lemmatizer = nlp.add_pipe(
|
2020-09-03 15:31:14 +00:00
|
|
|
"lemmatizer", config={"mode": "lookup", "lookups": {"@misc": "cope_lookups"}}
|
2020-08-07 13:27:13 +00:00
|
|
|
)
|
|
|
|
assert isinstance(lemmatizer.lookups, Lookups)
|
|
|
|
assert lemmatizer.mode == "lookup"
|
|
|
|
# replace any tables from spacy-lookups-data
|
|
|
|
lemmatizer.lookups = Lookups()
|
|
|
|
doc = nlp("coping")
|
|
|
|
# lookup with no tables sets text as lemma
|
|
|
|
assert doc[0].lemma_ == "coping"
|
|
|
|
|
|
|
|
nlp.remove_pipe("lemmatizer")
|
|
|
|
|
2020-09-03 15:31:14 +00:00
|
|
|
@registry.misc("empty_lookups")
|
2020-08-07 13:27:13 +00:00
|
|
|
def empty_lookups():
|
|
|
|
return Lookups()
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
nlp.add_pipe(
|
|
|
|
"lemmatizer",
|
2020-09-03 15:31:14 +00:00
|
|
|
config={"mode": "lookup", "lookups": {"@misc": "empty_lookups"}},
|
2020-08-07 13:27:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_lemmatizer_config(nlp, lemmatizer):
|
|
|
|
doc = nlp.make_doc("coping")
|
|
|
|
doc[0].pos_ = "VERB"
|
|
|
|
assert doc[0].lemma_ == ""
|
|
|
|
doc = lemmatizer(doc)
|
|
|
|
assert doc[0].text == "coping"
|
|
|
|
assert doc[0].lemma_ == "cope"
|
|
|
|
|
|
|
|
doc = nlp.make_doc("coping")
|
|
|
|
doc[0].pos_ = "VERB"
|
|
|
|
assert doc[0].lemma_ == ""
|
|
|
|
doc = lemmatizer(doc)
|
|
|
|
assert doc[0].text == "coping"
|
|
|
|
assert doc[0].lemma_ == "cope"
|
|
|
|
|
|
|
|
|
|
|
|
def test_lemmatizer_serialize(nlp, lemmatizer):
|
2020-09-03 15:31:14 +00:00
|
|
|
@registry.misc("cope_lookups")
|
2020-08-07 13:27:13 +00:00
|
|
|
def cope_lookups():
|
|
|
|
lookups = Lookups()
|
|
|
|
lookups.add_table("lemma_lookup", {"cope": "cope"})
|
|
|
|
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
|
|
|
|
|
|
|
|
nlp2 = English()
|
|
|
|
lemmatizer2 = nlp2.add_pipe(
|
2020-09-03 15:31:14 +00:00
|
|
|
"lemmatizer", config={"mode": "rule", "lookups": {"@misc": "cope_lookups"}}
|
2020-08-07 13:27:13 +00:00
|
|
|
)
|
|
|
|
lemmatizer2.from_bytes(lemmatizer.to_bytes())
|
|
|
|
assert lemmatizer.to_bytes() == lemmatizer2.to_bytes()
|
|
|
|
assert lemmatizer.lookups.tables == lemmatizer2.lookups.tables
|
|
|
|
|
|
|
|
# Also test the results are still the same after IO
|
|
|
|
with make_tempdir() as tmp_dir:
|
|
|
|
nlp.to_disk(tmp_dir)
|
|
|
|
nlp2 = util.load_model_from_path(tmp_dir)
|
|
|
|
doc2 = nlp2.make_doc("coping")
|
|
|
|
doc2[0].pos_ = "VERB"
|
|
|
|
assert doc2[0].lemma_ == ""
|
|
|
|
doc2 = lemmatizer(doc2)
|
|
|
|
assert doc2[0].text == "coping"
|
|
|
|
assert doc2[0].lemma_ == "cope"
|