The code I have is
from spacy.matcher import Matcher
matcher = Matcher(nlp.vocab, validate=True)
pattern = [{'LOWER': 'play'},
{'OP': '*'}, {'OP': '!', 'LOWER': 'store'},
{'LOWER': {'IN': ["game", "pacman"]}}
]
matcher.add('HUNTING', None, pattern)
def extract_patterns(nlp_doc, matcher):
result_spans = []
matches = matcher(nlp_doc)
print("matches:", len(matches))
for match_id, start, end in matches:
span = nlp_doc[start:end]
result_spans.append(span)
return result_spans
text = ('play store game. \n play with pacman')
doc = nlp(text)
extract_patterns(doc, matcher=matcher)
The return result of the above code is the following.
[play with pacman, play store game.
play with pacman]
But the expected result is [play with pacman]
Is it possible to do with Spacy Matcher?
You can try with the pattern like this
pattern = [{'LOWER': 'play'},
{'LOWER': {'NOT_IN': ["store"]}, 'OP': '*'},
{'LOWER': {'IN': ["game", "pacman"]}}
]
This would give only 'play tennis with pacman' and not 'play tennis store with pacman'