2017-10-18 22:53:08 +00:00
|
|
|
# coding: utf-8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import pytest
|
2018-07-24 21:38:44 +00:00
|
|
|
from spacy.tokens import Doc
|
|
|
|
from spacy.vocab import Vocab
|
2017-10-18 22:53:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def doc_w_attrs(en_tokenizer):
|
2018-11-27 00:09:36 +00:00
|
|
|
Doc.set_extension("_test_attr", default=False)
|
|
|
|
Doc.set_extension("_test_prop", getter=lambda doc: len(doc.text))
|
|
|
|
Doc.set_extension(
|
|
|
|
"_test_method", method=lambda doc, arg: "{}{}".format(len(doc.text), arg)
|
|
|
|
)
|
2017-10-18 22:53:08 +00:00
|
|
|
doc = en_tokenizer("This is a test.")
|
2018-11-27 00:09:36 +00:00
|
|
|
doc._._test_attr = "test"
|
2017-10-18 22:53:08 +00:00
|
|
|
return doc
|
|
|
|
|
|
|
|
|
|
|
|
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 00:09:36 +00:00
|
|
|
assert doc._.has("_test_attr")
|
|
|
|
assert doc._._test_attr == "test"
|
2017-10-18 22:53:08 +00:00
|
|
|
assert doc._._test_prop == len(doc.text)
|
2018-11-27 00:09:36 +00:00
|
|
|
assert doc._._test_method("test") == "{}{}".format(len(doc.text), "test")
|