New version of find casesar break parameters
[cipher-tools.git] / find_best_caesar_break_parameters-2.py
1 import random
2 import collections
3 from cipher import *
4 from cipherbreak import *
5 import itertools
6
7 print('Loading...')
8
9 corpus = sanitise(''.join([open('shakespeare.txt', 'r').read(),
10 open('sherlock-holmes.txt', 'r').read(),
11 open('war-and-peace.txt', 'r').read()]))
12 corpus_length = len(corpus)
13
14 euclidean_scaled_english_counts = norms.euclidean_scale(english_counts)
15
16 metrics = [{'func': norms.l1, 'name': 'l1'},
17 {'func': norms.l2, 'name': 'l2'},
18 {'func': norms.l3, 'name': 'l2'},
19 {'func': norms.cosine_distance, 'name': 'cosine_distance'},
20 {'func': norms.harmonic_mean, 'name': 'harminic_mean'},
21 {'func': norms.geometric_mean, 'name': 'geometric_mean'},
22 {'func': norms.inverse_log_pl, 'name': 'inverse_log_pl'}]
23 scalings = [{'corpus_frequency': normalised_english_counts,
24 'scaling': norms.normalise,
25 'name': 'normalised'},
26 {'corpus_frequency': euclidean_scaled_english_counts,
27 'scaling': norms.euclidean_scale,
28 'name': 'euclidean_scaled'},
29 {'corpus_frequency': normalised_english_counts,
30 'scaling': norms.identity_scale,
31 'name': 'normalised_with_identity'}]
32 message_lengths = [300, 100, 50, 30, 20, 10, 5]
33
34 trials = 5000
35
36 scores = collections.defaultdict(int)
37
38 def eval_all():
39 list(itertools.starmap(eval_one_parameter_set,
40 itertools.product(metrics, scalings, message_lengths)))
41
42 def eval_one_parameter_set(metric, scaling, message_length):
43 for i in range(trials):
44 sample_start = random.randint(0, corpus_length - message_length)
45 sample = corpus[sample_start:(sample_start + message_length)]
46 key = random.randint(1, 25)
47 sample_ciphertext = caesar_encipher(sample, key)
48 (found_key, score) = caesar_break(sample_ciphertext,
49 metric=metric['func'],
50 target_counts=scaling['corpus_frequency'],
51 message_frequency_scaling=scaling['scaling'])
52 if found_key == key:
53 scores[(metric['name'], scaling['name'], message_length)] += 1
54 return scores[(metric['name'], scaling['name'], message_length)]
55
56 def show_results():
57 with open('caesar_break_parameter_trials.csv', 'w') as f:
58 for (k, v) in scores.items():
59 print(str(k)[1:-1], v, sep=",", file=f)
60
61 eval_all()
62 show_results()