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