blob: 99c957dd5608591a0dfea1f4bd4367f2767394ed [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 Kupietz33c79d32019-08-02 15:11:23 +0200199any '*/getPosWiseW2VCollocators' => sub {
200 my $self = shift;
201 $self->render(data => getPosWiseW2VCollocatorsAsTsv($self->param("w"),
202 ($self->param("max")? $self->param("max") : 200),
203 ($self->param("cutoff")? $self->param("cutoff") :750000),
204 ($self->param("threshold")? $self->param("threshold") : 0.2)),
205 format=>'tsv');
206};
207
208any '/getPosWiseW2VCollocators' => sub {
209 my $self = shift;
210 $self->render(data => getPosWiseW2VCollocatorsAsTsv($self->param("w"),
211 ($self->param("max")? $self->param("max") : 200),
212 ($self->param("cutoff")? $self->param("cutoff") : 750000),
213 ($self->param("threshold")? $self->param("threshold") : 0.2)),
214 format=>'tsv');
215};
216
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100217any '*/getSimilarProfiles' => sub {
218 my $self = shift;
219 $self->render(data => getSimilarProfilesCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
220};
221
Marc Kupietzc987fa82018-03-21 12:14:25 +0100222any '/getSimilarProfiles' => sub {
223 my $self = shift;
224 $self->render(data => getSimilarProfilesCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
225};
226
Marc Kupietz98ed1c02019-08-02 15:05:37 +0200227any '/getSimilarity' => sub {
228 my $self = shift;
229 my $w1 = $self->param("w1");
230 my $w2 = $self->param("w2");
231 $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json');
232};
233
234any '*/getSimilarity' => sub {
235 my $self = shift;
236 my $w1 = $self->param("w1");
237 my $w2 = $self->param("w2");
238 $self->render(data => cos_similarity_as_json($w1, $w2), format=>'json');
239};
240
Marc Kupietzdf3d4b52017-11-29 16:57:27 +0100241get '*/img/*' => sub {
242 my $c = shift;
243 my $url = $c->req->url;
244 $url =~ s@/derekovecs@@g;
245 $c->app->log->info("GET: " . $url);
246 $c->reply->static($url);
247};
248
Marc Kupietzdc22b982015-10-09 09:19:34 +0200249get '/' => sub {
250 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +0200251 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200252 my $word=$c->param('word');
Marc Kupietz2da2a812019-02-21 14:17:35 +0100253 my $no_nbs=$c->param('n') || ($opt_m? 50 : 100);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100254 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +0100255 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100256 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100257 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200258 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100259 my $sort=$c->param('sort') || 0;
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100260 my $csv=$c->param('csv') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +0200261 my $json=$c->param('json') || 0;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100262 my $cutoff=$c->param('cutoff') || 500000;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100263 my $dedupe=$c->param('dedupe') || 0;
Marc Kupietzac707b32018-12-20 11:36:38 +0100264 my $nosp=$c->param('nosp') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100265 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100266 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100267 my @collocations;
Marc Kupietzd897d5d2019-08-02 15:09:07 +0200268 $word="idea" if(!defined($word));
269 if(defined($word) && $word !~ /^\s*$/) {
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100270 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100271 $word =~ s/\s+/ /g;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100272 if($opt_m && $word !~ /\|/) {
273 $word .= "|$word";
274 }
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100275 for my $w (split(' *\| *', $word)) {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100276 if($opt_m) {
277 if($searchBaseVocabFirst) {
278 $searchBaseVocabFirst=0;
279 } else {
280 $searchBaseVocabFirst=1;
281 }
282 }
283 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe,$searchBaseVocabFirst}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100284 $c->app->log->info("Getting $w results from cache");
Marc Kupietz3082fd02019-01-09 14:54:06 +0100285 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe.$searchBaseVocabFirst}
Marc Kupietza5b90152016-03-15 17:39:19 +0100286 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100287 $c->app->log->info('Looking for neighbours of '.$w);
288 if($opt_i) {
Marc Kupietzac707b32018-12-20 11:36:38 +0100289 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100290 } else {
Marc Kupietzac707b32018-12-20 11:36:38 +0100291 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100292 }
Marc Kupietz2dd2dd72017-12-01 22:08:14 +0100293 $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100294 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100295 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100296 }
297 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100298 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200299 if($json) {
300 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100301 } elsif($csv) {
302 my $csv_data="";
303 for (my $i=0; $i <= $no_nbs; $i++) {
304 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
305 }
306 for (my $i=0; $i < $no_nbs; $i++) {
307 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
308 }
309 chop $csv_data;
310 chop $csv_data;
311 $csv_data .= "\n";
312 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200313 } else {
Marc Kupietzd7760b42019-02-21 09:01:44 +0100314 my $distantWords="";
315 if(!defined($word) || $word !~ /^\s*$/) {
316 $distantWords = getBiggestMergedDifferences();
317 }
318 $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 +0200319 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200320};
321
Marc Kupietz30ca4342017-11-22 21:21:20 +0100322helper(bitvec2window => sub {
323 my ($self, $n) = @_;
324 my $str = unpack("B32", pack("N", $n));
325 $str =~ s/^\d{22}//;
326 $str =~ s/^(\d{5})/$1x/;
327 $str =~ s/0/·/g;
328 $str =~ s/1/+/g;
329 return $str;
330 });
331
Marc Kupietza5b90152016-03-15 17:39:19 +0100332$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200333
334exit;
335
336__END__
337
338__C__
339#include <stdio.h>
340#include <string.h>
341#include <math.h>
342#include <malloc.h>
343#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +0100344#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +0100345#include <pthread.h>
Marc Kupietze243efd2018-01-11 22:19:24 +0100346#include <collocatordb.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200347
348#define max_size 2000
349#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100350#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100351#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +0100352#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100353#define MAX_CC 50
354#define EXP_TABLE_SIZE 1000
355#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +0100356#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +0200357
358//the thread function
359void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +0100360
361typedef struct {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100362 long long wordi;
363 long position;
364 float activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100365 float average;
Marc Kupietza77acce2017-11-30 16:59:07 +0100366 float cprobability; // column wise probability
Marc Kupietz4116b432017-12-06 14:15:32 +0100367 float cprobability_sum;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100368 float probability;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100369 float activation_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100370 float max_activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100371 float heat[16];
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100372} collocator;
373
374typedef struct {
375 collocator *best;
Marc Kupietz80abb442016-03-23 21:04:08 +0100376 int length;
Marc Kupietz000ad862016-02-26 14:59:12 +0100377} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100378
Marc Kupietz000ad862016-02-26 14:59:12 +0100379typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +0100380 long long wordi[MAX_NEIGHBOURS];
381 char sep[MAX_NEIGHBOURS];
382 int length;
383} wordlist;
384
385typedef struct {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100386 long cutoff;
Marc Kupietz48c29682016-03-19 11:30:43 +0100387 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100388 char *token;
389 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100390 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100391 unsigned long upto;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100392 collocator *best;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100393 float *target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100394 float *window_sums;
Marc Kupietz3c0fab12019-08-02 15:07:47 +0200395 float threshold;
Marc Kupietz000ad862016-02-26 14:59:12 +0100396} knnpars;
397
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100398typedef struct {
399 uint32_t index;
400 float value;
401} sparse_t;
402
403typedef struct {
404 uint32_t len;
405 sparse_t nbr[100];
406} profile_t;
407
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200408float *M, *M2=0L, *syn1neg_window, *expTable;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100409float *window_sums;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200410char *vocab;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200411char *garbage = NULL;
Marc Kupietze243efd2018-01-11 22:19:24 +0100412COLLOCATORDB *cdb = NULL;
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100413profile_t *sprofiles = NULL;
414size_t sprofiles_qty = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100415
Marc Kupietza2e64502016-04-27 09:53:51 +0200416long long words, size, merged_end;
Marc Kupietza5f60042017-05-04 10:38:12 +0200417long long merge_words = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100418int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100419int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100420int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200421
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100422/* load collocation profiles if file exists */
423int load_sprofiles(char *vecsname) {
424 char *basename = strdup(vecsname);
425 char *pos = strstr(basename, ".vecs");
426 if(pos)
427 *pos=0;
428
429 char binsprofiles_fname[256];
430 strcpy(binsprofiles_fname, basename);
431 strcat(binsprofiles_fname, ".sprofiles.bin");
432 FILE *fp = fopen(binsprofiles_fname, "rb");
433 if (fp == NULL) {
434 printf("Collocation profiles %s not found. No problem.\n", binsprofiles_fname);
435 return 0;
436 }
437 fseek(fp, 0L, SEEK_END);
438 size_t sz = ftell(fp);
439 fclose(fp);
440
441 int fd = open(binsprofiles_fname, O_RDONLY);
442 sprofiles = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0);
443 if (sprofiles == MAP_FAILED) {
444 close(fd);
445 fprintf(stderr, "Cannot mmap %s\n", binsprofiles_fname);
446 sprofiles = NULL;
447 return 0;
448 } else {
449 sprofiles_qty = sz / sizeof(profile_t);
450 fprintf(stderr, "Successfully mmaped %s containing similar profiles for %ld word forms.\n", binsprofiles_fname, sprofiles_qty);
451 }
452 return 1;
453}
454
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100455int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100456 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100457 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100458 long long a, b, c, d, cn;
459 float len;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200460 double val;
Marc Kupietz82b02672016-02-26 12:32:25 +0100461
Marc Kupietz67c20282016-02-26 09:42:00 +0100462 char binvecs_fname[256], binwords_fname[256];
463 strcpy(binwords_fname, file_name);
464 strcat(binwords_fname, ".words");
465 strcpy(binvecs_fname, file_name);
466 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200467
Marc Kupietza5b90152016-03-15 17:39:19 +0100468 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200469 f = fopen(file_name, "rb");
470 if (f == NULL) {
471 printf("Input file %s not found\n", file_name);
472 return -1;
473 }
474 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100475 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200476 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100477 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
478 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100479 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
480 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
481 if (M == NULL) {
482 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
483 return -1;
484 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200485 if(strstr(file_name, ".txt")) {
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 len = 0;
495 for (a = 0; a < size; a++) {
496 fscanf(f, "%lf", &val);
497 M[a + b * size] = val;
498 len += val * val;
499 }
500 len = sqrt(len);
501 for (a = 0; a < size; a++) M[a + b * size] /= len;
502 }
503 } else {
504 for (b = 0; b < words; b++) {
505 a = 0;
506 while (1) {
507 vocab[b * max_w + a] = fgetc(f);
508 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
509 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
510 }
511 vocab[b * max_w + a] = 0;
512 fread(&M[b * size], sizeof(float), size, f);
513 len = 0;
514 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
515 len = sqrt(len);
516 for (a = 0; a < size; a++) M[a + b * size] /= len;
517 }
518 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100519 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
520 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
521 fclose(binvecs);
522 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
523 fclose(binwords);
524 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100525 }
526 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
527 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
528 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
529 if (M == MAP_FAILED || vocab == MAP_FAILED) {
530 close(binvecs_fd);
531 close(binwords_fd);
532 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
533 exit(-1);
534 }
535 } else {
536 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
537 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100538 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200539 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100540
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200541 if(net_name && strlen(net_name) > 0) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100542 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
543 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
544 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100545 // munmap(M, sizeof(float) * words * size);
546 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 +0200547 if (M2 == MAP_FAILED) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100548 close(net_fd);
549 fprintf(stderr, "Cannot mmap %s\n", net_name);
550 exit(-1);
551 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100552 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100553 } else {
554 fprintf(stderr, "Cannot open %s\n", net_name);
555 exit(-1);
556 }
557 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
Marc Kupietze243efd2018-01-11 22:19:24 +0100558
559 char collocatordb_name[2048];
560 strcpy(collocatordb_name, net_name);
561 char *ext = rindex(collocatordb_name, '.');
562 if(ext) {
563 strcpy(ext, ".rocksdb");
564 if(access(collocatordb_name, R_OK) == 0) {
565 *ext = 0;
566 fprintf(stderr, "Opening collocator DB %s\n", collocatordb_name);
567 cdb = open_collocatordb(collocatordb_name);
568 }
569 }
570 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100571
572 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
573 for (i = 0; i < EXP_TABLE_SIZE; i++) {
574 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
575 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
576 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100577 window_sums = malloc(sizeof(float) * (window+1) * 2);
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100578
579 return 0;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200580}
581
Marc Kupietza2e64502016-04-27 09:53:51 +0200582long mergeVectors(char *file_name){
583 FILE *f, *binvecs, *binwords;
584 int binwords_fd, binvecs_fd, net_fd, i;
585 long long a, b, c, d, cn;
586 float len;
587 float *merge_vecs;
588 char *merge_vocab;
Marc Kupietza5f60042017-05-04 10:38:12 +0200589 /* long long merge_words, merge_size; */
590 long long merge_size;
Marc Kupietza2e64502016-04-27 09:53:51 +0200591
592 char binvecs_fname[256], binwords_fname[256];
593 strcpy(binwords_fname, file_name);
594 strcat(binwords_fname, ".words");
595 strcpy(binvecs_fname, file_name);
596 strcat(binvecs_fname, ".vecs");
597
598 f = fopen(file_name, "rb");
599 if (f == NULL) {
600 printf("Input file %s not found\n", file_name);
601 exit -1;
602 }
603 fscanf(f, "%lld", &merge_words);
604 fscanf(f, "%lld", &merge_size);
605 if(merge_size != size){
606 fprintf(stderr, "vectors must have the same length\n");
607 exit(-1);
608 }
609 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
610 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
611 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
612 if (merge_vecs == NULL || merge_vocab == NULL) {
613 close(binvecs_fd);
614 close(binwords_fd);
615 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
616 exit(-1);
617 }
618 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
619 read(binwords_fd, merge_vocab, merge_words * max_w);
620 } else {
621 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
622 exit(-1);
623 }
624 printf("Successfully reallocated memory\nMerging...\n");
625 fflush(stdout);
626 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
627 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
628 munmap(M, words * size * sizeof(float));
629 munmap(vocab, words * max_w);
630 M = merge_vecs;
631 vocab = merge_vocab;
632 merged_end = merge_words;
633 words += merge_words;
634 fclose(f);
635 printf("merged_end: %lld, words: %lld\n", merged_end, words);
Marc Kupietzd7760b42019-02-21 09:01:44 +0100636 //printBiggestMergedDifferences();
Marc Kupietza2e64502016-04-27 09:53:51 +0200637 return((long) merged_end);
638}
639
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200640void filter_garbage() {
641 long i;
Marc Kupietzab591a82016-04-28 14:08:49 +0200642 unsigned char *w, previous, c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200643 garbage = malloc(words);
644 memset(garbage, 0, words);
645 for (i = 0; i < words; i++) {
646 w = vocab + i * max_w;
647 previous = 0;
Marc Kupietz969adfa2018-03-12 09:43:53 +0100648 if(strncmp("quot", w, 4) == 0) {
649 garbage[i]=1;
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100650// printf("Gargabe: %s\n", vocab + i * max_w);
Marc Kupietz969adfa2018-03-12 09:43:53 +0100651 } else {
652 while((c = *w++) && !garbage[i]) {
653 if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
654 (previous == '-' && (c & 32)) ||
655 (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) ||
656 (previous == 'q' && c == 'u' && *(w) == 'o' && *(w+1) == 't') || /* quot */
657 c == '<'
658 ) {
659 garbage[i]=1;
660 continue;
661 }
662 previous = c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200663 }
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200664 }
665 }
666 return;
667}
668
Marc Kupietzac707b32018-12-20 11:36:38 +0100669
670knn *simpleGetCollocators(int word, int number, long cutoff, int *result) {
671 knnpars *pars = calloc(sizeof(knnpars), 1);
672 float *target_sums;
673 float *window_sums = malloc(sizeof(float) * (window+1) * 2);
674 pars->cutoff = (cutoff? cutoff : 300000);
675 long a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
676 for(a = 0; a < cutoff; a++)
677 target_sums[a] = 0;
678 pars->target_sums = target_sums;
679 pars->window_sums = window_sums;
680 pars->N = (number? number : 20);
681 pars->from = 0;
682 pars->upto = window * 2 -1;
Marc Kupietz7631e022019-01-25 12:04:42 +0100683 knn *syn_nbs = NULL; // = (knn*) getCollocators(pars);
Marc Kupietzac707b32018-12-20 11:36:38 +0100684 free(pars);
685 free(window_sums);
686 free(target_sums);
687 return syn_nbs;
688}
689
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100690void *getCollocators(void *args) {
691 knnpars *pars = args;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100692 int N = pars->N;
Marc Kupietz52702832019-01-15 17:55:52 +0100693
Marc Kupietz271e2a42016-03-22 11:37:43 +0100694 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100695 knn *nbs = NULL;
696 long window_layer_size = size * window * 2;
697 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
698 float f, max_f, maxmax_f;
Marc Kupietz7631e022019-01-25 12:04:42 +0100699 float *target_sums=NULL, worstbest, wpos_sum;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100700 collocator *best;
Marc Kupietzd5642582016-03-19 22:23:13 +0100701
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200702 if(M2 == NULL || cc == -1)
Marc Kupietzd5642582016-03-19 22:23:13 +0100703 return NULL;
704
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100705 a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietz7631e022019-01-25 12:04:42 +0100706 memset(target_sums, 0, pars->cutoff * sizeof(float));
707 best = malloc((N>200?N:200) * sizeof(collocator));
708 memset(best, 0, (N>200?N:200) * sizeof(collocator));
Marc Kupietz3c0fab12019-08-02 15:07:47 +0200709 worstbest = pars->threshold;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100710
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100711 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100712 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100713 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100714 best[b].wordi = -1;
715 best[b].probability = 1;
716 best[b].activation = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100717 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100718
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100719 d = cc;
720 maxmax_f = -1;
721 maxmax_target = 0;
722
Marc Kupietz271e2a42016-03-22 11:37:43 +0100723 for (a = pars->from; a < pars->upto; a++) {
724 if(a >= window)
725 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100726 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100727 printf("window pos: %ld\n", a);
728 if (a != window) {
729 max_f = -1;
730 window_offset = a * size;
731 if (a > window)
732 window_offset -= size;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100733 for(target = 0; target < pars->cutoff; target ++) {
734 if(garbage && garbage[target]) continue;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100735 if(target == d)
736 continue;
737 f = 0;
738 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100739 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100740 if (f < -MAX_EXP)
741 continue;
742 else if (f > MAX_EXP)
743 continue;
744 else
745 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100746 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100747
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100748 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100749 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100750 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100751 if (f > best[b].activation) {
752 memmove(best + b + 1, best + b, (N - b -1) * sizeof(collocator));
753 best[b].activation = f;
754 best[b].wordi = target;
755 best[b].position = window-a;
Marc Kupietz33679a32016-03-22 08:49:39 +0100756 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100757 }
758 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100759 if(b == N - 1)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100760 worstbest = best[N-1].activation;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100761 }
762 }
763 printf("%d %.2f\n", max_target, max_f);
764 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
765 if(max_f > maxmax_f) {
766 maxmax_f = max_f;
767 maxmax_target = max_target;
768 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100769 for (b = 0; b < N; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100770 if(best[b].position == window-a)
Marc Kupietza77acce2017-11-30 16:59:07 +0100771 best[b].cprobability = best[b].activation / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100772 } else {
773 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
774 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100775 pars->window_sums[a] = wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100776 }
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100777 for (b = 0; b < pars->cutoff; b++)
Marc Kupietza77acce2017-11-30 16:59:07 +0100778 pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2);
Marc Kupietzd89431f2019-08-02 15:06:38 +0200779
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100780 free(target_sums);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100781 for(b=0; b<N && best[b].wordi >= 0; b++);; // THIS LOOP IS NEEDED (b...)
782// 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 +0100783// printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100784 nbs = malloc(sizeof(knn));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100785 nbs->best = best;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100786 nbs->length = b-1;
787 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100788}
789
Marc Kupietz30ca4342017-11-22 21:21:20 +0100790
Marc Kupietz66bfd952017-12-11 09:59:45 +0100791AV *getVecs(AV *array) {
792 int i, b;
793 AV *result = newAV();
794 for (i=0; i<=av_len(array); i++) {
795 SV** elem = av_fetch(array, i, 0);
796 if (elem != NULL) {
797 long j = (long) SvNV(*elem);
798 AV *vector = newAV();
799 for (b = 0; b < size; b++) {
800 av_push(vector, newSVnv(M[b + j * size]));
801 }
802 av_push(result, newRV_noinc(vector));
803 }
804 }
805 return result;
806}
807
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100808char *getSimilarProfiles(long node) {
809 int i;
810 char buffer[120000];
811 char pair_buffer[2048];
812 buffer[0]='[';
813 buffer[1]=0;
814 if(node >= sprofiles_qty) {
815 printf("Not available in precomputed profile\n");
816 return(strdup("[{\"w\":\"not available\", \"v\":0}]\n"));
817 }
818
819 printf("******* %s ******\n", &vocab[max_w * node]);
820
821 for(i=0; i < 100 && i < sprofiles[node].len; i++) {
822 sprintf(pair_buffer, "{\"w\":\"%s\", \"v\":%f},", &vocab[max_w * (sprofiles[node].nbr[i].index)], sprofiles[node].nbr[i].value);
823 strcat(buffer, pair_buffer);
824 }
825 buffer[strlen(buffer)-1]=']';
826 strcat(buffer, "\n");
827 printf(buffer);
828 return(strdup(buffer));
829}
830
Marc Kupietze243efd2018-01-11 22:19:24 +0100831char *getClassicCollocators(long node) {
832 char *res = (cdb? strdup(get_collocators_as_json(cdb, node)) : "[]");
833 return res;
834}
835
Marc Kupietz0957d532019-01-03 17:54:00 +0100836wordlist *getTargetWords(char *st1, int search_backw) {
Marc Kupietz48c29682016-03-19 11:30:43 +0100837 wordlist *wl = malloc(sizeof(wordlist));
838 char st[100][max_size], sep[100];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100839 long a, b=0, c=0, cn=0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200840 int unmerged;
841
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100842 while (1) {
843 st[cn][b] = st1[c];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100844 b++;
845 c++;
846 st[cn][b] = 0;
847 if (st1[c] == 0) break;
848 if (st1[c] == ' ' || st1[c] == '-') {
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100849 sep[cn++] = st1[c];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100850 b = 0;
851 c++;
852 }
853 }
854 cn++;
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100855 for (a = 0; a < cn; a++) {
856 if (search_backw) {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100857 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 +0100858 } else {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100859 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 +0100860 }
861 if (b == words) b = -1;
862 wl->wordi[a] = b;
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100863 if (b == -1) {
864 fprintf(stderr, "Out of dictionary word!\n");
865 cn--;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100866 } else {
867 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 +0100868 }
869 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100870 wl->length=cn;
871 return(wl);
872}
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100873
Marc Kupietz98ed1c02019-08-02 15:05:37 +0200874float get_distance(long b, long c) {
875 long a;
876 float dist = 0;
877 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + b * size];
878 return dist;
879}
880
Marc Kupietzd7760b42019-02-21 09:01:44 +0100881char *getBiggestMergedDifferences() {
882 static char *result = NULL;
Marc Kupietzebc131e2019-01-03 16:24:27 +0100883 float dist, len, vec[max_size];
884 long long a, b, c, d, cn, *bi;
885 char ch;
886 knn *nbs = NULL;
Marc Kupietzd7760b42019-02-21 09:01:44 +0100887 int N = 1000;
888
Marc Kupietzb6514dc2019-06-26 11:47:56 +0200889 if(merged_end == 0)
890 result = "[]";
891
Marc Kupietzd7760b42019-02-21 09:01:44 +0100892 if(result != NULL)
893 return result;
Marc Kupietzebc131e2019-01-03 16:24:27 +0100894
895 printf("Looking for biggest distances between main and merged vectors ...\n");
896 collocator *best;
897 best = malloc(N * sizeof(collocator));
898 memset(best, 0, N * sizeof(collocator));
899
900 float worstbest=1000000;
901
902 for (a = 0; a < N; a++) best[a].activation = worstbest;
903
Marc Kupietz2da2a812019-02-21 14:17:35 +0100904 for (c = 0; c < 500000; c++) {
Marc Kupietzebc131e2019-01-03 16:24:27 +0100905 if(garbage && garbage[c]) continue;
906 a = 0;
907 dist = 0;
908 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + (c+merged_end) * size];
909 if(dist < worstbest) {
910 for (a = 0; a < N; a++) {
911 if (dist < best[a].activation) {
912 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
913 best[a].activation = dist;
914 best[a].wordi = c;
915 break;
916 }
917 }
918 worstbest = best[N-1].activation;
919 }
920 }
921
Marc Kupietzd7760b42019-02-21 09:01:44 +0100922 result = malloc(N*max_w);
923 char *p = result;
924 *p++ = '['; *p = 0;
Marc Kupietzebc131e2019-01-03 16:24:27 +0100925 for (a = 0; a < N; a++) {
Marc Kupietzd7760b42019-02-21 09:01:44 +0100926 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 +0100927 }
Marc Kupietzd7760b42019-02-21 09:01:44 +0100928 *--p = ']';
929 return(result);
Marc Kupietzebc131e2019-01-03 16:24:27 +0100930}
931
Marc Kupietz98ed1c02019-08-02 15:05:37 +0200932
933float cos_similarity(long b, long c) {
934 float dist=0;
935 long a;
936 for (a = 0; a < size; a++) dist += M[b * size + a] * M[c * size + a];
937 return dist;
938}
939
940char *cos_similarity_as_json(char *w1, char *w2) {
941 wordlist *a, *b;
942 float res;
943 a = getTargetWords(w1, 0);
944 b = getTargetWords(w2, 0);
945 if (a == NULL || b==NULL || a->length != 1 || b->length != 1)
946 res = -1;
947 else
948 res = cos_similarity(a->wordi[0], b->wordi[0]);
949 fprintf(stderr, "a: %lld b: %lld res:%f\n", a->wordi[0], b->wordi[0], res);
950 char *json = malloc(16);
951 sprintf(json, "%.5f", res);
952 return json;
953}
954
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100955void *_get_neighbours(void *arg) {
956 knnpars *pars = arg;
Marc Kupietz48c29682016-03-19 11:30:43 +0100957 char *st1 = pars->token;
958 int N = pars->N;
959 long from = pars -> from;
960 unsigned long upto = pars -> upto;
961 char file_name[max_size], st[100][max_size], *sep;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100962 float dist, len, vec[max_size];
963 long long a, b, c, d, cn, *bi;
Marc Kupietz48c29682016-03-19 11:30:43 +0100964 char ch;
965 knn *nbs = NULL;
966 wordlist *wl = pars->wl;
967
Marc Kupietzc55c8872017-12-01 23:27:10 +0100968 collocator *best = pars->best;
Marc Kupietz48c29682016-03-19 11:30:43 +0100969
970 float worstbest=-1;
971
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100972 for (a = 0; a < N; a++) best[a].activation = 0;
Marc Kupietz48c29682016-03-19 11:30:43 +0100973 a = 0;
974 bi = wl->wordi;
975 cn = wl->length;
976 sep = wl->sep;
977 b = bi[0];
978 c = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100979 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100980 N = 0;
981 goto end;
982 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200983 for (a = 0; a < size; a++) vec[a] = 0;
984 for (b = 0; b < cn; b++) {
985 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100986 if(b>0 && sep[b-1] == '-')
987 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
988 else
989 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200990 }
991 len = 0;
992 for (a = 0; a < size; a++) len += vec[a] * vec[a];
993 len = sqrt(len);
994 for (a = 0; a < size; a++) vec[a] /= len;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100995 for (a = 0; a < N; a++) best[a].activation = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100996 for (c = from; c < upto; c++) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200997 if(garbage && garbage[c]) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200998 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100999// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +01001000// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
1001// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +02001002 dist = 0;
1003 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +01001004 if(dist > worstbest) {
1005 for (a = 0; a < N; a++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001006 if (dist > best[a].activation) {
1007 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
1008 best[a].activation = dist;
1009 best[a].wordi = c;
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +01001010 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +02001011 }
Marc Kupietzdc22b982015-10-09 09:19:34 +02001012 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001013 worstbest = best[N-1].activation;
Marc Kupietzdc22b982015-10-09 09:19:34 +02001014 }
1015 }
Marc Kupietz34020dc2016-02-25 08:44:19 +01001016
Marc Kupietz44bee3c2016-02-25 16:26:29 +01001017end:
Marc Kupietz000ad862016-02-26 14:59:12 +01001018 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +02001019}
1020
Marc Kupietze0e42132017-11-24 16:44:34 +01001021int cmp_activation (const void * a, const void * b) {
1022 float fb = ((collocator *)a)->activation;
1023 float fa = ((collocator *)b)->activation;
1024 return (fa > fb) - (fa < fb);
1025}
1026
1027int cmp_probability (const void * a, const void * b) {
1028 float fb = ((collocator *)a)->probability;
1029 float fa = ((collocator *)b)->probability;
1030 return (fa > fb) - (fa < fb);
1031}
1032
Marc Kupietz33c79d32019-08-02 15:11:23 +02001033char* getPosWiseW2VCollocatorsAsTsv(char *word, long maxPerPos, long cutoff, float threshold) {
1034 HV *result = newHV();
1035 float *target_sums=NULL, vec[max_size];
1036 long long old_words;
1037 long a, b, c, d;
1038 knn *para_nbs[MAX_THREADS];
1039 knn *syn_nbs[MAX_THREADS];
1040 knnpars pars[MAX_THREADS];
1041 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
1042 wordlist *wl;
1043 int syn_threads = (M2? window * 2 : 0);
1044 int search_backw = 0;
1045 collocator *best = NULL;
1046 posix_memalign((void **) &best, 128, 10 * (maxPerPos>=200? maxPerPos : 200) * sizeof(collocator));
1047 memset(best, 0, (maxPerPos>=200? maxPerPos : 200) * sizeof(collocator));
1048
1049
1050 if(cutoff < 1 || cutoff > words)
1051 cutoff=words;
1052
1053 wl = getTargetWords(word, search_backw);
1054 if(wl == NULL || wl->length < 1)
1055 return "";
1056
1057 a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float));
1058 memset(target_sums, 0, cutoff * sizeof(float));
1059
1060 printf("Starting %d threads\n", syn_threads);
1061 fflush(stdout);
1062 for(a=0; a < syn_threads; a++) {
1063 pars[a].cutoff = cutoff;
1064 pars[a].target_sums = target_sums;
1065 pars[a].window_sums = window_sums;
1066 pars[a].wl = wl;
1067 pars[a].N = maxPerPos;
1068 pars[a].threshold = threshold;
1069 pars[a].from = a;
1070 pars[a].upto = a+1;
1071 pthread_create(&pt[a], NULL, getCollocators, (void *) &pars[a]);
1072 }
1073 printf("Waiting for syn threads to join\n");
1074 fflush(stdout);
1075 for (a = 0; a < syn_threads; a++) pthread_join(pt[a], (void *) &syn_nbs[a]);
1076 printf("Syn threads joint\n");
1077 fflush(stdout);
1078 result = malloc(maxPerPos*80*syn_threads);
1079 char *p = result;
1080 *p = 0;
1081 for (a = syn_threads -1; a >= 0; a--) {
1082 for (b=0; b < syn_nbs[a]->length; b++) {
1083 p += sprintf(p, "%ld\t%s\t%f\n", syn_nbs[a]->best[b].position, &vocab[syn_nbs[a]->best[b].wordi * max_w], syn_nbs[a]->best[b].activation);
1084 }
1085 }
1086 return(result);
1087}
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001088
Marc Kupietzac707b32018-12-20 11:36:38 +01001089SV *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 +01001090 HV *result = newHV();
Marc Kupietz7631e022019-01-25 12:04:42 +01001091 float *target_sums=NULL, vec[max_size];
Marc Kupietza5f60042017-05-04 10:38:12 +02001092 long long old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001093 long a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +01001094 knn *para_nbs[MAX_THREADS];
1095 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +01001096 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001097 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +01001098 wordlist *wl;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001099 int syn_threads = (M2? window * 2 : 0);
Marc Kupietzac707b32018-12-20 11:36:38 +01001100 int para_threads = (no_similar_profiles? 0 : num_threads - syn_threads);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001101
Marc Kupietz7631e022019-01-25 12:04:42 +01001102 collocator *best = NULL;
1103 posix_memalign((void **) &best, 128, 10 * (N>=200? N : 200) * sizeof(collocator));
1104 memset(best, 0, (N>=200? N : 200) * sizeof(collocator));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001105
Marc Kupietz000ad862016-02-26 14:59:12 +01001106 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
1107
Marc Kupietzd5d05732018-01-30 11:57:35 +01001108 if(cutoff < 1 || cutoff > words)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +01001109 cutoff=words;
1110
Marc Kupietza2e64502016-04-27 09:53:51 +02001111 wl = getTargetWords(st1, search_backw);
Marc Kupietzf9ac54e2017-11-21 09:22:29 +01001112 if(wl == NULL || wl->length < 1)
Marc Kupietz271e2a42016-03-22 11:37:43 +01001113 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +01001114
Marc Kupietz2c79c5e2017-11-09 16:18:40 +01001115 old_words = cutoff;
Marc Kupietz3082fd02019-01-09 14:54:06 +01001116 slice = cutoff / para_threads;
Marc Kupietza5f60042017-05-04 10:38:12 +02001117
Marc Kupietz2c79c5e2017-11-09 16:18:40 +01001118 a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float));
Marc Kupietz7631e022019-01-25 12:04:42 +01001119 memset(target_sums, 0, cutoff * sizeof(float));
Marc Kupietzce3d4c62016-03-23 16:11:25 +01001120
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001121 printf("Starting %d threads\n", para_threads);
1122 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001123 for(a=0; a < para_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +01001124 pars[a].cutoff = cutoff;
Marc Kupietz000ad862016-02-26 14:59:12 +01001125 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +01001126 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +01001127 pars[a].N = N;
Marc Kupietzc55c8872017-12-01 23:27:10 +01001128 pars[a].best = &best[N*a];
Marc Kupietz3082fd02019-01-09 14:54:06 +01001129 if(merge_words == 0 || search_backw == 0) {
Marc Kupietz1f0f7802019-01-03 16:23:29 +01001130 pars[a].from = a*slice;
1131 pars[a].upto = ((a+1)*slice > cutoff? cutoff : (a+1) * slice);
1132 } else {
Marc Kupietz3082fd02019-01-09 14:54:06 +01001133 pars[a].from = merge_words + a * slice;
1134 pars[a].upto = merge_words + ((a+1)*slice > cutoff? cutoff : (a+1) * slice);
Marc Kupietz1f0f7802019-01-03 16:23:29 +01001135 }
1136 printf("From: %ld, Upto: %ld\n", pars[a].from, pars[a].upto);
Marc Kupietz000ad862016-02-26 14:59:12 +01001137 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
1138 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001139 if(M2) {
1140 for(a=0; a < syn_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +01001141 pars[a + para_threads].cutoff = cutoff;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001142 pars[a + para_threads].target_sums = target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001143 pars[a + para_threads].window_sums = window_sums;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001144 pars[a + para_threads].wl = wl;
1145 pars[a + para_threads].N = N;
Marc Kupietz3c0fab12019-08-02 15:07:47 +02001146 pars[a + para_threads].threshold = MIN_RESP;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001147 pars[a + para_threads].from = a;
1148 pars[a + para_threads].upto = a+1;
1149 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
1150 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001151 }
1152 printf("Waiting for para threads to join\n");
1153 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001154 for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *) &para_nbs[a]);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001155 printf("Para threads joint\n");
1156 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +01001157
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001158 /* if(!syn_nbs[0]) */
1159 /* goto end; */
Marc Kupietz000ad862016-02-26 14:59:12 +01001160
Marc Kupietzc55c8872017-12-01 23:27:10 +01001161 qsort(best, N*para_threads, sizeof(collocator), cmp_activation);
Marc Kupietz000ad862016-02-26 14:59:12 +01001162
Marc Kupietz000ad862016-02-26 14:59:12 +01001163
Marc Kupietzd0d3e912017-12-01 22:09:43 +01001164 long long chosen[MAX_NEIGHBOURS];
1165 printf("N: %ld\n", N);
1166
Marc Kupietz271e2a42016-03-22 11:37:43 +01001167 AV* array = newAV();
Marc Kupietzd91212f2017-11-13 10:05:09 +01001168 int i, j;
Marc Kupietza5f60042017-05-04 10:38:12 +02001169 int l1_words=0, l2_words=0;
Marc Kupietzd0d3e912017-12-01 22:09:43 +01001170
1171 for (a = 0, i = 0; i < N && a < N*para_threads; a++) {
Marc Kupietzd91212f2017-11-13 10:05:09 +01001172 int filtered=0;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001173 long long c = best[a].wordi;
Marc Kupietze8b9dd22019-01-03 17:55:59 +01001174 if ((merge_words && dedupe && i > 1) || (!merge_words && dedupe && i > 0)) {
Marc Kupietzd0d3e912017-12-01 22:09:43 +01001175 for (j=0; j<i && !filtered; j++)
1176 if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) ||
1177 strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) {
1178 printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]);
Marc Kupietzd91212f2017-11-13 10:05:09 +01001179 filtered = 1;
1180 }
1181 if(filtered)
1182 continue;
1183 }
Marc Kupietz3082fd02019-01-09 14:54:06 +01001184
1185
1186 if(0 && merge_words > 0) {
Marc Kupietza5f60042017-05-04 10:38:12 +02001187 if(c >= merge_words) {
1188 if(l1_words > N / 2)
1189 continue;
1190 else
1191 l1_words++;
1192 } else {
1193 if(l2_words > N / 2)
1194 continue;
1195 else
1196 l2_words++;
1197 }
1198 }
Marc Kupietz3082fd02019-01-09 14:54:06 +01001199
1200// printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
1201// fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001202 HV* hash = newHV();
Marc Kupietza5f60042017-05-04 10:38:12 +02001203 SV* word = newSVpvf(&vocab[c * max_w], 0);
Marc Kupietzd0d3e912017-12-01 22:09:43 +01001204 chosen[i] = c;
Marc Kupietz271e2a42016-03-22 11:37:43 +01001205 if(latin_enc == 0) SvUTF8_on(word);
Marc Kupietza5f60042017-05-04 10:38:12 +02001206 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001207 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001208 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
1209 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001210 AV *vector = newAV();
1211 for (b = 0; b < size; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001212 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001213 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001214 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
1215 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietza5f60042017-05-04 10:38:12 +02001216 i++;
Marc Kupietz271e2a42016-03-22 11:37:43 +01001217 }
1218 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz7631e022019-01-25 12:04:42 +01001219
Marc Kupietz50485ba2016-03-23 09:13:14 +01001220 for(b=0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001221 best[b].wordi = -1L;
1222 best[b].activation = 0;
1223 best[b].probability = 0;
1224 best[b].position = 0;
1225 best[b].activation_sum = 0;
Marc Kupietz7631e022019-01-25 12:04:42 +01001226 memset(best[b].heat, 0, sizeof(float)*16);
Marc Kupietz50485ba2016-03-23 09:13:14 +01001227 }
1228
Marc Kupietza77acce2017-11-30 16:59:07 +01001229 float total_activation = 0;
1230
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001231 if (M2) {
1232 printf("Waiting for syn threads to join\n");
1233 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001234 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], (void *) &syn_nbs[a]);
Marc Kupietza77acce2017-11-30 16:59:07 +01001235 for (a = 0; a <= syn_threads; a++) {
Marc Kupietz63872182018-03-12 09:44:49 +01001236 if(a == window) continue;
Marc Kupietza77acce2017-11-30 16:59:07 +01001237 total_activation += window_sums[a];
1238 printf("window pos: %d, sum: %f\n", a, window_sums[a]);
1239 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001240 printf("syn threads joint\n");
1241 fflush(stdout);
Marc Kupietz50485ba2016-03-23 09:13:14 +01001242
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001243 for(b=0; b < syn_nbs[0]->length; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001244 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
1245 best[b].position = -1; // syn_nbs[0]->pos[b];
1246 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001247 best[b].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +01001248 best[b].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001249 best[b].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +01001250 best[b].cprobability = syn_nbs[0]->best[b].cprobability;
Marc Kupietz7631e022019-01-25 12:04:42 +01001251 memset(best[b].heat, 0, sizeof(float)*16);
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001252 }
Marc Kupietz30ca4342017-11-22 21:21:20 +01001253
1254 float best_window_sum[MAX_NEIGHBOURS];
1255 int found_index=0, i=0, j, w;
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001256 for(a=0; a < syn_threads; a++) {
1257 for(b=0; b < syn_nbs[a]->length; b++) {
1258 for(i=0; i < found_index; i++)
1259 if(best[i].wordi == syn_nbs[a]->best[b].wordi)
1260 break;
1261 if(i >= found_index) {
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001262 best[found_index].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +01001263 best[found_index].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001264 best[found_index].probability = 0.0;
Marc Kupietz7631e022019-01-25 12:04:42 +01001265 memset(best[found_index].heat, 0, sizeof(float)*16);
Marc Kupietza77acce2017-11-30 16:59:07 +01001266 best[found_index].cprobability = syn_nbs[a]->best[b].cprobability;
1267 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 +01001268 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
1269 // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
Marc Kupietz30ca4342017-11-22 21:21:20 +01001270 }
1271 }
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001272 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001273 sort_by =0; // ALWAYS AUTO-FOCUS
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001274 if(sort_by != 1 && sort_by != 2) { // sort by auto focus mean
Marc Kupietz30ca4342017-11-22 21:21:20 +01001275 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) -1);
Marc Kupietzbd41da32017-11-24 10:26:43 +01001276 int wpos;
Marc Kupietz4116b432017-12-06 14:15:32 +01001277 int bits_set = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001278 for(i=0; i < found_index; i++) {
Marc Kupietz4116b432017-12-06 14:15:32 +01001279 best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001280 for(w=1; w < (1 << syn_threads); w++) { // loop through all possible windows
Marc Kupietz4116b432017-12-06 14:15:32 +01001281 float word_window_sum = 0, word_window_average=0, word_cprobability_sum=0, word_activation_sum = 0, total_window_sum = 0;
1282 bits_set = 0;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001283 for(a=0; a < syn_threads; a++) {
Marc Kupietz30ca4342017-11-22 21:21:20 +01001284 if((1 << a) & w) {
Marc Kupietzbd41da32017-11-24 10:26:43 +01001285 wpos = (a >= window? a+1 : a);
1286 total_window_sum += window_sums[wpos];
Marc Kupietz30ca4342017-11-22 21:21:20 +01001287 }
1288 }
Marc Kupietzbd41da32017-11-24 10:26:43 +01001289// printf("%d window-sum %f\n", w, total_window_sum);
1290 for(a=0; a < syn_threads; a++) {
1291 if((1 << a) & w) {
1292 wpos = (a >= window? a+1 : a);
1293 bits_set++;
1294 for(b=0; b < syn_nbs[a]->length; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001295 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001296// float acti = syn_nbs[a]->best[b].activation / total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001297// 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 +01001298// word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1299// 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 +01001300
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001301 word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
Marc Kupietza77acce2017-11-30 16:59:07 +01001302// word_window_sum += acti - (word_window_sum * acti); syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1303
Marc Kupietz4116b432017-12-06 14:15:32 +01001304 word_window_average += syn_nbs[a]->best[b].activation; // - word_window_average * syn_nbs[a]->best[b].activation; // conormalied activation sum
1305 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 +01001306 word_activation_sum += syn_nbs[a]->best[b].activation;
1307 if(syn_nbs[a]->best[b].activation > best[i].max_activation)
1308 best[i].max_activation = syn_nbs[a]->best[b].activation;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +01001309 if(syn_nbs[a]->best[b].activation > best[i].heat[wpos] )
Marc Kupietz728b8ed2017-12-02 11:13:49 +01001310 best[i].heat[wpos] = syn_nbs[a]->best[b].activation;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001311 }
1312 }
1313 }
Marc Kupietz4116b432017-12-06 14:15:32 +01001314 if(bits_set) {
1315 word_window_average /= bits_set;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001316// word_activation_sum /= bits_set;
1317// word_window_sum /= bits_set;
Marc Kupietz4116b432017-12-06 14:15:32 +01001318 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001319
1320 word_window_sum /= total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001321
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001322 if(word_window_sum > best[i].probability) {
Marc Kupietz4116b432017-12-06 14:15:32 +01001323// best[i].position = w;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001324 best[i].probability = word_window_sum;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001325 }
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001326
Marc Kupietz4116b432017-12-06 14:15:32 +01001327 if(word_cprobability_sum > best[i].cprobability_sum) {
1328 best[i].position = w;
1329 best[i].cprobability_sum = word_cprobability_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001330 }
Marc Kupietz4116b432017-12-06 14:15:32 +01001331
1332 best[i].average = word_window_average;
1333// best[i].activation = word_activation_sum;
1334 }
Marc Kupietz30ca4342017-11-22 21:21:20 +01001335 }
Marc Kupietze0e42132017-11-24 16:44:34 +01001336 qsort(best, found_index, sizeof(collocator), cmp_probability);
Marc Kupietz30ca4342017-11-22 21:21:20 +01001337// for(i=0; i < found_index; i++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001338// 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 +01001339// }
1340
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001341 } else if(sort_by == 1) { // responsiveness any window position
1342 int wpos;
1343 for(i=0; i < found_index; i++) {
1344 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
1345 for(a=0; a < syn_threads; a++) {
1346 wpos = (a >= window? a+1 : a);
1347 for(b=0; b < syn_nbs[a]->length; b++)
1348 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
1349 best[i].probability += syn_nbs[a]->best[b].probability;
1350 if(syn_nbs[a]->best[b].activation > 0.25)
1351 best[i].position |= 1 << wpos;
1352 if(syn_nbs[a]->best[b].activation > best[i].activation) {
1353 best[i].activation = syn_nbs[a]->best[b].activation;
1354 }
1355 }
1356 }
1357 }
1358 qsort(best, found_index, sizeof(collocator), cmp_activation);
1359 } else if(sort_by == 2) { // single window position
Marc Kupietz30ca4342017-11-22 21:21:20 +01001360 for(a=1; a < syn_threads; a++) {
1361 for(b=0; b < syn_nbs[a]->length; b++) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001362 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001363 if(syn_nbs[a]->best[b].activation > best[c].activation) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001364 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001365 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001366 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001367 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
1368 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 +02001369 break;
Marc Kupietz6d9a6782016-03-23 17:25:25 +01001370 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001371 }
1372 }
1373 }
1374 } else { // sort by mean p
1375 for(a=1; a < syn_threads; a++) {
1376 for(b=0; b < syn_nbs[a]->length; b++) {
1377 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001378 if(target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001379 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001380 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001381 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001382 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
1383 best[c].position = (1 << 2*window) - 1; // syn_nbs[a]->pos[b];
1384 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001385 break;
1386 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001387 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +01001388 }
1389 }
1390 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001391 array = newAV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001392 for (a = 0, i=0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
1393 long long c = best[a].wordi;
Marc Kupietza77acce2017-11-30 16:59:07 +01001394/*
Marc Kupietzd91212f2017-11-13 10:05:09 +01001395 if (dedupe) {
1396 int filtered=0;
1397 for (j=0; j<i; j++)
1398 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
1399 strcasestr(chosen[j], &vocab[c * max_w])) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001400 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
1401 filtered = 1;
1402 }
Marc Kupietzd91212f2017-11-13 10:05:09 +01001403 if(filtered)
1404 continue;
1405 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001406*/
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001407 chosen[i++]=c;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001408 HV* hash = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001409 SV* word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001410 AV* heat = newAV();
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001411 if(latin_enc == 0) SvUTF8_on(word);
1412 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001413 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001414 hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001415 hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001416 hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001417 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 +01001418 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 +01001419 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001420 best[a].heat[5]=0;
1421 for(i=10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i]));
1422 hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV*)heat), 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001423 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietz271e2a42016-03-22 11:37:43 +01001424 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001425 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001426 }
Marc Kupietz000ad862016-02-26 14:59:12 +01001427end:
Marc Kupietz257a93d2019-01-03 17:55:22 +01001428 // words = old_words; // why was this here?
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001429 free(best);
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001430 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +01001431}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +01001432
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001433int dump_vecs(char *fname) {
1434 long i, j;
1435 FILE *f;
Marc Kupietz0a68a792018-12-20 11:41:45 +01001436 if(words>100000)
1437 words=100000;
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001438
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001439 if((f=fopen(fname, "w")) == NULL) {
1440 fprintf(stderr, "cannot open %s for writing\n", fname);
1441 return(-1);
1442 }
1443 fprintf(f, "%lld %lld\n", words, size);
1444 for (i=0; i < words; i++) {
1445 fprintf(f, "%s ", &vocab[i * max_w]);
1446 for(j=0; j < size - 1; j++)
1447 fprintf(f, "%f ", M[i*size + j]);
1448 fprintf(f, "%f\n", M[i*size + j]);
1449 }
1450 fclose(f);
1451 return(0);
1452}
Marc Kupietzdc22b982015-10-09 09:19:34 +02001453
Marc Kupietzfa194262018-06-05 09:39:32 +02001454int dump_for_numpy(char *fname) {
1455 long i, j;
1456 FILE *f;
Marc Kupietz1233eda2019-03-06 14:16:43 +01001457 int max;
1458 if(merged_end > 0)
1459 max = 150000;
1460 else
1461 max = 300000;
1462
1463 if(words>300000)
1464 words=300000;
Marc Kupietzfa194262018-06-05 09:39:32 +02001465
1466 if((f=fopen(fname, "w")) == NULL) {
1467 fprintf(stderr, "cannot open %s for writing\n", fname);
1468 return(-1);
1469 }
Marc Kupietz1233eda2019-03-06 14:16:43 +01001470 for (i=0; i < max; i++) {
Marc Kupietzfa194262018-06-05 09:39:32 +02001471 for(j=0; j < size - 1; j++)
1472 fprintf(f, "%f\t", M[i*size + j]);
1473 fprintf(f, "%f\n", M[i*size + j]);
Marc Kupietz1233eda2019-03-06 14:16:43 +01001474 printf("%s\n", &vocab[i * max_w]);
1475
1476 if(merged_end > 0) {
1477 for(j=0; j < size - 1; j++)
1478 fprintf(f, "%f\t", M[(merged_end + i)*size + j]);
1479 fprintf(f, "%f\n", M[(merged_end + i)*size + j]);
1480 printf("_%s\n", &vocab[i * max_w]);
1481 }
Marc Kupietzfa194262018-06-05 09:39:32 +02001482 }
1483 fclose(f);
Marc Kupietz7631e022019-01-25 12:04:42 +01001484 return(0);
Marc Kupietzfa194262018-06-05 09:39:32 +02001485}