Finding the Most Frequent Word in Text

  • Share this:

Code introduction


This function takes a text as input and returns the most frequent word in the text. It first tokenizes the text into words, then uses SnowballStemmer for stemming, counts the frequency of each word, and finally returns the most frequent word.


Technology Stack : Lingua

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
from lingua.en import word_frequency
from lingua.stem import SnowballStemmer

def most_frequent_word(text):
    """
    Returns the most frequent word in the given text.
    """
    # Tokenize the text into words
    words = text.split()
    
    # Use SnowballStemmer to stem the words
    stemmer = SnowballStemmer('english')
    stemmed_words = [stemmer.stem(word) for word in words]
    
    # Count the frequency of each word
    word_freq = word_frequency(stemmed_words)
    
    # Find the most frequent word
    most_frequent = max(word_freq, key=word_freq.get)
    
    return most_frequent                
              
Tags: