Write a Python Program to Count The Occurrences of Each Word in a Given Sentence

Hi, in this article you will How to count the number of occurrences of each word in a given sentence.

Let's try to solve this using a Python program.

 

Write a Python Program to Count The Occurrences of Each Word in a Given Sentence

 

 


Write a Python Program to Count The Occurrences of Each Word in a Given Sentence

Take a variable named as  sentence,  ask the user to interest using the input function call.  then convert the sentence to a list of words using the  built in functions split().

I will be using dictionary data structure to store the words present in a sentence  as keys and count of occurrences of words as value,  so let's find the key  value pair and print the occurrences of each word in given sentence

sentence = input("Enter a Sentence : ")
words = sentence.split()





Take a new variable named as frequency and initialize it to an Empty dictionary.   Iterate all the words present in the list of words (from the given sentence).  check if a word is present in a dictionary then implement the value ( occurrence count by one)  using the else block if the word is new to the dictionary then initialize the count to 1.

freq = dict()

for word in words:
   if word in freq:
       freq[word] += 1
   else:
       freq[word] = 1





Once the for loop is completed you will have a dictionary containing key  value pairs,  where key is a word and value is a count of occurrences of that word. Iterate the key value pair of dictionary using the for loop and print the key value pairs

print("Count of Occurrences of Each Word in Given Sentence is ...")
for key, value in freq.items():
   print(f"{key} Occurs : {value} Times")





Python Program
====================

sentence = input("Enter a Sentence : ")
words = sentence.split()
freq = dict()

for word in words:
   if word in freq:
       freq[word] += 1
   else:
       freq[word] = 1

print("Count of Occurrences of Each Word in Given Sentence is ...")
for key, value in freq.items():
   print(f"{key} Occurs : {value} Times")






Output
=============
Enter a Sentence : Write a Python Program to Count The Occurrences of Each Word in a Given Sentence
Count of Occurrences of Each Word in Given Sentence is ...
Write Occurs : 1 Times
a Occurs : 2 Times
Python Occurs : 1 Times
Program Occurs : 1 Times
to Occurs : 1 Times
Count Occurs : 1 Times
The Occurs : 1 Times
Occurrences Occurs : 1 Times
of Occurs : 1 Times
Each Occurs : 1 Times
Word Occurs : 1 Times
in Occurs : 1 Times
Given Occurs : 1 Times
Sentence Occurs : 1 Times




Output
==============
Enter a Sentence : hi, how Are you ? Are you busy ?
Count of Occurrences of Each Word in Given Sentence is ...
hi, Occurs : 1 Times
how Occurs : 1 Times
Are Occurs : 2 Times
you Occurs : 2 Times
? Occurs : 2 Times
busy Occurs : 1 Times




Conclusion
===================
This is a simple method to count the number of words present in the sentence using the Python dictionary. Execute the above Python program by providing sample sentences as input to it.

Comment it down below if you have any suggestions to improve the above Python program.






Post a Comment

0 Comments