-
Notifications
You must be signed in to change notification settings - Fork 0
/
consensus.py
300 lines (229 loc) · 9.5 KB
/
consensus.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# Trying to prepare "consensus discount" features
# Using Apertium
# MLF 20170505
# import sys
import subprocess
from nltk.tokenize import word_tokenize
import argparse
import sys
import math
import numpy as np
from scipy.optimize import minimize
def readdata(filename):
return ((open(filename).read()).rstrip("\n")).split("\n")
# Lifted from GSoC code by Pankaj Sharma
def translateText(text, pair, directory=None):
p1 = subprocess.Popen(['echo', text], stdout=subprocess.PIPE)
if directory:
p2 = subprocess.Popen(['apertium', '-d', directory, '{0}-{1}'.format(*pair)], stdin=p1.stdout, stdout=subprocess.PIPE)
else:
p2 = subprocess.Popen(['apertium', '{0}-{1}'.format(*pair)], stdin=p1.stdout, stdout=subprocess.PIPE)
p1.stdout.close()
return p2.communicate()[0].decode('utf-8').strip()
# lifted from http://stackoverflow.com/questions/10106901/elegant-find-sub-list-in-list
def subfinder(mylist, pattern):
matches = []
for i in range(len(mylist)):
if mylist[i] == pattern[0] and mylist[i:i+len(pattern)] == pattern:
matches.append(pattern)
return matches
reload(sys)
sys.setdefaultencoding("utf-8")
# Argument parsing
parser = argparse.ArgumentParser()
# Mandatory arguments: PE time, source, MT
parser.add_argument("tr_time",help="Training PE time")
parser.add_argument("tr_source", help="Training source segments")
parser.add_argument("tr_mt", help="Training MTed segments")
#
parser.add_argument("te_time",help="Test PE time")
parser.add_argument("te_source", help="Test source segments")
parser.add_argument("te_mt", help="Test MTed segments")
# Ngram size
parser.add_argument('--maxngram', help='Maximum ngram size', type=int, default=3)
# Verbose
parser.add_argument('-v', '--verbose', help='Verbose Mode', dest="verbose", action='store_true',default=False)
# Optimization
parser.add_argument('--maxiter', help="Maximum number of iterations (default 200)", type=int, default=200)
# Repeats
parser.add_argument('--repeats', help="Number of repeats", type=int, default=10)
# Counts
# By default, multiple hits are taken as a Boolean value
# This option actually counts the hits
parser.add_argument('--counts', help="Count hits instead of Boolean", action="store_true", default=False)
# Parse them
args=parser.parse_args()
# Read the data
train_pe_time = readdata(args.tr_time)
train_source = readdata(args.tr_source)
train_mt = readdata(args.tr_mt)
test_pe_time = readdata(args.te_time)
test_source = readdata(args.te_source)
test_mt = readdata(args.te_mt)
# Zip it to loop
training_data=zip(train_pe_time,train_source,train_mt)
test_data=zip(test_pe_time,test_source,test_mt)
languagepair=("en","es")
directory="/home/mlf/apertium-sources/apertium-en-es"
# segment translations will be held in a cache
cache={}
# Store the hits for each n-gram size and each segment
hits = [[0 for x in range(args.maxngram+1)] for y in range(len(training_data))]
# To count the training set coverage ratio
attempts = [0 for x in range(args.maxngram+1)]
successes = [0 for x in range(args.maxngram+1)]
for m, (time, source, target) in enumerate(training_data) :
# source=train[1]
# target=train[2]
if args.verbose :
print "=========== segment {0}".format(m)
print source
print target
source_tok=word_tokenize(source)
target_tok=word_tokenize(target.lower())
# Prepare long translation job to avoid starting Apertium over and over again
# for each n-gram
sourcesegs=""
for n in range(1,args.maxngram+1) :
for i in range(len(source_tok)-n+1):
# The cache could be looked up here to avoid translating stuff --> later
seg=" ".join(source_tok[i:i+n])
sourcesegs= sourcesegs + seg + "\n\n"
targetsegs=translateText(sourcesegs,languagepair,directory).lower()
# Store n-gram translations in a cache
for pair in zip(sourcesegs.split("\n\n"),targetsegs.split("\n\n")):
cache[pair[0]]=pair[1]
# I'll do the reverse cache later
# Now run the thing
hits[m][0]=len(source_tok) # Store length for zero-grams
if args.verbose :
print "Length=", hits[m][0]
for n in range(1,args.maxngram+1) :
hits[m][n]=0
for i in range(len(source_tok)-n+1):
seg=" ".join(source_tok[i:i+n])
try : # this should not be needed
res=cache[seg]
except KeyError:
res="Dummy Dummy Dummy"
if args.verbose :
print "==== KeyError exception handled when querying cache during the training phase"
print "Key=[[["+seg+"]]]"
res_tok=word_tokenize(res.lower())
attempts[n] = attempts[n] + 1 # matching attempts for n-gram size n
if len(res_tok) :
found_tok=subfinder(target_tok,res_tok)
if found_tok :
# print "( ",seg," : ", res, ")"
# print float(len(source_tok))/float((len(source_tok)-n+1))
if args.counts :
successes[n] = successes[n] + len(found_tok)
hits[m][n] = hits[m][n] + float(len(source_tok))/float((len(source_tok)-n+1)) * len(found_tok)
else :
successes[n] = successes[n] + 1
hits[m][n] = hits[m][n] + float(len(source_tok))/float((len(source_tok)-n+1))
if args.verbose :
print "Ngram=", n, " Hits=", hits[m][n]
# End for train
if args.verbose :
for key in cache :
print "((( " + key + " ::: " + cache[key] + ")))"
# Can I move this function definition to the top?
def mae(a) :
MAE=0
for m, (time, source, target) in enumerate(training_data) :
dev=float(time)
for n in range(0,args.maxngram+1) :
dev=dev-a[n]*hits[m][n]
MAE=MAE+math.fabs(dev)
return MAE/len(training_data)
best_train_set_MAE=float("inf")
for i in range(args.repeats) :
# Initial values --- starting with a value sampled from N(0,1)
a0=np.array([np.random.randn() for y in range(args.maxngram+1)])
# Optimize to an error of 0.00001 in MAE (will change later, can also use BFGS)
result = minimize(mae, a0, method='nelder-mead', options={'fatol' : 1e-6, 'disp' : True, 'maxiter' : args.maxiter})
if result.success :
print "Optimization successful in {0} iterations".format(result.nit)
else :
print "Optimization unsuccessful"
print result.status
print "Result=", result.x
print "Length coefficient=", (result.x)[0]
for n in range(1,args.maxngram+1):
print n,"-gram coefficient=", (result.x)[n]
print n,"-gram success rate=", float(successes[n])/float(attempts[n])
current_MAE=mae(result.x)
print "Training set MAE=", current_MAE
if current_MAE<best_train_set_MAE :
best_train_set_MAE = current_MAE
best_a=result.x
print "Best training set MAE so far=", best_train_set_MAE
# End for
# Now compute MAE over test set
testhits = [0 for x in range(args.maxngram+1)]
# To count the test set coverage ratio
attempts = [0 for x in range(args.maxngram+1)]
successes = [0 for x in range(args.maxngram+1)]
MAE=0.0
for m, (time, source, target) in enumerate(test_data) :
# source=test[1]
# target=test[2]
if args.verbose :
print source
print target
source_tok=word_tokenize(source)
target_tok=word_tokenize(target.lower())
# Prepare long translation job to avoid starting Apertium over and over again
# for each n-gram
sourcesegs=""
for n in range(1,args.maxngram+1) :
for i in range(len(source_tok)-n+1):
# The cache could be looked up here to avoid translating stuff --> later
seg=" ".join(source_tok[i:i+n])
sourcesegs= sourcesegs + seg + "\n\n"
targetsegs=translateText(sourcesegs,languagepair,directory).lower()
# Store n-gram translations in a cache
for pair in zip(sourcesegs.split("\n\n"),targetsegs.split("\n\n")):
cache[pair[0]]=pair[1]
# Now run the thing
testhits[0]=len(source_tok) # Store length for zero-grams
if args.verbose :
print "Length=", testhits[0]
for n in range(1,args.maxngram+1) :
testhits[n]=0
for i in range(len(source_tok)-n+1):
seg=" ".join(source_tok[i:i+n])
try : # This should not be necessary, but avoids some (strange) KeyErrors
res=cache[seg]
except KeyError :
res="Dummy Dummy Dummy"
if args.verbose :
print "==== KeyError exception handled when querying cache during the test phase"
print "Key=[[["+seg+"]]]"
res_tok=word_tokenize(res.lower())
attempts[n] = attempts[n] +1 # matching attempts for n-gram size n
if len(res_tok) :
found_tok=subfinder(target_tok,res_tok)
if found_tok :
# print "( ",seg," : ", res, ")"
# print float(len(source_tok))/float((len(source_tok)-n+1))
if args.counts :
successes[n] = successes[n] + len(found_tok) # successful attempts for n-gram size n
testhits[n] = testhits[n] + float(len(source_tok))/float((len(source_tok)-n+1)) * len(found_tok)
else :
successes[n] = successes[n] + 1 # successful attempts for n-gram size n
testhits[n] = testhits[n] + float(len(source_tok))/float((len(source_tok)-n+1))
if args.verbose :
print "Ngram=", n, " Hits=", testhits[n]
# Compute contribution to MAE using best_a
prediction=0
for n in range(0,args.maxngram+1) :
prediction=prediction+best_a[n]*testhits[n]
if args.verbose :
print "Prediction={0} Time={1}".format(prediction, time)
MAE=MAE+math.fabs(float(time)-prediction)/len(test_data)
# End for train
print "Test set MAE=", MAE
for n in range(1,args.maxngram+1):
print "Test set", n,"-gram success rate=", float(successes[n])/float(attempts[n])