You can download this code by clicking the button below.
This code is now available for download.
This function uses the Polyglot library to calculate the frequency of words in a given text and returns a dictionary containing words and their frequencies. It first downloads the necessary models for the specified language, then creates a Polyglot Text object, and finally retrieves the word frequencies from it.
Technology Stack : Polyglot, Text, downloader, embeddings2.en, ner2.en
Code Type : Function
Code Difficulty : Intermediate
def random_word_frequency(text, language='en'):
from polyglot.text import Text
from polyglot.downloader import downloader
# Download necessary models for the specified language
downloader.download('embeddings2.en')
downloader.download('ner2.en')
# Create a Polyglot Text object
polyglot_text = Text(text, hint_language_code=language)
# Get the word frequencies
word_freqs = polyglot_text.words.by_freq()
# Return the frequencies as a dictionary
return dict(word_freqs)