Marc Kupietz | 66bbd2d | 2022-07-19 17:53:49 +0200 | [diff] [blame] | 1 | #!/usr/bin/env perl |
| 2 | |
Marc Kupietz | 91f021b | 2023-11-05 11:44:46 +0100 | [diff] [blame] | 3 | our $VERSION = '0.92'; |
Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 4 | |
Marc Kupietz | c82b15f | 2022-07-19 17:36:27 +0200 | [diff] [blame] | 5 | use IDS::DeReKoVecs::Read; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 6 | use Mojolicious::Lite; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 7 | use Mojo::JSON qw(decode_json encode_json to_json); |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 8 | use base 'Mojolicious::Plugin'; |
| 9 | |
Marc Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 10 | use Encode qw(decode encode); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 11 | use Getopt::Std; |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame] | 12 | #use Mojo::Server::Daemon; |
Marc Kupietz | ffef930 | 2017-11-07 15:58:01 +0100 | [diff] [blame] | 13 | use Cwd; |
Marc Kupietz | 66bfd95 | 2017-12-11 09:59:45 +0100 | [diff] [blame] | 14 | |
Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 15 | my $mojo_config = $ENV{MOJO_CONFIG} // '../derekovecs-server.conf'; |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 16 | plugin Config => {file => $mojo_config}; |
| 17 | |
Marc K | 20476c7 | 2021-03-11 12:18:01 +0100 | [diff] [blame] | 18 | my $DEFAULT_VECS = app->config->{w2v}->{vecs} // "../models/dereko-2021-i.vecs"; |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 19 | my $DEFAULT_NET_NAME = ""; |
| 20 | if ($DEFAULT_VECS=~ /\.vecs/) { |
| 21 | $DEFAULT_NET_NAME = $DEFAULT_VECS; |
| 22 | $DEFAULT_NET_NAME =~ s/\.vecs/.net/; |
| 23 | } |
Marc Kupietz | 9ae184c | 2023-11-04 18:21:54 +0100 | [diff] [blame] | 24 | my $DEFAULT_NET = app->config->{w2v}->{net} // $DEFAULT_NET_NAME; |
| 25 | my $DOWNTIME_CALENDAR_URL = app->config->{downtime_calendar_url} // ''; |
Marc Kupietz | ffef930 | 2017-11-07 15:58:01 +0100 | [diff] [blame] | 26 | app->static->paths->[0] = getcwd; |
| 27 | |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 28 | plugin 'Piwik'; |
Marc Kupietz | 2b8d44a | 2019-12-09 10:38:16 +0100 | [diff] [blame] | 29 | plugin "RemoteAddr"; |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 30 | plugin 'Util::RandomString' => { |
| 31 | piwik_rand_id => { |
| 32 | alphabet => '0123456789abcdef', |
| 33 | length => 16 |
| 34 | } |
| 35 | }; |
| 36 | |
Marc Kupietz | d422739 | 2016-03-01 16:45:12 +0100 | [diff] [blame] | 37 | plugin 'Log::Access'; |
Marc Kupietz | b3422c1 | 2017-07-04 14:12:11 +0200 | [diff] [blame] | 38 | plugin "RequestBase"; |
Marc Kupietz | 9510451 | 2019-12-05 10:13:05 +0100 | [diff] [blame] | 39 | #plugin 'AutoReload'; |
| 40 | plugin Localize => { |
| 41 | dict => { |
| 42 | _ => sub { $_->locale }, |
| 43 | }, |
Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 44 | resources => ['../derekovecs-server.dict'] |
Marc Kupietz | 9510451 | 2019-12-05 10:13:05 +0100 | [diff] [blame] | 45 | }; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 46 | our $opt_i = 0; # latin1-input? |
| 47 | our $opt_l = undef; |
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 | da7b349 | 2023-02-27 20:10:59 +0100 | [diff] [blame] | 53 | our $opt_G; |
Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 54 | |
| 55 | our $mergedEnd=0; |
| 56 | our %cache; |
| 57 | our %cccache; # classic collocator cache |
| 58 | our %spcache; # similar profile cache |
| 59 | our $opt_p = 5676; |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame] | 60 | our $opt_C; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 61 | |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 62 | my %marked; |
Marc Kupietz | c053d97 | 2019-01-10 10:41:51 +0100 | [diff] [blame] | 63 | my $title=""; |
Marc Kupietz | 793413b | 2016-04-02 21:48:57 +0200 | [diff] [blame] | 64 | my $training_args=""; |
| 65 | |
Marc Kupietz | c82b15f | 2022-07-19 17:36:27 +0200 | [diff] [blame] | 66 | getopts('d:D:Gil:p:m:n:M:C') or usage(); |
| 67 | |
| 68 | sub usage() { |
| 69 | print STDERR <<EOF; |
| 70 | non-server mode usage: MOJO_CONFIG=`pwd`/example.conf $0 [-h] [-d <file>] |
| 71 | -h : this (help) message |
| 72 | -d file : dump binary vecs as ascii text to <file> |
| 73 | |
| 74 | server-mode invocation: |
| 75 | |
| 76 | MOJO_CONFIG=`pwd`/example.conf morbo $0 |
| 77 | EOF |
| 78 | exit; |
| 79 | } |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 80 | |
| 81 | if($opt_M) { |
Marc Kupietz | ed93021 | 2016-04-27 15:42:38 +0200 | [diff] [blame] | 82 | open my $handle, '<:encoding(UTF-8)', $opt_M |
| 83 | or die "Can't open '$opt_M' for reading: $!"; |
| 84 | while(<$handle>) { |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 85 | foreach my $mw (split /\s+/) { |
| 86 | $marked{$mw}=1 |
| 87 | } |
| 88 | } |
Marc Kupietz | ed93021 | 2016-04-27 15:42:38 +0200 | [diff] [blame] | 89 | close($handle); |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 90 | } |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 91 | |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame] | 92 | my $vecs_name = (@ARGV > 0 && -r $ARGV[0] ? $ARGV[0] : $DEFAULT_VECS); |
| 93 | init_net($vecs_name, $opt_n, ($opt_i? 1 : 0), 1); |
| 94 | if(open(FILE, "$vecs_name.args")) { |
| 95 | $training_args = <FILE>; |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 96 | } |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame] | 97 | close(FILE); |
| 98 | $title = fname2corpusname($vecs_name); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 99 | |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame] | 100 | my $have_sprofiles = load_sprofiles($vecs_name); |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 101 | |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 102 | if (app->config->{w2v}->{merge}) { |
| 103 | $opt_m = app->config->{w2v}->{merge}; |
| 104 | } |
| 105 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 106 | if($opt_m) { |
| 107 | $mergedEnd = mergeVectors($opt_m); |
Marc Kupietz | c053d97 | 2019-01-10 10:41:51 +0100 | [diff] [blame] | 108 | $title = "<span class=\"merged\">" . $title . "</span> vs. " . fname2corpusname($opt_m); |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 109 | } |
| 110 | |
Marc Kupietz | e5568a0 | 2018-12-20 11:42:02 +0100 | [diff] [blame] | 111 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 112 | if($opt_d) { # -d: dump vecs and exit |
| 113 | dump_vecs($opt_d); |
| 114 | exit; |
| 115 | } |
| 116 | |
Marc Kupietz | fa19426 | 2018-06-05 09:39:32 +0200 | [diff] [blame] | 117 | if($opt_D) { # -D: dump vecs for numpy and exit |
| 118 | dump_for_numpy($opt_D); |
| 119 | exit; |
| 120 | } |
| 121 | |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame] | 122 | #my $daemon = Mojo::Server::Daemon->new( |
| 123 | # app => app, |
| 124 | # listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"] |
| 125 | #); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 126 | |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 127 | if($opt_G) { |
| 128 | print "Filtering garbage\n"; |
Marc Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 129 | filter_garbage(); |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 130 | print "Finished filtering garbage\n"; |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Marc Kupietz | 554aff5 | 2017-11-09 14:42:09 +0100 | [diff] [blame] | 133 | get '*/js/*' => sub { |
Marc Kupietz | ffef930 | 2017-11-07 15:58:01 +0100 | [diff] [blame] | 134 | my $c = shift; |
| 135 | my $url = $c->req->url; |
Marc K | 20476c7 | 2021-03-11 12:18:01 +0100 | [diff] [blame] | 136 | $url =~ s@/derekovecs/@/@g; |
Marc Kupietz | ffef930 | 2017-11-07 15:58:01 +0100 | [diff] [blame] | 137 | $c->app->log->info("GET: " . $url); |
Marc Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 138 | $c->reply->static($url); |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 139 | } => 'js'; |
Marc Kupietz | ffef930 | 2017-11-07 15:58:01 +0100 | [diff] [blame] | 140 | |
Marc Kupietz | a927057 | 2018-03-17 15:17:07 +0100 | [diff] [blame] | 141 | get '*/css/*' => sub { |
| 142 | my $c = shift; |
| 143 | my $url = $c->req->url; |
| 144 | $url =~ s@/derekovecs/@/@g; |
| 145 | $c->app->log->info("GET: " . $url); |
| 146 | $c->reply->static($url); |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 147 | } => 'css'; |
Marc Kupietz | a927057 | 2018-03-17 15:17:07 +0100 | [diff] [blame] | 148 | |
Marc Kupietz | c053d97 | 2019-01-10 10:41:51 +0100 | [diff] [blame] | 149 | sub fname2corpusname { |
| 150 | ($_) = @_; |
| 151 | s@.*/@@; |
Marc Kupietz | 86b5029 | 2019-02-17 21:03:59 +0100 | [diff] [blame] | 152 | s@\.en@-en@; |
Marc Kupietz | c053d97 | 2019-01-10 10:41:51 +0100 | [diff] [blame] | 153 | s@\..*@@; |
| 154 | return $_; |
| 155 | } |
| 156 | |
Marc Kupietz | cb43e49 | 2019-12-03 10:07:53 +0100 | [diff] [blame] | 157 | sub getWord { |
| 158 | ($_) = @_; |
| 159 | if ($_ =~ /^\d+/) { |
| 160 | return $_; |
| 161 | } else { |
| 162 | return getWordNumber($_); |
| 163 | } |
| 164 | } |
| 165 | |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 166 | |
Marc Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 167 | post '/derekovecs/getVecsByRanks' => sub { |
Marc Kupietz | 66bfd95 | 2017-12-11 09:59:45 +0100 | [diff] [blame] | 168 | my $self = shift; |
| 169 | my $vec = getVecs($self->req->json); |
| 170 | $self->render(json => $vec); |
| 171 | }; |
Marc Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 172 | |
Marc Kupietz | f608001 | 2021-03-12 09:14:42 +0100 | [diff] [blame] | 173 | any '*/getCollocationAssociation' => sub { |
| 174 | my $self = shift; |
| 175 | $self->render(data => getCollocationAssociation($self, getWord($self->param("w") ? $self->param("w") : $self->req->json), getWord($self->param("c"))), format=>'json'); |
| 176 | } => 'getCollocationAssociation'; |
| 177 | |
| 178 | any '/getCollocationAssociation' => sub { |
| 179 | my $self = shift; |
| 180 | $self->render(data => getCollocationAssociation($self, getWord($self->param("w") ? $self->param("w") : $self->req->json), getWord($self->param("c"))), format=>'json'); |
| 181 | } => 'getCollocationAssociation1'; |
| 182 | |
Marc Kupietz | e13a355 | 2018-01-25 08:48:34 +0100 | [diff] [blame] | 183 | any '*/getClassicCollocators' => sub { |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 184 | my $self = shift; |
Marc Kupietz | cb43e49 | 2019-12-03 10:07:53 +0100 | [diff] [blame] | 185 | $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] | 186 | } => 'getClassicCollocators1'; |
Marc Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 187 | |
Marc Kupietz | e13a355 | 2018-01-25 08:48:34 +0100 | [diff] [blame] | 188 | any '/getClassicCollocators' => sub { |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 189 | my $self = shift; |
Marc Kupietz | cb43e49 | 2019-12-03 10:07:53 +0100 | [diff] [blame] | 190 | $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] | 191 | } => 'getClassicCollocators'; |
Marc Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 192 | |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 193 | any '/getBiggestVocabDistances' => sub { |
| 194 | my $self = shift; |
| 195 | $self->render(data => getBiggestMergedDifferences(), format=>'json'); |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 196 | } => 'getBiggestVocabDistances1'; |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 197 | |
| 198 | any '*/getBiggestVocabDistances' => sub { |
| 199 | my $self = shift; |
| 200 | $self->render(data => getBiggestMergedDifferences(), format=>'json'); |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 201 | } => 'getBiggestVocabDistances'; |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 202 | |
Marc Kupietz | 33c79d3 | 2019-08-02 15:11:23 +0200 | [diff] [blame] | 203 | any '*/getPosWiseW2VCollocators' => sub { |
| 204 | my $self = shift; |
| 205 | $self->render(data => getPosWiseW2VCollocatorsAsTsv($self->param("w"), |
| 206 | ($self->param("max")? $self->param("max") : 200), |
| 207 | ($self->param("cutoff")? $self->param("cutoff") :750000), |
| 208 | ($self->param("threshold")? $self->param("threshold") : 0.2)), |
| 209 | format=>'tsv'); |
| 210 | }; |
| 211 | |
| 212 | any '/getPosWiseW2VCollocators' => sub { |
| 213 | my $self = shift; |
| 214 | $self->render(data => getPosWiseW2VCollocatorsAsTsv($self->param("w"), |
| 215 | ($self->param("max")? $self->param("max") : 200), |
| 216 | ($self->param("cutoff")? $self->param("cutoff") : 750000), |
| 217 | ($self->param("threshold")? $self->param("threshold") : 0.2)), |
| 218 | format=>'tsv'); |
| 219 | }; |
| 220 | |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 221 | any '*/getSimilarProfiles' => sub { |
| 222 | my $self = shift; |
Marc Kupietz | cb43e49 | 2019-12-03 10:07:53 +0100 | [diff] [blame] | 223 | $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] | 224 | }; |
| 225 | |
Marc Kupietz | c987fa8 | 2018-03-21 12:14:25 +0100 | [diff] [blame] | 226 | any '/getSimilarProfiles' => sub { |
| 227 | my $self = shift; |
Marc Kupietz | cb43e49 | 2019-12-03 10:07:53 +0100 | [diff] [blame] | 228 | $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] | 229 | }; |
| 230 | |
Marc Kupietz | 9f30157 | 2020-04-06 18:29:16 +0200 | [diff] [blame] | 231 | any '*/getWord' => sub { |
| 232 | my $self = shift; |
| 233 | my $w = $self->param("w"); |
| 234 | my $rank = getWord($w); |
| 235 | my $status = 200; |
| 236 | if ($rank <= 0) { |
| 237 | $rank = -1; |
| 238 | $status = 404; |
| 239 | } |
| 240 | $self->render(data => encode_json({word => $w, frequencyRank => $rank}), format => 'json', status => $status); |
| 241 | }; |
| 242 | |
| 243 | any '/getWord' => sub { |
| 244 | my $self = shift; |
| 245 | my $w = $self->param("w"); |
| 246 | my $rank = getWord($w); |
| 247 | my $status = 200; |
| 248 | if ($rank <= 0) { |
| 249 | $rank = -1; |
| 250 | $status = 404; |
| 251 | } |
| 252 | $self->render(data => encode_json({word => $w, frequencyRank => $rank}), format => 'json', status => $status); |
| 253 | }; |
| 254 | |
Marc Kupietz | 98ed1c0 | 2019-08-02 15:05:37 +0200 | [diff] [blame] | 255 | any '/getSimilarity' => sub { |
| 256 | my $self = shift; |
| 257 | my $w1 = $self->param("w1"); |
| 258 | my $w2 = $self->param("w2"); |
| 259 | $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json'); |
| 260 | }; |
| 261 | |
| 262 | any '*/getSimilarity' => sub { |
| 263 | my $self = shift; |
| 264 | my $w1 = $self->param("w1"); |
| 265 | my $w2 = $self->param("w2"); |
| 266 | $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json'); |
| 267 | }; |
| 268 | |
Marc Kupietz | 3576c62 | 2023-11-05 08:51:58 +0100 | [diff] [blame] | 269 | any '*/getDowntimeCalendar' => sub { |
| 270 | my $self = shift; |
| 271 | my $calendar = getDowntimeCalendar($DOWNTIME_CALENDAR_URL); |
| 272 | $self->render(data => $calendar, format=>'text'); |
| 273 | }; |
| 274 | |
| 275 | any '/getDowntimeCalendar' => sub { |
| 276 | my $self = shift; |
| 277 | my $calendar = getDowntimeCalendar($DOWNTIME_CALENDAR_URL); |
| 278 | $self->render(data => $calendar, format=>'text'); |
| 279 | }; |
| 280 | |
Marc Kupietz | b36bc74 | 2023-11-05 17:46:11 +0100 | [diff] [blame] | 281 | any '/getVocabSize' => sub { |
| 282 | my $self = shift; |
| 283 | $self->render(data => getVocabSize(), format=>'json'); |
| 284 | }; |
| 285 | |
| 286 | any '*/getVocabSize' => sub { |
| 287 | my $self = shift; |
| 288 | $self->render(data => getVocabSize(), format=>'json'); |
| 289 | }; |
| 290 | |
Marc Kupietz | af708c2 | 2023-11-05 11:20:20 +0100 | [diff] [blame] | 291 | any '/getVersion' => sub { |
| 292 | my $self = shift; |
Marc Kupietz | b36bc74 | 2023-11-05 17:46:11 +0100 | [diff] [blame] | 293 | $self->render(data => to_json($VERSION), format=>'json'); |
Marc Kupietz | af708c2 | 2023-11-05 11:20:20 +0100 | [diff] [blame] | 294 | }; |
| 295 | |
| 296 | any '*/getVersion' => sub { |
| 297 | my $self = shift; |
Marc Kupietz | b36bc74 | 2023-11-05 17:46:11 +0100 | [diff] [blame] | 298 | $self->render(data => to_json($VERSION), format=>'json'); |
Marc Kupietz | af708c2 | 2023-11-05 11:20:20 +0100 | [diff] [blame] | 299 | }; |
| 300 | |
Marc Kupietz | dea505e | 2023-11-05 11:42:36 +0100 | [diff] [blame] | 301 | any '/getModelName' => sub { |
| 302 | my $self = shift; |
Marc Kupietz | b36bc74 | 2023-11-05 17:46:11 +0100 | [diff] [blame] | 303 | $self->render(data => to_json($title), format=>'json'); |
Marc Kupietz | dea505e | 2023-11-05 11:42:36 +0100 | [diff] [blame] | 304 | }; |
| 305 | |
| 306 | any '*/getModelName' => sub { |
| 307 | my $self = shift; |
Marc Kupietz | b36bc74 | 2023-11-05 17:46:11 +0100 | [diff] [blame] | 308 | $self->render(data => to_json($title), format=>'json'); |
Marc Kupietz | dea505e | 2023-11-05 11:42:36 +0100 | [diff] [blame] | 309 | }; |
| 310 | |
Marc Kupietz | df3d4b5 | 2017-11-29 16:57:27 +0100 | [diff] [blame] | 311 | get '*/img/*' => sub { |
| 312 | my $c = shift; |
| 313 | my $url = $c->req->url; |
| 314 | $url =~ s@/derekovecs@@g; |
| 315 | $c->app->log->info("GET: " . $url); |
| 316 | $c->reply->static($url); |
| 317 | }; |
| 318 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 319 | get '/' => sub { |
| 320 | my $c = shift; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 321 | $c->app->log->info("get: ".$c->req->url->to_abs); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 322 | my $word=$c->param('word'); |
Marc Kupietz | 2da2a81 | 2019-02-21 14:17:35 +0100 | [diff] [blame] | 323 | my $no_nbs=$c->param('n') || ($opt_m? 50 : 100); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 324 | my $no_iterations=$c->param('N') || 2000; |
Marc Kupietz | d422739 | 2016-03-01 16:45:12 +0100 | [diff] [blame] | 325 | my $perplexity=$c->param('perplexity') || 20; |
Marc Kupietz | c4d62f8 | 2016-03-01 11:04:24 +0100 | [diff] [blame] | 326 | my $epsilon=$c->param('epsilon') || 5; |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 327 | my $som=$c->param('som') || 0; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 328 | my $searchBaseVocabFirst=$c->param('sbf') || 0; |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 329 | my $sort=$c->param('sort') || 0; |
Marc Kupietz | c469f3b | 2017-11-13 14:07:36 +0100 | [diff] [blame] | 330 | my $csv=$c->param('csv') || 0; |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 331 | my $json=$c->param('json') || 0; |
Marc Kupietz | db2dc7e | 2017-12-02 12:04:03 +0100 | [diff] [blame] | 332 | my $cutoff=$c->param('cutoff') || 500000; |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 333 | my $dedupe=$c->param('dedupe') || 0; |
Marc Kupietz | ac707b3 | 2018-12-20 11:36:38 +0100 | [diff] [blame] | 334 | my $nosp=$c->param('nosp') || 0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 335 | my $res; |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 336 | my @lists; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 337 | my @collocations; |
Marc Kupietz | cddc848 | 2019-12-04 08:57:33 +0100 | [diff] [blame] | 338 | if(defined($word) && $word !~ /^\s*$/) { |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 339 | $c->inactivity_timeout(300); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 340 | $word =~ s/\s+/ /g; |
Marc Kupietz | 3082fd0 | 2019-01-09 14:54:06 +0100 | [diff] [blame] | 341 | if($opt_m && $word !~ /\|/) { |
| 342 | $word .= "|$word"; |
| 343 | } |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 344 | for my $w (split(' *\| *', $word)) { |
Marc Kupietz | 3082fd0 | 2019-01-09 14:54:06 +0100 | [diff] [blame] | 345 | if($opt_m) { |
| 346 | if($searchBaseVocabFirst) { |
| 347 | $searchBaseVocabFirst=0; |
| 348 | } else { |
| 349 | $searchBaseVocabFirst=1; |
| 350 | } |
| 351 | } |
| 352 | if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe,$searchBaseVocabFirst}) { |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 353 | $c->app->log->info("Getting $w results from cache"); |
Marc Kupietz | 3082fd0 | 2019-01-09 14:54:06 +0100 | [diff] [blame] | 354 | $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe.$searchBaseVocabFirst} |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 355 | } else { |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 356 | $c->app->log->info('Looking for neighbours of '.$w); |
| 357 | if($opt_i) { |
Marc Kupietz | ac707b3 | 2018-12-20 11:36:38 +0100 | [diff] [blame] | 358 | $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] | 359 | } else { |
Marc Kupietz | ac707b3 | 2018-12-20 11:36:38 +0100 | [diff] [blame] | 360 | $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp); |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 361 | } |
Marc Kupietz | 2dd2dd7 | 2017-12-01 22:08:14 +0100 | [diff] [blame] | 362 | $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 363 | } |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 364 | push(@lists, $res->{paradigmatic}); |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 365 | } |
| 366 | } |
Marc Kupietz | 56844a2 | 2019-08-02 15:12:19 +0200 | [diff] [blame] | 367 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 368 | $word =~ s/ *\| */ | /g; |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 369 | if($json) { |
| 370 | return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}}); |
Marc Kupietz | c469f3b | 2017-11-13 14:07:36 +0100 | [diff] [blame] | 371 | } elsif($csv) { |
| 372 | my $csv_data=""; |
| 373 | for (my $i=0; $i <= $no_nbs; $i++) { |
| 374 | $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", "; |
| 375 | } |
| 376 | for (my $i=0; $i < $no_nbs; $i++) { |
| 377 | $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", "; |
| 378 | } |
| 379 | chop $csv_data; |
| 380 | chop $csv_data; |
Marc Kupietz | 56dbabe | 2019-12-10 14:33:57 +0100 | [diff] [blame] | 381 | $csv_data .= "\n"; |
Marc Kupietz | c469f3b | 2017-11-13 14:07:36 +0100 | [diff] [blame] | 382 | return $c->render(text=>$csv_data); |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 383 | } else { |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 384 | my $distantWords=""; |
| 385 | if(!defined($word) || $word !~ /^\s*$/) { |
| 386 | $distantWords = getBiggestMergedDifferences(); |
| 387 | } |
Marc Kupietz | 9ae184c | 2023-11-04 18:21:54 +0100 | [diff] [blame] | 388 | $c->render( |
| 389 | template => "index", |
| 390 | title => $title, |
| 391 | word => $word, |
| 392 | distantWords => $distantWords, |
| 393 | cutoff => $cutoff, |
| 394 | no_nbs => $no_nbs, |
| 395 | no_iterations => $no_iterations, |
| 396 | epsilon => $epsilon, |
| 397 | perplexity => $perplexity, |
| 398 | show_som => $som, |
| 399 | searchBaseVocabFirst => $searchBaseVocabFirst, |
| 400 | sort => $sort, |
| 401 | training_args => $training_args, |
| 402 | mergedEnd => $mergedEnd, |
| 403 | haveSProfiles => $have_sprofiles, |
| 404 | dedupe => $dedupe, |
| 405 | marked => \%marked, |
| 406 | lists => \@lists, |
Marc Kupietz | eacc63f | 2023-11-06 15:39:13 +0100 | [diff] [blame^] | 407 | collocators => $res->{syntagmatic}, |
| 408 | version => $VERSION, |
Marc Kupietz | 9ae184c | 2023-11-04 18:21:54 +0100 | [diff] [blame] | 409 | ); |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 410 | } |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 411 | } => "paradigmaticAndSyntagmaticNbs"; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 412 | |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 413 | helper(bitvec2window => sub { |
| 414 | my ($self, $n) = @_; |
| 415 | my $str = unpack("B32", pack("N", $n)); |
| 416 | $str =~ s/^\d{22}//; |
| 417 | $str =~ s/^(\d{5})/$1x/; |
| 418 | $str =~ s/0/ยท/g; |
| 419 | $str =~ s/1/+/g; |
| 420 | return $str; |
| 421 | }); |
| 422 | |
Marc Kupietz | 6017daf | 2022-05-19 09:22:49 +0200 | [diff] [blame] | 423 | if(app->config->{Piwik} && app->config->{Piwik}->{url}) { |
Marc Kupietz | 3e3e326 | 2022-04-12 23:11:45 +0200 | [diff] [blame] | 424 | hook( |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 425 | after_render => sub { |
| 426 | my $c = shift; |
| 427 | |
| 428 | # Only track valid routes |
| 429 | my $route = $c->current_route or return; |
| 430 | |
| 431 | # This won't forward personalized information |
| 432 | my $hash = { |
Marc Kupietz | 251de9f | 2020-01-14 16:12:05 +0100 | [diff] [blame] | 433 | action_url => $c->req->url->to_abs, |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 434 | action_name => $route, |
Marc Kupietz | 251de9f | 2020-01-14 16:12:05 +0100 | [diff] [blame] | 435 | ua => $c->req->headers->user_agent, |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 436 | urlref => '', |
| 437 | send_image => 0, |
| 438 | dnt => 0, |
Marc Kupietz | 251de9f | 2020-01-14 16:12:05 +0100 | [diff] [blame] | 439 | cip => $c->remote_addr, |
| 440 | lang => $c->req->headers->accept_language, |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 441 | uid => $c->random_string('piwik_rand_id') |
| 442 | }; |
Marc Kupietz | 251de9f | 2020-01-14 16:12:05 +0100 | [diff] [blame] | 443 | # $c->app->log->info("PIWIK: counting " . $hash->{action_url} . "\nremote:" . $c->remote_addr); |
| 444 | # $c->app->log->info("PIWIK: tag " . $c->piwik_tag); |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 445 | |
| 446 | # Send track |
| 447 | $c->piwik->api_p(Track => $hash)->wait; |
Marc Kupietz | 251de9f | 2020-01-14 16:12:05 +0100 | [diff] [blame] | 448 | |
| 449 | # $c->app->log->info("PIWIK: counted."); |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 450 | } |
Marc Kupietz | 3e3e326 | 2022-04-12 23:11:45 +0200 | [diff] [blame] | 451 | ); |
| 452 | } |
Marc Kupietz | 1b856fa | 2019-12-07 23:01:43 +0100 | [diff] [blame] | 453 | |
Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 454 | app->renderer->paths([app->home->rel_file('../templates')]); |
Marc Kupietz | e8e3ded | 2020-07-13 17:53:56 +0200 | [diff] [blame] | 455 | app->start; |
| 456 | #$daemon->run; |
Marc Kupietz | 9510451 | 2019-12-05 10:13:05 +0100 | [diff] [blame] | 457 | # app->start; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 458 | |
Marc Kupietz | 9510451 | 2019-12-05 10:13:05 +0100 | [diff] [blame] | 459 | # exit; |