code for generating tex output for friedman test

This commit is contained in:
Simon Pirkelmann 2020-01-28 18:45:05 +01:00
parent 3b1b03c338
commit 657a447733
2 changed files with 102 additions and 0 deletions

83
code/friedman.py Normal file
View File

@ -0,0 +1,83 @@
f = open('frost_cipher.txt')
cipher = f.readlines()
cipher = [c.strip('\n').split(' ') for c in cipher]
clean = ''
for c in cipher:
clean += "".join(c)
# print("".join(c))
ciphertext = clean.upper()
colors = ['red', 'blue', 'darkgreen', 'orange', 'black', 'yellow']
width = 110
blocknum = 4
blocks = ["" for i in range(0,blocknum)]
output0 = "\\texttt{"
output1 = "\\texttt{"
for i in range(len(ciphertext)):
output0 += ciphertext[i]
for j in range(blocknum):
if i % blocknum == j:
output1 += "\\textcolor{" + colors[j] + "}{" + ciphertext[i] + "}"
blocks[i%blocknum] += ciphertext[i]
if (i+1)%width == 0:
output0 += '\n'
output1 += '\n'
output0 += "}"
output1 += "}"
f0 = open('friedman_0.tex', 'w')
f0.write(output0)
f0.close()
f1 = open('friedman_{}.tex'.format(blocknum), 'w')
f1.write(output1)
f1.close()
def compute_coincidence(text):
letter_count = {}
letters = [chr(i) for i in range(ord('A'), ord('Z')+1)]
for c in letters:
letter_count[c] = 0
for c in text:
if c in letter_count.keys():
letter_count[c] += 1
N = len(text)
s = 0
for n_i in letter_count.values():
s += n_i * (n_i - 1)
kappa = float(s) / (N * (N - 1))
print("kappa_o = ", kappa)
return kappa
def output_block(block, color, width=20):
output = "\\texttt{\\textcolor{" + color + "}{"
for i in range(len(block)):
output += block[i]
if (i+1)%width == 0:
output += '\n'
output += "}}"
kappa = compute_coincidence(blocks[j])
output += "\\textcolor{"+color+"}{$$" + "\\kappa_o = {:6.4f}".format(kappa) + "$$}"
print(output)
f = open('friedman_{}_'.format(blocknum) + color + '.tex', 'w')
f.write(output)
f.close()
for j in range(blocknum):
output_block(blocks[j], colors[j])

19
code/frost_cipher.txt Normal file
View File

@ -0,0 +1,19 @@
Ync jhfug vbavfyxi zb s rjczgp bfcv
Tsu ggkwp W uhzcr fhy kfsojc pgmm
Rbv uj fbw mwrjweji zggl Z glhtu
Ofw qfccxi ucog tes sl krf sl N tcmei
Kc oajis am gvbl bs kvw nsusjzwfkla
Yysf mtfy laj fhzxw rg bnxk ok yfzf
Sgi yonbsx dwkmrdk mmv pwmyvf uefza
Txhrikx nk ksl liokld rbv pfehww bvoj
Mmfiya fj tgk yyol mmv dslxzby mmvfw
Sgi scla yyol ftibagl vemtqcm dtd
Zb dxfmsk gt jhwi mrr lkturwg gcoud
Ty W cxuk hzx kzfkm kff sgtkvwk irm
Qxy bbgpneu zhb noq ejrrk hs kc otd
Z rgngksv bk Z gzhzcr woji qgfj soud
N jvseq ss lxqcwfz yywk pnkv s lnxv
Khrvkzxwv oyxx rbv tlvg zxsts
Lpt icswx uwnxwxsv bs r kghi rbv B
N kcgd yys ggj cskl yionxqvr tr
Fer lafk vsl rrrw tqc hzx iztxxwvbux