-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrequency.py
More file actions
59 lines (50 loc) · 1.98 KB
/
Copy pathfrequency.py
File metadata and controls
59 lines (50 loc) · 1.98 KB
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
55
56
57
58
59
class Frequency:
def __init__(self, n_gram, alphabet):
self.n_gram = n_gram
self.alphabet = alphabet
# grams[i] is all string in i_gram
# pos_of[i][s] is the position of s in gram[i]
self.grams = [[] for _ in n_gram]
self.pos_of = [dict() for _ in n_gram]
for i in range(len(n_gram)):
def add_grams(x, s):
if x == n_gram[i]:
self.grams[i].append(s)
self.pos_of[i][s] = len(self.grams[i]) - 1
return
for c in alphabet:
add_grams(x + 1, s + c)
add_grams(0, "")
self.ftab = []
def get_frequency(self, text):
appearance = calculate_appearance(text, self.alphabet, self.n_gram, self.grams, self.pos_of)
self.ftab = frequency_table(appearance)
def get_frequency_from_file(self, filename):
appearance = []
for i in range(len(self.n_gram)):
f = open(filename[i], "r")
text = f.read()
text = list(text.split())
appearance.append([0 for _ in self.grams[i]])
for j in range(0, len(text) - 1, 2):
appearance[i][self.pos_of[i][text[j]]] = int(text[j + 1])
self.ftab = frequency_table(appearance)
def calculate_appearance(text, alphabet, n_gram, grams, pos_of):
text = plaintext(text, alphabet)
appearance = []
for i in range(len(n_gram)):
appearance.append([0 for _ in grams[i]])
for j in range(len(text) - n_gram[i]):
t = text[j:j + n_gram[i]]
appearance[i][pos_of[i][t]] += 1
return appearance
def frequency_table(appearance):
ftab = appearance
for i in range(len(appearance)):
total = sum(appearance[i])
for j in range(len(appearance[i])):
ftab[i][j] = appearance[i][j] / total
return ftab
def plaintext(text, alphabet):
text = "".join(c.upper() for c in text if c.upper() in alphabet)
return text