2018-07-25 00:38:44 +03:00
|
|
|
import pytest
|
2018-11-27 03:09:36 +03:00
|
|
|
from spacy.pipeline import Tagger, DependencyParser, EntityRecognizer
|
2019-11-28 13:10:07 +03:00
|
|
|
from spacy.pipeline import Tensorizer, TextCategorizer, SentenceRecognizer
|
2020-02-28 13:57:41 +03:00
|
|
|
from spacy.ml.models.defaults import default_parser, default_tensorizer, default_tagger
|
2020-03-06 16:45:02 +03:00
|
|
|
from spacy.ml.models.defaults import default_textcat, default_senter
|
2018-07-25 00:38:44 +03:00
|
|
|
|
|
|
|
from ..util import make_tempdir
|
|
|
|
|
|
|
|
|
|
|
|
test_parsers = [DependencyParser, EntityRecognizer]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def parser(en_vocab):
|
2020-02-27 20:42:27 +03:00
|
|
|
parser = DependencyParser(en_vocab, default_parser())
|
2018-11-27 03:09:36 +03:00
|
|
|
parser.add_label("nsubj")
|
2018-07-25 00:38:44 +03:00
|
|
|
return parser
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def blank_parser(en_vocab):
|
2020-02-27 20:42:27 +03:00
|
|
|
parser = DependencyParser(en_vocab, default_parser())
|
2018-07-25 00:38:44 +03:00
|
|
|
return parser
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def taggers(en_vocab):
|
2020-02-27 20:42:27 +03:00
|
|
|
model = default_tagger()
|
|
|
|
tagger1 = Tagger(en_vocab, model)
|
|
|
|
tagger2 = Tagger(en_vocab, model)
|
|
|
|
return tagger1, tagger2
|
2018-07-25 00:38:44 +03:00
|
|
|
|
|
|
|
|
2018-11-27 03:09:36 +03:00
|
|
|
@pytest.mark.parametrize("Parser", test_parsers)
|
2018-07-25 00:38:44 +03:00
|
|
|
def test_serialize_parser_roundtrip_bytes(en_vocab, Parser):
|
2020-02-27 20:42:27 +03:00
|
|
|
parser = Parser(en_vocab, default_parser())
|
|
|
|
new_parser = Parser(en_vocab, default_parser())
|
2019-09-10 20:45:16 +03:00
|
|
|
new_parser = new_parser.from_bytes(parser.to_bytes(exclude=["vocab"]))
|
2020-02-27 20:42:27 +03:00
|
|
|
bytes_2 = new_parser.to_bytes(exclude=["vocab"])
|
|
|
|
bytes_3 = parser.to_bytes(exclude=["vocab"])
|
|
|
|
assert len(bytes_2) == len(bytes_3)
|
|
|
|
assert bytes_2 == bytes_3
|
2018-07-25 00:38:44 +03:00
|
|
|
|
|
|
|
|
2018-11-27 03:09:36 +03:00
|
|
|
@pytest.mark.parametrize("Parser", test_parsers)
|
2018-07-25 00:38:44 +03:00
|
|
|
def test_serialize_parser_roundtrip_disk(en_vocab, Parser):
|
2020-02-27 20:42:27 +03:00
|
|
|
parser = Parser(en_vocab, default_parser())
|
2018-07-25 00:38:44 +03:00
|
|
|
with make_tempdir() as d:
|
2018-11-27 03:09:36 +03:00
|
|
|
file_path = d / "parser"
|
2018-07-25 00:38:44 +03:00
|
|
|
parser.to_disk(file_path)
|
2020-02-27 20:42:27 +03:00
|
|
|
parser_d = Parser(en_vocab, default_parser())
|
2018-07-25 00:38:44 +03:00
|
|
|
parser_d = parser_d.from_disk(file_path)
|
2019-09-10 20:45:16 +03:00
|
|
|
parser_bytes = parser.to_bytes(exclude=["model", "vocab"])
|
|
|
|
parser_d_bytes = parser_d.to_bytes(exclude=["model", "vocab"])
|
2020-02-27 20:42:27 +03:00
|
|
|
assert len(parser_bytes) == len(parser_d_bytes)
|
2019-03-10 21:16:45 +03:00
|
|
|
assert parser_bytes == parser_d_bytes
|
2018-07-25 00:38:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_to_from_bytes(parser, blank_parser):
|
|
|
|
assert parser.model is not True
|
2020-02-27 20:42:27 +03:00
|
|
|
assert blank_parser.model is not True
|
2018-07-25 00:38:44 +03:00
|
|
|
assert blank_parser.moves.n_moves != parser.moves.n_moves
|
2019-09-10 20:45:16 +03:00
|
|
|
bytes_data = parser.to_bytes(exclude=["vocab"])
|
2020-02-27 20:42:27 +03:00
|
|
|
|
|
|
|
# the blank parser needs to be resized before we can call from_bytes
|
2020-05-18 23:23:33 +03:00
|
|
|
blank_parser.model.attrs["resize_output"](blank_parser.model, parser.moves.n_moves)
|
2018-07-25 00:38:44 +03:00
|
|
|
blank_parser.from_bytes(bytes_data)
|
|
|
|
assert blank_parser.model is not True
|
|
|
|
assert blank_parser.moves.n_moves == parser.moves.n_moves
|
|
|
|
|
|
|
|
|
2018-11-27 03:09:36 +03:00
|
|
|
@pytest.mark.skip(
|
|
|
|
reason="This seems to be a dict ordering bug somewhere. Only failing on some platforms."
|
|
|
|
)
|
2018-07-25 00:38:44 +03:00
|
|
|
def test_serialize_tagger_roundtrip_bytes(en_vocab, taggers):
|
2018-11-30 19:43:08 +03:00
|
|
|
tagger1 = taggers[0]
|
2018-07-25 00:38:44 +03:00
|
|
|
tagger1_b = tagger1.to_bytes()
|
|
|
|
tagger1 = tagger1.from_bytes(tagger1_b)
|
|
|
|
assert tagger1.to_bytes() == tagger1_b
|
2020-02-27 20:42:27 +03:00
|
|
|
new_tagger1 = Tagger(en_vocab, default_tagger()).from_bytes(tagger1_b)
|
|
|
|
new_tagger1_b = new_tagger1.to_bytes()
|
|
|
|
assert len(new_tagger1_b) == len(tagger1_b)
|
|
|
|
assert new_tagger1_b == tagger1_b
|
2018-07-25 00:38:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_serialize_tagger_roundtrip_disk(en_vocab, taggers):
|
|
|
|
tagger1, tagger2 = taggers
|
|
|
|
with make_tempdir() as d:
|
2018-11-27 03:09:36 +03:00
|
|
|
file_path1 = d / "tagger1"
|
|
|
|
file_path2 = d / "tagger2"
|
2018-07-25 00:38:44 +03:00
|
|
|
tagger1.to_disk(file_path1)
|
|
|
|
tagger2.to_disk(file_path2)
|
2020-02-27 20:42:27 +03:00
|
|
|
tagger1_d = Tagger(en_vocab, default_tagger()).from_disk(file_path1)
|
|
|
|
tagger2_d = Tagger(en_vocab, default_tagger()).from_disk(file_path2)
|
2018-07-25 00:38:44 +03:00
|
|
|
assert tagger1_d.to_bytes() == tagger2_d.to_bytes()
|
|
|
|
|
|
|
|
|
|
|
|
def test_serialize_tensorizer_roundtrip_bytes(en_vocab):
|
2020-02-27 20:42:27 +03:00
|
|
|
tensorizer = Tensorizer(en_vocab, default_tensorizer())
|
2019-09-10 20:45:16 +03:00
|
|
|
tensorizer_b = tensorizer.to_bytes(exclude=["vocab"])
|
2020-02-27 20:42:27 +03:00
|
|
|
new_tensorizer = Tensorizer(en_vocab, default_tensorizer()).from_bytes(tensorizer_b)
|
2019-09-10 20:45:16 +03:00
|
|
|
assert new_tensorizer.to_bytes(exclude=["vocab"]) == tensorizer_b
|
2018-07-25 00:38:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_serialize_tensorizer_roundtrip_disk(en_vocab):
|
2020-02-27 20:42:27 +03:00
|
|
|
tensorizer = Tensorizer(en_vocab, default_tensorizer())
|
2018-07-25 00:38:44 +03:00
|
|
|
with make_tempdir() as d:
|
2018-11-27 03:09:36 +03:00
|
|
|
file_path = d / "tensorizer"
|
2018-07-25 00:38:44 +03:00
|
|
|
tensorizer.to_disk(file_path)
|
2020-02-27 20:42:27 +03:00
|
|
|
tensorizer_d = Tensorizer(en_vocab, default_tensorizer()).from_disk(file_path)
|
2019-09-10 20:45:16 +03:00
|
|
|
assert tensorizer.to_bytes(exclude=["vocab"]) == tensorizer_d.to_bytes(
|
|
|
|
exclude=["vocab"]
|
|
|
|
)
|
2018-07-25 00:38:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_serialize_textcat_empty(en_vocab):
|
|
|
|
# See issue #1105
|
2020-02-28 13:57:41 +03:00
|
|
|
textcat = TextCategorizer(
|
|
|
|
en_vocab, default_textcat(), labels=["ENTITY", "ACTION", "MODIFIER"]
|
|
|
|
)
|
2019-09-10 20:45:16 +03:00
|
|
|
textcat.to_bytes(exclude=["vocab"])
|
2019-03-10 21:16:45 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("Parser", test_parsers)
|
|
|
|
def test_serialize_pipe_exclude(en_vocab, Parser):
|
|
|
|
def get_new_parser():
|
2020-02-27 20:42:27 +03:00
|
|
|
new_parser = Parser(en_vocab, default_parser())
|
2019-03-10 21:16:45 +03:00
|
|
|
return new_parser
|
|
|
|
|
2020-02-27 20:42:27 +03:00
|
|
|
parser = Parser(en_vocab, default_parser())
|
2019-03-10 21:16:45 +03:00
|
|
|
parser.cfg["foo"] = "bar"
|
2019-09-10 20:45:16 +03:00
|
|
|
new_parser = get_new_parser().from_bytes(parser.to_bytes(exclude=["vocab"]))
|
2019-03-10 21:16:45 +03:00
|
|
|
assert "foo" in new_parser.cfg
|
2019-09-10 20:45:16 +03:00
|
|
|
new_parser = get_new_parser().from_bytes(
|
|
|
|
parser.to_bytes(exclude=["vocab"]), exclude=["cfg"]
|
|
|
|
)
|
2019-03-10 21:16:45 +03:00
|
|
|
assert "foo" not in new_parser.cfg
|
2019-09-10 20:45:16 +03:00
|
|
|
new_parser = get_new_parser().from_bytes(
|
|
|
|
parser.to_bytes(exclude=["cfg"]), exclude=["vocab"]
|
|
|
|
)
|
2019-03-10 21:16:45 +03:00
|
|
|
assert "foo" not in new_parser.cfg
|
|
|
|
with pytest.raises(ValueError):
|
2019-09-10 20:45:16 +03:00
|
|
|
parser.to_bytes(cfg=False, exclude=["vocab"])
|
2019-03-10 21:16:45 +03:00
|
|
|
with pytest.raises(ValueError):
|
2019-09-10 20:45:16 +03:00
|
|
|
get_new_parser().from_bytes(parser.to_bytes(exclude=["vocab"]), cfg=False)
|
2019-11-28 13:10:07 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_serialize_sentencerecognizer(en_vocab):
|
2020-03-06 16:45:02 +03:00
|
|
|
sr = SentenceRecognizer(en_vocab, default_senter())
|
2019-11-28 13:10:07 +03:00
|
|
|
sr_b = sr.to_bytes()
|
2020-03-06 16:45:02 +03:00
|
|
|
sr_d = SentenceRecognizer(en_vocab, default_senter()).from_bytes(sr_b)
|
2019-11-28 13:10:07 +03:00
|
|
|
assert sr.to_bytes() == sr_d.to_bytes()
|