【发布时间】:2020-03-04 11:46:27
【问题描述】:
我正在尝试使用 Spacy 从文档中删除命名实体。我没有发现识别命名实体的任何麻烦。使用此代码:
ne = [(ent.text, ent.label_) for ent in doc.ents]
print(ne)
persons = [ent.text for ent in doc.ents if ent.label_ == 'PERSON']
print(persons)
输出:
'Timothy D. Cook',
'Peter',
'Peter',
'Benjamin A. Reitzes',
'Timothy D. Cook',
'Steve Milunovich',
'Steven Mark Milunovich',
'Peter',
'Luca Maestri'
但后来我试图使用这个块从文档中实际删除它们:
text_no_namedentities = []
ents = [e.text for e in doc.ents]
for item in doc:
if item.text in ents:
pass
else:
text_no_namedentities.append(item.text)
print(" ".join(text_no_namedentities))
它不起作用,因为 NE 是 n-gram。如果我只是检查一小块 spacy 对象的内容,如下所示:
for item in doc:
print(item.text)
iPad
has
a
78
%
Steve
Milunovich
share
of
the
U.S.
commercial
tablet
market
所以 spacy 对象被标记化了。因此,我无法使用上面的代码删除 NE。关于如何从对象中删除所有命名实体的任何想法?
【问题讨论】:
标签: python nlp spacy named-entity-recognition