2015-08-04 13:55:28 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
import pytest
|
|
|
|
|
2015-08-04 23:05:54 +00:00
|
|
|
from spacy.strings import StringStore
|
2015-08-04 13:55:28 +00:00
|
|
|
from spacy.matcher import *
|
2015-09-06 03:40:10 +00:00
|
|
|
from spacy.attrs import LOWER
|
2015-08-04 23:05:54 +00:00
|
|
|
from spacy.tokens.doc import Doc
|
|
|
|
from spacy.vocab import Vocab
|
2015-08-04 13:55:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2015-08-04 23:05:54 +00:00
|
|
|
def matcher(EN):
|
2015-08-06 12:33:35 +00:00
|
|
|
patterns = {
|
2015-08-06 14:09:28 +00:00
|
|
|
'Javascript': ['PRODUCT', {}, [[{'ORTH': 'JavaScript'}]]],
|
|
|
|
'GoogleNow': ['PRODUCT', {}, [[{'ORTH': 'Google'}, {'ORTH': 'Now'}]]],
|
2015-09-06 03:40:10 +00:00
|
|
|
'Java': ['PRODUCT', {}, [[{'LOWER': 'java'}]]],
|
2015-08-06 12:33:35 +00:00
|
|
|
}
|
|
|
|
return Matcher(EN.vocab, patterns)
|
2015-08-04 13:55:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_compile(matcher):
|
2015-08-04 23:05:54 +00:00
|
|
|
assert matcher.n_patterns == 3
|
2015-08-04 13:55:28 +00:00
|
|
|
|
2015-08-06 12:33:35 +00:00
|
|
|
|
2015-08-04 23:05:54 +00:00
|
|
|
def test_no_match(matcher, EN):
|
|
|
|
tokens = EN('I like cheese')
|
2015-08-04 13:55:28 +00:00
|
|
|
assert matcher(tokens) == []
|
|
|
|
|
|
|
|
|
2015-08-04 23:05:54 +00:00
|
|
|
def test_match_start(matcher, EN):
|
|
|
|
tokens = EN('JavaScript is good')
|
2015-08-06 12:33:35 +00:00
|
|
|
assert matcher(tokens) == [(EN.vocab.strings['PRODUCT'], 0, 1)]
|
2015-08-04 23:05:54 +00:00
|
|
|
|
2015-08-04 13:55:28 +00:00
|
|
|
|
2015-08-04 23:05:54 +00:00
|
|
|
def test_match_end(matcher, EN):
|
2015-09-06 03:40:10 +00:00
|
|
|
tokens = EN('I like java')
|
2015-08-06 12:33:35 +00:00
|
|
|
assert matcher(tokens) == [(EN.vocab.strings['PRODUCT'], 2, 3)]
|
2015-08-04 13:55:28 +00:00
|
|
|
|
|
|
|
|
2015-08-04 23:05:54 +00:00
|
|
|
def test_match_middle(matcher, EN):
|
|
|
|
tokens = EN('I like Google Now best')
|
2015-08-06 12:33:35 +00:00
|
|
|
assert matcher(tokens) == [(EN.vocab.strings['PRODUCT'], 2, 4)]
|
2015-08-04 13:55:28 +00:00
|
|
|
|
|
|
|
|
2015-08-04 23:05:54 +00:00
|
|
|
def test_match_multi(matcher, EN):
|
2015-09-06 03:40:10 +00:00
|
|
|
tokens = EN('I like Google Now and java best')
|
2015-08-06 12:33:35 +00:00
|
|
|
assert matcher(tokens) == [(EN.vocab.strings['PRODUCT'], 2, 4),
|
|
|
|
(EN.vocab.strings['PRODUCT'], 5, 6)]
|
|
|
|
|
2015-08-04 13:55:28 +00:00
|
|
|
|
2015-08-05 22:35:40 +00:00
|
|
|
def test_match_preserved(matcher, EN):
|
2015-09-06 03:40:10 +00:00
|
|
|
doc = EN.tokenizer('I like java')
|
2015-08-05 22:35:40 +00:00
|
|
|
EN.tagger(doc)
|
|
|
|
EN.entity(doc)
|
|
|
|
assert len(doc.ents) == 0
|
2015-09-06 03:40:10 +00:00
|
|
|
doc = EN.tokenizer('I like java')
|
2015-08-05 22:35:40 +00:00
|
|
|
matcher(doc)
|
|
|
|
assert len(doc.ents) == 1
|
|
|
|
EN.tagger(doc)
|
|
|
|
EN.entity(doc)
|
|
|
|
assert len(doc.ents) == 1
|