【发布时间】: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']
还有其他方法吗?
【问题讨论】: