Random Sentence Generation Using Word2Vec and LdaModel

  • Share this:

Code introduction


The function uses gensim's Dictionary, Word2Vec, and LdaModel to process text data and generate a sentence based on a random topic.


Technology Stack : gensim, Dictionary, Word2Vec, LdaModel

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
from gensim.models import Word2Vec
from gensim.models.ldamodel import LdaModel
from gensim.corpora import Dictionary
import random

def generate_random_model(texts, num_topics=2, num_words=10):
    # Create a dictionary representation of the documents.
    dictionary = Dictionary(texts)
    
    # Train a Word2Vec model.
    word2vec_model = Word2Vec(texts, vector_size=100, window=5, min_count=1, workers=4)
    
    # Train an LdaModel from the dictionary and corpus.
    corpus = [dictionary.doc2bow(text) for text in texts]
    lda_model = LdaModel(corpus, num_topics=num_topics, id2word=dictionary, passes=15)
    
    # Get random topics and their associated words.
    random_topic = random.choice(lda_model.print_topics())
    random_words = [word[0] for word in random_topic[1].split('+') if word.strip() != '']
    
    # Generate a sentence using the random words and Word2Vec model.
    random_sentence = ' '.join(random.sample(random_words, num_words))
    
    return random_sentence

# Example usage
texts = ["This is a sentence", "This is another sentence", "And this is a third sentence"]
result = generate_random_model(texts)
print(result)