2017-01-12 19:51:46 +03:00
|
|
|
# coding: utf-8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-05-23 11:06:53 +03:00
|
|
|
from ..matcher import Matcher, PhraseMatcher
|
|
|
|
from .util import get_doc
|
2017-01-12 19:51:46 +03:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
2017-01-13 00:23:11 +03:00
|
|
|
@pytest.fixture
|
|
|
|
def matcher(en_vocab):
|
2017-05-22 13:59:50 +03:00
|
|
|
rules = {
|
|
|
|
'JS': [[{'ORTH': 'JavaScript'}]],
|
|
|
|
'GoogleNow': [[{'ORTH': 'Google'}, {'ORTH': 'Now'}]],
|
|
|
|
'Java': [[{'LOWER': 'java'}]]
|
2017-01-13 00:23:11 +03:00
|
|
|
}
|
2017-05-22 13:59:50 +03:00
|
|
|
matcher = Matcher(en_vocab)
|
|
|
|
for key, patterns in rules.items():
|
|
|
|
matcher.add(key, None, *patterns)
|
|
|
|
return matcher
|
2017-01-13 00:23:11 +03:00
|
|
|
|
|
|
|
|
2017-01-12 19:51:46 +03:00
|
|
|
@pytest.mark.parametrize('words', [["Some", "words"]])
|
|
|
|
def test_matcher_init(en_vocab, words):
|
|
|
|
matcher = Matcher(en_vocab)
|
|
|
|
doc = get_doc(en_vocab, words)
|
2017-05-22 13:59:50 +03:00
|
|
|
assert len(matcher) == 0
|
2017-01-12 19:51:46 +03:00
|
|
|
assert matcher(doc) == []
|
2017-01-13 00:23:11 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_no_match(matcher):
|
|
|
|
words = ["I", "like", "cheese", "."]
|
|
|
|
doc = get_doc(matcher.vocab, words)
|
|
|
|
assert matcher(doc) == []
|
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_compile(matcher):
|
2017-05-22 13:59:50 +03:00
|
|
|
assert len(matcher) == 3
|
2017-01-13 00:23:11 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_match_start(matcher):
|
|
|
|
words = ["JavaScript", "is", "good"]
|
|
|
|
doc = get_doc(matcher.vocab, words)
|
2017-05-22 13:59:50 +03:00
|
|
|
assert matcher(doc) == [(matcher.vocab.strings['JS'], 0, 1)]
|
2017-01-13 00:23:11 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_match_end(matcher):
|
|
|
|
words = ["I", "like", "java"]
|
|
|
|
doc = get_doc(matcher.vocab, words)
|
2017-05-22 13:59:50 +03:00
|
|
|
assert matcher(doc) == [(doc.vocab.strings['Java'], 2, 3)]
|
2017-01-13 00:23:11 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_match_middle(matcher):
|
|
|
|
words = ["I", "like", "Google", "Now", "best"]
|
|
|
|
doc = get_doc(matcher.vocab, words)
|
2017-05-22 13:59:50 +03:00
|
|
|
assert matcher(doc) == [(doc.vocab.strings['GoogleNow'], 2, 4)]
|
2017-01-13 00:23:11 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_match_multi(matcher):
|
|
|
|
words = ["I", "like", "Google", "Now", "and", "java", "best"]
|
|
|
|
doc = get_doc(matcher.vocab, words)
|
2017-05-22 13:59:50 +03:00
|
|
|
assert matcher(doc) == [(doc.vocab.strings['GoogleNow'], 2, 4),
|
|
|
|
(doc.vocab.strings['Java'], 5, 6)]
|
2017-01-13 00:23:11 +03:00
|
|
|
|
|
|
|
|
2017-05-22 13:59:50 +03:00
|
|
|
@pytest.mark.xfail
|
2017-01-13 00:23:11 +03:00
|
|
|
def test_matcher_phrase_matcher(en_vocab):
|
|
|
|
words = ["Google", "Now"]
|
|
|
|
doc = get_doc(en_vocab, words)
|
|
|
|
matcher = PhraseMatcher(en_vocab, [doc])
|
|
|
|
words = ["I", "like", "Google", "Now", "best"]
|
|
|
|
doc = get_doc(en_vocab, words)
|
|
|
|
assert len(matcher(doc)) == 1
|
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_match_zero(matcher):
|
|
|
|
words1 = 'He said , " some words " ...'.split()
|
|
|
|
words2 = 'He said , " some three words " ...'.split()
|
|
|
|
pattern1 = [{'ORTH': '"'},
|
|
|
|
{'OP': '!', 'IS_PUNCT': True},
|
|
|
|
{'OP': '!', 'IS_PUNCT': True},
|
|
|
|
{'ORTH': '"'}]
|
|
|
|
pattern2 = [{'ORTH': '"'},
|
|
|
|
{'IS_PUNCT': True},
|
|
|
|
{'IS_PUNCT': True},
|
|
|
|
{'IS_PUNCT': True},
|
|
|
|
{'ORTH': '"'}]
|
|
|
|
|
2017-05-22 14:54:20 +03:00
|
|
|
matcher.add('Quote', None, pattern1)
|
2017-01-13 00:23:11 +03:00
|
|
|
doc = get_doc(matcher.vocab, words1)
|
|
|
|
assert len(matcher(doc)) == 1
|
|
|
|
|
|
|
|
doc = get_doc(matcher.vocab, words2)
|
|
|
|
assert len(matcher(doc)) == 0
|
2017-05-22 14:54:20 +03:00
|
|
|
matcher.add('Quote', None, pattern2)
|
2017-01-13 00:23:11 +03:00
|
|
|
assert len(matcher(doc)) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_matcher_match_zero_plus(matcher):
|
|
|
|
words = 'He said , " some words " ...'.split()
|
|
|
|
pattern = [{'ORTH': '"'},
|
|
|
|
{'OP': '*', 'IS_PUNCT': False},
|
|
|
|
{'ORTH': '"'}]
|
2017-05-22 14:54:20 +03:00
|
|
|
matcher.add('Quote', None, pattern)
|
2017-01-13 00:23:11 +03:00
|
|
|
doc = get_doc(matcher.vocab, words)
|
|
|
|
assert len(matcher(doc)) == 1
|
2017-02-24 16:27:02 +03:00
|
|
|
|
2017-05-22 14:54:20 +03:00
|
|
|
|
2017-02-24 16:27:02 +03:00
|
|
|
def test_matcher_match_one_plus(matcher):
|
|
|
|
control = Matcher(matcher.vocab)
|
2017-05-22 13:59:50 +03:00
|
|
|
control.add('BasicPhilippe', None, [{'ORTH': 'Philippe'}])
|
2017-02-24 16:27:02 +03:00
|
|
|
doc = get_doc(control.vocab, ['Philippe', 'Philippe'])
|
|
|
|
m = control(doc)
|
|
|
|
assert len(m) == 2
|
2017-05-22 14:54:20 +03:00
|
|
|
matcher.add('KleenePhilippe', None, [{'ORTH': 'Philippe', 'OP': '1'},
|
|
|
|
{'ORTH': 'Philippe', 'OP': '+'}])
|
2017-02-24 16:27:02 +03:00
|
|
|
m = matcher(doc)
|
|
|
|
assert len(m) == 1
|