我试图用这样的EntityRuler找到FRT
实体:
from spacy.lang.en import English
from spacy.pipeline import EntityRuler
nlp = English()
ruler = EntityRuler(nlp)
patterns = [{"label": "FRT", "pattern": [{'REGEX': "[Aa]ppl[e|es])"}]},
{"label": "BRN", "pattern": [{"LOWER": "granny"}, {"LOWER": "smith"}]}]
ruler.add_patterns(patterns)
nlp.add_pipe(ruler)
doc = nlp(u"Apple is red. Granny Smith apples are green.")
print([(ent.text, ent.label_) for ent in doc.ents])
我得到了这个结果
^{pr2}$你能告诉我如何修正我的代码,这样我就能得到这个结果吗
[('Apple', 'FRT'), ('Granny Smith', 'BRN'), ('apples', 'FRT')]
提前谢谢你。在
您错过了试图在正则表达式中匹配的顶级令牌属性。由于缺少top lever token属性,因此忽略REGEX键,并将模式解释为“any token”
工作代码
输出
[('Apple', 'FRT'), ('Granny Smith', 'BRN'), ('apples', 'FRT')]
事实上,您还可以为
apple
使用下面的模式{"label": "FRT", "pattern": [{'LOWER' : {'REGEX': "appl[e|es]"}}]}
您需要使用以下
patterns
声明修复整个代码:有两件事:1)如果不在
TEXT
、LOWER
等下定义,REGEX
运算符本身就不起作用。top-level token和2)当使用字符类而不是分组构造时,您使用的正则表达式已损坏。在注意,}匹配。因此,如果有一个
[e|es]
是regex character class,与e
、s
或{Appl| is red.
字符串,结果将包含[('Appl|', 'FRT')
。您需要使用non-capturing group-(?:es|s)
,或者只使用匹配e
的es?
,然后使用可选的s
。在同样,请参考以下场景:
[{"TEXT" : {"REGEX": "[Aa]pples?"}}]
将找到Apple
、apple
、Apples
、apples
,但找不到{[{"LOWER" : {"REGEX": "apples?"}}]
将找到Apple
、apple
、Apples
、apples
、APPLES
、aPPleS
等。而且还stapples
(一个staples
的拼写错误)[{"TEXT" : {"REGEX": r"\b[Aa]pples?\b"}}]
将找到Apple
、apple
、Apples
、apples
,但找不到{stapples
,因为\b
是单词边界。在相关问题 更多 >
编程相关推荐