2017-10-19 01:53:08 +03:00
|
|
|
import pytest
|
2023-06-14 18:48:41 +03:00
|
|
|
|
2020-01-06 16:57:34 +03:00
|
|
|
from spacy.tokens import Doc, Token
|
2018-07-25 00:38:44 +03:00
|
|
|
from spacy.vocab import Vocab
|
2017-10-19 01:53:08 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def doc_w_attrs(en_tokenizer):
|
2018-11-27 03:09:36 +03:00
|
|
|
Doc.set_extension("_test_attr", default=False)
|
|
|
|
Doc.set_extension("_test_prop", getter=lambda doc: len(doc.text))
|
2019-12-22 03:53:56 +03:00
|
|
|
Doc.set_extension("_test_method", method=lambda doc, arg: f"{len(doc.text)}{arg}")
|
2017-10-19 01:53:08 +03:00
|
|
|
doc = en_tokenizer("This is a test.")
|
2018-11-27 03:09:36 +03:00
|
|
|
doc._._test_attr = "test"
|
2020-01-06 16:57:34 +03:00
|
|
|
|
|
|
|
Token.set_extension("_test_token", default="t0")
|
|
|
|
doc[1]._._test_token = "t1"
|
|
|
|
|
2024-01-29 15:51:56 +03:00
|
|
|
yield doc
|
|
|
|
|
|
|
|
Doc.remove_extension("_test_attr")
|
|
|
|
Doc.remove_extension("_test_prop")
|
|
|
|
Doc.remove_extension("_test_method")
|
|
|
|
Token.remove_extension("_test_token")
|
2017-10-19 01:53:08 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_serialize_ext_attrs_from_bytes(doc_w_attrs):
|
|
|
|
doc_b = doc_w_attrs.to_bytes()
|
|
|
|
doc = Doc(Vocab()).from_bytes(doc_b)
|
2018-11-27 03:09:36 +03:00
|
|
|
assert doc._.has("_test_attr")
|
|
|
|
assert doc._._test_attr == "test"
|
2017-10-19 01:53:08 +03:00
|
|
|
assert doc._._test_prop == len(doc.text)
|
2019-12-22 03:53:56 +03:00
|
|
|
assert doc._._test_method("test") == f"{len(doc.text)}test"
|
2020-01-06 16:57:34 +03:00
|
|
|
assert doc[0]._._test_token == "t0"
|
|
|
|
assert doc[1]._._test_token == "t1"
|
|
|
|
assert doc[2]._._test_token == "t0"
|