blob: 2ec8c400bc58ba02a76f2c955df263d2a4dcc7d3 [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
123 open PIPE, "GET http://compute:5673/getClassicCollocators?w=$word |" ;
124 }
Marc Kupietz19c68242018-03-12 09:42:21 +0100125 if(!$cccache{$word}) {
Marc Kupietz06d61292019-02-04 12:33:22 +0100126 $c->app->log->info("Getting classic collocates of $word.");
Marc Kupietz19c68242018-03-12 09:42:21 +0100127 $cccache{$word} = getClassicCollocators($word);
Marc Kupietz1d96a082019-02-18 09:29:06 +0100128 $cccache{$word} =~ s/:(-?)(nan|inf)/:"${1}${2}"/g;
Marc Kupietz19c68242018-03-12 09:42:21 +0100129 } else {
Marc Kupietz06d61292019-02-04 12:33:22 +0100130 $c->app->log->info("Getting classic collocates for $word from cache.");
Marc Kupietz19c68242018-03-12 09:42:21 +0100131 }
Marc Kupietz81aeed22019-02-17 21:22:45 +0100132 if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100133 while(<PIPE>) {
134 $s2 .= $_;
135 }
136 close(PIPE);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100137 }
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100138
Marc Kupietz81aeed22019-02-17 21:22:45 +0100139 if(length($s2) > 2000) {
140 my $d1 = decode_json($cccache{$word});
141 my $d2 = decode_json($s2);
142 my %d2ld;
Marc Kupietz001bffd2019-02-21 08:52:41 +0100143 my $minLd = 14;
Marc Kupietz81aeed22019-02-17 21:22:45 +0100144 foreach my $i (@{$d2->{collocates}}) {
145 $d2ld{$i->{word}}=$i->{ld};
Marc Kupietz001bffd2019-02-21 08:52:41 +0100146 $minLd=$i->{ld} if($i->{ld} < $minLd);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100147 }
148 foreach my $i (@{$d1->{collocates}}) {
149 my $w = $i->{word};
Marc Kupietz001bffd2019-02-21 08:52:41 +0100150 $i->{delta} = $i->{ld} - (defined $d2ld{$w} ? $d2ld{$w} : $minLd-0.1);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100151 }
152 return(encode_json($d1));
153 } else {
154 my $d1 = decode_json($cccache{$word});
155 foreach my $i (@{$d1->{collocates}}) {
156 $i->{delta} = 0;
157 }
158 return(encode_json($d1));
159 }
Marc Kupietz19c68242018-03-12 09:42:21 +0100160}
161
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100162sub getSimilarProfilesCached {
163 my ($c, $word) = @_;
164 if(!$spcache{$word}) {
165 $spcache{$word} = getSimilarProfiles($word);
166 } else {
167 $c->app->log->info("Getting similar profiles for $word from cache:");
168 print $spcache{$word};
169 }
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 Kupietzdf3d4b52017-11-29 16:57:27 +0100209get '*/img/*' => sub {
210 my $c = shift;
211 my $url = $c->req->url;
212 $url =~ s@/derekovecs@@g;
213 $c->app->log->info("GET: " . $url);
214 $c->reply->static($url);
215};
216
Marc Kupietzdc22b982015-10-09 09:19:34 +0200217get '/' => sub {
218 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +0200219 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200220 my $word=$c->param('word');
Marc Kupietz2da2a812019-02-21 14:17:35 +0100221 my $no_nbs=$c->param('n') || ($opt_m? 50 : 100);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100222 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +0100223 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100224 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100225 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200226 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100227 my $sort=$c->param('sort') || 0;
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100228 my $csv=$c->param('csv') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +0200229 my $json=$c->param('json') || 0;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100230 my $cutoff=$c->param('cutoff') || 500000;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100231 my $dedupe=$c->param('dedupe') || 0;
Marc Kupietzac707b32018-12-20 11:36:38 +0100232 my $nosp=$c->param('nosp') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100233 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100234 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100235 my @collocations;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100236 if(defined($word) && $word !~ /^\s*$/) {
237 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100238 $word =~ s/\s+/ /g;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100239 if($opt_m && $word !~ /\|/) {
240 $word .= "|$word";
241 }
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100242 for my $w (split(' *\| *', $word)) {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100243 if($opt_m) {
244 if($searchBaseVocabFirst) {
245 $searchBaseVocabFirst=0;
246 } else {
247 $searchBaseVocabFirst=1;
248 }
249 }
250 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe,$searchBaseVocabFirst}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100251 $c->app->log->info("Getting $w results from cache");
Marc Kupietz3082fd02019-01-09 14:54:06 +0100252 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe.$searchBaseVocabFirst}
Marc Kupietza5b90152016-03-15 17:39:19 +0100253 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100254 $c->app->log->info('Looking for neighbours of '.$w);
255 if($opt_i) {
Marc Kupietzac707b32018-12-20 11:36:38 +0100256 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100257 } else {
Marc Kupietzac707b32018-12-20 11:36:38 +0100258 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100259 }
Marc Kupietz2dd2dd72017-12-01 22:08:14 +0100260 $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100261 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100262 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100263 }
264 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100265 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200266 if($json) {
267 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100268 } elsif($csv) {
269 my $csv_data="";
270 for (my $i=0; $i <= $no_nbs; $i++) {
271 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
272 }
273 for (my $i=0; $i < $no_nbs; $i++) {
274 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
275 }
276 chop $csv_data;
277 chop $csv_data;
278 $csv_data .= "\n";
279 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200280 } else {
Marc Kupietzd7760b42019-02-21 09:01:44 +0100281 my $distantWords="";
282 if(!defined($word) || $word !~ /^\s*$/) {
283 $distantWords = getBiggestMergedDifferences();
284 }
285 $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 +0200286 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200287};
288
Marc Kupietz30ca4342017-11-22 21:21:20 +0100289helper(bitvec2window => sub {
290 my ($self, $n) = @_;
291 my $str = unpack("B32", pack("N", $n));
292 $str =~ s/^\d{22}//;
293 $str =~ s/^(\d{5})/$1x/;
294 $str =~ s/0/·/g;
295 $str =~ s/1/+/g;
296 return $str;
297 });
298
Marc Kupietza5b90152016-03-15 17:39:19 +0100299$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200300
301exit;
302
303__END__
304
305__C__
306#include <stdio.h>
307#include <string.h>
308#include <math.h>
309#include <malloc.h>
310#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +0100311#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +0100312#include <pthread.h>
Marc Kupietze243efd2018-01-11 22:19:24 +0100313#include <collocatordb.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200314
315#define max_size 2000
316#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100317#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100318#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +0100319#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100320#define MAX_CC 50
321#define EXP_TABLE_SIZE 1000
322#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +0100323#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +0200324
325//the thread function
326void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +0100327
328typedef struct {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100329 long long wordi;
330 long position;
331 float activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100332 float average;
Marc Kupietza77acce2017-11-30 16:59:07 +0100333 float cprobability; // column wise probability
Marc Kupietz4116b432017-12-06 14:15:32 +0100334 float cprobability_sum;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100335 float probability;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100336 float activation_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100337 float max_activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100338 float heat[16];
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100339} collocator;
340
341typedef struct {
342 collocator *best;
Marc Kupietz80abb442016-03-23 21:04:08 +0100343 int length;
Marc Kupietz000ad862016-02-26 14:59:12 +0100344} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100345
Marc Kupietz000ad862016-02-26 14:59:12 +0100346typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +0100347 long long wordi[MAX_NEIGHBOURS];
348 char sep[MAX_NEIGHBOURS];
349 int length;
350} wordlist;
351
352typedef struct {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100353 long cutoff;
Marc Kupietz48c29682016-03-19 11:30:43 +0100354 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100355 char *token;
356 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100357 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100358 unsigned long upto;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100359 collocator *best;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100360 float *target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100361 float *window_sums;
Marc Kupietz000ad862016-02-26 14:59:12 +0100362} knnpars;
363
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100364typedef struct {
365 uint32_t index;
366 float value;
367} sparse_t;
368
369typedef struct {
370 uint32_t len;
371 sparse_t nbr[100];
372} profile_t;
373
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200374float *M, *M2=0L, *syn1neg_window, *expTable;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100375float *window_sums;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200376char *vocab;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200377char *garbage = NULL;
Marc Kupietze243efd2018-01-11 22:19:24 +0100378COLLOCATORDB *cdb = NULL;
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100379profile_t *sprofiles = NULL;
380size_t sprofiles_qty = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100381
Marc Kupietza2e64502016-04-27 09:53:51 +0200382long long words, size, merged_end;
Marc Kupietza5f60042017-05-04 10:38:12 +0200383long long merge_words = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100384int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100385int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100386int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200387
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100388/* load collocation profiles if file exists */
389int load_sprofiles(char *vecsname) {
390 char *basename = strdup(vecsname);
391 char *pos = strstr(basename, ".vecs");
392 if(pos)
393 *pos=0;
394
395 char binsprofiles_fname[256];
396 strcpy(binsprofiles_fname, basename);
397 strcat(binsprofiles_fname, ".sprofiles.bin");
398 FILE *fp = fopen(binsprofiles_fname, "rb");
399 if (fp == NULL) {
400 printf("Collocation profiles %s not found. No problem.\n", binsprofiles_fname);
401 return 0;
402 }
403 fseek(fp, 0L, SEEK_END);
404 size_t sz = ftell(fp);
405 fclose(fp);
406
407 int fd = open(binsprofiles_fname, O_RDONLY);
408 sprofiles = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0);
409 if (sprofiles == MAP_FAILED) {
410 close(fd);
411 fprintf(stderr, "Cannot mmap %s\n", binsprofiles_fname);
412 sprofiles = NULL;
413 return 0;
414 } else {
415 sprofiles_qty = sz / sizeof(profile_t);
416 fprintf(stderr, "Successfully mmaped %s containing similar profiles for %ld word forms.\n", binsprofiles_fname, sprofiles_qty);
417 }
418 return 1;
419}
420
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100421int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100422 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100423 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100424 long long a, b, c, d, cn;
425 float len;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200426 double val;
Marc Kupietz82b02672016-02-26 12:32:25 +0100427
Marc Kupietz67c20282016-02-26 09:42:00 +0100428 char binvecs_fname[256], binwords_fname[256];
429 strcpy(binwords_fname, file_name);
430 strcat(binwords_fname, ".words");
431 strcpy(binvecs_fname, file_name);
432 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200433
Marc Kupietza5b90152016-03-15 17:39:19 +0100434 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200435 f = fopen(file_name, "rb");
436 if (f == NULL) {
437 printf("Input file %s not found\n", file_name);
438 return -1;
439 }
440 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100441 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200442 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100443 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
444 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100445 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
446 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
447 if (M == NULL) {
448 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
449 return -1;
450 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200451 if(strstr(file_name, ".txt")) {
452 for (b = 0; b < words; b++) {
453 a = 0;
454 while (1) {
455 vocab[b * max_w + a] = fgetc(f);
456 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
457 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
458 }
459 vocab[b * max_w + a] = 0;
460 len = 0;
461 for (a = 0; a < size; a++) {
462 fscanf(f, "%lf", &val);
463 M[a + b * size] = val;
464 len += val * val;
465 }
466 len = sqrt(len);
467 for (a = 0; a < size; a++) M[a + b * size] /= len;
468 }
469 } else {
470 for (b = 0; b < words; b++) {
471 a = 0;
472 while (1) {
473 vocab[b * max_w + a] = fgetc(f);
474 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
475 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
476 }
477 vocab[b * max_w + a] = 0;
478 fread(&M[b * size], sizeof(float), size, f);
479 len = 0;
480 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
481 len = sqrt(len);
482 for (a = 0; a < size; a++) M[a + b * size] /= len;
483 }
484 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100485 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
486 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
487 fclose(binvecs);
488 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
489 fclose(binwords);
490 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100491 }
492 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
493 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
494 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
495 if (M == MAP_FAILED || vocab == MAP_FAILED) {
496 close(binvecs_fd);
497 close(binwords_fd);
498 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
499 exit(-1);
500 }
501 } else {
502 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
503 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100504 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200505 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100506
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200507 if(net_name && strlen(net_name) > 0) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100508 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
509 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
510 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100511 // munmap(M, sizeof(float) * words * size);
512 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 +0200513 if (M2 == MAP_FAILED) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100514 close(net_fd);
515 fprintf(stderr, "Cannot mmap %s\n", net_name);
516 exit(-1);
517 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100518 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100519 } else {
520 fprintf(stderr, "Cannot open %s\n", net_name);
521 exit(-1);
522 }
523 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
Marc Kupietze243efd2018-01-11 22:19:24 +0100524
525 char collocatordb_name[2048];
526 strcpy(collocatordb_name, net_name);
527 char *ext = rindex(collocatordb_name, '.');
528 if(ext) {
529 strcpy(ext, ".rocksdb");
530 if(access(collocatordb_name, R_OK) == 0) {
531 *ext = 0;
532 fprintf(stderr, "Opening collocator DB %s\n", collocatordb_name);
533 cdb = open_collocatordb(collocatordb_name);
534 }
535 }
536 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100537
538 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
539 for (i = 0; i < EXP_TABLE_SIZE; i++) {
540 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
541 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
542 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100543 window_sums = malloc(sizeof(float) * (window+1) * 2);
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100544
545 return 0;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200546}
547
Marc Kupietza2e64502016-04-27 09:53:51 +0200548long mergeVectors(char *file_name){
549 FILE *f, *binvecs, *binwords;
550 int binwords_fd, binvecs_fd, net_fd, i;
551 long long a, b, c, d, cn;
552 float len;
553 float *merge_vecs;
554 char *merge_vocab;
Marc Kupietza5f60042017-05-04 10:38:12 +0200555 /* long long merge_words, merge_size; */
556 long long merge_size;
Marc Kupietza2e64502016-04-27 09:53:51 +0200557
558 char binvecs_fname[256], binwords_fname[256];
559 strcpy(binwords_fname, file_name);
560 strcat(binwords_fname, ".words");
561 strcpy(binvecs_fname, file_name);
562 strcat(binvecs_fname, ".vecs");
563
564 f = fopen(file_name, "rb");
565 if (f == NULL) {
566 printf("Input file %s not found\n", file_name);
567 exit -1;
568 }
569 fscanf(f, "%lld", &merge_words);
570 fscanf(f, "%lld", &merge_size);
571 if(merge_size != size){
572 fprintf(stderr, "vectors must have the same length\n");
573 exit(-1);
574 }
575 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
576 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
577 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
578 if (merge_vecs == NULL || merge_vocab == NULL) {
579 close(binvecs_fd);
580 close(binwords_fd);
581 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
582 exit(-1);
583 }
584 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
585 read(binwords_fd, merge_vocab, merge_words * max_w);
586 } else {
587 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
588 exit(-1);
589 }
590 printf("Successfully reallocated memory\nMerging...\n");
591 fflush(stdout);
592 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
593 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
594 munmap(M, words * size * sizeof(float));
595 munmap(vocab, words * max_w);
596 M = merge_vecs;
597 vocab = merge_vocab;
598 merged_end = merge_words;
599 words += merge_words;
600 fclose(f);
601 printf("merged_end: %lld, words: %lld\n", merged_end, words);
Marc Kupietzd7760b42019-02-21 09:01:44 +0100602 //printBiggestMergedDifferences();
Marc Kupietza2e64502016-04-27 09:53:51 +0200603 return((long) merged_end);
604}
605
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200606void filter_garbage() {
607 long i;
Marc Kupietzab591a82016-04-28 14:08:49 +0200608 unsigned char *w, previous, c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200609 garbage = malloc(words);
610 memset(garbage, 0, words);
611 for (i = 0; i < words; i++) {
612 w = vocab + i * max_w;
613 previous = 0;
Marc Kupietz969adfa2018-03-12 09:43:53 +0100614 if(strncmp("quot", w, 4) == 0) {
615 garbage[i]=1;
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100616// printf("Gargabe: %s\n", vocab + i * max_w);
Marc Kupietz969adfa2018-03-12 09:43:53 +0100617 } else {
618 while((c = *w++) && !garbage[i]) {
619 if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
620 (previous == '-' && (c & 32)) ||
621 (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) ||
622 (previous == 'q' && c == 'u' && *(w) == 'o' && *(w+1) == 't') || /* quot */
623 c == '<'
624 ) {
625 garbage[i]=1;
626 continue;
627 }
628 previous = c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200629 }
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200630 }
631 }
632 return;
633}
634
Marc Kupietzac707b32018-12-20 11:36:38 +0100635
636knn *simpleGetCollocators(int word, int number, long cutoff, int *result) {
637 knnpars *pars = calloc(sizeof(knnpars), 1);
638 float *target_sums;
639 float *window_sums = malloc(sizeof(float) * (window+1) * 2);
640 pars->cutoff = (cutoff? cutoff : 300000);
641 long a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
642 for(a = 0; a < cutoff; a++)
643 target_sums[a] = 0;
644 pars->target_sums = target_sums;
645 pars->window_sums = window_sums;
646 pars->N = (number? number : 20);
647 pars->from = 0;
648 pars->upto = window * 2 -1;
Marc Kupietz7631e022019-01-25 12:04:42 +0100649 knn *syn_nbs = NULL; // = (knn*) getCollocators(pars);
Marc Kupietzac707b32018-12-20 11:36:38 +0100650 free(pars);
651 free(window_sums);
652 free(target_sums);
653 return syn_nbs;
654}
655
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100656void *getCollocators(void *args) {
657 knnpars *pars = args;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100658 int N = pars->N;
Marc Kupietz52702832019-01-15 17:55:52 +0100659
Marc Kupietz271e2a42016-03-22 11:37:43 +0100660 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100661 knn *nbs = NULL;
662 long window_layer_size = size * window * 2;
663 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
664 float f, max_f, maxmax_f;
Marc Kupietz7631e022019-01-25 12:04:42 +0100665 float *target_sums=NULL, worstbest, wpos_sum;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100666 collocator *best;
Marc Kupietzd5642582016-03-19 22:23:13 +0100667
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200668 if(M2 == NULL || cc == -1)
Marc Kupietzd5642582016-03-19 22:23:13 +0100669 return NULL;
670
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100671 a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietz7631e022019-01-25 12:04:42 +0100672 memset(target_sums, 0, pars->cutoff * sizeof(float));
673 best = malloc((N>200?N:200) * sizeof(collocator));
674 memset(best, 0, (N>200?N:200) * sizeof(collocator));
Marc Kupietz271e2a42016-03-22 11:37:43 +0100675 worstbest = MIN_RESP;
676
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100677 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100678 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100679 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100680 best[b].wordi = -1;
681 best[b].probability = 1;
682 best[b].activation = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100683 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100684
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100685 d = cc;
686 maxmax_f = -1;
687 maxmax_target = 0;
688
Marc Kupietz271e2a42016-03-22 11:37:43 +0100689 for (a = pars->from; a < pars->upto; a++) {
690 if(a >= window)
691 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100692 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100693 printf("window pos: %ld\n", a);
694 if (a != window) {
695 max_f = -1;
696 window_offset = a * size;
697 if (a > window)
698 window_offset -= size;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100699 for(target = 0; target < pars->cutoff; target ++) {
700 if(garbage && garbage[target]) continue;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100701 if(target == d)
702 continue;
703 f = 0;
704 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100705 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100706 if (f < -MAX_EXP)
707 continue;
708 else if (f > MAX_EXP)
709 continue;
710 else
711 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100712 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100713
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100714 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100715 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100716 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100717 if (f > best[b].activation) {
718 memmove(best + b + 1, best + b, (N - b -1) * sizeof(collocator));
719 best[b].activation = f;
720 best[b].wordi = target;
721 best[b].position = window-a;
Marc Kupietz33679a32016-03-22 08:49:39 +0100722 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100723 }
724 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100725 if(b == N - 1)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100726 worstbest = best[N-1].activation;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100727 }
728 }
729 printf("%d %.2f\n", max_target, max_f);
730 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
731 if(max_f > maxmax_f) {
732 maxmax_f = max_f;
733 maxmax_target = max_target;
734 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100735 for (b = 0; b < N; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100736 if(best[b].position == window-a)
Marc Kupietza77acce2017-11-30 16:59:07 +0100737 best[b].cprobability = best[b].activation / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100738 } else {
739 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
740 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100741 pars->window_sums[a] = wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100742 }
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100743 for (b = 0; b < pars->cutoff; b++)
Marc Kupietza77acce2017-11-30 16:59:07 +0100744 pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2);
745 printf("Target-Summe von 0: %f\n", pars->target_sums[150298]);
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100746 free(target_sums);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100747 for(b=0; b<N && best[b].wordi >= 0; b++);; // THIS LOOP IS NEEDED (b...)
748// 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 +0100749// printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100750 nbs = malloc(sizeof(knn));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100751 nbs->best = best;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100752 nbs->length = b-1;
753 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100754}
755
Marc Kupietz30ca4342017-11-22 21:21:20 +0100756
Marc Kupietz66bfd952017-12-11 09:59:45 +0100757AV *getVecs(AV *array) {
758 int i, b;
759 AV *result = newAV();
760 for (i=0; i<=av_len(array); i++) {
761 SV** elem = av_fetch(array, i, 0);
762 if (elem != NULL) {
763 long j = (long) SvNV(*elem);
764 AV *vector = newAV();
765 for (b = 0; b < size; b++) {
766 av_push(vector, newSVnv(M[b + j * size]));
767 }
768 av_push(result, newRV_noinc(vector));
769 }
770 }
771 return result;
772}
773
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100774char *getSimilarProfiles(long node) {
775 int i;
776 char buffer[120000];
777 char pair_buffer[2048];
778 buffer[0]='[';
779 buffer[1]=0;
780 if(node >= sprofiles_qty) {
781 printf("Not available in precomputed profile\n");
782 return(strdup("[{\"w\":\"not available\", \"v\":0}]\n"));
783 }
784
785 printf("******* %s ******\n", &vocab[max_w * node]);
786
787 for(i=0; i < 100 && i < sprofiles[node].len; i++) {
788 sprintf(pair_buffer, "{\"w\":\"%s\", \"v\":%f},", &vocab[max_w * (sprofiles[node].nbr[i].index)], sprofiles[node].nbr[i].value);
789 strcat(buffer, pair_buffer);
790 }
791 buffer[strlen(buffer)-1]=']';
792 strcat(buffer, "\n");
793 printf(buffer);
794 return(strdup(buffer));
795}
796
Marc Kupietze243efd2018-01-11 22:19:24 +0100797char *getClassicCollocators(long node) {
798 char *res = (cdb? strdup(get_collocators_as_json(cdb, node)) : "[]");
799 return res;
800}
801
Marc Kupietz0957d532019-01-03 17:54:00 +0100802wordlist *getTargetWords(char *st1, int search_backw) {
Marc Kupietz48c29682016-03-19 11:30:43 +0100803 wordlist *wl = malloc(sizeof(wordlist));
804 char st[100][max_size], sep[100];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100805 long a, b=0, c=0, cn=0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200806 int unmerged;
807
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100808 while (1) {
809 st[cn][b] = st1[c];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100810 b++;
811 c++;
812 st[cn][b] = 0;
813 if (st1[c] == 0) break;
814 if (st1[c] == ' ' || st1[c] == '-') {
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100815 sep[cn++] = st1[c];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100816 b = 0;
817 c++;
818 }
819 }
820 cn++;
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100821 for (a = 0; a < cn; a++) {
822 if (search_backw) {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100823 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 +0100824 } else {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100825 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 +0100826 }
827 if (b == words) b = -1;
828 wl->wordi[a] = b;
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100829 if (b == -1) {
830 fprintf(stderr, "Out of dictionary word!\n");
831 cn--;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100832 } else {
833 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 +0100834 }
835 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100836 wl->length=cn;
837 return(wl);
838}
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100839
Marc Kupietzd7760b42019-02-21 09:01:44 +0100840char *getBiggestMergedDifferences() {
841 static char *result = NULL;
Marc Kupietzebc131e2019-01-03 16:24:27 +0100842 float dist, len, vec[max_size];
843 long long a, b, c, d, cn, *bi;
844 char ch;
845 knn *nbs = NULL;
Marc Kupietzd7760b42019-02-21 09:01:44 +0100846 int N = 1000;
847
848 if(result != NULL)
849 return result;
Marc Kupietzebc131e2019-01-03 16:24:27 +0100850
851 printf("Looking for biggest distances between main and merged vectors ...\n");
852 collocator *best;
853 best = malloc(N * sizeof(collocator));
854 memset(best, 0, N * sizeof(collocator));
855
856 float worstbest=1000000;
857
858 for (a = 0; a < N; a++) best[a].activation = worstbest;
859
Marc Kupietz2da2a812019-02-21 14:17:35 +0100860 for (c = 0; c < 500000; c++) {
Marc Kupietzebc131e2019-01-03 16:24:27 +0100861 if(garbage && garbage[c]) continue;
862 a = 0;
863 dist = 0;
864 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + (c+merged_end) * size];
865 if(dist < worstbest) {
866 for (a = 0; a < N; a++) {
867 if (dist < best[a].activation) {
868 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
869 best[a].activation = dist;
870 best[a].wordi = c;
871 break;
872 }
873 }
874 worstbest = best[N-1].activation;
875 }
876 }
877
Marc Kupietzd7760b42019-02-21 09:01:44 +0100878 result = malloc(N*max_w);
879 char *p = result;
880 *p++ = '['; *p = 0;
Marc Kupietzebc131e2019-01-03 16:24:27 +0100881 for (a = 0; a < N; a++) {
Marc Kupietzd7760b42019-02-21 09:01:44 +0100882 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 +0100883 }
Marc Kupietzd7760b42019-02-21 09:01:44 +0100884 *--p = ']';
885 return(result);
Marc Kupietzebc131e2019-01-03 16:24:27 +0100886}
887
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100888void *_get_neighbours(void *arg) {
889 knnpars *pars = arg;
Marc Kupietz48c29682016-03-19 11:30:43 +0100890 char *st1 = pars->token;
891 int N = pars->N;
892 long from = pars -> from;
893 unsigned long upto = pars -> upto;
894 char file_name[max_size], st[100][max_size], *sep;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100895 float dist, len, vec[max_size];
896 long long a, b, c, d, cn, *bi;
Marc Kupietz48c29682016-03-19 11:30:43 +0100897 char ch;
898 knn *nbs = NULL;
899 wordlist *wl = pars->wl;
900
Marc Kupietzc55c8872017-12-01 23:27:10 +0100901 collocator *best = pars->best;
Marc Kupietz48c29682016-03-19 11:30:43 +0100902
903 float worstbest=-1;
904
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100905 for (a = 0; a < N; a++) best[a].activation = 0;
Marc Kupietz48c29682016-03-19 11:30:43 +0100906 a = 0;
907 bi = wl->wordi;
908 cn = wl->length;
909 sep = wl->sep;
910 b = bi[0];
911 c = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100912 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100913 N = 0;
914 goto end;
915 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200916 for (a = 0; a < size; a++) vec[a] = 0;
917 for (b = 0; b < cn; b++) {
918 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100919 if(b>0 && sep[b-1] == '-')
920 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
921 else
922 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200923 }
924 len = 0;
925 for (a = 0; a < size; a++) len += vec[a] * vec[a];
926 len = sqrt(len);
927 for (a = 0; a < size; a++) vec[a] /= len;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100928 for (a = 0; a < N; a++) best[a].activation = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100929 for (c = from; c < upto; c++) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200930 if(garbage && garbage[c]) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200931 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100932// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100933// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
934// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200935 dist = 0;
936 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100937 if(dist > worstbest) {
938 for (a = 0; a < N; a++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100939 if (dist > best[a].activation) {
940 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
941 best[a].activation = dist;
942 best[a].wordi = c;
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100943 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200944 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200945 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100946 worstbest = best[N-1].activation;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200947 }
948 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100949
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100950end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100951 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200952}
953
Marc Kupietze0e42132017-11-24 16:44:34 +0100954int cmp_activation (const void * a, const void * b) {
955 float fb = ((collocator *)a)->activation;
956 float fa = ((collocator *)b)->activation;
957 return (fa > fb) - (fa < fb);
958}
959
960int cmp_probability (const void * a, const void * b) {
961 float fb = ((collocator *)a)->probability;
962 float fa = ((collocator *)b)->probability;
963 return (fa > fb) - (fa < fb);
964}
965
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100966
Marc Kupietzac707b32018-12-20 11:36:38 +0100967SV *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 +0100968 HV *result = newHV();
Marc Kupietz7631e022019-01-25 12:04:42 +0100969 float *target_sums=NULL, vec[max_size];
Marc Kupietza5f60042017-05-04 10:38:12 +0200970 long long old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100971 long a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100972 knn *para_nbs[MAX_THREADS];
973 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100974 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100975 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100976 wordlist *wl;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200977 int syn_threads = (M2? window * 2 : 0);
Marc Kupietzac707b32018-12-20 11:36:38 +0100978 int para_threads = (no_similar_profiles? 0 : num_threads - syn_threads);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100979
Marc Kupietz7631e022019-01-25 12:04:42 +0100980 collocator *best = NULL;
981 posix_memalign((void **) &best, 128, 10 * (N>=200? N : 200) * sizeof(collocator));
982 memset(best, 0, (N>=200? N : 200) * sizeof(collocator));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100983
Marc Kupietz000ad862016-02-26 14:59:12 +0100984 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
985
Marc Kupietzd5d05732018-01-30 11:57:35 +0100986 if(cutoff < 1 || cutoff > words)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100987 cutoff=words;
988
Marc Kupietza2e64502016-04-27 09:53:51 +0200989 wl = getTargetWords(st1, search_backw);
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100990 if(wl == NULL || wl->length < 1)
Marc Kupietz271e2a42016-03-22 11:37:43 +0100991 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100992
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100993 old_words = cutoff;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100994 slice = cutoff / para_threads;
Marc Kupietza5f60042017-05-04 10:38:12 +0200995
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100996 a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float));
Marc Kupietz7631e022019-01-25 12:04:42 +0100997 memset(target_sums, 0, cutoff * sizeof(float));
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100998
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200999 printf("Starting %d threads\n", para_threads);
1000 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001001 for(a=0; a < para_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +01001002 pars[a].cutoff = cutoff;
Marc Kupietz000ad862016-02-26 14:59:12 +01001003 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +01001004 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +01001005 pars[a].N = N;
Marc Kupietzc55c8872017-12-01 23:27:10 +01001006 pars[a].best = &best[N*a];
Marc Kupietz3082fd02019-01-09 14:54:06 +01001007 if(merge_words == 0 || search_backw == 0) {
Marc Kupietz1f0f7802019-01-03 16:23:29 +01001008 pars[a].from = a*slice;
1009 pars[a].upto = ((a+1)*slice > cutoff? cutoff : (a+1) * slice);
1010 } else {
Marc Kupietz3082fd02019-01-09 14:54:06 +01001011 pars[a].from = merge_words + a * slice;
1012 pars[a].upto = merge_words + ((a+1)*slice > cutoff? cutoff : (a+1) * slice);
Marc Kupietz1f0f7802019-01-03 16:23:29 +01001013 }
1014 printf("From: %ld, Upto: %ld\n", pars[a].from, pars[a].upto);
Marc Kupietz000ad862016-02-26 14:59:12 +01001015 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
1016 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001017 if(M2) {
1018 for(a=0; a < syn_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +01001019 pars[a + para_threads].cutoff = cutoff;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001020 pars[a + para_threads].target_sums = target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001021 pars[a + para_threads].window_sums = window_sums;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001022 pars[a + para_threads].wl = wl;
1023 pars[a + para_threads].N = N;
1024 pars[a + para_threads].from = a;
1025 pars[a + para_threads].upto = a+1;
1026 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
1027 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001028 }
1029 printf("Waiting for para threads to join\n");
1030 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001031 for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *) &para_nbs[a]);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001032 printf("Para threads joint\n");
1033 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +01001034
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001035 /* if(!syn_nbs[0]) */
1036 /* goto end; */
Marc Kupietz000ad862016-02-26 14:59:12 +01001037
Marc Kupietzc55c8872017-12-01 23:27:10 +01001038 qsort(best, N*para_threads, sizeof(collocator), cmp_activation);
Marc Kupietz000ad862016-02-26 14:59:12 +01001039
Marc Kupietz000ad862016-02-26 14:59:12 +01001040
Marc Kupietzd0d3e912017-12-01 22:09:43 +01001041 long long chosen[MAX_NEIGHBOURS];
1042 printf("N: %ld\n", N);
1043
Marc Kupietz271e2a42016-03-22 11:37:43 +01001044 AV* array = newAV();
Marc Kupietzd91212f2017-11-13 10:05:09 +01001045 int i, j;
Marc Kupietza5f60042017-05-04 10:38:12 +02001046 int l1_words=0, l2_words=0;
Marc Kupietzd0d3e912017-12-01 22:09:43 +01001047
1048 for (a = 0, i = 0; i < N && a < N*para_threads; a++) {
Marc Kupietzd91212f2017-11-13 10:05:09 +01001049 int filtered=0;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001050 long long c = best[a].wordi;
Marc Kupietze8b9dd22019-01-03 17:55:59 +01001051 if ((merge_words && dedupe && i > 1) || (!merge_words && dedupe && i > 0)) {
Marc Kupietzd0d3e912017-12-01 22:09:43 +01001052 for (j=0; j<i && !filtered; j++)
1053 if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) ||
1054 strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) {
1055 printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]);
Marc Kupietzd91212f2017-11-13 10:05:09 +01001056 filtered = 1;
1057 }
1058 if(filtered)
1059 continue;
1060 }
Marc Kupietz3082fd02019-01-09 14:54:06 +01001061
1062
1063 if(0 && merge_words > 0) {
Marc Kupietza5f60042017-05-04 10:38:12 +02001064 if(c >= merge_words) {
1065 if(l1_words > N / 2)
1066 continue;
1067 else
1068 l1_words++;
1069 } else {
1070 if(l2_words > N / 2)
1071 continue;
1072 else
1073 l2_words++;
1074 }
1075 }
Marc Kupietz3082fd02019-01-09 14:54:06 +01001076
1077// printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
1078// fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001079 HV* hash = newHV();
Marc Kupietza5f60042017-05-04 10:38:12 +02001080 SV* word = newSVpvf(&vocab[c * max_w], 0);
Marc Kupietzd0d3e912017-12-01 22:09:43 +01001081 chosen[i] = c;
Marc Kupietz271e2a42016-03-22 11:37:43 +01001082 if(latin_enc == 0) SvUTF8_on(word);
Marc Kupietza5f60042017-05-04 10:38:12 +02001083 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001084 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001085 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
1086 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001087 AV *vector = newAV();
1088 for (b = 0; b < size; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001089 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001090 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001091 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
1092 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietza5f60042017-05-04 10:38:12 +02001093 i++;
Marc Kupietz271e2a42016-03-22 11:37:43 +01001094 }
1095 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz7631e022019-01-25 12:04:42 +01001096
Marc Kupietz50485ba2016-03-23 09:13:14 +01001097 for(b=0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001098 best[b].wordi = -1L;
1099 best[b].activation = 0;
1100 best[b].probability = 0;
1101 best[b].position = 0;
1102 best[b].activation_sum = 0;
Marc Kupietz7631e022019-01-25 12:04:42 +01001103 memset(best[b].heat, 0, sizeof(float)*16);
Marc Kupietz50485ba2016-03-23 09:13:14 +01001104 }
1105
Marc Kupietza77acce2017-11-30 16:59:07 +01001106 float total_activation = 0;
1107
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001108 if (M2) {
1109 printf("Waiting for syn threads to join\n");
1110 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001111 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], (void *) &syn_nbs[a]);
Marc Kupietza77acce2017-11-30 16:59:07 +01001112 for (a = 0; a <= syn_threads; a++) {
Marc Kupietz63872182018-03-12 09:44:49 +01001113 if(a == window) continue;
Marc Kupietza77acce2017-11-30 16:59:07 +01001114 total_activation += window_sums[a];
1115 printf("window pos: %d, sum: %f\n", a, window_sums[a]);
1116 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001117 printf("syn threads joint\n");
1118 fflush(stdout);
Marc Kupietz50485ba2016-03-23 09:13:14 +01001119
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001120 for(b=0; b < syn_nbs[0]->length; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001121 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
1122 best[b].position = -1; // syn_nbs[0]->pos[b];
1123 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001124 best[b].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +01001125 best[b].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001126 best[b].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +01001127 best[b].cprobability = syn_nbs[0]->best[b].cprobability;
Marc Kupietz7631e022019-01-25 12:04:42 +01001128 memset(best[b].heat, 0, sizeof(float)*16);
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001129 }
Marc Kupietz30ca4342017-11-22 21:21:20 +01001130
1131 float best_window_sum[MAX_NEIGHBOURS];
1132 int found_index=0, i=0, j, w;
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001133 for(a=0; a < syn_threads; a++) {
1134 for(b=0; b < syn_nbs[a]->length; b++) {
1135 for(i=0; i < found_index; i++)
1136 if(best[i].wordi == syn_nbs[a]->best[b].wordi)
1137 break;
1138 if(i >= found_index) {
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001139 best[found_index].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +01001140 best[found_index].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001141 best[found_index].probability = 0.0;
Marc Kupietz7631e022019-01-25 12:04:42 +01001142 memset(best[found_index].heat, 0, sizeof(float)*16);
Marc Kupietza77acce2017-11-30 16:59:07 +01001143 best[found_index].cprobability = syn_nbs[a]->best[b].cprobability;
1144 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 +01001145 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
1146 // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
Marc Kupietz30ca4342017-11-22 21:21:20 +01001147 }
1148 }
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001149 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001150 sort_by =0; // ALWAYS AUTO-FOCUS
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001151 if(sort_by != 1 && sort_by != 2) { // sort by auto focus mean
Marc Kupietz30ca4342017-11-22 21:21:20 +01001152 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) -1);
Marc Kupietzbd41da32017-11-24 10:26:43 +01001153 int wpos;
Marc Kupietz4116b432017-12-06 14:15:32 +01001154 int bits_set = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001155 for(i=0; i < found_index; i++) {
Marc Kupietz4116b432017-12-06 14:15:32 +01001156 best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001157 for(w=1; w < (1 << syn_threads); w++) { // loop through all possible windows
Marc Kupietz4116b432017-12-06 14:15:32 +01001158 float word_window_sum = 0, word_window_average=0, word_cprobability_sum=0, word_activation_sum = 0, total_window_sum = 0;
1159 bits_set = 0;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001160 for(a=0; a < syn_threads; a++) {
Marc Kupietz30ca4342017-11-22 21:21:20 +01001161 if((1 << a) & w) {
Marc Kupietzbd41da32017-11-24 10:26:43 +01001162 wpos = (a >= window? a+1 : a);
1163 total_window_sum += window_sums[wpos];
Marc Kupietz30ca4342017-11-22 21:21:20 +01001164 }
1165 }
Marc Kupietzbd41da32017-11-24 10:26:43 +01001166// printf("%d window-sum %f\n", w, total_window_sum);
1167 for(a=0; a < syn_threads; a++) {
1168 if((1 << a) & w) {
1169 wpos = (a >= window? a+1 : a);
1170 bits_set++;
1171 for(b=0; b < syn_nbs[a]->length; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001172 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001173// float acti = syn_nbs[a]->best[b].activation / total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001174// 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 +01001175// word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1176// 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 +01001177
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001178 word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
Marc Kupietza77acce2017-11-30 16:59:07 +01001179// word_window_sum += acti - (word_window_sum * acti); syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1180
Marc Kupietz4116b432017-12-06 14:15:32 +01001181 word_window_average += syn_nbs[a]->best[b].activation; // - word_window_average * syn_nbs[a]->best[b].activation; // conormalied activation sum
1182 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 +01001183 word_activation_sum += syn_nbs[a]->best[b].activation;
1184 if(syn_nbs[a]->best[b].activation > best[i].max_activation)
1185 best[i].max_activation = syn_nbs[a]->best[b].activation;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +01001186 if(syn_nbs[a]->best[b].activation > best[i].heat[wpos] )
Marc Kupietz728b8ed2017-12-02 11:13:49 +01001187 best[i].heat[wpos] = syn_nbs[a]->best[b].activation;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001188 }
1189 }
1190 }
Marc Kupietz4116b432017-12-06 14:15:32 +01001191 if(bits_set) {
1192 word_window_average /= bits_set;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001193// word_activation_sum /= bits_set;
1194// word_window_sum /= bits_set;
Marc Kupietz4116b432017-12-06 14:15:32 +01001195 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001196
1197 word_window_sum /= total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001198
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001199 if(word_window_sum > best[i].probability) {
Marc Kupietz4116b432017-12-06 14:15:32 +01001200// best[i].position = w;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001201 best[i].probability = word_window_sum;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001202 }
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001203
Marc Kupietz4116b432017-12-06 14:15:32 +01001204 if(word_cprobability_sum > best[i].cprobability_sum) {
1205 best[i].position = w;
1206 best[i].cprobability_sum = word_cprobability_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001207 }
Marc Kupietz4116b432017-12-06 14:15:32 +01001208
1209 best[i].average = word_window_average;
1210// best[i].activation = word_activation_sum;
1211 }
Marc Kupietz30ca4342017-11-22 21:21:20 +01001212 }
Marc Kupietze0e42132017-11-24 16:44:34 +01001213 qsort(best, found_index, sizeof(collocator), cmp_probability);
Marc Kupietz30ca4342017-11-22 21:21:20 +01001214// for(i=0; i < found_index; i++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001215// 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 +01001216// }
1217
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001218 } else if(sort_by == 1) { // responsiveness any window position
1219 int wpos;
1220 for(i=0; i < found_index; i++) {
1221 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
1222 for(a=0; a < syn_threads; a++) {
1223 wpos = (a >= window? a+1 : a);
1224 for(b=0; b < syn_nbs[a]->length; b++)
1225 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
1226 best[i].probability += syn_nbs[a]->best[b].probability;
1227 if(syn_nbs[a]->best[b].activation > 0.25)
1228 best[i].position |= 1 << wpos;
1229 if(syn_nbs[a]->best[b].activation > best[i].activation) {
1230 best[i].activation = syn_nbs[a]->best[b].activation;
1231 }
1232 }
1233 }
1234 }
1235 qsort(best, found_index, sizeof(collocator), cmp_activation);
1236 } else if(sort_by == 2) { // single window position
Marc Kupietz30ca4342017-11-22 21:21:20 +01001237 for(a=1; a < syn_threads; a++) {
1238 for(b=0; b < syn_nbs[a]->length; b++) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001239 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001240 if(syn_nbs[a]->best[b].activation > best[c].activation) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001241 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001242 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001243 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001244 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
1245 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 +02001246 break;
Marc Kupietz6d9a6782016-03-23 17:25:25 +01001247 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001248 }
1249 }
1250 }
1251 } else { // sort by mean p
1252 for(a=1; a < syn_threads; a++) {
1253 for(b=0; b < syn_nbs[a]->length; b++) {
1254 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001255 if(target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001256 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001257 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001258 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001259 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
1260 best[c].position = (1 << 2*window) - 1; // syn_nbs[a]->pos[b];
1261 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001262 break;
1263 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001264 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +01001265 }
1266 }
1267 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001268 array = newAV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001269 for (a = 0, i=0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
1270 long long c = best[a].wordi;
Marc Kupietza77acce2017-11-30 16:59:07 +01001271/*
Marc Kupietzd91212f2017-11-13 10:05:09 +01001272 if (dedupe) {
1273 int filtered=0;
1274 for (j=0; j<i; j++)
1275 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
1276 strcasestr(chosen[j], &vocab[c * max_w])) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001277 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
1278 filtered = 1;
1279 }
Marc Kupietzd91212f2017-11-13 10:05:09 +01001280 if(filtered)
1281 continue;
1282 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001283*/
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001284 chosen[i++]=c;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001285 HV* hash = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001286 SV* word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001287 AV* heat = newAV();
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001288 if(latin_enc == 0) SvUTF8_on(word);
1289 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001290 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001291 hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001292 hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001293 hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001294 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 +01001295 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 +01001296 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001297 best[a].heat[5]=0;
1298 for(i=10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i]));
1299 hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV*)heat), 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001300 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietz271e2a42016-03-22 11:37:43 +01001301 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001302 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001303 }
Marc Kupietz000ad862016-02-26 14:59:12 +01001304end:
Marc Kupietz257a93d2019-01-03 17:55:22 +01001305 // words = old_words; // why was this here?
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001306 free(best);
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001307 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +01001308}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +01001309
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001310int dump_vecs(char *fname) {
1311 long i, j;
1312 FILE *f;
Marc Kupietz0a68a792018-12-20 11:41:45 +01001313 if(words>100000)
1314 words=100000;
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001315
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001316 if((f=fopen(fname, "w")) == NULL) {
1317 fprintf(stderr, "cannot open %s for writing\n", fname);
1318 return(-1);
1319 }
1320 fprintf(f, "%lld %lld\n", words, size);
1321 for (i=0; i < words; i++) {
1322 fprintf(f, "%s ", &vocab[i * max_w]);
1323 for(j=0; j < size - 1; j++)
1324 fprintf(f, "%f ", M[i*size + j]);
1325 fprintf(f, "%f\n", M[i*size + j]);
1326 }
1327 fclose(f);
1328 return(0);
1329}
Marc Kupietzdc22b982015-10-09 09:19:34 +02001330
Marc Kupietzfa194262018-06-05 09:39:32 +02001331int dump_for_numpy(char *fname) {
1332 long i, j;
1333 FILE *f;
Marc Kupietz1233eda2019-03-06 14:16:43 +01001334 int max;
1335 if(merged_end > 0)
1336 max = 150000;
1337 else
1338 max = 300000;
1339
1340 if(words>300000)
1341 words=300000;
Marc Kupietzfa194262018-06-05 09:39:32 +02001342
1343 if((f=fopen(fname, "w")) == NULL) {
1344 fprintf(stderr, "cannot open %s for writing\n", fname);
1345 return(-1);
1346 }
Marc Kupietz1233eda2019-03-06 14:16:43 +01001347 for (i=0; i < max; i++) {
Marc Kupietzfa194262018-06-05 09:39:32 +02001348 for(j=0; j < size - 1; j++)
1349 fprintf(f, "%f\t", M[i*size + j]);
1350 fprintf(f, "%f\n", M[i*size + j]);
Marc Kupietz1233eda2019-03-06 14:16:43 +01001351 printf("%s\n", &vocab[i * max_w]);
1352
1353 if(merged_end > 0) {
1354 for(j=0; j < size - 1; j++)
1355 fprintf(f, "%f\t", M[(merged_end + i)*size + j]);
1356 fprintf(f, "%f\n", M[(merged_end + i)*size + j]);
1357 printf("_%s\n", &vocab[i * max_w]);
1358 }
Marc Kupietzfa194262018-06-05 09:39:32 +02001359 }
1360 fclose(f);
Marc Kupietz7631e022019-01-25 12:04:42 +01001361 return(0);
Marc Kupietzfa194262018-06-05 09:39:32 +02001362}