Error when removing a matcher rule that doesn't exist (#4420)

* raise specific error when removing a matcher rule that doesn't exist

* rephrasing
This commit is contained in:
Sofie Van Landeghem 2019-10-10 14:01:53 +02:00 committed by Ines Montani
parent fa95c030a5
commit 5efae495f1
3 changed files with 21 additions and 3 deletions

View File

@ -498,6 +498,7 @@ class Errors(object):
"details: https://spacy.io/api/lemmatizer#init")
E174 = ("Architecture '{name}' not found in registry. Available "
"names: {names}")
E175 = ("Can't remove rule for unknown match pattern ID: {key}")
@add_codes

View File

@ -133,9 +133,11 @@ cdef class Matcher:
key (unicode): The ID of the match rule.
"""
key = self._normalize_key(key)
self._patterns.pop(key)
self._callbacks.pop(key)
norm_key = self._normalize_key(key)
if not norm_key in self._patterns:
raise ValueError(Errors.E175.format(key=key))
self._patterns.pop(norm_key)
self._callbacks.pop(norm_key)
cdef int i = 0
while i < self.patterns.size():
pattern_key = get_ent_id(self.patterns.at(i))

View File

@ -143,3 +143,18 @@ def test_matcher_sets_return_correct_tokens(en_vocab):
matches = matcher(doc)
texts = [Span(doc, s, e, label=L).text for L, s, e in matches]
assert texts == ["zero", "one", "two"]
def test_matcher_remove(en_vocab):
matcher = Matcher(en_vocab)
pattern = [{"ORTH": "test"}, {"OP": "?"}]
assert len(matcher) == 0
matcher.add("Rule", None, pattern)
assert "Rule" in matcher
# removing once should work
matcher.remove("Rule")
# removing again should throw an error
with pytest.raises(ValueError):
matcher.remove("Rule")