From 334c3820c673c9226513b69df93b43ac37308bd1 Mon Sep 17 00:00:00 2001 From: Patrick Simianer
Date: Wed, 10 Jun 2015 12:31:09 +0200
Subject: stable system
---
server.rb | 187 ++++++++++++++++++++++++++++++++++++++++++++++----------------
1 file changed, 140 insertions(+), 47 deletions(-)
(limited to 'server.rb')
diff --git a/server.rb b/server.rb
index a0dcd9c..fdc99fd 100755
--- a/server.rb
+++ b/server.rb
@@ -2,14 +2,40 @@
require 'sinatra'
require 'sinatra/cross_origin'
+require "sinatra/reloader"
require 'nanomsg'
require 'zipf'
require 'digest'
+require 'json'
+# load configuration file and setup global variables
require_relative "#{ARGV[0]}"
-INPUT = ReadFile.readlines INPUT_FILE
-INPUT_RAW = ReadFile.readlines RAW_INPUT_FILE
-`mkdir -p #{WORK_DIR}/g`
+$lock = false # lock if currently learning/translating
+$last_reply = nil # cache last reply
+$confirmed = true # client received translation?
+if !FileTest.exist? LOCK_FILE
+ $db = {} # FIXME: that is supposed to be a database connection
+ $env = {}
+end
+
+$daemons = {
+ :detokenizer => "/fast_scratch/simianer/lfpe/lfpe/de-tok.rb -a D -S '__ADDR__' -p #{SCRIPTS_DIR} -l #{TARGET_LANG}",
+ :tokenizer => "/fast_scratch/simianer/lfpe/lfpe/de-tok.rb -a T -S '__ADDR__' -p #{SCRIPTS_DIR} -l #{TARGET_LANG}",
+ :extractor => "python -m cdec.sa.extract -c #{DATA_DIR}/sa.ini --online -u -S '__ADDR__'",
+ :aligner_fwd => "#{CDEC_NET}/word-aligner/net_fa -f #{DATA_DIR}/a/forward.params -m #{FWD_MEAN_SRCLEN_MULT} -T #{FWD_TENSION} --sock_url '__ADDR__'",
+ :aligner_back => "#{CDEC_NET}/word-aligner/net_fa -f #{DATA_DIR}/a/backward.params -m #{BACK_MEAN_SRCLEN_MULT} -T #{BACK_TENSION} --sock_url '__ADDR__'",
+ :atools => "#{CDEC_NET}/utils/atools_net -c grow-diag-final-and -S '__ADDR__'",
+ :dtrain => "#{CDEC_NET}/training/dtrain/dtrain_net_interface -c #{DATA_DIR}/dtrain.ini -o #{WORK_DIR}/weights.final -a '__ADDR__'"
+}
+
+# setup Sinatra
+set :bind, SERVER_IP
+set :port, WEB_PORT
+set :allow_origin, :any
+set :allow_methods, [:get, :post, :options]
+set :allow_credentials, true
+set :max_age, "1728000"
+set :expose_headers, ['Content-Type']
def start_daemon cmd, name, addr
STDERR.write "> starting #{name} daemon\n"
@@ -24,112 +50,179 @@ def start_daemon cmd, name, addr
return sock, pid
end
-def stop_all_daemons env
+def stop_all_daemons
STDERR.write "shutting down all daemons\n"
- env.each { |name,p|
+ $env.each { |name,p|
p[:socket].send "shutdown"
STDERR.write "< #{name} is #{p[:socket].recv}\n"
}
end
-daemons = {
- :extractor => "python -m cdec.sa.extract -c #{DATA_DIR}/sa.ini --online -u -S '__ADDR__'",
- :aligner_fwd => "#{CDEC_NET}/word-aligner/net_fa -f #{DATA_DIR}/a/forward.params -m #{FWD_MEAN_SRCLEN_MULT} -T #{FWD_TENSION} --sock_url '__ADDR__'",
- :aligner_back => "#{CDEC_NET}/word-aligner/net_fa -f #{DATA_DIR}/a/backward.params -m #{BACK_MEAN_SRCLEN_MULT} -T #{BACK_TENSION} --sock_url '__ADDR__'",
- :atools => "#{CDEC_NET}/utils/atools_net -c grow-diag-final-and -S '__ADDR__'",
- :dtrain => "#{CDEC_NET}/training/dtrain/dtrain_net_interface -c #{DATA_DIR}/dtrain.ini -o #{WORK_DIR}/weights.final -a '__ADDR__'" ##{DTRAIN_EXTRA}"
-}
+def update_database # FIXME: real database
+ $db['progress'] += 1
+ j = JSON.generate $db
+ f = WriteFile.new DB_FILE
+ f.write j.to_s
+ f.close
+end
-env = {}
-port = BEGIN_PORT_RANGE
-daemons.each { |name,cmd|
- sock, pid = start_daemon cmd, name, "tcp://127.0.0.1:#{port}"
- env[name] = { :socket => sock, :pid => pid }
- port += 1
-}
+def init
+ # database connection
+ $db = JSON.parse ReadFile.read DB_FILE
+ # working directory
+ `mkdir -p #{WORK_DIR}/g`
+ # setup environment, start daemons
+ port = BEGIN_PORT_RANGE
+ $daemons.each { |name,cmd|
+ sock, pid = start_daemon cmd, name, "tcp://127.0.0.1:#{port}"
+ $env[name] = { :socket => sock, :pid => pid }
+ port += 1
+ }
+ `touch #{LOCK_FILE}`
+end
-set :bind, SERVER_IP
-set :port, WEB_PORT
-set :allow_origin, :any
-set :allow_methods, [:get, :post, :options]
-set :allow_credentials, true
-set :max_age, "1728000"
-set :expose_headers, ['Content-Type']
+init if !FileTest.exist?(LOCK_FILE)
get '/' do
cross_origin
"Nothing to see here."
end
+# receive post-edit, send translation
get '/next' do
cross_origin
+ return "locked" if $lock
+ $lock = true
+ key = params[:key] # FIXME: do something with it
if params[:example]
source, reference = params[:example].strip.split(" ||| ")
+ # tokenize, lowercase
+ $db['post_edits_raw'] << reference.strip
+ $env[:tokenizer][:socket].send reference
+ STDERR.write "[tokenizer] waiting ...\n"
+ reference = $env[:tokenizer][:socket].recv.force_encoding("UTF-8").strip
+ STDERR.write "[tokenizer] < received tokenized reference: '#{reference}'\n"
+ reference.downcase!
+ # save post-edits
+ $db['post_edits'] << reference.strip
# update weights
grammar = "#{WORK_DIR}/g/#{Digest::SHA256.hexdigest(source)}.grammar"
annotated_source = "