下面的link显示了如何在实体跨越多个令牌的情况下添加自定义实体规则。执行此操作的代码如下所示:
import spacy
from spacy.pipeline import EntityRuler
nlp = spacy.load('en_core_web_sm', parse=True, tag=True, entity=True)
animal = ["cat", "dog", "artic fox"]
ruler = EntityRuler(nlp)
for a in animal:
ruler.add_patterns([{"label": "animal", "pattern": a}])
nlp.add_pipe(ruler)
doc = nlp("There is no cat in the house and no artic fox in the basement")
with doc.retokenize() as retokenizer:
for ent in doc.ents:
retokenizer.merge(doc[ent.start:ent.end])
from spacy.matcher import Matcher
matcher = Matcher(nlp.vocab)
pattern =[{'lower': 'no'},{'ENT_TYPE': {'REGEX': 'animal', 'OP': '+'}}]
matcher.add('negated animal', None, pattern)
matches = matcher(doc)
for match_id, start, end in matches:
span = doc[start:end]
print(span)
我试过了,但错误如下:
如果使用nlp.create_pipe('name')
创建组件:请删除nlp.create_pipe并调用nlp.add_pipe('name')
如果您传入了一个类似TextCategorizer()
的组件:使用字符串名调用nlp.add_pipe
,例如nlp.add_pipe('textcat')
如果您正在使用自定义组件:将装饰器@Language.component
(用于函数组件)或@Language.factory
(用于类组件/工厂)添加到自定义组件并为其指定名称,例如@Language.component('your_name')
。然后可以运行nlp.add_pipe('your_name')
将其添加到管道中
请问我怎样才能修好? NB:spaCy版本3.0.6
对于spaCy v2,添加实体标尺的常规方法如下所示:
对于spaCy v3,您只需将其与字符串名称一起添加,并跳过单独实例化该类:
见:https://spacy.io/usage/v3#migrating-add-pipe
您需要定义自己的方法来实例化实体标尺:
然后,您可以通过以下方式使用它:
另外,请注意,您编写的模式无效。我想你可以这样修理它:
然后,结果是
相关问题 更多 >
编程相关推荐