如何使用 Spacy Matcher 松散地匹配中间的单词

How to loosely match words in between with exception using Spacy Matcher

我的密码是

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)

以上代码的return结果如下

[play with pacman, play store game. 
 play with pacman]

但预期的结果是[play with pacman]

是否可以使用 Spacy Matcher?

你可以试试这样的模式

pattern = [{'LOWER': 'play'},
           {'LOWER': {'NOT_IN': ["store"]}, 'OP': '*'},
           {'LOWER': {'IN': ["game", "pacman"]}}
           ]

这只会给出 'play tennis with pacman' 而不会 'play tennis store with pacman'