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