zaklad mailoveho rozhrani
[krypto.git] / referencni.py
index 5afbd86..692b4aa 100644 (file)
@@ -1,15 +1,15 @@
 #!/usr/bin/env python
 
-from itertools import combinations
+"""Modul pro praci s referencnimi tabulkami."""
+
 import pickle
 from ocesavac import ocesat
 import codecs
-from koincidence import index_koincidence
-
-ABECEDA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
-MABECEDA = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ'
+from koincidence import index_koincidence2
+from spolecne import ABECEDA, MABECEDA
 
 def nova_tabulka(tabulka, abc=MABECEDA):
+    """Z tabulky cetnosti vyskytu udela tabulku percentualni cetnosti."""
     celkem = sum(sum(tab2.values()) for tab2 in tabulka.values())
     if celkem != 0:
         for i in abc:
@@ -56,18 +56,35 @@ class treference:
         self.soubory.append(soubor)
 
 def uloz(co, kam):
-    f = open(kam, 'w')
-    pickle.dump(co, f)
-    f.close()
+    soubor = open(kam, 'w')
+    pickle.dump(co, soubor)
+    soubor.close()
 
 def nacti(odkud):
     return pickle.load(open(odkud))
 
+def tiskni(sref):
+    r = nacti(sref)
+    slovnik = dict(zip(r.abeceda, r.frekvence))
+    for i in range(len(r.abeceda)):
+        print "{0} {1:>6.2%}   {2} {3:>6.2%}".format(
+            r.abeceda[i],
+            r.frekvence[i],
+            r.poradi[i],
+            slovnik[r.poradi[i]] )
+    print " "
+    print "Index koincidence: {0:>5.2%}".format(r.index_koinc)
+    print " "
+    for char in r.abeceda:
+        print char + " " + " ".join(
+            ["{:0>5.2f}".format(r.tabulka[char][ch2] * 1000)
+                for ch2 in r.abeceda])
+
 def prumerny_index(tref):
     suma = 0
-    for soubor in ref.soubory:
+    for soubor in tref.soubory:
         f = codecs.open(soubor, encoding='UTF-8')
         text = ocesat(f.read(), tref.mezery)
         f.close()
-        suma += index_koincidence(text[:len(text)/2], text[len(text)/2:])
+        suma += index_koincidence2(text[:len(text)/2], text[len(text)/2:])
     return float(suma)/len(tref.soubory)