mirror of
				https://github.com/explosion/spaCy.git
				synced 2025-11-04 01:48:04 +03:00 
			
		
		
		
	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:
		
							parent
							
								
									fa95c030a5
								
							
						
					
					
						commit
						5efae495f1
					
				| 
						 | 
					@ -498,6 +498,7 @@ class Errors(object):
 | 
				
			||||||
            "details: https://spacy.io/api/lemmatizer#init")
 | 
					            "details: https://spacy.io/api/lemmatizer#init")
 | 
				
			||||||
    E174 = ("Architecture '{name}' not found in registry. Available "
 | 
					    E174 = ("Architecture '{name}' not found in registry. Available "
 | 
				
			||||||
            "names: {names}")
 | 
					            "names: {names}")
 | 
				
			||||||
 | 
					    E175 = ("Can't remove rule for unknown match pattern ID: {key}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@add_codes
 | 
					@add_codes
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -133,9 +133,11 @@ cdef class Matcher:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        key (unicode): The ID of the match rule.
 | 
					        key (unicode): The ID of the match rule.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        key = self._normalize_key(key)
 | 
					        norm_key = self._normalize_key(key)
 | 
				
			||||||
        self._patterns.pop(key)
 | 
					        if not norm_key in self._patterns:
 | 
				
			||||||
        self._callbacks.pop(key)
 | 
					            raise ValueError(Errors.E175.format(key=key))
 | 
				
			||||||
 | 
					        self._patterns.pop(norm_key)
 | 
				
			||||||
 | 
					        self._callbacks.pop(norm_key)
 | 
				
			||||||
        cdef int i = 0
 | 
					        cdef int i = 0
 | 
				
			||||||
        while i < self.patterns.size():
 | 
					        while i < self.patterns.size():
 | 
				
			||||||
            pattern_key = get_ent_id(self.patterns.at(i))
 | 
					            pattern_key = get_ent_id(self.patterns.at(i))
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -143,3 +143,18 @@ def test_matcher_sets_return_correct_tokens(en_vocab):
 | 
				
			||||||
    matches = matcher(doc)
 | 
					    matches = matcher(doc)
 | 
				
			||||||
    texts = [Span(doc, s, e, label=L).text for L, s, e in matches]
 | 
					    texts = [Span(doc, s, e, label=L).text for L, s, e in matches]
 | 
				
			||||||
    assert texts == ["zero", "one", "two"]
 | 
					    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")
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user