blob: 1b9e5eacbcd19744530f0286151050f62f29eab8 [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 Kupietze8e3ded2020-07-13 17:53:56 +020011#use Mojo::Server::Daemon;
Marc Kupietzffef9302017-11-07 15:58:01 +010012use Cwd;
Marc Kupietz66bfd952017-12-11 09:59:45 +010013
Marc Kupietzc0d41872021-02-25 16:33:22 +010014my $mojo_config = $ENV{MOJO_CONFIG} // 'w2v-server.conf';
15plugin Config => {file => $mojo_config};
16
17my $DEFAULT_VECS = app->config->{w2v}->{vecs} // "/vol/work/kupietz/Work2/kl/trunk/Analysemethoden/word2vec/models/dereko-2020-ii.vecs";
18my $DEFAULT_NET_NAME = "";
19if ($DEFAULT_VECS=~ /\.vecs/) {
20 $DEFAULT_NET_NAME = $DEFAULT_VECS;
21 $DEFAULT_NET_NAME =~ s/\.vecs/.net/;
22}
23my $DEFAULT_NET = app->config->{w2v}->{net} // $DEFAULT_NET_NAME;
Marc Kupietz397ce852020-07-13 17:52:21 +020024
Marc Kupietzffef9302017-11-07 15:58:01 +010025app->static->paths->[0] = getcwd;
26
Marc Kupietz1b856fa2019-12-07 23:01:43 +010027plugin 'Piwik';
Marc Kupietz2b8d44a2019-12-09 10:38:16 +010028plugin "RemoteAddr";
Marc Kupietz1b856fa2019-12-07 23:01:43 +010029plugin 'Util::RandomString' => {
30 piwik_rand_id => {
31 alphabet => '0123456789abcdef',
32 length => 16
33 }
34};
35
Marc Kupietzd4227392016-03-01 16:45:12 +010036plugin 'Log::Access';
Marc Kupietzb3422c12017-07-04 14:12:11 +020037plugin "RequestBase";
Marc Kupietz95104512019-12-05 10:13:05 +010038#plugin 'AutoReload';
39plugin Localize => {
40 dict => {
41 _ => sub { $_->locale },
42 },
43 resources => ['w2v-server.dict']
44};
Marc Kupietza5b90152016-03-15 17:39:19 +010045our $opt_i = 0; # latin1-input?
46our $opt_l = undef;
47our $opt_p = 5676;
Marc Kupietza2e64502016-04-27 09:53:51 +020048our $opt_m;
Marc Kupietz6ed81872016-04-27 14:04:04 +020049our $opt_M;
Marc Kupietze8e3ded2020-07-13 17:53:56 +020050our $opt_n = $DEFAULT_NET;
Marc Kupietz43ee87e2016-04-25 10:50:08 +020051our $opt_d;
Marc Kupietzfa194262018-06-05 09:39:32 +020052our $opt_D;
Marc Kupietze8e3ded2020-07-13 17:53:56 +020053our $opt_G = 1;
54our $opt_C;
Marc Kupietza5b90152016-03-15 17:39:19 +010055
Marc Kupietz6ed81872016-04-27 14:04:04 +020056my %marked;
Marc Kupietzc053d972019-01-10 10:41:51 +010057my $title="";
Marc Kupietz793413b2016-04-02 21:48:57 +020058my $training_args="";
Marc Kupietza2e64502016-04-27 09:53:51 +020059my $mergedEnd=0;
Marc Kupietz15987412017-11-07 15:56:58 +010060my %cache;
Marc Kupietz19c68242018-03-12 09:42:21 +010061my %cccache; # classic collocator cache
Marc Kupietza51dcfa2018-03-19 16:22:05 +010062my %spcache; # similar profile cache
Marc Kupietz793413b2016-04-02 21:48:57 +020063
Marc Kupietz3080f172020-07-13 17:51:28 +020064getopts('d:D:Gil:p:m:n:M:C');
Marc Kupietz6ed81872016-04-27 14:04:04 +020065
66if($opt_M) {
Marc Kupietzed930212016-04-27 15:42:38 +020067 open my $handle, '<:encoding(UTF-8)', $opt_M
68 or die "Can't open '$opt_M' for reading: $!";
69 while(<$handle>) {
Marc Kupietz6ed81872016-04-27 14:04:04 +020070 foreach my $mw (split /\s+/) {
71 $marked{$mw}=1
72 }
73 }
Marc Kupietzed930212016-04-27 15:42:38 +020074 close($handle);
Marc Kupietz6ed81872016-04-27 14:04:04 +020075}
Marc Kupietza5b90152016-03-15 17:39:19 +010076
Marc Kupietze8e3ded2020-07-13 17:53:56 +020077my $vecs_name = (@ARGV > 0 && -r $ARGV[0] ? $ARGV[0] : $DEFAULT_VECS);
78init_net($vecs_name, $opt_n, ($opt_i? 1 : 0), 1);
79if(open(FILE, "$vecs_name.args")) {
80 $training_args = <FILE>;
Marc Kupietz2cb667e2016-03-10 09:44:12 +010081}
Marc Kupietze8e3ded2020-07-13 17:53:56 +020082close(FILE);
83$title = fname2corpusname($vecs_name);
Marc Kupietzdc22b982015-10-09 09:19:34 +020084
Marc Kupietze8e3ded2020-07-13 17:53:56 +020085my $have_sprofiles = load_sprofiles($vecs_name);
Marc Kupietza51dcfa2018-03-19 16:22:05 +010086
Marc Kupietzc0d41872021-02-25 16:33:22 +010087if (app->config->{w2v}->{merge}) {
88 $opt_m = app->config->{w2v}->{merge};
89}
90
Marc Kupietza2e64502016-04-27 09:53:51 +020091if($opt_m) {
92 $mergedEnd = mergeVectors($opt_m);
Marc Kupietzc053d972019-01-10 10:41:51 +010093 $title = "<span class=\"merged\">" . $title . "</span> vs. " . fname2corpusname($opt_m);
Marc Kupietza2e64502016-04-27 09:53:51 +020094}
95
Marc Kupietze5568a02018-12-20 11:42:02 +010096
Marc Kupietz43ee87e2016-04-25 10:50:08 +020097if($opt_d) { # -d: dump vecs and exit
98 dump_vecs($opt_d);
99 exit;
100}
101
Marc Kupietzfa194262018-06-05 09:39:32 +0200102if($opt_D) { # -D: dump vecs for numpy and exit
103 dump_for_numpy($opt_D);
104 exit;
105}
106
Marc Kupietze8e3ded2020-07-13 17:53:56 +0200107#my $daemon = Mojo::Server::Daemon->new(
108# app => app,
109# listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"]
110#);
Marc Kupietza5b90152016-03-15 17:39:19 +0100111
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200112if($opt_G) {
113 print "Filtering garbage\n";
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100114 filter_garbage();
Marc Kupietzc0d41872021-02-25 16:33:22 +0100115 print "Finished filtering garbage\n";
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200116}
117
Marc Kupietz554aff52017-11-09 14:42:09 +0100118get '*/js/*' => sub {
Marc Kupietzffef9302017-11-07 15:58:01 +0100119 my $c = shift;
120 my $url = $c->req->url;
121 $url =~ s@/derekovecs@@g;
122 $c->app->log->info("GET: " . $url);
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100123 $c->reply->static($url);
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100124} => 'js';
Marc Kupietzffef9302017-11-07 15:58:01 +0100125
Marc Kupietza9270572018-03-17 15:17:07 +0100126get '*/css/*' => sub {
127 my $c = shift;
128 my $url = $c->req->url;
129 $url =~ s@/derekovecs/@/@g;
130 $c->app->log->info("GET: " . $url);
131 $c->reply->static($url);
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100132} => 'css';
Marc Kupietza9270572018-03-17 15:17:07 +0100133
Marc Kupietzc053d972019-01-10 10:41:51 +0100134sub fname2corpusname {
135 ($_) = @_;
136 s@.*/@@;
Marc Kupietz86b50292019-02-17 21:03:59 +0100137 s@\.en@-en@;
Marc Kupietzc053d972019-01-10 10:41:51 +0100138 s@\..*@@;
139 return $_;
140}
141
Marc Kupietzcb43e492019-12-03 10:07:53 +0100142sub getWord {
143 ($_) = @_;
144 if ($_ =~ /^\d+/) {
145 return $_;
146 } else {
147 return getWordNumber($_);
148 }
149}
150
Marc Kupietz19c68242018-03-12 09:42:21 +0100151sub getClassicCollocatorsCached {
152 my ($c, $word) = @_;
Marc Kupietz81aeed22019-02-17 21:22:45 +0100153 my $s2 = "";
Marc Kupietz9ff3c992019-02-04 12:32:54 +0100154 if($word > $mergedEnd) {
155 $word-=$mergedEnd;
156 }
Marc Kupietz81aeed22019-02-17 21:22:45 +0100157
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100158 if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference
Marc Kupietz0dd689e2020-01-14 16:07:18 +0100159 open PIPE, "GET http://corpora.ids-mannheim.de/openlab/derekovecs/getClassicCollocators?w=$word |";
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100160 }
Marc Kupietz3080f172020-07-13 17:51:28 +0200161 if($opt_C || !$cccache{$word}) {
Marc Kupietz06d61292019-02-04 12:33:22 +0100162 $c->app->log->info("Getting classic collocates of $word.");
Marc Kupietz19c68242018-03-12 09:42:21 +0100163 $cccache{$word} = getClassicCollocators($word);
Marc Kupietz1d96a082019-02-18 09:29:06 +0100164 $cccache{$word} =~ s/:(-?)(nan|inf)/:"${1}${2}"/g;
Marc Kupietz19c68242018-03-12 09:42:21 +0100165 } else {
Marc Kupietz06d61292019-02-04 12:33:22 +0100166 $c->app->log->info("Getting classic collocates for $word from cache.");
Marc Kupietz19c68242018-03-12 09:42:21 +0100167 }
Marc Kupietz81aeed22019-02-17 21:22:45 +0100168 if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100169 while(<PIPE>) {
170 $s2 .= $_;
171 }
172 close(PIPE);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100173 }
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100174
Marc Kupietz81aeed22019-02-17 21:22:45 +0100175 if(length($s2) > 2000) {
176 my $d1 = decode_json($cccache{$word});
177 my $d2 = decode_json($s2);
178 my %d2ld;
Marc Kupietz001bffd2019-02-21 08:52:41 +0100179 my $minLd = 14;
Marc Kupietz81aeed22019-02-17 21:22:45 +0100180 foreach my $i (@{$d2->{collocates}}) {
181 $d2ld{$i->{word}}=$i->{ld};
Marc Kupietz001bffd2019-02-21 08:52:41 +0100182 $minLd=$i->{ld} if($i->{ld} < $minLd);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100183 }
184 foreach my $i (@{$d1->{collocates}}) {
185 my $w = $i->{word};
Marc Kupietz001bffd2019-02-21 08:52:41 +0100186 $i->{delta} = $i->{ld} - (defined $d2ld{$w} ? $d2ld{$w} : $minLd-0.1);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100187 }
188 return(encode_json($d1));
189 } else {
190 my $d1 = decode_json($cccache{$word});
191 foreach my $i (@{$d1->{collocates}}) {
192 $i->{delta} = 0;
193 }
194 return(encode_json($d1));
195 }
Marc Kupietz19c68242018-03-12 09:42:21 +0100196}
197
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100198sub getSimilarProfilesCached {
199 my ($c, $word) = @_;
200 if(!$spcache{$word}) {
201 $spcache{$word} = getSimilarProfiles($word);
202 } else {
203 $c->app->log->info("Getting similar profiles for $word from cache:");
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100204 }
205 return $spcache{$word};
206}
207
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100208post '/derekovecs/getVecsByRanks' => sub {
Marc Kupietz66bfd952017-12-11 09:59:45 +0100209 my $self = shift;
210 my $vec = getVecs($self->req->json);
211 $self->render(json => $vec);
212};
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100213
Marc Kupietze13a3552018-01-25 08:48:34 +0100214any '*/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100215 my $self = shift;
Marc Kupietzcb43e492019-12-03 10:07:53 +0100216 $self->render(data => getClassicCollocatorsCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json');
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100217} => 'getClassicCollocators1';
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100218
Marc Kupietze13a3552018-01-25 08:48:34 +0100219any '/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100220 my $self = shift;
Marc Kupietzcb43e492019-12-03 10:07:53 +0100221 $self->render(data => getClassicCollocatorsCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json');
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100222} => 'getClassicCollocators';
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100223
Marc Kupietzd7760b42019-02-21 09:01:44 +0100224any '/getBiggestVocabDistances' => sub {
225 my $self = shift;
226 $self->render(data => getBiggestMergedDifferences(), format=>'json');
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100227} => 'getBiggestVocabDistances1';
Marc Kupietzd7760b42019-02-21 09:01:44 +0100228
229any '*/getBiggestVocabDistances' => sub {
230 my $self = shift;
231 $self->render(data => getBiggestMergedDifferences(), format=>'json');
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100232} => 'getBiggestVocabDistances';
Marc Kupietzd7760b42019-02-21 09:01:44 +0100233
Marc Kupietz33c79d32019-08-02 15:11:23 +0200234any '*/getPosWiseW2VCollocators' => sub {
235 my $self = shift;
236 $self->render(data => getPosWiseW2VCollocatorsAsTsv($self->param("w"),
237 ($self->param("max")? $self->param("max") : 200),
238 ($self->param("cutoff")? $self->param("cutoff") :750000),
239 ($self->param("threshold")? $self->param("threshold") : 0.2)),
240 format=>'tsv');
241};
242
243any '/getPosWiseW2VCollocators' => sub {
244 my $self = shift;
245 $self->render(data => getPosWiseW2VCollocatorsAsTsv($self->param("w"),
246 ($self->param("max")? $self->param("max") : 200),
247 ($self->param("cutoff")? $self->param("cutoff") : 750000),
248 ($self->param("threshold")? $self->param("threshold") : 0.2)),
249 format=>'tsv');
250};
251
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100252any '*/getSimilarProfiles' => sub {
253 my $self = shift;
Marc Kupietzcb43e492019-12-03 10:07:53 +0100254 $self->render(data => getSimilarProfilesCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json');
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100255};
256
Marc Kupietzc987fa82018-03-21 12:14:25 +0100257any '/getSimilarProfiles' => sub {
258 my $self = shift;
Marc Kupietzcb43e492019-12-03 10:07:53 +0100259 $self->render(data => getSimilarProfilesCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json');
Marc Kupietzc987fa82018-03-21 12:14:25 +0100260};
261
Marc Kupietz9f301572020-04-06 18:29:16 +0200262any '*/getWord' => sub {
263 my $self = shift;
264 my $w = $self->param("w");
265 my $rank = getWord($w);
266 my $status = 200;
267 if ($rank <= 0) {
268 $rank = -1;
269 $status = 404;
270 }
271 $self->render(data => encode_json({word => $w, frequencyRank => $rank}), format => 'json', status => $status);
272};
273
274any '/getWord' => sub {
275 my $self = shift;
276 my $w = $self->param("w");
277 my $rank = getWord($w);
278 my $status = 200;
279 if ($rank <= 0) {
280 $rank = -1;
281 $status = 404;
282 }
283 $self->render(data => encode_json({word => $w, frequencyRank => $rank}), format => 'json', status => $status);
284};
285
Marc Kupietz98ed1c02019-08-02 15:05:37 +0200286any '/getSimilarity' => sub {
287 my $self = shift;
288 my $w1 = $self->param("w1");
289 my $w2 = $self->param("w2");
290 $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json');
291};
292
293any '*/getSimilarity' => sub {
294 my $self = shift;
295 my $w1 = $self->param("w1");
296 my $w2 = $self->param("w2");
297 $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json');
298};
299
Marc Kupietzdf3d4b52017-11-29 16:57:27 +0100300get '*/img/*' => sub {
301 my $c = shift;
302 my $url = $c->req->url;
303 $url =~ s@/derekovecs@@g;
304 $c->app->log->info("GET: " . $url);
305 $c->reply->static($url);
306};
307
Marc Kupietzdc22b982015-10-09 09:19:34 +0200308get '/' => sub {
309 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +0200310 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200311 my $word=$c->param('word');
Marc Kupietz2da2a812019-02-21 14:17:35 +0100312 my $no_nbs=$c->param('n') || ($opt_m? 50 : 100);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100313 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +0100314 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100315 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100316 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200317 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100318 my $sort=$c->param('sort') || 0;
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100319 my $csv=$c->param('csv') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +0200320 my $json=$c->param('json') || 0;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100321 my $cutoff=$c->param('cutoff') || 500000;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100322 my $dedupe=$c->param('dedupe') || 0;
Marc Kupietzac707b32018-12-20 11:36:38 +0100323 my $nosp=$c->param('nosp') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100324 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100325 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100326 my @collocations;
Marc Kupietzcddc8482019-12-04 08:57:33 +0100327 if(defined($word) && $word !~ /^\s*$/) {
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100328 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100329 $word =~ s/\s+/ /g;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100330 if($opt_m && $word !~ /\|/) {
331 $word .= "|$word";
332 }
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100333 for my $w (split(' *\| *', $word)) {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100334 if($opt_m) {
335 if($searchBaseVocabFirst) {
336 $searchBaseVocabFirst=0;
337 } else {
338 $searchBaseVocabFirst=1;
339 }
340 }
341 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe,$searchBaseVocabFirst}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100342 $c->app->log->info("Getting $w results from cache");
Marc Kupietz3082fd02019-01-09 14:54:06 +0100343 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe.$searchBaseVocabFirst}
Marc Kupietza5b90152016-03-15 17:39:19 +0100344 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100345 $c->app->log->info('Looking for neighbours of '.$w);
346 if($opt_i) {
Marc Kupietzac707b32018-12-20 11:36:38 +0100347 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100348 } else {
Marc Kupietzac707b32018-12-20 11:36:38 +0100349 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100350 }
Marc Kupietz2dd2dd72017-12-01 22:08:14 +0100351 $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100352 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100353 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100354 }
355 }
Marc Kupietz56844a22019-08-02 15:12:19 +0200356
Marc Kupietz000ad862016-02-26 14:59:12 +0100357 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200358 if($json) {
359 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100360 } elsif($csv) {
361 my $csv_data="";
362 for (my $i=0; $i <= $no_nbs; $i++) {
363 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
364 }
365 for (my $i=0; $i < $no_nbs; $i++) {
366 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
367 }
368 chop $csv_data;
369 chop $csv_data;
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100370 $csv_data .= "\n";
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100371 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200372 } else {
Marc Kupietzd7760b42019-02-21 09:01:44 +0100373 my $distantWords="";
374 if(!defined($word) || $word !~ /^\s*$/) {
375 $distantWords = getBiggestMergedDifferences();
376 }
377 $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 +0200378 }
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100379} => "paradigmaticAndSyntagmaticNbs";
Marc Kupietzdc22b982015-10-09 09:19:34 +0200380
Marc Kupietz30ca4342017-11-22 21:21:20 +0100381helper(bitvec2window => sub {
382 my ($self, $n) = @_;
383 my $str = unpack("B32", pack("N", $n));
384 $str =~ s/^\d{22}//;
385 $str =~ s/^(\d{5})/$1x/;
386 $str =~ s/0/ยท/g;
387 $str =~ s/1/+/g;
388 return $str;
389 });
390
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100391hook(
392 after_render => sub {
393 my $c = shift;
394
395 # Only track valid routes
396 my $route = $c->current_route or return;
397
398 # This won't forward personalized information
399 my $hash = {
Marc Kupietz251de9f2020-01-14 16:12:05 +0100400 action_url => $c->req->url->to_abs,
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100401 action_name => $route,
Marc Kupietz251de9f2020-01-14 16:12:05 +0100402 ua => $c->req->headers->user_agent,
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100403 urlref => '',
404 send_image => 0,
405 dnt => 0,
Marc Kupietz251de9f2020-01-14 16:12:05 +0100406 cip => $c->remote_addr,
407 lang => $c->req->headers->accept_language,
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100408 uid => $c->random_string('piwik_rand_id')
409 };
Marc Kupietz251de9f2020-01-14 16:12:05 +0100410 # $c->app->log->info("PIWIK: counting " . $hash->{action_url} . "\nremote:" . $c->remote_addr);
411 # $c->app->log->info("PIWIK: tag " . $c->piwik_tag);
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100412
413 # Send track
414 $c->piwik->api_p(Track => $hash)->wait;
Marc Kupietz251de9f2020-01-14 16:12:05 +0100415
416 # $c->app->log->info("PIWIK: counted.");
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100417 }
418);
419
Marc Kupietze8e3ded2020-07-13 17:53:56 +0200420app->start;
421#$daemon->run;
Marc Kupietz95104512019-12-05 10:13:05 +0100422# app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200423
Marc Kupietz95104512019-12-05 10:13:05 +0100424# exit;