Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 1 | #!/usr/local/bin/perl |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 2 | use 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 Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 3 | #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 Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 5 | use Mojolicious::Lite; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 6 | use Mojo::JSON qw(decode_json encode_json to_json); |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 7 | use base 'Mojolicious::Plugin'; |
| 8 | |
Marc Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 9 | use Encode qw(decode encode); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 10 | use Getopt::Std; |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame^] | 11 | #use Mojo::Server::Daemon; |
Marc Kupietz | ffef930 | 2017-11-07 15:58:01 +0100 | [diff] [blame] | 12 | use Cwd; |
Marc Kupietz | 66bfd95 | 2017-12-11 09:59:45 +0100 | [diff] [blame] | 13 | |
Marc Kupietz | 397ce85 | 2020-07-13 17:52:21 +0200 | [diff] [blame] | 14 | my $DEFAULT_VECS="/vol/work/kupietz/Work2/kl/trunk/Analysemethoden/word2vec/models/dereko-2020-ii-ALPHA.vecs"; |
| 15 | my $DEFAULT_NET = $DEFAULT_VECS; |
| 16 | $DEFAULT_NET =~ s/\.vecs/.net/; |
| 17 | |
Marc Kupietz | ffef930 | 2017-11-07 15:58:01 +0100 | [diff] [blame] | 18 | app->static->paths->[0] = getcwd; |
| 19 | |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 20 | plugin Config => {file => 'w2v-server.conf'}; |
| 21 | plugin 'Piwik'; |
Marc Kupietz | 2b8d44a | 2019-12-09 10:38:16 +0100 | [diff] [blame] | 22 | plugin "RemoteAddr"; |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 23 | plugin 'Util::RandomString' => { |
| 24 | piwik_rand_id => { |
| 25 | alphabet => '0123456789abcdef', |
| 26 | length => 16 |
| 27 | } |
| 28 | }; |
| 29 | |
Marc Kupietz | d422739 | 2016-03-01 16:45:12 +0100 | [diff] [blame] | 30 | plugin 'Log::Access'; |
Marc Kupietz | b3422c1 | 2017-07-04 14:12:11 +0200 | [diff] [blame] | 31 | plugin "RequestBase"; |
Marc Kupietz | 9510451 | 2019-12-05 10:13:05 +0100 | [diff] [blame] | 32 | #plugin 'AutoReload'; |
| 33 | plugin Localize => { |
| 34 | dict => { |
| 35 | _ => sub { $_->locale }, |
| 36 | }, |
| 37 | resources => ['w2v-server.dict'] |
| 38 | }; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 39 | our $opt_i = 0; # latin1-input? |
| 40 | our $opt_l = undef; |
| 41 | our $opt_p = 5676; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 42 | our $opt_m; |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 43 | our $opt_M; |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame^] | 44 | our $opt_n = $DEFAULT_NET; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 45 | our $opt_d; |
Marc Kupietz | fa19426 | 2018-06-05 09:39:32 +0200 | [diff] [blame] | 46 | our $opt_D; |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame^] | 47 | our $opt_G = 1; |
| 48 | our $opt_C; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 49 | |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 50 | my %marked; |
Marc Kupietz | c053d97 | 2019-01-10 10:41:51 +0100 | [diff] [blame] | 51 | my $title=""; |
Marc Kupietz | 793413b | 2016-04-02 21:48:57 +0200 | [diff] [blame] | 52 | my $training_args=""; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 53 | my $mergedEnd=0; |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 54 | my %cache; |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 55 | my %cccache; # classic collocator cache |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 56 | my %spcache; # similar profile cache |
Marc Kupietz | 793413b | 2016-04-02 21:48:57 +0200 | [diff] [blame] | 57 | |
Marc Kupietz | 3080f17 | 2020-07-13 17:51:28 +0200 | [diff] [blame] | 58 | getopts('d:D:Gil:p:m:n:M:C'); |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 59 | |
| 60 | if($opt_M) { |
Marc Kupietz | ed93021 | 2016-04-27 15:42:38 +0200 | [diff] [blame] | 61 | open my $handle, '<:encoding(UTF-8)', $opt_M |
| 62 | or die "Can't open '$opt_M' for reading: $!"; |
| 63 | while(<$handle>) { |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 64 | foreach my $mw (split /\s+/) { |
| 65 | $marked{$mw}=1 |
| 66 | } |
| 67 | } |
Marc Kupietz | ed93021 | 2016-04-27 15:42:38 +0200 | [diff] [blame] | 68 | close($handle); |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 69 | } |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 70 | |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame^] | 71 | my $vecs_name = (@ARGV > 0 && -r $ARGV[0] ? $ARGV[0] : $DEFAULT_VECS); |
| 72 | init_net($vecs_name, $opt_n, ($opt_i? 1 : 0), 1); |
| 73 | if(open(FILE, "$vecs_name.args")) { |
| 74 | $training_args = <FILE>; |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 75 | } |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame^] | 76 | close(FILE); |
| 77 | $title = fname2corpusname($vecs_name); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 78 | |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame^] | 79 | my $have_sprofiles = load_sprofiles($vecs_name); |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 80 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 81 | if($opt_m) { |
| 82 | $mergedEnd = mergeVectors($opt_m); |
Marc Kupietz | c053d97 | 2019-01-10 10:41:51 +0100 | [diff] [blame] | 83 | $title = "<span class=\"merged\">" . $title . "</span> vs. " . fname2corpusname($opt_m); |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Marc Kupietz | e5568a0 | 2018-12-20 11:42:02 +0100 | [diff] [blame] | 86 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 87 | if($opt_d) { # -d: dump vecs and exit |
| 88 | dump_vecs($opt_d); |
| 89 | exit; |
| 90 | } |
| 91 | |
Marc Kupietz | fa19426 | 2018-06-05 09:39:32 +0200 | [diff] [blame] | 92 | if($opt_D) { # -D: dump vecs for numpy and exit |
| 93 | dump_for_numpy($opt_D); |
| 94 | exit; |
| 95 | } |
| 96 | |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame^] | 97 | #my $daemon = Mojo::Server::Daemon->new( |
| 98 | # app => app, |
| 99 | # listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"] |
| 100 | #); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 101 | |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 102 | if($opt_G) { |
| 103 | print "Filtering garbage\n"; |
Marc Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 104 | filter_garbage(); |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Marc Kupietz | 554aff5 | 2017-11-09 14:42:09 +0100 | [diff] [blame] | 107 | get '*/js/*' => sub { |
Marc Kupietz | ffef930 | 2017-11-07 15:58:01 +0100 | [diff] [blame] | 108 | my $c = shift; |
| 109 | my $url = $c->req->url; |
| 110 | $url =~ s@/derekovecs@@g; |
| 111 | $c->app->log->info("GET: " . $url); |
Marc Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 112 | $c->reply->static($url); |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 113 | } => 'js'; |
Marc Kupietz | ffef930 | 2017-11-07 15:58:01 +0100 | [diff] [blame] | 114 | |
Marc Kupietz | a927057 | 2018-03-17 15:17:07 +0100 | [diff] [blame] | 115 | get '*/css/*' => sub { |
| 116 | my $c = shift; |
| 117 | my $url = $c->req->url; |
| 118 | $url =~ s@/derekovecs/@/@g; |
| 119 | $c->app->log->info("GET: " . $url); |
| 120 | $c->reply->static($url); |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 121 | } => 'css'; |
Marc Kupietz | a927057 | 2018-03-17 15:17:07 +0100 | [diff] [blame] | 122 | |
Marc Kupietz | c053d97 | 2019-01-10 10:41:51 +0100 | [diff] [blame] | 123 | sub fname2corpusname { |
| 124 | ($_) = @_; |
| 125 | s@.*/@@; |
Marc Kupietz | 86b5029 | 2019-02-17 21:03:59 +0100 | [diff] [blame] | 126 | s@\.en@-en@; |
Marc Kupietz | c053d97 | 2019-01-10 10:41:51 +0100 | [diff] [blame] | 127 | s@\..*@@; |
| 128 | return $_; |
| 129 | } |
| 130 | |
Marc Kupietz | cb43e49 | 2019-12-03 10:07:53 +0100 | [diff] [blame] | 131 | sub getWord { |
| 132 | ($_) = @_; |
| 133 | if ($_ =~ /^\d+/) { |
| 134 | return $_; |
| 135 | } else { |
| 136 | return getWordNumber($_); |
| 137 | } |
| 138 | } |
| 139 | |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 140 | sub getClassicCollocatorsCached { |
| 141 | my ($c, $word) = @_; |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 142 | my $s2 = ""; |
Marc Kupietz | 9ff3c99 | 2019-02-04 12:32:54 +0100 | [diff] [blame] | 143 | if($word > $mergedEnd) { |
| 144 | $word-=$mergedEnd; |
| 145 | } |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 146 | |
Marc Kupietz | 999ab8c | 2019-02-17 21:42:21 +0100 | [diff] [blame] | 147 | if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference |
Marc Kupietz | 0dd689e | 2020-01-14 16:07:18 +0100 | [diff] [blame] | 148 | open PIPE, "GET http://corpora.ids-mannheim.de/openlab/derekovecs/getClassicCollocators?w=$word |"; |
Marc Kupietz | 999ab8c | 2019-02-17 21:42:21 +0100 | [diff] [blame] | 149 | } |
Marc Kupietz | 3080f17 | 2020-07-13 17:51:28 +0200 | [diff] [blame] | 150 | if($opt_C || !$cccache{$word}) { |
Marc Kupietz | 06d6129 | 2019-02-04 12:33:22 +0100 | [diff] [blame] | 151 | $c->app->log->info("Getting classic collocates of $word."); |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 152 | $cccache{$word} = getClassicCollocators($word); |
Marc Kupietz | 1d96a08 | 2019-02-18 09:29:06 +0100 | [diff] [blame] | 153 | $cccache{$word} =~ s/:(-?)(nan|inf)/:"${1}${2}"/g; |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 154 | } else { |
Marc Kupietz | 06d6129 | 2019-02-04 12:33:22 +0100 | [diff] [blame] | 155 | $c->app->log->info("Getting classic collocates for $word from cache."); |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 156 | } |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 157 | if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference |
Marc Kupietz | 999ab8c | 2019-02-17 21:42:21 +0100 | [diff] [blame] | 158 | while(<PIPE>) { |
| 159 | $s2 .= $_; |
| 160 | } |
| 161 | close(PIPE); |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 162 | } |
Marc Kupietz | 999ab8c | 2019-02-17 21:42:21 +0100 | [diff] [blame] | 163 | |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 164 | if(length($s2) > 2000) { |
| 165 | my $d1 = decode_json($cccache{$word}); |
| 166 | my $d2 = decode_json($s2); |
| 167 | my %d2ld; |
Marc Kupietz | 001bffd | 2019-02-21 08:52:41 +0100 | [diff] [blame] | 168 | my $minLd = 14; |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 169 | foreach my $i (@{$d2->{collocates}}) { |
| 170 | $d2ld{$i->{word}}=$i->{ld}; |
Marc Kupietz | 001bffd | 2019-02-21 08:52:41 +0100 | [diff] [blame] | 171 | $minLd=$i->{ld} if($i->{ld} < $minLd); |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 172 | } |
| 173 | foreach my $i (@{$d1->{collocates}}) { |
| 174 | my $w = $i->{word}; |
Marc Kupietz | 001bffd | 2019-02-21 08:52:41 +0100 | [diff] [blame] | 175 | $i->{delta} = $i->{ld} - (defined $d2ld{$w} ? $d2ld{$w} : $minLd-0.1); |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 176 | } |
| 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 Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 187 | sub 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 Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 193 | } |
| 194 | return $spcache{$word}; |
| 195 | } |
| 196 | |
Marc Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 197 | post '/derekovecs/getVecsByRanks' => sub { |
Marc Kupietz | 66bfd95 | 2017-12-11 09:59:45 +0100 | [diff] [blame] | 198 | my $self = shift; |
| 199 | my $vec = getVecs($self->req->json); |
| 200 | $self->render(json => $vec); |
| 201 | }; |
Marc Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 202 | |
Marc Kupietz | e13a355 | 2018-01-25 08:48:34 +0100 | [diff] [blame] | 203 | any '*/getClassicCollocators' => sub { |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 204 | my $self = shift; |
Marc Kupietz | cb43e49 | 2019-12-03 10:07:53 +0100 | [diff] [blame] | 205 | $self->render(data => getClassicCollocatorsCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json'); |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 206 | } => 'getClassicCollocators1'; |
Marc Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 207 | |
Marc Kupietz | e13a355 | 2018-01-25 08:48:34 +0100 | [diff] [blame] | 208 | any '/getClassicCollocators' => sub { |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 209 | my $self = shift; |
Marc Kupietz | cb43e49 | 2019-12-03 10:07:53 +0100 | [diff] [blame] | 210 | $self->render(data => getClassicCollocatorsCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json'); |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 211 | } => 'getClassicCollocators'; |
Marc Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 212 | |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 213 | any '/getBiggestVocabDistances' => sub { |
| 214 | my $self = shift; |
| 215 | $self->render(data => getBiggestMergedDifferences(), format=>'json'); |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 216 | } => 'getBiggestVocabDistances1'; |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 217 | |
| 218 | any '*/getBiggestVocabDistances' => sub { |
| 219 | my $self = shift; |
| 220 | $self->render(data => getBiggestMergedDifferences(), format=>'json'); |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 221 | } => 'getBiggestVocabDistances'; |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 222 | |
Marc Kupietz | 33c79d3 | 2019-08-02 15:11:23 +0200 | [diff] [blame] | 223 | any '*/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 | |
| 232 | any '/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 Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 241 | any '*/getSimilarProfiles' => sub { |
| 242 | my $self = shift; |
Marc Kupietz | cb43e49 | 2019-12-03 10:07:53 +0100 | [diff] [blame] | 243 | $self->render(data => getSimilarProfilesCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json'); |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 244 | }; |
| 245 | |
Marc Kupietz | c987fa8 | 2018-03-21 12:14:25 +0100 | [diff] [blame] | 246 | any '/getSimilarProfiles' => sub { |
| 247 | my $self = shift; |
Marc Kupietz | cb43e49 | 2019-12-03 10:07:53 +0100 | [diff] [blame] | 248 | $self->render(data => getSimilarProfilesCached($self, getWord($self->param("w") ? $self->param("w") : $self->req->json)), format=>'json'); |
Marc Kupietz | c987fa8 | 2018-03-21 12:14:25 +0100 | [diff] [blame] | 249 | }; |
| 250 | |
Marc Kupietz | 9f30157 | 2020-04-06 18:29:16 +0200 | [diff] [blame] | 251 | any '*/getWord' => sub { |
| 252 | my $self = shift; |
| 253 | my $w = $self->param("w"); |
| 254 | my $rank = getWord($w); |
| 255 | my $status = 200; |
| 256 | if ($rank <= 0) { |
| 257 | $rank = -1; |
| 258 | $status = 404; |
| 259 | } |
| 260 | $self->render(data => encode_json({word => $w, frequencyRank => $rank}), format => 'json', status => $status); |
| 261 | }; |
| 262 | |
| 263 | any '/getWord' => sub { |
| 264 | my $self = shift; |
| 265 | my $w = $self->param("w"); |
| 266 | my $rank = getWord($w); |
| 267 | my $status = 200; |
| 268 | if ($rank <= 0) { |
| 269 | $rank = -1; |
| 270 | $status = 404; |
| 271 | } |
| 272 | $self->render(data => encode_json({word => $w, frequencyRank => $rank}), format => 'json', status => $status); |
| 273 | }; |
| 274 | |
Marc Kupietz | 98ed1c0 | 2019-08-02 15:05:37 +0200 | [diff] [blame] | 275 | any '/getSimilarity' => sub { |
| 276 | my $self = shift; |
| 277 | my $w1 = $self->param("w1"); |
| 278 | my $w2 = $self->param("w2"); |
| 279 | $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json'); |
| 280 | }; |
| 281 | |
| 282 | any '*/getSimilarity' => sub { |
| 283 | my $self = shift; |
| 284 | my $w1 = $self->param("w1"); |
| 285 | my $w2 = $self->param("w2"); |
| 286 | $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json'); |
| 287 | }; |
| 288 | |
Marc Kupietz | df3d4b5 | 2017-11-29 16:57:27 +0100 | [diff] [blame] | 289 | get '*/img/*' => sub { |
| 290 | my $c = shift; |
| 291 | my $url = $c->req->url; |
| 292 | $url =~ s@/derekovecs@@g; |
| 293 | $c->app->log->info("GET: " . $url); |
| 294 | $c->reply->static($url); |
| 295 | }; |
| 296 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 297 | get '/' => sub { |
| 298 | my $c = shift; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 299 | $c->app->log->info("get: ".$c->req->url->to_abs); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 300 | my $word=$c->param('word'); |
Marc Kupietz | 2da2a81 | 2019-02-21 14:17:35 +0100 | [diff] [blame] | 301 | my $no_nbs=$c->param('n') || ($opt_m? 50 : 100); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 302 | my $no_iterations=$c->param('N') || 2000; |
Marc Kupietz | d422739 | 2016-03-01 16:45:12 +0100 | [diff] [blame] | 303 | my $perplexity=$c->param('perplexity') || 20; |
Marc Kupietz | c4d62f8 | 2016-03-01 11:04:24 +0100 | [diff] [blame] | 304 | my $epsilon=$c->param('epsilon') || 5; |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 305 | my $som=$c->param('som') || 0; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 306 | my $searchBaseVocabFirst=$c->param('sbf') || 0; |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 307 | my $sort=$c->param('sort') || 0; |
Marc Kupietz | c469f3b | 2017-11-13 14:07:36 +0100 | [diff] [blame] | 308 | my $csv=$c->param('csv') || 0; |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 309 | my $json=$c->param('json') || 0; |
Marc Kupietz | db2dc7e | 2017-12-02 12:04:03 +0100 | [diff] [blame] | 310 | my $cutoff=$c->param('cutoff') || 500000; |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 311 | my $dedupe=$c->param('dedupe') || 0; |
Marc Kupietz | ac707b3 | 2018-12-20 11:36:38 +0100 | [diff] [blame] | 312 | my $nosp=$c->param('nosp') || 0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 313 | my $res; |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 314 | my @lists; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 315 | my @collocations; |
Marc Kupietz | cddc848 | 2019-12-04 08:57:33 +0100 | [diff] [blame] | 316 | if(defined($word) && $word !~ /^\s*$/) { |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 317 | $c->inactivity_timeout(300); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 318 | $word =~ s/\s+/ /g; |
Marc Kupietz | 3082fd0 | 2019-01-09 14:54:06 +0100 | [diff] [blame] | 319 | if($opt_m && $word !~ /\|/) { |
| 320 | $word .= "|$word"; |
| 321 | } |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 322 | for my $w (split(' *\| *', $word)) { |
Marc Kupietz | 3082fd0 | 2019-01-09 14:54:06 +0100 | [diff] [blame] | 323 | if($opt_m) { |
| 324 | if($searchBaseVocabFirst) { |
| 325 | $searchBaseVocabFirst=0; |
| 326 | } else { |
| 327 | $searchBaseVocabFirst=1; |
| 328 | } |
| 329 | } |
| 330 | if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe,$searchBaseVocabFirst}) { |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 331 | $c->app->log->info("Getting $w results from cache"); |
Marc Kupietz | 3082fd0 | 2019-01-09 14:54:06 +0100 | [diff] [blame] | 332 | $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe.$searchBaseVocabFirst} |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 333 | } else { |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 334 | $c->app->log->info('Looking for neighbours of '.$w); |
| 335 | if($opt_i) { |
Marc Kupietz | ac707b3 | 2018-12-20 11:36:38 +0100 | [diff] [blame] | 336 | $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp); |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 337 | } else { |
Marc Kupietz | ac707b3 | 2018-12-20 11:36:38 +0100 | [diff] [blame] | 338 | $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp); |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 339 | } |
Marc Kupietz | 2dd2dd7 | 2017-12-01 22:08:14 +0100 | [diff] [blame] | 340 | $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 341 | } |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 342 | push(@lists, $res->{paradigmatic}); |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 343 | } |
| 344 | } |
Marc Kupietz | 56844a2 | 2019-08-02 15:12:19 +0200 | [diff] [blame] | 345 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 346 | $word =~ s/ *\| */ | /g; |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 347 | if($json) { |
| 348 | return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}}); |
Marc Kupietz | c469f3b | 2017-11-13 14:07:36 +0100 | [diff] [blame] | 349 | } elsif($csv) { |
| 350 | my $csv_data=""; |
| 351 | for (my $i=0; $i <= $no_nbs; $i++) { |
| 352 | $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", "; |
| 353 | } |
| 354 | for (my $i=0; $i < $no_nbs; $i++) { |
| 355 | $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", "; |
| 356 | } |
| 357 | chop $csv_data; |
| 358 | chop $csv_data; |
Marc Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 359 | $csv_data .= "\n"; |
Marc Kupietz | c469f3b | 2017-11-13 14:07:36 +0100 | [diff] [blame] | 360 | return $c->render(text=>$csv_data); |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 361 | } else { |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 362 | my $distantWords=""; |
| 363 | if(!defined($word) || $word !~ /^\s*$/) { |
| 364 | $distantWords = getBiggestMergedDifferences(); |
| 365 | } |
| 366 | $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 Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 367 | } |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 368 | } => "paradigmaticAndSyntagmaticNbs"; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 369 | |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 370 | helper(bitvec2window => sub { |
| 371 | my ($self, $n) = @_; |
| 372 | my $str = unpack("B32", pack("N", $n)); |
| 373 | $str =~ s/^\d{22}//; |
| 374 | $str =~ s/^(\d{5})/$1x/; |
| 375 | $str =~ s/0/ยท/g; |
| 376 | $str =~ s/1/+/g; |
| 377 | return $str; |
| 378 | }); |
| 379 | |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 380 | hook( |
| 381 | after_render => sub { |
| 382 | my $c = shift; |
| 383 | |
| 384 | # Only track valid routes |
| 385 | my $route = $c->current_route or return; |
| 386 | |
| 387 | # This won't forward personalized information |
| 388 | my $hash = { |
Marc Kupietz | 251de9f | 2020-01-14 16:12:05 +0100 | [diff] [blame] | 389 | action_url => $c->req->url->to_abs, |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 390 | action_name => $route, |
Marc Kupietz | 251de9f | 2020-01-14 16:12:05 +0100 | [diff] [blame] | 391 | ua => $c->req->headers->user_agent, |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 392 | urlref => '', |
| 393 | send_image => 0, |
| 394 | dnt => 0, |
Marc Kupietz | 251de9f | 2020-01-14 16:12:05 +0100 | [diff] [blame] | 395 | cip => $c->remote_addr, |
| 396 | lang => $c->req->headers->accept_language, |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 397 | uid => $c->random_string('piwik_rand_id') |
| 398 | }; |
Marc Kupietz | 251de9f | 2020-01-14 16:12:05 +0100 | [diff] [blame] | 399 | # $c->app->log->info("PIWIK: counting " . $hash->{action_url} . "\nremote:" . $c->remote_addr); |
| 400 | # $c->app->log->info("PIWIK: tag " . $c->piwik_tag); |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 401 | |
| 402 | # Send track |
| 403 | $c->piwik->api_p(Track => $hash)->wait; |
Marc Kupietz | 251de9f | 2020-01-14 16:12:05 +0100 | [diff] [blame] | 404 | |
| 405 | # $c->app->log->info("PIWIK: counted."); |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 406 | } |
| 407 | ); |
| 408 | |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame^] | 409 | app->start; |
| 410 | #$daemon->run; |
Marc Kupietz | 9510451 | 2019-12-05 10:13:05 +0100 | [diff] [blame] | 411 | # app->start; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 412 | |
Marc Kupietz | 9510451 | 2019-12-05 10:13:05 +0100 | [diff] [blame] | 413 | # exit; |