blob: 2b8c35da3d42f007e1d883203bc9d9a65921f713 [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 Kupietz1b856fa2019-12-07 23:01:43 +010016plugin Config => {file => 'w2v-server.conf'};
17plugin 'Piwik';
Marc Kupietz2b8d44a2019-12-09 10:38:16 +010018plugin "RemoteAddr";
Marc Kupietz1b856fa2019-12-07 23:01:43 +010019plugin 'Util::RandomString' => {
20 piwik_rand_id => {
21 alphabet => '0123456789abcdef',
22 length => 16
23 }
24};
25
Marc Kupietzd4227392016-03-01 16:45:12 +010026plugin 'Log::Access';
Marc Kupietzb3422c12017-07-04 14:12:11 +020027plugin "RequestBase";
Marc Kupietz95104512019-12-05 10:13:05 +010028#plugin 'AutoReload';
29plugin Localize => {
30 dict => {
31 _ => sub { $_->locale },
32 },
33 resources => ['w2v-server.dict']
34};
Marc Kupietza5b90152016-03-15 17:39:19 +010035our $opt_i = 0; # latin1-input?
36our $opt_l = undef;
37our $opt_p = 5676;
Marc Kupietza2e64502016-04-27 09:53:51 +020038our $opt_m;
Marc Kupietz6ed81872016-04-27 14:04:04 +020039our $opt_M;
Marc Kupietz43ee87e2016-04-25 10:50:08 +020040our $opt_n = '';
41our $opt_d;
Marc Kupietzfa194262018-06-05 09:39:32 +020042our $opt_D;
Marc Kupietz5c3887d2016-04-28 08:53:35 +020043our $opt_G;
Marc Kupietza5b90152016-03-15 17:39:19 +010044
Marc Kupietz6ed81872016-04-27 14:04:04 +020045my %marked;
Marc Kupietzc053d972019-01-10 10:41:51 +010046my $title="";
Marc Kupietz793413b2016-04-02 21:48:57 +020047my $training_args="";
Marc Kupietza2e64502016-04-27 09:53:51 +020048my $mergedEnd=0;
Marc Kupietz15987412017-11-07 15:56:58 +010049my %cache;
Marc Kupietz19c68242018-03-12 09:42:21 +010050my %cccache; # classic collocator cache
Marc Kupietza51dcfa2018-03-19 16:22:05 +010051my %spcache; # similar profile cache
Marc Kupietz793413b2016-04-02 21:48:57 +020052
Marc Kupietzfa194262018-06-05 09:39:32 +020053getopts('d:D:Gil:p:m:n:M:');
Marc Kupietz6ed81872016-04-27 14:04:04 +020054
55if($opt_M) {
Marc Kupietzed930212016-04-27 15:42:38 +020056 open my $handle, '<:encoding(UTF-8)', $opt_M
57 or die "Can't open '$opt_M' for reading: $!";
58 while(<$handle>) {
Marc Kupietz6ed81872016-04-27 14:04:04 +020059 foreach my $mw (split /\s+/) {
60 $marked{$mw}=1
61 }
62 }
Marc Kupietzed930212016-04-27 15:42:38 +020063 close($handle);
Marc Kupietz6ed81872016-04-27 14:04:04 +020064}
Marc Kupietza5b90152016-03-15 17:39:19 +010065
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010066# -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 +010067if(!$ARGV[0]) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010068 init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0));
Marc Kupietz2cb667e2016-03-10 09:44:12 +010069} else {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010070 init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0));
Marc Kupietz793413b2016-04-02 21:48:57 +020071 if(open(FILE, "$ARGV[0].args")) {
72 $training_args = <FILE>;
73 }
74 close(FILE);
Marc Kupietzc053d972019-01-10 10:41:51 +010075 $title = fname2corpusname($ARGV[0]);
Marc Kupietz2cb667e2016-03-10 09:44:12 +010076}
Marc Kupietzdc22b982015-10-09 09:19:34 +020077
Marc Kupietza51dcfa2018-03-19 16:22:05 +010078my $have_sprofiles = load_sprofiles($ARGV[0]);
79
Marc Kupietza2e64502016-04-27 09:53:51 +020080if($opt_m) {
81 $mergedEnd = mergeVectors($opt_m);
Marc Kupietzc053d972019-01-10 10:41:51 +010082 $title = "<span class=\"merged\">" . $title . "</span> vs. " . fname2corpusname($opt_m);
Marc Kupietza2e64502016-04-27 09:53:51 +020083}
84
Marc Kupietze5568a02018-12-20 11:42:02 +010085
Marc Kupietz43ee87e2016-04-25 10:50:08 +020086if($opt_d) { # -d: dump vecs and exit
87 dump_vecs($opt_d);
88 exit;
89}
90
Marc Kupietzfa194262018-06-05 09:39:32 +020091if($opt_D) { # -D: dump vecs for numpy and exit
92 dump_for_numpy($opt_D);
93 exit;
94}
95
Marc Kupietza5b90152016-03-15 17:39:19 +010096my $daemon = Mojo::Server::Daemon->new(
97 app => app,
98 listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"]
99);
100
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200101if($opt_G) {
102 print "Filtering garbage\n";
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100103 filter_garbage();
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200104}
105
Marc Kupietz554aff52017-11-09 14:42:09 +0100106get '*/js/*' => sub {
Marc Kupietzffef9302017-11-07 15:58:01 +0100107 my $c = shift;
108 my $url = $c->req->url;
109 $url =~ s@/derekovecs@@g;
110 $c->app->log->info("GET: " . $url);
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100111 $c->reply->static($url);
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100112} => 'js';
Marc Kupietzffef9302017-11-07 15:58:01 +0100113
Marc Kupietza9270572018-03-17 15:17:07 +0100114get '*/css/*' => sub {
115 my $c = shift;
116 my $url = $c->req->url;
117 $url =~ s@/derekovecs/@/@g;
118 $c->app->log->info("GET: " . $url);
119 $c->reply->static($url);
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100120} => 'css';
Marc Kupietza9270572018-03-17 15:17:07 +0100121
Marc Kupietzc053d972019-01-10 10:41:51 +0100122sub fname2corpusname {
123 ($_) = @_;
124 s@.*/@@;
Marc Kupietz86b50292019-02-17 21:03:59 +0100125 s@\.en@-en@;
Marc Kupietzc053d972019-01-10 10:41:51 +0100126 s@\..*@@;
127 return $_;
128}
129
Marc Kupietzcb43e492019-12-03 10:07:53 +0100130sub getWord {
131 ($_) = @_;
132 if ($_ =~ /^\d+/) {
133 return $_;
134 } else {
135 return getWordNumber($_);
136 }
137}
138
Marc Kupietz19c68242018-03-12 09:42:21 +0100139sub getClassicCollocatorsCached {
140 my ($c, $word) = @_;
Marc Kupietz81aeed22019-02-17 21:22:45 +0100141 my $s2 = "";
Marc Kupietz9ff3c992019-02-04 12:32:54 +0100142 if($word > $mergedEnd) {
143 $word-=$mergedEnd;
144 }
Marc Kupietz81aeed22019-02-17 21:22:45 +0100145
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100146 if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference
Marc Kupietz9ee3f412019-08-02 14:58:19 +0200147 open PIPE, "GET http://compute:5673/getClassicCollocators?w=$word |" or
148 open PIPE, "GET http://klinux10:5673/getClassicCollocators?w=$word |";
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100149 }
Marc Kupietz19c68242018-03-12 09:42:21 +0100150 if(!$cccache{$word}) {
Marc Kupietz06d61292019-02-04 12:33:22 +0100151 $c->app->log->info("Getting classic collocates of $word.");
Marc Kupietz19c68242018-03-12 09:42:21 +0100152 $cccache{$word} = getClassicCollocators($word);
Marc Kupietz1d96a082019-02-18 09:29:06 +0100153 $cccache{$word} =~ s/:(-?)(nan|inf)/:"${1}${2}"/g;
Marc Kupietz19c68242018-03-12 09:42:21 +0100154 } else {
Marc Kupietz06d61292019-02-04 12:33:22 +0100155 $c->app->log->info("Getting classic collocates for $word from cache.");
Marc Kupietz19c68242018-03-12 09:42:21 +0100156 }
Marc Kupietz81aeed22019-02-17 21:22:45 +0100157 if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100158 while(<PIPE>) {
159 $s2 .= $_;
160 }
161 close(PIPE);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100162 }
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100163
Marc Kupietz81aeed22019-02-17 21:22:45 +0100164 if(length($s2) > 2000) {
165 my $d1 = decode_json($cccache{$word});
166 my $d2 = decode_json($s2);
167 my %d2ld;
Marc Kupietz001bffd2019-02-21 08:52:41 +0100168 my $minLd = 14;
Marc Kupietz81aeed22019-02-17 21:22:45 +0100169 foreach my $i (@{$d2->{collocates}}) {
170 $d2ld{$i->{word}}=$i->{ld};
Marc Kupietz001bffd2019-02-21 08:52:41 +0100171 $minLd=$i->{ld} if($i->{ld} < $minLd);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100172 }
173 foreach my $i (@{$d1->{collocates}}) {
174 my $w = $i->{word};
Marc Kupietz001bffd2019-02-21 08:52:41 +0100175 $i->{delta} = $i->{ld} - (defined $d2ld{$w} ? $d2ld{$w} : $minLd-0.1);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100176 }
177 return(encode_json($d1));
178 } else {
179 my $d1 = decode_json($cccache{$word});
180 foreach my $i (@{$d1->{collocates}}) {
181 $i->{delta} = 0;
182 }
183 return(encode_json($d1));
184 }
Marc Kupietz19c68242018-03-12 09:42:21 +0100185}
186
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100187sub getSimilarProfilesCached {
188 my ($c, $word) = @_;
189 if(!$spcache{$word}) {
190 $spcache{$word} = getSimilarProfiles($word);
191 } else {
192 $c->app->log->info("Getting similar profiles for $word from cache:");
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100193 }
194 return $spcache{$word};
195}
196
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100197post '/derekovecs/getVecsByRanks' => sub {
Marc Kupietz66bfd952017-12-11 09:59:45 +0100198 my $self = shift;
199 my $vec = getVecs($self->req->json);
200 $self->render(json => $vec);
201};
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100202
Marc Kupietze13a3552018-01-25 08:48:34 +0100203any '*/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100204 my $self = shift;
Marc Kupietzcb43e492019-12-03 10:07:53 +0100205 $self->render(data => getClassicCollocatorsCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json');
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100206} => 'getClassicCollocators1';
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100207
Marc Kupietze13a3552018-01-25 08:48:34 +0100208any '/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100209 my $self = shift;
Marc Kupietzcb43e492019-12-03 10:07:53 +0100210 $self->render(data => getClassicCollocatorsCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json');
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100211} => 'getClassicCollocators';
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100212
Marc Kupietzd7760b42019-02-21 09:01:44 +0100213any '/getBiggestVocabDistances' => sub {
214 my $self = shift;
215 $self->render(data => getBiggestMergedDifferences(), format=>'json');
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100216} => 'getBiggestVocabDistances1';
Marc Kupietzd7760b42019-02-21 09:01:44 +0100217
218any '*/getBiggestVocabDistances' => sub {
219 my $self = shift;
220 $self->render(data => getBiggestMergedDifferences(), format=>'json');
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100221} => 'getBiggestVocabDistances';
Marc Kupietzd7760b42019-02-21 09:01:44 +0100222
Marc Kupietz33c79d32019-08-02 15:11:23 +0200223any '*/getPosWiseW2VCollocators' => sub {
224 my $self = shift;
225 $self->render(data => getPosWiseW2VCollocatorsAsTsv($self->param("w"),
226 ($self->param("max")? $self->param("max") : 200),
227 ($self->param("cutoff")? $self->param("cutoff") :750000),
228 ($self->param("threshold")? $self->param("threshold") : 0.2)),
229 format=>'tsv');
230};
231
232any '/getPosWiseW2VCollocators' => sub {
233 my $self = shift;
234 $self->render(data => getPosWiseW2VCollocatorsAsTsv($self->param("w"),
235 ($self->param("max")? $self->param("max") : 200),
236 ($self->param("cutoff")? $self->param("cutoff") : 750000),
237 ($self->param("threshold")? $self->param("threshold") : 0.2)),
238 format=>'tsv');
239};
240
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100241any '*/getSimilarProfiles' => sub {
242 my $self = shift;
Marc Kupietzcb43e492019-12-03 10:07:53 +0100243 $self->render(data => getSimilarProfilesCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json');
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100244};
245
Marc Kupietzc987fa82018-03-21 12:14:25 +0100246any '/getSimilarProfiles' => sub {
247 my $self = shift;
Marc Kupietzcb43e492019-12-03 10:07:53 +0100248 $self->render(data => getSimilarProfilesCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json');
Marc Kupietzc987fa82018-03-21 12:14:25 +0100249};
250
Marc Kupietz98ed1c02019-08-02 15:05:37 +0200251any '/getSimilarity' => sub {
252 my $self = shift;
253 my $w1 = $self->param("w1");
254 my $w2 = $self->param("w2");
255 $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json');
256};
257
258any '*/getSimilarity' => sub {
259 my $self = shift;
260 my $w1 = $self->param("w1");
261 my $w2 = $self->param("w2");
262 $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json');
263};
264
Marc Kupietzdf3d4b52017-11-29 16:57:27 +0100265get '*/img/*' => sub {
266 my $c = shift;
267 my $url = $c->req->url;
268 $url =~ s@/derekovecs@@g;
269 $c->app->log->info("GET: " . $url);
270 $c->reply->static($url);
271};
272
Marc Kupietzdc22b982015-10-09 09:19:34 +0200273get '/' => sub {
274 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +0200275 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200276 my $word=$c->param('word');
Marc Kupietz2da2a812019-02-21 14:17:35 +0100277 my $no_nbs=$c->param('n') || ($opt_m? 50 : 100);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100278 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +0100279 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100280 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100281 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200282 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100283 my $sort=$c->param('sort') || 0;
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100284 my $csv=$c->param('csv') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +0200285 my $json=$c->param('json') || 0;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100286 my $cutoff=$c->param('cutoff') || 500000;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100287 my $dedupe=$c->param('dedupe') || 0;
Marc Kupietzac707b32018-12-20 11:36:38 +0100288 my $nosp=$c->param('nosp') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100289 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100290 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100291 my @collocations;
Marc Kupietzcddc8482019-12-04 08:57:33 +0100292 if(defined($word) && $word !~ /^\s*$/) {
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100293 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100294 $word =~ s/\s+/ /g;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100295 if($opt_m && $word !~ /\|/) {
296 $word .= "|$word";
297 }
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100298 for my $w (split(' *\| *', $word)) {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100299 if($opt_m) {
300 if($searchBaseVocabFirst) {
301 $searchBaseVocabFirst=0;
302 } else {
303 $searchBaseVocabFirst=1;
304 }
305 }
306 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe,$searchBaseVocabFirst}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100307 $c->app->log->info("Getting $w results from cache");
Marc Kupietz3082fd02019-01-09 14:54:06 +0100308 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe.$searchBaseVocabFirst}
Marc Kupietza5b90152016-03-15 17:39:19 +0100309 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100310 $c->app->log->info('Looking for neighbours of '.$w);
311 if($opt_i) {
Marc Kupietzac707b32018-12-20 11:36:38 +0100312 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100313 } else {
Marc Kupietzac707b32018-12-20 11:36:38 +0100314 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100315 }
Marc Kupietz2dd2dd72017-12-01 22:08:14 +0100316 $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100317 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100318 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100319 }
320 }
Marc Kupietz56844a22019-08-02 15:12:19 +0200321
Marc Kupietz000ad862016-02-26 14:59:12 +0100322 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200323 if($json) {
324 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100325 } elsif($csv) {
326 my $csv_data="";
327 for (my $i=0; $i <= $no_nbs; $i++) {
328 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
329 }
330 for (my $i=0; $i < $no_nbs; $i++) {
331 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
332 }
333 chop $csv_data;
334 chop $csv_data;
Marc Kupietz56dbabe2019-12-10 14:33:57 +0100335 $csv_data .= "\n";
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100336 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200337 } else {
Marc Kupietzd7760b42019-02-21 09:01:44 +0100338 my $distantWords="";
339 if(!defined($word) || $word !~ /^\s*$/) {
340 $distantWords = getBiggestMergedDifferences();
341 }
342 $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 +0200343 }
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100344} => "paradigmaticAndSyntagmaticNbs";
Marc Kupietzdc22b982015-10-09 09:19:34 +0200345
Marc Kupietz30ca4342017-11-22 21:21:20 +0100346helper(bitvec2window => sub {
Marc Kupietz2b8d44a2019-12-09 10:38:16 +0100347hook(
348 after_render => sub {
349 my $c = shift;
350
351 # Only track valid routes
352 my $route = $c->current_route or return;
353
354 # This won't forward personalized information
355 my $hash = {
356 action_url => $c->req->url->to_abs,
357 action_name => $route,
358 ua => $c->req->headers->user_agent,
359 urlref => '',
360 send_image => 0,
361 dnt => 0,
362 cip => $c->remote_addr,
363 lang => $c->req->headers->accept_language,
364 uid => $c->random_string('piwik_rand_id')
365 };
366 # $c->app->log->info("PIWIK: counting " . $hash->{action_url} . "\nremote:" . $c->remote_addr);
367 # $c->app->log->info("PIWIK: tag " . $c->piwik_tag);
368
369 # Send track
370 $c->piwik->api_p(Track => $hash)->wait;
371
372 # $c->app->log->info("PIWIK: counted.");
373 }
374);
375
Marc Kupietz30ca4342017-11-22 21:21:20 +0100376 my ($self, $n) = @_;
377 my $str = unpack("B32", pack("N", $n));
378 $str =~ s/^\d{22}//;
379 $str =~ s/^(\d{5})/$1x/;
380 $str =~ s/0/ยท/g;
381 $str =~ s/1/+/g;
382 return $str;
383 });
384
Marc Kupietz1b856fa2019-12-07 23:01:43 +0100385hook(
386 after_render => sub {
387 my $c = shift;
388
389 # Only track valid routes
390 my $route = $c->current_route or return;
391
392 # This won't forward personalized information
393 my $hash = {
394 action_url => $c->url_for->to_abs,
395 action_name => $route,
396 ua => '',
397 urlref => '',
398 send_image => 0,
399 dnt => 0,
400 uid => $c->random_string('piwik_rand_id')
401 };
402 $c->app->log->info("PIWIK: counting " . $hash->{action_url});
403 $c->app->log->info("PIWIK: tag " . $c->piwik_tag);
404
405 # Send track
406 $c->piwik->api_p(Track => $hash)->wait;
407 }
408);
409
Marc Kupietz95104512019-12-05 10:13:05 +0100410$daemon->run;
411# app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200412
Marc Kupietz95104512019-12-05 10:13:05 +0100413# exit;