【发布时间】:2021-03-02 17:17:36
【问题描述】:
我尝试提取一些关键字,但我不确定句子结构是什么。
import spacy
from spacy.matcher import Matcher
nlp = spacy.load("en_core_web_sm")
matcher = Matcher(nlp.vocab, validate=True)
patterns = [{"LOWER": "cat"}, {"OP": "?"}, {"OP": "?"}, {"OP": "?"}, {"LOWER": "cute"}]
matcher.add("CAT", None, patterns)
doc = nlp(u"I have a white cat. It is cute; I have a cute cat. It is white")
matches = matcher(doc)
for match_id, start, end in matches:
rule_id = nlp.vocab.strings[match_id] # get the unicode ID, i.e. 'CategoryID'
span = doc[start : end] # get the matched slice of the doc
print(rule_id, span.text)
#Output
CAT cat. It is cute
此模式仅显示顺序为 cat -> 可爱的结果,但没有可爱 -> cat。由于我不确定句子是什么样的,如何更改它以反映两个方向?或者我是否需要创建另一个模式来捕捉另一个方向?谢谢。
【问题讨论】: