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():
|
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()
|
2020-10-02 13:42:36 +00:00
|
|
|
lookups.add_table("lemma_lookup", {"cope": "cope", "coped": "cope"})
|
2020-08-07 13:27:13 +00: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-02 13:42:36 +00:00
|
|
|
nlp = English()
|
|
|
|
nlp.config["initialize"]["components"]["lemmatizer"] = {
|
|
|
|
"lookups": {"@misc": "cope_lookups"}
|
|
|
|
}
|
|
|
|
return nlp
|
2020-08-07 13:27:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_lemmatizer_init(nlp):
|
2020-10-02 13:42:36 +00:00
|
|
|
lemmatizer = nlp.add_pipe("lemmatizer", config={"mode": "lookup"})
|
2020-08-07 13:27:13 +00:00
|
|
|
assert isinstance(lemmatizer.lookups, Lookups)
|
2020-10-02 13:42:36 +00:00
|
|
|
assert not lemmatizer.lookups.tables
|
2020-08-07 13:27:13 +00:00
|
|
|
assert lemmatizer.mode == "lookup"
|
2020-10-02 13:42:36 +00:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
nlp("test")
|
|
|
|
nlp.initialize()
|
|
|
|
assert lemmatizer.lookups.tables
|
|
|
|
assert nlp("cope")[0].lemma_ == "cope"
|
|
|
|
assert nlp("coped")[0].lemma_ == "cope"
|
2020-08-07 13:27:13 +00:00
|
|
|
# replace any tables from spacy-lookups-data
|
|
|
|
lemmatizer.lookups = Lookups()
|
|
|
|
# lookup with no tables sets text as lemma
|
2020-10-02 13:42:36 +00:00
|
|
|
assert nlp("cope")[0].lemma_ == "cope"
|
|
|
|
assert nlp("coped")[0].lemma_ == "coped"
|
2020-08-07 13:27:13 +00:00
|
|
|
nlp.remove_pipe("lemmatizer")
|
2020-10-02 13:42:36 +00:00
|
|
|
lemmatizer = nlp.add_pipe("lemmatizer", config={"mode": "lookup"})
|
2020-08-07 13:27:13 +00:00
|
|
|
with pytest.raises(ValueError):
|
2020-10-02 13:42:36 +00:00
|
|
|
# Can't initialize without required tables
|
|
|
|
lemmatizer.initialize(lookups=Lookups())
|
|
|
|
lookups = Lookups()
|
|
|
|
lookups.add_table("lemma_lookup", {})
|
|
|
|
lemmatizer.initialize(lookups=lookups)
|
|
|
|
|
2020-08-07 13:27:13 +00:00
|
|
|
|
2020-10-02 13:42:36 +00:00
|
|
|
def test_lemmatizer_config(nlp):
|
|
|
|
lemmatizer = nlp.add_pipe("lemmatizer", config={"mode": "rule"})
|
|
|
|
nlp.initialize()
|
2020-08-07 13:27:13 +00:00
|
|
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
|
2020-10-02 13:42:36 +00:00
|
|
|
def test_lemmatizer_serialize(nlp):
|
|
|
|
lemmatizer = nlp.add_pipe("lemmatizer", config={"mode": "rule"})
|
|
|
|
nlp.initialize()
|
|
|
|
|
2020-08-07 13:27:13 +00:00
|
|
|
def cope_lookups():
|
|
|
|
lookups = Lookups()
|
2020-10-02 13:42:36 +00:00
|
|
|
lookups.add_table("lemma_lookup", {"cope": "cope", "coped": "cope"})
|
2020-08-07 13:27:13 +00: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
|
|
|
|
|
|
|
|
nlp2 = English()
|
2020-10-02 13:42:36 +00:00
|
|
|
lemmatizer2 = nlp2.add_pipe("lemmatizer", config={"mode": "rule"})
|
|
|
|
lemmatizer2.initialize(lookups=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)
|
2020-10-02 13:42:36 +00:00
|
|
|
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"
|