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