-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
165 lines (133 loc) · 5.09 KB
/
main.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import prepare_data
from Feature import Feature
from Decoder import Decoder
import operator
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--mode', '-m', type=str,
default='train_avg', help='train/train_avg/test')
parser.add_argument('--beam', '-b', type=int, default=16, help='beam size')
parser.add_argument('--iter', '-n', type=int, default=10, help='iterations')
args = parser.parse_args()
args.mode = 'test_avg' # test_avg
mode = args.mode
iter = args.iter
beam_size = args.beam
def train(iterations, train_file, beam_size):
data = prepare_data.read_file(train_file)
feature = Feature()
decoder = Decoder(beam_size, feature.get_score)
for t in range(iterations):
count = 0
data_size = len(data)
for line in data:
y = line.split()
z = decoder.beamSearch(line)
if z != y:
feature.update_weight(y, z)
train_seg = ' '.join(z)
seg_data_file = 'train_seg_data/train-seg-data_ model-' + \
str(t) + '.txt'
with open(seg_data_file, 'a') as f:
f.write(train_seg + '\n')
count += 1
if count % 1000 == 0:
print("iter %d , finish %.2f%%" % (t, (count/data_size)*100))
model_file = open("model_result/model-" +
str(t)+"_beam-size-"+str(beam_size)+'.pkl', 'wb')
feature.save_model(model_file)
model_file.close()
f.close()
print("segment with model-%d finish" % t)
print("iteration %d finish" % t)
def train_avg(iterations, train_file, beam_size):
data = prepare_data.read_file(train_file)
feature = Feature()
decoder = Decoder(beam_size, feature.get_score)
n = 0
for t in range(iterations):
count = 0
data_size = len(data)
for line in data:
n += 1
y = line.split()
z = decoder.beamSearch(line)
if z != y:
feature.update_avgWeight(y, z, n, t, data_size)
train_seg = ' '.join(z)
count += 1
if count % 1000 == 0:
print("iter %d , finish %.2f%%" % \
(t, (count / data_size) * 100))
model_file = open("model_result/model-" + \
str(t) + "_beam-size-" + str(beam_size) + '.pkl', 'wb')
feature.save_model(model_file)
model_file.close()
print("segment with model-%d finish" % t)
print("iteration %d finish" % t)
feature.last_update(iterations, data_size)
feature.cal_avg_weight(iterations, data_size)
avg_model = open(
"model_result/avg-model_beam-size-" + str(beam_size) + '.pkl', 'wb')
feature.save_model(avg_model)
avg_model.close()
print("segment with avg-model finish")
def test(iterations, test_file, beam_size, mode):
data = prepare_data.read_file(test_file)
feature = Feature()
decoder = Decoder(beam_size, feature.get_score)
for t in range(iterations):
count = 0
data_size = len(data)
model_file = open('model_result/model-' +
str(t)+'_beam-size-'+str(beam_size)+'.pkl', 'rb')
feature.load_model(model_file)
model_file.close()
for line in data:
z = decoder.beamSearch(line)
seg_data = ' '.join(z)
seg_data_file = 'test_seg_data/test-seg-data_model-' + \
str(t)+'_beam-size-'+str(beam_size)+'.txt'
with open(seg_data_file, 'a') as f:
f.write(seg_data+'\n')
count += 1
if count % 1000 == 0:
print("segment with model-%d , finish %.2f%%" %
(t, (count / data_size) * 100))
f.close()
print("segment with model-%d finish" % t)
def test_avg(iterations, test_file, beam_size):
data = prepare_data.read_file(test_file)
feature = Feature()
decoder = Decoder(beam_size, feature.get_score)
count = 0
data_size = len(data)
model_file = open(
'model_result/avg-model_beam-size-' + str(beam_size) + '.pkl', 'rb')
feature.load_model(model_file)
model_file.close()
for line in data:
z = decoder.beamSearch(line)
seg_data = ' '.join(z)
seg_data_file = 'test_seg_data/avg-test-seg-data' + \
'_beam-size-' + str(beam_size) + '.txt'
with open(seg_data_file, 'a') as f:
f.write(seg_data + '\n')
count += 1
if count % 1000 == 0:
print("segment with avg-model, finish %.2f%%" %
((count / data_size) * 100))
f.close()
print("segment with avg model finish")
if __name__ == '__main__':
print("The mode at this moment is:", mode)
train_file = 'data/filter_train.txt'
test_file = 'data/filter_test.txt'
if mode == 'train':
train(iter, train_file, beam_size)
elif mode == 'train_avg':
train_avg(iter, train_file, beam_size)
elif mode == 'test':
test(iter, test_file, beam_size, mode)
elif mode == 'test_avg':
test_avg(iter, test_file, beam_size)