gpt4 book ai didi

nlp - 在 Spacy 中标记命名实体

转载 作者:行者123 更新时间:2023-12-01 21:58:42 28 4
gpt4 key购买 nike

谁能帮忙吗

我正在尝试使用 Spacy 对文档进行标记化,从而对命名实体进行标记化。例如:

'纽约是美利坚合众国的一个城市'

将被标记为:

['纽约', '是', 'a', '城市', '在', 'the', '美利坚合众国']

非常欢迎任何有关如何执行此操作的提示。看过使用 span.merge(),但没有成功,但我是编码新手,所以可能遗漏了一些东西。

提前致谢

最佳答案

使用 doc.retokenize将实体跨度合并为单个标记的上下文管理器。将其包装在 custom pipeline component 中,并将该组件添加到您的语言模型中。

import spacy

class EntityRetokenizeComponent:
def __init__(self, nlp):
pass
def __call__(self, doc):
with doc.retokenize() as retokenizer:
for ent in doc.ents:
retokenizer.merge(doc[ent.start:ent.end], attrs={"LEMMA": str(doc[ent.start:ent.end])})
return doc

nlp = spacy.load('en')
retokenizer = EntityRetokenizeComponent(nlp)
nlp.add_pipe(retokenizer, name='merge_phrases', last=True)

doc = nlp("German Chancellor Angela Merkel and US President Barack Obama "
"converse in the Oval Office inside the White House in Washington, D.C.")

[tok for tok in doc]

#[German,
# Chancellor,
# Angela Merkel,
# and,
# US,
# President,
# Barack Obama,
# converse,
# in,
# the Oval Office,
# inside,
# the White House,
# in,
# Washington,
# ,,
# D.C.]

关于nlp - 在 Spacy 中标记命名实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54640715/

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