-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex09.py
41 lines (34 loc) · 1.19 KB
/
ex09.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
30
31
32
33
34
35
36
37
38
39
40
41
# playing with colors in my terminal
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
# I changed his confusing variable acronyms
filename = input('Enter File: ')
if len(filename) < 1 : filename = 'words.txt'
fullfile = open(filename)
dictionary = dict()
for line in fullfile :
line = line.rstrip()
words = line.split()
for w in words:
dictionary[w] = dictionary.get(w,0) + 1
print(bcolors.WARNING + '\n📖 dictionary of ' + bcolors.OKCYAN + filename.upper() + bcolors.ENDC)
print(dictionary,'\n')
# find most common word
numtimes = -1
mostword = ""
print(bcolors.WARNING + '\n💬 word count of ' + bcolors.OKCYAN + filename.upper() + bcolors.ENDC)
for k,v in dictionary.items() :
print(k,v)
if v > numtimes :
numtimes = v
mostword = k
print(bcolors.OKGREEN + '\nDONE ✅' + bcolors.WARNING)
print('most frequent word in ' + bcolors.OKCYAN + filename.upper() + bcolors.WARNING + ' is ', bcolors.HEADER + str(mostword), bcolors.WARNING + ' which appears ', bcolors.HEADER + str(numtimes), bcolors.WARNING + ' times.')