使用带有scikit-learn的TfidfVectorizer的NLTK停用词时的Unicode警告

mar*_*ian 3 python unicode nltk python-2.7 scikit-learn

我正在尝试使用来自sckit-learn的Tf-idf Vectorizer,使用来自NLTK的西班牙语停用词:

from nltk.corpus import stopwords

vectorizer = TfidfVectorizer(stop_words=stopwords.words("spanish"))
Run Code Online (Sandbox Code Playgroud)

问题是我得到以下警告:

/home/---/.virtualenvs/thesis/local/lib/python2.7/site-packages/sklearn/feature_extraction/text.py:122: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
tokens = [w for w in tokens if w not in stop_words]
Run Code Online (Sandbox Code Playgroud)

有没有简单的方法来解决这个问题?

mar*_*ian 5

实际上问题比我想象的更容易解决.这里的问题是NLTK不返回unicode对象,而是str对象.所以我需要在使用之前从utf-8解码它们:

stopwords = [word.decode('utf-8') for word in stopwords.words('spanish')]
Run Code Online (Sandbox Code Playgroud)