blob: 527c7cee9b8447187f3422a02a8d0fdd9e3d5da6 [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);
128 } else {
Marc Kupietz06d61292019-02-04 12:33:22 +0100129 $c->app->log->info("Getting classic collocates for $word from cache.");
Marc Kupietz19c68242018-03-12 09:42:21 +0100130 }
Marc Kupietz81aeed22019-02-17 21:22:45 +0100131 if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100132 while(<PIPE>) {
133 $s2 .= $_;
134 }
135 close(PIPE);
Marc Kupietz81aeed22019-02-17 21:22:45 +0100136 }
Marc Kupietz999ab8c2019-02-17 21:42:21 +0100137
Marc Kupietz81aeed22019-02-17 21:22:45 +0100138 if(length($s2) > 2000) {
139 my $d1 = decode_json($cccache{$word});
140 my $d2 = decode_json($s2);
141 my %d2ld;
142 foreach my $i (@{$d2->{collocates}}) {
143 $d2ld{$i->{word}}=$i->{ld};
144 }
145 foreach my $i (@{$d1->{collocates}}) {
146 my $w = $i->{word};
147 $i->{delta} = $i->{ld} - (defined $d2ld{$w} ? $d2ld{$w} : -5);
148 }
149 return(encode_json($d1));
150 } else {
151 my $d1 = decode_json($cccache{$word});
152 foreach my $i (@{$d1->{collocates}}) {
153 $i->{delta} = 0;
154 }
155 return(encode_json($d1));
156 }
Marc Kupietz19c68242018-03-12 09:42:21 +0100157}
158
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100159sub getSimilarProfilesCached {
160 my ($c, $word) = @_;
161 if(!$spcache{$word}) {
162 $spcache{$word} = getSimilarProfiles($word);
163 } else {
164 $c->app->log->info("Getting similar profiles for $word from cache:");
165 print $spcache{$word};
166 }
167 return $spcache{$word};
168}
169
Marc Kupietz66bfd952017-12-11 09:59:45 +0100170post '/derekovecs/getVecsByRanks' => sub {
171 my $self = shift;
172 my $vec = getVecs($self->req->json);
173 $self->render(json => $vec);
174};
175
Marc Kupietze13a3552018-01-25 08:48:34 +0100176any '*/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100177 my $self = shift;
Marc Kupietz19c68242018-03-12 09:42:21 +0100178 $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100179};
180
Marc Kupietze13a3552018-01-25 08:48:34 +0100181any '/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100182 my $self = shift;
Marc Kupietz19c68242018-03-12 09:42:21 +0100183 $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100184};
185
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100186any '*/getSimilarProfiles' => sub {
187 my $self = shift;
188 $self->render(data => getSimilarProfilesCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
189};
190
Marc Kupietzc987fa82018-03-21 12:14:25 +0100191any '/getSimilarProfiles' => sub {
192 my $self = shift;
193 $self->render(data => getSimilarProfilesCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
194};
195
Marc Kupietzdf3d4b52017-11-29 16:57:27 +0100196get '*/img/*' => sub {
197 my $c = shift;
198 my $url = $c->req->url;
199 $url =~ s@/derekovecs@@g;
200 $c->app->log->info("GET: " . $url);
201 $c->reply->static($url);
202};
203
Marc Kupietzdc22b982015-10-09 09:19:34 +0200204get '/' => sub {
205 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +0200206 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200207 my $word=$c->param('word');
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100208 my $no_nbs=$c->param('n') || 100;
209 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +0100210 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100211 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100212 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200213 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100214 my $sort=$c->param('sort') || 0;
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100215 my $csv=$c->param('csv') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +0200216 my $json=$c->param('json') || 0;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100217 my $cutoff=$c->param('cutoff') || 500000;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100218 my $dedupe=$c->param('dedupe') || 0;
Marc Kupietzac707b32018-12-20 11:36:38 +0100219 my $nosp=$c->param('nosp') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100220 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100221 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100222 my @collocations;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100223 if(defined($word) && $word !~ /^\s*$/) {
224 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100225 $word =~ s/\s+/ /g;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100226 if($opt_m && $word !~ /\|/) {
227 $word .= "|$word";
228 }
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100229 for my $w (split(' *\| *', $word)) {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100230 if($opt_m) {
231 if($searchBaseVocabFirst) {
232 $searchBaseVocabFirst=0;
233 } else {
234 $searchBaseVocabFirst=1;
235 }
236 }
237 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe,$searchBaseVocabFirst}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100238 $c->app->log->info("Getting $w results from cache");
Marc Kupietz3082fd02019-01-09 14:54:06 +0100239 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe.$searchBaseVocabFirst}
Marc Kupietza5b90152016-03-15 17:39:19 +0100240 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100241 $c->app->log->info('Looking for neighbours of '.$w);
242 if($opt_i) {
Marc Kupietzac707b32018-12-20 11:36:38 +0100243 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100244 } else {
Marc Kupietzac707b32018-12-20 11:36:38 +0100245 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100246 }
Marc Kupietz2dd2dd72017-12-01 22:08:14 +0100247 $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100248 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100249 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100250 }
251 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100252 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200253 if($json) {
254 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100255 } elsif($csv) {
256 my $csv_data="";
257 for (my $i=0; $i <= $no_nbs; $i++) {
258 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
259 }
260 for (my $i=0; $i < $no_nbs; $i++) {
261 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
262 }
263 chop $csv_data;
264 chop $csv_data;
265 $csv_data .= "\n";
266 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200267 } else {
Marc Kupietzc053d972019-01-10 10:41:51 +0100268 $c->render(template=>"index", title=>$title, word=>$word, cutoff=>$cutoff, no_nbs=>$no_nbs, no_iterations => $no_iterations, epsilon=> $epsilon, perplexity=> $perplexity, show_som=>$som, searchBaseVocabFirst=>$searchBaseVocabFirst, sort=>$sort, training_args=>$training_args, mergedEnd=> $mergedEnd, haveSProfiles=> $have_sprofiles, dedupe=> $dedupe, marked=>\%marked, lists=> \@lists, collocators=> $res->{syntagmatic});
Marc Kupietzb613b052016-04-28 14:11:59 +0200269 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200270};
271
Marc Kupietz30ca4342017-11-22 21:21:20 +0100272helper(bitvec2window => sub {
273 my ($self, $n) = @_;
274 my $str = unpack("B32", pack("N", $n));
275 $str =~ s/^\d{22}//;
276 $str =~ s/^(\d{5})/$1x/;
277 $str =~ s/0/·/g;
278 $str =~ s/1/+/g;
279 return $str;
280 });
281
Marc Kupietza5b90152016-03-15 17:39:19 +0100282$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200283
284exit;
285
286__END__
287
288__C__
289#include <stdio.h>
290#include <string.h>
291#include <math.h>
292#include <malloc.h>
293#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +0100294#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +0100295#include <pthread.h>
Marc Kupietze243efd2018-01-11 22:19:24 +0100296#include <collocatordb.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200297
298#define max_size 2000
299#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100300#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100301#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +0100302#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100303#define MAX_CC 50
304#define EXP_TABLE_SIZE 1000
305#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +0100306#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +0200307
308//the thread function
309void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +0100310
311typedef struct {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100312 long long wordi;
313 long position;
314 float activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100315 float average;
Marc Kupietza77acce2017-11-30 16:59:07 +0100316 float cprobability; // column wise probability
Marc Kupietz4116b432017-12-06 14:15:32 +0100317 float cprobability_sum;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100318 float probability;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100319 float activation_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100320 float max_activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100321 float heat[16];
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100322} collocator;
323
324typedef struct {
325 collocator *best;
Marc Kupietz80abb442016-03-23 21:04:08 +0100326 int length;
Marc Kupietz000ad862016-02-26 14:59:12 +0100327} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100328
Marc Kupietz000ad862016-02-26 14:59:12 +0100329typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +0100330 long long wordi[MAX_NEIGHBOURS];
331 char sep[MAX_NEIGHBOURS];
332 int length;
333} wordlist;
334
335typedef struct {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100336 long cutoff;
Marc Kupietz48c29682016-03-19 11:30:43 +0100337 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100338 char *token;
339 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100340 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100341 unsigned long upto;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100342 collocator *best;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100343 float *target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100344 float *window_sums;
Marc Kupietz000ad862016-02-26 14:59:12 +0100345} knnpars;
346
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100347typedef struct {
348 uint32_t index;
349 float value;
350} sparse_t;
351
352typedef struct {
353 uint32_t len;
354 sparse_t nbr[100];
355} profile_t;
356
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200357float *M, *M2=0L, *syn1neg_window, *expTable;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100358float *window_sums;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200359char *vocab;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200360char *garbage = NULL;
Marc Kupietze243efd2018-01-11 22:19:24 +0100361COLLOCATORDB *cdb = NULL;
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100362profile_t *sprofiles = NULL;
363size_t sprofiles_qty = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100364
Marc Kupietza2e64502016-04-27 09:53:51 +0200365long long words, size, merged_end;
Marc Kupietza5f60042017-05-04 10:38:12 +0200366long long merge_words = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100367int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100368int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100369int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200370
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100371/* load collocation profiles if file exists */
372int load_sprofiles(char *vecsname) {
373 char *basename = strdup(vecsname);
374 char *pos = strstr(basename, ".vecs");
375 if(pos)
376 *pos=0;
377
378 char binsprofiles_fname[256];
379 strcpy(binsprofiles_fname, basename);
380 strcat(binsprofiles_fname, ".sprofiles.bin");
381 FILE *fp = fopen(binsprofiles_fname, "rb");
382 if (fp == NULL) {
383 printf("Collocation profiles %s not found. No problem.\n", binsprofiles_fname);
384 return 0;
385 }
386 fseek(fp, 0L, SEEK_END);
387 size_t sz = ftell(fp);
388 fclose(fp);
389
390 int fd = open(binsprofiles_fname, O_RDONLY);
391 sprofiles = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0);
392 if (sprofiles == MAP_FAILED) {
393 close(fd);
394 fprintf(stderr, "Cannot mmap %s\n", binsprofiles_fname);
395 sprofiles = NULL;
396 return 0;
397 } else {
398 sprofiles_qty = sz / sizeof(profile_t);
399 fprintf(stderr, "Successfully mmaped %s containing similar profiles for %ld word forms.\n", binsprofiles_fname, sprofiles_qty);
400 }
401 return 1;
402}
403
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100404int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100405 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100406 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100407 long long a, b, c, d, cn;
408 float len;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200409 double val;
Marc Kupietz82b02672016-02-26 12:32:25 +0100410
Marc Kupietz67c20282016-02-26 09:42:00 +0100411 char binvecs_fname[256], binwords_fname[256];
412 strcpy(binwords_fname, file_name);
413 strcat(binwords_fname, ".words");
414 strcpy(binvecs_fname, file_name);
415 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200416
Marc Kupietza5b90152016-03-15 17:39:19 +0100417 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200418 f = fopen(file_name, "rb");
419 if (f == NULL) {
420 printf("Input file %s not found\n", file_name);
421 return -1;
422 }
423 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100424 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200425 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100426 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
427 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100428 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
429 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
430 if (M == NULL) {
431 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
432 return -1;
433 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200434 if(strstr(file_name, ".txt")) {
435 for (b = 0; b < words; b++) {
436 a = 0;
437 while (1) {
438 vocab[b * max_w + a] = fgetc(f);
439 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
440 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
441 }
442 vocab[b * max_w + a] = 0;
443 len = 0;
444 for (a = 0; a < size; a++) {
445 fscanf(f, "%lf", &val);
446 M[a + b * size] = val;
447 len += val * val;
448 }
449 len = sqrt(len);
450 for (a = 0; a < size; a++) M[a + b * size] /= len;
451 }
452 } else {
453 for (b = 0; b < words; b++) {
454 a = 0;
455 while (1) {
456 vocab[b * max_w + a] = fgetc(f);
457 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
458 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
459 }
460 vocab[b * max_w + a] = 0;
461 fread(&M[b * size], sizeof(float), size, f);
462 len = 0;
463 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
464 len = sqrt(len);
465 for (a = 0; a < size; a++) M[a + b * size] /= len;
466 }
467 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100468 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
469 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
470 fclose(binvecs);
471 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
472 fclose(binwords);
473 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100474 }
475 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
476 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
477 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
478 if (M == MAP_FAILED || vocab == MAP_FAILED) {
479 close(binvecs_fd);
480 close(binwords_fd);
481 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
482 exit(-1);
483 }
484 } else {
485 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
486 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100487 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200488 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100489
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200490 if(net_name && strlen(net_name) > 0) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100491 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
492 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
493 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100494 // munmap(M, sizeof(float) * words * size);
495 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 +0200496 if (M2 == MAP_FAILED) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100497 close(net_fd);
498 fprintf(stderr, "Cannot mmap %s\n", net_name);
499 exit(-1);
500 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100501 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100502 } else {
503 fprintf(stderr, "Cannot open %s\n", net_name);
504 exit(-1);
505 }
506 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
Marc Kupietze243efd2018-01-11 22:19:24 +0100507
508 char collocatordb_name[2048];
509 strcpy(collocatordb_name, net_name);
510 char *ext = rindex(collocatordb_name, '.');
511 if(ext) {
512 strcpy(ext, ".rocksdb");
513 if(access(collocatordb_name, R_OK) == 0) {
514 *ext = 0;
515 fprintf(stderr, "Opening collocator DB %s\n", collocatordb_name);
516 cdb = open_collocatordb(collocatordb_name);
517 }
518 }
519 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100520
521 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
522 for (i = 0; i < EXP_TABLE_SIZE; i++) {
523 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
524 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
525 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100526 window_sums = malloc(sizeof(float) * (window+1) * 2);
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100527
528 return 0;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200529}
530
Marc Kupietza2e64502016-04-27 09:53:51 +0200531long mergeVectors(char *file_name){
532 FILE *f, *binvecs, *binwords;
533 int binwords_fd, binvecs_fd, net_fd, i;
534 long long a, b, c, d, cn;
535 float len;
536 float *merge_vecs;
537 char *merge_vocab;
Marc Kupietza5f60042017-05-04 10:38:12 +0200538 /* long long merge_words, merge_size; */
539 long long merge_size;
Marc Kupietza2e64502016-04-27 09:53:51 +0200540
541 char binvecs_fname[256], binwords_fname[256];
542 strcpy(binwords_fname, file_name);
543 strcat(binwords_fname, ".words");
544 strcpy(binvecs_fname, file_name);
545 strcat(binvecs_fname, ".vecs");
546
547 f = fopen(file_name, "rb");
548 if (f == NULL) {
549 printf("Input file %s not found\n", file_name);
550 exit -1;
551 }
552 fscanf(f, "%lld", &merge_words);
553 fscanf(f, "%lld", &merge_size);
554 if(merge_size != size){
555 fprintf(stderr, "vectors must have the same length\n");
556 exit(-1);
557 }
558 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
559 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
560 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
561 if (merge_vecs == NULL || merge_vocab == NULL) {
562 close(binvecs_fd);
563 close(binwords_fd);
564 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
565 exit(-1);
566 }
567 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
568 read(binwords_fd, merge_vocab, merge_words * max_w);
569 } else {
570 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
571 exit(-1);
572 }
573 printf("Successfully reallocated memory\nMerging...\n");
574 fflush(stdout);
575 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
576 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
577 munmap(M, words * size * sizeof(float));
578 munmap(vocab, words * max_w);
579 M = merge_vecs;
580 vocab = merge_vocab;
581 merged_end = merge_words;
582 words += merge_words;
583 fclose(f);
584 printf("merged_end: %lld, words: %lld\n", merged_end, words);
Marc Kupietzebc131e2019-01-03 16:24:27 +0100585 printBiggestMergedDifferences();
Marc Kupietza2e64502016-04-27 09:53:51 +0200586 return((long) merged_end);
587}
588
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200589void filter_garbage() {
590 long i;
Marc Kupietzab591a82016-04-28 14:08:49 +0200591 unsigned char *w, previous, c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200592 garbage = malloc(words);
593 memset(garbage, 0, words);
594 for (i = 0; i < words; i++) {
595 w = vocab + i * max_w;
596 previous = 0;
Marc Kupietz969adfa2018-03-12 09:43:53 +0100597 if(strncmp("quot", w, 4) == 0) {
598 garbage[i]=1;
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100599// printf("Gargabe: %s\n", vocab + i * max_w);
Marc Kupietz969adfa2018-03-12 09:43:53 +0100600 } else {
601 while((c = *w++) && !garbage[i]) {
602 if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
603 (previous == '-' && (c & 32)) ||
604 (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) ||
605 (previous == 'q' && c == 'u' && *(w) == 'o' && *(w+1) == 't') || /* quot */
606 c == '<'
607 ) {
608 garbage[i]=1;
609 continue;
610 }
611 previous = c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200612 }
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200613 }
614 }
615 return;
616}
617
Marc Kupietzac707b32018-12-20 11:36:38 +0100618
619knn *simpleGetCollocators(int word, int number, long cutoff, int *result) {
620 knnpars *pars = calloc(sizeof(knnpars), 1);
621 float *target_sums;
622 float *window_sums = malloc(sizeof(float) * (window+1) * 2);
623 pars->cutoff = (cutoff? cutoff : 300000);
624 long a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
625 for(a = 0; a < cutoff; a++)
626 target_sums[a] = 0;
627 pars->target_sums = target_sums;
628 pars->window_sums = window_sums;
629 pars->N = (number? number : 20);
630 pars->from = 0;
631 pars->upto = window * 2 -1;
Marc Kupietz7631e022019-01-25 12:04:42 +0100632 knn *syn_nbs = NULL; // = (knn*) getCollocators(pars);
Marc Kupietzac707b32018-12-20 11:36:38 +0100633 free(pars);
634 free(window_sums);
635 free(target_sums);
636 return syn_nbs;
637}
638
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100639void *getCollocators(void *args) {
640 knnpars *pars = args;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100641 int N = pars->N;
Marc Kupietz52702832019-01-15 17:55:52 +0100642
Marc Kupietz271e2a42016-03-22 11:37:43 +0100643 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100644 knn *nbs = NULL;
645 long window_layer_size = size * window * 2;
646 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
647 float f, max_f, maxmax_f;
Marc Kupietz7631e022019-01-25 12:04:42 +0100648 float *target_sums=NULL, worstbest, wpos_sum;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100649 collocator *best;
Marc Kupietzd5642582016-03-19 22:23:13 +0100650
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200651 if(M2 == NULL || cc == -1)
Marc Kupietzd5642582016-03-19 22:23:13 +0100652 return NULL;
653
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100654 a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietz7631e022019-01-25 12:04:42 +0100655 memset(target_sums, 0, pars->cutoff * sizeof(float));
656 best = malloc((N>200?N:200) * sizeof(collocator));
657 memset(best, 0, (N>200?N:200) * sizeof(collocator));
Marc Kupietz271e2a42016-03-22 11:37:43 +0100658 worstbest = MIN_RESP;
659
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100660 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100661 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100662 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100663 best[b].wordi = -1;
664 best[b].probability = 1;
665 best[b].activation = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100666 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100667
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100668 d = cc;
669 maxmax_f = -1;
670 maxmax_target = 0;
671
Marc Kupietz271e2a42016-03-22 11:37:43 +0100672 for (a = pars->from; a < pars->upto; a++) {
673 if(a >= window)
674 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100675 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100676 printf("window pos: %ld\n", a);
677 if (a != window) {
678 max_f = -1;
679 window_offset = a * size;
680 if (a > window)
681 window_offset -= size;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100682 for(target = 0; target < pars->cutoff; target ++) {
683 if(garbage && garbage[target]) continue;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100684 if(target == d)
685 continue;
686 f = 0;
687 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100688 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100689 if (f < -MAX_EXP)
690 continue;
691 else if (f > MAX_EXP)
692 continue;
693 else
694 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100695 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100696
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100697 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100698 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100699 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100700 if (f > best[b].activation) {
701 memmove(best + b + 1, best + b, (N - b -1) * sizeof(collocator));
702 best[b].activation = f;
703 best[b].wordi = target;
704 best[b].position = window-a;
Marc Kupietz33679a32016-03-22 08:49:39 +0100705 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100706 }
707 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100708 if(b == N - 1)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100709 worstbest = best[N-1].activation;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100710 }
711 }
712 printf("%d %.2f\n", max_target, max_f);
713 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
714 if(max_f > maxmax_f) {
715 maxmax_f = max_f;
716 maxmax_target = max_target;
717 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100718 for (b = 0; b < N; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100719 if(best[b].position == window-a)
Marc Kupietza77acce2017-11-30 16:59:07 +0100720 best[b].cprobability = best[b].activation / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100721 } else {
722 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
723 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100724 pars->window_sums[a] = wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100725 }
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100726 for (b = 0; b < pars->cutoff; b++)
Marc Kupietza77acce2017-11-30 16:59:07 +0100727 pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2);
728 printf("Target-Summe von 0: %f\n", pars->target_sums[150298]);
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100729 free(target_sums);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100730 for(b=0; b<N && best[b].wordi >= 0; b++);; // THIS LOOP IS NEEDED (b...)
731// 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 +0100732// printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100733 nbs = malloc(sizeof(knn));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100734 nbs->best = best;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100735 nbs->length = b-1;
736 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100737}
738
Marc Kupietz30ca4342017-11-22 21:21:20 +0100739
Marc Kupietz66bfd952017-12-11 09:59:45 +0100740AV *getVecs(AV *array) {
741 int i, b;
742 AV *result = newAV();
743 for (i=0; i<=av_len(array); i++) {
744 SV** elem = av_fetch(array, i, 0);
745 if (elem != NULL) {
746 long j = (long) SvNV(*elem);
747 AV *vector = newAV();
748 for (b = 0; b < size; b++) {
749 av_push(vector, newSVnv(M[b + j * size]));
750 }
751 av_push(result, newRV_noinc(vector));
752 }
753 }
754 return result;
755}
756
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100757char *getSimilarProfiles(long node) {
758 int i;
759 char buffer[120000];
760 char pair_buffer[2048];
761 buffer[0]='[';
762 buffer[1]=0;
763 if(node >= sprofiles_qty) {
764 printf("Not available in precomputed profile\n");
765 return(strdup("[{\"w\":\"not available\", \"v\":0}]\n"));
766 }
767
768 printf("******* %s ******\n", &vocab[max_w * node]);
769
770 for(i=0; i < 100 && i < sprofiles[node].len; i++) {
771 sprintf(pair_buffer, "{\"w\":\"%s\", \"v\":%f},", &vocab[max_w * (sprofiles[node].nbr[i].index)], sprofiles[node].nbr[i].value);
772 strcat(buffer, pair_buffer);
773 }
774 buffer[strlen(buffer)-1]=']';
775 strcat(buffer, "\n");
776 printf(buffer);
777 return(strdup(buffer));
778}
779
Marc Kupietze243efd2018-01-11 22:19:24 +0100780char *getClassicCollocators(long node) {
781 char *res = (cdb? strdup(get_collocators_as_json(cdb, node)) : "[]");
782 return res;
783}
784
Marc Kupietz0957d532019-01-03 17:54:00 +0100785wordlist *getTargetWords(char *st1, int search_backw) {
Marc Kupietz48c29682016-03-19 11:30:43 +0100786 wordlist *wl = malloc(sizeof(wordlist));
787 char st[100][max_size], sep[100];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100788 long a, b=0, c=0, cn=0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200789 int unmerged;
790
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100791 while (1) {
792 st[cn][b] = st1[c];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100793 b++;
794 c++;
795 st[cn][b] = 0;
796 if (st1[c] == 0) break;
797 if (st1[c] == ' ' || st1[c] == '-') {
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100798 sep[cn++] = st1[c];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100799 b = 0;
800 c++;
801 }
802 }
803 cn++;
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100804 for (a = 0; a < cn; a++) {
805 if (search_backw) {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100806 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 +0100807 } else {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100808 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 +0100809 }
810 if (b == words) b = -1;
811 wl->wordi[a] = b;
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100812 if (b == -1) {
813 fprintf(stderr, "Out of dictionary word!\n");
814 cn--;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100815 } else {
816 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 +0100817 }
818 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100819 wl->length=cn;
820 return(wl);
821}
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100822
Marc Kupietzebc131e2019-01-03 16:24:27 +0100823void printBiggestMergedDifferences() {
824 float dist, len, vec[max_size];
825 long long a, b, c, d, cn, *bi;
826 char ch;
827 knn *nbs = NULL;
828 int N = 100;
829
830 printf("Looking for biggest distances between main and merged vectors ...\n");
831 collocator *best;
832 best = malloc(N * sizeof(collocator));
833 memset(best, 0, N * sizeof(collocator));
834
835 float worstbest=1000000;
836
837 for (a = 0; a < N; a++) best[a].activation = worstbest;
838
839 for (c = 0; c < 10000; c++) {
840 if(garbage && garbage[c]) continue;
841 a = 0;
842 dist = 0;
843 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + (c+merged_end) * size];
844 if(dist < worstbest) {
845 for (a = 0; a < N; a++) {
846 if (dist < best[a].activation) {
847 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
848 best[a].activation = dist;
849 best[a].wordi = c;
850 break;
851 }
852 }
853 worstbest = best[N-1].activation;
854 }
855 }
856
857 printf("Most distant vectors for:\n ");
858 for (a = 0; a < N; a++) {
859 printf("%s ", &vocab[best[a].wordi * max_w]);
860 }
861 printf("\n");
862}
863
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100864void *_get_neighbours(void *arg) {
865 knnpars *pars = arg;
Marc Kupietz48c29682016-03-19 11:30:43 +0100866 char *st1 = pars->token;
867 int N = pars->N;
868 long from = pars -> from;
869 unsigned long upto = pars -> upto;
870 char file_name[max_size], st[100][max_size], *sep;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100871 float dist, len, vec[max_size];
872 long long a, b, c, d, cn, *bi;
Marc Kupietz48c29682016-03-19 11:30:43 +0100873 char ch;
874 knn *nbs = NULL;
875 wordlist *wl = pars->wl;
876
Marc Kupietzc55c8872017-12-01 23:27:10 +0100877 collocator *best = pars->best;
Marc Kupietz48c29682016-03-19 11:30:43 +0100878
879 float worstbest=-1;
880
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100881 for (a = 0; a < N; a++) best[a].activation = 0;
Marc Kupietz48c29682016-03-19 11:30:43 +0100882 a = 0;
883 bi = wl->wordi;
884 cn = wl->length;
885 sep = wl->sep;
886 b = bi[0];
887 c = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100888 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100889 N = 0;
890 goto end;
891 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200892 for (a = 0; a < size; a++) vec[a] = 0;
893 for (b = 0; b < cn; b++) {
894 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100895 if(b>0 && sep[b-1] == '-')
896 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
897 else
898 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200899 }
900 len = 0;
901 for (a = 0; a < size; a++) len += vec[a] * vec[a];
902 len = sqrt(len);
903 for (a = 0; a < size; a++) vec[a] /= len;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100904 for (a = 0; a < N; a++) best[a].activation = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100905 for (c = from; c < upto; c++) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200906 if(garbage && garbage[c]) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200907 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100908// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100909// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
910// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200911 dist = 0;
912 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100913 if(dist > worstbest) {
914 for (a = 0; a < N; a++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100915 if (dist > best[a].activation) {
916 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
917 best[a].activation = dist;
918 best[a].wordi = c;
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100919 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200920 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200921 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100922 worstbest = best[N-1].activation;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200923 }
924 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100925
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100926end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100927 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200928}
929
Marc Kupietze0e42132017-11-24 16:44:34 +0100930int cmp_activation (const void * a, const void * b) {
931 float fb = ((collocator *)a)->activation;
932 float fa = ((collocator *)b)->activation;
933 return (fa > fb) - (fa < fb);
934}
935
936int cmp_probability (const void * a, const void * b) {
937 float fb = ((collocator *)a)->probability;
938 float fa = ((collocator *)b)->probability;
939 return (fa > fb) - (fa < fb);
940}
941
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100942
Marc Kupietzac707b32018-12-20 11:36:38 +0100943SV *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 +0100944 HV *result = newHV();
Marc Kupietz7631e022019-01-25 12:04:42 +0100945 float *target_sums=NULL, vec[max_size];
Marc Kupietza5f60042017-05-04 10:38:12 +0200946 long long old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100947 long a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100948 knn *para_nbs[MAX_THREADS];
949 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100950 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100951 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100952 wordlist *wl;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200953 int syn_threads = (M2? window * 2 : 0);
Marc Kupietzac707b32018-12-20 11:36:38 +0100954 int para_threads = (no_similar_profiles? 0 : num_threads - syn_threads);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100955
Marc Kupietz7631e022019-01-25 12:04:42 +0100956 collocator *best = NULL;
957 posix_memalign((void **) &best, 128, 10 * (N>=200? N : 200) * sizeof(collocator));
958 memset(best, 0, (N>=200? N : 200) * sizeof(collocator));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100959
Marc Kupietz000ad862016-02-26 14:59:12 +0100960 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
961
Marc Kupietzd5d05732018-01-30 11:57:35 +0100962 if(cutoff < 1 || cutoff > words)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100963 cutoff=words;
964
Marc Kupietza2e64502016-04-27 09:53:51 +0200965 wl = getTargetWords(st1, search_backw);
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100966 if(wl == NULL || wl->length < 1)
Marc Kupietz271e2a42016-03-22 11:37:43 +0100967 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100968
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100969 old_words = cutoff;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100970 slice = cutoff / para_threads;
Marc Kupietza5f60042017-05-04 10:38:12 +0200971
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100972 a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float));
Marc Kupietz7631e022019-01-25 12:04:42 +0100973 memset(target_sums, 0, cutoff * sizeof(float));
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100974
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200975 printf("Starting %d threads\n", para_threads);
976 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100977 for(a=0; a < para_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100978 pars[a].cutoff = cutoff;
Marc Kupietz000ad862016-02-26 14:59:12 +0100979 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100980 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100981 pars[a].N = N;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100982 pars[a].best = &best[N*a];
Marc Kupietz3082fd02019-01-09 14:54:06 +0100983 if(merge_words == 0 || search_backw == 0) {
Marc Kupietz1f0f7802019-01-03 16:23:29 +0100984 pars[a].from = a*slice;
985 pars[a].upto = ((a+1)*slice > cutoff? cutoff : (a+1) * slice);
986 } else {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100987 pars[a].from = merge_words + a * slice;
988 pars[a].upto = merge_words + ((a+1)*slice > cutoff? cutoff : (a+1) * slice);
Marc Kupietz1f0f7802019-01-03 16:23:29 +0100989 }
990 printf("From: %ld, Upto: %ld\n", pars[a].from, pars[a].upto);
Marc Kupietz000ad862016-02-26 14:59:12 +0100991 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
992 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200993 if(M2) {
994 for(a=0; a < syn_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100995 pars[a + para_threads].cutoff = cutoff;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200996 pars[a + para_threads].target_sums = target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100997 pars[a + para_threads].window_sums = window_sums;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200998 pars[a + para_threads].wl = wl;
999 pars[a + para_threads].N = N;
1000 pars[a + para_threads].from = a;
1001 pars[a + para_threads].upto = a+1;
1002 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
1003 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001004 }
1005 printf("Waiting for para threads to join\n");
1006 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001007 for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *) &para_nbs[a]);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001008 printf("Para threads joint\n");
1009 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +01001010
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001011 /* if(!syn_nbs[0]) */
1012 /* goto end; */
Marc Kupietz000ad862016-02-26 14:59:12 +01001013
Marc Kupietzc55c8872017-12-01 23:27:10 +01001014 qsort(best, N*para_threads, sizeof(collocator), cmp_activation);
Marc Kupietz000ad862016-02-26 14:59:12 +01001015
Marc Kupietz000ad862016-02-26 14:59:12 +01001016
Marc Kupietzd0d3e912017-12-01 22:09:43 +01001017 long long chosen[MAX_NEIGHBOURS];
1018 printf("N: %ld\n", N);
1019
Marc Kupietz271e2a42016-03-22 11:37:43 +01001020 AV* array = newAV();
Marc Kupietzd91212f2017-11-13 10:05:09 +01001021 int i, j;
Marc Kupietza5f60042017-05-04 10:38:12 +02001022 int l1_words=0, l2_words=0;
Marc Kupietzd0d3e912017-12-01 22:09:43 +01001023
1024 for (a = 0, i = 0; i < N && a < N*para_threads; a++) {
Marc Kupietzd91212f2017-11-13 10:05:09 +01001025 int filtered=0;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001026 long long c = best[a].wordi;
Marc Kupietze8b9dd22019-01-03 17:55:59 +01001027 if ((merge_words && dedupe && i > 1) || (!merge_words && dedupe && i > 0)) {
Marc Kupietzd0d3e912017-12-01 22:09:43 +01001028 for (j=0; j<i && !filtered; j++)
1029 if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) ||
1030 strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) {
1031 printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]);
Marc Kupietzd91212f2017-11-13 10:05:09 +01001032 filtered = 1;
1033 }
1034 if(filtered)
1035 continue;
1036 }
Marc Kupietz3082fd02019-01-09 14:54:06 +01001037
1038
1039 if(0 && merge_words > 0) {
Marc Kupietza5f60042017-05-04 10:38:12 +02001040 if(c >= merge_words) {
1041 if(l1_words > N / 2)
1042 continue;
1043 else
1044 l1_words++;
1045 } else {
1046 if(l2_words > N / 2)
1047 continue;
1048 else
1049 l2_words++;
1050 }
1051 }
Marc Kupietz3082fd02019-01-09 14:54:06 +01001052
1053// printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
1054// fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001055 HV* hash = newHV();
Marc Kupietza5f60042017-05-04 10:38:12 +02001056 SV* word = newSVpvf(&vocab[c * max_w], 0);
Marc Kupietzd0d3e912017-12-01 22:09:43 +01001057 chosen[i] = c;
Marc Kupietz271e2a42016-03-22 11:37:43 +01001058 if(latin_enc == 0) SvUTF8_on(word);
Marc Kupietza5f60042017-05-04 10:38:12 +02001059 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001060 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001061 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
1062 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001063 AV *vector = newAV();
1064 for (b = 0; b < size; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001065 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001066 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001067 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
1068 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietza5f60042017-05-04 10:38:12 +02001069 i++;
Marc Kupietz271e2a42016-03-22 11:37:43 +01001070 }
1071 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz7631e022019-01-25 12:04:42 +01001072
Marc Kupietz50485ba2016-03-23 09:13:14 +01001073 for(b=0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001074 best[b].wordi = -1L;
1075 best[b].activation = 0;
1076 best[b].probability = 0;
1077 best[b].position = 0;
1078 best[b].activation_sum = 0;
Marc Kupietz7631e022019-01-25 12:04:42 +01001079 memset(best[b].heat, 0, sizeof(float)*16);
Marc Kupietz50485ba2016-03-23 09:13:14 +01001080 }
1081
Marc Kupietza77acce2017-11-30 16:59:07 +01001082 float total_activation = 0;
1083
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001084 if (M2) {
1085 printf("Waiting for syn threads to join\n");
1086 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001087 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], (void *) &syn_nbs[a]);
Marc Kupietza77acce2017-11-30 16:59:07 +01001088 for (a = 0; a <= syn_threads; a++) {
Marc Kupietz63872182018-03-12 09:44:49 +01001089 if(a == window) continue;
Marc Kupietza77acce2017-11-30 16:59:07 +01001090 total_activation += window_sums[a];
1091 printf("window pos: %d, sum: %f\n", a, window_sums[a]);
1092 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001093 printf("syn threads joint\n");
1094 fflush(stdout);
Marc Kupietz50485ba2016-03-23 09:13:14 +01001095
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001096 for(b=0; b < syn_nbs[0]->length; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001097 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
1098 best[b].position = -1; // syn_nbs[0]->pos[b];
1099 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001100 best[b].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +01001101 best[b].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001102 best[b].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +01001103 best[b].cprobability = syn_nbs[0]->best[b].cprobability;
Marc Kupietz7631e022019-01-25 12:04:42 +01001104 memset(best[b].heat, 0, sizeof(float)*16);
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001105 }
Marc Kupietz30ca4342017-11-22 21:21:20 +01001106
1107 float best_window_sum[MAX_NEIGHBOURS];
1108 int found_index=0, i=0, j, w;
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001109 for(a=0; a < syn_threads; a++) {
1110 for(b=0; b < syn_nbs[a]->length; b++) {
1111 for(i=0; i < found_index; i++)
1112 if(best[i].wordi == syn_nbs[a]->best[b].wordi)
1113 break;
1114 if(i >= found_index) {
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001115 best[found_index].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +01001116 best[found_index].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001117 best[found_index].probability = 0.0;
Marc Kupietz7631e022019-01-25 12:04:42 +01001118 memset(best[found_index].heat, 0, sizeof(float)*16);
Marc Kupietza77acce2017-11-30 16:59:07 +01001119 best[found_index].cprobability = syn_nbs[a]->best[b].cprobability;
1120 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 +01001121 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
1122 // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
Marc Kupietz30ca4342017-11-22 21:21:20 +01001123 }
1124 }
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001125 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001126 sort_by =0; // ALWAYS AUTO-FOCUS
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001127 if(sort_by != 1 && sort_by != 2) { // sort by auto focus mean
Marc Kupietz30ca4342017-11-22 21:21:20 +01001128 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) -1);
Marc Kupietzbd41da32017-11-24 10:26:43 +01001129 int wpos;
Marc Kupietz4116b432017-12-06 14:15:32 +01001130 int bits_set = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001131 for(i=0; i < found_index; i++) {
Marc Kupietz4116b432017-12-06 14:15:32 +01001132 best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001133 for(w=1; w < (1 << syn_threads); w++) { // loop through all possible windows
Marc Kupietz4116b432017-12-06 14:15:32 +01001134 float word_window_sum = 0, word_window_average=0, word_cprobability_sum=0, word_activation_sum = 0, total_window_sum = 0;
1135 bits_set = 0;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001136 for(a=0; a < syn_threads; a++) {
Marc Kupietz30ca4342017-11-22 21:21:20 +01001137 if((1 << a) & w) {
Marc Kupietzbd41da32017-11-24 10:26:43 +01001138 wpos = (a >= window? a+1 : a);
1139 total_window_sum += window_sums[wpos];
Marc Kupietz30ca4342017-11-22 21:21:20 +01001140 }
1141 }
Marc Kupietzbd41da32017-11-24 10:26:43 +01001142// printf("%d window-sum %f\n", w, total_window_sum);
1143 for(a=0; a < syn_threads; a++) {
1144 if((1 << a) & w) {
1145 wpos = (a >= window? a+1 : a);
1146 bits_set++;
1147 for(b=0; b < syn_nbs[a]->length; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001148 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001149// float acti = syn_nbs[a]->best[b].activation / total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001150// 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 +01001151// word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1152// 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 +01001153
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001154 word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
Marc Kupietza77acce2017-11-30 16:59:07 +01001155// word_window_sum += acti - (word_window_sum * acti); syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1156
Marc Kupietz4116b432017-12-06 14:15:32 +01001157 word_window_average += syn_nbs[a]->best[b].activation; // - word_window_average * syn_nbs[a]->best[b].activation; // conormalied activation sum
1158 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 +01001159 word_activation_sum += syn_nbs[a]->best[b].activation;
1160 if(syn_nbs[a]->best[b].activation > best[i].max_activation)
1161 best[i].max_activation = syn_nbs[a]->best[b].activation;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +01001162 if(syn_nbs[a]->best[b].activation > best[i].heat[wpos] )
Marc Kupietz728b8ed2017-12-02 11:13:49 +01001163 best[i].heat[wpos] = syn_nbs[a]->best[b].activation;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001164 }
1165 }
1166 }
Marc Kupietz4116b432017-12-06 14:15:32 +01001167 if(bits_set) {
1168 word_window_average /= bits_set;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001169// word_activation_sum /= bits_set;
1170// word_window_sum /= bits_set;
Marc Kupietz4116b432017-12-06 14:15:32 +01001171 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001172
1173 word_window_sum /= total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001174
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001175 if(word_window_sum > best[i].probability) {
Marc Kupietz4116b432017-12-06 14:15:32 +01001176// best[i].position = w;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001177 best[i].probability = word_window_sum;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001178 }
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001179
Marc Kupietz4116b432017-12-06 14:15:32 +01001180 if(word_cprobability_sum > best[i].cprobability_sum) {
1181 best[i].position = w;
1182 best[i].cprobability_sum = word_cprobability_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001183 }
Marc Kupietz4116b432017-12-06 14:15:32 +01001184
1185 best[i].average = word_window_average;
1186// best[i].activation = word_activation_sum;
1187 }
Marc Kupietz30ca4342017-11-22 21:21:20 +01001188 }
Marc Kupietze0e42132017-11-24 16:44:34 +01001189 qsort(best, found_index, sizeof(collocator), cmp_probability);
Marc Kupietz30ca4342017-11-22 21:21:20 +01001190// for(i=0; i < found_index; i++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001191// 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 +01001192// }
1193
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001194 } else if(sort_by == 1) { // responsiveness any window position
1195 int wpos;
1196 for(i=0; i < found_index; i++) {
1197 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
1198 for(a=0; a < syn_threads; a++) {
1199 wpos = (a >= window? a+1 : a);
1200 for(b=0; b < syn_nbs[a]->length; b++)
1201 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
1202 best[i].probability += syn_nbs[a]->best[b].probability;
1203 if(syn_nbs[a]->best[b].activation > 0.25)
1204 best[i].position |= 1 << wpos;
1205 if(syn_nbs[a]->best[b].activation > best[i].activation) {
1206 best[i].activation = syn_nbs[a]->best[b].activation;
1207 }
1208 }
1209 }
1210 }
1211 qsort(best, found_index, sizeof(collocator), cmp_activation);
1212 } else if(sort_by == 2) { // single window position
Marc Kupietz30ca4342017-11-22 21:21:20 +01001213 for(a=1; a < syn_threads; a++) {
1214 for(b=0; b < syn_nbs[a]->length; b++) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001215 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001216 if(syn_nbs[a]->best[b].activation > best[c].activation) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001217 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001218 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001219 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001220 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
1221 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 +02001222 break;
Marc Kupietz6d9a6782016-03-23 17:25:25 +01001223 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001224 }
1225 }
1226 }
1227 } else { // sort by mean p
1228 for(a=1; a < syn_threads; a++) {
1229 for(b=0; b < syn_nbs[a]->length; b++) {
1230 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001231 if(target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001232 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001233 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001234 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001235 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
1236 best[c].position = (1 << 2*window) - 1; // syn_nbs[a]->pos[b];
1237 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001238 break;
1239 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001240 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +01001241 }
1242 }
1243 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001244 array = newAV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001245 for (a = 0, i=0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
1246 long long c = best[a].wordi;
Marc Kupietza77acce2017-11-30 16:59:07 +01001247/*
Marc Kupietzd91212f2017-11-13 10:05:09 +01001248 if (dedupe) {
1249 int filtered=0;
1250 for (j=0; j<i; j++)
1251 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
1252 strcasestr(chosen[j], &vocab[c * max_w])) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001253 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
1254 filtered = 1;
1255 }
Marc Kupietzd91212f2017-11-13 10:05:09 +01001256 if(filtered)
1257 continue;
1258 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001259*/
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001260 chosen[i++]=c;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001261 HV* hash = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001262 SV* word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001263 AV* heat = newAV();
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001264 if(latin_enc == 0) SvUTF8_on(word);
1265 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001266 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001267 hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001268 hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001269 hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001270 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 +01001271 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 +01001272 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001273 best[a].heat[5]=0;
1274 for(i=10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i]));
1275 hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV*)heat), 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001276 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietz271e2a42016-03-22 11:37:43 +01001277 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001278 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001279 }
Marc Kupietz000ad862016-02-26 14:59:12 +01001280end:
Marc Kupietz257a93d2019-01-03 17:55:22 +01001281 // words = old_words; // why was this here?
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001282 free(best);
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001283 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +01001284}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +01001285
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001286int dump_vecs(char *fname) {
1287 long i, j;
1288 FILE *f;
Marc Kupietz0a68a792018-12-20 11:41:45 +01001289 if(words>100000)
1290 words=100000;
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001291
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001292 if((f=fopen(fname, "w")) == NULL) {
1293 fprintf(stderr, "cannot open %s for writing\n", fname);
1294 return(-1);
1295 }
1296 fprintf(f, "%lld %lld\n", words, size);
1297 for (i=0; i < words; i++) {
1298 fprintf(f, "%s ", &vocab[i * max_w]);
1299 for(j=0; j < size - 1; j++)
1300 fprintf(f, "%f ", M[i*size + j]);
1301 fprintf(f, "%f\n", M[i*size + j]);
1302 }
1303 fclose(f);
1304 return(0);
1305}
Marc Kupietzdc22b982015-10-09 09:19:34 +02001306
Marc Kupietzfa194262018-06-05 09:39:32 +02001307int dump_for_numpy(char *fname) {
1308 long i, j;
1309 FILE *f;
1310 /* if(words>200000) */
1311 /* words=200000; */
1312
1313 if((f=fopen(fname, "w")) == NULL) {
1314 fprintf(stderr, "cannot open %s for writing\n", fname);
1315 return(-1);
1316 }
1317 for (i=0; i < words; i++) {
1318 for(j=0; j < size - 1; j++)
1319 fprintf(f, "%f\t", M[i*size + j]);
1320 fprintf(f, "%f\n", M[i*size + j]);
1321 }
1322 fclose(f);
Marc Kupietz7631e022019-01-25 12:04:42 +01001323 return(0);
Marc Kupietzfa194262018-06-05 09:39:32 +02001324}