gpt4 book ai didi

python - 改变 Spacy 中的单个实体

转载 作者:行者123 更新时间:2023-11-30 21:58:36 25 4
gpt4 key购买 nike

是否可以更改 Spacy 中的单个实体?我的列表中有一些文档对象,其中一些文档包含“FRAUD”标签。但是,我需要将一些“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.

最佳答案

错误消息告诉您发生了什么事:spacy不允许重叠实体,并且您尝试将新实体添加到 token 而不先删除原始实体。你想要的东西更像是:

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]

这是对当前代码的一行更改,以使其正常工作,但列表理解确实效率很低。除非您的匹配项非常少,否则您可能希望重新构建处理匹配项的方式,以实现此目的,而无需重复迭代整个实体列表。将所有匹配作为列表处理 (matches = matcher(doc)) 可能比使用回调函数更有意义。

关于python - 改变 Spacy 中的单个实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54868693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com