我正在尝试计算一个tfidf没有停用词的矩阵。这是我的代码:
def removeStopWords(documents):
stop_words = set(stopwords.words('italian'))
english_stop_words = set(stopwords.words('english'))
stop_words.update(list(set(english_stop_words)))
for d in documents:
document = d['document']
word_tokens = word_tokenize(document)
filtered_sentence = ''
for w in word_tokens:
if not inStopwords(w, stop_words):
filtered_sentence = w + ' ' + filtered_sentence
d['document'] = filtered_sentence[:-1]
return calculateTFIDF(documents)
def calculateTFIDF(corpus):
tfidf = TfidfVectorizer()
x = tfidf.fit_transform(corpus)
df_tfidf = pd.DataFrame(x.toarray(), columns=tfidf.get_feature_names())
return {c: s[s > 0] for c, s in zip(df_tfidf, df_tfidf.T.values)}
但是当我返回矩阵(使用形式{word:value})时,它还包含一些停用词,例如whenor il。我该如何解决?谢谢
一只萌萌小番薯
相关分类