我是文本挖掘和 python 的新手,我正在尝试做一个简单的任务。我想从句子中创建 TF 矩阵:['This is the first sentence','This is the second sentence','This is the third sentence']
并在循环中(或以某种方式)将新句子与该矩阵进行比较。
在stackoverflow上,我找到了很好的例子,但在我的例子中,它每次都会计算样本句子和新句子的TF矩阵。它在大型数据集上运行会有点慢。
from sklearn.feature_extraction.text import TfidfVectorizer
vect = TfidfVectorizer()
text = []
text = ['This is the first sentence','This is the second sentence', 'This is the third sentence']
text.append('new sentence')
tfidf = vect.fit_transform(text)
# Get an array of results
results = ( tfidf * tfidf.T ).A
我想知道如何以其他更准确的方式做到这一点,谢谢。