spaCy Matcher 条件或/和 Python

问题描述

我想对以下关键字进行分类

import spacy
from spacy.matcher import PhraseMatcher

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

cat_patterns = [nlp(text) for text in ('cat','cute','fat')]
dog_patterns = [nlp(text) for text in ('dog','fat')]

matcher = PhraseMatcher(nlp.vocab)
matcher.add('Category1',None,*cat_patterns)
matcher.add('Category2',*dog_patterns)

doc = nlp("I have a white cat. It is cute and fat; I have a black dog. It is fat,too")
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
#Category1 cat
#Category1 cute
#Category1 fat
#Category2 fat
#Category2 dog
#Category1 fat
#Category2 fat

然而,我的预期输出是如果文本包含猫和可爱或猫和脂肪一起,它会落入第一类;如果文本同时包含狗和脂肪,则属于第二类。

#Category1 cat cute
#Category1 cat fat
#Category2 dog fat

是否可以使用类似的算法来实现?谢谢

解决方法

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

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

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