我想在一组文档中找到最相关的单词。
我想在 3 个文档上调用 Tf Idf 算法,并返回一个包含每个单词及其频率的 csv 文件。
之后,我将只选取那些数字较高的并使用它们。
我发现这个实现可以满足我的需要https://github.com/mccurdyc/tf-idf/.
我用subprocess
图书馆。但该代码存在一个巨大的问题:它在分析单词时犯了很多错误。它混合了一些单词,有问题'
and -
(我认为)。我在 3 本书(哈利·波特)的文本中使用它,例如,我获得这样的单词hermiones, hermionell, riddlehermione, thinghermione
而不仅仅是hermione
在 csv 文件中。
我做错了什么吗?你能给我一个 Tf idf 算法的工作实现吗?有没有一个 python 库可以做到这一点?
这是 Tf-idf 算法的实现,使用scikit学习。
在应用之前,您可以word_tokenize() and stem你的话。
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from nltk import word_tokenize
from nltk.stem.porter import PorterStemmer
def tokenize(text):
tokens = word_tokenize(text)
stems = []
for item in tokens: stems.append(PorterStemmer().stem(item))
return stems
# your corpus
text = ["This is your first text book", "This is the third text for analysis", "This is another text"]
# word tokenize and stem
text = [" ".join(tokenize(txt.lower())) for txt in text]
vectorizer = TfidfVectorizer()
matrix = vectorizer.fit_transform(text).todense()
# transform the matrix to a pandas df
matrix = pd.DataFrame(matrix, columns=vectorizer.get_feature_names())
# sum over each document (axis=0)
top_words = matrix.sum(axis=0).sort_values(ascending=False)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)