首页 > 解决方案 > python中带有Tf-Idf的搜索引擎

问题描述

这是我的代码

 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 来做这个(我不能做正常的搜索)

我怎样才能做到这一点?

标签: pythonmachine-learning

解决方案


首先,你要问自己一个问题:TfidfVectorizer 是做什么的?答案是:它将您的文档转换为向量。你怎么能更进一步?一种解决方案是使用矢量化器将您的查询也转换为矢量。然后,您可以比较转换后的查询向量与数据库中文档的每个向量之间的余弦相似度。与您的查询向量具有最高余弦相似度的文档是最相关的文档(至少根据向量空间模型)。这里https://towardsdatascience.com/tf-idf-for-document-ranking-from-scratch-in-python-on-real-world-dataset-796d339a4089是一个示例实现。


推荐阅读