Random Text Generation Using Word2Vec Model

  • Share this:

Code introduction


This function uses the Word2Vec model from the gensim library to generate random text. It first loads a pre-trained Word2Vec model, then randomly selects words from the vocabulary, ensuring they are not stop words, and constructs a random text.


Technology Stack : gensim, Word2Vec, KeyedVectors, numpy, random

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
def random_text_generator(num_words):
    from gensim.models import Word2Vec
    from gensim.models import KeyedVectors
    import numpy as np
    import random

    # Load pre-trained Word2Vec model
    model = KeyedVectors.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True)

    # Initialize a list to store random words
    random_words = []

    # Generate random words based on the Word2Vec model
    for _ in range(num_words):
        while True:
            # Randomly select a word from the vocabulary
            word = random.choice(list(model.wv.index_to_key))
            # Check if the word is in the model's vocabulary and not a stopword
            if word in model.wv and word not in model.wv.stop_words:
                random_words.append(word)
                break

    return ' '.join(random_words)