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

时间:2019-11-29 08:44:00

标签: 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)

以上代码的返回结果如下。

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

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

是否可以使用Spacy Matcher?

1 个答案:

答案 0 :(得分:-1)

您可以尝试使用这种模式

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

这只会提供“和吃豆子打网球”而不是“和吃豆子打网球”