blob: 78f1e03ab38f16019852a0aa71d36ce11d5e3048 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
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;
|