def _score_text(self, text): """Score text by counting known English words.""" words = re.findall(r'[A-Za-z]+', text.lower()) score = 0 for w in words: if w in self.word_set: score += len(w) # longer words give more confidence return score 5. Interactive solver (command line) ---------------------------------------------------------------------- def main(): print("=" * 50) print("CODE CRACKER WORD SOLVER") print("Solves simple substitution ciphers (A-Z -> any letter)") print("Enter your coded message (use letters only, spaces fine):") print("(Example: 'Gur dhvpx oebja sbk whzcf bire gur ynml qbt')") print("Type 'quit' to exit.") print("=" * 50)
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) code cracker word solver
def _consistent(self, cipher_word, plain_word, mapping): """Check if cipher_word can map to plain_word given existing mapping.""" for c, p in zip(cipher_word, plain_word): if c in mapping and mapping[c] != p: return False return True def _score_text(self, text): """Score text by counting known
Gur dhvpx oebja sbk whzcf bire gur ynml qbt Pattern matching for words (e