How do I get topic distribution of a document after LDA using gensim?

5,717 views
Skip to first unread message

ramanth

unread,
Nov 1, 2016, 5:12:09 AM11/1/16
to gensim
Code so far :
import gensim
import logging
logging.basicConfig(filename="logfile",format='%(message)s', level=logging.INFO)


def get_doc_topics(lda, bow):
    gamma, _ = lda.inference([bow])
    topic_dist = gamma[0] / sum(gamma[0])  # normalize distribution

documents = ['Human machine interface for lab abc computer applications',
             'A survey of user opinion of computer system response time',
             'The EPS user interface management system',
             'System and human system engineering testing of EPS',
             'Relation of user perceived response time to error measurement',
             'The generation of random binary unordered trees',
             'The intersection graph of paths in trees',
             'Graph minors IV Widths of trees and well quasi ordering',
             'Graph minors A survey']

texts = [[word for word in document.lower().split()] for document in documents]
dictionary = gensim.corpora.Dictionary(texts)
id2word = {}
for word in dictionary.token2id:    
    id2word[dictionary.token2id[word]] = word
mm = [dictionary.doc2bow(text) for text in texts]
lda = gensim.models.ldamodel.LdaModel(corpus=mm, id2word=id2word, num_topics=2, update_every=1, chunksize=10000, passes=1,minimum_probability=0.0)

Message has been deleted

Lev Konstantinovskiy

unread,
Nov 7, 2016, 3:11:03 AM11/7/16
to gensim

Hi Ramanth,

You can use get_document_topics as described in this tutorial.

Regards
Lev
Reply all
Reply to author
Forward
0 new messages