Articles

Program to check occurrence of each word within a paragraph in Python

Program to check occurrence of each word within a paragraph in Python


In the python programming article, we are going to learn

  • program to count the occurence of each word within a paragraph in python

Program to count the occurence of each word within a paragraph

The text file (paragraph.txt) is as follows:

 

Lorem Ipsum is simply dummied text of the printing and typesetting
industry. Lorem Ipsum has been the industry's standard dummy
text ever since the 1500s when an unknown printer took a galley
of type and scrambled it to make a type specimen book. It has
survived not only five centuries, but also the leap into electronic
typesetting, remaining essentially unchanged. It was popularised in
the 1960s with the release of Letraset sheets containing Lorem
Ipsum passages, and more recently with desktop publishing software
like Aldus PageMaker including versions of Lorem Ipsum.

The program is as follows:

 

# Owner : TutorialsInhand Author : Devjeet Roy

file = open('paragraph.txt','r')

word_dict = dict()

for i in file:
    words = i.split(" ")
    for j in words:
        if '\n' in j:
            j = j.replace('\n','')
        word_dict[j] = word_dict.get(j, 0) + 1

print("The word dictionary is:\n",word_dict)

The output is as follows:

 

PS C:\Users\DEVJEET\Desktop\tutorialsInHand> python code.py                                The word
The word dictionary is:
 {'Lorem': 4, 'Ipsum': 3, 'is': 1, 'simply': 1, 'dummied': 1, 'text': 2, 'o, "industry. Lorem Ipsumf': 4, 'the': 6, 'printing': 1, 'and': 3, 'typesetting': 1, 'industry.': 1,hen an unknown printer t 'has': 2, 'been': 1, "industry's": 1, 'standard': 1, 'dummy': 1, 'ever': 1 It has\n': 1, 'survived, 'since': 1, '1500s': 1, 'when': 1, 'an': 1, 'unknown': 1, 'printer': 1, 'ting, remaining essentiatook': 1, 'a': 2, 'galley': 1, 'type': 2, 'scrambled': 1, 'it': 1, 'to': 1,Letraset sheets containi 'make': 1, 'specimen': 1, 'book.': 1, 'It': 2, 'survived': 1, 'not': 1, 'osoftware\n': 1, 'like Alnly': 1, 'five': 1, 'centuries,': 1, 'but': 1, 'also': 1, 'leap': 1, 'into': 1, 'electronic': 1, 'typesetting,': 1, 'remaining': 1, 'essentially': 1, 
'unchanged.': 1, 'was': 1, 'popularised': 1, 'in': 1, '1960s': 1, 'with': 2, 'release': 1, 'Letraset': 1, 'sheets': 1, 'containing': 1, 'passages,': 1, 'more': 1, 'recently': 1, 'desktop': 1, 'publishing': 1, 'software': 1, 'like': 1, 'Aldus': 1, 'PageMaker': 1, 'including': 1, 'versions': 1, 'Ipsum.': 1}

Few important tips about the program

1. We use the open() function to open() a file in Python. The "r" argument signifies that the file has been opened in the reading mode.

2. We create a blank dictionary word_dict = {}

3. The i variable helps us to iterate the file, line by line.

4. We split the line that i variable points to, spacewise.

5. Now, we iterate over each word to count it. If the word is already present in the dictionary, we increment its count by 1, else we initialize it and set it to 1.

6. We check for new line character "\n" and remove them from the words.

 

program to check occurrence of each word within a paragraph in python

 


Basic Python Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Devjeet Roy
Full Stack Web Developer & Blockchain Enthusiast
Page Views :    Published Date : Oct 13,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!