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