是否可以在 Spacy 中更改单个实体?我在列表中有一些文档对象,其中一些文档包含“欺诈”标签。但是,我需要将一些“FRAUD”实体标签更改为“FALSE_ALARM”。我正在使用 Spacy 的匹配器来查找“FALSE_ALARM”实体,但我无法覆盖现有标签。我尝试了以下方法:
def add_event_ent(matcher, doc, i, matches):
match_id, start, end = matches[i]
match_doc = doc[start:end]
for entity in match_doc.ents:
# k.label = neg_hash <-- says " attribute 'label' of 'spacy.tokens.span.Span' objects is not writable"
span = Span(doc, entity.start, entity.end, label=false_alarm_hash)
doc.ents = list(doc.ents) + [span] # add span to doc.ents
ValueError: [E098] Trying to set conflicting doc.ents: '(14, 16,
'FRAUD')' and '(14, 16, 'FALSE_ALARM')'. A token can only be part of one entity, so make sure the entities you're setting don't overlap.
Run Code Online (Sandbox Code Playgroud)
错误消息告诉您发生了什么:spacy 不允许重叠实体,并且您正在尝试将新实体添加到令牌而不先删除原始实体。你想要更像的东西:
for entity in match_doc.ents:
span = Span(doc, entity.start, entity.end, label=false_alarm_hash)
doc.ents = [span if e == entity else e for e in doc.ents]
Run Code Online (Sandbox Code Playgroud)
这是对当前代码的一行更改以使其工作,但列表理解确实效率低下。除非您的匹配项很少,否则您可能希望重组处理匹配项的方式来完成此操作,而无需重复遍历整个实体列表。将所有匹配项处理为列表 ( matches = matcher(doc)) 而不是使用回调函数可能更有意义。
| 归档时间: |
|
| 查看次数: |
3241 次 |
| 最近记录: |