Skip to content

Instantly share code, notes, and snippets.

@khuangaf
Created February 2, 2020 07:28
Show Gist options
  • Save khuangaf/0827377670bad433ee31ebfd95b1b2ef to your computer and use it in GitHub Desktop.
Save khuangaf/0827377670bad433ee31ebfd95b1b2ef to your computer and use it in GitHub Desktop.
from gensim.test.utils import common_texts
from gensim.corpora.dictionary import Dictionary
from gensim.models import LdaModel
# Create a corpus from a list of texts
common_dictionary = Dictionary(common_texts)
common_corpus = [common_dictionary.doc2bow(text) for text in common_texts]
# Train the model on the corpus.
lda = LdaModel(common_corpus, num_topics=10)
# infer the topic distribution of the second corpus.
lda[common_corpus[1]]
'''
output:
[(0, 0.014287902),
(1, 0.014287437),
(2, 0.014287902),
(3, 0.014285716),
(4, 0.014285716),
(5, 0.014285714),
(6, 0.014285716),
(7, 0.014285716),
(8, 0.014289378),
(9, 0.87141883)]
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment