Skip to content

Commit 059b4e5

Browse files
Merge pull request #2560 from andoriyaprashant/branch22
Crytogram Solver Script added
2 parents c7f42f6 + be6696c commit 059b4e5

File tree

4 files changed

+466680
-0
lines changed

4 files changed

+466680
-0
lines changed

Cryptogram Solver/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Cryptogram Solver
2+
3+
## Description
4+
Cryptogram Solver is a Python script that automatically decrypts cryptograms. Cryptograms are word puzzles where each letter in the original word is replaced with another letter, and your task is to find the original word. This script uses frequency analysis techniques and a dictionary of words to solve the cryptogram.
5+
6+
## Features
7+
1. **Letter Frequency Visualization**: The script displays a bar chart to visualize the letter frequency in the cryptogram.
8+
2. **Letter Substitution Game**: You can manually guess the letter substitutions to solve the cryptogram.
9+
3. **Hint System**: The script provides frequency analysis of letters to help you make educated guesses.
10+
4. **Save/Load Progress**: Your progress is saved and loaded for future sessions.
11+
12+
## How to Use
13+
1. Install Python on your computer (Python 3.x is recommended).
14+
2. Download the script `cryptogram_solver.py` and the word dictionary file `english.words.txt` (or use your own word dictionary).
15+
3. Run the script using the command `python cryptogram_solver.py`.
16+
4. Enter the cryptogram when prompted.
17+
5. Optionally, enter the path to your word dictionary file.
18+
6. The script will automatically attempt to decrypt the cryptogram using frequency analysis and the word dictionary.
19+
7. After decryption, you can play the letter substitution game, view frequency analysis, and manually modify the decrypted message.
20+
21+
## Requirements
22+
- Python 3.x
23+
- Matplotlib (for letter frequency visualization)
24+
- nltk
25+
26+
## File Structure
27+
- `cryptogram_solver.py`: The main Python script for solving cryptograms.
28+
- `english.words.txt`: A text file containing a list of English words, one word per line, to use as a word dictionary.
29+
- `progress.pickle`: A binary file that stores your progress during decryption.
30+
31+
## Notes
32+
- The success of the decryption depends on the quality of the word dictionary used and the length of the cryptogram.
33+
- The letter frequency analysis may not be perfect for very short cryptograms.
34+
- Have fun and enjoy solving cryptograms!
35+
36+
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)