Optional parameters
from collections import Counter
def print_most_common_words(histogram, num_words=10):
# Use Counter for efficient word frequency counting and sorting
word_counts = Counter(histogram).most_common(num_words)
# Print the most common words and their counts
for word, count in word_counts:
print(f"{word}: {count}")
# Example usage
text = "This is a sample text to find the most common words. It has some repeated words for
emphasis."
words = [Link]().split() # Lowercase words and split into a list
# Create a dictionary (histogram) to count word occurrences
word_counts = Counter(words)
# Print the top 10 most common words (adjust num_words as needed)
print_most_common_words(word_counts)
Explanation:
1. print_most_common_words function:
o Takes a histogram dictionary and an optional num_words argument (default
10).
o Uses Counter(histogram) to efficiently create a Counter object representing
word counts.
o Uses Counter.most_common(num_words) to get a list of tuples containing the
(word, count) for the most frequent words up to the specified num_words.
o Iterates through the list and prints each word and its count in a clear format.
2. Example Usage:
o Defines a sample text and splits it into lowercase words.
o Creates a Counter object directly from the words list for efficient word
counting.
o Calls print_most_common_words with the word_counts dictionary (adjust
num_words as needed).
Improvements:
Clarity: Clear function documentation and comments improve understanding.
Efficiency: Using Counter provides built-in counting and sorting functionality.
Flexibility: The function allows specifying the number of most common words to
print.
Readability: The code uses f-strings for clear output formatting.