首页 > 解决方案 > 为什么我不能在 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

标签: pythonentity-frameworknlpspacy

解决方案


问题不在于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))

推荐阅读