Python program to count the frequency of each word in the file

 

Problem Definition

Develop a Python program to accept the file name from the user and read the content of the file line by line. Count the number of times each word appears in the given file that is the frequency of character in the string.

Video Tutorial

Steps using “in” operator

First, accept the file name from the user. Use open statement to open the file, if the file is not present display file not found and exit.

If the file exists, then create an empty dictionary named counts to store the words as key and frequency of words as value.

Use for loop to read the contents of the file line by line.

Once the line is read then, split the line using the split() function. The split() function divides the line into words stores those words as the elements of the words list.

The returned values are stored in a list of named words. Loop over the words list, to get one word at a time.

Check whether the word is already present in the dictionary using in operator.

If the word is not present then add the word into the dictionary with the word as key and value set to 1.

If the word is already present in the dictionary then increase the value of the word by 1.

Repeat the procedure for all the lines in the file. Finally, print the dictionary with the word as keys and values as the frequency of the word.

Python program to count the frequency of each word in the file using “in” operator

#Read file name and count how many times each word appears

fname = input('Enter the file name: ')
try:
    fhand = open(fname)
    counts = dict()
    for line in fhand:
        words = line.split()
        for word in words:
            if word in counts:
                counts[word] += 1
            else:
                counts[word] = 1
    print(counts)
except:
    print('File cannot be opened:', fname)

Let test.txt is the file

VTU BGM
VTU BELAGAVI
VTU BGM karnataka

Output

Enter the file name: test.txt
{'VTU': 3, 'BGM': 2, 'BELAGAVI': 1, 'karnataka': 1}

Steps using “in” operator

First, accept the file name from the user. Use open statement to open the file, if the file is not present display file not found and exit.

If the file exists, then create an empty dictionary named counts to store the words as key and frequency of words as value.

Use for loop to read the contents of the file line by line.

Once the line is read then, split the line using the split() function. The split() function divides the line into words stores those words as the elements of the words list.

The returned values are stored in a list of named words. Loop over the word lists to get one word at a time.

Use get() the function to check the word is present in the dictionary, if present get function returns the value of the word, then add one to it.

If the word is not present then the get() function returns 0, that is the second parameter, adds 1 to it, and puts the word into the dictionary.

Repeat the procedure for all the lines in the file. Finally, print the dictionary with the word as keys and values as the frequency of the word.

Python program to count the frequency of each word in the file using “get()” function

#Read file name and count how many times each word appears

fname = input('Enter the file name: ')
try:
    fhand = open(fname)
    counts = dict()
    for line in fhand:
        words = line.split()
        for word in words:
            counts[word] = counts.get(word, 0) + 1
    print(counts)
except:
    print('File cannot be opened:', fname)

Let test.txt is the file

VTU BGM
VTU BELAGAVI
VTU BGM karnataka

Output

Enter the file name: test.txt
{'VTU': 3, 'BGM': 2, 'BELAGAVI': 1, 'karnataka': 1}

Summary:

This tutorial discusses, how to develop a Python program to count the frequency of each word in the file. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *