python - 为什么我不能在 doc_ents 上进行过滤?
问题描述
我在 spacy 中遇到一件非常奇怪的事情。我想要。确定除其中一些实体之外的所有实体。所以我这样做了:
for ent in x3.ents:
# print(str(spacy.explain(ent.label_)))
if not ent.label_ in [ 'ORG', 'PERSON']:
if not ent.text in { 'technician', 'service',' hcc'}:
print(ent.text)
而是technician
打印出来的。
我的文档有很多行,例如:
agricultural English
balancer
front office director
clinical laboratory technician ii
对于这 4 行,我的 ent.text 是:
English
technician
解决方案
问题不在于spacy
,而在于您尝试过滤句子的方式。您需要将其中的每个单词ent.text
与要丢弃的单词列表进行比较 ( { 'technician', 'service',' hcc'}
)。例如:
# This could be your ent.text
s = "my sentence contains technician"
new_s = []
for w in s.split(" "):
if w not in { 'technician', 'service',' hcc'}:
new_s.append(w)
# Here you would consider to replace the original ent.text
print(" ".join(new_s))