summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2015-12-11 15:46:41 +0100
committerPatrick Simianer <p@simianer.de>2015-12-11 15:46:41 +0100
commit3424f3867f8e047a8385a4c1a554b858425cdd7c (patch)
tree28bff542792efbcd3a3f6e84fbdcb24c7a605ad2 /util
parent3800c61db7395ca2e15a5273aa9a51811ebc5038 (diff)
renamed wrapper.rb
Diffstat (limited to 'util')
-rwxr-xr-xutil/nanomsg_wrapper.rb51
1 files changed, 51 insertions, 0 deletions
diff --git a/util/nanomsg_wrapper.rb b/util/nanomsg_wrapper.rb
new file mode 100755
index 0000000..d0e6ca7
--- /dev/null
+++ b/util/nanomsg_wrapper.rb
@@ -0,0 +1,51 @@
+#!/usr/bin/env ruby
+
+require 'nanomsg'
+require 'open3'
+require 'trollop'
+
+conf = Trollop::options do
+ opt :action, "tokenize, detokenize, truecase, or lowercase", :short => "-a", :type => :string, :required => true
+ opt :addr, "socket address", :short => "-S", :type => :string, :required => true
+ opt :ext, "path to externals", :short => "-e", :type => :string, :required => true
+ opt :lang, "language", :short => "-l", :type => :string
+ opt :truecase_model, "model file for truecaser", :short => "-t", :type => :string
+end
+
+sock = NanoMsg::PairSocket.new
+sock.bind conf[:addr]
+sock.send "hello"
+
+if conf[:action] == "detokenize"
+ cmd = "#{conf[:ext]}/detokenizer.perl -q -b -u -l #{conf[:lang]}"
+ if !conf[:lang]
+ STDERR.write "[detokenizer] No language given, exiting!\n"; exit
+ end
+elsif conf[:action] == "tokenize"
+ cmd = "#{conf[:ext]}/tokenizer-no-escape.perl -q -b -l #{conf[:lang]}"
+ if !conf[:lang]
+ STDERR.write "[tokenizer] No language given, exiting!\n"; exit
+ end
+elsif conf[:action] == "truecase"
+ cmd = "#{conf[:ext]}/truecase.perl -b --model #{conf[:truecase_model]}"
+ if !conf[:truecase_model]
+ STDERR.write "[truecaser] No model given for truecaser, exiting!\n"; exit
+ end
+elsif conf[:action] == "lowercase"
+ cmd = "#{conf[:ext]}/lowercase.perl"
+else
+ STDERR.write "[wrapper] Unknown action #{conf[:action]}, exiting!\n"; exit
+end
+pin, pout, perr = Open3.popen3(cmd)
+while true
+ inp = sock.recv.strip
+ break if !inp||inp=="shutdown"
+ pin.write inp+"\n"
+ sock.send pout.gets.strip
+end
+
+STDERR.write "[wrapper] shutting down\n"
+pin.close; pout.close; perr.close
+sock.send "off"
+exit 0
+