将NLTK停止字与scikitlearn的TfidVectorizer一起使用时出现Unicode警告

2024-03-28 17:44:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用scikit learn的Tf idf Vectorizer,使用NLTK的西班牙语stopwords:

from nltk.corpus import stopwords

vectorizer = TfidfVectorizer(stop_words=stopwords.words("spanish"))

问题是我收到了以下警告:

^{pr2}$

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


Tags: fromimporttfcorpusscikitlearnstopwords
1条回答
网友
1楼 · 发布于 2024-03-28 17:44:20

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

stopwords = [word.decode('utf-8') for word in stopwords.words('spanish')]

相关问题 更多 >