2015-10-01 06:21:00 +00:00
|
|
|
import pytest
|
2016-01-19 18:23:16 +00:00
|
|
|
import numpy
|
2016-04-17 13:19:17 +00:00
|
|
|
import os
|
2015-10-01 06:21:00 +00:00
|
|
|
|
2016-04-17 13:19:17 +00:00
|
|
|
import spacy
|
2015-10-01 06:21:00 +00:00
|
|
|
from spacy.matcher import Matcher
|
2016-01-19 18:23:16 +00:00
|
|
|
from spacy.attrs import ORTH, LOWER, ENT_IOB, ENT_TYPE
|
2016-04-17 13:19:17 +00:00
|
|
|
from spacy.attrs import ORTH, TAG, LOWER, IS_ALPHA, FLAG63
|
2016-04-20 14:40:36 +00:00
|
|
|
from spacy.symbols import DATE, LOC
|
2015-10-19 05:45:12 +00:00
|
|
|
|
2015-10-01 06:21:00 +00: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 05:45:12 +00:00
|
|
|
matcher = Matcher(EN.vocab,
|
|
|
|
{'BostonCeltics':
|
|
|
|
('ORG', {},
|
|
|
|
[
|
|
|
|
[{LOWER: 'celtics'}],
|
|
|
|
[{LOWER: 'boston'}, {LOWER: 'celtics'}],
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
assert len(list(doc.ents)) == 0
|
2016-09-21 18:45:20 +00:00
|
|
|
matches = [(ent_type, start, end) for ent_id, ent_type, start, end in matcher(doc)]
|
2016-10-17 14:12:22 +00:00
|
|
|
assert matches == [(ORG, doc[9:11].start_char, doc[9:11].end_char), \
|
|
|
|
(ORG, doc[10:11].start_char, doc[10:11].end_char)]
|
2015-10-19 05:45:12 +00:00
|
|
|
|
|
|
|
|
2016-04-17 13:19:17 +00:00
|
|
|
def test_overlap_issue242():
|
2016-04-17 13:40:21 +00:00
|
|
|
'''Test overlapping multi-word phrases.'''
|
2016-04-17 13:19:17 +00:00
|
|
|
|
|
|
|
patterns = [
|
|
|
|
[{LOWER: 'food'}, {LOWER: 'safety'}],
|
|
|
|
[{LOWER: 'safety'}, {LOWER: 'standards'}],
|
|
|
|
]
|
|
|
|
|
|
|
|
if os.environ.get('SPACY_DATA'):
|
|
|
|
data_dir = os.environ.get('SPACY_DATA')
|
|
|
|
else:
|
2016-10-15 12:13:41 +00:00
|
|
|
data_dir = False
|
2016-04-17 13:19:17 +00:00
|
|
|
|
2016-10-15 12:13:41 +00:00
|
|
|
nlp = spacy.en.English(path=data_dir, tagger=False, parser=False, entity=False)
|
2016-04-17 13:19:17 +00:00
|
|
|
|
2016-10-17 14:12:22 +00:00
|
|
|
nlp.matcher.add('FOOD', 'FOOD', {}, patterns,
|
|
|
|
on_match=lambda _, doc, i, match: doc.merge(match[i][2], match[i][3]))
|
2016-04-17 13:19:17 +00:00
|
|
|
|
2016-04-17 13:34:23 +00:00
|
|
|
doc = nlp.tokenizer(u'There are different food safety standards in different countries.')
|
2016-09-21 18:45:20 +00:00
|
|
|
|
|
|
|
matches = [(ent_type, start, end) for ent_id, ent_type, start, end in nlp.matcher(doc)]
|
2016-09-23 23:17:03 +00:00
|
|
|
doc.ents += tuple(matches)
|
2016-09-21 18:45:20 +00:00
|
|
|
food_safety, safety_standards = matches
|
2016-10-17 14:12:22 +00:00
|
|
|
assert food_safety[1] == len('There are different ')
|
|
|
|
assert food_safety[2] == len('There are different food safety')
|
|
|
|
assert safety_standards[1] == len('There are different food ')
|
|
|
|
assert safety_standards[2] == len('There are different food safety standards')
|
|
|
|
|
|
|
|
|
|
|
|
# These are issues that arose in the old Matcher. Rather than updating them all,
|
|
|
|
# let's see whether they re-occur --- they don't have such a high prior atm.
|
|
|
|
#
|
|
|
|
#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 = [(ent_type, start, end) for ent_id, ent_type, start, end in matcher(doc)]
|
|
|
|
# assert matches == [(ORG, 9, 11), (ORG, 10, 11)]
|
|
|
|
# doc.ents = matches[:1]
|
|
|
|
# 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 = [(ent_type, start, end) for ent_id, ent_type, start, end in matcher(doc)]
|
|
|
|
# doc.ents = matches[1:]
|
|
|
|
# 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'}],
|
|
|
|
# ]
|
|
|
|
# )
|
|
|
|
# }
|
|
|
|
# )
|
|
|
|
#
|
|
|
|
# assert len(list(doc.ents)) == 0
|
|
|
|
# matches = [(ent_type, start, end) for ent_id, ent_type, start, end in matcher(doc)]
|
|
|
|
# doc.ents += tuple(matches)[1:]
|
|
|
|
# assert matches == [(ORG, 9, 10), (ORG, 9, 11)]
|
|
|
|
# ents = doc.ents
|
|
|
|
# assert len(ents) == 1
|
|
|
|
# assert ents[0].label == ORG
|
|
|
|
# assert ents[0].start == 9
|
|
|
|
# assert ents[0].end == 11
|
|
|
|
#
|
|
|
|
#
|
2016-04-20 14:40:36 +00:00
|
|
|
# @pytest.mark.models
|
|
|
|
# def test_ner_interaction(EN):
|
|
|
|
# EN.matcher.add('LAX_Airport', 'AIRPORT', {}, [[{ORTH: 'LAX'}]])
|
|
|
|
# EN.matcher.add('SFO_Airport', 'AIRPORT', {}, [[{ORTH: 'SFO'}]])
|
|
|
|
# doc = EN(u'get me a flight from SFO to LAX leaving 20 December and arriving on January 5th')
|
2016-01-19 18:23:16 +00:00
|
|
|
|
2016-04-20 14:40:36 +00:00
|
|
|
# ents = [(ent.label_, ent.text) for ent in doc.ents]
|
|
|
|
# assert ents[0] == ('AIRPORT', 'SFO')
|
|
|
|
# assert ents[1] == ('AIRPORT', 'LAX')
|
|
|
|
# assert ents[2] == ('DATE', '20 December')
|
|
|
|
# assert ents[3] == ('DATE', 'January 5th')
|
2016-01-19 18:23:16 +00:00
|
|
|
|
2016-04-20 14:40:36 +00:00
|
|
|
|
|
|
|
# @pytest.mark.models
|
|
|
|
# def test_ner_interaction(EN):
|
|
|
|
# # ensure that matcher doesn't overwrite annotations set by the NER model
|
|
|
|
# doc = EN.tokenizer.tokens_from_list(u'get me a flight from SFO to LAX leaving 20 December and arriving on January 5th'.split(' '))
|
|
|
|
# EN.tagger(doc)
|
|
|
|
|
|
|
|
# columns = [ENT_IOB, ENT_TYPE]
|
|
|
|
# values = numpy.ndarray(shape=(len(doc),len(columns)), dtype='int32')
|
|
|
|
# # IOB values are 0=missing, 1=I, 2=O, 3=B
|
|
|
|
# iobs = [2,2,2,2,2,3,2,3,2,3,1,2,2,2,3,1]
|
|
|
|
# types = [0,0,0,0,0,LOC,0,LOC,0,DATE,DATE,0,0,0,DATE,DATE]
|
|
|
|
# values[:] = zip(iobs,types)
|
|
|
|
# doc.from_array(columns,values)
|
|
|
|
|
|
|
|
# assert doc[5].ent_type_ == 'LOC'
|
|
|
|
# assert doc[7].ent_type_ == 'LOC'
|
|
|
|
# assert doc[9].ent_type_ == 'DATE'
|
|
|
|
# assert doc[10].ent_type_ == 'DATE'
|
|
|
|
# assert doc[14].ent_type_ == 'DATE'
|
|
|
|
# assert doc[15].ent_type_ == 'DATE'
|
|
|
|
|
|
|
|
# EN.matcher.add('LAX_Airport', 'AIRPORT', {}, [[{ORTH: 'LAX'}]])
|
|
|
|
# EN.matcher.add('SFO_Airport', 'AIRPORT', {}, [[{ORTH: 'SFO'}]])
|
|
|
|
# EN.matcher(doc)
|
|
|
|
|
|
|
|
# assert doc[5].ent_type_ != 'AIRPORT'
|
|
|
|
# assert doc[7].ent_type_ != 'AIRPORT'
|
|
|
|
# assert doc[5].ent_type_ == 'LOC'
|
|
|
|
# assert doc[7].ent_type_ == 'LOC'
|
|
|
|
# assert doc[9].ent_type_ == 'DATE'
|
|
|
|
# assert doc[10].ent_type_ == 'DATE'
|
|
|
|
# assert doc[14].ent_type_ == 'DATE'
|
|
|
|
# assert doc[15].ent_type_ == 'DATE'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|