18

我有一些代码可以从我的数据集中删除停用词,因为停用词列表似乎也没有删除我想要的大部分单词,我希望将单词添加到这个停用词列表中,以便它将删除他们为这个案子。我用来删除停用词的代码是:

word_list2 = [w.strip() for w in word_list if w.strip() not in nltk.corpus.stopwords.words('english')]

我不确定添加单词的正确语法,而且似乎在任何地方都找不到正确的语法。任何帮助表示赞赏。谢谢。

4

10 回答 10

24

您可以简单地使用 append 方法向其中添加单词:

stopwords = nltk.corpus.stopwords.words('english')
stopwords.append('newWord')

或扩展以附加单词列表,正如查理在评论中所建议的那样。

stopwords = nltk.corpus.stopwords.words('english')
newStopWords = ['stopWord1','stopWord2']
stopwords.extend(newStopWords)
于 2017-09-12T16:42:03.577 回答
5
import nltk
stopwords = nltk.corpus.stopwords.words('english')
new_words=('re','name', 'user', 'ct')
for i in new_words:
    stopwords.append(i)
print(stopwords)
于 2019-02-12T12:00:19.033 回答
3

我在我的 Ubuntu 机器上的做法是,我 ctrl + F 在 root 中输入“停用词”。它给了我一个文件夹。我走进了里面,里面有不同的文件。我打开了只有 128 个单词的“english”。加上我的话。保存并完成。

于 2015-03-21T08:40:49.967 回答
2

Was also looking for solution on this. After some trail and error I got to add words to the stoplist. Hope this helps.

def removeStopWords(str):
#select english stopwords
cachedStopWords = set(stopwords.words("english"))
#add custom words
cachedStopWords.update(('and','I','A','And','So','arnt','This','When','It','many','Many','so','cant','Yes','yes','No','no','These','these'))
#remove stop words
new_str = ' '.join([word for word in str.split() if word not in cachedStopWords]) 
return new_str
于 2015-01-08T13:40:00.827 回答
2

英语停用词是 nltk/corpus/stopwords/english.txt 中的一个文件(我想它会在这里......我在这台机器上没有 nltk..最好的办法是在 nltk repo 中搜索'english.txt)

您可以在此文件中添加新的停用词。

如果您的停用词列表增加到数百个,也请尝试查看布隆过滤器

于 2011-04-01T11:11:29.867 回答
2

我总是stopset = set(nltk.corpus.stopwords.words('english'))在任何需要它的模块的顶部做。然后很容易向集合中添加更多单词,而且成员资格检查速度更快。

于 2011-04-01T16:01:14.627 回答
1

我使用此代码将新的停用词添加到 python 中的 nltk 停用词列表

from nltk.corpus import stopwords
#...#
stop_words = set(stopwords.words("english"))

#add words that aren't in the NLTK stopwords list
new_stopwords = ['apple','mango','banana']
new_stopwords_list = stop_words.union(new_stopwords)

print(new_stopwords_list)
于 2019-01-20T08:58:56.680 回答
0

我发现(Python 3.7、Windows 10 上的 jupyter notebook、企业防火墙)创建一个列表并使用“附加”命令会导致整个停用词列表作为原始列表的一个元素附加。

这使“停用词”成为列表列表。

Snijesh 的回答和 Jayantha 的回答一样有效。

于 2020-01-23T17:31:10.920 回答
0
 import nltk
 nltk.download('stopwords')
 from nltk.corpus import stopwords
 #add new words to the list
 new_stopwords = ["new", "custom", "words", "add","to","list"]
 stopwrd = nltk.corpus.stopwords.words('english')
 stopwrd.extend(new_stopwords)
于 2017-12-12T06:27:32.940 回答
0

STOP_WORDS.add(“Lol”) #根据需要将新的停用词添加到语料库中

于 2021-06-07T05:17:21.130 回答