mosesdecoder/contrib/iSenWeb/moses.pl
2012-07-09 10:27:28 +01:00

60 lines
1.8 KiB
Perl
Executable File

#!/usr/bin/perl -w
use warnings;
use strict;
$|++;
# file: daemon.pl
# Herve Saint-Amand
# Universitaet des Saarlandes
# Tue May 13 19:45:31 2008
# This script starts Moses to run in the background, so that it can be used by
# the CGI script. It spawns the Moses process, then binds itself to listen on
# some port, and when it gets a connection, reads it line by line, feeds those
# to Moses, and sends back the translation.
# You can either run one instance of this on your Web server, or, if you have
# the hardware setup for it, run several instances of this, then configure
# translate.cgi to connect to these.
#------------------------------------------------------------------------------
# includes
use IO::Socket::INET;
use IPC::Open2;
#------------------------------------------------------------------------------
# constants, global vars, config
my $MOSES = '/home/tianliang/research/moses-smt/scripts/training/model/moses';
my $MOSES_INI = '/home/tianliang/research/moses-smt/scripts/training/model/moses.ini';
die "usage: daemon.pl <hostname> <port>" unless (@ARGV == 2);
my $LISTEN_HOST = shift;
my $LISTEN_PORT = shift;
#------------------------------------------------------------------------------
# main
# spawn moses
my ($MOSES_IN, $MOSES_OUT);
my $pid = open2 ($MOSES_OUT, $MOSES_IN, $MOSES, '-f', $MOSES_INI);
# open server socket
my $server_sock = new IO::Socket::INET
(LocalAddr => $LISTEN_HOST, LocalPort => $LISTEN_PORT, Listen => 1)
|| die "Can't bind server socket";
while (my $client_sock = $server_sock->accept) {
while (my $line = <$client_sock>) {
print $MOSES_IN $line;
$MOSES_IN->flush ();
print $client_sock scalar <$MOSES_OUT>;
}
$client_sock->close ();
}
#------------------------------------------------------------------------------