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()'肯定吗? –