如何要求 Spacy 短语匹配匹配列表中的所有标记?

问题描述

我有以下算法:

import spacy
from spacy.matcher import PhraseMatcher

nlp = spacy.load("en_core_web_sm")
phrase_matcher = PhraseMatcher(nlp.vocab)


CAT = [nlp.make_doc(text) for text in ['pension','underwriter','health','client']]
                                
phrase_matcher.add("CATEGORY 1",None,*CAT)


text = 'The client works as a marine assistant underwriter. He has recently opted to stop paying into his pension. '
doc = nlp(text)
matches = phrase_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
CATEGORY 1 client
CATEGORY 1 underwriter
CATEGORY 1 pension

当在句子中可以找到所有单词时,我可以要求返回结果吗?我希望不会在这里看到任何内容,因为“健康”不是句子的一部分。

我可以用 PhraseMatcher 做这种类型的匹配吗?或者我是否需要更改其他类型的基于规则的匹配?谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)