Code Cracker Word Solver Exclusive -
import sys import re from collections import Counter ---------------------------------------------------------------------- 1. Build a frequency dictionary from an English word list ---------------------------------------------------------------------- def load_word_list(filename="words_alpha.txt"): """Load a list of English words (one per line).""" try: with open(filename, 'r') as f: words = [w.strip().lower() for w in f if len(w.strip()) > 1] return set(words) except FileNotFoundError: # Fallback: common short word list return set([ "a", "i", "an", "as", "at", "be", "by", "do", "go", "he", "it", "is", "me", "my", "no", "of", "on", "or", "so", "to", "up", "us", "we", "the", "and", "for", "are", "but", "not", "you", "with", "have", "from", "they", "this", "that", "was", "were", "word", "code", "crack", "solve" ]) ---------------------------------------------------------------------- 2. Pattern matching for words (e.g., "abc" pattern for "the") ---------------------------------------------------------------------- def get_word_pattern(word): """Return pattern like 0.1.2.0.3 for 'test' -> '0.1.2.0.3'.""" pattern = [] letter_map = {} next_num = 0 for ch in word: if ch not in letter_map: letter_map[ch] = str(next_num) next_num += 1 pattern.append(letter_map[ch]) return '.'.join(pattern) ---------------------------------------------------------------------- 3. Candidate word finder ---------------------------------------------------------------------- def build_pattern_dict(word_set): """Map pattern -> list of words with that pattern.""" pattern_dict = {} for w in word_set: p = get_word_pattern(w) pattern_dict.setdefault(p, []).append(w) return pattern_dict ---------------------------------------------------------------------- 4. Solve substitution cipher using word patterns ---------------------------------------------------------------------- class CodeCracker: def init (self, word_set=None): if word_set is None: word_set = load_word_list() self.word_set = word_set self.pattern_dict = build_pattern_dict(word_set)
cracker = CodeCracker()
while True: print("\n> ", end="") cipher = input().strip() if cipher.lower() in ('quit', 'exit', 'q'): break if not cipher: continue decoded, mapping = cracker.solve(cipher) print("\nDecoded message:") print(decoded) if mapping: print("\nCipher mapping (cipher -> plain):") for c in sorted(mapping.keys()): print(f" c.upper() -> mapping[c].upper()") else: print("\nNo mapping found — try a larger word list.") print("-" * 50) 6. Example usage (if run as script) ---------------------------------------------------------------------- if name == " main ": # Test with a famous cipher example (ROT13 actually) test_message = "Gur dhvpx oebja sbk whzcf bire gur ynml qbt" cracker = CodeCracker() result, _ = cracker.solve(test_message) print("\nTest:\n" + test_message) print("-> " + result) code cracker word solver
Gur dhvpx oebja sbk whzcf bire gur ynml qbt import sys import re from collections import Counter