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