【问题标题】:Spacy Remove stopwords without affecting Named EntitiesSpacy 删除停用词而不影响命名实体
【发布时间】:2020-01-25 04:45:49
【问题描述】:

我正在尝试从字符串中删除停用词,但我想要达到的条件是不应删除字符串中的命名实体。

import spacy
nlp = spacy.load('en_core_web_sm')
text = "The Bank of Australia has an agreement according to the Letter Of Offer which states that the deduction should be made at the last date of each month"
doc = nlp(text)

如果我检查文本中的命名实体,我会得到以下信息

print(doc.ents)
(The Bank of Australia, the Letter Of Offer, the last date of each month)

删除停用词的常用方法如下所示

[token.text for token in doc if not token.is_stop]
['Bank',
 'Australia',
 'agreement',
 'according',
 'Letter',
 'Offer',
 'states',
 'deduction',
 'date',
 'month']

正常方式完全消除了我的任务所需的含义。 我想保留命名实体。

我尝试添加具有相同列表的命名实体。

list1 = [token.text for token in doc if not token.is_stop]
list2 = [str(a) for a in doc.ents]

list1 + list2

['Bank',
 'Australia',
 'agreement',
 'according',
 'Letter',
 'Offer',
 'states',
 'deduction',
 'date',
 'month',
 'The Bank of Australia',
 'the Letter Of Offer',
 'the last date of each month']

还有其他方法吗?

【问题讨论】:

    标签: python nlp spacy


    【解决方案1】:

    您可以使用token.ent_iob_token.ent_type_ 在令牌级别检查它是否是实体的一部分,参见API documentation。所以你可能想要这样的东西:

    print([token.text for token in doc if token.ent_type_ or not token.is_stop])
    

    返回

    ['The', 'Bank', 'of', 'Australia', 'agreement', 'according', 'the', 'Letter', 'Of', 'Offer', 'states', 'deduction ', 'the', 'last', 'date', 'of', 'each', 'month']

    【讨论】:

    • 感谢您的回复。但在这种情况下,我丢失了“澳大利亚银行”、“录取通知书”和“每个月的最后一天”这些代币,并且只有拆分的代币。
    • 我明白你在说什么,但请记住,“澳大利亚银行”不是一个代币,它是一个多代币实体。因此,如果我了解您要做什么,我会将不间断和非实体令牌与 doc.ents 合并。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 2020-04-06
    • 2019-09-12
    • 2019-02-19
    • 2021-05-06
    • 2017-11-13
    相关资源
    最近更新 更多