w2v-server: cache results (in memory, for now)
diff --git a/w2v-server.pl b/w2v-server.pl
index a71ee6e..85a7e25 100755
--- a/w2v-server.pl
+++ b/w2v-server.pl
@@ -23,6 +23,7 @@
my %marked;
my $training_args="";
my $mergedEnd=0;
+my %cache;
getopts('d:Gil:p:m:n:M:');
@@ -87,15 +88,21 @@
$c->inactivity_timeout(300);
$word =~ s/\s+/ /g;
for my $w (split(' *\| *', $word)) {
- $c->app->log->info('Looking for neighbours of '.$w);
- if($opt_i) {
- $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst);
+ if ($cache{$w}) {
+ $c->app->log->info("Getting $w results from cache");
+ $res = $cache{$w}
} else {
- $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst);
+ $c->app->log->info('Looking for neighbours of '.$w);
+ if($opt_i) {
+ $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst);
+ } else {
+ $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst);
+ }
+ $cache{$w} = $res;
}
push(@lists, $res->{paradigmatic});
- }
- }
+ }
+ }
$word =~ s/ *\| */ | /g;
if($json) {
return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});