使用python中的余弦相似度返回与查询文档最相似的文档

2024-04-19 00:22:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一组文件和一个查询我的医生目的是通过比较每个文档。收件人首先使用余弦相似性我必须将文档字符串映射到向量。还有我已经创建了一个tf idf函数来计算每个文档。在

为了得到字符串的索引,我有一个这样的函数

def getvectorKeywordIndex(self, documentList):
    """ create the keyword associated to the position of the elements within the    document vectors """
    #Mapped documents into a single word string
    vocabularyString = " ".join(documentList)
    vocabularylist= vocabularyString.split(' ')
    vocabularylist= list(set(vocabularylist))
    print 'vocabularylist',vocabularylist
    vectorIndex={}
    offset=0
    #Associate a position with the keywords which maps to the dimension on the vector used to represent this word
    for word in vocabularylist:
        vectorIndex[word]=offset
        offset+=1
  print vectorIndex
  return vectorIndex,vocabularylist  #(keyword:position),vocabularylist

对于余弦相似性,我的功能是:

^{pr2}$

TF-IDF是

def tfidf(self, term, key):

    return (self.tf(term,key) * self.idf(term))

我的问题是如何使用索引和词汇表列表以及该函数内部的tf idf来创建makevector。 任何答案都是欢迎的。在


Tags: theto函数字符串文档selftfposition
1条回答
网友
1楼 · 发布于 2024-04-19 00:22:17

您还应该将vectorIndex传递给makeVector,并使用它来查找文档和查询中的术语索引。忽略没有出现在vectorIndex中的术语。在

请注意,在处理文档时,您应该真正使用^{}矩阵而不是Numpy数组,否则您将很快耗尽内存。在

(或者,考虑使用scikit learn中的^{},它为您处理所有这些,使用scipy.sparse矩阵并计算tf idf值。免责声明:我写了部分课程。)

相关问题 更多 >