-
Notifications
You must be signed in to change notification settings - Fork 0
/
Analyse Text.py
271 lines (145 loc) · 5.73 KB
/
Analyse Text.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
from Alphabet import *
from operator import *
from WordFunctions import *
from FindWords import *
def AnalyseText(Text):
Frequencies = {}
for x in alphabetlist:
Frequencies[x] = 0
for letter in Text:
if letter in Frequencies:
Frequencies[letter] += 1
#else:
#Frequencies[letter] = 1
print ("\nFrequencies:\n")
for letter in alphabetlist:
print (letter, " ", Frequencies[letter])
SortedFrequencies = sorted(Frequencies.items(), key = lambda a: a[1])
#print ("\nThe Most Common Letter is: ", SortedFrequencies[0])
#print ("\nThe Most Common Letter is: ", SortedFrequencies[-1][0])
print ("\nIn Order, The Frequencies Are:\n")
for letter in reversed(SortedFrequencies):
#print (letter, " ", Frequencies[letter])
print (letter[0], " ", letter[1])
print ("\nThe Most Common Letter is: ", SortedFrequencies[-1][0])
return SortedFrequencies
def AnalyseWords(Text, LetterFrequencies):
Frequencies = {}
TextCopy = Text
Text = Text.split(" ")
for word in Text:
if word in Frequencies:
#Frequencies[letter] += 1
Frequencies[word] += 1
else:
Frequencies[word] = 1
SortedFrequencies = sorted(Frequencies.items(), key = lambda a: a[1])
print ("\nIn Order, The Frequencies Are:\n")
for word in reversed(SortedFrequencies):
#print (word[0], " ", word[1])
#print (word[0], "\t\t\t\t", word[1])
print (word[0], " "*(20-len(word[0])), word[1])
print ("\nThe Most Common Word is: ", SortedFrequencies[-1][0])
Key = {}
for x in alphabetlist:
Key[x] = ""
#Key["e"] = SortedFrequencies[-1][0]
#Key["e"] = LetterFrequencies[-1][0]
Key["e"] = SortedFrequencies[-1][0][-1]
for word in Text:
#if len(word) == 3 and word[-1] == SortedFrequencies[-1][0]:
#if len(word) == 3 and word[-1] == LetterFrequencies[-1][0]:
if len(word) == 3 and word[-1] == Key["e"]:
Key["t"] = word[0]
Key["h"] = word[1]
for word in Text:
if len(word) == 2 and word[0] == Key["t"]:
Key["o"] = word[1]
for word in Text:
if len(word) == 4 and word[0] == Key["t"] and word[1] == Key["h"] and word[3] == Key["t"]:
Key["a"] = word[2]
for word in Text:
if len(word) == 3 and word[0] == Key["a"]:
Key["n"] = word[1]
Key["d"] = word[2]
#if False:
if True:
#for trial in range(0, 100):
for trial in range(0, 26):
#print (trial, Key)
for word in Text:
newword = ""
for letter in word:
Found = False
#for character in Keys:
for character in Key:
#if Keys[character] == letter:
if Key[character] == letter:
Found = True
#newword = newword + Key[letter]
#newword = newword + Key[character]
newword = newword + character
if Found == False:
newword = newword + "_"
else:
#newword = newword + letter
pass
#print (newword)
try:
PotentialWords = FindPotentialWords(newword, Dictionary, "")
if len(PotentialWords) == 1:
for letter in range(0, len(PotentialWords[0])):
#print (letter, PotentialWords[0][letter], word)
#Keys[PotentialWords[0][letter]] = word[letter]
Key[PotentialWords[0][letter]] = word[letter]
except:
#print (newword, word, letter, PotentialWords)
pass
else:
NewPotentialWords = []
for word2 in PotentialWords:
for letter in range(0, len(word)):
if newword[letter] == "_" and Key[word2[letter]] == "":
NewPotentialWords.append(word2)
if len(NewPotentialWords) == 1:
for letter in range(0, len(NewPotentialWords[0])):
Key[NewPotentialWords[0][letter]] = word[letter]
BreakText()
print ("\n\nI Am Led To Believe:")
print ("")
for x in alphabetlist:
print (x, "->", Key[x])
NewMsg = ""
#for letter in Text:
for letter in TextCopy:
Found = False
for character in Key:
if Key[character] == letter:
NewMsg = NewMsg + character
Found = True
if not Found:
NewMsg = NewMsg + letter.upper()
print ("\n\nNew Message:\n")
print (NewMsg)
return
def BreakText():
print ("\n" + "-"*25 + "\n")
return
Text = RemovePunctuation(open(input("Enter File Name: "), "r").read().lower())
BreakText()
#print ("LETTERS:\n")
print ("\nLETTERS:\n")
LetterFrequencies = AnalyseText(Text)
#DictionaryName = input ("Enter Dictionary Name: ")
#DictionaryName = "Medium Dictionary.txt"
DictionaryName = "Large Dictionary.txt"
Dictionary = open(DictionaryName, "r").readlines()
for item in range(0, len(Dictionary)):
Dictionary[item] = RemovePunctuation(Dictionary[item].lower())
Dictionary = set(Dictionary)
BreakText()
print ("\n\nWORDS:\n")
#print ("\n\n\nWORDS:\n")
AnalyseWords(Text, LetterFrequencies)
#print ("\n\nPress ENTER To Exit...")
EXIT = input ("\n\nPress ENTER To Exit...")