-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_test.py
145 lines (124 loc) · 4.89 KB
/
train_test.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import pickle, components, pprint
pp = pprint.PrettyPrinter(indent=5)
def train_emission():
for language in ['EN','ES']:
file = open("raw/" + language + "/train", encoding='utf8')
en_file = file.readlines()
y = {}
for i in en_file:
i = i.replace("\n", "")
array = i.split(" ")
if len(array) == 2:
if array[1] not in y.keys():
y[str(array[1])] = 0##################################################0
else:
y[str(array[1])] += 1
testfile = open("test/" + language + "/test.in", encoding='utf8')
test_x = testfile.readlines()
x = []
counter = 0
for i in test_x:
counter += 1
i = i.replace("\n", "")
if i != "" and i not in x:
x.append(i)
emission = {}
for i in en_file:
i = i.replace("\n", "")
array = i.split(" ")
if len(array) == 2:
if str(array[0]) not in emission.keys():
emission[str(array[0])] = {array[1]: 1}
elif str(array[1]) not in emission[str(array[0])].keys():
emission[str(array[0])][array[1]] = 1
else:
emission[str(array[0])][array[1]] += 1
for i in emission.keys():
if i in x:
for k in emission[i].keys():
emission[i][k] = emission[i][k] * 1.0 / (y[k] + 1)
else:
for k in emission[i].keys():
emission[i][k] = emission[i][k] * 1.0 / y[k]
for i in x:
if i not in emission.keys():
emission[str(i)] = {}
for j in y.keys():
emission[str(i)][j] = 1.0 / (y[j] + 1)
#
# for word in emission:
# for state in y.keys():
# if state not in emission[word]:
# emission[word][state]=0.0
pp.pprint(y)
pickle.dump(emission, open("test/params/emission/" + language + ".txt", "wb"))
print("Finished training emission params for "+ language)
def train_transition():
for language in components.files:
transition_count = {"start": {}}
##read data
file = open("raw/" + language + "/train", encoding='utf8')
rawinput = file.readlines()
##import data
data = [[]]
index = 0
subindex = 0
for line in rawinput:
if line == "\n":
index += 1
data.append([])
else:
elements = line.split(" ")
if len(elements) != 0:
last = elements[len(elements) - 1]
last = last[:len(last) - 1]
data[index].append(last)
data.pop()
# Count numbers
for line in data:
##transition
for i in range(len(line) + 1):
if i == 0:
##start
from_state = "start"
to_state = line[0]
if line[0] in transition_count[from_state]:
transition_count[from_state][to_state] += 1
else:
transition_count[from_state][to_state] = 1
else:
##stop
if i == len(line):
from_state = line[i - 1]
to_state = "stop"
##the rest
else:
from_state = line[i - 1]
to_state = line[i]
if from_state in transition_count:
if to_state in transition_count[from_state]:
transition_count[from_state][to_state] += 1
else:
transition_count[from_state][to_state] = 1
else:
transition_count[from_state] = {}
transition_count[from_state][to_state] = 1
def a(f, t, count):
sum = 0
for state in count[f]:
sum += count[f][state]
return count[f][t] / float(sum)
transition = {}
for from_state in components.states:
for to_state in components.states:
if from_state not in transition:
transition[from_state] = {}
if from_state in transition_count and to_state in transition_count[from_state]:
transition[from_state][to_state] = a(from_state, to_state, transition_count)
else:
transition[from_state][to_state] = 0
pickle.dump(transition, open("test/params/transition/" + language + ".txt", "wb"))
print("Finished training transition params for "+ language)
#
train_emission()
train_transition()