-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathios.py
55 lines (38 loc) · 930 Bytes
/
ios.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from numpy import loadtxt, load
def get_plaintext(file):
plain_seq = []
f = open(file, 'r')
for line in f:
line = line.strip().split()
plain_seq.extend(line)
return plain_seq
def get_ciphertext(file):
cipher_seq = []
f = open(file, 'r')
for line in f:
line = line.strip().split()
cipher_seq.extend(line)
return cipher_seq
def get_bigram(file):
mat = load(file)
assert (abs(mat[0, :].sum() - 1) < 0.0001)
return mat
def get_trigram(file, fmt='npy'):
if fmt == 'npy':
mat = load(file)
assert (abs(mat[0, 0, :].sum() - 1) < 0.0001)
return mat
def toint(c):
return ord(c) - 65
def tochar(i):
return chr(i + 65)
def tointseq(charseq):
seq = []
for c in charseq:
seq += [toint(c)]
return seq
def tocharseq(intseq):
seq = []
for i in intseq:
seq += [tochar(i)]
return seq