Python Gensim:如何使用LDA模型计算文档相似度? [英] Python Gensim: how to calculate document similarity using the LDA model?

查看:834
本文介绍了Python Gensim:如何使用LDA模型计算文档相似度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个训练有素的LDA模型,我想从训练了我的模型的语料库中计算出两个文档之间的相似性得分. 在研究完所有Gensim教程和功能之后,我仍然无法解决.有人可以给我提示吗?谢谢!

I've got a trained LDA model and I want to calculate the similarity score between two documents from the corpus I trained my model on. After studying all the Gensim tutorials and functions, I still can't get my head around it. Can somebody give me a hint? Thanks!

推荐答案

不知道这是否有帮助,但是当使用实际文档作为查询时,我设法在文档匹配和相似性方面获得了成功的结果.

Don't know if this'll help but, I managed to attain successful results on document matching and similarities when using the actual document as a query.

dictionary = corpora.Dictionary.load('dictionary.dict')
corpus = corpora.MmCorpus("corpus.mm")
lda = models.LdaModel.load("model.lda") #result from running online lda (training)

index = similarities.MatrixSimilarity(lda[corpus])
index.save("simIndex.index")

docname = "docs/the_doc.txt"
doc = open(docname, 'r').read()
vec_bow = dictionary.doc2bow(doc.lower().split())
vec_lda = lda[vec_bow]

sims = index[vec_lda]
sims = sorted(enumerate(sims), key=lambda item: -item[1])
print sims

您在语料库中驻留的所有文档与用作查询的文档之间的相似性得分将是每个sim卡中sim卡的第二个索引.

Your similarity score between all documents residing in the corpus and the document that was used as a query will be the second index of every sim for sims.

这篇关于Python Gensim:如何使用LDA模型计算文档相似度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆