[英]NLTK Stopword List
我有下面的代碼,我試圖將停用詞列表應用於單詞列表。 然而,結果仍然顯示“a”和“the”這樣的詞,我認為這個詞會被這個過程刪除。 任何出錯的想法都會很棒。
import nltk
from nltk.corpus import stopwords
word_list = open("xxx.y.txt", "r")
filtered_words = [w for w in word_list if not w in stopwords.words('english')]
print filtered_words
一些值得注意的事情。
如果您要反復檢查列表中的成員資格,我會使用集合而不是列表。
stopwords.words('english')
返回一個小寫停用詞列表。 您的來源很可能包含大寫字母,因此不匹配。
您沒有正確讀取文件,您正在檢查文件對象而不是按空格分割的單詞列表。
把它們放在一起:
import nltk
from nltk.corpus import stopwords
word_list = open("xxx.y.txt", "r")
stops = set(stopwords.words('english'))
for line in word_list:
for w in line.split():
if w.lower() not in stops:
print w
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.