2015-10-01 09:21:00 +03:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from spacy.matcher import Matcher
|
2015-10-19 08:45:12 +03:00
|
|
|
from spacy.attrs import LOWER
|
|
|
|
|
2015-10-01 09:21:00 +03:00
|
|
|
|
|
|
|
def test_overlap_issue118(EN):
|
|
|
|
'''Test a bug that arose from having overlapping matches'''
|
|
|
|
doc = EN.tokenizer(u'how many points did lebron james score against the boston celtics last night')
|
|
|
|
ORG = doc.vocab.strings['ORG']
|
2015-10-19 08:45:12 +03:00
|
|
|
matcher = Matcher(EN.vocab,
|
|
|
|
{'BostonCeltics':
|
|
|
|
('ORG', {},
|
|
|
|
[
|
|
|
|
[{LOWER: 'celtics'}],
|
|
|
|
[{LOWER: 'boston'}, {LOWER: 'celtics'}],
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert len(list(doc.ents)) == 0
|
|
|
|
matches = matcher(doc)
|
|
|
|
assert matches == [(ORG, 9, 11), (ORG, 10, 11)]
|
|
|
|
ents = list(doc.ents)
|
|
|
|
assert len(ents) == 1
|
|
|
|
assert ents[0].label == ORG
|
|
|
|
assert ents[0].start == 9
|
|
|
|
assert ents[0].end == 11
|
|
|
|
|
|
|
|
|
|
|
|
def test_overlap_reorder(EN):
|
|
|
|
'''Test order dependence'''
|
|
|
|
doc = EN.tokenizer(u'how many points did lebron james score against the boston celtics last night')
|
|
|
|
ORG = doc.vocab.strings['ORG']
|
|
|
|
matcher = Matcher(EN.vocab,
|
|
|
|
{'BostonCeltics':
|
|
|
|
('ORG', {},
|
|
|
|
[
|
|
|
|
[{LOWER: 'boston'}, {LOWER: 'celtics'}],
|
|
|
|
[{LOWER: 'celtics'}],
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert len(list(doc.ents)) == 0
|
|
|
|
matches = matcher(doc)
|
|
|
|
assert matches == [(ORG, 9, 11), (ORG, 10, 11)]
|
|
|
|
ents = list(doc.ents)
|
|
|
|
assert len(ents) == 1
|
|
|
|
assert ents[0].label == ORG
|
|
|
|
assert ents[0].start == 9
|
|
|
|
assert ents[0].end == 11
|
|
|
|
|
|
|
|
|
|
|
|
def test_overlap_prefix(EN):
|
|
|
|
'''Test order dependence'''
|
|
|
|
doc = EN.tokenizer(u'how many points did lebron james score against the boston celtics last night')
|
|
|
|
ORG = doc.vocab.strings['ORG']
|
|
|
|
matcher = Matcher(EN.vocab,
|
|
|
|
{'BostonCeltics':
|
|
|
|
('ORG', {},
|
|
|
|
[
|
|
|
|
[{LOWER: 'boston'}],
|
|
|
|
[{LOWER: 'boston'}, {LOWER: 'celtics'}],
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert len(list(doc.ents)) == 0
|
|
|
|
matches = matcher(doc)
|
|
|
|
assert matches == [(ORG, 9, 10), (ORG, 9, 11)]
|
|
|
|
ents = list(doc.ents)
|
|
|
|
assert len(ents) == 1
|
|
|
|
assert ents[0].label == ORG
|
|
|
|
assert ents[0].start == 9
|
|
|
|
assert ents[0].end == 11
|
|
|
|
|
|
|
|
|
|
|
|
def test_overlap_prefix_reorder(EN):
|
|
|
|
'''Test order dependence'''
|
|
|
|
doc = EN.tokenizer(u'how many points did lebron james score against the boston celtics last night')
|
|
|
|
ORG = doc.vocab.strings['ORG']
|
|
|
|
matcher = Matcher(EN.vocab,
|
|
|
|
{'BostonCeltics':
|
|
|
|
('ORG', {},
|
|
|
|
[
|
|
|
|
[{LOWER: 'boston'}, {LOWER: 'celtics'}],
|
|
|
|
[{LOWER: 'boston'}],
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2015-10-01 09:21:00 +03:00
|
|
|
|
2015-10-19 08:45:12 +03:00
|
|
|
assert len(list(doc.ents)) == 0
|
2015-10-01 09:21:00 +03:00
|
|
|
matches = matcher(doc)
|
2015-10-19 08:45:12 +03:00
|
|
|
assert matches == [(ORG, 9, 10), (ORG, 9, 11)]
|
2015-10-01 09:21:00 +03:00
|
|
|
ents = list(doc.ents)
|
|
|
|
assert len(ents) == 1
|
|
|
|
assert ents[0].label == ORG
|
|
|
|
assert ents[0].start == 9
|
|
|
|
assert ents[0].end == 11
|
|
|
|
|