-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrequency_histogram.py
91 lines (60 loc) · 1.96 KB
/
frequency_histogram.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
This program will check how many characters there are in a given text file
and will create a histogram from it (if the value is 0, then will be omitted)
"""
from os import strerror
# In this case, we will assume that the text file is in the same directory as the program
def take_txt_name():
out= False
while not out:
txt_name= input("Please, enter the name of the file you want to analyse: ")
try:
assert txt_name != str()
out= True
except:
print("Error: no valid input")
exit()
return txt_name
# This will try to read the file and put it in the memory
def read_file(txt_name):
file= ''
try:
lines_no= 0
txt_f= open(txt_name, 'rt')
line= txt_f.readline()
while line != '':
file += line
line= txt_f.readline()
lines_no += 1
except IOError as e:
print("Error in the I/O operation: ", strerror(e.errno))
exit(e.errno)
# close the stream
else:
txt_f.close()
print("Number of lines read=", lines_no)
return file
def create_results(file):
result= dict()
file= file.lower() # transform the file to be case-insensitive
for i in range(97, 123): # define and initialize the dictionary's keys
result.update({chr(i): 0})
for ch in file: # actualize the values for each key for this concrete text
if ch in result.keys():
result[ch] += 1
else:
continue
for el in result: # show the results if the value is greater than 0
if result[el] != 0:
print(el, result[el], sep=" -> ")
else:
continue
# start
try:
txt_name= take_txt_name()
file= read_file(txt_name)
create_results(file)
except KeyboardInterrupt:
print()
print("Thanks anyways!")
print()