-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount.py
29 lines (24 loc) · 978 Bytes
/
count.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import operator
import re
words = {}
regex = re.compile("[^a-z]")
# read all words from the example.txt text file and count how often they occur
with open("example.txt", "r") as f:
for line in f:
for word in line.split():
# convert word to lowercase as we don't care about case
word = word.lower()
# remove any superfluous comma's and full stops etc.
word = regex.sub("", word)
# add one tot he count if we've seen the word before, else set count to 1
if word in words:
words[word] += 1
else:
words[word] = 1
# sort the words by their frequency in reverse
sorted_words = sorted(words.items(), key=operator.itemgetter(1), reverse=True)
print("total distinct words: " + str(len(sorted_words)))
print("top 100 words:")
# print the 100 most frequently occurring words
for word in sorted_words[:100]:
print(">> " + word[0] + ": " + str(word[1]))