Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 1 | #!/usr/local/bin/perl |
| 2 | use Inline C; |
Marc Kupietz | 00eecb1 | 2019-01-24 16:08:44 +0100 | [diff] [blame] | 3 | use Inline C => Config => CLEAN_AFTER_BUILD => 0, BUILD_NOISY => 1, ccflags => $Config{ccflags}." -I/vol/work/kupietz/Work2/kl/trunk/CollocatorDB -I, -L. -Wall -O4", libs => "-shared -lpthread -lcollocatordb -lrt -lsnappy -lz -lbz2 -llz4 -lzstd -lrocksdb -lgomp"; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 4 | #use Inline C => Config => BUILD_NOISY => 1, CFLAGS => $Config{cflags}." -O4 -mtune k9"; |
| 5 | #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] | 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 | 7bc85fd | 2016-02-24 11:42:41 +0100 | [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 | ffef930 | 2017-11-07 15:58:01 +0100 | [diff] [blame] | 15 | app->static->paths->[0] = getcwd; |
| 16 | |
Marc Kupietz | d422739 | 2016-03-01 16:45:12 +0100 | [diff] [blame] | 17 | plugin 'Log::Access'; |
Marc Kupietz | b3422c1 | 2017-07-04 14:12:11 +0200 | [diff] [blame] | 18 | plugin "RequestBase"; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 19 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 20 | our $opt_i = 0; # latin1-input? |
| 21 | our $opt_l = undef; |
| 22 | our $opt_p = 5676; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 23 | our $opt_m; |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 24 | our $opt_M; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 25 | our $opt_n = ''; |
| 26 | our $opt_d; |
Marc Kupietz | fa19426 | 2018-06-05 09:39:32 +0200 | [diff] [blame] | 27 | our $opt_D; |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 28 | our $opt_G; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 29 | |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 30 | my %marked; |
Marc Kupietz | c053d97 | 2019-01-10 10:41:51 +0100 | [diff] [blame] | 31 | my $title=""; |
Marc Kupietz | 793413b | 2016-04-02 21:48:57 +0200 | [diff] [blame] | 32 | my $training_args=""; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 33 | my $mergedEnd=0; |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 34 | my %cache; |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 35 | my %cccache; # classic collocator cache |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 36 | my %spcache; # similar profile cache |
Marc Kupietz | 793413b | 2016-04-02 21:48:57 +0200 | [diff] [blame] | 37 | |
Marc Kupietz | fa19426 | 2018-06-05 09:39:32 +0200 | [diff] [blame] | 38 | getopts('d:D:Gil:p:m:n:M:'); |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 39 | |
| 40 | if($opt_M) { |
Marc Kupietz | ed93021 | 2016-04-27 15:42:38 +0200 | [diff] [blame] | 41 | open my $handle, '<:encoding(UTF-8)', $opt_M |
| 42 | or die "Can't open '$opt_M' for reading: $!"; |
| 43 | while(<$handle>) { |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 44 | foreach my $mw (split /\s+/) { |
| 45 | $marked{$mw}=1 |
| 46 | } |
| 47 | } |
Marc Kupietz | ed93021 | 2016-04-27 15:42:38 +0200 | [diff] [blame] | 48 | close($handle); |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 49 | } |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 50 | |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 51 | # -cbow 1 -size 200 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 40 -binary 1 -iter 15 |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 52 | if(!$ARGV[0]) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 53 | init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0)); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 54 | } else { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 55 | init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0)); |
Marc Kupietz | 793413b | 2016-04-02 21:48:57 +0200 | [diff] [blame] | 56 | if(open(FILE, "$ARGV[0].args")) { |
| 57 | $training_args = <FILE>; |
| 58 | } |
| 59 | close(FILE); |
Marc Kupietz | c053d97 | 2019-01-10 10:41:51 +0100 | [diff] [blame] | 60 | $title = fname2corpusname($ARGV[0]); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 61 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 62 | |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 63 | my $have_sprofiles = load_sprofiles($ARGV[0]); |
| 64 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 65 | if($opt_m) { |
| 66 | $mergedEnd = mergeVectors($opt_m); |
Marc Kupietz | c053d97 | 2019-01-10 10:41:51 +0100 | [diff] [blame] | 67 | $title = "<span class=\"merged\">" . $title . "</span> vs. " . fname2corpusname($opt_m); |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Marc Kupietz | e5568a0 | 2018-12-20 11:42:02 +0100 | [diff] [blame] | 70 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 71 | if($opt_d) { # -d: dump vecs and exit |
| 72 | dump_vecs($opt_d); |
| 73 | exit; |
| 74 | } |
| 75 | |
Marc Kupietz | fa19426 | 2018-06-05 09:39:32 +0200 | [diff] [blame] | 76 | if($opt_D) { # -D: dump vecs for numpy and exit |
| 77 | dump_for_numpy($opt_D); |
| 78 | exit; |
| 79 | } |
| 80 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 81 | my $daemon = Mojo::Server::Daemon->new( |
| 82 | app => app, |
| 83 | listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"] |
| 84 | ); |
| 85 | |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 86 | if($opt_G) { |
| 87 | print "Filtering garbage\n"; |
| 88 | filter_garbage(); |
| 89 | } |
| 90 | |
Marc Kupietz | 554aff5 | 2017-11-09 14:42:09 +0100 | [diff] [blame] | 91 | get '*/js/*' => sub { |
Marc Kupietz | ffef930 | 2017-11-07 15:58:01 +0100 | [diff] [blame] | 92 | my $c = shift; |
| 93 | my $url = $c->req->url; |
| 94 | $url =~ s@/derekovecs@@g; |
| 95 | $c->app->log->info("GET: " . $url); |
| 96 | $c->reply->static($url); |
| 97 | }; |
| 98 | |
Marc Kupietz | a927057 | 2018-03-17 15:17:07 +0100 | [diff] [blame] | 99 | get '*/css/*' => sub { |
| 100 | my $c = shift; |
| 101 | my $url = $c->req->url; |
| 102 | $url =~ s@/derekovecs/@/@g; |
| 103 | $c->app->log->info("GET: " . $url); |
| 104 | $c->reply->static($url); |
| 105 | }; |
| 106 | |
Marc Kupietz | c053d97 | 2019-01-10 10:41:51 +0100 | [diff] [blame] | 107 | sub fname2corpusname { |
| 108 | ($_) = @_; |
| 109 | s@.*/@@; |
Marc Kupietz | 86b5029 | 2019-02-17 21:03:59 +0100 | [diff] [blame] | 110 | s@\.en@-en@; |
Marc Kupietz | c053d97 | 2019-01-10 10:41:51 +0100 | [diff] [blame] | 111 | s@\..*@@; |
| 112 | return $_; |
| 113 | } |
| 114 | |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 115 | sub getClassicCollocatorsCached { |
| 116 | my ($c, $word) = @_; |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 117 | my $s2 = ""; |
Marc Kupietz | 9ff3c99 | 2019-02-04 12:32:54 +0100 | [diff] [blame] | 118 | if($word > $mergedEnd) { |
| 119 | $word-=$mergedEnd; |
| 120 | } |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 121 | |
Marc Kupietz | 999ab8c | 2019-02-17 21:42:21 +0100 | [diff] [blame] | 122 | if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference |
Marc Kupietz | 9ee3f41 | 2019-08-02 14:58:19 +0200 | [diff] [blame] | 123 | open PIPE, "GET http://compute:5673/getClassicCollocators?w=$word |" or |
| 124 | open PIPE, "GET http://klinux10:5673/getClassicCollocators?w=$word |"; |
Marc Kupietz | 999ab8c | 2019-02-17 21:42:21 +0100 | [diff] [blame] | 125 | } |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 126 | if(!$cccache{$word}) { |
Marc Kupietz | 06d6129 | 2019-02-04 12:33:22 +0100 | [diff] [blame] | 127 | $c->app->log->info("Getting classic collocates of $word."); |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 128 | $cccache{$word} = getClassicCollocators($word); |
Marc Kupietz | 1d96a08 | 2019-02-18 09:29:06 +0100 | [diff] [blame] | 129 | $cccache{$word} =~ s/:(-?)(nan|inf)/:"${1}${2}"/g; |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 130 | } else { |
Marc Kupietz | 06d6129 | 2019-02-04 12:33:22 +0100 | [diff] [blame] | 131 | $c->app->log->info("Getting classic collocates for $word from cache."); |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 132 | } |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 133 | if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference |
Marc Kupietz | 999ab8c | 2019-02-17 21:42:21 +0100 | [diff] [blame] | 134 | while(<PIPE>) { |
| 135 | $s2 .= $_; |
| 136 | } |
| 137 | close(PIPE); |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 138 | } |
Marc Kupietz | 999ab8c | 2019-02-17 21:42:21 +0100 | [diff] [blame] | 139 | |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 140 | if(length($s2) > 2000) { |
| 141 | my $d1 = decode_json($cccache{$word}); |
| 142 | my $d2 = decode_json($s2); |
| 143 | my %d2ld; |
Marc Kupietz | 001bffd | 2019-02-21 08:52:41 +0100 | [diff] [blame] | 144 | my $minLd = 14; |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 145 | foreach my $i (@{$d2->{collocates}}) { |
| 146 | $d2ld{$i->{word}}=$i->{ld}; |
Marc Kupietz | 001bffd | 2019-02-21 08:52:41 +0100 | [diff] [blame] | 147 | $minLd=$i->{ld} if($i->{ld} < $minLd); |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 148 | } |
| 149 | foreach my $i (@{$d1->{collocates}}) { |
| 150 | my $w = $i->{word}; |
Marc Kupietz | 001bffd | 2019-02-21 08:52:41 +0100 | [diff] [blame] | 151 | $i->{delta} = $i->{ld} - (defined $d2ld{$w} ? $d2ld{$w} : $minLd-0.1); |
Marc Kupietz | 81aeed2 | 2019-02-17 21:22:45 +0100 | [diff] [blame] | 152 | } |
| 153 | return(encode_json($d1)); |
| 154 | } else { |
| 155 | my $d1 = decode_json($cccache{$word}); |
| 156 | foreach my $i (@{$d1->{collocates}}) { |
| 157 | $i->{delta} = 0; |
| 158 | } |
| 159 | return(encode_json($d1)); |
| 160 | } |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 163 | sub getSimilarProfilesCached { |
| 164 | my ($c, $word) = @_; |
| 165 | if(!$spcache{$word}) { |
| 166 | $spcache{$word} = getSimilarProfiles($word); |
| 167 | } else { |
| 168 | $c->app->log->info("Getting similar profiles for $word from cache:"); |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 169 | } |
| 170 | return $spcache{$word}; |
| 171 | } |
| 172 | |
Marc Kupietz | 66bfd95 | 2017-12-11 09:59:45 +0100 | [diff] [blame] | 173 | post '/derekovecs/getVecsByRanks' => sub { |
| 174 | my $self = shift; |
| 175 | my $vec = getVecs($self->req->json); |
| 176 | $self->render(json => $vec); |
| 177 | }; |
| 178 | |
Marc Kupietz | e13a355 | 2018-01-25 08:48:34 +0100 | [diff] [blame] | 179 | any '*/getClassicCollocators' => sub { |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 180 | my $self = shift; |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 181 | $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json'); |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 182 | }; |
| 183 | |
Marc Kupietz | e13a355 | 2018-01-25 08:48:34 +0100 | [diff] [blame] | 184 | any '/getClassicCollocators' => sub { |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 185 | my $self = shift; |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 186 | $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json'); |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 187 | }; |
| 188 | |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 189 | any '/getBiggestVocabDistances' => sub { |
| 190 | my $self = shift; |
| 191 | $self->render(data => getBiggestMergedDifferences(), format=>'json'); |
| 192 | }; |
| 193 | |
| 194 | any '*/getBiggestVocabDistances' => sub { |
| 195 | my $self = shift; |
| 196 | $self->render(data => getBiggestMergedDifferences(), format=>'json'); |
| 197 | }; |
| 198 | |
Marc Kupietz | 33c79d3 | 2019-08-02 15:11:23 +0200 | [diff] [blame] | 199 | any '*/getPosWiseW2VCollocators' => sub { |
| 200 | my $self = shift; |
| 201 | $self->render(data => getPosWiseW2VCollocatorsAsTsv($self->param("w"), |
| 202 | ($self->param("max")? $self->param("max") : 200), |
| 203 | ($self->param("cutoff")? $self->param("cutoff") :750000), |
| 204 | ($self->param("threshold")? $self->param("threshold") : 0.2)), |
| 205 | format=>'tsv'); |
| 206 | }; |
| 207 | |
| 208 | any '/getPosWiseW2VCollocators' => sub { |
| 209 | my $self = shift; |
| 210 | $self->render(data => getPosWiseW2VCollocatorsAsTsv($self->param("w"), |
| 211 | ($self->param("max")? $self->param("max") : 200), |
| 212 | ($self->param("cutoff")? $self->param("cutoff") : 750000), |
| 213 | ($self->param("threshold")? $self->param("threshold") : 0.2)), |
| 214 | format=>'tsv'); |
| 215 | }; |
| 216 | |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 217 | any '*/getSimilarProfiles' => sub { |
| 218 | my $self = shift; |
| 219 | $self->render(data => getSimilarProfilesCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json'); |
| 220 | }; |
| 221 | |
Marc Kupietz | c987fa8 | 2018-03-21 12:14:25 +0100 | [diff] [blame] | 222 | any '/getSimilarProfiles' => sub { |
| 223 | my $self = shift; |
| 224 | $self->render(data => getSimilarProfilesCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json'); |
| 225 | }; |
| 226 | |
Marc Kupietz | 98ed1c0 | 2019-08-02 15:05:37 +0200 | [diff] [blame] | 227 | any '/getSimilarity' => sub { |
| 228 | my $self = shift; |
| 229 | my $w1 = $self->param("w1"); |
| 230 | my $w2 = $self->param("w2"); |
| 231 | $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json'); |
| 232 | }; |
| 233 | |
| 234 | any '*/getSimilarity' => sub { |
| 235 | my $self = shift; |
| 236 | my $w1 = $self->param("w1"); |
| 237 | my $w2 = $self->param("w2"); |
| 238 | $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json'); |
| 239 | }; |
| 240 | |
Marc Kupietz | df3d4b5 | 2017-11-29 16:57:27 +0100 | [diff] [blame] | 241 | get '*/img/*' => sub { |
| 242 | my $c = shift; |
| 243 | my $url = $c->req->url; |
| 244 | $url =~ s@/derekovecs@@g; |
| 245 | $c->app->log->info("GET: " . $url); |
| 246 | $c->reply->static($url); |
| 247 | }; |
| 248 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 249 | get '/' => sub { |
| 250 | my $c = shift; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 251 | $c->app->log->info("get: ".$c->req->url->to_abs); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 252 | my $word=$c->param('word'); |
Marc Kupietz | 2da2a81 | 2019-02-21 14:17:35 +0100 | [diff] [blame] | 253 | my $no_nbs=$c->param('n') || ($opt_m? 50 : 100); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 254 | my $no_iterations=$c->param('N') || 2000; |
Marc Kupietz | d422739 | 2016-03-01 16:45:12 +0100 | [diff] [blame] | 255 | my $perplexity=$c->param('perplexity') || 20; |
Marc Kupietz | c4d62f8 | 2016-03-01 11:04:24 +0100 | [diff] [blame] | 256 | my $epsilon=$c->param('epsilon') || 5; |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 257 | my $som=$c->param('som') || 0; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 258 | my $searchBaseVocabFirst=$c->param('sbf') || 0; |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 259 | my $sort=$c->param('sort') || 0; |
Marc Kupietz | c469f3b | 2017-11-13 14:07:36 +0100 | [diff] [blame] | 260 | my $csv=$c->param('csv') || 0; |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 261 | my $json=$c->param('json') || 0; |
Marc Kupietz | db2dc7e | 2017-12-02 12:04:03 +0100 | [diff] [blame] | 262 | my $cutoff=$c->param('cutoff') || 500000; |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 263 | my $dedupe=$c->param('dedupe') || 0; |
Marc Kupietz | ac707b3 | 2018-12-20 11:36:38 +0100 | [diff] [blame] | 264 | my $nosp=$c->param('nosp') || 0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 265 | my $res; |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 266 | my @lists; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 267 | my @collocations; |
Marc Kupietz | d897d5d | 2019-08-02 15:09:07 +0200 | [diff] [blame] | 268 | $word="idea" if(!defined($word)); |
| 269 | if(defined($word) && $word !~ /^\s*$/) { |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 270 | $c->inactivity_timeout(300); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 271 | $word =~ s/\s+/ /g; |
Marc Kupietz | 3082fd0 | 2019-01-09 14:54:06 +0100 | [diff] [blame] | 272 | if($opt_m && $word !~ /\|/) { |
| 273 | $word .= "|$word"; |
| 274 | } |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 275 | for my $w (split(' *\| *', $word)) { |
Marc Kupietz | 3082fd0 | 2019-01-09 14:54:06 +0100 | [diff] [blame] | 276 | if($opt_m) { |
| 277 | if($searchBaseVocabFirst) { |
| 278 | $searchBaseVocabFirst=0; |
| 279 | } else { |
| 280 | $searchBaseVocabFirst=1; |
| 281 | } |
| 282 | } |
| 283 | if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe,$searchBaseVocabFirst}) { |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 284 | $c->app->log->info("Getting $w results from cache"); |
Marc Kupietz | 3082fd0 | 2019-01-09 14:54:06 +0100 | [diff] [blame] | 285 | $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe.$searchBaseVocabFirst} |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 286 | } else { |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 287 | $c->app->log->info('Looking for neighbours of '.$w); |
| 288 | if($opt_i) { |
Marc Kupietz | ac707b3 | 2018-12-20 11:36:38 +0100 | [diff] [blame] | 289 | $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] | 290 | } else { |
Marc Kupietz | ac707b3 | 2018-12-20 11:36:38 +0100 | [diff] [blame] | 291 | $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp); |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 292 | } |
Marc Kupietz | 2dd2dd7 | 2017-12-01 22:08:14 +0100 | [diff] [blame] | 293 | $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 294 | } |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 295 | push(@lists, $res->{paradigmatic}); |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 296 | } |
| 297 | } |
Marc Kupietz | 56844a2 | 2019-08-02 15:12:19 +0200 | [diff] [blame] | 298 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 299 | $word =~ s/ *\| */ | /g; |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 300 | if($json) { |
| 301 | return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}}); |
Marc Kupietz | c469f3b | 2017-11-13 14:07:36 +0100 | [diff] [blame] | 302 | } elsif($csv) { |
| 303 | my $csv_data=""; |
| 304 | for (my $i=0; $i <= $no_nbs; $i++) { |
| 305 | $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", "; |
| 306 | } |
| 307 | for (my $i=0; $i < $no_nbs; $i++) { |
| 308 | $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", "; |
| 309 | } |
| 310 | chop $csv_data; |
| 311 | chop $csv_data; |
| 312 | $csv_data .= "\n"; |
| 313 | return $c->render(text=>$csv_data); |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 314 | } else { |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 315 | my $distantWords=""; |
| 316 | if(!defined($word) || $word !~ /^\s*$/) { |
| 317 | $distantWords = getBiggestMergedDifferences(); |
| 318 | } |
| 319 | $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] | 320 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 321 | }; |
| 322 | |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 323 | helper(bitvec2window => sub { |
| 324 | my ($self, $n) = @_; |
| 325 | my $str = unpack("B32", pack("N", $n)); |
| 326 | $str =~ s/^\d{22}//; |
| 327 | $str =~ s/^(\d{5})/$1x/; |
| 328 | $str =~ s/0/·/g; |
| 329 | $str =~ s/1/+/g; |
| 330 | return $str; |
| 331 | }); |
| 332 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 333 | $daemon->run; # app->start; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 334 | |
| 335 | exit; |
| 336 | |
| 337 | __END__ |
| 338 | |
| 339 | __C__ |
| 340 | #include <stdio.h> |
| 341 | #include <string.h> |
| 342 | #include <math.h> |
| 343 | #include <malloc.h> |
| 344 | #include <stdlib.h> //strlen |
Marc Kupietz | f080976 | 2016-02-26 10:13:47 +0100 | [diff] [blame] | 345 | #include <sys/mman.h> |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 346 | #include <pthread.h> |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 347 | #include <collocatordb.h> |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 348 | |
| 349 | #define max_size 2000 |
| 350 | #define max_w 50 |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 351 | #define MAX_NEIGHBOURS 1000 |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 352 | #define MAX_WORDS -1 |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 353 | #define MAX_THREADS 100 |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 354 | #define MAX_CC 50 |
| 355 | #define EXP_TABLE_SIZE 1000 |
| 356 | #define MAX_EXP 6 |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 357 | #define MIN_RESP 0.50 |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 358 | |
| 359 | //the thread function |
| 360 | void *connection_handler(void *); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 361 | |
| 362 | typedef struct { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 363 | long long wordi; |
| 364 | long position; |
| 365 | float activation; |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 366 | float average; |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 367 | float cprobability; // column wise probability |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 368 | float cprobability_sum; |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 369 | float probability; |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 370 | float activation_sum; |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 371 | float max_activation; |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 372 | float heat[16]; |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 373 | } collocator; |
| 374 | |
| 375 | typedef struct { |
| 376 | collocator *best; |
Marc Kupietz | 80abb44 | 2016-03-23 21:04:08 +0100 | [diff] [blame] | 377 | int length; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 378 | } knn; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 379 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 380 | typedef struct { |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 381 | long long wordi[MAX_NEIGHBOURS]; |
| 382 | char sep[MAX_NEIGHBOURS]; |
| 383 | int length; |
| 384 | } wordlist; |
| 385 | |
| 386 | typedef struct { |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 387 | long cutoff; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 388 | wordlist *wl; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 389 | char *token; |
| 390 | int N; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 391 | long from; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 392 | unsigned long upto; |
Marc Kupietz | c55c887 | 2017-12-01 23:27:10 +0100 | [diff] [blame] | 393 | collocator *best; |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 394 | float *target_sums; |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 395 | float *window_sums; |
Marc Kupietz | 3c0fab1 | 2019-08-02 15:07:47 +0200 | [diff] [blame] | 396 | float threshold; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 397 | } knnpars; |
| 398 | |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 399 | typedef struct { |
| 400 | uint32_t index; |
| 401 | float value; |
| 402 | } sparse_t; |
| 403 | |
| 404 | typedef struct { |
| 405 | uint32_t len; |
| 406 | sparse_t nbr[100]; |
| 407 | } profile_t; |
| 408 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 409 | float *M, *M2=0L, *syn1neg_window, *expTable; |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 410 | float *window_sums; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 411 | char *vocab; |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 412 | char *garbage = NULL; |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 413 | COLLOCATORDB *cdb = NULL; |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 414 | profile_t *sprofiles = NULL; |
| 415 | size_t sprofiles_qty = 0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 416 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 417 | long long words, size, merged_end; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 418 | long long merge_words = 0; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 419 | int num_threads=20; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 420 | int latin_enc=0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 421 | int window; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 422 | |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 423 | /* load collocation profiles if file exists */ |
| 424 | int load_sprofiles(char *vecsname) { |
| 425 | char *basename = strdup(vecsname); |
| 426 | char *pos = strstr(basename, ".vecs"); |
| 427 | if(pos) |
| 428 | *pos=0; |
| 429 | |
| 430 | char binsprofiles_fname[256]; |
| 431 | strcpy(binsprofiles_fname, basename); |
| 432 | strcat(binsprofiles_fname, ".sprofiles.bin"); |
| 433 | FILE *fp = fopen(binsprofiles_fname, "rb"); |
| 434 | if (fp == NULL) { |
| 435 | printf("Collocation profiles %s not found. No problem.\n", binsprofiles_fname); |
| 436 | return 0; |
| 437 | } |
| 438 | fseek(fp, 0L, SEEK_END); |
| 439 | size_t sz = ftell(fp); |
| 440 | fclose(fp); |
| 441 | |
| 442 | int fd = open(binsprofiles_fname, O_RDONLY); |
| 443 | sprofiles = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0); |
| 444 | if (sprofiles == MAP_FAILED) { |
| 445 | close(fd); |
| 446 | fprintf(stderr, "Cannot mmap %s\n", binsprofiles_fname); |
| 447 | sprofiles = NULL; |
| 448 | return 0; |
| 449 | } else { |
| 450 | sprofiles_qty = sz / sizeof(profile_t); |
| 451 | fprintf(stderr, "Successfully mmaped %s containing similar profiles for %ld word forms.\n", binsprofiles_fname, sprofiles_qty); |
| 452 | } |
| 453 | return 1; |
| 454 | } |
| 455 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 456 | int init_net(char *file_name, char *net_name, int latin) { |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 457 | FILE *f, *binvecs, *binwords; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 458 | int binwords_fd, binvecs_fd, net_fd, i; |
Marc Kupietz | 82b0267 | 2016-02-26 12:32:25 +0100 | [diff] [blame] | 459 | long long a, b, c, d, cn; |
| 460 | float len; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 461 | double val; |
Marc Kupietz | 82b0267 | 2016-02-26 12:32:25 +0100 | [diff] [blame] | 462 | |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 463 | char binvecs_fname[256], binwords_fname[256]; |
| 464 | strcpy(binwords_fname, file_name); |
| 465 | strcat(binwords_fname, ".words"); |
| 466 | strcpy(binvecs_fname, file_name); |
| 467 | strcat(binvecs_fname, ".vecs"); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 468 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 469 | latin_enc = latin; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 470 | f = fopen(file_name, "rb"); |
| 471 | if (f == NULL) { |
| 472 | printf("Input file %s not found\n", file_name); |
| 473 | return -1; |
| 474 | } |
| 475 | fscanf(f, "%lld", &words); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 476 | if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 477 | fscanf(f, "%lld", &size); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 478 | if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) { |
| 479 | printf("Converting %s to memory mappable structures\n", file_name); |
Marc Kupietz | f080976 | 2016-02-26 10:13:47 +0100 | [diff] [blame] | 480 | vocab = (char *)malloc((long long)words * max_w * sizeof(char)); |
| 481 | M = (float *)malloc((long long)words * (long long)size * sizeof(float)); |
| 482 | if (M == NULL) { |
| 483 | printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size); |
| 484 | return -1; |
| 485 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 486 | if(strstr(file_name, ".txt")) { |
| 487 | for (b = 0; b < words; b++) { |
| 488 | a = 0; |
| 489 | while (1) { |
| 490 | vocab[b * max_w + a] = fgetc(f); |
| 491 | if (feof(f) || (vocab[b * max_w + a] == ' ')) break; |
| 492 | if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++; |
| 493 | } |
| 494 | vocab[b * max_w + a] = 0; |
| 495 | len = 0; |
| 496 | for (a = 0; a < size; a++) { |
| 497 | fscanf(f, "%lf", &val); |
| 498 | M[a + b * size] = val; |
| 499 | len += val * val; |
| 500 | } |
| 501 | len = sqrt(len); |
| 502 | for (a = 0; a < size; a++) M[a + b * size] /= len; |
| 503 | } |
| 504 | } else { |
| 505 | for (b = 0; b < words; b++) { |
| 506 | a = 0; |
| 507 | while (1) { |
| 508 | vocab[b * max_w + a] = fgetc(f); |
| 509 | if (feof(f) || (vocab[b * max_w + a] == ' ')) break; |
| 510 | if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++; |
| 511 | } |
| 512 | vocab[b * max_w + a] = 0; |
| 513 | fread(&M[b * size], sizeof(float), size, f); |
| 514 | len = 0; |
| 515 | for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size]; |
| 516 | len = sqrt(len); |
| 517 | for (a = 0; a < size; a++) M[a + b * size] /= len; |
| 518 | } |
| 519 | } |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 520 | if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) { |
| 521 | fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs); |
| 522 | fclose(binvecs); |
| 523 | fwrite(vocab, sizeof(char), (long long)words * max_w, binwords); |
| 524 | fclose(binwords); |
| 525 | } |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 526 | } |
| 527 | if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) { |
| 528 | M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0); |
| 529 | vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0); |
| 530 | if (M == MAP_FAILED || vocab == MAP_FAILED) { |
| 531 | close(binvecs_fd); |
| 532 | close(binwords_fd); |
| 533 | fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname); |
| 534 | exit(-1); |
| 535 | } |
| 536 | } else { |
| 537 | fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname); |
| 538 | exit(-1); |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 539 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 540 | fclose(f); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 541 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 542 | if(net_name && strlen(net_name) > 0) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 543 | if( (net_fd = open(net_name, O_RDONLY)) >= 0) { |
| 544 | window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2; |
| 545 | // lseek(net_fd, sizeof(float) * words * size, SEEK_SET); |
Marc Kupietz | 10bec2b | 2016-03-23 09:41:31 +0100 | [diff] [blame] | 546 | // munmap(M, sizeof(float) * words * size); |
| 547 | M2 = mmap(0, sizeof(float) * words * size + sizeof(float) * 2 * window * size * words, PROT_READ, MAP_SHARED, net_fd, 0); |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 548 | if (M2 == MAP_FAILED) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 549 | close(net_fd); |
| 550 | fprintf(stderr, "Cannot mmap %s\n", net_name); |
| 551 | exit(-1); |
| 552 | } |
Marc Kupietz | 10bec2b | 2016-03-23 09:41:31 +0100 | [diff] [blame] | 553 | syn1neg_window = M2 + words * size; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 554 | } else { |
| 555 | fprintf(stderr, "Cannot open %s\n", net_name); |
| 556 | exit(-1); |
| 557 | } |
| 558 | fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window); |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 559 | |
| 560 | char collocatordb_name[2048]; |
| 561 | strcpy(collocatordb_name, net_name); |
| 562 | char *ext = rindex(collocatordb_name, '.'); |
| 563 | if(ext) { |
| 564 | strcpy(ext, ".rocksdb"); |
| 565 | if(access(collocatordb_name, R_OK) == 0) { |
| 566 | *ext = 0; |
| 567 | fprintf(stderr, "Opening collocator DB %s\n", collocatordb_name); |
| 568 | cdb = open_collocatordb(collocatordb_name); |
| 569 | } |
| 570 | } |
| 571 | } |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 572 | |
| 573 | expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float)); |
| 574 | for (i = 0; i < EXP_TABLE_SIZE; i++) { |
| 575 | expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table |
| 576 | expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1) |
| 577 | } |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 578 | window_sums = malloc(sizeof(float) * (window+1) * 2); |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 579 | |
| 580 | return 0; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 581 | } |
| 582 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 583 | long mergeVectors(char *file_name){ |
| 584 | FILE *f, *binvecs, *binwords; |
| 585 | int binwords_fd, binvecs_fd, net_fd, i; |
| 586 | long long a, b, c, d, cn; |
| 587 | float len; |
| 588 | float *merge_vecs; |
| 589 | char *merge_vocab; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 590 | /* long long merge_words, merge_size; */ |
| 591 | long long merge_size; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 592 | |
| 593 | char binvecs_fname[256], binwords_fname[256]; |
| 594 | strcpy(binwords_fname, file_name); |
| 595 | strcat(binwords_fname, ".words"); |
| 596 | strcpy(binvecs_fname, file_name); |
| 597 | strcat(binvecs_fname, ".vecs"); |
| 598 | |
| 599 | f = fopen(file_name, "rb"); |
| 600 | if (f == NULL) { |
| 601 | printf("Input file %s not found\n", file_name); |
| 602 | exit -1; |
| 603 | } |
| 604 | fscanf(f, "%lld", &merge_words); |
| 605 | fscanf(f, "%lld", &merge_size); |
| 606 | if(merge_size != size){ |
| 607 | fprintf(stderr, "vectors must have the same length\n"); |
| 608 | exit(-1); |
| 609 | } |
| 610 | if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) { |
| 611 | merge_vecs = malloc(sizeof(float) * (words + merge_words) * size); |
| 612 | merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w); |
| 613 | if (merge_vecs == NULL || merge_vocab == NULL) { |
| 614 | close(binvecs_fd); |
| 615 | close(binwords_fd); |
| 616 | fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname); |
| 617 | exit(-1); |
| 618 | } |
| 619 | read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float)); |
| 620 | read(binwords_fd, merge_vocab, merge_words * max_w); |
| 621 | } else { |
| 622 | fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname); |
| 623 | exit(-1); |
| 624 | } |
| 625 | printf("Successfully reallocated memory\nMerging...\n"); |
| 626 | fflush(stdout); |
| 627 | memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float)); |
| 628 | memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w); |
| 629 | munmap(M, words * size * sizeof(float)); |
| 630 | munmap(vocab, words * max_w); |
| 631 | M = merge_vecs; |
| 632 | vocab = merge_vocab; |
| 633 | merged_end = merge_words; |
| 634 | words += merge_words; |
| 635 | fclose(f); |
| 636 | printf("merged_end: %lld, words: %lld\n", merged_end, words); |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 637 | //printBiggestMergedDifferences(); |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 638 | return((long) merged_end); |
| 639 | } |
| 640 | |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 641 | void filter_garbage() { |
| 642 | long i; |
Marc Kupietz | ab591a8 | 2016-04-28 14:08:49 +0200 | [diff] [blame] | 643 | unsigned char *w, previous, c; |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 644 | garbage = malloc(words); |
| 645 | memset(garbage, 0, words); |
| 646 | for (i = 0; i < words; i++) { |
| 647 | w = vocab + i * max_w; |
| 648 | previous = 0; |
Marc Kupietz | 969adfa | 2018-03-12 09:43:53 +0100 | [diff] [blame] | 649 | if(strncmp("quot", w, 4) == 0) { |
| 650 | garbage[i]=1; |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 651 | // printf("Gargabe: %s\n", vocab + i * max_w); |
Marc Kupietz | 969adfa | 2018-03-12 09:43:53 +0100 | [diff] [blame] | 652 | } else { |
| 653 | while((c = *w++) && !garbage[i]) { |
| 654 | if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) || |
| 655 | (previous == '-' && (c & 32)) || |
| 656 | (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) || |
| 657 | (previous == 'q' && c == 'u' && *(w) == 'o' && *(w+1) == 't') || /* quot */ |
| 658 | c == '<' |
| 659 | ) { |
| 660 | garbage[i]=1; |
| 661 | continue; |
| 662 | } |
| 663 | previous = c; |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 664 | } |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | return; |
| 668 | } |
| 669 | |
Marc Kupietz | ac707b3 | 2018-12-20 11:36:38 +0100 | [diff] [blame] | 670 | |
| 671 | knn *simpleGetCollocators(int word, int number, long cutoff, int *result) { |
| 672 | knnpars *pars = calloc(sizeof(knnpars), 1); |
| 673 | float *target_sums; |
| 674 | float *window_sums = malloc(sizeof(float) * (window+1) * 2); |
| 675 | pars->cutoff = (cutoff? cutoff : 300000); |
| 676 | long a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float)); |
| 677 | for(a = 0; a < cutoff; a++) |
| 678 | target_sums[a] = 0; |
| 679 | pars->target_sums = target_sums; |
| 680 | pars->window_sums = window_sums; |
| 681 | pars->N = (number? number : 20); |
| 682 | pars->from = 0; |
| 683 | pars->upto = window * 2 -1; |
Marc Kupietz | 7631e02 | 2019-01-25 12:04:42 +0100 | [diff] [blame] | 684 | knn *syn_nbs = NULL; // = (knn*) getCollocators(pars); |
Marc Kupietz | ac707b3 | 2018-12-20 11:36:38 +0100 | [diff] [blame] | 685 | free(pars); |
| 686 | free(window_sums); |
| 687 | free(target_sums); |
| 688 | return syn_nbs; |
| 689 | } |
| 690 | |
Marc Kupietz | 580ebdf | 2017-11-29 21:18:38 +0100 | [diff] [blame] | 691 | void *getCollocators(void *args) { |
| 692 | knnpars *pars = args; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 693 | int N = pars->N; |
Marc Kupietz | 5270283 | 2019-01-15 17:55:52 +0100 | [diff] [blame] | 694 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 695 | int cc = pars->wl->wordi[0]; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 696 | knn *nbs = NULL; |
| 697 | long window_layer_size = size * window * 2; |
| 698 | long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target; |
| 699 | float f, max_f, maxmax_f; |
Marc Kupietz | 7631e02 | 2019-01-25 12:04:42 +0100 | [diff] [blame] | 700 | float *target_sums=NULL, worstbest, wpos_sum; |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 701 | collocator *best; |
Marc Kupietz | d564258 | 2016-03-19 22:23:13 +0100 | [diff] [blame] | 702 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 703 | if(M2 == NULL || cc == -1) |
Marc Kupietz | d564258 | 2016-03-19 22:23:13 +0100 | [diff] [blame] | 704 | return NULL; |
| 705 | |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 706 | a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float)); |
Marc Kupietz | 7631e02 | 2019-01-25 12:04:42 +0100 | [diff] [blame] | 707 | memset(target_sums, 0, pars->cutoff * sizeof(float)); |
| 708 | best = malloc((N>200?N:200) * sizeof(collocator)); |
| 709 | memset(best, 0, (N>200?N:200) * sizeof(collocator)); |
Marc Kupietz | 3c0fab1 | 2019-08-02 15:07:47 +0200 | [diff] [blame] | 710 | worstbest = pars->threshold; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 711 | |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 712 | for (b = 0; b < pars->cutoff; b++) |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 713 | target_sums[b]=0; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 714 | for (b = 0; b < N; b++) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 715 | best[b].wordi = -1; |
| 716 | best[b].probability = 1; |
| 717 | best[b].activation = worstbest; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 718 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 719 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 720 | d = cc; |
| 721 | maxmax_f = -1; |
| 722 | maxmax_target = 0; |
| 723 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 724 | for (a = pars->from; a < pars->upto; a++) { |
| 725 | if(a >= window) |
| 726 | a++; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 727 | wpos_sum = 0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 728 | printf("window pos: %ld\n", a); |
| 729 | if (a != window) { |
| 730 | max_f = -1; |
| 731 | window_offset = a * size; |
| 732 | if (a > window) |
| 733 | window_offset -= size; |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 734 | for(target = 0; target < pars->cutoff; target ++) { |
| 735 | if(garbage && garbage[target]) continue; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 736 | if(target == d) |
| 737 | continue; |
| 738 | f = 0; |
| 739 | for (c = 0; c < size; c++) |
Marc Kupietz | 10bec2b | 2016-03-23 09:41:31 +0100 | [diff] [blame] | 740 | f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c]; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 741 | if (f < -MAX_EXP) |
| 742 | continue; |
| 743 | else if (f > MAX_EXP) |
| 744 | continue; |
| 745 | else |
| 746 | f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))]; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 747 | wpos_sum += f; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 748 | |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 749 | target_sums[target] += f; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 750 | if(f > worstbest) { |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 751 | for (b = 0; b < N; b++) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 752 | if (f > best[b].activation) { |
| 753 | memmove(best + b + 1, best + b, (N - b -1) * sizeof(collocator)); |
| 754 | best[b].activation = f; |
| 755 | best[b].wordi = target; |
| 756 | best[b].position = window-a; |
Marc Kupietz | 33679a3 | 2016-03-22 08:49:39 +0100 | [diff] [blame] | 757 | break; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 758 | } |
| 759 | } |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 760 | if(b == N - 1) |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 761 | worstbest = best[N-1].activation; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | printf("%d %.2f\n", max_target, max_f); |
| 765 | printf("%s (%.2f) ", &vocab[max_target * max_w], max_f); |
| 766 | if(max_f > maxmax_f) { |
| 767 | maxmax_f = max_f; |
| 768 | maxmax_target = max_target; |
| 769 | } |
Marc Kupietz | 33679a3 | 2016-03-22 08:49:39 +0100 | [diff] [blame] | 770 | for (b = 0; b < N; b++) |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 771 | if(best[b].position == window-a) |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 772 | best[b].cprobability = best[b].activation / wpos_sum; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 773 | } else { |
| 774 | printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]); |
| 775 | } |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 776 | pars->window_sums[a] = wpos_sum; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 777 | } |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 778 | for (b = 0; b < pars->cutoff; b++) |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 779 | pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2); |
Marc Kupietz | d89431f | 2019-08-02 15:06:38 +0200 | [diff] [blame] | 780 | |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 781 | free(target_sums); |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 782 | for(b=0; b<N && best[b].wordi >= 0; b++);; // THIS LOOP IS NEEDED (b...) |
| 783 | // printf("%d: best syn: %s %.2f %.5f\n", b, &vocab[best[b].wordi*max_w], best[b].activation, best[b].probability); |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 784 | // printf("\n"); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 785 | nbs = malloc(sizeof(knn)); |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 786 | nbs->best = best; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 787 | nbs->length = b-1; |
| 788 | pthread_exit(nbs); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 789 | } |
| 790 | |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 791 | |
Marc Kupietz | 66bfd95 | 2017-12-11 09:59:45 +0100 | [diff] [blame] | 792 | AV *getVecs(AV *array) { |
| 793 | int i, b; |
| 794 | AV *result = newAV(); |
| 795 | for (i=0; i<=av_len(array); i++) { |
| 796 | SV** elem = av_fetch(array, i, 0); |
| 797 | if (elem != NULL) { |
| 798 | long j = (long) SvNV(*elem); |
| 799 | AV *vector = newAV(); |
| 800 | for (b = 0; b < size; b++) { |
| 801 | av_push(vector, newSVnv(M[b + j * size])); |
| 802 | } |
| 803 | av_push(result, newRV_noinc(vector)); |
| 804 | } |
| 805 | } |
| 806 | return result; |
| 807 | } |
| 808 | |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 809 | char *getSimilarProfiles(long node) { |
| 810 | int i; |
| 811 | char buffer[120000]; |
| 812 | char pair_buffer[2048]; |
| 813 | buffer[0]='['; |
| 814 | buffer[1]=0; |
| 815 | if(node >= sprofiles_qty) { |
| 816 | printf("Not available in precomputed profile\n"); |
| 817 | return(strdup("[{\"w\":\"not available\", \"v\":0}]\n")); |
| 818 | } |
| 819 | |
| 820 | printf("******* %s ******\n", &vocab[max_w * node]); |
| 821 | |
| 822 | for(i=0; i < 100 && i < sprofiles[node].len; i++) { |
| 823 | sprintf(pair_buffer, "{\"w\":\"%s\", \"v\":%f},", &vocab[max_w * (sprofiles[node].nbr[i].index)], sprofiles[node].nbr[i].value); |
| 824 | strcat(buffer, pair_buffer); |
| 825 | } |
| 826 | buffer[strlen(buffer)-1]=']'; |
| 827 | strcat(buffer, "\n"); |
| 828 | printf(buffer); |
| 829 | return(strdup(buffer)); |
| 830 | } |
| 831 | |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 832 | char *getClassicCollocators(long node) { |
| 833 | char *res = (cdb? strdup(get_collocators_as_json(cdb, node)) : "[]"); |
| 834 | return res; |
| 835 | } |
| 836 | |
Marc Kupietz | 0957d53 | 2019-01-03 17:54:00 +0100 | [diff] [blame] | 837 | wordlist *getTargetWords(char *st1, int search_backw) { |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 838 | wordlist *wl = malloc(sizeof(wordlist)); |
| 839 | char st[100][max_size], sep[100]; |
Marc Kupietz | 1f36dfa | 2019-01-03 16:18:12 +0100 | [diff] [blame] | 840 | long a, b=0, c=0, cn=0; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 841 | int unmerged; |
| 842 | |
Marc Kupietz | 1f36dfa | 2019-01-03 16:18:12 +0100 | [diff] [blame] | 843 | while (1) { |
| 844 | st[cn][b] = st1[c]; |
Marc Kupietz | 1f36dfa | 2019-01-03 16:18:12 +0100 | [diff] [blame] | 845 | b++; |
| 846 | c++; |
| 847 | st[cn][b] = 0; |
| 848 | if (st1[c] == 0) break; |
| 849 | if (st1[c] == ' ' || st1[c] == '-') { |
Marc Kupietz | 95aa1c0 | 2016-03-15 09:40:43 +0100 | [diff] [blame] | 850 | sep[cn++] = st1[c]; |
Marc Kupietz | 1f36dfa | 2019-01-03 16:18:12 +0100 | [diff] [blame] | 851 | b = 0; |
| 852 | c++; |
| 853 | } |
| 854 | } |
| 855 | cn++; |
Marc Kupietz | 1f36dfa | 2019-01-03 16:18:12 +0100 | [diff] [blame] | 856 | for (a = 0; a < cn; a++) { |
| 857 | if (search_backw) { |
Marc Kupietz | 3082fd0 | 2019-01-09 14:54:06 +0100 | [diff] [blame] | 858 | for (b = words - 1; b >= (merge_words? merge_words : 0) && strcmp(&vocab[b * max_w], st[a]) !=0; b--); |
Marc Kupietz | 1f36dfa | 2019-01-03 16:18:12 +0100 | [diff] [blame] | 859 | } else { |
Marc Kupietz | 3082fd0 | 2019-01-09 14:54:06 +0100 | [diff] [blame] | 860 | for (b = 0; b < (merge_words? merge_words : words) && strcmp(&vocab[b * max_w], st[a]) != 0; b++); |
Marc Kupietz | 1f36dfa | 2019-01-03 16:18:12 +0100 | [diff] [blame] | 861 | } |
| 862 | if (b == words) b = -1; |
| 863 | wl->wordi[a] = b; |
Marc Kupietz | 1f36dfa | 2019-01-03 16:18:12 +0100 | [diff] [blame] | 864 | if (b == -1) { |
| 865 | fprintf(stderr, "Out of dictionary word!\n"); |
| 866 | cn--; |
Marc Kupietz | 3082fd0 | 2019-01-09 14:54:06 +0100 | [diff] [blame] | 867 | } else { |
| 868 | fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", &vocab[wl->wordi[a]*max_w], wl->wordi[a]); |
Marc Kupietz | 1f36dfa | 2019-01-03 16:18:12 +0100 | [diff] [blame] | 869 | } |
| 870 | } |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 871 | wl->length=cn; |
| 872 | return(wl); |
| 873 | } |
Marc Kupietz | 1f36dfa | 2019-01-03 16:18:12 +0100 | [diff] [blame] | 874 | |
Marc Kupietz | 98ed1c0 | 2019-08-02 15:05:37 +0200 | [diff] [blame] | 875 | float get_distance(long b, long c) { |
| 876 | long a; |
| 877 | float dist = 0; |
| 878 | for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + b * size]; |
| 879 | return dist; |
| 880 | } |
| 881 | |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 882 | char *getBiggestMergedDifferences() { |
| 883 | static char *result = NULL; |
Marc Kupietz | ebc131e | 2019-01-03 16:24:27 +0100 | [diff] [blame] | 884 | float dist, len, vec[max_size]; |
| 885 | long long a, b, c, d, cn, *bi; |
| 886 | char ch; |
| 887 | knn *nbs = NULL; |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 888 | int N = 1000; |
| 889 | |
Marc Kupietz | b6514dc | 2019-06-26 11:47:56 +0200 | [diff] [blame] | 890 | if(merged_end == 0) |
| 891 | result = "[]"; |
| 892 | |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 893 | if(result != NULL) |
| 894 | return result; |
Marc Kupietz | ebc131e | 2019-01-03 16:24:27 +0100 | [diff] [blame] | 895 | |
| 896 | printf("Looking for biggest distances between main and merged vectors ...\n"); |
| 897 | collocator *best; |
| 898 | best = malloc(N * sizeof(collocator)); |
| 899 | memset(best, 0, N * sizeof(collocator)); |
| 900 | |
| 901 | float worstbest=1000000; |
| 902 | |
| 903 | for (a = 0; a < N; a++) best[a].activation = worstbest; |
| 904 | |
Marc Kupietz | 2da2a81 | 2019-02-21 14:17:35 +0100 | [diff] [blame] | 905 | for (c = 0; c < 500000; c++) { |
Marc Kupietz | ebc131e | 2019-01-03 16:24:27 +0100 | [diff] [blame] | 906 | if(garbage && garbage[c]) continue; |
| 907 | a = 0; |
| 908 | dist = 0; |
| 909 | for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + (c+merged_end) * size]; |
| 910 | if(dist < worstbest) { |
| 911 | for (a = 0; a < N; a++) { |
| 912 | if (dist < best[a].activation) { |
| 913 | memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator)); |
| 914 | best[a].activation = dist; |
| 915 | best[a].wordi = c; |
| 916 | break; |
| 917 | } |
| 918 | } |
| 919 | worstbest = best[N-1].activation; |
| 920 | } |
| 921 | } |
| 922 | |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 923 | result = malloc(N*max_w); |
| 924 | char *p = result; |
| 925 | *p++ = '['; *p = 0; |
Marc Kupietz | ebc131e | 2019-01-03 16:24:27 +0100 | [diff] [blame] | 926 | for (a = 0; a < N; a++) { |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 927 | p += sprintf(p, "{\"rank\":%d,\"word\":\"%s\",\"dist\":%.3f},", a, &vocab[best[a].wordi * max_w], 1-best[a].activation); |
Marc Kupietz | ebc131e | 2019-01-03 16:24:27 +0100 | [diff] [blame] | 928 | } |
Marc Kupietz | d7760b4 | 2019-02-21 09:01:44 +0100 | [diff] [blame] | 929 | *--p = ']'; |
| 930 | return(result); |
Marc Kupietz | ebc131e | 2019-01-03 16:24:27 +0100 | [diff] [blame] | 931 | } |
| 932 | |
Marc Kupietz | 98ed1c0 | 2019-08-02 15:05:37 +0200 | [diff] [blame] | 933 | |
| 934 | float cos_similarity(long b, long c) { |
| 935 | float dist=0; |
| 936 | long a; |
| 937 | for (a = 0; a < size; a++) dist += M[b * size + a] * M[c * size + a]; |
| 938 | return dist; |
| 939 | } |
| 940 | |
| 941 | char *cos_similarity_as_json(char *w1, char *w2) { |
| 942 | wordlist *a, *b; |
| 943 | float res; |
| 944 | a = getTargetWords(w1, 0); |
| 945 | b = getTargetWords(w2, 0); |
| 946 | if (a == NULL || b==NULL || a->length != 1 || b->length != 1) |
| 947 | res = -1; |
| 948 | else |
| 949 | res = cos_similarity(a->wordi[0], b->wordi[0]); |
| 950 | fprintf(stderr, "a: %lld b: %lld res:%f\n", a->wordi[0], b->wordi[0], res); |
| 951 | char *json = malloc(16); |
| 952 | sprintf(json, "%.5f", res); |
| 953 | return json; |
| 954 | } |
| 955 | |
Marc Kupietz | 580ebdf | 2017-11-29 21:18:38 +0100 | [diff] [blame] | 956 | void *_get_neighbours(void *arg) { |
| 957 | knnpars *pars = arg; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 958 | char *st1 = pars->token; |
| 959 | int N = pars->N; |
| 960 | long from = pars -> from; |
| 961 | unsigned long upto = pars -> upto; |
| 962 | char file_name[max_size], st[100][max_size], *sep; |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 963 | float dist, len, vec[max_size]; |
| 964 | long long a, b, c, d, cn, *bi; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 965 | char ch; |
| 966 | knn *nbs = NULL; |
| 967 | wordlist *wl = pars->wl; |
| 968 | |
Marc Kupietz | c55c887 | 2017-12-01 23:27:10 +0100 | [diff] [blame] | 969 | collocator *best = pars->best; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 970 | |
| 971 | float worstbest=-1; |
| 972 | |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 973 | for (a = 0; a < N; a++) best[a].activation = 0; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 974 | a = 0; |
| 975 | bi = wl->wordi; |
| 976 | cn = wl->length; |
| 977 | sep = wl->sep; |
| 978 | b = bi[0]; |
| 979 | c = 0; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 980 | if (b == -1) { |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 981 | N = 0; |
| 982 | goto end; |
| 983 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 984 | for (a = 0; a < size; a++) vec[a] = 0; |
| 985 | for (b = 0; b < cn; b++) { |
| 986 | if (bi[b] == -1) continue; |
Marc Kupietz | 95aa1c0 | 2016-03-15 09:40:43 +0100 | [diff] [blame] | 987 | if(b>0 && sep[b-1] == '-') |
| 988 | for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size]; |
| 989 | else |
| 990 | for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 991 | } |
| 992 | len = 0; |
| 993 | for (a = 0; a < size; a++) len += vec[a] * vec[a]; |
| 994 | len = sqrt(len); |
| 995 | for (a = 0; a < size; a++) vec[a] /= len; |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 996 | for (a = 0; a < N; a++) best[a].activation = -1; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 997 | for (c = from; c < upto; c++) { |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 998 | if(garbage && garbage[c]) continue; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 999 | a = 0; |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 1000 | // do not skip taget word |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 1001 | // for (b = 0; b < cn; b++) if (bi[b] == c) a = 1; |
| 1002 | // if (a == 1) continue; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 1003 | dist = 0; |
| 1004 | for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size]; |
Marc Kupietz | be1b9fc | 2016-02-26 10:34:30 +0100 | [diff] [blame] | 1005 | if(dist > worstbest) { |
| 1006 | for (a = 0; a < N; a++) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1007 | if (dist > best[a].activation) { |
| 1008 | memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator)); |
| 1009 | best[a].activation = dist; |
| 1010 | best[a].wordi = c; |
Marc Kupietz | be1b9fc | 2016-02-26 10:34:30 +0100 | [diff] [blame] | 1011 | break; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 1012 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 1013 | } |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1014 | worstbest = best[N-1].activation; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 1015 | } |
| 1016 | } |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 1017 | |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 1018 | end: |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 1019 | pthread_exit(nbs); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 1020 | } |
| 1021 | |
Marc Kupietz | e0e4213 | 2017-11-24 16:44:34 +0100 | [diff] [blame] | 1022 | int cmp_activation (const void * a, const void * b) { |
| 1023 | float fb = ((collocator *)a)->activation; |
| 1024 | float fa = ((collocator *)b)->activation; |
| 1025 | return (fa > fb) - (fa < fb); |
| 1026 | } |
| 1027 | |
| 1028 | int cmp_probability (const void * a, const void * b) { |
| 1029 | float fb = ((collocator *)a)->probability; |
| 1030 | float fa = ((collocator *)b)->probability; |
| 1031 | return (fa > fb) - (fa < fb); |
| 1032 | } |
| 1033 | |
Marc Kupietz | 33c79d3 | 2019-08-02 15:11:23 +0200 | [diff] [blame] | 1034 | char* getPosWiseW2VCollocatorsAsTsv(char *word, long maxPerPos, long cutoff, float threshold) { |
| 1035 | HV *result = newHV(); |
| 1036 | float *target_sums=NULL, vec[max_size]; |
| 1037 | long long old_words; |
| 1038 | long a, b, c, d; |
| 1039 | knn *para_nbs[MAX_THREADS]; |
| 1040 | knn *syn_nbs[MAX_THREADS]; |
| 1041 | knnpars pars[MAX_THREADS]; |
| 1042 | pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t)); |
| 1043 | wordlist *wl; |
| 1044 | int syn_threads = (M2? window * 2 : 0); |
| 1045 | int search_backw = 0; |
| 1046 | collocator *best = NULL; |
| 1047 | posix_memalign((void **) &best, 128, 10 * (maxPerPos>=200? maxPerPos : 200) * sizeof(collocator)); |
| 1048 | memset(best, 0, (maxPerPos>=200? maxPerPos : 200) * sizeof(collocator)); |
| 1049 | |
| 1050 | |
| 1051 | if(cutoff < 1 || cutoff > words) |
| 1052 | cutoff=words; |
| 1053 | |
| 1054 | wl = getTargetWords(word, search_backw); |
| 1055 | if(wl == NULL || wl->length < 1) |
| 1056 | return ""; |
| 1057 | |
| 1058 | a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float)); |
| 1059 | memset(target_sums, 0, cutoff * sizeof(float)); |
| 1060 | |
| 1061 | printf("Starting %d threads\n", syn_threads); |
| 1062 | fflush(stdout); |
| 1063 | for(a=0; a < syn_threads; a++) { |
| 1064 | pars[a].cutoff = cutoff; |
| 1065 | pars[a].target_sums = target_sums; |
| 1066 | pars[a].window_sums = window_sums; |
| 1067 | pars[a].wl = wl; |
| 1068 | pars[a].N = maxPerPos; |
| 1069 | pars[a].threshold = threshold; |
| 1070 | pars[a].from = a; |
| 1071 | pars[a].upto = a+1; |
| 1072 | pthread_create(&pt[a], NULL, getCollocators, (void *) &pars[a]); |
| 1073 | } |
| 1074 | printf("Waiting for syn threads to join\n"); |
| 1075 | fflush(stdout); |
| 1076 | for (a = 0; a < syn_threads; a++) pthread_join(pt[a], (void *) &syn_nbs[a]); |
| 1077 | printf("Syn threads joint\n"); |
| 1078 | fflush(stdout); |
| 1079 | result = malloc(maxPerPos*80*syn_threads); |
| 1080 | char *p = result; |
| 1081 | *p = 0; |
| 1082 | for (a = syn_threads -1; a >= 0; a--) { |
| 1083 | for (b=0; b < syn_nbs[a]->length; b++) { |
| 1084 | p += sprintf(p, "%ld\t%s\t%f\n", syn_nbs[a]->best[b].position, &vocab[syn_nbs[a]->best[b].wordi * max_w], syn_nbs[a]->best[b].activation); |
| 1085 | } |
| 1086 | } |
| 1087 | return(result); |
| 1088 | } |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 1089 | |
Marc Kupietz | ac707b3 | 2018-12-20 11:36:38 +0100 | [diff] [blame] | 1090 | SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe, int no_similar_profiles) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 1091 | HV *result = newHV(); |
Marc Kupietz | 7631e02 | 2019-01-25 12:04:42 +0100 | [diff] [blame] | 1092 | float *target_sums=NULL, vec[max_size]; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 1093 | long long old_words; |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1094 | long a, b, c, d, slice; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 1095 | knn *para_nbs[MAX_THREADS]; |
| 1096 | knn *syn_nbs[MAX_THREADS]; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 1097 | knnpars pars[MAX_THREADS]; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 1098 | pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t)); |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 1099 | wordlist *wl; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1100 | int syn_threads = (M2? window * 2 : 0); |
Marc Kupietz | ac707b3 | 2018-12-20 11:36:38 +0100 | [diff] [blame] | 1101 | int para_threads = (no_similar_profiles? 0 : num_threads - syn_threads); |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1102 | |
Marc Kupietz | 7631e02 | 2019-01-25 12:04:42 +0100 | [diff] [blame] | 1103 | collocator *best = NULL; |
| 1104 | posix_memalign((void **) &best, 128, 10 * (N>=200? N : 200) * sizeof(collocator)); |
| 1105 | memset(best, 0, (N>=200? N : 200) * sizeof(collocator)); |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1106 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 1107 | if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS; |
| 1108 | |
Marc Kupietz | d5d0573 | 2018-01-30 11:57:35 +0100 | [diff] [blame] | 1109 | if(cutoff < 1 || cutoff > words) |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 1110 | cutoff=words; |
| 1111 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 1112 | wl = getTargetWords(st1, search_backw); |
Marc Kupietz | f9ac54e | 2017-11-21 09:22:29 +0100 | [diff] [blame] | 1113 | if(wl == NULL || wl->length < 1) |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 1114 | goto end; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 1115 | |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 1116 | old_words = cutoff; |
Marc Kupietz | 3082fd0 | 2019-01-09 14:54:06 +0100 | [diff] [blame] | 1117 | slice = cutoff / para_threads; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 1118 | |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 1119 | a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float)); |
Marc Kupietz | 7631e02 | 2019-01-25 12:04:42 +0100 | [diff] [blame] | 1120 | memset(target_sums, 0, cutoff * sizeof(float)); |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 1121 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1122 | printf("Starting %d threads\n", para_threads); |
| 1123 | fflush(stdout); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 1124 | for(a=0; a < para_threads; a++) { |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 1125 | pars[a].cutoff = cutoff; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 1126 | pars[a].token = st1; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 1127 | pars[a].wl = wl; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 1128 | pars[a].N = N; |
Marc Kupietz | c55c887 | 2017-12-01 23:27:10 +0100 | [diff] [blame] | 1129 | pars[a].best = &best[N*a]; |
Marc Kupietz | 3082fd0 | 2019-01-09 14:54:06 +0100 | [diff] [blame] | 1130 | if(merge_words == 0 || search_backw == 0) { |
Marc Kupietz | 1f0f780 | 2019-01-03 16:23:29 +0100 | [diff] [blame] | 1131 | pars[a].from = a*slice; |
| 1132 | pars[a].upto = ((a+1)*slice > cutoff? cutoff : (a+1) * slice); |
| 1133 | } else { |
Marc Kupietz | 3082fd0 | 2019-01-09 14:54:06 +0100 | [diff] [blame] | 1134 | pars[a].from = merge_words + a * slice; |
| 1135 | pars[a].upto = merge_words + ((a+1)*slice > cutoff? cutoff : (a+1) * slice); |
Marc Kupietz | 1f0f780 | 2019-01-03 16:23:29 +0100 | [diff] [blame] | 1136 | } |
| 1137 | printf("From: %ld, Upto: %ld\n", pars[a].from, pars[a].upto); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 1138 | pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]); |
| 1139 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1140 | if(M2) { |
| 1141 | for(a=0; a < syn_threads; a++) { |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 1142 | pars[a + para_threads].cutoff = cutoff; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1143 | pars[a + para_threads].target_sums = target_sums; |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 1144 | pars[a + para_threads].window_sums = window_sums; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1145 | pars[a + para_threads].wl = wl; |
| 1146 | pars[a + para_threads].N = N; |
Marc Kupietz | 3c0fab1 | 2019-08-02 15:07:47 +0200 | [diff] [blame] | 1147 | pars[a + para_threads].threshold = MIN_RESP; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1148 | pars[a + para_threads].from = a; |
| 1149 | pars[a + para_threads].upto = a+1; |
| 1150 | pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]); |
| 1151 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 1152 | } |
| 1153 | printf("Waiting for para threads to join\n"); |
| 1154 | fflush(stdout); |
Marc Kupietz | 580ebdf | 2017-11-29 21:18:38 +0100 | [diff] [blame] | 1155 | for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *) ¶_nbs[a]); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 1156 | printf("Para threads joint\n"); |
| 1157 | fflush(stdout); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 1158 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1159 | /* if(!syn_nbs[0]) */ |
| 1160 | /* goto end; */ |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 1161 | |
Marc Kupietz | c55c887 | 2017-12-01 23:27:10 +0100 | [diff] [blame] | 1162 | qsort(best, N*para_threads, sizeof(collocator), cmp_activation); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 1163 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 1164 | |
Marc Kupietz | d0d3e91 | 2017-12-01 22:09:43 +0100 | [diff] [blame] | 1165 | long long chosen[MAX_NEIGHBOURS]; |
| 1166 | printf("N: %ld\n", N); |
| 1167 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 1168 | AV* array = newAV(); |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 1169 | int i, j; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 1170 | int l1_words=0, l2_words=0; |
Marc Kupietz | d0d3e91 | 2017-12-01 22:09:43 +0100 | [diff] [blame] | 1171 | |
| 1172 | for (a = 0, i = 0; i < N && a < N*para_threads; a++) { |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 1173 | int filtered=0; |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1174 | long long c = best[a].wordi; |
Marc Kupietz | e8b9dd2 | 2019-01-03 17:55:59 +0100 | [diff] [blame] | 1175 | if ((merge_words && dedupe && i > 1) || (!merge_words && dedupe && i > 0)) { |
Marc Kupietz | d0d3e91 | 2017-12-01 22:09:43 +0100 | [diff] [blame] | 1176 | for (j=0; j<i && !filtered; j++) |
| 1177 | if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) || |
| 1178 | strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) { |
| 1179 | printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]); |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 1180 | filtered = 1; |
| 1181 | } |
| 1182 | if(filtered) |
| 1183 | continue; |
| 1184 | } |
Marc Kupietz | 3082fd0 | 2019-01-09 14:54:06 +0100 | [diff] [blame] | 1185 | |
| 1186 | |
| 1187 | if(0 && merge_words > 0) { |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 1188 | if(c >= merge_words) { |
| 1189 | if(l1_words > N / 2) |
| 1190 | continue; |
| 1191 | else |
| 1192 | l1_words++; |
| 1193 | } else { |
| 1194 | if(l2_words > N / 2) |
| 1195 | continue; |
| 1196 | else |
| 1197 | l2_words++; |
| 1198 | } |
| 1199 | } |
Marc Kupietz | 3082fd0 | 2019-01-09 14:54:06 +0100 | [diff] [blame] | 1200 | |
| 1201 | // printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a); |
| 1202 | // fflush(stdout); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 1203 | HV* hash = newHV(); |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 1204 | SV* word = newSVpvf(&vocab[c * max_w], 0); |
Marc Kupietz | d0d3e91 | 2017-12-01 22:09:43 +0100 | [diff] [blame] | 1205 | chosen[i] = c; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 1206 | if(latin_enc == 0) SvUTF8_on(word); |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 1207 | fflush(stdout); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 1208 | hv_store(hash, "word", strlen("word"), word , 0); |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1209 | hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0); |
| 1210 | hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 1211 | AV *vector = newAV(); |
| 1212 | for (b = 0; b < size; b++) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1213 | av_push(vector, newSVnv(M[b + best[a].wordi * size])); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 1214 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 1215 | hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0); |
| 1216 | av_push(array, newRV_noinc((SV*)hash)); |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 1217 | i++; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 1218 | } |
| 1219 | hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0); |
Marc Kupietz | 7631e02 | 2019-01-25 12:04:42 +0100 | [diff] [blame] | 1220 | |
Marc Kupietz | 50485ba | 2016-03-23 09:13:14 +0100 | [diff] [blame] | 1221 | for(b=0; b < MAX_NEIGHBOURS; b++) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1222 | best[b].wordi = -1L; |
| 1223 | best[b].activation = 0; |
| 1224 | best[b].probability = 0; |
| 1225 | best[b].position = 0; |
| 1226 | best[b].activation_sum = 0; |
Marc Kupietz | 7631e02 | 2019-01-25 12:04:42 +0100 | [diff] [blame] | 1227 | memset(best[b].heat, 0, sizeof(float)*16); |
Marc Kupietz | 50485ba | 2016-03-23 09:13:14 +0100 | [diff] [blame] | 1228 | } |
| 1229 | |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1230 | float total_activation = 0; |
| 1231 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1232 | if (M2) { |
| 1233 | printf("Waiting for syn threads to join\n"); |
| 1234 | fflush(stdout); |
Marc Kupietz | 580ebdf | 2017-11-29 21:18:38 +0100 | [diff] [blame] | 1235 | for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], (void *) &syn_nbs[a]); |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1236 | for (a = 0; a <= syn_threads; a++) { |
Marc Kupietz | 6387218 | 2018-03-12 09:44:49 +0100 | [diff] [blame] | 1237 | if(a == window) continue; |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1238 | total_activation += window_sums[a]; |
| 1239 | printf("window pos: %d, sum: %f\n", a, window_sums[a]); |
| 1240 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1241 | printf("syn threads joint\n"); |
| 1242 | fflush(stdout); |
Marc Kupietz | 50485ba | 2016-03-23 09:13:14 +0100 | [diff] [blame] | 1243 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1244 | for(b=0; b < syn_nbs[0]->length; b++) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1245 | memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator)); |
| 1246 | best[b].position = -1; // syn_nbs[0]->pos[b]; |
| 1247 | best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi]; |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 1248 | best[b].max_activation = 0.0; |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1249 | best[b].average = 0.0; |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 1250 | best[b].probability = 0.0; |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1251 | best[b].cprobability = syn_nbs[0]->best[b].cprobability; |
Marc Kupietz | 7631e02 | 2019-01-25 12:04:42 +0100 | [diff] [blame] | 1252 | memset(best[b].heat, 0, sizeof(float)*16); |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1253 | } |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 1254 | |
| 1255 | float best_window_sum[MAX_NEIGHBOURS]; |
| 1256 | int found_index=0, i=0, j, w; |
Marc Kupietz | 580ebdf | 2017-11-29 21:18:38 +0100 | [diff] [blame] | 1257 | for(a=0; a < syn_threads; a++) { |
| 1258 | for(b=0; b < syn_nbs[a]->length; b++) { |
| 1259 | for(i=0; i < found_index; i++) |
| 1260 | if(best[i].wordi == syn_nbs[a]->best[b].wordi) |
| 1261 | break; |
| 1262 | if(i >= found_index) { |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 1263 | best[found_index].max_activation = 0.0; |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1264 | best[found_index].average = 0.0; |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 1265 | best[found_index].probability = 0.0; |
Marc Kupietz | 7631e02 | 2019-01-25 12:04:42 +0100 | [diff] [blame] | 1266 | memset(best[found_index].heat, 0, sizeof(float)*16); |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1267 | best[found_index].cprobability = syn_nbs[a]->best[b].cprobability; |
| 1268 | best[found_index].activation_sum = target_sums[syn_nbs[a]->best[b].wordi]; // syn_nbs[a]->best[b].activation_sum; |
Marc Kupietz | 580ebdf | 2017-11-29 21:18:38 +0100 | [diff] [blame] | 1269 | best[found_index++].wordi = syn_nbs[a]->best[b].wordi; |
| 1270 | // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]); |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 1271 | } |
| 1272 | } |
Marc Kupietz | 580ebdf | 2017-11-29 21:18:38 +0100 | [diff] [blame] | 1273 | } |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1274 | sort_by =0; // ALWAYS AUTO-FOCUS |
Marc Kupietz | 580ebdf | 2017-11-29 21:18:38 +0100 | [diff] [blame] | 1275 | if(sort_by != 1 && sort_by != 2) { // sort by auto focus mean |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 1276 | printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) -1); |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 1277 | int wpos; |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1278 | int bits_set = 0; |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 1279 | for(i=0; i < found_index; i++) { |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1280 | best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0; |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 1281 | for(w=1; w < (1 << syn_threads); w++) { // loop through all possible windows |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1282 | float word_window_sum = 0, word_window_average=0, word_cprobability_sum=0, word_activation_sum = 0, total_window_sum = 0; |
| 1283 | bits_set = 0; |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 1284 | for(a=0; a < syn_threads; a++) { |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 1285 | if((1 << a) & w) { |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 1286 | wpos = (a >= window? a+1 : a); |
| 1287 | total_window_sum += window_sums[wpos]; |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 1288 | } |
| 1289 | } |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 1290 | // printf("%d window-sum %f\n", w, total_window_sum); |
| 1291 | for(a=0; a < syn_threads; a++) { |
| 1292 | if((1 << a) & w) { |
| 1293 | wpos = (a >= window? a+1 : a); |
| 1294 | bits_set++; |
| 1295 | for(b=0; b < syn_nbs[a]->length; b++) |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1296 | if(best[i].wordi == syn_nbs[a]->best[b].wordi) { |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1297 | // float acti = syn_nbs[a]->best[b].activation / total_window_sum; |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 1298 | // word_window_sum += syn_nbs[a]->dist[b] * syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b]; |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 1299 | // word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b]; |
| 1300 | // word_window_sum = (word_window_sum + syn_nbs[a]->norm[b]) - (word_window_sum * syn_nbs[a]->norm[b]); // syn_nbs[a]->norm[b]; |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1301 | |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 1302 | word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b]; |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1303 | // word_window_sum += acti - (word_window_sum * acti); syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b]; |
| 1304 | |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1305 | word_window_average += syn_nbs[a]->best[b].activation; // - word_window_average * syn_nbs[a]->best[b].activation; // conormalied activation sum |
| 1306 | word_cprobability_sum += syn_nbs[a]->best[b].cprobability - word_cprobability_sum * syn_nbs[a]->best[b].cprobability; // conormalied column probability sum |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 1307 | word_activation_sum += syn_nbs[a]->best[b].activation; |
| 1308 | if(syn_nbs[a]->best[b].activation > best[i].max_activation) |
| 1309 | best[i].max_activation = syn_nbs[a]->best[b].activation; |
Marc Kupietz | db2dc7e | 2017-12-02 12:04:03 +0100 | [diff] [blame] | 1310 | if(syn_nbs[a]->best[b].activation > best[i].heat[wpos] ) |
Marc Kupietz | 728b8ed | 2017-12-02 11:13:49 +0100 | [diff] [blame] | 1311 | best[i].heat[wpos] = syn_nbs[a]->best[b].activation; |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 1312 | } |
| 1313 | } |
| 1314 | } |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1315 | if(bits_set) { |
| 1316 | word_window_average /= bits_set; |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 1317 | // word_activation_sum /= bits_set; |
| 1318 | // word_window_sum /= bits_set; |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1319 | } |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1320 | |
| 1321 | word_window_sum /= total_window_sum; |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 1322 | |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1323 | if(word_window_sum > best[i].probability) { |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1324 | // best[i].position = w; |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1325 | best[i].probability = word_window_sum; |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 1326 | } |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 1327 | |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1328 | if(word_cprobability_sum > best[i].cprobability_sum) { |
| 1329 | best[i].position = w; |
| 1330 | best[i].cprobability_sum = word_cprobability_sum; |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 1331 | } |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1332 | |
| 1333 | best[i].average = word_window_average; |
| 1334 | // best[i].activation = word_activation_sum; |
| 1335 | } |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 1336 | } |
Marc Kupietz | e0e4213 | 2017-11-24 16:44:34 +0100 | [diff] [blame] | 1337 | qsort(best, found_index, sizeof(collocator), cmp_probability); |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 1338 | // for(i=0; i < found_index; i++) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1339 | // printf("found: %s - sum: %f - window: %d\n", &vocab[best[i].wordi * max_w], best[i].activation, best[i].position); |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 1340 | // } |
| 1341 | |
Marc Kupietz | 580ebdf | 2017-11-29 21:18:38 +0100 | [diff] [blame] | 1342 | } else if(sort_by == 1) { // responsiveness any window position |
| 1343 | int wpos; |
| 1344 | for(i=0; i < found_index; i++) { |
| 1345 | float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0; |
| 1346 | for(a=0; a < syn_threads; a++) { |
| 1347 | wpos = (a >= window? a+1 : a); |
| 1348 | for(b=0; b < syn_nbs[a]->length; b++) |
| 1349 | if(best[i].wordi == syn_nbs[a]->best[b].wordi) { |
| 1350 | best[i].probability += syn_nbs[a]->best[b].probability; |
| 1351 | if(syn_nbs[a]->best[b].activation > 0.25) |
| 1352 | best[i].position |= 1 << wpos; |
| 1353 | if(syn_nbs[a]->best[b].activation > best[i].activation) { |
| 1354 | best[i].activation = syn_nbs[a]->best[b].activation; |
| 1355 | } |
| 1356 | } |
| 1357 | } |
| 1358 | } |
| 1359 | qsort(best, found_index, sizeof(collocator), cmp_activation); |
| 1360 | } else if(sort_by == 2) { // single window position |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 1361 | for(a=1; a < syn_threads; a++) { |
| 1362 | for(b=0; b < syn_nbs[a]->length; b++) { |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1363 | for(c=0; c < MAX_NEIGHBOURS; c++) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1364 | if(syn_nbs[a]->best[b].activation > best[c].activation) { |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1365 | for(d=MAX_NEIGHBOURS-1; d>c; d--) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1366 | memmove(best + d, best + d - 1, sizeof(collocator)); |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1367 | } |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1368 | memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator)); |
| 1369 | best[c].position = 1 << (-syn_nbs[a]->best[b].position+window - (syn_nbs[a]->best[b].position < 0 ? 1:0)); |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1370 | break; |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 1371 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1372 | } |
| 1373 | } |
| 1374 | } |
| 1375 | } else { // sort by mean p |
| 1376 | for(a=1; a < syn_threads; a++) { |
| 1377 | for(b=0; b < syn_nbs[a]->length; b++) { |
| 1378 | for(c=0; c < MAX_NEIGHBOURS; c++) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1379 | if(target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) { |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1380 | for(d=MAX_NEIGHBOURS-1; d>c; d--) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1381 | memmove(best + d, best + d - 1, sizeof(collocator)); |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1382 | } |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1383 | memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator)); |
| 1384 | best[c].position = (1 << 2*window) - 1; // syn_nbs[a]->pos[b]; |
| 1385 | best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi]; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1386 | break; |
| 1387 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 1388 | } |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 1389 | } |
| 1390 | } |
| 1391 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1392 | array = newAV(); |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1393 | for (a = 0, i=0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) { |
| 1394 | long long c = best[a].wordi; |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1395 | /* |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 1396 | if (dedupe) { |
| 1397 | int filtered=0; |
| 1398 | for (j=0; j<i; j++) |
| 1399 | if (strcasestr(&vocab[c * max_w], chosen[j]) || |
| 1400 | strcasestr(chosen[j], &vocab[c * max_w])) { |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1401 | printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]); |
| 1402 | filtered = 1; |
| 1403 | } |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 1404 | if(filtered) |
| 1405 | continue; |
| 1406 | } |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1407 | */ |
Marc Kupietz | b6c615d | 2017-12-02 10:38:20 +0100 | [diff] [blame] | 1408 | chosen[i++]=c; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1409 | HV* hash = newHV(); |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1410 | SV* word = newSVpvf(&vocab[best[a].wordi * max_w], 0); |
Marc Kupietz | b6c615d | 2017-12-02 10:38:20 +0100 | [diff] [blame] | 1411 | AV* heat = newAV(); |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1412 | if(latin_enc == 0) SvUTF8_on(word); |
| 1413 | hv_store(hash, "word", strlen("word"), word , 0); |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1414 | hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0); |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1415 | hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0); |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 1416 | hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0); |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1417 | hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0); |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 1418 | hv_store(hash, "max", strlen("max"), newSVnv(best[a].max_activation), 0); // newSVnv(target_sums[best[a].wordi]), 0); |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1419 | hv_store(hash, "overall", strlen("overall"), newSVnv(best[a].activation_sum/total_activation), 0); // newSVnv(target_sums[best[a].wordi]), 0); |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1420 | hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0); |
Marc Kupietz | b6c615d | 2017-12-02 10:38:20 +0100 | [diff] [blame] | 1421 | best[a].heat[5]=0; |
| 1422 | for(i=10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i])); |
| 1423 | hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV*)heat), 0); |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1424 | av_push(array, newRV_noinc((SV*)hash)); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 1425 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1426 | hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 1427 | } |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 1428 | end: |
Marc Kupietz | 257a93d | 2019-01-03 17:55:22 +0100 | [diff] [blame] | 1429 | // words = old_words; // why was this here? |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1430 | free(best); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 1431 | return newRV_noinc((SV*)result); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 1432 | } |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 1433 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1434 | int dump_vecs(char *fname) { |
| 1435 | long i, j; |
| 1436 | FILE *f; |
Marc Kupietz | 56844a2 | 2019-08-02 15:12:19 +0200 | [diff] [blame] | 1437 | /* if(words>100000) |
Marc Kupietz | 0a68a79 | 2018-12-20 11:41:45 +0100 | [diff] [blame] | 1438 | words=100000; |
Marc Kupietz | 56844a2 | 2019-08-02 15:12:19 +0200 | [diff] [blame] | 1439 | */ |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1440 | if((f=fopen(fname, "w")) == NULL) { |
| 1441 | fprintf(stderr, "cannot open %s for writing\n", fname); |
| 1442 | return(-1); |
| 1443 | } |
| 1444 | fprintf(f, "%lld %lld\n", words, size); |
| 1445 | for (i=0; i < words; i++) { |
| 1446 | fprintf(f, "%s ", &vocab[i * max_w]); |
| 1447 | for(j=0; j < size - 1; j++) |
| 1448 | fprintf(f, "%f ", M[i*size + j]); |
| 1449 | fprintf(f, "%f\n", M[i*size + j]); |
| 1450 | } |
| 1451 | fclose(f); |
| 1452 | return(0); |
| 1453 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 1454 | |
Marc Kupietz | fa19426 | 2018-06-05 09:39:32 +0200 | [diff] [blame] | 1455 | int dump_for_numpy(char *fname) { |
| 1456 | long i, j; |
| 1457 | FILE *f; |
Marc Kupietz | 56844a2 | 2019-08-02 15:12:19 +0200 | [diff] [blame] | 1458 | int max = 300000; |
Marc Kupietz | fa19426 | 2018-06-05 09:39:32 +0200 | [diff] [blame] | 1459 | |
| 1460 | if((f=fopen(fname, "w")) == NULL) { |
| 1461 | fprintf(stderr, "cannot open %s for writing\n", fname); |
| 1462 | return(-1); |
| 1463 | } |
Marc Kupietz | 1233eda | 2019-03-06 14:16:43 +0100 | [diff] [blame] | 1464 | for (i=0; i < max; i++) { |
Marc Kupietz | fa19426 | 2018-06-05 09:39:32 +0200 | [diff] [blame] | 1465 | for(j=0; j < size - 1; j++) |
| 1466 | fprintf(f, "%f\t", M[i*size + j]); |
| 1467 | fprintf(f, "%f\n", M[i*size + j]); |
Marc Kupietz | 56844a2 | 2019-08-02 15:12:19 +0200 | [diff] [blame] | 1468 | printf("%s\r\n", &vocab[i * max_w]); |
| 1469 | } |
| 1470 | if(merged_end > 0) { |
| 1471 | for (i=0; i < max; i++) { |
Marc Kupietz | 1233eda | 2019-03-06 14:16:43 +0100 | [diff] [blame] | 1472 | for(j=0; j < size - 1; j++) |
| 1473 | fprintf(f, "%f\t", M[(merged_end + i)*size + j]); |
| 1474 | fprintf(f, "%f\n", M[(merged_end + i)*size + j]); |
Marc Kupietz | 56844a2 | 2019-08-02 15:12:19 +0200 | [diff] [blame] | 1475 | printf("_%s\r\n", &vocab[i * max_w]); |
Marc Kupietz | 1233eda | 2019-03-06 14:16:43 +0100 | [diff] [blame] | 1476 | } |
Marc Kupietz | fa19426 | 2018-06-05 09:39:32 +0200 | [diff] [blame] | 1477 | } |
| 1478 | fclose(f); |
Marc Kupietz | 7631e02 | 2019-01-25 12:04:42 +0100 | [diff] [blame] | 1479 | return(0); |
Marc Kupietz | fa19426 | 2018-06-05 09:39:32 +0200 | [diff] [blame] | 1480 | } |