【问题标题】:Removing compound worded named entities from a document using spacy使用 spacy 从文档中删除复合词命名实体
【发布时间】:2020-06-26 01:18:30
【问题描述】:

如果某些命名实体是复合词,如何使用 spaCy 从文本中删除命名实体?

我知道Removing named entities from a document using spacy 的问题 我相信这不是该问题的重复,因为如果命名实体是复合词,则发布的已接受答案将失败。 下面显示了链接问题的接受答案失败的示例代码。

import spacy

nlp = spacy.load('en_core_web_sm')

text_data = 'This is a text document that speaks about entities like New York and Nokia'

document = nlp(text_data)

text_no_namedentities = []

ents = [e.text for e in document.ents]
for item in document:
    if item.text in ents:
        pass
    else:
        text_no_namedentities.append(item.text)
print(" ".join(text_no_namedentities))

输出:

This is a text document that speaks about entities like New York and'

从文本中删除命名实体(包括复合词实体)的最佳方法是什么? 谢谢。

附:我会将此作为对链接问题的评论发布,但作为新用户,我缺乏足够的声誉来发表评论。我尝试将其作为答案发布在那里,但由于我不知道解决方案(只有接受的答案会因复合词而失败),所以它不是一个好的答案,因此被删除。一个新问题似乎是留给我的最后手段,但如果这不合适,任何关于在这种情况下正确行动的建议将不胜感激。

【问题讨论】:

    标签: python nlp spacy named-entity-recognition


    【解决方案1】:

    如果令牌是命名实体的一部分,则其token.ent_type_ 属性将属于该实体类型,即使它是复合命名实体的一部分。您可以使用以下代码实现您正在寻找的内容:

    import spacy
    
    nlp = spacy.load('en_core_web_sm')
    
    text_data = 'This is a text document that speaks about entities like New York and Nokia. My name is John Smith'
    
    doc = nlp(text_data)
    
    # You can add other Named Entities types that you would want to remove to the list
    text_without_ne = [token.text for token in doc if token.ent_type_ not in ['GPE', 'PERSON']]
    
    print(" ".join(text_without_ne))
    

    输出

    This is a text document that speaks about entities like and . My name is
    

    【讨论】:

      猜你喜欢
      • 2020-04-06
      • 1970-01-01
      • 2020-01-25
      • 1970-01-01
      • 2021-02-23
      • 1970-01-01
      • 2019-09-12
      • 2019-02-19
      • 2023-03-24
      相关资源
      最近更新 更多