X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=cipherbreak.py;fp=cipherbreak.py;h=e3c1e5f18a9d1a1dc8fcdda43346bf39828741ee;hb=837af0a78bfaf51266a1e21fbfd2baa80857d5bb;hp=7c6cea7ef9758f638dc396049cb833c874d02820;hpb=f3e1998f73574cc3178ab45c93fd8aa27cf7d6ae;p=cipher-tools.git

diff --git a/cipherbreak.py b/cipherbreak.py
index 7c6cea7..e3c1e5f 100644
--- a/cipherbreak.py
+++ b/cipherbreak.py
@@ -40,6 +40,18 @@ from language_models import *
 # timeit.timeit('keyword_break(c5a)', setup='gc.enable() ; from __main__ import c5a ; from cipher import keyword_break', number=1)
 # timeit.repeat('keyword_break_mp(c5a, chunksize=500)', setup='gc.enable() ; from __main__ import c5a ; from cipher import keyword_break_mp', repeat=5, number=1)
 
+
+def index_of_coincidence(text):
+    stext = sanitise(text)
+    counts = collections.Counter(stext)
+    denom = len(stext) * (len(text) - 1) / 26
+    return (
+        sum(max(counts[l] * counts[l] - 1, 0) for l in string.ascii_lowercase)
+        /
+        denom
+    )
+
+
 transpositions = collections.defaultdict(list)
 for word in keywords:
     transpositions[transpositions_of(word)] += [word]