我正在尝试使用Spacy基于规则的匹配,如下所示:
nlp_ruler = EntityRuler(nlp, overwrite_ents=True, validate=True)
nlp_ruler.add_patterns(match_rules)
ents = nlp_ruler(doc).ents
事实是,实体是匹配的,但标记是分别标记的。因此,如果两个标记组成一个街道名称,则两个单词(标记)都将被标记为“ street_name”,但是在实体对象本身似乎没有任何迹象表明它们确实已连接。
与基于规则的实体匹配不同,如果使用NER,则似乎存在诸如IOB和BILUO方案的指示符。但是这些不适用于规则库匹配。那么如何合并实体? (由于某种原因,merge_entities在这里也不起作用...)
答案 0 :(得分:0)
您可能需要仔细检查模式,看它们是否正确构建,这是一个可供参考的有效示例:
import en_core_web_sm
from spacy.pipeline import EntityRuler
nlp = en_core_web_sm.load()
ruler = EntityRuler(nlp, overwrite_ents=True, validate=True)
ruler.add_patterns([
{"label": "WHEN", "pattern": [{"LOWER": "the"}, {"LOWER": "other"}, {"LOWER": "day"}]},
{"label": "WHAT", "pattern": [{"LOWER": "camouflage"}, {"LOWER": "trousers"}]},
])
nlp.add_pipe(ruler)
doc = nlp("I went to buy some camouflage trousers the other day but I couldn’t find any.")
ents = ruler(doc).ents
ents = [(ent.text, ent.label_) for ent in doc.ents]
ents
输出:
[('camouflage trousers', 'WHAT'), ('the other day', 'WHEN')]