Skip to content

Commit

Permalink
Fix PhraseMatcher.__contains__ and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ines committed Oct 25, 2017
1 parent 91beacf commit c0b55eb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion spacy/matcher.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ cdef class PhraseMatcher:
RETURNS (bool): Whether the matcher contains rules for this match ID.
"""
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):
return (self.__class__, (self.vocab,), None, None)
Expand Down
28 changes: 26 additions & 2 deletions spacy/tests/test_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ def test_matcher_init(en_vocab, words):
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):
words = ["I", "like", "cheese", "."]
doc = get_doc(matcher.vocab, words)
Expand Down Expand Up @@ -112,7 +118,8 @@ def test_matcher_empty_dict(en_vocab):
matcher.add('A.', None, [{'ORTH': 'a'}, {}])
matches = matcher(doc)
assert matches[0][1:] == (0, 2)



def test_matcher_operator_shadow(en_vocab):
matcher = Matcher(en_vocab)
abc = ["a", "b", "c"]
Expand All @@ -123,7 +130,8 @@ def test_matcher_operator_shadow(en_vocab):
matches = matcher(doc)
assert len(matches) == 1
assert matches[0][1:] == (0, 3)



def test_matcher_phrase_matcher(en_vocab):
words = ["Google", "Now"]
doc = get_doc(en_vocab, words)
Expand All @@ -134,6 +142,22 @@ def test_matcher_phrase_matcher(en_vocab):
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):
words1 = 'He said , " some words " ...'.split()
words2 = 'He said , " some three words " ...'.split()
Expand Down

0 comments on commit c0b55eb

Please sign in to comment.