You can download this code by clicking the button below.
This code is now available for download.
This function generates a random LDA topic model using the gensim library. It accepts a list of documents, the number of topics, and the number of passes as parameters.
Technology Stack : gensim, numpy, random
Code Type : Python Function
Code Difficulty : Intermediate
import gensim
import numpy as np
import random
def generate_random_topic_model(document_list, num_topics, passes):
"""
Generates a random LDA topic model using gensim library.
"""
# Create a dictionary representation of the documents.
dictionary = gensim.corpora.Dictionary(document_list)
# Convert dictionary to a Bag of Words (BoW) corpus.
corpus = [dictionary.doc2bow(doc) for doc in document_list]
# Generate random LDA model.
random.seed(42) # For reproducibility
lda_model = gensim.models.ldamodel.LdaModel(
corpus=corpus,
id2word=dictionary,
num_topics=num_topics,
random_state=42,
passes=passes,
alpha='auto',
per_word_topics=True
)
# Print the LDA model.
print(lda_model.print_topics())
# Example usage:
# document_list = [['python', 'data', 'science'], ['gensim', 'lda', 'topic'], ['model', 'corpus', 'dictionary']]
# generate_random_topic_model(document_list, num_topics=2, passes=10)