首页 > 解决方案 > Gensim 的相似度接口使用 TfIdf + LdaModel 对完全相同的文档给出低相似度分数

问题描述

我正在尝试使用 Gensim 的 LDA 模型实现文档相似性 API。为了试验性能,我尝试通过使用 TfIdf 向量而不是文档中描述的普通 BoW 语料库训练 LDA 模型来实现它。我面临的问题是,在使用 Gensim 的 Similarity API 创建索引并找出相似度分数时,我遇到的是,如果我尝试将同一个文档与其自身匹配,有时,相似度值不是~ 1. 我得到的值低至 ~0.06。这并不总是发生,但仅适用于某些文档。我用 229 个与每个文档匹配的文档再次对此进行了测试,我发现其中 45 个文档给出的结果小于 0.98,有时给出的值类似于 0.65、0.41 等。我想要一些帮助,

用于测试的最小代码:

docs = [ 'Document 1 as a string', 'Document 2 as a string', 'Document 3 as a string', 'and so on.....' ]
cleaned_docs = list(map(clean_function, docs))                 # Here, clean_function return tokens for each string. So, cleaned_docs is essentially a list of list of strings List[List[str]]
bow_corpus = [dictionary.doc2bow(i) for i in cleaned_docs]
tfidf_corpus = tfidf_model[bow_corpus]
lda_corpus = lda_model[tfidf_corpus]
index = Similarity(lda_corpus)
sims = index[lda_corpus]                   # Getting similarity for all combinations. Got a (229, 229) array for my case
final_sims = np.diag(sims)                 # Getting similarity with itself
print(final_sims)                          # Getting very low score with some docs

45 个文档中 3 个文档的 LDAModel 的输出向量我得到了低分以供参考:

[[(0, 0.17789464), (2, 0.03806097), (12, 0.2273234), (14, 0.08613937), (21, 0.13261063), (22, 0.17807047), (36, 0.058883864)],
[(1, 0.43381935), (2, 0.14317065), (3, 0.07986226), (36, 0.062136874)], 
[(0, 0.32848448), (2, 0.16667062), (14, 0.0485237), (15, 0.11480027), (18, 0.086506054), (35, 0.059970867)]]

标签: pythongensimsimilaritytf-idflda

解决方案


推荐阅读