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