0% found this document useful (0 votes)
6 views2 pages

Mostcommonwords 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Mostcommonwords 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Mostcommonwords

Exercise 4.1. Find the most common words by applying the DSU pattern;most_common
takes a histogram and returns a list of word-frequency
tuples,sortedinreverseorderbyfrequency.
Exercise4.2. Prints theten most common words.

import re

from collections import Counter

def most_common_words(text, num_words):

"""

Returns the most common words in a given text.

Args:

text (str): The input text.

num_words (int): The number of most common words to return.

Returns:

list: A list of tuples containing the word and its frequency.

"""

# Convert text to lowercase and remove punctuation

text = text.lower()

text = re.sub(r'[^\w\s]', '', text)

# Split text into words

words = text.split()

# Count word frequencies


word_freq = Counter(words)

# Get the most common words

most_common = word_freq.most_common(num_words)

return most_common

# Example usage:

text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat."

num_words = 5

most_common = most_common_words(text, num_words)

print(most_common)

O/P

[('ut', 3), ('lorem', 1), ('ipsum', 1), ('dolor', 1), ('sit', 1)]

You might also like