mosesdecoder/contrib/lmserver/examples/LMClient.pm
Barry Haddow 5e2731619c Move projects from top level to contrib
server build probably broken
2011-11-23 14:48:50 +00:00

38 lines
763 B
Perl

package LMClient;
use IO::Socket;
sub new {
my ($class, $cstr) = @_;
my $self = {};
$cstr =~ s/^!//;
my ($host, $port) = split /\:/, $cstr;
die "Please specify connection string as host:port" unless ($host && $port);
$self->{'SOCK'} = new IO::Socket::INET(
PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp') or die "Couldn't create connection to $host:$port -- is memcached running?\n";
bless $self, $class;
return $self;
}
sub word_prob {
my ($self, $word, $context) = @_;
my @cwords = reverse split /\s+/, $context;
my $qstr = "prob $word @cwords";
my $s = $self->{'SOCK'};
print $s "$qstr\r\n";
my $r = <$s>;
my $x= unpack "f", $r;
return $x;
}
sub close {
my ($self) = @_;
close $self->{'SOCK'};
}
1;