Word2Vec-Based Word Frequency Calculation

  • Share this:

Code introduction


This function uses the Word2Vec model from the gensim library to calculate the frequency of each word in a word list. The frequency is calculated by the vector representation of the word in the Word2Vec model.


Technology Stack : gensim, Word2Vec, STOPWORDS

Code Type : Function

Code Difficulty : Intermediate


                
                    
def random_word_frequency(word_list):
    from gensim.models import Word2Vec
    from gensim.parsing.preprocessing import STOPWORDS

    # Initialize Word2Vec model
    model = Word2Vec(word_list, vector_size=100, window=5, min_count=5, workers=4)

    # Remove stopwords from the model
    model.build_vocab([STOPWORDS])

    # Get frequency of each word in the list
    word_freq = {word: model.wv[word] for word in word_list if word in model.wv}

    return word_freq