使用 python spacy 从字符串中删除单词而不影响单词

removing words from strings without affecting words using python spacy

我正在使用 spacy,我有一个句子列表,我想从中删除停用词和标点符号。

for i in sentences_list: 
for token in docfile:
    if token.is_stop or token.is_punct and token.text in i[1]:
       i[1] = i[1].replace(token.text, '') 
print(sentences_list)

但它也会影响单词,例如单词 I 是停用词,所以单词 big 变成 bg

您可以使用:

" ".join([token.text for token in doc if not token.is_stop and not token.is_punct])

这是一个示例代码演示:

import spacy
nlp = spacy.load("en_core_web_sm")
sentences_list = ["I like big planes.", "No, I saw no big flames."]
new_sentence_list = []
for i in sentences_list:
    doc = nlp(i)
    new_sentence_list.append(" ".join([token.text for token in doc if not token.is_stop and not token.is_punct]))

new_sentence_list现在是:

['like big planes', 'saw big flames']