blob: 70731834d49907b6faae48651053f02638b6dd8e [file] [log] [blame]
Marc Kupietzdc22b982015-10-09 09:19:34 +02001#!/usr/local/bin/perl
Marc Kupietzf11d20c2019-08-02 15:42:04 +02002use Inline C => "./w2v-server.c" => CLEAN_AFTER_BUILD => 0, BUILD_NOISY => 1, ccflags => $Config{ccflags}." -I/vol/work/kupietz/Work2/kl/trunk/CollocatorDB -I, -L. -Wall -O4", libs => "-shared -lpthread -lcollocatordb -lrt -lsnappy -lz -lbz2 -llz4 -lzstd -lrocksdb -lgomp";
Marc Kupietza5f60042017-05-04 10:38:12 +02003#use Inline C => Config => BUILD_NOISY => 1, CFLAGS => $Config{cflags}." -O4 -mtune k9";
4#use Inline C => Config => CLEAN_AFTER_BUILD => 0, ccflags => $Config{ccflags}." -Ofast -march k8 -mtune k8 ";
Marc Kupietzdc22b982015-10-09 09:19:34 +02005use Mojolicious::Lite;
Marc Kupietzc4893362016-02-25 08:04:46 +01006use Mojo::JSON qw(decode_json encode_json to_json);
Marc Kupietz30ca4342017-11-22 21:21:20 +01007use base 'Mojolicious::Plugin';
8
Marc Kupietz247500f2015-10-09 11:29:01 +02009use Encode qw(decode encode);
Marc Kupietza5b90152016-03-15 17:39:19 +010010use Getopt::Std;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010011use Mojo::Server::Daemon;
Marc Kupietzffef9302017-11-07 15:58:01 +010012use Cwd;
Marc Kupietz66bfd952017-12-11 09:59:45 +010013
Marc Kupietzffef9302017-11-07 15:58:01 +010014app->static->paths->[0] = getcwd;
15
Marc Kupietzd4227392016-03-01 16:45:12 +010016plugin 'Log::Access';
Marc Kupietzb3422c12017-07-04 14:12:11 +020017plugin "RequestBase";
Marc Kupietz95104512019-12-05 10:13:05 +010018#plugin 'AutoReload';
19plugin Localize => {
20 dict => {
21 _ => sub { $_->locale },
22 },
23 resources => ['w2v-server.dict']
24};
Marc Kupietza5b90152016-03-15 17:39:19 +010025our $opt_i = 0; # latin1-input?
26our $opt_l = undef;
27our $opt_p = 5676;
Marc Kupietza2e64502016-04-27 09:53:51 +020028our $opt_m;
Marc Kupietz6ed81872016-04-27 14:04:04 +020029our $opt_M;
Marc Kupietz43ee87e2016-04-25 10:50:08 +020030our $opt_n = '';
31our $opt_d;
Marc Kupietzfa194262018-06-05 09:39:32 +020032our $opt_D;
Marc Kupietz5c3887d2016-04-28 08:53:35 +020033our $opt_G;
Marc Kupietza5b90152016-03-15 17:39:19 +010034
Marc Kupietz6ed81872016-04-27 14:04:04 +020035my %marked;
Marc Kupietzc053d972019-01-10 10:41:51 +010036my $title="";
Marc Kupietz793413b2016-04-02 21:48:57 +020037my $training_args="";
Marc Kupietza2e64502016-04-27 09:53:51 +020038my $mergedEnd=0;
Marc Kupietz15987412017-11-07 15:56:58 +010039my %cache;
Marc Kupietz19c68242018-03-12 09:42:21 +010040my %cccache; # classic collocator cache
Marc Kupietza51dcfa2018-03-19 16:22:05 +010041my %spcache; # similar profile cache
Marc Kupietz793413b2016-04-02 21:48:57 +020042
Marc Kupietzfa194262018-06-05 09:39:32 +020043getopts('d:D:Gil:p:m:n:M:');
Marc Kupietz6ed81872016-04-27 14:04:04 +020044
45if($opt_M) {
Marc Kupietzed930212016-04-27 15:42:38 +020046 open my $handle, '<:encoding(UTF-8)', $opt_M
47 or die "Can't open '$opt_M' for reading: $!";
48 while(<$handle>) {
Marc Kupietz6ed81872016-04-27 14:04:04 +020049 foreach my $mw (split /\s+/) {
50 $marked{$mw}=1
51 }
52 }
Marc Kupietzed930212016-04-27 15:42:38 +020053 close($handle);
Marc Kupietz6ed81872016-04-27 14:04:04 +020054}
Marc Kupietza5b90152016-03-15 17:39:19 +010055
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010056# -cbow 1 -size 200 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 40 -binary 1 -iter 15
Marc Kupietza5b90152016-03-15 17:39:19 +010057if(!$ARGV[0]) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010058 init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0));
Marc Kupietz2cb667e2016-03-10 09:44:12 +010059} else {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010060 init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0));
Marc Kupietz793413b2016-04-02 21:48:57 +020061 if(open(FILE, "$ARGV[0].args")) {
62 $training_args = <FILE>;
63 }
64 close(FILE);
Marc Kupietzc053d972019-01-10 10:41:51 +010065 $title = fname2corpusname($ARGV[0]);
Marc Kupietz2cb667e2016-03-10 09:44:12 +010066}
Marc Kupietzdc22b982015-10-09 09:19:34 +020067
Marc Kupietza51dcfa2018-03-19 16:22:05 +010068my $have_sprofiles = load_sprofiles($ARGV[0]);
69
Marc Kupietza2e64502016-04-27 09:53:51 +020070if($opt_m) {
71 $mergedEnd = mergeVectors($opt_m);
Marc Kupietzc053d972019-01-10 10:41:51 +010072 $title = "<span class=\"merged\">" . $title . "</span> vs. " . fname2corpusname($opt_m);
Marc Kupietza2e64502016-04-27 09:53:51 +020073}
74
Marc Kupietze5568a02018-12-20 11:42:02 +010075
Marc Kupietz43ee87e2016-04-25 10:50:08 +020076if($opt_d) { # -d: dump vecs and exit
77 dump_vecs($opt_d);
78 exit;
79}
80
Marc Kupietzfa194262018-06-05 09:39:32 +020081if($opt_D) { # -D: dump vecs for numpy and exit
82 dump_for_numpy($opt_D);
83 exit;
84}
85
Marc Kupietza5b90152016-03-15 17:39:19 +010086my $daemon = Mojo::Server::Daemon->new(
87 app => app,
88 listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"]
89);
90
Marc Kupietz5c3887d2016-04-28 08:53:35 +020091if($opt_G) {
92 print "Filtering garbage\n";
93 filter_garbage();
94}
95
Marc Kupietz554aff52017-11-09 14:42:09 +010096get '*/js/*' => sub {
Marc Kupietzffef9302017-11-07 15:58:01 +010097 my $c = shift;
98 my $url = $c->req->url;
99 $url =~ s@/derekovecs@@g;
100 $c->app->log->info("GET: " . $url);
101 $c->reply->static($url);
102};
103
Marc Kupietza9270572018-03-17 15:17:07 +0100104get '*/css/*' => sub {
105 my $c = shift;
106 my $url = $c->req->url;
107 $url =~ s@/derekovecs/@/@g;
108 $c->app->log->info("GET: " . $url);
109 $c->reply->static($url);
110};
111
Marc Kupietzc053d972019-01-10 10:41:51 +0100112sub fname2corpusname {
113 ($_) = @_;
114 s@.*/@@;
Marc Kupietz86b50292019-02-17 21:03:59 +0100115 s@\.en@-en@;
Marc Kupietzc053d972019-01-10 10:41:51 +0100116 s@\..*@@;
117 return $_;
118}
119
Marc Kupietzcb43e492019-12-03 10:07:53 +0100120sub getWord {
121 ($_) = @_;
122 if ($_ =~ /^\d+/) {
123 return $_;
124 } else {
125 return getWordNumber($_);
126 }
127}
128
Marc Kupietz19c68242018-03-12 09:42:21 +0100129sub getClassicCollocatorsCached {
130 my ($c, $word) = @_;
Marc Kupietz81aeed22019-02-17 21:22:45 +0100131 my $s2 = "";
Marc Kupietz9ff3c992019-02-04 12:32:54 +0100132 if($word > $mergedEnd) {
133 $word-=$mergedEnd;
134 }
Marc Kupietz81aeed22019-02-17 21:22:45 +0100135
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100136 if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference
Marc Kupietz9ee3f412019-08-02 14:58:19 +0200137 open PIPE, "GET http://compute:5673/getClassicCollocators?w=$word |" or
138 open PIPE, "GET http://klinux10:5673/getClassicCollocators?w=$word |";
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100139 }
Marc Kupietz19c68242018-03-12 09:42:21 +0100140 if(!$cccache{$word}) {
Marc Kupietz06d61292019-02-04 12:33:22 +0100141 $c->app->log->info("Getting classic collocates of $word.");
Marc Kupietz19c68242018-03-12 09:42:21 +0100142 $cccache{$word} = getClassicCollocators($word);
Marc Kupietz1d96a082019-02-18 09:29:06 +0100143 $cccache{$word} =~ s/:(-?)(nan|inf)/:"${1}${2}"/g;
Marc Kupietz19c68242018-03-12 09:42:21 +0100144 } else {
Marc Kupietz06d61292019-02-04 12:33:22 +0100145 $c->app->log->info("Getting classic collocates for $word from cache.");
Marc Kupietz19c68242018-03-12 09:42:21 +0100146 }
Marc Kupietz81aeed22019-02-17 21:22:45 +0100147 if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100148 while(<PIPE>) {
149 $s2 .= $_;
150 }
151 close(PIPE);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100152 }
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100153
Marc Kupietz81aeed22019-02-17 21:22:45 +0100154 if(length($s2) > 2000) {
155 my $d1 = decode_json($cccache{$word});
156 my $d2 = decode_json($s2);
157 my %d2ld;
Marc Kupietz001bffd2019-02-21 08:52:41 +0100158 my $minLd = 14;
Marc Kupietz81aeed22019-02-17 21:22:45 +0100159 foreach my $i (@{$d2->{collocates}}) {
160 $d2ld{$i->{word}}=$i->{ld};
Marc Kupietz001bffd2019-02-21 08:52:41 +0100161 $minLd=$i->{ld} if($i->{ld} < $minLd);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100162 }
163 foreach my $i (@{$d1->{collocates}}) {
164 my $w = $i->{word};
Marc Kupietz001bffd2019-02-21 08:52:41 +0100165 $i->{delta} = $i->{ld} - (defined $d2ld{$w} ? $d2ld{$w} : $minLd-0.1);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100166 }
167 return(encode_json($d1));
168 } else {
169 my $d1 = decode_json($cccache{$word});
170 foreach my $i (@{$d1->{collocates}}) {
171 $i->{delta} = 0;
172 }
173 return(encode_json($d1));
174 }
Marc Kupietz19c68242018-03-12 09:42:21 +0100175}
176
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100177sub getSimilarProfilesCached {
178 my ($c, $word) = @_;
179 if(!$spcache{$word}) {
180 $spcache{$word} = getSimilarProfiles($word);
181 } else {
182 $c->app->log->info("Getting similar profiles for $word from cache:");
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100183 }
184 return $spcache{$word};
185}
186
Marc Kupietz66bfd952017-12-11 09:59:45 +0100187post '/derekovecs/getVecsByRanks' => sub {
188 my $self = shift;
189 my $vec = getVecs($self->req->json);
190 $self->render(json => $vec);
191};
192
Marc Kupietze13a3552018-01-25 08:48:34 +0100193any '*/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100194 my $self = shift;
Marc Kupietzcb43e492019-12-03 10:07:53 +0100195 $self->render(data => getClassicCollocatorsCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100196};
197
Marc Kupietze13a3552018-01-25 08:48:34 +0100198any '/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100199 my $self = shift;
Marc Kupietzcb43e492019-12-03 10:07:53 +0100200 $self->render(data => getClassicCollocatorsCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100201};
202
Marc Kupietzd7760b42019-02-21 09:01:44 +0100203any '/getBiggestVocabDistances' => sub {
204 my $self = shift;
205 $self->render(data => getBiggestMergedDifferences(), format=>'json');
206};
207
208any '*/getBiggestVocabDistances' => sub {
209 my $self = shift;
210 $self->render(data => getBiggestMergedDifferences(), format=>'json');
211};
212
Marc Kupietz33c79d32019-08-02 15:11:23 +0200213any '*/getPosWiseW2VCollocators' => sub {
214 my $self = shift;
215 $self->render(data => getPosWiseW2VCollocatorsAsTsv($self->param("w"),
216 ($self->param("max")? $self->param("max") : 200),
217 ($self->param("cutoff")? $self->param("cutoff") :750000),
218 ($self->param("threshold")? $self->param("threshold") : 0.2)),
219 format=>'tsv');
220};
221
222any '/getPosWiseW2VCollocators' => sub {
223 my $self = shift;
224 $self->render(data => getPosWiseW2VCollocatorsAsTsv($self->param("w"),
225 ($self->param("max")? $self->param("max") : 200),
226 ($self->param("cutoff")? $self->param("cutoff") : 750000),
227 ($self->param("threshold")? $self->param("threshold") : 0.2)),
228 format=>'tsv');
229};
230
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100231any '*/getSimilarProfiles' => sub {
232 my $self = shift;
Marc Kupietzcb43e492019-12-03 10:07:53 +0100233 $self->render(data => getSimilarProfilesCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json');
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100234};
235
Marc Kupietzc987fa82018-03-21 12:14:25 +0100236any '/getSimilarProfiles' => sub {
237 my $self = shift;
Marc Kupietzcb43e492019-12-03 10:07:53 +0100238 $self->render(data => getSimilarProfilesCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json');
Marc Kupietzc987fa82018-03-21 12:14:25 +0100239};
240
Marc Kupietz98ed1c02019-08-02 15:05:37 +0200241any '/getSimilarity' => sub {
242 my $self = shift;
243 my $w1 = $self->param("w1");
244 my $w2 = $self->param("w2");
245 $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json');
246};
247
248any '*/getSimilarity' => sub {
249 my $self = shift;
250 my $w1 = $self->param("w1");
251 my $w2 = $self->param("w2");
252 $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json');
253};
254
Marc Kupietzdf3d4b52017-11-29 16:57:27 +0100255get '*/img/*' => sub {
256 my $c = shift;
257 my $url = $c->req->url;
258 $url =~ s@/derekovecs@@g;
259 $c->app->log->info("GET: " . $url);
260 $c->reply->static($url);
261};
262
Marc Kupietzdc22b982015-10-09 09:19:34 +0200263get '/' => sub {
264 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +0200265 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200266 my $word=$c->param('word');
Marc Kupietz2da2a812019-02-21 14:17:35 +0100267 my $no_nbs=$c->param('n') || ($opt_m? 50 : 100);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100268 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +0100269 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100270 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100271 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200272 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100273 my $sort=$c->param('sort') || 0;
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100274 my $csv=$c->param('csv') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +0200275 my $json=$c->param('json') || 0;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100276 my $cutoff=$c->param('cutoff') || 500000;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100277 my $dedupe=$c->param('dedupe') || 0;
Marc Kupietzac707b32018-12-20 11:36:38 +0100278 my $nosp=$c->param('nosp') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100279 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100280 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100281 my @collocations;
Marc Kupietzcddc8482019-12-04 08:57:33 +0100282 if(defined($word) && $word !~ /^\s*$/) {
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100283 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100284 $word =~ s/\s+/ /g;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100285 if($opt_m && $word !~ /\|/) {
286 $word .= "|$word";
287 }
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100288 for my $w (split(' *\| *', $word)) {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100289 if($opt_m) {
290 if($searchBaseVocabFirst) {
291 $searchBaseVocabFirst=0;
292 } else {
293 $searchBaseVocabFirst=1;
294 }
295 }
296 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe,$searchBaseVocabFirst}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100297 $c->app->log->info("Getting $w results from cache");
Marc Kupietz3082fd02019-01-09 14:54:06 +0100298 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe.$searchBaseVocabFirst}
Marc Kupietza5b90152016-03-15 17:39:19 +0100299 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100300 $c->app->log->info('Looking for neighbours of '.$w);
301 if($opt_i) {
Marc Kupietzac707b32018-12-20 11:36:38 +0100302 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100303 } else {
Marc Kupietzac707b32018-12-20 11:36:38 +0100304 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100305 }
Marc Kupietz2dd2dd72017-12-01 22:08:14 +0100306 $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100307 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100308 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100309 }
310 }
Marc Kupietz56844a22019-08-02 15:12:19 +0200311
Marc Kupietz000ad862016-02-26 14:59:12 +0100312 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200313 if($json) {
314 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100315 } elsif($csv) {
316 my $csv_data="";
317 for (my $i=0; $i <= $no_nbs; $i++) {
318 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
319 }
320 for (my $i=0; $i < $no_nbs; $i++) {
321 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
322 }
323 chop $csv_data;
324 chop $csv_data;
325 $csv_data .= "\n";
326 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200327 } else {
Marc Kupietzd7760b42019-02-21 09:01:44 +0100328 my $distantWords="";
329 if(!defined($word) || $word !~ /^\s*$/) {
330 $distantWords = getBiggestMergedDifferences();
331 }
332 $c->render(template=>"index", title=>$title, word=>$word, distantWords=>$distantWords, cutoff=>$cutoff, no_nbs=>$no_nbs, no_iterations => $no_iterations, epsilon=> $epsilon, perplexity=> $perplexity, show_som=>$som, searchBaseVocabFirst=>$searchBaseVocabFirst, sort=>$sort, training_args=>$training_args, mergedEnd=> $mergedEnd, haveSProfiles=> $have_sprofiles, dedupe=> $dedupe, marked=>\%marked, lists=> \@lists, collocators=> $res->{syntagmatic});
Marc Kupietzb613b052016-04-28 14:11:59 +0200333 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200334};
335
Marc Kupietz30ca4342017-11-22 21:21:20 +0100336helper(bitvec2window => sub {
337 my ($self, $n) = @_;
338 my $str = unpack("B32", pack("N", $n));
339 $str =~ s/^\d{22}//;
340 $str =~ s/^(\d{5})/$1x/;
341 $str =~ s/0/ยท/g;
342 $str =~ s/1/+/g;
343 return $str;
344 });
345
Marc Kupietz95104512019-12-05 10:13:05 +0100346$daemon->run;
347# app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200348
Marc Kupietz95104512019-12-05 10:13:05 +0100349# exit;