Deleted old copy of 'find caesar break params'
authorNeil Smith <neil.git@njae.me.uk>
Sun, 19 Jan 2014 20:51:20 +0000 (20:51 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Sun, 19 Jan 2014 20:51:20 +0000 (20:51 +0000)
find_best_caesar_break_parameters-2.py [deleted file]
find_best_caesar_break_parameters.py

diff --git a/find_best_caesar_break_parameters-2.py b/find_best_caesar_break_parameters-2.py
deleted file mode 100644 (file)
index 88de1dc..0000000
+++ /dev/null
@@ -1,65 +0,0 @@
-import random
-import collections
-from cipher import *
-from cipherbreak import *
-import itertools
-
-corpus = sanitise(''.join([open('shakespeare.txt', 'r').read(), 
-    open('sherlock-holmes.txt', 'r').read(), 
-    open('war-and-peace.txt', 'r').read()]))
-corpus_length = len(corpus)
-
-euclidean_scaled_english_counts = norms.euclidean_scale(english_counts)
-
-metrics = [{'func': norms.l1, 'name': 'l1'}, 
-    {'func': norms.l2, 'name': 'l2'},
-    {'func': norms.l3, 'name': 'l3'},
-    {'func': norms.cosine_distance, 'name': 'cosine_distance'},
-    {'func': norms.harmonic_mean, 'name': 'harmonic_mean'},
-    {'func': norms.geometric_mean, 'name': 'geometric_mean'},
-    {'func': norms.inverse_log_pl, 'name': 'inverse_log_pl'}]
-scalings = [{'corpus_frequency': normalised_english_counts, 
-         'scaling': norms.normalise,
-         'name': 'normalised'},
-        {'corpus_frequency': euclidean_scaled_english_counts, 
-         'scaling': norms.euclidean_scale,
-         'name': 'euclidean_scaled'},
-         {'corpus_frequency': normalised_english_counts,
-         'scaling': norms.identity_scale,
-         'name': 'normalised_with_identity'}]
-message_lengths = [300, 100, 50, 30, 20, 10, 5]
-
-trials = 5000
-
-scores = collections.defaultdict(int)
-
-def eval_all():
-    list(itertools.starmap(eval_one_parameter_set,
-        itertools.product(metrics, scalings, message_lengths)))
-
-def eval_one_parameter_set(metric, scaling, message_length):
-    for i in range(trials):
-        sample_start = random.randint(0, corpus_length - message_length)
-        sample = corpus[sample_start:(sample_start + message_length)]
-        key = random.randint(1, 25)
-        sample_ciphertext = caesar_encipher(sample, key)
-        (found_key, score) = caesar_break(sample_ciphertext, 
-                                          metric=metric['func'], 
-                                          target_counts=scaling['corpus_frequency'], 
-                                          message_frequency_scaling=scaling['scaling'])
-        if found_key == key:
-            scores[(metric['name'], scaling['name'], message_length)] += 1 
-    return scores[(metric['name'], scaling['name'], message_length)]
-
-def show_results():
-    with open('caesar_break_parameter_trials.csv', 'w') as f:
-        print(',message_length', file = f)
-        print('metric+scaling,', ','.join([str(l) for l in message_lengths]), file = f)
-        for (metric, scaling) in itertools.product(metrics, scalings):
-            print('{}:{}'.format(metric['name'], scaling['name']), end='', file=f)
-            for l in message_lengths:
-                print(',', scores[(metric['name'], scaling['name'], l)] / trials, end='', file=f)
-            print('', file = f)
-
-eval_all()
-show_results()
index 16f3bfad4f529f1ebbd27b6ef43c32379352ad6f..88de1dc8da1515679ca8b732162523a280ec37c9 100644 (file)
@@ -2,6 +2,7 @@ import random
 import collections
 from cipher import *
 from cipherbreak import *
+import itertools
 
 corpus = sanitise(''.join([open('shakespeare.txt', 'r').read(), 
     open('sherlock-holmes.txt', 'r').read(), 
@@ -32,25 +33,33 @@ trials = 5000
 
 scores = collections.defaultdict(int)
 
-with open('caesar_break_parameter_trials.csv', 'w') as f:
-    print('metric,scaling,message_length,score', file = f)
-    for metric in metrics:
-        for scaling in scalings:
-            for message_length in message_lengths:
-                for i in range(trials):
-                    sample_start = random.randint(0, corpus_length - message_length)
-                    sample = corpus[sample_start:(sample_start + message_length)]
-                    key = random.randint(1, 25)
-                    sample_ciphertext = caesar_encipher(sample, key)
-                    (found_key, score) = caesar_break(sample_ciphertext, 
-                                                      metric=metric['func'], 
-                                                      target_counts=scaling['corpus_frequency'], 
-                                                      message_frequency_scaling=scaling['scaling'])
-                    if found_key == key:
-                        scores[(metric['name'], scaling['name'], message_length)] += 1 
-                print(', '.join([metric['name'], 
-                                 scaling['name'], 
-                                 str(message_length), 
-                                 str(scores[(metric['name'], scaling['name'], message_length)] / trials) ]),
-                    file = f)
-print()
+def eval_all():
+    list(itertools.starmap(eval_one_parameter_set,
+        itertools.product(metrics, scalings, message_lengths)))
+
+def eval_one_parameter_set(metric, scaling, message_length):
+    for i in range(trials):
+        sample_start = random.randint(0, corpus_length - message_length)
+        sample = corpus[sample_start:(sample_start + message_length)]
+        key = random.randint(1, 25)
+        sample_ciphertext = caesar_encipher(sample, key)
+        (found_key, score) = caesar_break(sample_ciphertext, 
+                                          metric=metric['func'], 
+                                          target_counts=scaling['corpus_frequency'], 
+                                          message_frequency_scaling=scaling['scaling'])
+        if found_key == key:
+            scores[(metric['name'], scaling['name'], message_length)] += 1 
+    return scores[(metric['name'], scaling['name'], message_length)]
+
+def show_results():
+    with open('caesar_break_parameter_trials.csv', 'w') as f:
+        print(',message_length', file = f)
+        print('metric+scaling,', ','.join([str(l) for l in message_lengths]), file = f)
+        for (metric, scaling) in itertools.product(metrics, scalings):
+            print('{}:{}'.format(metric['name'], scaling['name']), end='', file=f)
+            for l in message_lengths:
+                print(',', scores[(metric['name'], scaling['name'], l)] / trials, end='', file=f)
+            print('', file = f)
+
+eval_all()
+show_results()