blob: 04998a94c1468a9421e92e0103b64c7d337f74b6 [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 Kupietz397ce852020-07-13 17:52:21 +020014my $DEFAULT_VECS="/vol/work/kupietz/Work2/kl/trunk/Analysemethoden/word2vec/models/dereko-2020-ii-ALPHA.vecs";
15my $DEFAULT_NET = $DEFAULT_VECS;
16$DEFAULT_NET =~ s/\.vecs/.net/;
17
Marc Kupietzffef9302017-11-07 15:58:01 +010018app->static->paths->[0] = getcwd;
19
Marc Kupietz1b856fa2019-12-07 23:01:43 +010020plugin Config => {file => 'w2v-server.conf'};
21plugin 'Piwik';
Marc Kupietz2b8d44a2019-12-09 10:38:16 +010022plugin "RemoteAddr";
Marc Kupietz1b856fa2019-12-07 23:01:43 +010023plugin 'Util::RandomString' => {
24 piwik_rand_id => {
25 alphabet => '0123456789abcdef',
26 length => 16
27 }
28};
29
Marc Kupietzd4227392016-03-01 16:45:12 +010030plugin 'Log::Access';
Marc Kupietzb3422c12017-07-04 14:12:11 +020031plugin "RequestBase";
Marc Kupietz95104512019-12-05 10:13:05 +010032#plugin 'AutoReload';
33plugin Localize => {
34 dict => {
35 _ => sub { $_->locale },
36 },
37 resources => ['w2v-server.dict']
38};
Marc Kupietza5b90152016-03-15 17:39:19 +010039our $opt_i = 0; # latin1-input?
40our $opt_l = undef;
41our $opt_p = 5676;
Marc Kupietza2e64502016-04-27 09:53:51 +020042our $opt_m;
Marc Kupietz6ed81872016-04-27 14:04:04 +020043our $opt_M;
Marc Kupietz43ee87e2016-04-25 10:50:08 +020044our $opt_n = '';
45our $opt_d;
Marc Kupietzfa194262018-06-05 09:39:32 +020046our $opt_D;
Marc Kupietz5c3887d2016-04-28 08:53:35 +020047our $opt_G;
Marc Kupietza5b90152016-03-15 17:39:19 +010048
Marc Kupietz6ed81872016-04-27 14:04:04 +020049my %marked;
Marc Kupietzc053d972019-01-10 10:41:51 +010050my $title="";
Marc Kupietz793413b2016-04-02 21:48:57 +020051my $training_args="";
Marc Kupietza2e64502016-04-27 09:53:51 +020052my $mergedEnd=0;
Marc Kupietz15987412017-11-07 15:56:58 +010053my %cache;
Marc Kupietz19c68242018-03-12 09:42:21 +010054my %cccache; # classic collocator cache
Marc Kupietza51dcfa2018-03-19 16:22:05 +010055my %spcache; # similar profile cache
Marc Kupietz793413b2016-04-02 21:48:57 +020056
Marc Kupietz3080f172020-07-13 17:51:28 +020057getopts('d:D:Gil:p:m:n:M:C');
Marc Kupietz6ed81872016-04-27 14:04:04 +020058
59if($opt_M) {
Marc Kupietzed930212016-04-27 15:42:38 +020060 open my $handle, '<:encoding(UTF-8)', $opt_M
61 or die "Can't open '$opt_M' for reading: $!";
62 while(<$handle>) {
Marc Kupietz6ed81872016-04-27 14:04:04 +020063 foreach my $mw (split /\s+/) {
64 $marked{$mw}=1
65 }
66 }
Marc Kupietzed930212016-04-27 15:42:38 +020067 close($handle);
Marc Kupietz6ed81872016-04-27 14:04:04 +020068}
Marc Kupietza5b90152016-03-15 17:39:19 +010069
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010070# -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 +010071if(!$ARGV[0]) {
Marc Kupietz0efe49b2020-04-06 18:30:22 +020072 init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0), 0);
Marc Kupietz2cb667e2016-03-10 09:44:12 +010073} else {
Marc Kupietz0efe49b2020-04-06 18:30:22 +020074 init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0), 1);
Marc Kupietz793413b2016-04-02 21:48:57 +020075 if(open(FILE, "$ARGV[0].args")) {
76 $training_args = <FILE>;
77 }
78 close(FILE);
Marc Kupietzc053d972019-01-10 10:41:51 +010079 $title = fname2corpusname($ARGV[0]);
Marc Kupietz2cb667e2016-03-10 09:44:12 +010080}
Marc Kupietzdc22b982015-10-09 09:19:34 +020081
Marc Kupietza51dcfa2018-03-19 16:22:05 +010082my $have_sprofiles = load_sprofiles($ARGV[0]);
83
Marc Kupietza2e64502016-04-27 09:53:51 +020084if($opt_m) {
85 $mergedEnd = mergeVectors($opt_m);
Marc Kupietzc053d972019-01-10 10:41:51 +010086 $title = "<span class=\"merged\">" . $title . "</span> vs. " . fname2corpusname($opt_m);
Marc Kupietza2e64502016-04-27 09:53:51 +020087}
88
Marc Kupietze5568a02018-12-20 11:42:02 +010089
Marc Kupietz43ee87e2016-04-25 10:50:08 +020090if($opt_d) { # -d: dump vecs and exit
91 dump_vecs($opt_d);
92 exit;
93}
94
Marc Kupietzfa194262018-06-05 09:39:32 +020095if($opt_D) { # -D: dump vecs for numpy and exit
96 dump_for_numpy($opt_D);
97 exit;
98}
99
Marc Kupietza5b90152016-03-15 17:39:19 +0100100my $daemon = Mojo::Server::Daemon->new(
101 app => app,
102 listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"]
103);
104
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200105if($opt_G) {
106 print "Filtering garbage\n";
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100107 filter_garbage();
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200108}
109
Marc Kupietz554aff52017-11-09 14:42:09 +0100110get '*/js/*' => sub {
Marc Kupietzffef9302017-11-07 15:58:01 +0100111 my $c = shift;
112 my $url = $c->req->url;
113 $url =~ s@/derekovecs@@g;
114 $c->app->log->info("GET: " . $url);
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100115 $c->reply->static($url);
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100116} => 'js';
Marc Kupietzffef9302017-11-07 15:58:01 +0100117
Marc Kupietza9270572018-03-17 15:17:07 +0100118get '*/css/*' => sub {
119 my $c = shift;
120 my $url = $c->req->url;
121 $url =~ s@/derekovecs/@/@g;
122 $c->app->log->info("GET: " . $url);
123 $c->reply->static($url);
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100124} => 'css';
Marc Kupietza9270572018-03-17 15:17:07 +0100125
Marc Kupietzc053d972019-01-10 10:41:51 +0100126sub fname2corpusname {
127 ($_) = @_;
128 s@.*/@@;
Marc Kupietz86b50292019-02-17 21:03:59 +0100129 s@\.en@-en@;
Marc Kupietzc053d972019-01-10 10:41:51 +0100130 s@\..*@@;
131 return $_;
132}
133
Marc Kupietzcb43e492019-12-03 10:07:53 +0100134sub getWord {
135 ($_) = @_;
136 if ($_ =~ /^\d+/) {
137 return $_;
138 } else {
139 return getWordNumber($_);
140 }
141}
142
Marc Kupietz19c68242018-03-12 09:42:21 +0100143sub getClassicCollocatorsCached {
144 my ($c, $word) = @_;
Marc Kupietz81aeed22019-02-17 21:22:45 +0100145 my $s2 = "";
Marc Kupietz9ff3c992019-02-04 12:32:54 +0100146 if($word > $mergedEnd) {
147 $word-=$mergedEnd;
148 }
Marc Kupietz81aeed22019-02-17 21:22:45 +0100149
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100150 if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference
Marc Kupietz0dd689e2020-01-14 16:07:18 +0100151 open PIPE, "GET http://corpora.ids-mannheim.de/openlab/derekovecs/getClassicCollocators?w=$word |";
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100152 }
Marc Kupietz3080f172020-07-13 17:51:28 +0200153 if($opt_C || !$cccache{$word}) {
Marc Kupietz06d61292019-02-04 12:33:22 +0100154 $c->app->log->info("Getting classic collocates of $word.");
Marc Kupietz19c68242018-03-12 09:42:21 +0100155 $cccache{$word} = getClassicCollocators($word);
Marc Kupietz1d96a082019-02-18 09:29:06 +0100156 $cccache{$word} =~ s/:(-?)(nan|inf)/:"${1}${2}"/g;
Marc Kupietz19c68242018-03-12 09:42:21 +0100157 } else {
Marc Kupietz06d61292019-02-04 12:33:22 +0100158 $c->app->log->info("Getting classic collocates for $word from cache.");
Marc Kupietz19c68242018-03-12 09:42:21 +0100159 }
Marc Kupietz81aeed22019-02-17 21:22:45 +0100160 if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100161 while(<PIPE>) {
162 $s2 .= $_;
163 }
164 close(PIPE);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100165 }
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100166
Marc Kupietz81aeed22019-02-17 21:22:45 +0100167 if(length($s2) > 2000) {
168 my $d1 = decode_json($cccache{$word});
169 my $d2 = decode_json($s2);
170 my %d2ld;
Marc Kupietz001bffd2019-02-21 08:52:41 +0100171 my $minLd = 14;
Marc Kupietz81aeed22019-02-17 21:22:45 +0100172 foreach my $i (@{$d2->{collocates}}) {
173 $d2ld{$i->{word}}=$i->{ld};
Marc Kupietz001bffd2019-02-21 08:52:41 +0100174 $minLd=$i->{ld} if($i->{ld} < $minLd);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100175 }
176 foreach my $i (@{$d1->{collocates}}) {
177 my $w = $i->{word};
Marc Kupietz001bffd2019-02-21 08:52:41 +0100178 $i->{delta} = $i->{ld} - (defined $d2ld{$w} ? $d2ld{$w} : $minLd-0.1);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100179 }
180 return(encode_json($d1));
181 } else {
182 my $d1 = decode_json($cccache{$word});
183 foreach my $i (@{$d1->{collocates}}) {
184 $i->{delta} = 0;
185 }
186 return(encode_json($d1));
187 }
Marc Kupietz19c68242018-03-12 09:42:21 +0100188}
189
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100190sub getSimilarProfilesCached {
191 my ($c, $word) = @_;
192 if(!$spcache{$word}) {
193 $spcache{$word} = getSimilarProfiles($word);
194 } else {
195 $c->app->log->info("Getting similar profiles for $word from cache:");
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100196 }
197 return $spcache{$word};
198}
199
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100200post '/derekovecs/getVecsByRanks' => sub {
Marc Kupietz66bfd952017-12-11 09:59:45 +0100201 my $self = shift;
202 my $vec = getVecs($self->req->json);
203 $self->render(json => $vec);
204};
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100205
Marc Kupietze13a3552018-01-25 08:48:34 +0100206any '*/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100207 my $self = shift;
Marc Kupietzcb43e492019-12-03 10:07:53 +0100208 $self->render(data => getClassicCollocatorsCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json');
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100209} => 'getClassicCollocators1';
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100210
Marc Kupietze13a3552018-01-25 08:48:34 +0100211any '/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100212 my $self = shift;
Marc Kupietzcb43e492019-12-03 10:07:53 +0100213 $self->render(data => getClassicCollocatorsCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json');
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100214} => 'getClassicCollocators';
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100215
Marc Kupietzd7760b42019-02-21 09:01:44 +0100216any '/getBiggestVocabDistances' => sub {
217 my $self = shift;
218 $self->render(data => getBiggestMergedDifferences(), format=>'json');
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100219} => 'getBiggestVocabDistances1';
Marc Kupietzd7760b42019-02-21 09:01:44 +0100220
221any '*/getBiggestVocabDistances' => sub {
222 my $self = shift;
223 $self->render(data => getBiggestMergedDifferences(), format=>'json');
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100224} => 'getBiggestVocabDistances';
Marc Kupietzd7760b42019-02-21 09:01:44 +0100225
Marc Kupietz33c79d32019-08-02 15:11:23 +0200226any '*/getPosWiseW2VCollocators' => sub {
227 my $self = shift;
228 $self->render(data => getPosWiseW2VCollocatorsAsTsv($self->param("w"),
229 ($self->param("max")? $self->param("max") : 200),
230 ($self->param("cutoff")? $self->param("cutoff") :750000),
231 ($self->param("threshold")? $self->param("threshold") : 0.2)),
232 format=>'tsv');
233};
234
235any '/getPosWiseW2VCollocators' => sub {
236 my $self = shift;
237 $self->render(data => getPosWiseW2VCollocatorsAsTsv($self->param("w"),
238 ($self->param("max")? $self->param("max") : 200),
239 ($self->param("cutoff")? $self->param("cutoff") : 750000),
240 ($self->param("threshold")? $self->param("threshold") : 0.2)),
241 format=>'tsv');
242};
243
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100244any '*/getSimilarProfiles' => sub {
245 my $self = shift;
Marc Kupietzcb43e492019-12-03 10:07:53 +0100246 $self->render(data => getSimilarProfilesCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json');
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100247};
248
Marc Kupietzc987fa82018-03-21 12:14:25 +0100249any '/getSimilarProfiles' => sub {
250 my $self = shift;
Marc Kupietzcb43e492019-12-03 10:07:53 +0100251 $self->render(data => getSimilarProfilesCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json');
Marc Kupietzc987fa82018-03-21 12:14:25 +0100252};
253
Marc Kupietz9f301572020-04-06 18:29:16 +0200254any '*/getWord' => sub {
255 my $self = shift;
256 my $w = $self->param("w");
257 my $rank = getWord($w);
258 my $status = 200;
259 if ($rank <= 0) {
260 $rank = -1;
261 $status = 404;
262 }
263 $self->render(data => encode_json({word => $w, frequencyRank => $rank}), format => 'json', status => $status);
264};
265
266any '/getWord' => sub {
267 my $self = shift;
268 my $w = $self->param("w");
269 my $rank = getWord($w);
270 my $status = 200;
271 if ($rank <= 0) {
272 $rank = -1;
273 $status = 404;
274 }
275 $self->render(data => encode_json({word => $w, frequencyRank => $rank}), format => 'json', status => $status);
276};
277
Marc Kupietz98ed1c02019-08-02 15:05:37 +0200278any '/getSimilarity' => sub {
279 my $self = shift;
280 my $w1 = $self->param("w1");
281 my $w2 = $self->param("w2");
282 $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json');
283};
284
285any '*/getSimilarity' => sub {
286 my $self = shift;
287 my $w1 = $self->param("w1");
288 my $w2 = $self->param("w2");
289 $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json');
290};
291
Marc Kupietzdf3d4b52017-11-29 16:57:27 +0100292get '*/img/*' => sub {
293 my $c = shift;
294 my $url = $c->req->url;
295 $url =~ s@/derekovecs@@g;
296 $c->app->log->info("GET: " . $url);
297 $c->reply->static($url);
298};
299
Marc Kupietzdc22b982015-10-09 09:19:34 +0200300get '/' => sub {
301 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +0200302 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200303 my $word=$c->param('word');
Marc Kupietz2da2a812019-02-21 14:17:35 +0100304 my $no_nbs=$c->param('n') || ($opt_m? 50 : 100);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100305 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +0100306 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100307 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100308 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200309 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100310 my $sort=$c->param('sort') || 0;
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100311 my $csv=$c->param('csv') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +0200312 my $json=$c->param('json') || 0;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100313 my $cutoff=$c->param('cutoff') || 500000;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100314 my $dedupe=$c->param('dedupe') || 0;
Marc Kupietzac707b32018-12-20 11:36:38 +0100315 my $nosp=$c->param('nosp') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100316 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100317 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100318 my @collocations;
Marc Kupietzcddc8482019-12-04 08:57:33 +0100319 if(defined($word) && $word !~ /^\s*$/) {
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100320 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100321 $word =~ s/\s+/ /g;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100322 if($opt_m && $word !~ /\|/) {
323 $word .= "|$word";
324 }
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100325 for my $w (split(' *\| *', $word)) {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100326 if($opt_m) {
327 if($searchBaseVocabFirst) {
328 $searchBaseVocabFirst=0;
329 } else {
330 $searchBaseVocabFirst=1;
331 }
332 }
333 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe,$searchBaseVocabFirst}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100334 $c->app->log->info("Getting $w results from cache");
Marc Kupietz3082fd02019-01-09 14:54:06 +0100335 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe.$searchBaseVocabFirst}
Marc Kupietza5b90152016-03-15 17:39:19 +0100336 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100337 $c->app->log->info('Looking for neighbours of '.$w);
338 if($opt_i) {
Marc Kupietzac707b32018-12-20 11:36:38 +0100339 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100340 } else {
Marc Kupietzac707b32018-12-20 11:36:38 +0100341 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100342 }
Marc Kupietz2dd2dd72017-12-01 22:08:14 +0100343 $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100344 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100345 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100346 }
347 }
Marc Kupietz56844a22019-08-02 15:12:19 +0200348
Marc Kupietz000ad862016-02-26 14:59:12 +0100349 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200350 if($json) {
351 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100352 } elsif($csv) {
353 my $csv_data="";
354 for (my $i=0; $i <= $no_nbs; $i++) {
355 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
356 }
357 for (my $i=0; $i < $no_nbs; $i++) {
358 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
359 }
360 chop $csv_data;
361 chop $csv_data;
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100362 $csv_data .= "\n";
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100363 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200364 } else {
Marc Kupietzd7760b42019-02-21 09:01:44 +0100365 my $distantWords="";
366 if(!defined($word) || $word !~ /^\s*$/) {
367 $distantWords = getBiggestMergedDifferences();
368 }
369 $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 +0200370 }
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100371} => "paradigmaticAndSyntagmaticNbs";
Marc Kupietzdc22b982015-10-09 09:19:34 +0200372
Marc Kupietz30ca4342017-11-22 21:21:20 +0100373helper(bitvec2window => sub {
374 my ($self, $n) = @_;
375 my $str = unpack("B32", pack("N", $n));
376 $str =~ s/^\d{22}//;
377 $str =~ s/^(\d{5})/$1x/;
378 $str =~ s/0/ยท/g;
379 $str =~ s/1/+/g;
380 return $str;
381 });
382
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100383hook(
384 after_render => sub {
385 my $c = shift;
386
387 # Only track valid routes
388 my $route = $c->current_route or return;
389
390 # This won't forward personalized information
391 my $hash = {
Marc Kupietz251de9f2020-01-14 16:12:05 +0100392 action_url => $c->req->url->to_abs,
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100393 action_name => $route,
Marc Kupietz251de9f2020-01-14 16:12:05 +0100394 ua => $c->req->headers->user_agent,
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100395 urlref => '',
396 send_image => 0,
397 dnt => 0,
Marc Kupietz251de9f2020-01-14 16:12:05 +0100398 cip => $c->remote_addr,
399 lang => $c->req->headers->accept_language,
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100400 uid => $c->random_string('piwik_rand_id')
401 };
Marc Kupietz251de9f2020-01-14 16:12:05 +0100402 # $c->app->log->info("PIWIK: counting " . $hash->{action_url} . "\nremote:" . $c->remote_addr);
403 # $c->app->log->info("PIWIK: tag " . $c->piwik_tag);
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100404
405 # Send track
406 $c->piwik->api_p(Track => $hash)->wait;
Marc Kupietz251de9f2020-01-14 16:12:05 +0100407
408 # $c->app->log->info("PIWIK: counted.");
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100409 }
410);
411
Marc Kupietz95104512019-12-05 10:13:05 +0100412$daemon->run;
413# app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200414
Marc Kupietz95104512019-12-05 10:13:05 +0100415# exit;