Make all Span attrs writable (#8062)

Also allow `Span` string properties `label_` and `kb_id_` to be writable
following #6696.
This commit is contained in:
Adriane Boyd 2021-05-17 10:05:45 +02:00 committed by GitHub
parent 595ef03e23
commit 82fa81d095
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 31 deletions

View File

@ -375,21 +375,10 @@ class Errors:
E125 = ("Unexpected value: {value}")
E126 = ("Unexpected matcher predicate: '{bad}'. Expected one of: {good}. "
"This is likely a bug in spaCy, so feel free to open an issue.")
E129 = ("Cannot write the label of an existing Span object because a Span "
"is a read-only view of the underlying Token objects stored in the "
"Doc. Instead, create a new Span object and specify the `label` "
"keyword argument, for example:\nfrom spacy.tokens import Span\n"
"span = Span(doc, start={start}, end={end}, label='{label}')")
E130 = ("You are running a narrow unicode build, which is incompatible "
"with spacy >= 2.1.0. To fix this, reinstall Python and use a wide "
"unicode build instead. You can also rebuild Python and set the "
"`--enable-unicode=ucs4 flag`.")
E131 = ("Cannot write the kb_id of an existing Span object because a Span "
"is a read-only view of the underlying Token objects stored in "
"the Doc. Instead, create a new Span object and specify the "
"`kb_id` keyword argument, for example:\nfrom spacy.tokens "
"import Span\nspan = Span(doc, start={start}, end={end}, "
"label='{label}', kb_id='{kb_id}')")
E132 = ("The vectors for entities and probabilities for alias '{alias}' "
"should have equal length, but found {entities_length} and "
"{probabilities_length} respectively.")

View File

@ -266,16 +266,10 @@ def test_span_string_label_kb_id(doc):
assert span.kb_id == doc.vocab.strings["Q342"]
def test_span_label_readonly(doc):
def test_span_attrs_writable(doc):
span = Span(doc, 0, 1)
with pytest.raises(NotImplementedError):
span.label_ = "hello"
def test_span_kb_id_readonly(doc):
span = Span(doc, 0, 1)
with pytest.raises(NotImplementedError):
span.kb_id_ = "Q342"
span.label_ = "label"
span.kb_id_ = "kb_id"
def test_span_ents_property(doc):

View File

@ -740,7 +740,7 @@ cdef class Span:
def __get__(self):
return self.root.ent_id_
def __set__(self, hash_t key):
def __set__(self, unicode key):
raise NotImplementedError(Errors.E200.format(attr="ent_id_"))
@property
@ -762,9 +762,7 @@ cdef class Span:
return self.doc.vocab.strings[self.label]
def __set__(self, unicode label_):
if not label_:
label_ = ''
raise NotImplementedError(Errors.E129.format(start=self.start, end=self.end, label=label_))
self.label = self.doc.vocab.strings.add(label_)
property kb_id_:
"""RETURNS (str): The named entity's KB ID."""
@ -772,13 +770,7 @@ cdef class Span:
return self.doc.vocab.strings[self.kb_id]
def __set__(self, unicode kb_id_):
if not kb_id_:
kb_id_ = ''
current_label = self.label_
if not current_label:
current_label = ''
raise NotImplementedError(Errors.E131.format(start=self.start, end=self.end,
label=current_label, kb_id=kb_id_))
self.kb_id = self.doc.vocab.strings.add(kb_id_)
cdef int _count_words_to_root(const TokenC* token, int sent_length) except -1: