向nltk中止列表添加单词

问题描述 投票:10回答:9

我有一些代码可以从数据集中删除停用词,因为停用列表似乎也无法删除我想要的大多数单词,因此我希望将单词添加到此停用列表中,以便在这种情况下将删除它们。我用来删除停用词的代码是:

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

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

python nltk stop-words
9个回答
13
投票

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

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

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

stopwords = nltk.corpus.stopwords.words('english')
newStopWords = ['stopWord1','stopWord2']
stopwords.extend(newStopWords)

2
投票

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


2
投票

也在寻找解决方案。经过一番周折后,我不得不将单词添加到非索引字表中。希望这会有所帮助。

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

2
投票

我在Ubuntu机器上的操作方式是,我ctrl + F表示root的“停用词”。它给了我一个文件夹。我走进了里面,里面有不同的文件。我打开了几乎只有128个单词的“英语”。加上我的话。保存并完成。


1
投票

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

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

如果停用词列表增加到几百,也请尝试查看bloom filters


0
投票

在Windows C:\ Users \ username \ AppData \ Roaming \ nltk_data \ corpora上,请转到此路径以获取停用词并根据要求进行编辑


0
投票

我使用此代码将新的停用词添加到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)

0
投票
import nltk
stopwords = nltk.corpus.stopwords.words('english')
new_words=('re','name', 'user', 'ct')
for i in new_words:
    stopwords.append(i)
print(stopwords)

0
投票

我已经发现(Python 3.7,Windows 10上的jupyter笔记本,公司防火墙)创建列表并使用“ append”命令会导致整个停用词列表被追加为原始列表的元素。

这将'停用词'变成列表列表。

Snijesh的答案很好,Jayantha的答案也很好。

© www.soinside.com 2019 - 2024. All rights reserved.