mirror of
https://github.com/explosion/spaCy.git
synced 2025-02-04 21:50:35 +03:00
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:
parent
595ef03e23
commit
82fa81d095
|
@ -375,21 +375,10 @@ class Errors:
|
||||||
E125 = ("Unexpected value: {value}")
|
E125 = ("Unexpected value: {value}")
|
||||||
E126 = ("Unexpected matcher predicate: '{bad}'. Expected one of: {good}. "
|
E126 = ("Unexpected matcher predicate: '{bad}'. Expected one of: {good}. "
|
||||||
"This is likely a bug in spaCy, so feel free to open an issue.")
|
"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 "
|
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 "
|
"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 "
|
"unicode build instead. You can also rebuild Python and set the "
|
||||||
"`--enable-unicode=ucs4 flag`.")
|
"`--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}' "
|
E132 = ("The vectors for entities and probabilities for alias '{alias}' "
|
||||||
"should have equal length, but found {entities_length} and "
|
"should have equal length, but found {entities_length} and "
|
||||||
"{probabilities_length} respectively.")
|
"{probabilities_length} respectively.")
|
||||||
|
|
|
@ -266,16 +266,10 @@ def test_span_string_label_kb_id(doc):
|
||||||
assert span.kb_id == doc.vocab.strings["Q342"]
|
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)
|
span = Span(doc, 0, 1)
|
||||||
with pytest.raises(NotImplementedError):
|
span.label_ = "label"
|
||||||
span.label_ = "hello"
|
span.kb_id_ = "kb_id"
|
||||||
|
|
||||||
|
|
||||||
def test_span_kb_id_readonly(doc):
|
|
||||||
span = Span(doc, 0, 1)
|
|
||||||
with pytest.raises(NotImplementedError):
|
|
||||||
span.kb_id_ = "Q342"
|
|
||||||
|
|
||||||
|
|
||||||
def test_span_ents_property(doc):
|
def test_span_ents_property(doc):
|
||||||
|
|
|
@ -740,7 +740,7 @@ cdef class Span:
|
||||||
def __get__(self):
|
def __get__(self):
|
||||||
return self.root.ent_id_
|
return self.root.ent_id_
|
||||||
|
|
||||||
def __set__(self, hash_t key):
|
def __set__(self, unicode key):
|
||||||
raise NotImplementedError(Errors.E200.format(attr="ent_id_"))
|
raise NotImplementedError(Errors.E200.format(attr="ent_id_"))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -762,9 +762,7 @@ cdef class Span:
|
||||||
return self.doc.vocab.strings[self.label]
|
return self.doc.vocab.strings[self.label]
|
||||||
|
|
||||||
def __set__(self, unicode label_):
|
def __set__(self, unicode label_):
|
||||||
if not label_:
|
self.label = self.doc.vocab.strings.add(label_)
|
||||||
label_ = ''
|
|
||||||
raise NotImplementedError(Errors.E129.format(start=self.start, end=self.end, label=label_))
|
|
||||||
|
|
||||||
property kb_id_:
|
property kb_id_:
|
||||||
"""RETURNS (str): The named entity's 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]
|
return self.doc.vocab.strings[self.kb_id]
|
||||||
|
|
||||||
def __set__(self, unicode kb_id_):
|
def __set__(self, unicode kb_id_):
|
||||||
if not kb_id_:
|
self.kb_id = self.doc.vocab.strings.add(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_))
|
|
||||||
|
|
||||||
|
|
||||||
cdef int _count_words_to_root(const TokenC* token, int sent_length) except -1:
|
cdef int _count_words_to_root(const TokenC* token, int sent_length) except -1:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user