-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.py
More file actions
42 lines (35 loc) · 1.28 KB
/
counter.py
File metadata and controls
42 lines (35 loc) · 1.28 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
from collections import Counter
import os, sys
words=[]
c=Counter()
d=Counter()
MAX_ELEMENTS=5
# read file into list
# Source: https://github.com/charlesreid1/five-letter-words/blob/master/sgb-words.txt
filename = "source.txt"
with open(os.path.join(sys.path[0], filename)) as file:
for line in file:
words.append(line.strip())
print("########### WORDS COUNTED ################")
wordcount=len(words)
lettercount = wordcount * 5
print ("Word count: " + str(wordcount))
print ("Letter count: " + str(lettercount))
# most common letters - read the word as a set, and add to the counter (nifty python feature)
for word in words:
c.update(set(word))
print ("########### MOST COMMON LETTERS ###########")
commonletters=c.most_common(MAX_ELEMENTS)
for k in commonletters:
# convert to percent and print
print(k[0] + " {:.2f}".format(float(100*k[1])/lettercount) + "%")
# most common letters per position x
for i in range(5):
for word in words:
d.update(set(word[i]))
print ("########### Most popular in position: " + str(i+1) + " ###########")
commonpositionletters=d.most_common(MAX_ELEMENTS)
for k in commonpositionletters:
# convert to percent and print
print(k[0] + " {:.2f}".format(float(100*k[1])/wordcount) + "%")
d.clear()