Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 1 | #!/usr/local/bin/perl |
Marc K | 20476c7 | 2021-03-11 12:18:01 +0100 | [diff] [blame^] | 2 | use Inline C => "./derekovecs-server.c" => CLEAN_AFTER_BUILD => 0, BUILD_NOISY => 1, ccflags => $Config{ccflags} . "-Wall -O4", libs => "-L. -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 K | 20476c7 | 2021-03-11 12:18:01 +0100 | [diff] [blame^] | 14 | my $mojo_config = $ENV{MOJO_CONFIG} // 'derekovecs-server.conf'; |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 15 | plugin Config => {file => $mojo_config}; |
| 16 | |
Marc K | 20476c7 | 2021-03-11 12:18:01 +0100 | [diff] [blame^] | 17 | my $DEFAULT_VECS = app->config->{w2v}->{vecs} // "../models/dereko-2021-i.vecs"; |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 18 | my $DEFAULT_NET_NAME = ""; |
| 19 | if ($DEFAULT_VECS=~ /\.vecs/) { |
| 20 | $DEFAULT_NET_NAME = $DEFAULT_VECS; |
| 21 | $DEFAULT_NET_NAME =~ s/\.vecs/.net/; |
| 22 | } |
| 23 | my $DEFAULT_NET = app->config->{w2v}->{net} // $DEFAULT_NET_NAME; |
Marc Kupietz | 397ce85 | 2020-07-13 17:52:21 +0200 | [diff] [blame] | 24 | |
Marc Kupietz | ffef930 | 2017-11-07 15:58:01 +0100 | [diff] [blame] | 25 | app->static->paths->[0] = getcwd; |
| 26 | |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 27 | plugin 'Piwik'; |
Marc Kupietz | 2b8d44a | 2019-12-09 10:38:16 +0100 | [diff] [blame] | 28 | plugin "RemoteAddr"; |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 29 | plugin 'Util::RandomString' => { |
| 30 | piwik_rand_id => { |
| 31 | alphabet => '0123456789abcdef', |
| 32 | length => 16 |
| 33 | } |
| 34 | }; |
| 35 | |
Marc Kupietz | d422739 | 2016-03-01 16:45:12 +0100 | [diff] [blame] | 36 | plugin 'Log::Access'; |
Marc Kupietz | b3422c1 | 2017-07-04 14:12:11 +0200 | [diff] [blame] | 37 | plugin "RequestBase"; |
Marc Kupietz | 9510451 | 2019-12-05 10:13:05 +0100 | [diff] [blame] | 38 | #plugin 'AutoReload'; |
| 39 | plugin Localize => { |
| 40 | dict => { |
| 41 | _ => sub { $_->locale }, |
| 42 | }, |
Marc K | 20476c7 | 2021-03-11 12:18:01 +0100 | [diff] [blame^] | 43 | resources => ['derekovecs-server.dict'] |
Marc Kupietz | 9510451 | 2019-12-05 10:13:05 +0100 | [diff] [blame] | 44 | }; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 45 | our $opt_i = 0; # latin1-input? |
| 46 | our $opt_l = undef; |
| 47 | our $opt_p = 5676; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 48 | our $opt_m; |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 49 | our $opt_M; |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame] | 50 | our $opt_n = $DEFAULT_NET; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 51 | our $opt_d; |
Marc Kupietz | fa19426 | 2018-06-05 09:39:32 +0200 | [diff] [blame] | 52 | our $opt_D; |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame] | 53 | our $opt_G = 1; |
| 54 | our $opt_C; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 55 | |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 56 | my %marked; |
Marc Kupietz | c053d97 | 2019-01-10 10:41:51 +0100 | [diff] [blame] | 57 | my $title=""; |
Marc Kupietz | 793413b | 2016-04-02 21:48:57 +0200 | [diff] [blame] | 58 | my $training_args=""; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 59 | my $mergedEnd=0; |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 60 | my %cache; |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 61 | my %cccache; # classic collocator cache |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 62 | my %spcache; # similar profile cache |
Marc Kupietz | 793413b | 2016-04-02 21:48:57 +0200 | [diff] [blame] | 63 | |
Marc Kupietz | 3080f17 | 2020-07-13 17:51:28 +0200 | [diff] [blame] | 64 | getopts('d:D:Gil:p:m:n:M:C'); |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 65 | |
| 66 | if($opt_M) { |
Marc Kupietz | ed93021 | 2016-04-27 15:42:38 +0200 | [diff] [blame] | 67 | open my $handle, '<:encoding(UTF-8)', $opt_M |
| 68 | or die "Can't open '$opt_M' for reading: $!"; |
| 69 | while(<$handle>) { |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 70 | foreach my $mw (split /\s+/) { |
| 71 | $marked{$mw}=1 |
| 72 | } |
| 73 | } |
Marc Kupietz | ed93021 | 2016-04-27 15:42:38 +0200 | [diff] [blame] | 74 | close($handle); |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 75 | } |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 76 | |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame] | 77 | my $vecs_name = (@ARGV > 0 && -r $ARGV[0] ? $ARGV[0] : $DEFAULT_VECS); |
| 78 | init_net($vecs_name, $opt_n, ($opt_i? 1 : 0), 1); |
| 79 | if(open(FILE, "$vecs_name.args")) { |
| 80 | $training_args = <FILE>; |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 81 | } |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame] | 82 | close(FILE); |
| 83 | $title = fname2corpusname($vecs_name); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 84 | |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame] | 85 | my $have_sprofiles = load_sprofiles($vecs_name); |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 86 | |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 87 | if (app->config->{w2v}->{merge}) { |
| 88 | $opt_m = app->config->{w2v}->{merge}; |
| 89 | } |
| 90 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 91 | if($opt_m) { |
| 92 | $mergedEnd = mergeVectors($opt_m); |
Marc Kupietz | c053d97 | 2019-01-10 10:41:51 +0100 | [diff] [blame] | 93 | $title = "<span class=\"merged\">" . $title . "</span> vs. " . fname2corpusname($opt_m); |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Marc Kupietz | e5568a0 | 2018-12-20 11:42:02 +0100 | [diff] [blame] | 96 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 97 | if($opt_d) { # -d: dump vecs and exit |
| 98 | dump_vecs($opt_d); |
| 99 | exit; |
| 100 | } |
| 101 | |
Marc Kupietz | fa19426 | 2018-06-05 09:39:32 +0200 | [diff] [blame] | 102 | if($opt_D) { # -D: dump vecs for numpy and exit |
| 103 | dump_for_numpy($opt_D); |
| 104 | exit; |
| 105 | } |
| 106 | |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame] | 107 | #my $daemon = Mojo::Server::Daemon->new( |
| 108 | # app => app, |
| 109 | # listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"] |
| 110 | #); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 111 | |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 112 | if($opt_G) { |
| 113 | print "Filtering garbage\n"; |
Marc Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 114 | filter_garbage(); |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 115 | print "Finished filtering garbage\n"; |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Marc Kupietz | 554aff5 | 2017-11-09 14:42:09 +0100 | [diff] [blame] | 118 | get '*/js/*' => sub { |
Marc Kupietz | ffef930 | 2017-11-07 15:58:01 +0100 | [diff] [blame] | 119 | my $c = shift; |
| 120 | my $url = $c->req->url; |
Marc K | 20476c7 | 2021-03-11 12:18:01 +0100 | [diff] [blame^] | 121 | $url =~ s@/derekovecs/@/@g; |
Marc Kupietz | ffef930 | 2017-11-07 15:58:01 +0100 | [diff] [blame] | 122 | $c->app->log->info("GET: " . $url); |
Marc Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 123 | $c->reply->static($url); |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 124 | } => 'js'; |
Marc Kupietz | ffef930 | 2017-11-07 15:58:01 +0100 | [diff] [blame] | 125 | |
Marc Kupietz | a927057 | 2018-03-17 15:17:07 +0100 | [diff] [blame] | 126 | get '*/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 Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 132 | } => 'css'; |
Marc Kupietz | a927057 | 2018-03-17 15:17:07 +0100 | [diff] [blame] | 133 | |
Marc Kupietz | c053d97 | 2019-01-10 10:41:51 +0100 | [diff] [blame] | 134 | sub fname2corpusname { |
| 135 | ($_) = @_; |
| 136 | s@.*/@@; |
Marc Kupietz | 86b5029 | 2019-02-17 21:03:59 +0100 | [diff] [blame] | 137 | s@\.en@-en@; |
Marc Kupietz | c053d97 | 2019-01-10 10:41:51 +0100 | [diff] [blame] | 138 | s@\..*@@; |
| 139 | return $_; |
| 140 | } |
| 141 | |
Marc Kupietz | cb43e49 | 2019-12-03 10:07:53 +0100 | [diff] [blame] | 142 | sub getWord { |
| 143 | ($_) = @_; |
| 144 | if ($_ =~ /^\d+/) { |
| 145 | return $_; |
| 146 | } else { |
| 147 | return getWordNumber($_); |
| 148 | } |
| 149 | } |
| 150 | |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 151 | sub getClassicCollocatorsCached { |
| 152 | my ($c, $word) = @_; |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 153 | my $s2 = ""; |
Marc Kupietz | 9ff3c99 | 2019-02-04 12:32:54 +0100 | [diff] [blame] | 154 | if($word > $mergedEnd) { |
| 155 | $word-=$mergedEnd; |
| 156 | } |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 157 | |
Marc Kupietz | 999ab8c | 2019-02-17 21:42:21 +0100 | [diff] [blame] | 158 | if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference |
Marc Kupietz | 0dd689e | 2020-01-14 16:07:18 +0100 | [diff] [blame] | 159 | 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] | 160 | } |
Marc Kupietz | 3080f17 | 2020-07-13 17:51:28 +0200 | [diff] [blame] | 161 | if($opt_C || !$cccache{$word}) { |
Marc Kupietz | 06d6129 | 2019-02-04 12:33:22 +0100 | [diff] [blame] | 162 | $c->app->log->info("Getting classic collocates of $word."); |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 163 | $cccache{$word} = getClassicCollocators($word); |
Marc Kupietz | 1d96a08 | 2019-02-18 09:29:06 +0100 | [diff] [blame] | 164 | $cccache{$word} =~ s/:(-?)(nan|inf)/:"${1}${2}"/g; |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 165 | } else { |
Marc Kupietz | 06d6129 | 2019-02-04 12:33:22 +0100 | [diff] [blame] | 166 | $c->app->log->info("Getting classic collocates for $word from cache."); |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 167 | } |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 168 | if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference |
Marc Kupietz | 999ab8c | 2019-02-17 21:42:21 +0100 | [diff] [blame] | 169 | while(<PIPE>) { |
| 170 | $s2 .= $_; |
| 171 | } |
| 172 | close(PIPE); |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 173 | } |
Marc Kupietz | 999ab8c | 2019-02-17 21:42:21 +0100 | [diff] [blame] | 174 | |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 175 | if(length($s2) > 2000) { |
| 176 | my $d1 = decode_json($cccache{$word}); |
| 177 | my $d2 = decode_json($s2); |
| 178 | my %d2ld; |
Marc Kupietz | 001bffd | 2019-02-21 08:52:41 +0100 | [diff] [blame] | 179 | my $minLd = 14; |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 180 | foreach my $i (@{$d2->{collocates}}) { |
| 181 | $d2ld{$i->{word}}=$i->{ld}; |
Marc Kupietz | 001bffd | 2019-02-21 08:52:41 +0100 | [diff] [blame] | 182 | $minLd=$i->{ld} if($i->{ld} < $minLd); |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 183 | } |
| 184 | foreach my $i (@{$d1->{collocates}}) { |
| 185 | my $w = $i->{word}; |
Marc Kupietz | 001bffd | 2019-02-21 08:52:41 +0100 | [diff] [blame] | 186 | $i->{delta} = $i->{ld} - (defined $d2ld{$w} ? $d2ld{$w} : $minLd-0.1); |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 187 | } |
| 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 Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 196 | } |
| 197 | |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 198 | sub 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 Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 204 | } |
| 205 | return $spcache{$word}; |
| 206 | } |
| 207 | |
Marc Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 208 | post '/derekovecs/getVecsByRanks' => sub { |
Marc Kupietz | 66bfd95 | 2017-12-11 09:59:45 +0100 | [diff] [blame] | 209 | my $self = shift; |
| 210 | my $vec = getVecs($self->req->json); |
| 211 | $self->render(json => $vec); |
| 212 | }; |
Marc Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 213 | |
Marc Kupietz | e13a355 | 2018-01-25 08:48:34 +0100 | [diff] [blame] | 214 | any '*/getClassicCollocators' => sub { |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 215 | my $self = shift; |
Marc Kupietz | cb43e49 | 2019-12-03 10:07:53 +0100 | [diff] [blame] | 216 | $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] | 217 | } => 'getClassicCollocators1'; |
Marc Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 218 | |
Marc Kupietz | e13a355 | 2018-01-25 08:48:34 +0100 | [diff] [blame] | 219 | any '/getClassicCollocators' => sub { |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 220 | my $self = shift; |
Marc Kupietz | cb43e49 | 2019-12-03 10:07:53 +0100 | [diff] [blame] | 221 | $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] | 222 | } => 'getClassicCollocators'; |
Marc Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 223 | |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 224 | any '/getBiggestVocabDistances' => sub { |
| 225 | my $self = shift; |
| 226 | $self->render(data => getBiggestMergedDifferences(), format=>'json'); |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 227 | } => 'getBiggestVocabDistances1'; |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 228 | |
| 229 | any '*/getBiggestVocabDistances' => sub { |
| 230 | my $self = shift; |
| 231 | $self->render(data => getBiggestMergedDifferences(), format=>'json'); |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 232 | } => 'getBiggestVocabDistances'; |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 233 | |
Marc Kupietz | 33c79d3 | 2019-08-02 15:11:23 +0200 | [diff] [blame] | 234 | any '*/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 | |
| 243 | any '/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 Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 252 | any '*/getSimilarProfiles' => sub { |
| 253 | my $self = shift; |
Marc Kupietz | cb43e49 | 2019-12-03 10:07:53 +0100 | [diff] [blame] | 254 | $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] | 255 | }; |
| 256 | |
Marc Kupietz | c987fa8 | 2018-03-21 12:14:25 +0100 | [diff] [blame] | 257 | any '/getSimilarProfiles' => sub { |
| 258 | my $self = shift; |
Marc Kupietz | cb43e49 | 2019-12-03 10:07:53 +0100 | [diff] [blame] | 259 | $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] | 260 | }; |
| 261 | |
Marc Kupietz | 9f30157 | 2020-04-06 18:29:16 +0200 | [diff] [blame] | 262 | any '*/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 | |
| 274 | any '/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 Kupietz | 98ed1c0 | 2019-08-02 15:05:37 +0200 | [diff] [blame] | 286 | any '/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 | |
| 293 | any '*/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 Kupietz | df3d4b5 | 2017-11-29 16:57:27 +0100 | [diff] [blame] | 300 | get '*/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 Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 308 | get '/' => sub { |
| 309 | my $c = shift; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 310 | $c->app->log->info("get: ".$c->req->url->to_abs); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 311 | my $word=$c->param('word'); |
Marc Kupietz | 2da2a81 | 2019-02-21 14:17:35 +0100 | [diff] [blame] | 312 | my $no_nbs=$c->param('n') || ($opt_m? 50 : 100); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 313 | my $no_iterations=$c->param('N') || 2000; |
Marc Kupietz | d422739 | 2016-03-01 16:45:12 +0100 | [diff] [blame] | 314 | my $perplexity=$c->param('perplexity') || 20; |
Marc Kupietz | c4d62f8 | 2016-03-01 11:04:24 +0100 | [diff] [blame] | 315 | my $epsilon=$c->param('epsilon') || 5; |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 316 | my $som=$c->param('som') || 0; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 317 | my $searchBaseVocabFirst=$c->param('sbf') || 0; |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 318 | my $sort=$c->param('sort') || 0; |
Marc Kupietz | c469f3b | 2017-11-13 14:07:36 +0100 | [diff] [blame] | 319 | my $csv=$c->param('csv') || 0; |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 320 | my $json=$c->param('json') || 0; |
Marc Kupietz | db2dc7e | 2017-12-02 12:04:03 +0100 | [diff] [blame] | 321 | my $cutoff=$c->param('cutoff') || 500000; |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 322 | my $dedupe=$c->param('dedupe') || 0; |
Marc Kupietz | ac707b3 | 2018-12-20 11:36:38 +0100 | [diff] [blame] | 323 | my $nosp=$c->param('nosp') || 0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 324 | my $res; |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 325 | my @lists; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 326 | my @collocations; |
Marc Kupietz | cddc848 | 2019-12-04 08:57:33 +0100 | [diff] [blame] | 327 | if(defined($word) && $word !~ /^\s*$/) { |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 328 | $c->inactivity_timeout(300); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 329 | $word =~ s/\s+/ /g; |
Marc Kupietz | 3082fd0 | 2019-01-09 14:54:06 +0100 | [diff] [blame] | 330 | if($opt_m && $word !~ /\|/) { |
| 331 | $word .= "|$word"; |
| 332 | } |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 333 | for my $w (split(' *\| *', $word)) { |
Marc Kupietz | 3082fd0 | 2019-01-09 14:54:06 +0100 | [diff] [blame] | 334 | 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 Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 342 | $c->app->log->info("Getting $w results from cache"); |
Marc Kupietz | 3082fd0 | 2019-01-09 14:54:06 +0100 | [diff] [blame] | 343 | $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe.$searchBaseVocabFirst} |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 344 | } else { |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 345 | $c->app->log->info('Looking for neighbours of '.$w); |
| 346 | if($opt_i) { |
Marc Kupietz | ac707b3 | 2018-12-20 11:36:38 +0100 | [diff] [blame] | 347 | $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] | 348 | } else { |
Marc Kupietz | ac707b3 | 2018-12-20 11:36:38 +0100 | [diff] [blame] | 349 | $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp); |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 350 | } |
Marc Kupietz | 2dd2dd7 | 2017-12-01 22:08:14 +0100 | [diff] [blame] | 351 | $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 352 | } |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 353 | push(@lists, $res->{paradigmatic}); |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 354 | } |
| 355 | } |
Marc Kupietz | 56844a2 | 2019-08-02 15:12:19 +0200 | [diff] [blame] | 356 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 357 | $word =~ s/ *\| */ | /g; |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 358 | if($json) { |
| 359 | return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}}); |
Marc Kupietz | c469f3b | 2017-11-13 14:07:36 +0100 | [diff] [blame] | 360 | } 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 Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 370 | $csv_data .= "\n"; |
Marc Kupietz | c469f3b | 2017-11-13 14:07:36 +0100 | [diff] [blame] | 371 | return $c->render(text=>$csv_data); |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 372 | } else { |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 373 | 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 Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 378 | } |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 379 | } => "paradigmaticAndSyntagmaticNbs"; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 380 | |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 381 | helper(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 Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 391 | hook( |
| 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 Kupietz | 251de9f | 2020-01-14 16:12:05 +0100 | [diff] [blame] | 400 | action_url => $c->req->url->to_abs, |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 401 | action_name => $route, |
Marc Kupietz | 251de9f | 2020-01-14 16:12:05 +0100 | [diff] [blame] | 402 | ua => $c->req->headers->user_agent, |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 403 | urlref => '', |
| 404 | send_image => 0, |
| 405 | dnt => 0, |
Marc Kupietz | 251de9f | 2020-01-14 16:12:05 +0100 | [diff] [blame] | 406 | cip => $c->remote_addr, |
| 407 | lang => $c->req->headers->accept_language, |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 408 | uid => $c->random_string('piwik_rand_id') |
| 409 | }; |
Marc Kupietz | 251de9f | 2020-01-14 16:12:05 +0100 | [diff] [blame] | 410 | # $c->app->log->info("PIWIK: counting " . $hash->{action_url} . "\nremote:" . $c->remote_addr); |
| 411 | # $c->app->log->info("PIWIK: tag " . $c->piwik_tag); |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 412 | |
| 413 | # Send track |
| 414 | $c->piwik->api_p(Track => $hash)->wait; |
Marc Kupietz | 251de9f | 2020-01-14 16:12:05 +0100 | [diff] [blame] | 415 | |
| 416 | # $c->app->log->info("PIWIK: counted."); |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 417 | } |
| 418 | ); |
| 419 | |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame] | 420 | app->start; |
| 421 | #$daemon->run; |
Marc Kupietz | 9510451 | 2019-12-05 10:13:05 +0100 | [diff] [blame] | 422 | # app->start; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 423 | |
Marc Kupietz | 9510451 | 2019-12-05 10:13:05 +0100 | [diff] [blame] | 424 | # exit; |