Word Frequency Analysis and Most Common Word Identification

  • Share this:

Code introduction


Analyzes the frequency of words in a text and returns the most frequent word along with its frequency.


Technology Stack : NLTK

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
from nltk import FreqDist
from nltk.tokenize import word_tokenize

def analyze_word_frequency(text):
    # Tokenize the text into words
    words = word_tokenize(text)
    # Create a frequency distribution of the words
    freq_dist = FreqDist(words)
    # Return the most common word and its frequency
    most_common_word, frequency = freq_dist.most_common(1)[0]
    return most_common_word, frequency

# JSON representation of the code                
              
Tags: