2020-04-06 19:46:51 +03:00
|
|
|
import warnings
|
2020-04-06 22:36:02 +03:00
|
|
|
from unittest import TestCase
|
2020-04-06 19:46:51 +03:00
|
|
|
import pytest
|
|
|
|
import srsly
|
2020-04-06 21:51:12 +03:00
|
|
|
from numpy import zeros
|
2020-04-10 21:35:52 +03:00
|
|
|
from spacy.kb import KnowledgeBase, Writer
|
2020-04-06 19:46:51 +03:00
|
|
|
from spacy.vectors import Vectors
|
|
|
|
from spacy.language import Language
|
2020-10-08 22:33:49 +03:00
|
|
|
from spacy.pipeline import TrainablePipe
|
2020-10-10 19:55:07 +03:00
|
|
|
from spacy.vocab import Vocab
|
2020-05-22 15:22:36 +03:00
|
|
|
|
|
|
|
from ..util import make_tempdir
|
2020-04-06 19:46:51 +03:00
|
|
|
|
|
|
|
|
2020-04-06 21:51:12 +03:00
|
|
|
def nlp():
|
|
|
|
return Language()
|
2020-04-06 19:46:51 +03:00
|
|
|
|
|
|
|
|
2020-04-06 21:51:12 +03:00
|
|
|
def vectors():
|
|
|
|
data = zeros((3, 1), dtype="f")
|
2020-04-06 19:46:51 +03:00
|
|
|
keys = ["cat", "dog", "rat"]
|
2020-04-06 21:51:12 +03:00
|
|
|
return Vectors(data=data, keys=keys)
|
2020-04-06 19:46:51 +03:00
|
|
|
|
|
|
|
|
2020-04-06 21:51:12 +03:00
|
|
|
def custom_pipe():
|
2020-04-06 19:46:51 +03:00
|
|
|
# create dummy pipe partially implementing interface -- only want to test to_disk
|
2020-07-12 15:03:23 +03:00
|
|
|
class SerializableDummy:
|
2020-04-06 19:46:51 +03:00
|
|
|
def __init__(self, **cfg):
|
|
|
|
if cfg:
|
|
|
|
self.cfg = cfg
|
|
|
|
else:
|
|
|
|
self.cfg = None
|
|
|
|
super(SerializableDummy, self).__init__()
|
|
|
|
|
|
|
|
def to_bytes(self, exclude=tuple(), disable=None, **kwargs):
|
|
|
|
return srsly.msgpack_dumps({"dummy": srsly.json_dumps(None)})
|
|
|
|
|
|
|
|
def from_bytes(self, bytes_data, exclude):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def to_disk(self, path, exclude=tuple(), **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def from_disk(self, path, exclude=tuple(), **kwargs):
|
|
|
|
return self
|
|
|
|
|
2020-10-08 22:33:49 +03:00
|
|
|
class MyPipe(TrainablePipe):
|
2020-04-06 19:46:51 +03:00
|
|
|
def __init__(self, vocab, model=True, **cfg):
|
|
|
|
if cfg:
|
|
|
|
self.cfg = cfg
|
|
|
|
else:
|
|
|
|
self.cfg = None
|
|
|
|
self.model = SerializableDummy()
|
2020-10-10 19:55:07 +03:00
|
|
|
self.vocab = vocab
|
2020-04-06 19:46:51 +03:00
|
|
|
|
2020-10-10 19:55:07 +03:00
|
|
|
return MyPipe(Vocab())
|
2020-04-06 19:46:51 +03:00
|
|
|
|
|
|
|
|
2020-04-06 21:51:12 +03:00
|
|
|
def tagger():
|
2020-04-06 19:46:51 +03:00
|
|
|
nlp = Language()
|
2020-07-22 14:42:59 +03:00
|
|
|
tagger = nlp.add_pipe("tagger")
|
2020-04-06 19:46:51 +03:00
|
|
|
# need to add model for two reasons:
|
|
|
|
# 1. no model leads to error in serialization,
|
|
|
|
# 2. the affected line is the one for model serialization
|
2020-08-31 22:24:33 +03:00
|
|
|
tagger.add_label("A")
|
2020-09-28 22:35:09 +03:00
|
|
|
nlp.initialize()
|
2020-04-06 21:51:12 +03:00
|
|
|
return tagger
|
2020-04-06 19:46:51 +03:00
|
|
|
|
|
|
|
|
2020-04-06 21:51:12 +03:00
|
|
|
def entity_linker():
|
2020-04-06 19:46:51 +03:00
|
|
|
nlp = Language()
|
2020-07-22 14:42:59 +03:00
|
|
|
|
2020-10-08 11:34:01 +03:00
|
|
|
def create_kb(vocab):
|
|
|
|
kb = KnowledgeBase(vocab, entity_vector_length=1)
|
|
|
|
kb.add_entity("test", 0.0, zeros((1, 1), dtype="f"))
|
|
|
|
return kb
|
2020-08-23 22:15:12 +03:00
|
|
|
|
2020-10-08 11:34:01 +03:00
|
|
|
entity_linker = nlp.add_pipe("entity_linker")
|
|
|
|
entity_linker.set_kb(create_kb)
|
2020-04-06 19:46:51 +03:00
|
|
|
# need to add model for two reasons:
|
|
|
|
# 1. no model leads to error in serialization,
|
|
|
|
# 2. the affected line is the one for model serialization
|
2020-09-28 22:35:09 +03:00
|
|
|
nlp.initialize()
|
2020-04-06 21:51:12 +03:00
|
|
|
return entity_linker
|
|
|
|
|
2020-04-06 19:46:51 +03:00
|
|
|
|
2020-04-06 22:36:02 +03:00
|
|
|
objects_to_test = (
|
2020-04-06 21:51:12 +03:00
|
|
|
[nlp(), vectors(), custom_pipe(), tagger(), entity_linker()],
|
2020-04-06 22:36:02 +03:00
|
|
|
["nlp", "vectors", "custom_pipe", "tagger", "entity_linker"],
|
2020-04-06 21:51:12 +03:00
|
|
|
)
|
2020-04-06 22:36:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
def write_obj_and_catch_warnings(obj):
|
2020-04-06 19:46:51 +03:00
|
|
|
with make_tempdir() as d:
|
2020-04-06 21:51:12 +03:00
|
|
|
with warnings.catch_warnings(record=True) as warnings_list:
|
2020-04-10 23:26:55 +03:00
|
|
|
warnings.filterwarnings("always", category=ResourceWarning)
|
2020-04-06 21:51:12 +03:00
|
|
|
obj.to_disk(d)
|
2020-04-11 00:21:13 +03:00
|
|
|
# in python3.5 it seems that deprecation warnings are not filtered by filterwarnings
|
|
|
|
return list(filter(lambda x: isinstance(x, ResourceWarning), warnings_list))
|
2020-04-06 22:36:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("obj", objects_to_test[0], ids=objects_to_test[1])
|
|
|
|
def test_to_disk_resource_warning(obj):
|
|
|
|
warnings_list = write_obj_and_catch_warnings(obj)
|
|
|
|
assert len(warnings_list) == 0
|
|
|
|
|
|
|
|
|
2020-04-10 21:35:52 +03:00
|
|
|
def test_writer_with_path_py35():
|
|
|
|
writer = None
|
|
|
|
with make_tempdir() as d:
|
|
|
|
path = d / "test"
|
|
|
|
try:
|
|
|
|
writer = Writer(path)
|
|
|
|
except Exception as e:
|
|
|
|
pytest.fail(str(e))
|
|
|
|
finally:
|
|
|
|
if writer:
|
|
|
|
writer.close()
|
|
|
|
|
|
|
|
|
2020-04-12 10:08:41 +03:00
|
|
|
def test_save_and_load_knowledge_base():
|
|
|
|
nlp = Language()
|
2020-08-18 17:10:36 +03:00
|
|
|
kb = KnowledgeBase(nlp.vocab, entity_vector_length=1)
|
2020-04-12 10:08:41 +03:00
|
|
|
with make_tempdir() as d:
|
|
|
|
path = d / "kb"
|
|
|
|
try:
|
2020-08-18 17:10:36 +03:00
|
|
|
kb.to_disk(path)
|
2020-04-12 10:08:41 +03:00
|
|
|
except Exception as e:
|
|
|
|
pytest.fail(str(e))
|
|
|
|
|
|
|
|
try:
|
2020-08-18 17:10:36 +03:00
|
|
|
kb_loaded = KnowledgeBase(nlp.vocab, entity_vector_length=1)
|
|
|
|
kb_loaded.from_disk(path)
|
2020-04-12 10:08:41 +03:00
|
|
|
except Exception as e:
|
|
|
|
pytest.fail(str(e))
|
|
|
|
|
|
|
|
|
2020-06-20 17:11:13 +03:00
|
|
|
class TestToDiskResourceWarningUnittest(TestCase):
|
|
|
|
def test_resource_warning(self):
|
|
|
|
scenarios = zip(*objects_to_test)
|
2020-05-22 15:22:36 +03:00
|
|
|
|
2020-06-20 17:11:13 +03:00
|
|
|
for scenario in scenarios:
|
|
|
|
with self.subTest(msg=scenario[1]):
|
|
|
|
warnings_list = write_obj_and_catch_warnings(scenario[0])
|
|
|
|
self.assertEqual(len(warnings_list), 0)
|