2017-09-30 15 views
0

だから、テキストファイルからすべてのストップワードを削除しようとしている。問題は、単語ごとにストップワードを削除することです。私の関数remove_stopwords。単語のすべてのストップワードを削除する

def remove_stopwords(input): 
    stop_words = set(stopwords.words('english')) 
    filtered_words = [word for word in input if not word in stop_words] 
    return filtered_words 

Sample Input: Damage from Typhoon Lando soars to P6B 
Output: Dge fr Tphn Ln r P6B 
+1

もし 'input'が文字列であれば、それを単語に分割する必要があります。たとえば、' '単語がstop_wordsにないならinput.split()内の単語 ''、次にdoあなたが結果のリストで何をしたいのですか...そうでなければ、各文字を繰り返して、その文字がストップワード内に存在する場所を取り除いています。 –

+0

@JonClementsありがとうございました! –

答えて

2

ストップワードを削除する前にstr入力をトークン化してください。

from nltk.corpus import stopwords 
from nltk import word_tokenize 

stoplist = set(stopwords.words('english')) 

def remove_stopwords(text): 
    return [word for word in word_tokenize(text) if not word in stoplist] 
+1

「単語ではない」と「単語ではない」をメモするだけです... https://stackoverflow.com/questions/8738388/order-of-syntax-for-using-not-and-in-keywords( 'not実際には技術的な違いはありませんが、英語に近いほど一般的にはより明確になると考えられています) –

+0

「not in word」 - >「not in word」を変更しました。それではまた、私はこれが人々をhttps://stackoverflow.com/questions/8738388/order-of-syntax-for-using-not-and-in-keywordsに導くのはすばらしいと思っています。 「言葉ではない」これを指摘してくれてありがとう@JonClements; P – alvas

関連する問題