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