3
3
import pickle
4
4
import os
5
5
6
+
6
7
def load_word_dictionary (file_path ):
7
8
with open (file_path , 'r' ) as file :
8
9
return set (word .strip ().lower () for word in file .readlines ())
9
10
11
+
10
12
def get_letter_frequencies (text ):
11
13
frequencies = {}
12
14
for char in text :
@@ -15,27 +17,33 @@ def get_letter_frequencies(text):
15
17
frequencies [char_lower ] = frequencies .get (char_lower , 0 ) + 1
16
18
return frequencies
17
19
20
+
18
21
def decrypt_cryptogram (cryptogram , frequency_map , word_dict ):
19
22
alphabet = string .ascii_lowercase
20
- sorted_freq = sorted (frequency_map .items (), key = lambda x : x [1 ], reverse = True )
23
+ sorted_freq = sorted (frequency_map .items (),
24
+ key = lambda x : x [1 ], reverse = True )
21
25
freq_letters = '' .join (letter for letter , _ in sorted_freq )
22
26
23
27
def decrypt_letter (letter ):
24
28
if letter .isalpha ():
25
29
index = freq_letters .find (letter .lower ())
26
- decrypted_letter = alphabet [index ] if letter .islower () else alphabet [index ].upper ()
30
+ decrypted_letter = alphabet [index ] if letter .islower (
31
+ ) else alphabet [index ].upper ()
27
32
return decrypted_letter
28
33
return letter
29
34
30
35
decrypted_text = '' .join (decrypt_letter (char ) for char in cryptogram )
31
36
words = decrypted_text .split ()
32
- decrypted_words = [word if word .lower () not in word_dict else word_dict [word .lower ()] for word in words ]
37
+ decrypted_words = [word if word .lower (
38
+ ) not in word_dict else word_dict [word .lower ()] for word in words ]
33
39
return ' ' .join (decrypted_words )
34
40
41
+
35
42
def guess_word_length (cryptogram ):
36
43
spaces = cryptogram .count (' ' )
37
44
return len (cryptogram ) // (spaces + 1 )
38
45
46
+
39
47
def manual_decryption (decrypted_text , cryptogram ):
40
48
print ("\n Manual Decryption:" )
41
49
while True :
@@ -47,34 +55,40 @@ def manual_decryption(decrypted_text, cryptogram):
47
55
decrypted_text = decrypted_text .replace (guess , replacement )
48
56
return decrypted_text
49
57
58
+
50
59
def visualize_frequency (frequency_map ):
51
- sorted_freq = sorted (frequency_map .items (), key = lambda x : x [1 ], reverse = True )
60
+ sorted_freq = sorted (frequency_map .items (),
61
+ key = lambda x : x [1 ], reverse = True )
52
62
letters , frequencies = zip (* sorted_freq )
53
63
plt .bar (letters , frequencies )
54
64
plt .xlabel ('Letters' )
55
65
plt .ylabel ('Frequency' )
56
66
plt .title ('Letter Frequency in the Cryptogram' )
57
67
plt .show ()
58
68
69
+
59
70
def save_progress (decrypted_text ):
60
71
with open ('progress.pickle' , 'wb' ) as file :
61
72
pickle .dump (decrypted_text , file )
62
73
74
+
63
75
def load_progress ():
64
76
if os .path .exists ('progress.pickle' ):
65
77
with open ('progress.pickle' , 'rb' ) as file :
66
78
return pickle .load (file )
67
79
else :
68
80
return None
69
81
82
+
70
83
if __name__ == '__main__' :
71
84
cryptogram = input ("Enter the cryptogram: " )
72
85
word_dict_path = input ("Enter the path to the word dictionary file: " )
73
86
74
87
word_dictionary = load_word_dictionary (word_dict_path )
75
88
76
89
letter_frequencies = get_letter_frequencies (cryptogram )
77
- decrypted_message = load_progress () or decrypt_cryptogram (cryptogram , letter_frequencies , word_dictionary )
90
+ decrypted_message = load_progress () or decrypt_cryptogram (
91
+ cryptogram , letter_frequencies , word_dictionary )
78
92
79
93
print ("\n Original cryptogram:" , cryptogram )
80
94
print ("Decrypted message:" , decrypted_message )
0 commit comments