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
|
2016-09-21 12:54:55 +00:00
|
|
|
from spacy.en import English
|
2015-08-04 13:55:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2016-09-21 12:54:55 +00:00
|
|
|
def matcher():
|
2015-08-06 12:33:35 +00:00
|
|
|
patterns = {
|
2016-09-21 12:54:55 +00:00
|
|
|
'JS': ['PRODUCT', {}, [[{'ORTH': 'JavaScript'}]]],
|
2015-08-06 14:09:28 +00:00
|
|
|
'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
|
|
|
}
|
2016-09-25 12:49:53 +00:00
|
|
|
return Matcher(Vocab(lex_attr_getters=English.Defaults.lex_attr_getters), 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
|
|
|
|
2016-09-21 12:54:55 +00:00
|
|
|
def test_no_match(matcher):
|
2016-10-16 23:53:24 +00:00
|
|
|
doc = Doc(matcher.vocab, words=['I', 'like', 'cheese', '.'])
|
2016-09-21 12:54:55 +00:00
|
|
|
assert matcher(doc) == []
|
|
|
|
|
|
|
|
|
|
|
|
def test_match_start(matcher):
|
2016-10-16 23:53:24 +00:00
|
|
|
doc = Doc(matcher.vocab, words=['JavaScript', 'is', 'good'])
|
2016-09-21 12:54:55 +00:00
|
|
|
assert matcher(doc) == [(matcher.vocab.strings['JS'],
|
2016-10-17 14:00:10 +00:00
|
|
|
matcher.vocab.strings['PRODUCT'], 0, len('JavaScript'))]
|
2016-09-21 12:54:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_match_end(matcher):
|
2016-10-16 23:53:24 +00:00
|
|
|
doc = Doc(matcher.vocab, words=['I', 'like', 'java'])
|
2016-09-21 12:54:55 +00:00
|
|
|
assert matcher(doc) == [(doc.vocab.strings['Java'],
|
2016-10-17 14:00:10 +00:00
|
|
|
doc.vocab.strings['PRODUCT'],
|
|
|
|
len('I like '), len('I like java'))]
|
2016-09-21 12:54:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_match_middle(matcher):
|
2016-10-16 23:53:24 +00:00
|
|
|
doc = Doc(matcher.vocab, words=['I', 'like', 'Google', 'Now', 'best'])
|
2016-09-21 12:54:55 +00:00
|
|
|
assert matcher(doc) == [(doc.vocab.strings['GoogleNow'],
|
2016-10-17 14:00:10 +00:00
|
|
|
doc.vocab.strings['PRODUCT'], len('I like '),
|
|
|
|
len('I like Google Now'))]
|
2016-09-21 12:54:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_match_multi(matcher):
|
2016-10-16 23:53:24 +00:00
|
|
|
doc = Doc(matcher.vocab, words='I like Google Now and java best'.split())
|
2016-09-21 12:54:55 +00:00
|
|
|
assert matcher(doc) == [(doc.vocab.strings['GoogleNow'],
|
2016-10-17 14:00:10 +00:00
|
|
|
doc.vocab.strings['PRODUCT'], len('I like '),
|
|
|
|
len('I like Google Now')),
|
2016-09-21 12:54:55 +00:00
|
|
|
(doc.vocab.strings['Java'],
|
2016-10-17 14:00:10 +00:00
|
|
|
doc.vocab.strings['PRODUCT'],
|
|
|
|
len('I like Google Now and '),
|
|
|
|
len('I like Google Now and java'))]
|
2016-09-21 12:54:55 +00:00
|
|
|
|
|
|
|
def test_match_zero(matcher):
|
|
|
|
matcher.add('Quote', '', {}, [
|
|
|
|
[
|
|
|
|
{'ORTH': '"'},
|
|
|
|
{'OP': '!', 'IS_PUNCT': True},
|
|
|
|
{'OP': '!', 'IS_PUNCT': True},
|
|
|
|
{'ORTH': '"'}
|
|
|
|
]])
|
2016-10-16 23:53:24 +00:00
|
|
|
doc = Doc(matcher.vocab, words='He said , " some words " ...'.split())
|
2016-09-21 12:54:55 +00:00
|
|
|
assert len(matcher(doc)) == 1
|
2016-10-16 23:53:24 +00:00
|
|
|
doc = Doc(matcher.vocab, words='He said , " some three words " ...'.split())
|
2016-09-21 12:54:55 +00:00
|
|
|
assert len(matcher(doc)) == 0
|
|
|
|
matcher.add('Quote', '', {}, [
|
|
|
|
[
|
|
|
|
{'ORTH': '"'},
|
|
|
|
{'IS_PUNCT': True},
|
|
|
|
{'IS_PUNCT': True},
|
|
|
|
{'IS_PUNCT': True},
|
|
|
|
{'ORTH': '"'}
|
|
|
|
]])
|
|
|
|
assert len(matcher(doc)) == 0
|
|
|
|
|
|
|
|
|
|
|
|
def test_match_zero_plus(matcher):
|
|
|
|
matcher.add('Quote', '', {}, [
|
|
|
|
[
|
|
|
|
{'ORTH': '"'},
|
|
|
|
{'OP': '*', 'IS_PUNCT': False},
|
|
|
|
{'ORTH': '"'}
|
|
|
|
]])
|
2016-10-16 23:53:24 +00:00
|
|
|
doc = Doc(matcher.vocab, words='He said , " some words " ...'.split())
|
2016-09-21 12:54:55 +00:00
|
|
|
assert len(matcher(doc)) == 1
|
2015-08-06 12:33:35 +00:00
|
|
|
|
2015-08-04 13:55:28 +00:00
|
|
|
|
2016-10-17 14:00:10 +00:00
|
|
|
@pytest.mark.models
|
|
|
|
def test_match_preserved(EN):
|
|
|
|
patterns = {
|
|
|
|
'JS': ['PRODUCT', {}, [[{'ORTH': 'JavaScript'}]]],
|
|
|
|
'GoogleNow': ['PRODUCT', {}, [[{'ORTH': 'Google'}, {'ORTH': 'Now'}]]],
|
|
|
|
'Java': ['PRODUCT', {}, [[{'LOWER': 'java'}]]],
|
|
|
|
}
|
|
|
|
matcher = Matcher(EN.vocab, patterns)
|
|
|
|
doc = EN.tokenizer('I like java.')
|
|
|
|
EN.tagger(doc)
|
|
|
|
assert len(doc.ents) == 0
|
|
|
|
doc = EN.tokenizer('I like java.')
|
|
|
|
doc.ents += tuple(matcher(doc))
|
|
|
|
assert len(doc.ents) == 1
|
|
|
|
EN.tagger(doc)
|
|
|
|
EN.entity(doc)
|
|
|
|
assert len(doc.ents) == 1
|