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
92
93
94
95
96
97
98
99
100
101
102
103
|
import matplotlib.pyplot as plt # pip install matplotlib
from string import ascii_uppercase
# English Letter Count (based on a sample of 40, 000 words)
# Source: http://pi.math.cornell.edu/~mec/2003-2004/cryptography/subs/frequencies.html
english_character_count = {"E": 21912, "T": 16587, "A": 14810, "O": 14003, "I": 13318, "N": 12666,
"S": 11450, "R": 10977, "H": 10795, "D": 7874, "L": 7253, "U": 5246, "C": 4943, "M": 4761,
"F": 4200, "Y": 3853, "W": 3819, "G": 3693, "P": 3316, "B": 2715, "V": 2019, "K": 1257,
"X": 315, "Q": 205, "J": 188, "Z": 128}
script_character_count = {}
for character in ascii_uppercase:
script_character_count[character] = 0
# script_character_count will now be {"A": 0, "B": 0, ... "Z": 0}
with open("script.txt") as script: # opening mode defaults to "r" (read) if not specified
while True:
line = script.readline()
if not line:
break
for character in line:
if character.upper() in script_character_count:
script_character_count[character.upper()] += 1
# print(script_character_count)
"""
# TWO PLOTS ----------------------------------------------------------------------------------------------
# Sort both data sets alphabetically
# .items() gives us pairs in a list (ex. [("A", 10), ("B", 20), ...])
# sorted() will use the first element of the tuple as the key for sorting
script_data = sorted(script_character_count.items())
english_data = sorted(english_character_count.items())
# Split the X values ("A", "B", ...) and Y values (10, 20, ...)
script_x = []
script_y = []
for letter, count in script_data:
script_x.append(letter)
script_y.append(count)
# Do the same for our other data set
english_x = []
english_y = []
for letter, count in english_data:
english_x.append(letter)
english_y.append(count)
# Create two plots, ** axes is a list of plots, NOT to be confused with x/y axis **
fig, axes = plt.subplots(2)
# Create a bar graph oon plot 0
axes[0].bar(script_x, script_y)
axes[0].set_title("Bee Movie Script Letter Count")
# Same thing for plot 1
axes[1].bar(english_x, english_y)
axes[1].set_title("English Letter Count (based on a sample of 40, 000 words)")
plt.show()
"""
# DOUBLE BAR GRAPH ---------------------------------------------------------------------------------------
script_data = sorted(script_character_count.items())
english_data = sorted(english_character_count.items())
x = [] # X data
y1 = [] # Bar heights for the bee movie
y2 = [] # Bar heights for english
# Split the data into separate lists like last time...
for letter, count in script_data:
x.append(letter)
y1.append(count)
for letter, count in english_data:
y2.append(count)
sum_y1 = sum(y1)
y1 = [item/sum_y1 for item in y1]
sum_y2 = sum(y2)
y2 = [item/sum_y2 for item in y2]
fig, axes = plt.subplots()
axes.bar([i - 0.2 for i in range(len(x))],y1, 0.4, color="#dba500", label="Bee Movie")
axes.bar([i + 0.2 for i in range(len(x))],y2, 0.4, color="#1c1c1c", label="English")
plt.xlabel("Letter")
plt.ylabel("Frequency")
plt.title("Bee Movie vs English Letter Frequency")
plt.legend()
plt.xticks(range(len(x)), x)
plt.show()
|