blob: f85748a56bc9b99d31f6971005a80eecb00c321f [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 Kupietzdc22b982015-10-09 09:19:34 +020018
Marc Kupietza5b90152016-03-15 17:39:19 +010019our $opt_i = 0; # latin1-input?
20our $opt_l = undef;
21our $opt_p = 5676;
Marc Kupietza2e64502016-04-27 09:53:51 +020022our $opt_m;
Marc Kupietz6ed81872016-04-27 14:04:04 +020023our $opt_M;
Marc Kupietz43ee87e2016-04-25 10:50:08 +020024our $opt_n = '';
25our $opt_d;
Marc Kupietzfa194262018-06-05 09:39:32 +020026our $opt_D;
Marc Kupietz5c3887d2016-04-28 08:53:35 +020027our $opt_G;
Marc Kupietza5b90152016-03-15 17:39:19 +010028
Marc Kupietz6ed81872016-04-27 14:04:04 +020029my %marked;
Marc Kupietzc053d972019-01-10 10:41:51 +010030my $title="";
Marc Kupietz793413b2016-04-02 21:48:57 +020031my $training_args="";
Marc Kupietza2e64502016-04-27 09:53:51 +020032my $mergedEnd=0;
Marc Kupietz15987412017-11-07 15:56:58 +010033my %cache;
Marc Kupietz19c68242018-03-12 09:42:21 +010034my %cccache; # classic collocator cache
Marc Kupietza51dcfa2018-03-19 16:22:05 +010035my %spcache; # similar profile cache
Marc Kupietz793413b2016-04-02 21:48:57 +020036
Marc Kupietzfa194262018-06-05 09:39:32 +020037getopts('d:D:Gil:p:m:n:M:');
Marc Kupietz6ed81872016-04-27 14:04:04 +020038
39if($opt_M) {
Marc Kupietzed930212016-04-27 15:42:38 +020040 open my $handle, '<:encoding(UTF-8)', $opt_M
41 or die "Can't open '$opt_M' for reading: $!";
42 while(<$handle>) {
Marc Kupietz6ed81872016-04-27 14:04:04 +020043 foreach my $mw (split /\s+/) {
44 $marked{$mw}=1
45 }
46 }
Marc Kupietzed930212016-04-27 15:42:38 +020047 close($handle);
Marc Kupietz6ed81872016-04-27 14:04:04 +020048}
Marc Kupietza5b90152016-03-15 17:39:19 +010049
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010050# -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 +010051if(!$ARGV[0]) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010052 init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0));
Marc Kupietz2cb667e2016-03-10 09:44:12 +010053} else {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010054 init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0));
Marc Kupietz793413b2016-04-02 21:48:57 +020055 if(open(FILE, "$ARGV[0].args")) {
56 $training_args = <FILE>;
57 }
58 close(FILE);
Marc Kupietzc053d972019-01-10 10:41:51 +010059 $title = fname2corpusname($ARGV[0]);
Marc Kupietz2cb667e2016-03-10 09:44:12 +010060}
Marc Kupietzdc22b982015-10-09 09:19:34 +020061
Marc Kupietza51dcfa2018-03-19 16:22:05 +010062my $have_sprofiles = load_sprofiles($ARGV[0]);
63
Marc Kupietza2e64502016-04-27 09:53:51 +020064if($opt_m) {
65 $mergedEnd = mergeVectors($opt_m);
Marc Kupietzc053d972019-01-10 10:41:51 +010066 $title = "<span class=\"merged\">" . $title . "</span> vs. " . fname2corpusname($opt_m);
Marc Kupietza2e64502016-04-27 09:53:51 +020067}
68
Marc Kupietze5568a02018-12-20 11:42:02 +010069
Marc Kupietz43ee87e2016-04-25 10:50:08 +020070if($opt_d) { # -d: dump vecs and exit
71 dump_vecs($opt_d);
72 exit;
73}
74
Marc Kupietzfa194262018-06-05 09:39:32 +020075if($opt_D) { # -D: dump vecs for numpy and exit
76 dump_for_numpy($opt_D);
77 exit;
78}
79
Marc Kupietza5b90152016-03-15 17:39:19 +010080my $daemon = Mojo::Server::Daemon->new(
81 app => app,
82 listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"]
83);
84
Marc Kupietz5c3887d2016-04-28 08:53:35 +020085if($opt_G) {
86 print "Filtering garbage\n";
87 filter_garbage();
88}
89
Marc Kupietz554aff52017-11-09 14:42:09 +010090get '*/js/*' => sub {
Marc Kupietzffef9302017-11-07 15:58:01 +010091 my $c = shift;
92 my $url = $c->req->url;
93 $url =~ s@/derekovecs@@g;
94 $c->app->log->info("GET: " . $url);
95 $c->reply->static($url);
96};
97
Marc Kupietza9270572018-03-17 15:17:07 +010098get '*/css/*' => sub {
99 my $c = shift;
100 my $url = $c->req->url;
101 $url =~ s@/derekovecs/@/@g;
102 $c->app->log->info("GET: " . $url);
103 $c->reply->static($url);
104};
105
Marc Kupietzc053d972019-01-10 10:41:51 +0100106sub fname2corpusname {
107 ($_) = @_;
108 s@.*/@@;
Marc Kupietz86b50292019-02-17 21:03:59 +0100109 s@\.en@-en@;
Marc Kupietzc053d972019-01-10 10:41:51 +0100110 s@\..*@@;
111 return $_;
112}
113
Marc Kupietz19c68242018-03-12 09:42:21 +0100114sub getClassicCollocatorsCached {
115 my ($c, $word) = @_;
Marc Kupietz81aeed22019-02-17 21:22:45 +0100116 my $s2 = "";
Marc Kupietz9ff3c992019-02-04 12:32:54 +0100117 if($word > $mergedEnd) {
118 $word-=$mergedEnd;
119 }
Marc Kupietz81aeed22019-02-17 21:22:45 +0100120
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100121 if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference
Marc Kupietz9ee3f412019-08-02 14:58:19 +0200122 open PIPE, "GET http://compute:5673/getClassicCollocators?w=$word |" or
123 open PIPE, "GET http://klinux10:5673/getClassicCollocators?w=$word |";
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100124 }
Marc Kupietz19c68242018-03-12 09:42:21 +0100125 if(!$cccache{$word}) {
Marc Kupietz06d61292019-02-04 12:33:22 +0100126 $c->app->log->info("Getting classic collocates of $word.");
Marc Kupietz19c68242018-03-12 09:42:21 +0100127 $cccache{$word} = getClassicCollocators($word);
Marc Kupietz1d96a082019-02-18 09:29:06 +0100128 $cccache{$word} =~ s/:(-?)(nan|inf)/:"${1}${2}"/g;
Marc Kupietz19c68242018-03-12 09:42:21 +0100129 } else {
Marc Kupietz06d61292019-02-04 12:33:22 +0100130 $c->app->log->info("Getting classic collocates for $word from cache.");
Marc Kupietz19c68242018-03-12 09:42:21 +0100131 }
Marc Kupietz81aeed22019-02-17 21:22:45 +0100132 if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100133 while(<PIPE>) {
134 $s2 .= $_;
135 }
136 close(PIPE);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100137 }
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100138
Marc Kupietz81aeed22019-02-17 21:22:45 +0100139 if(length($s2) > 2000) {
140 my $d1 = decode_json($cccache{$word});
141 my $d2 = decode_json($s2);
142 my %d2ld;
Marc Kupietz001bffd2019-02-21 08:52:41 +0100143 my $minLd = 14;
Marc Kupietz81aeed22019-02-17 21:22:45 +0100144 foreach my $i (@{$d2->{collocates}}) {
145 $d2ld{$i->{word}}=$i->{ld};
Marc Kupietz001bffd2019-02-21 08:52:41 +0100146 $minLd=$i->{ld} if($i->{ld} < $minLd);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100147 }
148 foreach my $i (@{$d1->{collocates}}) {
149 my $w = $i->{word};
Marc Kupietz001bffd2019-02-21 08:52:41 +0100150 $i->{delta} = $i->{ld} - (defined $d2ld{$w} ? $d2ld{$w} : $minLd-0.1);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100151 }
152 return(encode_json($d1));
153 } else {
154 my $d1 = decode_json($cccache{$word});
155 foreach my $i (@{$d1->{collocates}}) {
156 $i->{delta} = 0;
157 }
158 return(encode_json($d1));
159 }
Marc Kupietz19c68242018-03-12 09:42:21 +0100160}
161
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100162sub getSimilarProfilesCached {
163 my ($c, $word) = @_;
164 if(!$spcache{$word}) {
165 $spcache{$word} = getSimilarProfiles($word);
166 } else {
167 $c->app->log->info("Getting similar profiles for $word from cache:");
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100168 }
169 return $spcache{$word};
170}
171
Marc Kupietz66bfd952017-12-11 09:59:45 +0100172post '/derekovecs/getVecsByRanks' => sub {
173 my $self = shift;
174 my $vec = getVecs($self->req->json);
175 $self->render(json => $vec);
176};
177
Marc Kupietze13a3552018-01-25 08:48:34 +0100178any '*/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100179 my $self = shift;
Marc Kupietz19c68242018-03-12 09:42:21 +0100180 $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100181};
182
Marc Kupietze13a3552018-01-25 08:48:34 +0100183any '/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100184 my $self = shift;
Marc Kupietz19c68242018-03-12 09:42:21 +0100185 $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100186};
187
Marc Kupietzd7760b42019-02-21 09:01:44 +0100188any '/getBiggestVocabDistances' => sub {
189 my $self = shift;
190 $self->render(data => getBiggestMergedDifferences(), format=>'json');
191};
192
193any '*/getBiggestVocabDistances' => sub {
194 my $self = shift;
195 $self->render(data => getBiggestMergedDifferences(), format=>'json');
196};
197
Marc Kupietz33c79d32019-08-02 15:11:23 +0200198any '*/getPosWiseW2VCollocators' => sub {
199 my $self = shift;
200 $self->render(data => getPosWiseW2VCollocatorsAsTsv($self->param("w"),
201 ($self->param("max")? $self->param("max") : 200),
202 ($self->param("cutoff")? $self->param("cutoff") :750000),
203 ($self->param("threshold")? $self->param("threshold") : 0.2)),
204 format=>'tsv');
205};
206
207any '/getPosWiseW2VCollocators' => sub {
208 my $self = shift;
209 $self->render(data => getPosWiseW2VCollocatorsAsTsv($self->param("w"),
210 ($self->param("max")? $self->param("max") : 200),
211 ($self->param("cutoff")? $self->param("cutoff") : 750000),
212 ($self->param("threshold")? $self->param("threshold") : 0.2)),
213 format=>'tsv');
214};
215
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100216any '*/getSimilarProfiles' => sub {
217 my $self = shift;
218 $self->render(data => getSimilarProfilesCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
219};
220
Marc Kupietzc987fa82018-03-21 12:14:25 +0100221any '/getSimilarProfiles' => sub {
222 my $self = shift;
223 $self->render(data => getSimilarProfilesCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
224};
225
Marc Kupietz98ed1c02019-08-02 15:05:37 +0200226any '/getSimilarity' => sub {
227 my $self = shift;
228 my $w1 = $self->param("w1");
229 my $w2 = $self->param("w2");
230 $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json');
231};
232
233any '*/getSimilarity' => sub {
234 my $self = shift;
235 my $w1 = $self->param("w1");
236 my $w2 = $self->param("w2");
237 $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json');
238};
239
Marc Kupietzdf3d4b52017-11-29 16:57:27 +0100240get '*/img/*' => sub {
241 my $c = shift;
242 my $url = $c->req->url;
243 $url =~ s@/derekovecs@@g;
244 $c->app->log->info("GET: " . $url);
245 $c->reply->static($url);
246};
247
Marc Kupietzdc22b982015-10-09 09:19:34 +0200248get '/' => sub {
249 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +0200250 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200251 my $word=$c->param('word');
Marc Kupietz2da2a812019-02-21 14:17:35 +0100252 my $no_nbs=$c->param('n') || ($opt_m? 50 : 100);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100253 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +0100254 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100255 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100256 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200257 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100258 my $sort=$c->param('sort') || 0;
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100259 my $csv=$c->param('csv') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +0200260 my $json=$c->param('json') || 0;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100261 my $cutoff=$c->param('cutoff') || 500000;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100262 my $dedupe=$c->param('dedupe') || 0;
Marc Kupietzac707b32018-12-20 11:36:38 +0100263 my $nosp=$c->param('nosp') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100264 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100265 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100266 my @collocations;
Marc Kupietzd897d5d2019-08-02 15:09:07 +0200267 $word="idea" if(!defined($word));
268 if(defined($word) && $word !~ /^\s*$/) {
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100269 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100270 $word =~ s/\s+/ /g;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100271 if($opt_m && $word !~ /\|/) {
272 $word .= "|$word";
273 }
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100274 for my $w (split(' *\| *', $word)) {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100275 if($opt_m) {
276 if($searchBaseVocabFirst) {
277 $searchBaseVocabFirst=0;
278 } else {
279 $searchBaseVocabFirst=1;
280 }
281 }
282 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe,$searchBaseVocabFirst}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100283 $c->app->log->info("Getting $w results from cache");
Marc Kupietz3082fd02019-01-09 14:54:06 +0100284 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe.$searchBaseVocabFirst}
Marc Kupietza5b90152016-03-15 17:39:19 +0100285 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100286 $c->app->log->info('Looking for neighbours of '.$w);
287 if($opt_i) {
Marc Kupietzac707b32018-12-20 11:36:38 +0100288 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100289 } else {
Marc Kupietzac707b32018-12-20 11:36:38 +0100290 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100291 }
Marc Kupietz2dd2dd72017-12-01 22:08:14 +0100292 $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100293 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100294 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100295 }
296 }
Marc Kupietz56844a22019-08-02 15:12:19 +0200297
Marc Kupietz000ad862016-02-26 14:59:12 +0100298 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200299 if($json) {
300 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100301 } elsif($csv) {
302 my $csv_data="";
303 for (my $i=0; $i <= $no_nbs; $i++) {
304 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
305 }
306 for (my $i=0; $i < $no_nbs; $i++) {
307 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
308 }
309 chop $csv_data;
310 chop $csv_data;
311 $csv_data .= "\n";
312 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200313 } else {
Marc Kupietzd7760b42019-02-21 09:01:44 +0100314 my $distantWords="";
315 if(!defined($word) || $word !~ /^\s*$/) {
316 $distantWords = getBiggestMergedDifferences();
317 }
318 $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 +0200319 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200320};
321
Marc Kupietz30ca4342017-11-22 21:21:20 +0100322helper(bitvec2window => sub {
323 my ($self, $n) = @_;
324 my $str = unpack("B32", pack("N", $n));
325 $str =~ s/^\d{22}//;
326 $str =~ s/^(\d{5})/$1x/;
327 $str =~ s/0/ยท/g;
328 $str =~ s/1/+/g;
329 return $str;
330 });
331
Marc Kupietza5b90152016-03-15 17:39:19 +0100332$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200333
334exit;