Fix PhraseMatcher.__contains__ and add more tests

This commit is contained in:
ines 2017-10-25 16:31:11 +02:00
parent 91beacf5e3
commit c0b55ebdac
2 changed files with 27 additions and 3 deletions

View File

@ -490,7 +490,7 @@ cdef class PhraseMatcher:
RETURNS (bool): Whether the matcher contains rules for this match ID. RETURNS (bool): Whether the matcher contains rules for this match ID.
""" """
cdef hash_t ent_id = self.matcher._normalize_key(key) cdef hash_t ent_id = self.matcher._normalize_key(key)
return ent_id in self.phrase_ids return ent_id in self._callbacks
def __reduce__(self): def __reduce__(self):
return (self.__class__, (self.vocab,), None, None) return (self.__class__, (self.vocab,), None, None)

View File

@ -64,6 +64,12 @@ def test_matcher_init(en_vocab, words):
assert matcher(doc) == [] assert matcher(doc) == []
def test_matcher_contains(matcher):
matcher.add('TEST', None, [{'ORTH': 'test'}])
assert 'TEST' in matcher
assert 'TEST2' not in matcher
def test_matcher_no_match(matcher): def test_matcher_no_match(matcher):
words = ["I", "like", "cheese", "."] words = ["I", "like", "cheese", "."]
doc = get_doc(matcher.vocab, words) doc = get_doc(matcher.vocab, words)
@ -112,7 +118,8 @@ def test_matcher_empty_dict(en_vocab):
matcher.add('A.', None, [{'ORTH': 'a'}, {}]) matcher.add('A.', None, [{'ORTH': 'a'}, {}])
matches = matcher(doc) matches = matcher(doc)
assert matches[0][1:] == (0, 2) assert matches[0][1:] == (0, 2)
def test_matcher_operator_shadow(en_vocab): def test_matcher_operator_shadow(en_vocab):
matcher = Matcher(en_vocab) matcher = Matcher(en_vocab)
abc = ["a", "b", "c"] abc = ["a", "b", "c"]
@ -123,7 +130,8 @@ def test_matcher_operator_shadow(en_vocab):
matches = matcher(doc) matches = matcher(doc)
assert len(matches) == 1 assert len(matches) == 1
assert matches[0][1:] == (0, 3) assert matches[0][1:] == (0, 3)
def test_matcher_phrase_matcher(en_vocab): def test_matcher_phrase_matcher(en_vocab):
words = ["Google", "Now"] words = ["Google", "Now"]
doc = get_doc(en_vocab, words) doc = get_doc(en_vocab, words)
@ -134,6 +142,22 @@ def test_matcher_phrase_matcher(en_vocab):
assert len(matcher(doc)) == 1 assert len(matcher(doc)) == 1
def test_phrase_matcher_length(en_vocab):
matcher = PhraseMatcher(en_vocab)
assert len(matcher) == 0
matcher.add('TEST', None, get_doc(en_vocab, ['test']))
assert len(matcher) == 1
matcher.add('TEST2', None, get_doc(en_vocab, ['test2']))
assert len(matcher) == 2
def test_phrase_matcher_contains(en_vocab):
matcher = PhraseMatcher(en_vocab)
matcher.add('TEST', None, get_doc(en_vocab, ['test']))
assert 'TEST' in matcher
assert 'TEST2' not in matcher
def test_matcher_match_zero(matcher): def test_matcher_match_zero(matcher):
words1 = 'He said , " some words " ...'.split() words1 = 'He said , " some words " ...'.split()
words2 = 'He said , " some three words " ...'.split() words2 = 'He said , " some three words " ...'.split()