这是我的代码
from sklearn.feature_extraction.text import TfidfVectorizer
corpus = [
"this is first document ","this is second document","this is third","which document is first", ]
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(corpus)
X.toarray()
现在 这就是我想做的?
当我搜索document
它应该给我[1,2,4]文档(句子(
当我搜索first document
它应该给我[1]文档(句子(
当我搜索second
时,它应该给我[2]文档(句子(
我想用 TfIdf 做这件事(我不能做正常搜索(
我该怎么做?
首先,你必须问自己一个问题:TfidfVectorizer是做什么的?答案是:它将您的文档转换为矢量。您如何继续前进?一种解决方案是使用矢量化器将查询也转换为矢量。然后,您可以比较转换后的查询向量与数据库中文档的每个向量之间的余弦相似性。与查询向量具有最高余弦相似度的文档是最相关的文档(至少根据向量空间模型(。 这里 https://towardsdatascience.com/tf-idf-for-document-ranking-from-scratch-in-python-on-real-world-dataset-796d339a4089 是一个示例实现。