2011-04-01 116 views
4

我有一些代碼可以從我的數據集中刪除停用詞,因爲停止列表似乎並沒有刪除我希望的大多數單詞,我正在尋找將單詞添加到這個停止列表,以便它將在這種情況下刪除它們。 我使用去除停止詞的代碼是:添加單詞到nltk stoplist

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

我不能確定正確的語法用於添加的話,似乎無法在別處找到正確的一個。任何幫助表示讚賞。謝謝。

回答

1

英語停用詞是nltk/corpus/stopwords/english.txt中的一個文件(我想它會在這裏......我沒有在這臺機器上使用nltk ..最好的事情是搜索'english.txt在nltk回購)

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

也儘量尋找bloom filters如果您停止詞列表增加到幾百

+0

任何良好的英語停止字在那裏編輯它? nltk一個似乎很差 – fabrizioM 2011-04-01 11:15:38

+1

@fabrizioM http://fs1.position2.com/bm/txt/stopwords.txt這是我在我上次公司使用的名單.. – Rafi 2011-04-01 11:23:14

+0

@Rafi這是一個比NLTK !謝謝! – 2015-09-18 23:36:16

2

我總是在任何需要它的模塊的頂部做stopset = set(nltk.corpus.stopwords.words('english'))。然後,向該集合添加更多單詞很容易,而且會員檢查速度更快。

1

也在尋找解決方案。在發現一些線索和錯誤之後,我要將詞語添加到停止列表中。希望這可以幫助。

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 
1

我在我的Ubuntu機器上的做法是,我在Ctrl + F中爲「停用詞」。它給了我一個文件夾。我走進裏面有不同的文件。我打開了幾乎只有128個單詞的「英語」。添加了我的話。保存並完成。

1

您可以簡單地使用append方法將單詞添加到它:

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

或延長追加的單詞列表,作爲意見建議查理。

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

'CustomListofWordstoExclude = ['cat','dog'] stopwords.extend(CustomListofWordstoExclude)' 我用過你的代碼,但後來用'extend()'把我自己的列表添加到它 – Charlie 2018-01-10 23:26:53

+0

好點!剛剛將您的建議添加到答案! – 2018-01-12 16:07:28

0

在Windows上C:\ Users \用戶名\ AppData \漫遊\ nltk_data \語料庫去這個路徑停用詞,並根據要求