Check in new version of Web translator (written in Wandlitz)

git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/trunk@1804 1f5c12ca-751b-0410-a591-d2e778427230
This commit is contained in:
saintamh 2008-05-29 10:12:45 +00:00
parent cf0f22dd93
commit 0feddfc436
2 changed files with 112 additions and 0 deletions

51
web/lib/RemoteProcess.pm Normal file
View File

@ -0,0 +1,51 @@
# file: RemoteProcess.pm
# Herve Saint-Amand
# Universitaet des Saarlandes
# Thu May 15 08:30:19 2008
#------------------------------------------------------------------------------
# includes
package RemoteProcess;
our @ISA = qw/Subprocess/;
use warnings;
use strict;
use IO::Socket::INET;
use Subprocess;
#------------------------------------------------------------------------------
# constructor
sub new {
my ($class, $host, $port) = @_;
my $self = new Subprocess;
$self->{host} = $host;
$self->{port} = $port;
$self->{sock} = undef;
bless $self, $class;
}
#------------------------------------------------------------------------------
# should have the same interface as Subprocess.pm
sub start {
my ($self) = @_;
$self->{sock} = new IO::Socket::INET (%{{
PeerAddr => $self->{host},
PeerPort => $self->{port},
}}) || die "Can't connect to $self->{host}:$self->{port}";
$self->{child_in} = $self->{child_out} = $self->{sock};
}
#------------------------------------------------------------------------------
1;

61
web/lib/Subprocess.pm Normal file
View File

@ -0,0 +1,61 @@
# file: Subprocess.pm
# Herve Saint-Amand
# Universitaet des Saarlandes
# Wed May 14 09:55:46 2008
# NOTE that to use this with Philipp Koehn's tokenizer.perl I had to modify
# that script to autoflush its streams, by adding a '$|++' to it
#------------------------------------------------------------------------------
# includes
package Subprocess;
use warnings;
use strict;
use Encode;
use IPC::Open2;
#------------------------------------------------------------------------------
# constructor
sub new {
my ($class, @cmd) = @_;
bless {
cmd => \@cmd,
num_done => 0,
child_in => undef,
child_out => undef,
}, $class;
}
#------------------------------------------------------------------------------
sub start {
my ($self) = @_;
open2 ($self->{child_out}, $self->{child_in}, @{$self->{cmd}});
}
sub do_line {
my ($self, $line) = @_;
my ($in, $out) = ($self->{child_in}, $self->{child_out});
$line =~ s/\s+/ /g;
print $in encode ('UTF-8', $line), "\n";
$in->flush ();
my $ret = decode ('UTF-8', scalar <$out>);
chomp $ret;
$self->{num_done}++;
return $ret;
}
sub num_done { shift->{num_done} }
#------------------------------------------------------------------------------
1;