Count the number of words using Python

This article represents a way to count the number of words, the number of unique words, and the number of each word occurrences in a text file.

  1. Read the text file.
    text = open("filename.txt").read()
  2. Replace non-alphanumeric characters as a whitespace.
    import re
    text = re.sub('[^\w&^\d]', ' ', text)
  3. Change all characters to lowercase.
    text = text.lower()
  4. Split words into a list.
    text = text.split()
  5. Display the number for words in the text file.
    len(text)
  6. Display the number of unique words in the text file.
    len(set(text))
  7. Display the number of occurrences for each word.
  8. from collections import defaultdict
    
    wordsCount = defaultdict(int)
    for word in text:
      wordsCount[word] += 1
    
    for word, num in wordsCount.items():
    	print(word, num)
If you are new here, you might want to subscribe to the RSS feed or newsletter.

Enter your email address:

Related

This entry was posted in Python and tagged , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>