python - 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 来做这个(我不能做正常的搜索)
我怎样才能做到这一点?
解决方案
首先,你要问自己一个问题:TfidfVectorizer 是做什么的?答案是:它将您的文档转换为向量。你怎么能更进一步?一种解决方案是使用矢量化器将您的查询也转换为矢量。然后,您可以比较转换后的查询向量与数据库中文档的每个向量之间的余弦相似度。与您的查询向量具有最高余弦相似度的文档是最相关的文档(至少根据向量空间模型)。这里https://towardsdatascience.com/tf-idf-for-document-ranking-from-scratch-in-python-on-real-world-dataset-796d339a4089是一个示例实现。
推荐阅读
- python - Python windrose 90 度偏离和错误的标签
- javascript - 为不在锚点内的所有图像添加事件侦听器
- matlab - 在 MATLAB 中按数据 ID 对 .csv 中的行进行分组
- c++ - 如何将所有内核用于 C++ 程序
- r - 当代码在函数内部时,RStudio 中的 Rmarkdown 块预览不起作用
- laravel - 多次编译同一个scss文件——laravel mix
- amazon-web-services - 容器的 ECS 健康检查通过,但容器无响应
- java - 通过使用 maven 构建指定模块的依赖树来构建 classpath.properties
- amazon-web-services - AWS RedShift:并发扩展在峰值期间不添加集群
- laravel - 如何在延迟作业到达 available_at 之前处理它们?