LAB-5
Develop a program to print 10 most frequently appearing words in a
text file. [Hint: Use dictionary with distinct words and their frequency of
occurrences. Sort the dictionary in the reverse order of frequency and display
dictionary slice of first 10 items]
file = open("sample.txt", "r") # Mention location of the file
text = file.read()
print(text)
words = text.split()
frequency = {}
for word in words:
if word not in frequency:
frequency[word] = 1
else:
frequency[word] += 1
# Sort the frequency dictionary by values in descending order
most_frequency = dict(sorted(frequency.items(), key=lambda elem: elem[1],
reverse=True))
# Get the first 10 most frequent words
out = dict(list(most_frequency.items())[:10])
print("1st 10 most frequent words are: " + str(out))
Algorithm:
1. Start
2. Open the file named "sample.txt" in read mode.
3. Read the entire content of the file into a variable called text.
4. Print the content of the file to the output (optional for debugging/visualization).
5. Split the text into individual words using split() method and store them in a list called
words.
6. Initialize an empty dictionary called frequency to store word counts.
7. Iterate over each word in the words list:
• If the word is not already in the frequency dictionary:
• Add the word with a count of 1.
• Else:
• Increment the existing count of that word by 1.
8. Sort the dictionary items by value (i.e., word frequency) in descending order.
9. Select the first 10 items from the sorted dictionary and store them in out.
10.Print the top 10 most frequent words along with their counts.
11.End
Output:
Python is a popular general-purpose programming language. It is used in machine learning, web
development, desktop applications, and many other fields. Fortunately for beginners, Python has a
simple, easy-to-use syntax. This makes Python a great language to learn for beginners.
1st 10 most frequent words are{'Python': 3, 'a': 3, 'is': 2, 'for': 2, 'popular': 1, 'general purpose': 1,
'programming': 1, 'language.': 1, 'It': 1, 'used': 1}