已知重复项对的余弦相似度

Cosine similarity for already known pairs of duplicates

我有一个保存在 csv 文件中的重复文档对列表。第 1 列中的每个 ID 都是第 2 列中相应 ID 的副本。 该文件是这样的:

Document_ID1    Document_ID2
12345           87565
34546           45633
56453           78645
35667           67856
13636           67845

每个文档 ID 都与保存在其他地方的文本相关联。我提取了这段文本并将每列 ID 和相关文本保存到两个 lsm 数据库中。
所以我有 db1,它有来自 Document_ID1 的所有 ID 作为 keys 和它们相应的文本作为 values各自的键。因此,就像一本字典。同样,db2 来自 Document_ID2.
的所有 ID 所以,当我说 db1[12345] 时,我得到了与 ID 12345 关联的文本。

现在,我想获得每一对之间的余弦相似度分数以确定它们的重复性。直到现在我 运行 一个 tfidf 模型也做同样的事情。我用 db1 中的所有文档创建了一个 tfidf 矩阵作为语料库,并测量了来自 db2 的每个 tfidf 向量与 tfidf 矩阵的余弦相似度。出于安全原因,我无法提供完整的代码。代码是这样的:

# Generator function to pick one key (document) at a time for comparison against other documents
def generator(db):
    for key in db.keys():
        text = db[key]
        yield text

# Use spaCy to create a function to preprocess text from the generator function
nlp = spacy.load('en')
def spacy(generator_object):
    for doc in generator_object:
        words = <code to make words lower case, remove stop words, spaces and punctuations>
        yield u' '.join(words)

# TF-IDF Vectorizer
tfidf = TfidfVectorizer(min_df = 2)

# Applying tf-idf transformer to each key from db1 individually in the generator function.
tfidf_matrix = tfidf.fit_transform(spacy(generator(db1)))

# Function to calculate cosine similarity values between the tfidf matrix and the tfidf vector of a new key
def similarity(tfidf_vector, tfidf_matrix, keys):    
    sim_vec = <code to get cosine similarity>
    return sim_vec.sort_values(ascending=False)

# Applying tf-idf transformer on db2 keys on a loop and getting cosine similarity scores for each key from db2.
for key in db2.keys():
    # Create a new temporary db for each key from db2 to enter into generator function
    new = <code to create a temporary new lsm database>
    text = db2[key]
    new[key] = text
    new_key = <code to get next key from the temporary new lsm database>
    tfidf_vector = tfidf.transform(spacy_proc(corpus_gen(new)))
    similarity_values = similarity(tfidf_vector, tfidf_matrix, list(db1.keys()))
    for idx, i in similarity_values.iteritems(): 
            print new_key, idx, i
    del new[key]

但这为我提供了针对 db2 中每个键的 db1 中所有键的余弦相似度分数。示例:如果 db1 中有 5 个键,db2 中有 5 个键,我会得到 25 行作为此代码的结果。
我想要的是从 db1 中为 db2 中的键获取相应键的余弦相似度分数。这意味着如果 db1 和 db2 中各有 5 个键,结果我应该只有 5 行 - 只有每对重复项的余弦相似度得分。

我应该如何调整我的代码得到那个?

因为还没有确定的答案,我得到了包含所有行的数据框(上例中的 25 行结果)和 inner-joining/merging 它包含一个包含重复项列表的数据框对(即我需要的 5 行输出)。这样,生成的数据框就具有重复文档对的相似度分数。 这是一个临时解决方案。如果有人能想出一个更清洁的解决方案,我会接受它作为答案,如果它有效的话。