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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
|
#!/usr/bin/env ruby
require 'sinatra'
require 'sinatra/cross_origin'
require 'sinatra/reloader'
require 'nanomsg'
require 'zipf'
require 'json'
require 'tilt/haml'
require 'uri'
require 'rack'
# #############################################################################
# Load configuration file and setup global variables
# #############################################################################
require_relative "#{ARGV[0]}" # load configuration for this session
$lock = false # lock if currently learning/translating
$last_reply = nil # cache last reply
$last_processed_postedit = "" # to show to the user
$confirmed = true # client received translation?
if !FileTest.exist? LOCK_FILE # locked?
$db = {} # data file (JSON format)
$env = {} # environment variables (socket connections to daemons)
end
$status = "Idle" # current server status
# #############################################################################
# Daemons
# #############################################################################
DIR="/srv/postedit"
$daemons = {
:tokenizer => "#{DIR}/lfpe/util/nanomsg_wrapper.rb -a tokenize -S '__ADDR__' -e #{EXTERNAL} -l #{TARGET_LANG}",
:tokenizer_src => "#{DIR}/lfpe/util/nanomsg_wrapper.rb -a tokenize -S '__ADDR__' -e #{EXTERNAL} -l #{SOURCE_LANG}",
:detokenizer => "#{DIR}/lfpe/util/nanomsg_wrapper.rb -a detokenize -S '__ADDR__' -e #{EXTERNAL} -l #{TARGET_LANG}",
:detokenizer_src => "#{DIR}/lfpe/util/nanomsg_wrapper.rb -a detokenize -S '__ADDR__' -e #{EXTERNAL} -l #{SOURCE_LANG}",
:bpe => "#{DIR}/lfpe/util/nanomsg_wrapper.rb -a bpe -S '__ADDR__' -e #{EXTERNAL} -b #{SESSION_DIR}/bpe", #-B #{SESSION_DIR}/#{TARGET_LANG}",
#:debpe => "#{DIR}/lfpe/util/nanomsg_wrapper.rb -a de-bpe -S '__ADDR__' -e #{EXTERNAL}",
:truecaser => "#{DIR}/lfpe/util/nanomsg_wrapper.rb -a truecase -S '__ADDR__' -e #{EXTERNAL} -t #{SESSION_DIR}/truecase.model",
#:lamtram => "#{DIR}/online-lamtram/src/lamtram/lamtram-train-online --master_addr '__ADDR__' --model_in #{SESSION_DIR}/model --model_out #{SESSION_DIR}/model.out --dropout 0.5 --learning_rate 0.5 --beam_size 1 --size_limit 100 --unk_penalty 1.0 --word_penalty 2.5"
}
$remote_daemons = {
:lamtram => true
}
# #############################################################################
# Set-up Sinatra
# #############################################################################
set :server, 'thin'
set :bind, SERVER_IP
set :port, WEB_PORT
set :allow_origin, :any
set :allow_methods, [:get, :post]
set :allow_credentials, true
set :max_age, "1728000"
set :expose_headers, ['Content-Type']
set :public_folder, File.dirname(__FILE__) + '/static'
Rack::Utils.key_space_limit = 68719476736
# #############################################################################
# Helper functions
# #############################################################################
def logmsg name, msg
STDERR.write "[#{name}] #{msg}\n"
end
def start_daemon cmd, name, addr
logmsg :server, "starting #{name} daemon"
cmd.gsub! '__ADDR__', addr
sock = NanoMsg::PairSocket.new
sock.connect addr
pid = spawn(cmd)
Process.detach pid
logmsg :server, "#{name} detached"
logmsg :server, "< got #{sock.recv} from #{name}"
return sock, pid
end
def stop_daemon key
$env[key][:socket].send "shutdown" # every daemon shuts down
# after receiving this keyword
logmsg :server, "< #{key} is #{$env[key][:socket].recv}"
end
def stop_all_daemons
$status = "Shutting down" # status
logmsg :server, "shutting down all daemons"
$env.each_key { |k|
stop_daemon k
}
$status = "Ready to shutdown" # status
end
def update_database reset=false
$status = "Updating database" # status
if !reset
$db['progress'] += 1
else
$db['progress'] = 0
end
j = JSON.generate $db
f = WriteFile.new DB_FILE
f.write j.to_s
f.close
$status = "Updated database" # status
end
def init
$status = "Initialization" # status
$db = JSON.parse ReadFile.read DB_FILE # data from JSON file
# working directory
`mkdir -p #{WORK_DIR}/`
`mkdir #{WORK_DIR}/g`
if OLM
`mkfifo #{WORK_DIR}/refp`
end
# setup environment, start daemons
port = BEGIN_PORT_RANGE
$daemons.each { |name,cmd|
logmsg :server, "starting #{name} daemon"
sock, pid = start_daemon cmd, name, "tcp://147.142.207.34:#{port}"
$env[name] = { :socket => sock, :pid => pid }
port += 1
logmsg :server, "starting #{name} daemon done"
}
$remote_daemons.each { |name,_|
addr = "tcp://147.142.207.34:#{port}"
sock = NanoMsg::PairSocket.new
sock.bind addr
logmsg :server, "waiting for remote daemon #{name} on #{addr}"
logmsg :server, "< got #{sock.recv} from remote #{name}"
$env[name] = { :socket => sock, :pid => nil }
port += 1
}
send_recv :truecaser, "lOaD iT"
send_recv :bpe, "blablablubbbla"
#send_recv :debpe, "asdf@@ asd"
# lock file
`touch #{LOCK_FILE}`
$status = "Initialized" # status
end
def send_recv daemon, msg # simple pair communcation
socket = $env[daemon][:socket] # query -> answer
logmsg daemon, "> sending message: '#{msg}'"
socket.send msg
logmsg daemon, "waiting ..."
ans = socket.recv.force_encoding("UTF-8").strip
logmsg daemon, "< received answer: '#{ans}'"
return ans
end
# #############################################################################
# Run init() [just once]
# #############################################################################
init if !FileTest.exist?(LOCK_FILE)
# #############################################################################
# Routes
# #############################################################################
get '/' do
cross_origin # enable Cross-Origin Resource Sharing
return "" # return
end
post '/next' do
cross_origin
$status = "Received request" # status
reply = request.body.read
Thread.new { process_next reply }
end
def process_next reply
$status = "Processing request" # status
data = JSON.parse(URI.decode(reply))
if $lock
$status = "Locked" # status
return
end
$lock = true # lock
if !data['name'] || data['name'] == ""
$status = "Error: Name not given."
return
end
if data['key'] != SESSION_KEY
$status = "Error: Key mismatch (#{data['key']}, #{SESSION_KEY})"
return
end
if data["EDIT"]
$status = "Processing post-edit" # status
logmsg :server, "received post-edit"
# 0. save raw post-edit
source = data["source_value"]
post_edit = data["post_edit"]
$status = "Processing post-edit ..." # status
post_edit.strip!
post_edit.lstrip!
# fill db
$db['feedback'] << reply
$db['post_edits_raw'] << post_edit
$db['durations'] << data['duration'].to_f
$db['durations_rating'] << data['duration_rating'].to_f
$db['count_click'] << data['count_click'].to_i
$db['count_kbd'] << data['count_kbd'].to_i
$db['post_edits_display'] << send_recv(:detokenizer, post_edit)
$db['ratings'] << data['rating'].to_f
$last_processed_postedit = $db['post_edits_display'].last
# 1. tokenize
$status = "Tokenizing post-edit" # status
logmsg :server, "tokenizing post-edit"
post_edit = send_recv :tokenizer, post_edit
# 2. truecase
$status = "Truecasing post-edit" # status
logmsg :server, "truecasing post-edit"
post_edit = send_recv :truecaser, post_edit
# 3. bpe
$status = "BPE'ing post-edit"
logmsg :server, "bpe'ing post-edit"
post_edit = send_recv :bpe, post_edit
# 4. save processed post-edits
$status = "saving processed post-edit" # status
logmsg :db, "saving processed post-edit"
$db['post_edits'] << post_edit.strip
if data['nochange']
logmsg :server, "no change"
end
if !NOLEARN && !NOMT
$status = "Updating model" # status
logmsg :server, "updating ..."
$status = "Learning from post-edit" # status
send_recv :lamtram, "act:learn ||| #{source} ||| #{post_edit}"
$db['updated'] << true
end
logmsg :db, "updating database"
update_database
end
$status = "Getting next data to translate" # status
source = $db['source_segments'][$db['progress']]
raw_source = $db['raw_source_segments'][$db['progress']]
if !source # input is done
begin
stop_daemon :lamtram
rescue
end
logmsg :server, "end of input, sending 'fin'"
$lock = false
$status = "Ready" # status
$last_reply = {'fin'=>true,
'processed_postedit'=>$last_processed_postedit
}.to_json
return # return
elsif !$confirmed \
|| ($confirmed && $last_reply && $last_reply!="" \
&& !data["EDIT"]) # send last reply
logmsg :server, "locked, re-sending last reply"
logmsg :server, "last_reply: '#{$last_reply}'"
$lock = false
$status = "Ready" # status
return
else
source = source.strip.lstrip
raw_source = raw_source.strip.lstrip
if NOMT # 0. no mt?
$lock = false
logmsg :server, "no mt"
obj = Hash.new
obj["progress"] = $db["progress"]
obj["source"] = source
obj["raw_source"] = raw_source
obj["processed_postedit"] = $last_processed_postedit
$last_reply = obj.to_json
$status = "Ready" # status
return
end
$status = "Translating" # status
msg = "act:translate ||| #{source}"
obj = Hash.new
obj["transl_bpe"] = send_recv :lamtram, msg
obj["transl"] = send_recv(:truecaser, obj["transl_bpe"].gsub(/((@@ )|@@$)/, "")).strip
$status = "Processing raw translation" # status
obj["transl_detok"] = send_recv(:detokenizer, obj["transl"]).strip
obj["source"] = source
obj["progress"] = $db['progress']
obj["raw_source"] = raw_source
logmsg :server, "#{obj["transl_bpe"]}"
$db["mt_raw_bpe"] << obj["transl_bpe"]
$db["mt_raw"] << obj["transl"]
$db["mt"] << obj["transl_detok"]
obj["processed_postedit"] = $last_processed_postedit
# 5. reply
$last_reply = obj.to_json
$lock = false
$confirmed = false
logmsg :server, "response: '#{$last_reply}'"
$status = "Ready" # status
return # return
end
end
get '/fetch' do # fetch next
cross_origin
return "Locked" if $locked
return $last_reply
end
get '/fetch_processed_postedit/:i' do # processed post-edit
cross_origin
i = params[:i].to_i
return $db['post_edits_display'][i]
end
get '/progress' do # processed post-edit
cross_origin
return $db['progress']
end
get '/status' do # check status
cross_origin
logmsg :server, "status: #{$status}"
return "Locked" if $locked
return $status
end
get '/status_debug' do # check status
cross_origin
logmsg :server, "status: #{$status}"
return "[##{$db["progress"]}] Locked" if $locked
return "[##{$db["progress"]}] #{$status}"
end
get '/confirm' do # client confirms received translation
cross_origin
$confirmed = true
logmsg :server, "confirmed = #{$confirmed}"
return "#{$confirmed}"
end
get '/reset_progress' do # reset current session
return "locked" if $lock
$db = JSON.parse ReadFile.read DB_FILE
$db['post_edits'].clear
$db['post_edits_raw'].clear
$db['post_edits_display'].clear
$db['mt'].clear
$db['mt_raw'].clear
$db['updated'].clear
$db['durations'].clear
$db['durations_rating'].clear
$db['feedback'].clear
$db['progress'] = -1
$db['count_kbd'].clear
$db['count_click'].clear
$db['ratings'].clear
update_database true
$confirmed = true
$last_reply = nil
return "progress reset: done"
end
get '/shutdown' do # stop daemons and shut down server
logmsg :server, "shutting down daemons"
stop_all_daemons
return "stopped all daemons, ready to shutdown"
end
get '/summary' do
logmsg :server, "showing summary"
data = JSON.parse ReadFile.read(DB_FILE).force_encoding("UTF-8")
bleu_scores = []
data["post_edits"].each_with_index { |pe,j|
f = Tempfile.new "lfpe-summary-pe"
g = Tempfile.new "lfpe-summary-ref"
f.write pe+"\n"
g.write data["references"][j]+"\n"
f.close
g.close
bleu_scores << [1.0, (`#{CDEC}/mteval/fast_score -i #{f.path} -r #{g.path} -m ibm_bleu 2>/dev/null`.to_f).round(2)].min
f.unlink
g.unlink
}
ter_scores = []
data["post_edits"].each_with_index { |pe,j|
f = Tempfile.new "lfpe-summary-pe"
g = Tempfile.new "lfpe-summary-ref"
f.write pe.gsub("@@ ","")+"\n"
g.write data["references"][j]+"\n"
f.close
g.close
ter_scores << [1.0, (`#{CDEC}/mteval/fast_score -i #{f.path} -r #{g.path} -m ter 2>/dev/null`.to_f).round(2)].min
f.unlink
g.unlink
}
hter_scores = []
data["post_edits"].each_with_index { |pe,j|
f = Tempfile.new "lfpe-summary-mt"
g = Tempfile.new "lfpe-summary-pe"
f.write data["mt_raw"][j]+"\n"
g.write pe.gsub("@@ ", "")+"\n"
f.close
g.close
hter_scores << [1.0, (`#{CDEC}/mteval/fast_score -i #{f.path} -r #{g.path} -m ter 2>/dev/null`.to_f).round(2)].min
f.unlink
g.unlink
}
haml :summary, :locals => { :session_key => SESSION_KEY,
:data => data,
:bleu_scores => bleu_scores,
:ter_scores => ter_scores,
:hter_scores => hter_scores }
end
|