0
我有以下代碼。我必須在nltk stopword列表中添加更多的單詞。在我運行thsi之後,它不會添加列表中的單詞如何在nltk列表中添加更多停用詞?
from nltk.corpus import stopwords
from nltk.stem.wordnet import WordNetLemmatizer
import string
stop = set(stopwords.words('english'))
new_words = open("stopwords_en.txt", "r")
new_stopwords = stop.union(new_word)
exclude = set(string.punctuation)
lemma = WordNetLemmatizer()
def clean(doc):
stop_free = " ".join([i for i in doc.lower().split() if i not in new_stopwords])
punc_free = ''.join(ch for ch in stop_free if ch not in exclude)
normalized = " ".join(lemma.lemmatize(word) for word in punc_free.split())
return normalized
doc_clean = [clean(doc).split() for doc in emails_body_text]
請修正縮進代碼 - 它沒有意義的方式,你有它。 – alexis
'new_stopwords = stop.union(new_word)'一定要讀'new_stopwords = stop.union(new_words)'?此外,'new_words = open(「stopwords_en.txt」,「r」)'會返回一個文件對象,所以您將文件對象添加到停用詞列表中,而不是內容。你想像'new_words = open(「stopwords_en.txt」,「r」)。readlines()'肯定嗎? –