You are on page 1of 2

11.

THE MOST FREQUENT WORDS IN A TEXT READ FROM A FILE

AIM:

To write a python program to find the most frequent words from a file.

ALGORITHM :

Step 1: Create a file.

Step 2: Open the created file in read mode.

Step 3: Using for loop find the most frequent words.

Step 4: Assume the key for each of the words.

Step 5: Print the frequent words that are used in the file.

Step 6: Close the file and print the output

PROGRAM:

fr = open("c:\\cse.txt","r")

wordcount = {}

for word in fr.read().split():

if word not in wordcount:

wordcount[word] = 1

else:

wordcount[word] += 1

for k,v in wordcount.items():

print(k, v)

fr.close()
OUTPUT:

hai 1

hello 1

ram1

RESULT:

Thus the program to find the most frequent words in a text is executed and the output is

obtained.

You might also like