gpt4 book ai didi

python - 在 python 中使用 Tf-Idf 的搜索引擎

转载 作者:太空宇宙 更新时间:2023-11-04 06:39:58 25 4
gpt4 key购买 nike

这是我的代码

 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]documents(sentence)

当我搜索第一个文档时,它应该给我 [1]documents(sentence)

当我搜索 second 时,它应该给我 [2]documents(sentence)

我想用 TfIdf 做这个(我不能做正常的搜索)

我该怎么做?

最佳答案

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

关于python - 在 python 中使用 Tf-Idf 的搜索引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59285107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com