From 3b82269b94de43fa0150fe5b34ca39b4ba3b6ba4 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Fri, 11 Oct 2013 11:14:05 +0100 Subject: [PATCH] Refactored out sanitise, added loading of English letter counts --- cipher.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/cipher.py b/cipher.py index c4b28ba..b3ab488 100644 --- a/cipher.py +++ b/cipher.py @@ -1,6 +1,21 @@ import string import collections +english_counts = collections.defaultdict(int) +with open('count_1l.txt', 'r') as f: + for line in f: + (letter, count) = line.split("\t") + english_counts[letter] = int(count) + +def sanitise(text): + sanitised = [c.lower() for c in text if c in string.ascii_letters] + return ''.join(sanitised) + +def letter_frequencies(message): + frequencies = collections.defaultdict(int) + for letter in sanitise(message): + frequencies[letter]+=1 + return frequencies def caesar_cipher_letter(letter, shift): if letter in string.ascii_letters: @@ -22,11 +37,3 @@ def caesar_cipher_message(message, shift): def caesar_decipher_message(message, shift): return caesar_cipher_message(message, -shift) - -def letter_frequencies(message): - frequencies = collections.defaultdict(int) - for letter in message: - if letter in string.ascii_letters: - frequencies[letter.lower()]+=1 - return frequencies - -- 2.34.1