Q:

Write a Python program to count the occurrences of each word in a given sentence

0

Write a Python program to count the occurrences of each word in a given sentence

In this exercise, you will learn how to count the occurrences of a particular word or each word in a text file. Generally, it is required in the development process to count the occurrence of each word or a particular word in a text document or a given sentence. Here, we have mentioned different ways to achieve this.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Count occurrences of each word in a given sentence using dictionary

In the given example, we first read the sentence from the user using a built-in function input() and store it in a variable, and then, we utilize a dictionary object that stores the word as the key and its count as the corresponding value. We loop through each word and count the occurrences of each word, adding them to the dictionary with count 1.

def word_count(str):
    # Create an empty dictionary
    counts = dict()
    words = str.split()

    # Loop through each line of the file
    for word in words:
        if word in counts:
            counts[word] += 1
        else:
            counts[word] = 1
    return counts

# get input from user
string = input("Please enter sentence: ")

# Print the number of occurrences of word
print( word_count(string))

Output of the above code - 

Pleae enter sentence: Python is a high level open source programming language. Python is an extremely powerful language.
{'Python': 2, 'is': 2, 'a': 1, 'high': 1, 'level': 1, 'open': 1, 'source': 1, 'programming': 1, 'language.': 2, 'an': 1, 'extremely': 1, 'powerful': 1}

Count occurrences of a word in a given file using count() function

In the above examples, we got the occurrence of each word, but what if we need to get the occurrence of a particular word in a file? Here is the simplest solution to this. We can use the count() function to count the occurrences of a particular word in a given file.

Suppose we have a text file 'demo.txt' that contains the following text-

Python is a high level open source programming language. Python is an extremely powerful language.
#get file object reference to the file
file = open("C:\workspace\python\data.txt", "r")

# read content of file to string
data = file.read()

# get number of occurrences of the substring in the string
occurrences = data.count("python")

print('Number of occurrences of the word :', occurrences)

 Output of the above code - 

Number of occurrences of the word : 2

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Exercises, Practice Questions and Solutions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a program to calculate simple interest in Py... >>
<< Write a program in Python to filter the given list...