blob: 1e67011ad5e207847d585b8fb8575f79d2bc1969 [file] [log] [blame]
Marc Kupietz66bbd2d2022-07-19 17:53:49 +02001#!/usr/bin/env perl
2
3our $VERSION = '0.91';
Marc Kupietzbf9bac02022-04-11 21:16:47 +02004
Marc Kupietzc82b15f2022-07-19 17:36:27 +02005use IDS::DeReKoVecs::Read;
Marc Kupietzdc22b982015-10-09 09:19:34 +02006use Mojolicious::Lite;
Marc Kupietzc4893362016-02-25 08:04:46 +01007use Mojo::JSON qw(decode_json encode_json to_json);
Marc Kupietz30ca4342017-11-22 21:21:20 +01008use base 'Mojolicious::Plugin';
9
Marc Kupietz247500f2015-10-09 11:29:01 +020010use Encode qw(decode encode);
Marc Kupietza5b90152016-03-15 17:39:19 +010011use Getopt::Std;
Marc Kupietze8e3ded2020-07-13 17:53:56 +020012#use Mojo::Server::Daemon;
Marc Kupietzffef9302017-11-07 15:58:01 +010013use Cwd;
Marc Kupietz66bfd952017-12-11 09:59:45 +010014
Marc Kupietzbf9bac02022-04-11 21:16:47 +020015my $mojo_config = $ENV{MOJO_CONFIG} // '../derekovecs-server.conf';
Marc Kupietzc0d41872021-02-25 16:33:22 +010016plugin Config => {file => $mojo_config};
17
Marc K20476c72021-03-11 12:18:01 +010018my $DEFAULT_VECS = app->config->{w2v}->{vecs} // "../models/dereko-2021-i.vecs";
Marc Kupietzc0d41872021-02-25 16:33:22 +010019my $DEFAULT_NET_NAME = "";
20if ($DEFAULT_VECS=~ /\.vecs/) {
21 $DEFAULT_NET_NAME = $DEFAULT_VECS;
22 $DEFAULT_NET_NAME =~ s/\.vecs/.net/;
23}
24my $DEFAULT_NET = app->config->{w2v}->{net} // $DEFAULT_NET_NAME;
Marc Kupietz397ce852020-07-13 17:52:21 +020025
Marc Kupietz9ae184c2023-11-04 18:21:54 +010026my $DEFAULT_NET = app->config->{w2v}->{net} // $DEFAULT_NET_NAME;
27my $DOWNTIME_CALENDAR_URL = app->config->{downtime_calendar_url} // '';
Marc Kupietzffef9302017-11-07 15:58:01 +010028app->static->paths->[0] = getcwd;
29
Marc Kupietz1b856fa2019-12-07 23:01:43 +010030plugin 'Piwik';
Marc Kupietz2b8d44a2019-12-09 10:38:16 +010031plugin "RemoteAddr";
Marc Kupietz1b856fa2019-12-07 23:01:43 +010032plugin 'Util::RandomString' => {
33 piwik_rand_id => {
34 alphabet => '0123456789abcdef',
35 length => 16
36 }
37};
38
Marc Kupietzd4227392016-03-01 16:45:12 +010039plugin 'Log::Access';
Marc Kupietzb3422c12017-07-04 14:12:11 +020040plugin "RequestBase";
Marc Kupietz95104512019-12-05 10:13:05 +010041#plugin 'AutoReload';
42plugin Localize => {
43 dict => {
44 _ => sub { $_->locale },
45 },
Marc Kupietzbf9bac02022-04-11 21:16:47 +020046 resources => ['../derekovecs-server.dict']
Marc Kupietz95104512019-12-05 10:13:05 +010047};
Marc Kupietza5b90152016-03-15 17:39:19 +010048our $opt_i = 0; # latin1-input?
49our $opt_l = undef;
Marc Kupietza2e64502016-04-27 09:53:51 +020050our $opt_m;
Marc Kupietz6ed81872016-04-27 14:04:04 +020051our $opt_M;
Marc Kupietze8e3ded2020-07-13 17:53:56 +020052our $opt_n = $DEFAULT_NET;
Marc Kupietz43ee87e2016-04-25 10:50:08 +020053our $opt_d;
Marc Kupietzfa194262018-06-05 09:39:32 +020054our $opt_D;
Marc Kupietzda7b3492023-02-27 20:10:59 +010055our $opt_G;
Marc Kupietzbf9bac02022-04-11 21:16:47 +020056
57our $mergedEnd=0;
58our %cache;
59our %cccache; # classic collocator cache
60our %spcache; # similar profile cache
61our $opt_p = 5676;
Marc Kupietze8e3ded2020-07-13 17:53:56 +020062our $opt_C;
Marc Kupietza5b90152016-03-15 17:39:19 +010063
Marc Kupietz6ed81872016-04-27 14:04:04 +020064my %marked;
Marc Kupietzc053d972019-01-10 10:41:51 +010065my $title="";
Marc Kupietz793413b2016-04-02 21:48:57 +020066my $training_args="";
67
Marc Kupietzc82b15f2022-07-19 17:36:27 +020068getopts('d:D:Gil:p:m:n:M:C') or usage();
69
70sub usage() {
71 print STDERR <<EOF;
72non-server mode usage: MOJO_CONFIG=`pwd`/example.conf $0 [-h] [-d <file>]
73-h : this (help) message
74-d file : dump binary vecs as ascii text to <file>
75
76server-mode invocation:
77
78MOJO_CONFIG=`pwd`/example.conf morbo $0
79EOF
80 exit;
81}
Marc Kupietz6ed81872016-04-27 14:04:04 +020082
83if($opt_M) {
Marc Kupietzed930212016-04-27 15:42:38 +020084 open my $handle, '<:encoding(UTF-8)', $opt_M
85 or die "Can't open '$opt_M' for reading: $!";
86 while(<$handle>) {
Marc Kupietz6ed81872016-04-27 14:04:04 +020087 foreach my $mw (split /\s+/) {
88 $marked{$mw}=1
89 }
90 }
Marc Kupietzed930212016-04-27 15:42:38 +020091 close($handle);
Marc Kupietz6ed81872016-04-27 14:04:04 +020092}
Marc Kupietza5b90152016-03-15 17:39:19 +010093
Marc Kupietze8e3ded2020-07-13 17:53:56 +020094my $vecs_name = (@ARGV > 0 && -r $ARGV[0] ? $ARGV[0] : $DEFAULT_VECS);
95init_net($vecs_name, $opt_n, ($opt_i? 1 : 0), 1);
96if(open(FILE, "$vecs_name.args")) {
97 $training_args = <FILE>;
Marc Kupietz2cb667e2016-03-10 09:44:12 +010098}
Marc Kupietze8e3ded2020-07-13 17:53:56 +020099close(FILE);
100$title = fname2corpusname($vecs_name);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200101
Marc Kupietze8e3ded2020-07-13 17:53:56 +0200102my $have_sprofiles = load_sprofiles($vecs_name);
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100103
Marc Kupietzc0d41872021-02-25 16:33:22 +0100104if (app->config->{w2v}->{merge}) {
105 $opt_m = app->config->{w2v}->{merge};
106}
107
Marc Kupietza2e64502016-04-27 09:53:51 +0200108if($opt_m) {
109 $mergedEnd = mergeVectors($opt_m);
Marc Kupietzc053d972019-01-10 10:41:51 +0100110 $title = "<span class=\"merged\">" . $title . "</span> vs. " . fname2corpusname($opt_m);
Marc Kupietza2e64502016-04-27 09:53:51 +0200111}
112
Marc Kupietze5568a02018-12-20 11:42:02 +0100113
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200114if($opt_d) { # -d: dump vecs and exit
115 dump_vecs($opt_d);
116 exit;
117}
118
Marc Kupietzfa194262018-06-05 09:39:32 +0200119if($opt_D) { # -D: dump vecs for numpy and exit
120 dump_for_numpy($opt_D);
121 exit;
122}
123
Marc Kupietze8e3ded2020-07-13 17:53:56 +0200124#my $daemon = Mojo::Server::Daemon->new(
125# app => app,
126# listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"]
127#);
Marc Kupietza5b90152016-03-15 17:39:19 +0100128
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200129if($opt_G) {
130 print "Filtering garbage\n";
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100131 filter_garbage();
Marc Kupietzc0d41872021-02-25 16:33:22 +0100132 print "Finished filtering garbage\n";
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200133}
134
Marc Kupietz554aff52017-11-09 14:42:09 +0100135get '*/js/*' => sub {
Marc Kupietzffef9302017-11-07 15:58:01 +0100136 my $c = shift;
137 my $url = $c->req->url;
Marc K20476c72021-03-11 12:18:01 +0100138 $url =~ s@/derekovecs/@/@g;
Marc Kupietzffef9302017-11-07 15:58:01 +0100139 $c->app->log->info("GET: " . $url);
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100140 $c->reply->static($url);
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100141} => 'js';
Marc Kupietzffef9302017-11-07 15:58:01 +0100142
Marc Kupietza9270572018-03-17 15:17:07 +0100143get '*/css/*' => sub {
144 my $c = shift;
145 my $url = $c->req->url;
146 $url =~ s@/derekovecs/@/@g;
147 $c->app->log->info("GET: " . $url);
148 $c->reply->static($url);
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100149} => 'css';
Marc Kupietza9270572018-03-17 15:17:07 +0100150
Marc Kupietzc053d972019-01-10 10:41:51 +0100151sub fname2corpusname {
152 ($_) = @_;
153 s@.*/@@;
Marc Kupietz86b50292019-02-17 21:03:59 +0100154 s@\.en@-en@;
Marc Kupietzc053d972019-01-10 10:41:51 +0100155 s@\..*@@;
156 return $_;
157}
158
Marc Kupietzcb43e492019-12-03 10:07:53 +0100159sub getWord {
160 ($_) = @_;
161 if ($_ =~ /^\d+/) {
162 return $_;
163 } else {
164 return getWordNumber($_);
165 }
166}
167
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100168
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100169post '/derekovecs/getVecsByRanks' => sub {
Marc Kupietz66bfd952017-12-11 09:59:45 +0100170 my $self = shift;
171 my $vec = getVecs($self->req->json);
172 $self->render(json => $vec);
173};
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100174
Marc Kupietzf6080012021-03-12 09:14:42 +0100175any '*/getCollocationAssociation' => sub {
176 my $self = shift;
177 $self->render(data => getCollocationAssociation($self, getWord($self->param("w") ? $self->param("w") : $self->req->json), getWord($self->param("c"))), format=>'json');
178} => 'getCollocationAssociation';
179
180any '/getCollocationAssociation' => sub {
181 my $self = shift;
182 $self->render(data => getCollocationAssociation($self, getWord($self->param("w") ? $self->param("w") : $self->req->json), getWord($self->param("c"))), format=>'json');
183} => 'getCollocationAssociation1';
184
Marc Kupietze13a3552018-01-25 08:48:34 +0100185any '*/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100186 my $self = shift;
Marc Kupietzcb43e492019-12-03 10:07:53 +0100187 $self->render(data => getClassicCollocatorsCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json');
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100188} => 'getClassicCollocators1';
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100189
Marc Kupietze13a3552018-01-25 08:48:34 +0100190any '/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100191 my $self = shift;
Marc Kupietzcb43e492019-12-03 10:07:53 +0100192 $self->render(data => getClassicCollocatorsCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json');
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100193} => 'getClassicCollocators';
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100194
Marc Kupietzd7760b42019-02-21 09:01:44 +0100195any '/getBiggestVocabDistances' => sub {
196 my $self = shift;
197 $self->render(data => getBiggestMergedDifferences(), format=>'json');
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100198} => 'getBiggestVocabDistances1';
Marc Kupietzd7760b42019-02-21 09:01:44 +0100199
200any '*/getBiggestVocabDistances' => sub {
201 my $self = shift;
202 $self->render(data => getBiggestMergedDifferences(), format=>'json');
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100203} => 'getBiggestVocabDistances';
Marc Kupietzd7760b42019-02-21 09:01:44 +0100204
Marc Kupietz33c79d32019-08-02 15:11:23 +0200205any '*/getPosWiseW2VCollocators' => sub {
206 my $self = shift;
207 $self->render(data => getPosWiseW2VCollocatorsAsTsv($self->param("w"),
208 ($self->param("max")? $self->param("max") : 200),
209 ($self->param("cutoff")? $self->param("cutoff") :750000),
210 ($self->param("threshold")? $self->param("threshold") : 0.2)),
211 format=>'tsv');
212};
213
214any '/getPosWiseW2VCollocators' => sub {
215 my $self = shift;
216 $self->render(data => getPosWiseW2VCollocatorsAsTsv($self->param("w"),
217 ($self->param("max")? $self->param("max") : 200),
218 ($self->param("cutoff")? $self->param("cutoff") : 750000),
219 ($self->param("threshold")? $self->param("threshold") : 0.2)),
220 format=>'tsv');
221};
222
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100223any '*/getSimilarProfiles' => sub {
224 my $self = shift;
Marc Kupietzcb43e492019-12-03 10:07:53 +0100225 $self->render(data => getSimilarProfilesCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json');
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100226};
227
Marc Kupietzc987fa82018-03-21 12:14:25 +0100228any '/getSimilarProfiles' => sub {
229 my $self = shift;
Marc Kupietzcb43e492019-12-03 10:07:53 +0100230 $self->render(data => getSimilarProfilesCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json');
Marc Kupietzc987fa82018-03-21 12:14:25 +0100231};
232
Marc Kupietz9f301572020-04-06 18:29:16 +0200233any '*/getWord' => sub {
234 my $self = shift;
235 my $w = $self->param("w");
236 my $rank = getWord($w);
237 my $status = 200;
238 if ($rank <= 0) {
239 $rank = -1;
240 $status = 404;
241 }
242 $self->render(data => encode_json({word => $w, frequencyRank => $rank}), format => 'json', status => $status);
243};
244
245any '/getWord' => sub {
246 my $self = shift;
247 my $w = $self->param("w");
248 my $rank = getWord($w);
249 my $status = 200;
250 if ($rank <= 0) {
251 $rank = -1;
252 $status = 404;
253 }
254 $self->render(data => encode_json({word => $w, frequencyRank => $rank}), format => 'json', status => $status);
255};
256
Marc Kupietz98ed1c02019-08-02 15:05:37 +0200257any '/getSimilarity' => sub {
258 my $self = shift;
259 my $w1 = $self->param("w1");
260 my $w2 = $self->param("w2");
261 $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json');
262};
263
264any '*/getSimilarity' => sub {
265 my $self = shift;
266 my $w1 = $self->param("w1");
267 my $w2 = $self->param("w2");
268 $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json');
269};
270
Marc Kupietz3576c622023-11-05 08:51:58 +0100271any '*/getDowntimeCalendar' => sub {
272 my $self = shift;
273 my $calendar = getDowntimeCalendar($DOWNTIME_CALENDAR_URL);
274 $self->render(data => $calendar, format=>'text');
275};
276
277any '/getDowntimeCalendar' => sub {
278 my $self = shift;
279 my $calendar = getDowntimeCalendar($DOWNTIME_CALENDAR_URL);
280 $self->render(data => $calendar, format=>'text');
281};
282
Marc Kupietzaf708c22023-11-05 11:20:20 +0100283any '/getVersion' => sub {
284 my $self = shift;
285 $self->render(data => $VERSION, format=>'text');
286};
287
288any '*/getVersion' => sub {
289 my $self = shift;
290 $self->render(data => $VERSION, format=>'text');
291};
292
Marc Kupietzdea505e2023-11-05 11:42:36 +0100293any '/getModelName' => sub {
294 my $self = shift;
295 $self->render(data => $title, format=>'text');
296};
297
298any '*/getModelName' => sub {
299 my $self = shift;
300 $self->render(data => $title, format=>'text');
301};
302
Marc Kupietzdf3d4b52017-11-29 16:57:27 +0100303get '*/img/*' => sub {
304 my $c = shift;
305 my $url = $c->req->url;
306 $url =~ s@/derekovecs@@g;
307 $c->app->log->info("GET: " . $url);
308 $c->reply->static($url);
309};
310
Marc Kupietzdc22b982015-10-09 09:19:34 +0200311get '/' => sub {
312 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +0200313 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200314 my $word=$c->param('word');
Marc Kupietz2da2a812019-02-21 14:17:35 +0100315 my $no_nbs=$c->param('n') || ($opt_m? 50 : 100);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100316 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +0100317 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100318 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100319 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200320 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100321 my $sort=$c->param('sort') || 0;
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100322 my $csv=$c->param('csv') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +0200323 my $json=$c->param('json') || 0;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100324 my $cutoff=$c->param('cutoff') || 500000;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100325 my $dedupe=$c->param('dedupe') || 0;
Marc Kupietzac707b32018-12-20 11:36:38 +0100326 my $nosp=$c->param('nosp') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100327 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100328 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100329 my @collocations;
Marc Kupietzcddc8482019-12-04 08:57:33 +0100330 if(defined($word) && $word !~ /^\s*$/) {
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100331 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100332 $word =~ s/\s+/ /g;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100333 if($opt_m && $word !~ /\|/) {
334 $word .= "|$word";
335 }
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100336 for my $w (split(' *\| *', $word)) {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100337 if($opt_m) {
338 if($searchBaseVocabFirst) {
339 $searchBaseVocabFirst=0;
340 } else {
341 $searchBaseVocabFirst=1;
342 }
343 }
344 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe,$searchBaseVocabFirst}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100345 $c->app->log->info("Getting $w results from cache");
Marc Kupietz3082fd02019-01-09 14:54:06 +0100346 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe.$searchBaseVocabFirst}
Marc Kupietza5b90152016-03-15 17:39:19 +0100347 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100348 $c->app->log->info('Looking for neighbours of '.$w);
349 if($opt_i) {
Marc Kupietzac707b32018-12-20 11:36:38 +0100350 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100351 } else {
Marc Kupietzac707b32018-12-20 11:36:38 +0100352 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100353 }
Marc Kupietz2dd2dd72017-12-01 22:08:14 +0100354 $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100355 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100356 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100357 }
358 }
Marc Kupietz56844a22019-08-02 15:12:19 +0200359
Marc Kupietz000ad862016-02-26 14:59:12 +0100360 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200361 if($json) {
362 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100363 } elsif($csv) {
364 my $csv_data="";
365 for (my $i=0; $i <= $no_nbs; $i++) {
366 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
367 }
368 for (my $i=0; $i < $no_nbs; $i++) {
369 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
370 }
371 chop $csv_data;
372 chop $csv_data;
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100373 $csv_data .= "\n";
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100374 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200375 } else {
Marc Kupietzd7760b42019-02-21 09:01:44 +0100376 my $distantWords="";
377 if(!defined($word) || $word !~ /^\s*$/) {
378 $distantWords = getBiggestMergedDifferences();
379 }
Marc Kupietz9ae184c2023-11-04 18:21:54 +0100380 $c->render(
381 template => "index",
382 title => $title,
383 word => $word,
384 distantWords => $distantWords,
385 cutoff => $cutoff,
386 no_nbs => $no_nbs,
387 no_iterations => $no_iterations,
388 epsilon => $epsilon,
389 perplexity => $perplexity,
390 show_som => $som,
391 searchBaseVocabFirst => $searchBaseVocabFirst,
392 sort => $sort,
393 training_args => $training_args,
394 mergedEnd => $mergedEnd,
395 haveSProfiles => $have_sprofiles,
396 dedupe => $dedupe,
397 marked => \%marked,
398 lists => \@lists,
Marc Kupietz3576c622023-11-05 08:51:58 +0100399 collocators => $res->{syntagmatic}
Marc Kupietz9ae184c2023-11-04 18:21:54 +0100400 );
Marc Kupietzb613b052016-04-28 14:11:59 +0200401 }
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100402} => "paradigmaticAndSyntagmaticNbs";
Marc Kupietzdc22b982015-10-09 09:19:34 +0200403
Marc Kupietz30ca4342017-11-22 21:21:20 +0100404helper(bitvec2window => sub {
405 my ($self, $n) = @_;
406 my $str = unpack("B32", pack("N", $n));
407 $str =~ s/^\d{22}//;
408 $str =~ s/^(\d{5})/$1x/;
409 $str =~ s/0/ยท/g;
410 $str =~ s/1/+/g;
411 return $str;
412 });
413
Marc Kupietz6017daf2022-05-19 09:22:49 +0200414if(app->config->{Piwik} && app->config->{Piwik}->{url}) {
Marc Kupietz3e3e3262022-04-12 23:11:45 +0200415 hook(
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100416 after_render => sub {
417 my $c = shift;
418
419 # Only track valid routes
420 my $route = $c->current_route or return;
421
422 # This won't forward personalized information
423 my $hash = {
Marc Kupietz251de9f2020-01-14 16:12:05 +0100424 action_url => $c->req->url->to_abs,
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100425 action_name => $route,
Marc Kupietz251de9f2020-01-14 16:12:05 +0100426 ua => $c->req->headers->user_agent,
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100427 urlref => '',
428 send_image => 0,
429 dnt => 0,
Marc Kupietz251de9f2020-01-14 16:12:05 +0100430 cip => $c->remote_addr,
431 lang => $c->req->headers->accept_language,
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100432 uid => $c->random_string('piwik_rand_id')
433 };
Marc Kupietz251de9f2020-01-14 16:12:05 +0100434 # $c->app->log->info("PIWIK: counting " . $hash->{action_url} . "\nremote:" . $c->remote_addr);
435 # $c->app->log->info("PIWIK: tag " . $c->piwik_tag);
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100436
437 # Send track
438 $c->piwik->api_p(Track => $hash)->wait;
Marc Kupietz251de9f2020-01-14 16:12:05 +0100439
440 # $c->app->log->info("PIWIK: counted.");
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100441 }
Marc Kupietz3e3e3262022-04-12 23:11:45 +0200442 );
443}
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100444
Marc Kupietzbf9bac02022-04-11 21:16:47 +0200445app->renderer->paths([app->home->rel_file('../templates')]);
Marc Kupietze8e3ded2020-07-13 17:53:56 +0200446app->start;
447#$daemon->run;
Marc Kupietz95104512019-12-05 10:13:05 +0100448# app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200449
Marc Kupietz95104512019-12-05 10:13:05 +0100450# exit;