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.
- Read the text file.
text = open("filename.txt").read() - Replace non-alphanumeric characters as a whitespace.
import re text = re.sub('[^\w&^\d]', ' ', text) - Change all characters to lowercase.
text = text.lower() - Split words into a list.
text = text.split() - Display the number for words in the text file.
len(text) - Display the number of unique words in the text file.
len(set(text)) - Display the number of occurrences for each word.
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.
Creates the exact copy of your hard disk and allows you to instantly restore the entire machine.
New Acronis True Image Home 2010 is the most reliable and easy in use backup solution. Now with online backup option!
15% Discount Code: FMAATIH2010
New Acronis True Image Home 2010 is the most reliable and easy in use backup solution. Now with online backup option!
15% Discount Code: FMAATIH2010







