删除区分大小写的停用词

时间:2018-06-20 12:01:13

标签: python nltk case-insensitive stop-words

我正在预处理文本,并想删除德语中常见的停用词。使用以下代码[final_wordlist作为示例数据]几乎可以正常工作:

from nltk.corpus import stopwords

final_wordlist =['Status', 'laufende', 'Projekte', 'bei', 'Stand', 'Ende', 'diese', 'Bei']
stopwords_ger = stopwords.words('german')
filtered_words = [w for w in final_wordlist if w not in stopwords_ger]
print(filtered_words)

这将产生:

['Status', 'laufende', 'Projekte', 'Stand', 'Ende', 'Bei']

但是正如您所看到的,大写字母“ Bei”并没有被删除(应该这样做),因为nltk中的停用词都是小写字母。有一种不区分大小写的简便方法来删除所有停用词吗?

1 个答案:

答案 0 :(得分:4)

尝试一下:filtered_words = [w for w in final_wordlist if w.lower() not in stopwords_ger]