【发布时间】:2020-11-04 08:19:45
【问题描述】:
我是 Python NER 的新手,正在尝试用它们的标签替换文本输入中的命名实体。
from nerd import ner
input_text = """Stack Overflow is a question and answer site for professional and enthusiast programmers. It is a privately held website, the flagship site of the Stack Exchange Network,[5][6][7] created in 2008 by Jeff Atwood and Joel Spolsky."""
doc = ner.name(input_text, language='en_core_web_sm')
text_label = [(X.text, X.label_) for X in doc]
print(text_label)
输出为:[('2008', 'DATE'), ('Jeff Atwood', 'PERSON'), ('Joel Spolsky', 'PERSON')]
然后我可以提取人,例如:
people = [i for i,label in text_label if 'PERSON' in label]
print(people)
获取['Jeff Atwood', 'Joel Spolsky']。
我的问题是如何替换原始输入文本中已识别的命名实体,以便结果是:
Stack Overflow is a question and answer site for professional and enthusiast programmers. It is a privately held website, the flagship site of the Stack Exchange Network,[5][6][7] created in DATE by PERSON and PERSON.
非常感谢!
【问题讨论】:
标签: python nlp spacy named-entity-recognition