计算最小余弦相似度

2024-04-20 02:11:49 发布

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

我试图在语料库中检索具有最大和最小余弦相似度分数的文档对。 这是我的最大相似度代码:

def get_high_sim(self):
    """ Returns the maximum similarity score """
    flag = -1
    maximum = (flag, 0, 0)
    for d1 in self.sim_matrix.keys():
        for d2 in self.sim_matrix[d1].keys():
            if d1 != d2: #ignoring similarity with itself
                if self.active_clusters[d1] != -1 and self.active_clusters[d2] != -1: #checking if cluster is active
                    score = self.sim_matrix[d1][d2]
                    if score > flag and score != 1:
                        flag = score
                        maximum = (flag, d1, d2)
    return maximum

这似乎工作得很好。但我的最小相似度代码不起作用。你知道吗

    def get_lowest_sim(self):
    """ Returns the minimum similarity score in the corpus"""
    flag = -1
    minimum = (flag, 0, 0)
    for d1 in self.sim_matrix.keys():
        for d2 in self.sim_matrix[d1].keys():
            if d1 != d2: #ignoring similarity with itself
                if self.active_clusters[d1] != -1 and self.active_clusters[d2] != -1: #checking if cluster is active
                    score = self.sim_matrix[d1][d2]
                    if score < flag:
                        flag = score
                        minimum = (flag, d1, d2)
    return minimum

有人能帮我找出错误吗?提前谢谢!你知道吗


Tags: inselfforifsimkeysmatrixactive