blob: 7295d4b0e9b1da2472f164f477e0a15f4748ff01 [file] [log] [blame]
Marc Kupietzdc22b982015-10-09 09:19:34 +02001#!/usr/local/bin/perl
2use Inline C;
Marc Kupietz00eecb12019-01-24 16:08:44 +01003use Inline C => Config => CLEAN_AFTER_BUILD => 0, BUILD_NOISY => 1, ccflags => $Config{ccflags}." -I/vol/work/kupietz/Work2/kl/trunk/CollocatorDB -I, -L. -Wall -O4", libs => "-shared -lpthread -lcollocatordb -lrt -lsnappy -lz -lbz2 -llz4 -lzstd -lrocksdb -lgomp";
Marc Kupietza5f60042017-05-04 10:38:12 +02004#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 Kupietzdc22b982015-10-09 09:19:34 +02006use Mojolicious::Lite;
Marc Kupietzc4893362016-02-25 08:04:46 +01007use Mojo::JSON qw(decode_json encode_json to_json);
Marc Kupietz30ca4342017-11-22 21:21:20 +01008use base 'Mojolicious::Plugin';
9
Marc Kupietz247500f2015-10-09 11:29:01 +020010use Encode qw(decode encode);
Marc Kupietza5b90152016-03-15 17:39:19 +010011use Getopt::Std;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010012use Mojo::Server::Daemon;
Marc Kupietzffef9302017-11-07 15:58:01 +010013use Cwd;
Marc Kupietz66bfd952017-12-11 09:59:45 +010014
Marc Kupietzffef9302017-11-07 15:58:01 +010015app->static->paths->[0] = getcwd;
16
Marc Kupietzd4227392016-03-01 16:45:12 +010017plugin 'Log::Access';
Marc Kupietzb3422c12017-07-04 14:12:11 +020018plugin "RequestBase";
Marc Kupietzdc22b982015-10-09 09:19:34 +020019
Marc Kupietza5b90152016-03-15 17:39:19 +010020our $opt_i = 0; # latin1-input?
21our $opt_l = undef;
22our $opt_p = 5676;
Marc Kupietza2e64502016-04-27 09:53:51 +020023our $opt_m;
Marc Kupietz6ed81872016-04-27 14:04:04 +020024our $opt_M;
Marc Kupietz43ee87e2016-04-25 10:50:08 +020025our $opt_n = '';
26our $opt_d;
Marc Kupietzfa194262018-06-05 09:39:32 +020027our $opt_D;
Marc Kupietz5c3887d2016-04-28 08:53:35 +020028our $opt_G;
Marc Kupietza5b90152016-03-15 17:39:19 +010029
Marc Kupietz6ed81872016-04-27 14:04:04 +020030my %marked;
Marc Kupietzc053d972019-01-10 10:41:51 +010031my $title="";
Marc Kupietz793413b2016-04-02 21:48:57 +020032my $training_args="";
Marc Kupietza2e64502016-04-27 09:53:51 +020033my $mergedEnd=0;
Marc Kupietz15987412017-11-07 15:56:58 +010034my %cache;
Marc Kupietz19c68242018-03-12 09:42:21 +010035my %cccache; # classic collocator cache
Marc Kupietza51dcfa2018-03-19 16:22:05 +010036my %spcache; # similar profile cache
Marc Kupietz793413b2016-04-02 21:48:57 +020037
Marc Kupietzfa194262018-06-05 09:39:32 +020038getopts('d:D:Gil:p:m:n:M:');
Marc Kupietz6ed81872016-04-27 14:04:04 +020039
40if($opt_M) {
Marc Kupietzed930212016-04-27 15:42:38 +020041 open my $handle, '<:encoding(UTF-8)', $opt_M
42 or die "Can't open '$opt_M' for reading: $!";
43 while(<$handle>) {
Marc Kupietz6ed81872016-04-27 14:04:04 +020044 foreach my $mw (split /\s+/) {
45 $marked{$mw}=1
46 }
47 }
Marc Kupietzed930212016-04-27 15:42:38 +020048 close($handle);
Marc Kupietz6ed81872016-04-27 14:04:04 +020049}
Marc Kupietza5b90152016-03-15 17:39:19 +010050
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010051# -cbow 1 -size 200 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 40 -binary 1 -iter 15
Marc Kupietza5b90152016-03-15 17:39:19 +010052if(!$ARGV[0]) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010053 init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0));
Marc Kupietz2cb667e2016-03-10 09:44:12 +010054} else {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010055 init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0));
Marc Kupietz793413b2016-04-02 21:48:57 +020056 if(open(FILE, "$ARGV[0].args")) {
57 $training_args = <FILE>;
58 }
59 close(FILE);
Marc Kupietzc053d972019-01-10 10:41:51 +010060 $title = fname2corpusname($ARGV[0]);
Marc Kupietz2cb667e2016-03-10 09:44:12 +010061}
Marc Kupietzdc22b982015-10-09 09:19:34 +020062
Marc Kupietza51dcfa2018-03-19 16:22:05 +010063my $have_sprofiles = load_sprofiles($ARGV[0]);
64
Marc Kupietza2e64502016-04-27 09:53:51 +020065if($opt_m) {
66 $mergedEnd = mergeVectors($opt_m);
Marc Kupietzc053d972019-01-10 10:41:51 +010067 $title = "<span class=\"merged\">" . $title . "</span> vs. " . fname2corpusname($opt_m);
Marc Kupietza2e64502016-04-27 09:53:51 +020068}
69
Marc Kupietze5568a02018-12-20 11:42:02 +010070
Marc Kupietz43ee87e2016-04-25 10:50:08 +020071if($opt_d) { # -d: dump vecs and exit
72 dump_vecs($opt_d);
73 exit;
74}
75
Marc Kupietzfa194262018-06-05 09:39:32 +020076if($opt_D) { # -D: dump vecs for numpy and exit
77 dump_for_numpy($opt_D);
78 exit;
79}
80
Marc Kupietza5b90152016-03-15 17:39:19 +010081my $daemon = Mojo::Server::Daemon->new(
82 app => app,
83 listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"]
84);
85
Marc Kupietz5c3887d2016-04-28 08:53:35 +020086if($opt_G) {
87 print "Filtering garbage\n";
88 filter_garbage();
89}
90
Marc Kupietz554aff52017-11-09 14:42:09 +010091get '*/js/*' => sub {
Marc Kupietzffef9302017-11-07 15:58:01 +010092 my $c = shift;
93 my $url = $c->req->url;
94 $url =~ s@/derekovecs@@g;
95 $c->app->log->info("GET: " . $url);
96 $c->reply->static($url);
97};
98
Marc Kupietza9270572018-03-17 15:17:07 +010099get '*/css/*' => sub {
100 my $c = shift;
101 my $url = $c->req->url;
102 $url =~ s@/derekovecs/@/@g;
103 $c->app->log->info("GET: " . $url);
104 $c->reply->static($url);
105};
106
Marc Kupietzc053d972019-01-10 10:41:51 +0100107sub fname2corpusname {
108 ($_) = @_;
109 s@.*/@@;
Marc Kupietz86b50292019-02-17 21:03:59 +0100110 s@\.en@-en@;
Marc Kupietzc053d972019-01-10 10:41:51 +0100111 s@\..*@@;
112 return $_;
113}
114
Marc Kupietz19c68242018-03-12 09:42:21 +0100115sub getClassicCollocatorsCached {
116 my ($c, $word) = @_;
Marc Kupietz81aeed22019-02-17 21:22:45 +0100117 my $s2 = "";
Marc Kupietz9ff3c992019-02-04 12:32:54 +0100118 if($word > $mergedEnd) {
119 $word-=$mergedEnd;
120 }
Marc Kupietz81aeed22019-02-17 21:22:45 +0100121
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100122 if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference
Marc Kupietz9ee3f412019-08-02 14:58:19 +0200123 open PIPE, "GET http://compute:5673/getClassicCollocators?w=$word |" or
124 open PIPE, "GET http://klinux10:5673/getClassicCollocators?w=$word |";
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100125 }
Marc Kupietz19c68242018-03-12 09:42:21 +0100126 if(!$cccache{$word}) {
Marc Kupietz06d61292019-02-04 12:33:22 +0100127 $c->app->log->info("Getting classic collocates of $word.");
Marc Kupietz19c68242018-03-12 09:42:21 +0100128 $cccache{$word} = getClassicCollocators($word);
Marc Kupietz1d96a082019-02-18 09:29:06 +0100129 $cccache{$word} =~ s/:(-?)(nan|inf)/:"${1}${2}"/g;
Marc Kupietz19c68242018-03-12 09:42:21 +0100130 } else {
Marc Kupietz06d61292019-02-04 12:33:22 +0100131 $c->app->log->info("Getting classic collocates for $word from cache.");
Marc Kupietz19c68242018-03-12 09:42:21 +0100132 }
Marc Kupietz81aeed22019-02-17 21:22:45 +0100133 if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100134 while(<PIPE>) {
135 $s2 .= $_;
136 }
137 close(PIPE);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100138 }
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100139
Marc Kupietz81aeed22019-02-17 21:22:45 +0100140 if(length($s2) > 2000) {
141 my $d1 = decode_json($cccache{$word});
142 my $d2 = decode_json($s2);
143 my %d2ld;
Marc Kupietz001bffd2019-02-21 08:52:41 +0100144 my $minLd = 14;
Marc Kupietz81aeed22019-02-17 21:22:45 +0100145 foreach my $i (@{$d2->{collocates}}) {
146 $d2ld{$i->{word}}=$i->{ld};
Marc Kupietz001bffd2019-02-21 08:52:41 +0100147 $minLd=$i->{ld} if($i->{ld} < $minLd);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100148 }
149 foreach my $i (@{$d1->{collocates}}) {
150 my $w = $i->{word};
Marc Kupietz001bffd2019-02-21 08:52:41 +0100151 $i->{delta} = $i->{ld} - (defined $d2ld{$w} ? $d2ld{$w} : $minLd-0.1);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100152 }
153 return(encode_json($d1));
154 } else {
155 my $d1 = decode_json($cccache{$word});
156 foreach my $i (@{$d1->{collocates}}) {
157 $i->{delta} = 0;
158 }
159 return(encode_json($d1));
160 }
Marc Kupietz19c68242018-03-12 09:42:21 +0100161}
162
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100163sub getSimilarProfilesCached {
164 my ($c, $word) = @_;
165 if(!$spcache{$word}) {
166 $spcache{$word} = getSimilarProfiles($word);
167 } else {
168 $c->app->log->info("Getting similar profiles for $word from cache:");
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100169 }
170 return $spcache{$word};
171}
172
Marc Kupietz66bfd952017-12-11 09:59:45 +0100173post '/derekovecs/getVecsByRanks' => sub {
174 my $self = shift;
175 my $vec = getVecs($self->req->json);
176 $self->render(json => $vec);
177};
178
Marc Kupietze13a3552018-01-25 08:48:34 +0100179any '*/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100180 my $self = shift;
Marc Kupietz19c68242018-03-12 09:42:21 +0100181 $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100182};
183
Marc Kupietze13a3552018-01-25 08:48:34 +0100184any '/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100185 my $self = shift;
Marc Kupietz19c68242018-03-12 09:42:21 +0100186 $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100187};
188
Marc Kupietzd7760b42019-02-21 09:01:44 +0100189any '/getBiggestVocabDistances' => sub {
190 my $self = shift;
191 $self->render(data => getBiggestMergedDifferences(), format=>'json');
192};
193
194any '*/getBiggestVocabDistances' => sub {
195 my $self = shift;
196 $self->render(data => getBiggestMergedDifferences(), format=>'json');
197};
198
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100199any '*/getSimilarProfiles' => sub {
200 my $self = shift;
201 $self->render(data => getSimilarProfilesCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
202};
203
Marc Kupietzc987fa82018-03-21 12:14:25 +0100204any '/getSimilarProfiles' => sub {
205 my $self = shift;
206 $self->render(data => getSimilarProfilesCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
207};
208
Marc Kupietz98ed1c02019-08-02 15:05:37 +0200209any '/getSimilarity' => sub {
210 my $self = shift;
211 my $w1 = $self->param("w1");
212 my $w2 = $self->param("w2");
213 $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json');
214};
215
216any '*/getSimilarity' => sub {
217 my $self = shift;
218 my $w1 = $self->param("w1");
219 my $w2 = $self->param("w2");
220 $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json');
221};
222
Marc Kupietzdf3d4b52017-11-29 16:57:27 +0100223get '*/img/*' => sub {
224 my $c = shift;
225 my $url = $c->req->url;
226 $url =~ s@/derekovecs@@g;
227 $c->app->log->info("GET: " . $url);
228 $c->reply->static($url);
229};
230
Marc Kupietzdc22b982015-10-09 09:19:34 +0200231get '/' => sub {
232 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +0200233 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200234 my $word=$c->param('word');
Marc Kupietz2da2a812019-02-21 14:17:35 +0100235 my $no_nbs=$c->param('n') || ($opt_m? 50 : 100);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100236 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +0100237 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100238 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100239 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200240 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100241 my $sort=$c->param('sort') || 0;
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100242 my $csv=$c->param('csv') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +0200243 my $json=$c->param('json') || 0;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100244 my $cutoff=$c->param('cutoff') || 500000;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100245 my $dedupe=$c->param('dedupe') || 0;
Marc Kupietzac707b32018-12-20 11:36:38 +0100246 my $nosp=$c->param('nosp') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100247 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100248 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100249 my @collocations;
Marc Kupietzd897d5d2019-08-02 15:09:07 +0200250 $word="idea" if(!defined($word));
251 if(defined($word) && $word !~ /^\s*$/) {
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100252 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100253 $word =~ s/\s+/ /g;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100254 if($opt_m && $word !~ /\|/) {
255 $word .= "|$word";
256 }
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100257 for my $w (split(' *\| *', $word)) {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100258 if($opt_m) {
259 if($searchBaseVocabFirst) {
260 $searchBaseVocabFirst=0;
261 } else {
262 $searchBaseVocabFirst=1;
263 }
264 }
265 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe,$searchBaseVocabFirst}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100266 $c->app->log->info("Getting $w results from cache");
Marc Kupietz3082fd02019-01-09 14:54:06 +0100267 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe.$searchBaseVocabFirst}
Marc Kupietza5b90152016-03-15 17:39:19 +0100268 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100269 $c->app->log->info('Looking for neighbours of '.$w);
270 if($opt_i) {
Marc Kupietzac707b32018-12-20 11:36:38 +0100271 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100272 } else {
Marc Kupietzac707b32018-12-20 11:36:38 +0100273 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100274 }
Marc Kupietz2dd2dd72017-12-01 22:08:14 +0100275 $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100276 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100277 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100278 }
279 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100280 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200281 if($json) {
282 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100283 } elsif($csv) {
284 my $csv_data="";
285 for (my $i=0; $i <= $no_nbs; $i++) {
286 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
287 }
288 for (my $i=0; $i < $no_nbs; $i++) {
289 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
290 }
291 chop $csv_data;
292 chop $csv_data;
293 $csv_data .= "\n";
294 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200295 } else {
Marc Kupietzd7760b42019-02-21 09:01:44 +0100296 my $distantWords="";
297 if(!defined($word) || $word !~ /^\s*$/) {
298 $distantWords = getBiggestMergedDifferences();
299 }
300 $c->render(template=>"index", title=>$title, word=>$word, distantWords=>$distantWords, cutoff=>$cutoff, no_nbs=>$no_nbs, no_iterations => $no_iterations, epsilon=> $epsilon, perplexity=> $perplexity, show_som=>$som, searchBaseVocabFirst=>$searchBaseVocabFirst, sort=>$sort, training_args=>$training_args, mergedEnd=> $mergedEnd, haveSProfiles=> $have_sprofiles, dedupe=> $dedupe, marked=>\%marked, lists=> \@lists, collocators=> $res->{syntagmatic});
Marc Kupietzb613b052016-04-28 14:11:59 +0200301 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200302};
303
Marc Kupietz30ca4342017-11-22 21:21:20 +0100304helper(bitvec2window => sub {
305 my ($self, $n) = @_;
306 my $str = unpack("B32", pack("N", $n));
307 $str =~ s/^\d{22}//;
308 $str =~ s/^(\d{5})/$1x/;
309 $str =~ s/0/·/g;
310 $str =~ s/1/+/g;
311 return $str;
312 });
313
Marc Kupietza5b90152016-03-15 17:39:19 +0100314$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200315
316exit;
317
318__END__
319
320__C__
321#include <stdio.h>
322#include <string.h>
323#include <math.h>
324#include <malloc.h>
325#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +0100326#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +0100327#include <pthread.h>
Marc Kupietze243efd2018-01-11 22:19:24 +0100328#include <collocatordb.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200329
330#define max_size 2000
331#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100332#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100333#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +0100334#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100335#define MAX_CC 50
336#define EXP_TABLE_SIZE 1000
337#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +0100338#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +0200339
340//the thread function
341void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +0100342
343typedef struct {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100344 long long wordi;
345 long position;
346 float activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100347 float average;
Marc Kupietza77acce2017-11-30 16:59:07 +0100348 float cprobability; // column wise probability
Marc Kupietz4116b432017-12-06 14:15:32 +0100349 float cprobability_sum;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100350 float probability;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100351 float activation_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100352 float max_activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100353 float heat[16];
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100354} collocator;
355
356typedef struct {
357 collocator *best;
Marc Kupietz80abb442016-03-23 21:04:08 +0100358 int length;
Marc Kupietz000ad862016-02-26 14:59:12 +0100359} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100360
Marc Kupietz000ad862016-02-26 14:59:12 +0100361typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +0100362 long long wordi[MAX_NEIGHBOURS];
363 char sep[MAX_NEIGHBOURS];
364 int length;
365} wordlist;
366
367typedef struct {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100368 long cutoff;
Marc Kupietz48c29682016-03-19 11:30:43 +0100369 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100370 char *token;
371 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100372 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100373 unsigned long upto;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100374 collocator *best;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100375 float *target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100376 float *window_sums;
Marc Kupietz3c0fab12019-08-02 15:07:47 +0200377 float threshold;
Marc Kupietz000ad862016-02-26 14:59:12 +0100378} knnpars;
379
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100380typedef struct {
381 uint32_t index;
382 float value;
383} sparse_t;
384
385typedef struct {
386 uint32_t len;
387 sparse_t nbr[100];
388} profile_t;
389
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200390float *M, *M2=0L, *syn1neg_window, *expTable;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100391float *window_sums;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200392char *vocab;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200393char *garbage = NULL;
Marc Kupietze243efd2018-01-11 22:19:24 +0100394COLLOCATORDB *cdb = NULL;
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100395profile_t *sprofiles = NULL;
396size_t sprofiles_qty = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100397
Marc Kupietza2e64502016-04-27 09:53:51 +0200398long long words, size, merged_end;
Marc Kupietza5f60042017-05-04 10:38:12 +0200399long long merge_words = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100400int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100401int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100402int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200403
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100404/* load collocation profiles if file exists */
405int load_sprofiles(char *vecsname) {
406 char *basename = strdup(vecsname);
407 char *pos = strstr(basename, ".vecs");
408 if(pos)
409 *pos=0;
410
411 char binsprofiles_fname[256];
412 strcpy(binsprofiles_fname, basename);
413 strcat(binsprofiles_fname, ".sprofiles.bin");
414 FILE *fp = fopen(binsprofiles_fname, "rb");
415 if (fp == NULL) {
416 printf("Collocation profiles %s not found. No problem.\n", binsprofiles_fname);
417 return 0;
418 }
419 fseek(fp, 0L, SEEK_END);
420 size_t sz = ftell(fp);
421 fclose(fp);
422
423 int fd = open(binsprofiles_fname, O_RDONLY);
424 sprofiles = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0);
425 if (sprofiles == MAP_FAILED) {
426 close(fd);
427 fprintf(stderr, "Cannot mmap %s\n", binsprofiles_fname);
428 sprofiles = NULL;
429 return 0;
430 } else {
431 sprofiles_qty = sz / sizeof(profile_t);
432 fprintf(stderr, "Successfully mmaped %s containing similar profiles for %ld word forms.\n", binsprofiles_fname, sprofiles_qty);
433 }
434 return 1;
435}
436
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100437int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100438 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100439 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100440 long long a, b, c, d, cn;
441 float len;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200442 double val;
Marc Kupietz82b02672016-02-26 12:32:25 +0100443
Marc Kupietz67c20282016-02-26 09:42:00 +0100444 char binvecs_fname[256], binwords_fname[256];
445 strcpy(binwords_fname, file_name);
446 strcat(binwords_fname, ".words");
447 strcpy(binvecs_fname, file_name);
448 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200449
Marc Kupietza5b90152016-03-15 17:39:19 +0100450 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200451 f = fopen(file_name, "rb");
452 if (f == NULL) {
453 printf("Input file %s not found\n", file_name);
454 return -1;
455 }
456 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100457 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200458 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100459 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
460 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100461 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
462 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
463 if (M == NULL) {
464 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
465 return -1;
466 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200467 if(strstr(file_name, ".txt")) {
468 for (b = 0; b < words; b++) {
469 a = 0;
470 while (1) {
471 vocab[b * max_w + a] = fgetc(f);
472 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
473 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
474 }
475 vocab[b * max_w + a] = 0;
476 len = 0;
477 for (a = 0; a < size; a++) {
478 fscanf(f, "%lf", &val);
479 M[a + b * size] = val;
480 len += val * val;
481 }
482 len = sqrt(len);
483 for (a = 0; a < size; a++) M[a + b * size] /= len;
484 }
485 } else {
486 for (b = 0; b < words; b++) {
487 a = 0;
488 while (1) {
489 vocab[b * max_w + a] = fgetc(f);
490 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
491 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
492 }
493 vocab[b * max_w + a] = 0;
494 fread(&M[b * size], sizeof(float), size, f);
495 len = 0;
496 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
497 len = sqrt(len);
498 for (a = 0; a < size; a++) M[a + b * size] /= len;
499 }
500 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100501 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
502 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
503 fclose(binvecs);
504 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
505 fclose(binwords);
506 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100507 }
508 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
509 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
510 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
511 if (M == MAP_FAILED || vocab == MAP_FAILED) {
512 close(binvecs_fd);
513 close(binwords_fd);
514 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
515 exit(-1);
516 }
517 } else {
518 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
519 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100520 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200521 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100522
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200523 if(net_name && strlen(net_name) > 0) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100524 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
525 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
526 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100527 // munmap(M, sizeof(float) * words * size);
528 M2 = mmap(0, sizeof(float) * words * size + sizeof(float) * 2 * window * size * words, PROT_READ, MAP_SHARED, net_fd, 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200529 if (M2 == MAP_FAILED) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100530 close(net_fd);
531 fprintf(stderr, "Cannot mmap %s\n", net_name);
532 exit(-1);
533 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100534 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100535 } else {
536 fprintf(stderr, "Cannot open %s\n", net_name);
537 exit(-1);
538 }
539 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
Marc Kupietze243efd2018-01-11 22:19:24 +0100540
541 char collocatordb_name[2048];
542 strcpy(collocatordb_name, net_name);
543 char *ext = rindex(collocatordb_name, '.');
544 if(ext) {
545 strcpy(ext, ".rocksdb");
546 if(access(collocatordb_name, R_OK) == 0) {
547 *ext = 0;
548 fprintf(stderr, "Opening collocator DB %s\n", collocatordb_name);
549 cdb = open_collocatordb(collocatordb_name);
550 }
551 }
552 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100553
554 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
555 for (i = 0; i < EXP_TABLE_SIZE; i++) {
556 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
557 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
558 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100559 window_sums = malloc(sizeof(float) * (window+1) * 2);
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100560
561 return 0;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200562}
563
Marc Kupietza2e64502016-04-27 09:53:51 +0200564long mergeVectors(char *file_name){
565 FILE *f, *binvecs, *binwords;
566 int binwords_fd, binvecs_fd, net_fd, i;
567 long long a, b, c, d, cn;
568 float len;
569 float *merge_vecs;
570 char *merge_vocab;
Marc Kupietza5f60042017-05-04 10:38:12 +0200571 /* long long merge_words, merge_size; */
572 long long merge_size;
Marc Kupietza2e64502016-04-27 09:53:51 +0200573
574 char binvecs_fname[256], binwords_fname[256];
575 strcpy(binwords_fname, file_name);
576 strcat(binwords_fname, ".words");
577 strcpy(binvecs_fname, file_name);
578 strcat(binvecs_fname, ".vecs");
579
580 f = fopen(file_name, "rb");
581 if (f == NULL) {
582 printf("Input file %s not found\n", file_name);
583 exit -1;
584 }
585 fscanf(f, "%lld", &merge_words);
586 fscanf(f, "%lld", &merge_size);
587 if(merge_size != size){
588 fprintf(stderr, "vectors must have the same length\n");
589 exit(-1);
590 }
591 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
592 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
593 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
594 if (merge_vecs == NULL || merge_vocab == NULL) {
595 close(binvecs_fd);
596 close(binwords_fd);
597 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
598 exit(-1);
599 }
600 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
601 read(binwords_fd, merge_vocab, merge_words * max_w);
602 } else {
603 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
604 exit(-1);
605 }
606 printf("Successfully reallocated memory\nMerging...\n");
607 fflush(stdout);
608 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
609 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
610 munmap(M, words * size * sizeof(float));
611 munmap(vocab, words * max_w);
612 M = merge_vecs;
613 vocab = merge_vocab;
614 merged_end = merge_words;
615 words += merge_words;
616 fclose(f);
617 printf("merged_end: %lld, words: %lld\n", merged_end, words);
Marc Kupietzd7760b42019-02-21 09:01:44 +0100618 //printBiggestMergedDifferences();
Marc Kupietza2e64502016-04-27 09:53:51 +0200619 return((long) merged_end);
620}
621
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200622void filter_garbage() {
623 long i;
Marc Kupietzab591a82016-04-28 14:08:49 +0200624 unsigned char *w, previous, c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200625 garbage = malloc(words);
626 memset(garbage, 0, words);
627 for (i = 0; i < words; i++) {
628 w = vocab + i * max_w;
629 previous = 0;
Marc Kupietz969adfa2018-03-12 09:43:53 +0100630 if(strncmp("quot", w, 4) == 0) {
631 garbage[i]=1;
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100632// printf("Gargabe: %s\n", vocab + i * max_w);
Marc Kupietz969adfa2018-03-12 09:43:53 +0100633 } else {
634 while((c = *w++) && !garbage[i]) {
635 if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
636 (previous == '-' && (c & 32)) ||
637 (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) ||
638 (previous == 'q' && c == 'u' && *(w) == 'o' && *(w+1) == 't') || /* quot */
639 c == '<'
640 ) {
641 garbage[i]=1;
642 continue;
643 }
644 previous = c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200645 }
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200646 }
647 }
648 return;
649}
650
Marc Kupietzac707b32018-12-20 11:36:38 +0100651
652knn *simpleGetCollocators(int word, int number, long cutoff, int *result) {
653 knnpars *pars = calloc(sizeof(knnpars), 1);
654 float *target_sums;
655 float *window_sums = malloc(sizeof(float) * (window+1) * 2);
656 pars->cutoff = (cutoff? cutoff : 300000);
657 long a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
658 for(a = 0; a < cutoff; a++)
659 target_sums[a] = 0;
660 pars->target_sums = target_sums;
661 pars->window_sums = window_sums;
662 pars->N = (number? number : 20);
663 pars->from = 0;
664 pars->upto = window * 2 -1;
Marc Kupietz7631e022019-01-25 12:04:42 +0100665 knn *syn_nbs = NULL; // = (knn*) getCollocators(pars);
Marc Kupietzac707b32018-12-20 11:36:38 +0100666 free(pars);
667 free(window_sums);
668 free(target_sums);
669 return syn_nbs;
670}
671
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100672void *getCollocators(void *args) {
673 knnpars *pars = args;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100674 int N = pars->N;
Marc Kupietz52702832019-01-15 17:55:52 +0100675
Marc Kupietz271e2a42016-03-22 11:37:43 +0100676 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100677 knn *nbs = NULL;
678 long window_layer_size = size * window * 2;
679 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
680 float f, max_f, maxmax_f;
Marc Kupietz7631e022019-01-25 12:04:42 +0100681 float *target_sums=NULL, worstbest, wpos_sum;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100682 collocator *best;
Marc Kupietzd5642582016-03-19 22:23:13 +0100683
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200684 if(M2 == NULL || cc == -1)
Marc Kupietzd5642582016-03-19 22:23:13 +0100685 return NULL;
686
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100687 a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietz7631e022019-01-25 12:04:42 +0100688 memset(target_sums, 0, pars->cutoff * sizeof(float));
689 best = malloc((N>200?N:200) * sizeof(collocator));
690 memset(best, 0, (N>200?N:200) * sizeof(collocator));
Marc Kupietz3c0fab12019-08-02 15:07:47 +0200691 worstbest = pars->threshold;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100692
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100693 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100694 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100695 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100696 best[b].wordi = -1;
697 best[b].probability = 1;
698 best[b].activation = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100699 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100700
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100701 d = cc;
702 maxmax_f = -1;
703 maxmax_target = 0;
704
Marc Kupietz271e2a42016-03-22 11:37:43 +0100705 for (a = pars->from; a < pars->upto; a++) {
706 if(a >= window)
707 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100708 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100709 printf("window pos: %ld\n", a);
710 if (a != window) {
711 max_f = -1;
712 window_offset = a * size;
713 if (a > window)
714 window_offset -= size;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100715 for(target = 0; target < pars->cutoff; target ++) {
716 if(garbage && garbage[target]) continue;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100717 if(target == d)
718 continue;
719 f = 0;
720 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100721 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100722 if (f < -MAX_EXP)
723 continue;
724 else if (f > MAX_EXP)
725 continue;
726 else
727 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100728 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100729
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100730 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100731 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100732 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100733 if (f > best[b].activation) {
734 memmove(best + b + 1, best + b, (N - b -1) * sizeof(collocator));
735 best[b].activation = f;
736 best[b].wordi = target;
737 best[b].position = window-a;
Marc Kupietz33679a32016-03-22 08:49:39 +0100738 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100739 }
740 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100741 if(b == N - 1)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100742 worstbest = best[N-1].activation;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100743 }
744 }
745 printf("%d %.2f\n", max_target, max_f);
746 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
747 if(max_f > maxmax_f) {
748 maxmax_f = max_f;
749 maxmax_target = max_target;
750 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100751 for (b = 0; b < N; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100752 if(best[b].position == window-a)
Marc Kupietza77acce2017-11-30 16:59:07 +0100753 best[b].cprobability = best[b].activation / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100754 } else {
755 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
756 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100757 pars->window_sums[a] = wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100758 }
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100759 for (b = 0; b < pars->cutoff; b++)
Marc Kupietza77acce2017-11-30 16:59:07 +0100760 pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2);
Marc Kupietzd89431f2019-08-02 15:06:38 +0200761
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100762 free(target_sums);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100763 for(b=0; b<N && best[b].wordi >= 0; b++);; // THIS LOOP IS NEEDED (b...)
764// printf("%d: best syn: %s %.2f %.5f\n", b, &vocab[best[b].wordi*max_w], best[b].activation, best[b].probability);
Marc Kupietzbd41da32017-11-24 10:26:43 +0100765// printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100766 nbs = malloc(sizeof(knn));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100767 nbs->best = best;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100768 nbs->length = b-1;
769 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100770}
771
Marc Kupietz30ca4342017-11-22 21:21:20 +0100772
Marc Kupietz66bfd952017-12-11 09:59:45 +0100773AV *getVecs(AV *array) {
774 int i, b;
775 AV *result = newAV();
776 for (i=0; i<=av_len(array); i++) {
777 SV** elem = av_fetch(array, i, 0);
778 if (elem != NULL) {
779 long j = (long) SvNV(*elem);
780 AV *vector = newAV();
781 for (b = 0; b < size; b++) {
782 av_push(vector, newSVnv(M[b + j * size]));
783 }
784 av_push(result, newRV_noinc(vector));
785 }
786 }
787 return result;
788}
789
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100790char *getSimilarProfiles(long node) {
791 int i;
792 char buffer[120000];
793 char pair_buffer[2048];
794 buffer[0]='[';
795 buffer[1]=0;
796 if(node >= sprofiles_qty) {
797 printf("Not available in precomputed profile\n");
798 return(strdup("[{\"w\":\"not available\", \"v\":0}]\n"));
799 }
800
801 printf("******* %s ******\n", &vocab[max_w * node]);
802
803 for(i=0; i < 100 && i < sprofiles[node].len; i++) {
804 sprintf(pair_buffer, "{\"w\":\"%s\", \"v\":%f},", &vocab[max_w * (sprofiles[node].nbr[i].index)], sprofiles[node].nbr[i].value);
805 strcat(buffer, pair_buffer);
806 }
807 buffer[strlen(buffer)-1]=']';
808 strcat(buffer, "\n");
809 printf(buffer);
810 return(strdup(buffer));
811}
812
Marc Kupietze243efd2018-01-11 22:19:24 +0100813char *getClassicCollocators(long node) {
814 char *res = (cdb? strdup(get_collocators_as_json(cdb, node)) : "[]");
815 return res;
816}
817
Marc Kupietz0957d532019-01-03 17:54:00 +0100818wordlist *getTargetWords(char *st1, int search_backw) {
Marc Kupietz48c29682016-03-19 11:30:43 +0100819 wordlist *wl = malloc(sizeof(wordlist));
820 char st[100][max_size], sep[100];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100821 long a, b=0, c=0, cn=0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200822 int unmerged;
823
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100824 while (1) {
825 st[cn][b] = st1[c];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100826 b++;
827 c++;
828 st[cn][b] = 0;
829 if (st1[c] == 0) break;
830 if (st1[c] == ' ' || st1[c] == '-') {
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100831 sep[cn++] = st1[c];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100832 b = 0;
833 c++;
834 }
835 }
836 cn++;
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100837 for (a = 0; a < cn; a++) {
838 if (search_backw) {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100839 for (b = words - 1; b >= (merge_words? merge_words : 0) && strcmp(&vocab[b * max_w], st[a]) !=0; b--);
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100840 } else {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100841 for (b = 0; b < (merge_words? merge_words : words) && strcmp(&vocab[b * max_w], st[a]) != 0; b++);
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100842 }
843 if (b == words) b = -1;
844 wl->wordi[a] = b;
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100845 if (b == -1) {
846 fprintf(stderr, "Out of dictionary word!\n");
847 cn--;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100848 } else {
849 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", &vocab[wl->wordi[a]*max_w], wl->wordi[a]);
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100850 }
851 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100852 wl->length=cn;
853 return(wl);
854}
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100855
Marc Kupietz98ed1c02019-08-02 15:05:37 +0200856float get_distance(long b, long c) {
857 long a;
858 float dist = 0;
859 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + b * size];
860 return dist;
861}
862
Marc Kupietzd7760b42019-02-21 09:01:44 +0100863char *getBiggestMergedDifferences() {
864 static char *result = NULL;
Marc Kupietzebc131e2019-01-03 16:24:27 +0100865 float dist, len, vec[max_size];
866 long long a, b, c, d, cn, *bi;
867 char ch;
868 knn *nbs = NULL;
Marc Kupietzd7760b42019-02-21 09:01:44 +0100869 int N = 1000;
870
Marc Kupietzb6514dc2019-06-26 11:47:56 +0200871 if(merged_end == 0)
872 result = "[]";
873
Marc Kupietzd7760b42019-02-21 09:01:44 +0100874 if(result != NULL)
875 return result;
Marc Kupietzebc131e2019-01-03 16:24:27 +0100876
877 printf("Looking for biggest distances between main and merged vectors ...\n");
878 collocator *best;
879 best = malloc(N * sizeof(collocator));
880 memset(best, 0, N * sizeof(collocator));
881
882 float worstbest=1000000;
883
884 for (a = 0; a < N; a++) best[a].activation = worstbest;
885
Marc Kupietz2da2a812019-02-21 14:17:35 +0100886 for (c = 0; c < 500000; c++) {
Marc Kupietzebc131e2019-01-03 16:24:27 +0100887 if(garbage && garbage[c]) continue;
888 a = 0;
889 dist = 0;
890 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + (c+merged_end) * size];
891 if(dist < worstbest) {
892 for (a = 0; a < N; a++) {
893 if (dist < best[a].activation) {
894 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
895 best[a].activation = dist;
896 best[a].wordi = c;
897 break;
898 }
899 }
900 worstbest = best[N-1].activation;
901 }
902 }
903
Marc Kupietzd7760b42019-02-21 09:01:44 +0100904 result = malloc(N*max_w);
905 char *p = result;
906 *p++ = '['; *p = 0;
Marc Kupietzebc131e2019-01-03 16:24:27 +0100907 for (a = 0; a < N; a++) {
Marc Kupietzd7760b42019-02-21 09:01:44 +0100908 p += sprintf(p, "{\"rank\":%d,\"word\":\"%s\",\"dist\":%.3f},", a, &vocab[best[a].wordi * max_w], 1-best[a].activation);
Marc Kupietzebc131e2019-01-03 16:24:27 +0100909 }
Marc Kupietzd7760b42019-02-21 09:01:44 +0100910 *--p = ']';
911 return(result);
Marc Kupietzebc131e2019-01-03 16:24:27 +0100912}
913
Marc Kupietz98ed1c02019-08-02 15:05:37 +0200914
915float cos_similarity(long b, long c) {
916 float dist=0;
917 long a;
918 for (a = 0; a < size; a++) dist += M[b * size + a] * M[c * size + a];
919 return dist;
920}
921
922char *cos_similarity_as_json(char *w1, char *w2) {
923 wordlist *a, *b;
924 float res;
925 a = getTargetWords(w1, 0);
926 b = getTargetWords(w2, 0);
927 if (a == NULL || b==NULL || a->length != 1 || b->length != 1)
928 res = -1;
929 else
930 res = cos_similarity(a->wordi[0], b->wordi[0]);
931 fprintf(stderr, "a: %lld b: %lld res:%f\n", a->wordi[0], b->wordi[0], res);
932 char *json = malloc(16);
933 sprintf(json, "%.5f", res);
934 return json;
935}
936
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100937void *_get_neighbours(void *arg) {
938 knnpars *pars = arg;
Marc Kupietz48c29682016-03-19 11:30:43 +0100939 char *st1 = pars->token;
940 int N = pars->N;
941 long from = pars -> from;
942 unsigned long upto = pars -> upto;
943 char file_name[max_size], st[100][max_size], *sep;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100944 float dist, len, vec[max_size];
945 long long a, b, c, d, cn, *bi;
Marc Kupietz48c29682016-03-19 11:30:43 +0100946 char ch;
947 knn *nbs = NULL;
948 wordlist *wl = pars->wl;
949
Marc Kupietzc55c8872017-12-01 23:27:10 +0100950 collocator *best = pars->best;
Marc Kupietz48c29682016-03-19 11:30:43 +0100951
952 float worstbest=-1;
953
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100954 for (a = 0; a < N; a++) best[a].activation = 0;
Marc Kupietz48c29682016-03-19 11:30:43 +0100955 a = 0;
956 bi = wl->wordi;
957 cn = wl->length;
958 sep = wl->sep;
959 b = bi[0];
960 c = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100961 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100962 N = 0;
963 goto end;
964 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200965 for (a = 0; a < size; a++) vec[a] = 0;
966 for (b = 0; b < cn; b++) {
967 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100968 if(b>0 && sep[b-1] == '-')
969 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
970 else
971 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200972 }
973 len = 0;
974 for (a = 0; a < size; a++) len += vec[a] * vec[a];
975 len = sqrt(len);
976 for (a = 0; a < size; a++) vec[a] /= len;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100977 for (a = 0; a < N; a++) best[a].activation = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100978 for (c = from; c < upto; c++) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200979 if(garbage && garbage[c]) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200980 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100981// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100982// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
983// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200984 dist = 0;
985 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100986 if(dist > worstbest) {
987 for (a = 0; a < N; a++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100988 if (dist > best[a].activation) {
989 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
990 best[a].activation = dist;
991 best[a].wordi = c;
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100992 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200993 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200994 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100995 worstbest = best[N-1].activation;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200996 }
997 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100998
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100999end:
Marc Kupietz000ad862016-02-26 14:59:12 +01001000 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +02001001}
1002
Marc Kupietze0e42132017-11-24 16:44:34 +01001003int cmp_activation (const void * a, const void * b) {
1004 float fb = ((collocator *)a)->activation;
1005 float fa = ((collocator *)b)->activation;
1006 return (fa > fb) - (fa < fb);
1007}
1008
1009int cmp_probability (const void * a, const void * b) {
1010 float fb = ((collocator *)a)->probability;
1011 float fa = ((collocator *)b)->probability;
1012 return (fa > fb) - (fa < fb);
1013}
1014
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001015
Marc Kupietzac707b32018-12-20 11:36:38 +01001016SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe, int no_similar_profiles) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001017 HV *result = newHV();
Marc Kupietz7631e022019-01-25 12:04:42 +01001018 float *target_sums=NULL, vec[max_size];
Marc Kupietza5f60042017-05-04 10:38:12 +02001019 long long old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001020 long a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +01001021 knn *para_nbs[MAX_THREADS];
1022 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +01001023 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001024 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +01001025 wordlist *wl;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001026 int syn_threads = (M2? window * 2 : 0);
Marc Kupietzac707b32018-12-20 11:36:38 +01001027 int para_threads = (no_similar_profiles? 0 : num_threads - syn_threads);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001028
Marc Kupietz7631e022019-01-25 12:04:42 +01001029 collocator *best = NULL;
1030 posix_memalign((void **) &best, 128, 10 * (N>=200? N : 200) * sizeof(collocator));
1031 memset(best, 0, (N>=200? N : 200) * sizeof(collocator));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001032
Marc Kupietz000ad862016-02-26 14:59:12 +01001033 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
1034
Marc Kupietzd5d05732018-01-30 11:57:35 +01001035 if(cutoff < 1 || cutoff > words)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +01001036 cutoff=words;
1037
Marc Kupietza2e64502016-04-27 09:53:51 +02001038 wl = getTargetWords(st1, search_backw);
Marc Kupietzf9ac54e2017-11-21 09:22:29 +01001039 if(wl == NULL || wl->length < 1)
Marc Kupietz271e2a42016-03-22 11:37:43 +01001040 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +01001041
Marc Kupietz2c79c5e2017-11-09 16:18:40 +01001042 old_words = cutoff;
Marc Kupietz3082fd02019-01-09 14:54:06 +01001043 slice = cutoff / para_threads;
Marc Kupietza5f60042017-05-04 10:38:12 +02001044
Marc Kupietz2c79c5e2017-11-09 16:18:40 +01001045 a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float));
Marc Kupietz7631e022019-01-25 12:04:42 +01001046 memset(target_sums, 0, cutoff * sizeof(float));
Marc Kupietzce3d4c62016-03-23 16:11:25 +01001047
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001048 printf("Starting %d threads\n", para_threads);
1049 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001050 for(a=0; a < para_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +01001051 pars[a].cutoff = cutoff;
Marc Kupietz000ad862016-02-26 14:59:12 +01001052 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +01001053 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +01001054 pars[a].N = N;
Marc Kupietzc55c8872017-12-01 23:27:10 +01001055 pars[a].best = &best[N*a];
Marc Kupietz3082fd02019-01-09 14:54:06 +01001056 if(merge_words == 0 || search_backw == 0) {
Marc Kupietz1f0f7802019-01-03 16:23:29 +01001057 pars[a].from = a*slice;
1058 pars[a].upto = ((a+1)*slice > cutoff? cutoff : (a+1) * slice);
1059 } else {
Marc Kupietz3082fd02019-01-09 14:54:06 +01001060 pars[a].from = merge_words + a * slice;
1061 pars[a].upto = merge_words + ((a+1)*slice > cutoff? cutoff : (a+1) * slice);
Marc Kupietz1f0f7802019-01-03 16:23:29 +01001062 }
1063 printf("From: %ld, Upto: %ld\n", pars[a].from, pars[a].upto);
Marc Kupietz000ad862016-02-26 14:59:12 +01001064 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
1065 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001066 if(M2) {
1067 for(a=0; a < syn_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +01001068 pars[a + para_threads].cutoff = cutoff;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001069 pars[a + para_threads].target_sums = target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001070 pars[a + para_threads].window_sums = window_sums;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001071 pars[a + para_threads].wl = wl;
1072 pars[a + para_threads].N = N;
Marc Kupietz3c0fab12019-08-02 15:07:47 +02001073 pars[a + para_threads].threshold = MIN_RESP;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001074 pars[a + para_threads].from = a;
1075 pars[a + para_threads].upto = a+1;
1076 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
1077 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001078 }
1079 printf("Waiting for para threads to join\n");
1080 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001081 for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *) &para_nbs[a]);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001082 printf("Para threads joint\n");
1083 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +01001084
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001085 /* if(!syn_nbs[0]) */
1086 /* goto end; */
Marc Kupietz000ad862016-02-26 14:59:12 +01001087
Marc Kupietzc55c8872017-12-01 23:27:10 +01001088 qsort(best, N*para_threads, sizeof(collocator), cmp_activation);
Marc Kupietz000ad862016-02-26 14:59:12 +01001089
Marc Kupietz000ad862016-02-26 14:59:12 +01001090
Marc Kupietzd0d3e912017-12-01 22:09:43 +01001091 long long chosen[MAX_NEIGHBOURS];
1092 printf("N: %ld\n", N);
1093
Marc Kupietz271e2a42016-03-22 11:37:43 +01001094 AV* array = newAV();
Marc Kupietzd91212f2017-11-13 10:05:09 +01001095 int i, j;
Marc Kupietza5f60042017-05-04 10:38:12 +02001096 int l1_words=0, l2_words=0;
Marc Kupietzd0d3e912017-12-01 22:09:43 +01001097
1098 for (a = 0, i = 0; i < N && a < N*para_threads; a++) {
Marc Kupietzd91212f2017-11-13 10:05:09 +01001099 int filtered=0;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001100 long long c = best[a].wordi;
Marc Kupietze8b9dd22019-01-03 17:55:59 +01001101 if ((merge_words && dedupe && i > 1) || (!merge_words && dedupe && i > 0)) {
Marc Kupietzd0d3e912017-12-01 22:09:43 +01001102 for (j=0; j<i && !filtered; j++)
1103 if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) ||
1104 strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) {
1105 printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]);
Marc Kupietzd91212f2017-11-13 10:05:09 +01001106 filtered = 1;
1107 }
1108 if(filtered)
1109 continue;
1110 }
Marc Kupietz3082fd02019-01-09 14:54:06 +01001111
1112
1113 if(0 && merge_words > 0) {
Marc Kupietza5f60042017-05-04 10:38:12 +02001114 if(c >= merge_words) {
1115 if(l1_words > N / 2)
1116 continue;
1117 else
1118 l1_words++;
1119 } else {
1120 if(l2_words > N / 2)
1121 continue;
1122 else
1123 l2_words++;
1124 }
1125 }
Marc Kupietz3082fd02019-01-09 14:54:06 +01001126
1127// printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
1128// fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001129 HV* hash = newHV();
Marc Kupietza5f60042017-05-04 10:38:12 +02001130 SV* word = newSVpvf(&vocab[c * max_w], 0);
Marc Kupietzd0d3e912017-12-01 22:09:43 +01001131 chosen[i] = c;
Marc Kupietz271e2a42016-03-22 11:37:43 +01001132 if(latin_enc == 0) SvUTF8_on(word);
Marc Kupietza5f60042017-05-04 10:38:12 +02001133 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001134 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001135 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
1136 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001137 AV *vector = newAV();
1138 for (b = 0; b < size; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001139 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001140 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001141 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
1142 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietza5f60042017-05-04 10:38:12 +02001143 i++;
Marc Kupietz271e2a42016-03-22 11:37:43 +01001144 }
1145 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz7631e022019-01-25 12:04:42 +01001146
Marc Kupietz50485ba2016-03-23 09:13:14 +01001147 for(b=0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001148 best[b].wordi = -1L;
1149 best[b].activation = 0;
1150 best[b].probability = 0;
1151 best[b].position = 0;
1152 best[b].activation_sum = 0;
Marc Kupietz7631e022019-01-25 12:04:42 +01001153 memset(best[b].heat, 0, sizeof(float)*16);
Marc Kupietz50485ba2016-03-23 09:13:14 +01001154 }
1155
Marc Kupietza77acce2017-11-30 16:59:07 +01001156 float total_activation = 0;
1157
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001158 if (M2) {
1159 printf("Waiting for syn threads to join\n");
1160 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001161 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], (void *) &syn_nbs[a]);
Marc Kupietza77acce2017-11-30 16:59:07 +01001162 for (a = 0; a <= syn_threads; a++) {
Marc Kupietz63872182018-03-12 09:44:49 +01001163 if(a == window) continue;
Marc Kupietza77acce2017-11-30 16:59:07 +01001164 total_activation += window_sums[a];
1165 printf("window pos: %d, sum: %f\n", a, window_sums[a]);
1166 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001167 printf("syn threads joint\n");
1168 fflush(stdout);
Marc Kupietz50485ba2016-03-23 09:13:14 +01001169
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001170 for(b=0; b < syn_nbs[0]->length; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001171 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
1172 best[b].position = -1; // syn_nbs[0]->pos[b];
1173 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001174 best[b].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +01001175 best[b].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001176 best[b].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +01001177 best[b].cprobability = syn_nbs[0]->best[b].cprobability;
Marc Kupietz7631e022019-01-25 12:04:42 +01001178 memset(best[b].heat, 0, sizeof(float)*16);
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001179 }
Marc Kupietz30ca4342017-11-22 21:21:20 +01001180
1181 float best_window_sum[MAX_NEIGHBOURS];
1182 int found_index=0, i=0, j, w;
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001183 for(a=0; a < syn_threads; a++) {
1184 for(b=0; b < syn_nbs[a]->length; b++) {
1185 for(i=0; i < found_index; i++)
1186 if(best[i].wordi == syn_nbs[a]->best[b].wordi)
1187 break;
1188 if(i >= found_index) {
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001189 best[found_index].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +01001190 best[found_index].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001191 best[found_index].probability = 0.0;
Marc Kupietz7631e022019-01-25 12:04:42 +01001192 memset(best[found_index].heat, 0, sizeof(float)*16);
Marc Kupietza77acce2017-11-30 16:59:07 +01001193 best[found_index].cprobability = syn_nbs[a]->best[b].cprobability;
1194 best[found_index].activation_sum = target_sums[syn_nbs[a]->best[b].wordi]; // syn_nbs[a]->best[b].activation_sum;
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001195 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
1196 // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
Marc Kupietz30ca4342017-11-22 21:21:20 +01001197 }
1198 }
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001199 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001200 sort_by =0; // ALWAYS AUTO-FOCUS
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001201 if(sort_by != 1 && sort_by != 2) { // sort by auto focus mean
Marc Kupietz30ca4342017-11-22 21:21:20 +01001202 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) -1);
Marc Kupietzbd41da32017-11-24 10:26:43 +01001203 int wpos;
Marc Kupietz4116b432017-12-06 14:15:32 +01001204 int bits_set = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001205 for(i=0; i < found_index; i++) {
Marc Kupietz4116b432017-12-06 14:15:32 +01001206 best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001207 for(w=1; w < (1 << syn_threads); w++) { // loop through all possible windows
Marc Kupietz4116b432017-12-06 14:15:32 +01001208 float word_window_sum = 0, word_window_average=0, word_cprobability_sum=0, word_activation_sum = 0, total_window_sum = 0;
1209 bits_set = 0;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001210 for(a=0; a < syn_threads; a++) {
Marc Kupietz30ca4342017-11-22 21:21:20 +01001211 if((1 << a) & w) {
Marc Kupietzbd41da32017-11-24 10:26:43 +01001212 wpos = (a >= window? a+1 : a);
1213 total_window_sum += window_sums[wpos];
Marc Kupietz30ca4342017-11-22 21:21:20 +01001214 }
1215 }
Marc Kupietzbd41da32017-11-24 10:26:43 +01001216// printf("%d window-sum %f\n", w, total_window_sum);
1217 for(a=0; a < syn_threads; a++) {
1218 if((1 << a) & w) {
1219 wpos = (a >= window? a+1 : a);
1220 bits_set++;
1221 for(b=0; b < syn_nbs[a]->length; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001222 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001223// float acti = syn_nbs[a]->best[b].activation / total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001224// word_window_sum += syn_nbs[a]->dist[b] * syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
Marc Kupietzbd41da32017-11-24 10:26:43 +01001225// word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1226// word_window_sum = (word_window_sum + syn_nbs[a]->norm[b]) - (word_window_sum * syn_nbs[a]->norm[b]); // syn_nbs[a]->norm[b];
Marc Kupietza77acce2017-11-30 16:59:07 +01001227
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001228 word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
Marc Kupietza77acce2017-11-30 16:59:07 +01001229// word_window_sum += acti - (word_window_sum * acti); syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1230
Marc Kupietz4116b432017-12-06 14:15:32 +01001231 word_window_average += syn_nbs[a]->best[b].activation; // - word_window_average * syn_nbs[a]->best[b].activation; // conormalied activation sum
1232 word_cprobability_sum += syn_nbs[a]->best[b].cprobability - word_cprobability_sum * syn_nbs[a]->best[b].cprobability; // conormalied column probability sum
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001233 word_activation_sum += syn_nbs[a]->best[b].activation;
1234 if(syn_nbs[a]->best[b].activation > best[i].max_activation)
1235 best[i].max_activation = syn_nbs[a]->best[b].activation;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +01001236 if(syn_nbs[a]->best[b].activation > best[i].heat[wpos] )
Marc Kupietz728b8ed2017-12-02 11:13:49 +01001237 best[i].heat[wpos] = syn_nbs[a]->best[b].activation;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001238 }
1239 }
1240 }
Marc Kupietz4116b432017-12-06 14:15:32 +01001241 if(bits_set) {
1242 word_window_average /= bits_set;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001243// word_activation_sum /= bits_set;
1244// word_window_sum /= bits_set;
Marc Kupietz4116b432017-12-06 14:15:32 +01001245 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001246
1247 word_window_sum /= total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001248
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001249 if(word_window_sum > best[i].probability) {
Marc Kupietz4116b432017-12-06 14:15:32 +01001250// best[i].position = w;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001251 best[i].probability = word_window_sum;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001252 }
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001253
Marc Kupietz4116b432017-12-06 14:15:32 +01001254 if(word_cprobability_sum > best[i].cprobability_sum) {
1255 best[i].position = w;
1256 best[i].cprobability_sum = word_cprobability_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001257 }
Marc Kupietz4116b432017-12-06 14:15:32 +01001258
1259 best[i].average = word_window_average;
1260// best[i].activation = word_activation_sum;
1261 }
Marc Kupietz30ca4342017-11-22 21:21:20 +01001262 }
Marc Kupietze0e42132017-11-24 16:44:34 +01001263 qsort(best, found_index, sizeof(collocator), cmp_probability);
Marc Kupietz30ca4342017-11-22 21:21:20 +01001264// for(i=0; i < found_index; i++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001265// printf("found: %s - sum: %f - window: %d\n", &vocab[best[i].wordi * max_w], best[i].activation, best[i].position);
Marc Kupietz30ca4342017-11-22 21:21:20 +01001266// }
1267
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001268 } else if(sort_by == 1) { // responsiveness any window position
1269 int wpos;
1270 for(i=0; i < found_index; i++) {
1271 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
1272 for(a=0; a < syn_threads; a++) {
1273 wpos = (a >= window? a+1 : a);
1274 for(b=0; b < syn_nbs[a]->length; b++)
1275 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
1276 best[i].probability += syn_nbs[a]->best[b].probability;
1277 if(syn_nbs[a]->best[b].activation > 0.25)
1278 best[i].position |= 1 << wpos;
1279 if(syn_nbs[a]->best[b].activation > best[i].activation) {
1280 best[i].activation = syn_nbs[a]->best[b].activation;
1281 }
1282 }
1283 }
1284 }
1285 qsort(best, found_index, sizeof(collocator), cmp_activation);
1286 } else if(sort_by == 2) { // single window position
Marc Kupietz30ca4342017-11-22 21:21:20 +01001287 for(a=1; a < syn_threads; a++) {
1288 for(b=0; b < syn_nbs[a]->length; b++) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001289 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001290 if(syn_nbs[a]->best[b].activation > best[c].activation) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001291 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001292 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001293 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001294 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
1295 best[c].position = 1 << (-syn_nbs[a]->best[b].position+window - (syn_nbs[a]->best[b].position < 0 ? 1:0));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001296 break;
Marc Kupietz6d9a6782016-03-23 17:25:25 +01001297 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001298 }
1299 }
1300 }
1301 } else { // sort by mean p
1302 for(a=1; a < syn_threads; a++) {
1303 for(b=0; b < syn_nbs[a]->length; b++) {
1304 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001305 if(target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001306 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001307 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001308 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001309 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
1310 best[c].position = (1 << 2*window) - 1; // syn_nbs[a]->pos[b];
1311 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001312 break;
1313 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001314 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +01001315 }
1316 }
1317 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001318 array = newAV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001319 for (a = 0, i=0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
1320 long long c = best[a].wordi;
Marc Kupietza77acce2017-11-30 16:59:07 +01001321/*
Marc Kupietzd91212f2017-11-13 10:05:09 +01001322 if (dedupe) {
1323 int filtered=0;
1324 for (j=0; j<i; j++)
1325 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
1326 strcasestr(chosen[j], &vocab[c * max_w])) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001327 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
1328 filtered = 1;
1329 }
Marc Kupietzd91212f2017-11-13 10:05:09 +01001330 if(filtered)
1331 continue;
1332 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001333*/
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001334 chosen[i++]=c;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001335 HV* hash = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001336 SV* word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001337 AV* heat = newAV();
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001338 if(latin_enc == 0) SvUTF8_on(word);
1339 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001340 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001341 hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001342 hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001343 hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001344 hv_store(hash, "max", strlen("max"), newSVnv(best[a].max_activation), 0); // newSVnv(target_sums[best[a].wordi]), 0);
Marc Kupietza77acce2017-11-30 16:59:07 +01001345 hv_store(hash, "overall", strlen("overall"), newSVnv(best[a].activation_sum/total_activation), 0); // newSVnv(target_sums[best[a].wordi]), 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001346 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001347 best[a].heat[5]=0;
1348 for(i=10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i]));
1349 hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV*)heat), 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001350 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietz271e2a42016-03-22 11:37:43 +01001351 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001352 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001353 }
Marc Kupietz000ad862016-02-26 14:59:12 +01001354end:
Marc Kupietz257a93d2019-01-03 17:55:22 +01001355 // words = old_words; // why was this here?
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001356 free(best);
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001357 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +01001358}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +01001359
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001360int dump_vecs(char *fname) {
1361 long i, j;
1362 FILE *f;
Marc Kupietz0a68a792018-12-20 11:41:45 +01001363 if(words>100000)
1364 words=100000;
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001365
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001366 if((f=fopen(fname, "w")) == NULL) {
1367 fprintf(stderr, "cannot open %s for writing\n", fname);
1368 return(-1);
1369 }
1370 fprintf(f, "%lld %lld\n", words, size);
1371 for (i=0; i < words; i++) {
1372 fprintf(f, "%s ", &vocab[i * max_w]);
1373 for(j=0; j < size - 1; j++)
1374 fprintf(f, "%f ", M[i*size + j]);
1375 fprintf(f, "%f\n", M[i*size + j]);
1376 }
1377 fclose(f);
1378 return(0);
1379}
Marc Kupietzdc22b982015-10-09 09:19:34 +02001380
Marc Kupietzfa194262018-06-05 09:39:32 +02001381int dump_for_numpy(char *fname) {
1382 long i, j;
1383 FILE *f;
Marc Kupietz1233eda2019-03-06 14:16:43 +01001384 int max;
1385 if(merged_end > 0)
1386 max = 150000;
1387 else
1388 max = 300000;
1389
1390 if(words>300000)
1391 words=300000;
Marc Kupietzfa194262018-06-05 09:39:32 +02001392
1393 if((f=fopen(fname, "w")) == NULL) {
1394 fprintf(stderr, "cannot open %s for writing\n", fname);
1395 return(-1);
1396 }
Marc Kupietz1233eda2019-03-06 14:16:43 +01001397 for (i=0; i < max; i++) {
Marc Kupietzfa194262018-06-05 09:39:32 +02001398 for(j=0; j < size - 1; j++)
1399 fprintf(f, "%f\t", M[i*size + j]);
1400 fprintf(f, "%f\n", M[i*size + j]);
Marc Kupietz1233eda2019-03-06 14:16:43 +01001401 printf("%s\n", &vocab[i * max_w]);
1402
1403 if(merged_end > 0) {
1404 for(j=0; j < size - 1; j++)
1405 fprintf(f, "%f\t", M[(merged_end + i)*size + j]);
1406 fprintf(f, "%f\n", M[(merged_end + i)*size + j]);
1407 printf("_%s\n", &vocab[i * max_w]);
1408 }
Marc Kupietzfa194262018-06-05 09:39:32 +02001409 }
1410 fclose(f);
Marc Kupietz7631e022019-01-25 12:04:42 +01001411 return(0);
Marc Kupietzfa194262018-06-05 09:39:32 +02001412}