blob: a8ef55969fe308a1203b14dd4ec4221041ee9573 [file] [log] [blame]
Marc Kupietzdc22b982015-10-09 09:19:34 +02001#!/usr/local/bin/perl
2use Inline C;
Marc Kupietze243efd2018-01-11 22:19:24 +01003use Inline C => Config => CLEAN_AFTER_BUILD => 0, BUILD_NOISY => 1, ccflags => $Config{ccflags}." -I/vol/work/kupietz/Work2/kl/trunk/CollocatorDB -L/vol/work/kupietz/Work2/kl/trunk/CollocatorDB -Wall -O4", libs => "-shared -lpthread -lcollocatordb -lrt -lsnappy -lz -lbz2 -llz4 -lzstd -lrocksdb";
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 Kupietz5c3887d2016-04-28 08:53:35 +020027our $opt_G;
Marc Kupietza5b90152016-03-15 17:39:19 +010028
Marc Kupietz6ed81872016-04-27 14:04:04 +020029my %marked;
Marc Kupietz793413b2016-04-02 21:48:57 +020030my $training_args="";
Marc Kupietza2e64502016-04-27 09:53:51 +020031my $mergedEnd=0;
Marc Kupietz15987412017-11-07 15:56:58 +010032my %cache;
Marc Kupietz19c68242018-03-12 09:42:21 +010033my %cccache; # classic collocator cache
Marc Kupietz793413b2016-04-02 21:48:57 +020034
Marc Kupietza5f60042017-05-04 10:38:12 +020035getopts('d:Gil:p:m:n:M:');
Marc Kupietz6ed81872016-04-27 14:04:04 +020036
37if($opt_M) {
Marc Kupietzed930212016-04-27 15:42:38 +020038 open my $handle, '<:encoding(UTF-8)', $opt_M
39 or die "Can't open '$opt_M' for reading: $!";
40 while(<$handle>) {
Marc Kupietz6ed81872016-04-27 14:04:04 +020041 foreach my $mw (split /\s+/) {
42 $marked{$mw}=1
43 }
44 }
Marc Kupietzed930212016-04-27 15:42:38 +020045 close($handle);
Marc Kupietz6ed81872016-04-27 14:04:04 +020046}
Marc Kupietza5b90152016-03-15 17:39:19 +010047
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010048# -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 +010049if(!$ARGV[0]) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010050 init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0));
Marc Kupietz2cb667e2016-03-10 09:44:12 +010051} else {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010052 init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0));
Marc Kupietz793413b2016-04-02 21:48:57 +020053 if(open(FILE, "$ARGV[0].args")) {
54 $training_args = <FILE>;
55 }
56 close(FILE);
Marc Kupietz2cb667e2016-03-10 09:44:12 +010057}
Marc Kupietzdc22b982015-10-09 09:19:34 +020058
Marc Kupietza2e64502016-04-27 09:53:51 +020059if($opt_m) {
60 $mergedEnd = mergeVectors($opt_m);
61}
62
Marc Kupietz6ed81872016-04-27 14:04:04 +020063
Marc Kupietz43ee87e2016-04-25 10:50:08 +020064if($opt_d) { # -d: dump vecs and exit
65 dump_vecs($opt_d);
66 exit;
67}
68
Marc Kupietza5b90152016-03-15 17:39:19 +010069my $daemon = Mojo::Server::Daemon->new(
70 app => app,
71 listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"]
72);
73
Marc Kupietz5c3887d2016-04-28 08:53:35 +020074if($opt_G) {
75 print "Filtering garbage\n";
76 filter_garbage();
77}
78
Marc Kupietz554aff52017-11-09 14:42:09 +010079get '*/js/*' => sub {
Marc Kupietzffef9302017-11-07 15:58:01 +010080 my $c = shift;
81 my $url = $c->req->url;
82 $url =~ s@/derekovecs@@g;
83 $c->app->log->info("GET: " . $url);
84 $c->reply->static($url);
85};
86
Marc Kupietza9270572018-03-17 15:17:07 +010087get '*/css/*' => sub {
88 my $c = shift;
89 my $url = $c->req->url;
90 $url =~ s@/derekovecs/@/@g;
91 $c->app->log->info("GET: " . $url);
92 $c->reply->static($url);
93};
94
Marc Kupietz19c68242018-03-12 09:42:21 +010095sub getClassicCollocatorsCached {
96 my ($c, $word) = @_;
97 if(!$cccache{$word}) {
98 $cccache{$word} = getClassicCollocators($word);
99 } else {
100 $c->app->log->info("Getting classic collocators for $word from cache.");
101 }
102 return $cccache{$word};
103}
104
Marc Kupietz66bfd952017-12-11 09:59:45 +0100105post '/derekovecs/getVecsByRanks' => sub {
106 my $self = shift;
107 my $vec = getVecs($self->req->json);
108 $self->render(json => $vec);
109};
110
Marc Kupietze13a3552018-01-25 08:48:34 +0100111any '*/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100112 my $self = shift;
Marc Kupietz19c68242018-03-12 09:42:21 +0100113 $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100114};
115
Marc Kupietze13a3552018-01-25 08:48:34 +0100116any '/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100117 my $self = shift;
Marc Kupietz19c68242018-03-12 09:42:21 +0100118 $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100119};
120
Marc Kupietzdf3d4b52017-11-29 16:57:27 +0100121get '*/img/*' => sub {
122 my $c = shift;
123 my $url = $c->req->url;
124 $url =~ s@/derekovecs@@g;
125 $c->app->log->info("GET: " . $url);
126 $c->reply->static($url);
127};
128
Marc Kupietzdc22b982015-10-09 09:19:34 +0200129get '/' => sub {
130 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +0200131 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200132 my $word=$c->param('word');
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100133 my $no_nbs=$c->param('n') || 100;
134 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +0100135 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100136 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100137 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200138 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100139 my $sort=$c->param('sort') || 0;
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100140 my $csv=$c->param('csv') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +0200141 my $json=$c->param('json') || 0;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100142 my $cutoff=$c->param('cutoff') || 500000;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100143 my $dedupe=$c->param('dedupe') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100144 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100145 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100146 my @collocations;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100147 if(defined($word) && $word !~ /^\s*$/) {
148 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100149 $word =~ s/\s+/ /g;
150 for my $w (split(' *\| *', $word)) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100151 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100152 $c->app->log->info("Getting $w results from cache");
Marc Kupietzd91212f2017-11-13 10:05:09 +0100153 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe}
Marc Kupietza5b90152016-03-15 17:39:19 +0100154 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100155 $c->app->log->info('Looking for neighbours of '.$w);
156 if($opt_i) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100157 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe);
Marc Kupietz15987412017-11-07 15:56:58 +0100158 } else {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100159 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe);
Marc Kupietz15987412017-11-07 15:56:58 +0100160 }
Marc Kupietz2dd2dd72017-12-01 22:08:14 +0100161 $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100162 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100163 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100164 }
165 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100166 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200167 if($json) {
168 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100169 } elsif($csv) {
170 my $csv_data="";
171 for (my $i=0; $i <= $no_nbs; $i++) {
172 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
173 }
174 for (my $i=0; $i < $no_nbs; $i++) {
175 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
176 }
177 chop $csv_data;
178 chop $csv_data;
179 $csv_data .= "\n";
180 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200181 } else {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100182 $c->render(template=>"index", word=>$word, cutoff=>$cutoff, no_nbs=>$no_nbs, no_iterations => $no_iterations, epsilon=> $epsilon, perplexity=> $perplexity, show_som=>$som, searchBaseVocabFirst=>$searchBaseVocabFirst, sort=>$sort, training_args=>$training_args, mergedEnd=> $mergedEnd, dedupe=> $dedupe, marked=>\%marked, lists=> \@lists, collocators=> $res->{syntagmatic});
Marc Kupietzb613b052016-04-28 14:11:59 +0200183 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200184};
185
Marc Kupietz30ca4342017-11-22 21:21:20 +0100186helper(bitvec2window => sub {
187 my ($self, $n) = @_;
188 my $str = unpack("B32", pack("N", $n));
189 $str =~ s/^\d{22}//;
190 $str =~ s/^(\d{5})/$1x/;
191 $str =~ s/0/·/g;
192 $str =~ s/1/+/g;
193 return $str;
194 });
195
Marc Kupietza5b90152016-03-15 17:39:19 +0100196$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200197
198exit;
199
200__END__
201
202__C__
203#include <stdio.h>
204#include <string.h>
205#include <math.h>
206#include <malloc.h>
207#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +0100208#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +0100209#include <pthread.h>
Marc Kupietze243efd2018-01-11 22:19:24 +0100210#include <collocatordb.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200211
212#define max_size 2000
213#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100214#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100215#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +0100216#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100217#define MAX_CC 50
218#define EXP_TABLE_SIZE 1000
219#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +0100220#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +0200221
222//the thread function
223void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +0100224
225typedef struct {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100226 long long wordi;
227 long position;
228 float activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100229 float average;
Marc Kupietza77acce2017-11-30 16:59:07 +0100230 float cprobability; // column wise probability
Marc Kupietz4116b432017-12-06 14:15:32 +0100231 float cprobability_sum;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100232 float probability;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100233 float activation_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100234 float max_activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100235 float heat[16];
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100236} collocator;
237
238typedef struct {
239 collocator *best;
Marc Kupietz80abb442016-03-23 21:04:08 +0100240 int length;
Marc Kupietz000ad862016-02-26 14:59:12 +0100241} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100242
Marc Kupietz000ad862016-02-26 14:59:12 +0100243typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +0100244 long long wordi[MAX_NEIGHBOURS];
245 char sep[MAX_NEIGHBOURS];
246 int length;
247} wordlist;
248
249typedef struct {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100250 long cutoff;
Marc Kupietz48c29682016-03-19 11:30:43 +0100251 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100252 char *token;
253 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100254 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100255 unsigned long upto;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100256 collocator *best;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100257 float *target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100258 float *window_sums;
Marc Kupietz000ad862016-02-26 14:59:12 +0100259} knnpars;
260
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200261float *M, *M2=0L, *syn1neg_window, *expTable;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100262float *window_sums;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200263char *vocab;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200264char *garbage = NULL;
Marc Kupietze243efd2018-01-11 22:19:24 +0100265COLLOCATORDB *cdb = NULL;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100266
Marc Kupietza2e64502016-04-27 09:53:51 +0200267long long words, size, merged_end;
Marc Kupietza5f60042017-05-04 10:38:12 +0200268long long merge_words = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100269int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100270int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100271int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200272
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100273int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100274 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100275 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100276 long long a, b, c, d, cn;
277 float len;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200278 double val;
Marc Kupietz82b02672016-02-26 12:32:25 +0100279
Marc Kupietz67c20282016-02-26 09:42:00 +0100280 char binvecs_fname[256], binwords_fname[256];
281 strcpy(binwords_fname, file_name);
282 strcat(binwords_fname, ".words");
283 strcpy(binvecs_fname, file_name);
284 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200285
Marc Kupietza5b90152016-03-15 17:39:19 +0100286 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200287 f = fopen(file_name, "rb");
288 if (f == NULL) {
289 printf("Input file %s not found\n", file_name);
290 return -1;
291 }
292 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100293 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200294 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100295 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
296 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100297 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
298 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
299 if (M == NULL) {
300 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
301 return -1;
302 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200303 if(strstr(file_name, ".txt")) {
304 for (b = 0; b < words; b++) {
305 a = 0;
306 while (1) {
307 vocab[b * max_w + a] = fgetc(f);
308 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
309 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
310 }
311 vocab[b * max_w + a] = 0;
312 len = 0;
313 for (a = 0; a < size; a++) {
314 fscanf(f, "%lf", &val);
315 M[a + b * size] = val;
316 len += val * val;
317 }
318 len = sqrt(len);
319 for (a = 0; a < size; a++) M[a + b * size] /= len;
320 }
321 } else {
322 for (b = 0; b < words; b++) {
323 a = 0;
324 while (1) {
325 vocab[b * max_w + a] = fgetc(f);
326 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
327 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
328 }
329 vocab[b * max_w + a] = 0;
330 fread(&M[b * size], sizeof(float), size, f);
331 len = 0;
332 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
333 len = sqrt(len);
334 for (a = 0; a < size; a++) M[a + b * size] /= len;
335 }
336 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100337 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
338 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
339 fclose(binvecs);
340 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
341 fclose(binwords);
342 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100343 }
344 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
345 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
346 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
347 if (M == MAP_FAILED || vocab == MAP_FAILED) {
348 close(binvecs_fd);
349 close(binwords_fd);
350 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
351 exit(-1);
352 }
353 } else {
354 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
355 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100356 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200357 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100358
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200359 if(net_name && strlen(net_name) > 0) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100360 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
361 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
362 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100363 // munmap(M, sizeof(float) * words * size);
364 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 +0200365 if (M2 == MAP_FAILED) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100366 close(net_fd);
367 fprintf(stderr, "Cannot mmap %s\n", net_name);
368 exit(-1);
369 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100370 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100371 } else {
372 fprintf(stderr, "Cannot open %s\n", net_name);
373 exit(-1);
374 }
375 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
Marc Kupietze243efd2018-01-11 22:19:24 +0100376
377 char collocatordb_name[2048];
378 strcpy(collocatordb_name, net_name);
379 char *ext = rindex(collocatordb_name, '.');
380 if(ext) {
381 strcpy(ext, ".rocksdb");
382 if(access(collocatordb_name, R_OK) == 0) {
383 *ext = 0;
384 fprintf(stderr, "Opening collocator DB %s\n", collocatordb_name);
385 cdb = open_collocatordb(collocatordb_name);
386 }
387 }
388 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100389
390 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
391 for (i = 0; i < EXP_TABLE_SIZE; i++) {
392 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
393 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
394 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100395 window_sums = malloc(sizeof(float) * (window+1) * 2);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200396 return 0;
397}
398
Marc Kupietza2e64502016-04-27 09:53:51 +0200399long mergeVectors(char *file_name){
400 FILE *f, *binvecs, *binwords;
401 int binwords_fd, binvecs_fd, net_fd, i;
402 long long a, b, c, d, cn;
403 float len;
404 float *merge_vecs;
405 char *merge_vocab;
Marc Kupietza5f60042017-05-04 10:38:12 +0200406 /* long long merge_words, merge_size; */
407 long long merge_size;
Marc Kupietza2e64502016-04-27 09:53:51 +0200408
409 char binvecs_fname[256], binwords_fname[256];
410 strcpy(binwords_fname, file_name);
411 strcat(binwords_fname, ".words");
412 strcpy(binvecs_fname, file_name);
413 strcat(binvecs_fname, ".vecs");
414
415 f = fopen(file_name, "rb");
416 if (f == NULL) {
417 printf("Input file %s not found\n", file_name);
418 exit -1;
419 }
420 fscanf(f, "%lld", &merge_words);
421 fscanf(f, "%lld", &merge_size);
422 if(merge_size != size){
423 fprintf(stderr, "vectors must have the same length\n");
424 exit(-1);
425 }
426 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
427 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
428 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
429 if (merge_vecs == NULL || merge_vocab == NULL) {
430 close(binvecs_fd);
431 close(binwords_fd);
432 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
433 exit(-1);
434 }
435 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
436 read(binwords_fd, merge_vocab, merge_words * max_w);
437 } else {
438 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
439 exit(-1);
440 }
441 printf("Successfully reallocated memory\nMerging...\n");
442 fflush(stdout);
443 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
444 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
445 munmap(M, words * size * sizeof(float));
446 munmap(vocab, words * max_w);
447 M = merge_vecs;
448 vocab = merge_vocab;
449 merged_end = merge_words;
450 words += merge_words;
451 fclose(f);
452 printf("merged_end: %lld, words: %lld\n", merged_end, words);
453 return((long) merged_end);
454}
455
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200456void filter_garbage() {
457 long i;
Marc Kupietzab591a82016-04-28 14:08:49 +0200458 unsigned char *w, previous, c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200459 garbage = malloc(words);
460 memset(garbage, 0, words);
461 for (i = 0; i < words; i++) {
462 w = vocab + i * max_w;
463 previous = 0;
Marc Kupietz969adfa2018-03-12 09:43:53 +0100464 if(strncmp("quot", w, 4) == 0) {
465 garbage[i]=1;
466 printf("Gargabe: %s\n", vocab + i * max_w);
467 } else {
468 while((c = *w++) && !garbage[i]) {
469 if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
470 (previous == '-' && (c & 32)) ||
471 (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) ||
472 (previous == 'q' && c == 'u' && *(w) == 'o' && *(w+1) == 't') || /* quot */
473 c == '<'
474 ) {
475 garbage[i]=1;
476 continue;
477 }
478 previous = c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200479 }
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200480 }
481 }
482 return;
483}
484
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100485void *getCollocators(void *args) {
486 knnpars *pars = args;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100487 int N = pars->N;
488 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100489 knn *nbs = NULL;
490 long window_layer_size = size * window * 2;
491 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
492 float f, max_f, maxmax_f;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100493 float *target_sums, worstbest, wpos_sum;
494 collocator *best;
Marc Kupietzd5642582016-03-19 22:23:13 +0100495
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200496 if(M2 == NULL || cc == -1)
Marc Kupietzd5642582016-03-19 22:23:13 +0100497 return NULL;
498
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100499 a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100500 best = malloc(N * sizeof(collocator));
Marc Kupietz271e2a42016-03-22 11:37:43 +0100501 worstbest = MIN_RESP;
502
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100503 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100504 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100505 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100506 best[b].wordi = -1;
507 best[b].probability = 1;
508 best[b].activation = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100509 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100510
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100511 d = cc;
512 maxmax_f = -1;
513 maxmax_target = 0;
514
Marc Kupietz271e2a42016-03-22 11:37:43 +0100515 for (a = pars->from; a < pars->upto; a++) {
516 if(a >= window)
517 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100518 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100519 printf("window pos: %ld\n", a);
520 if (a != window) {
521 max_f = -1;
522 window_offset = a * size;
523 if (a > window)
524 window_offset -= size;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100525 for(target = 0; target < pars->cutoff; target ++) {
526 if(garbage && garbage[target]) continue;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100527 if(target == d)
528 continue;
529 f = 0;
530 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100531 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100532 if (f < -MAX_EXP)
533 continue;
534 else if (f > MAX_EXP)
535 continue;
536 else
537 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100538 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100539
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100540 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100541 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100542 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100543 if (f > best[b].activation) {
544 memmove(best + b + 1, best + b, (N - b -1) * sizeof(collocator));
545 best[b].activation = f;
546 best[b].wordi = target;
547 best[b].position = window-a;
Marc Kupietz33679a32016-03-22 08:49:39 +0100548 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100549 }
550 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100551 if(b == N - 1)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100552 worstbest = best[N-1].activation;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100553 }
554 }
555 printf("%d %.2f\n", max_target, max_f);
556 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
557 if(max_f > maxmax_f) {
558 maxmax_f = max_f;
559 maxmax_target = max_target;
560 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100561 for (b = 0; b < N; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100562 if(best[b].position == window-a)
Marc Kupietza77acce2017-11-30 16:59:07 +0100563 best[b].cprobability = best[b].activation / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100564 } else {
565 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
566 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100567 pars->window_sums[a] = wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100568 }
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100569 for (b = 0; b < pars->cutoff; b++)
Marc Kupietza77acce2017-11-30 16:59:07 +0100570 pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2);
571 printf("Target-Summe von 0: %f\n", pars->target_sums[150298]);
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100572 free(target_sums);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100573 for(b=0; b<N && best[b].wordi >= 0; b++);; // THIS LOOP IS NEEDED (b...)
574// 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 +0100575// printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100576 nbs = malloc(sizeof(knn));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100577 nbs->best = best;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100578 nbs->length = b-1;
579 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100580}
581
Marc Kupietz30ca4342017-11-22 21:21:20 +0100582
Marc Kupietz66bfd952017-12-11 09:59:45 +0100583AV *getVecs(AV *array) {
584 int i, b;
585 AV *result = newAV();
586 for (i=0; i<=av_len(array); i++) {
587 SV** elem = av_fetch(array, i, 0);
588 if (elem != NULL) {
589 long j = (long) SvNV(*elem);
590 AV *vector = newAV();
591 for (b = 0; b < size; b++) {
592 av_push(vector, newSVnv(M[b + j * size]));
593 }
594 av_push(result, newRV_noinc(vector));
595 }
596 }
597 return result;
598}
599
Marc Kupietze243efd2018-01-11 22:19:24 +0100600char *getClassicCollocators(long node) {
601 char *res = (cdb? strdup(get_collocators_as_json(cdb, node)) : "[]");
602 return res;
603}
604
Marc Kupietza2e64502016-04-27 09:53:51 +0200605wordlist *getTargetWords(char *st1, int search_backw) {
Marc Kupietz48c29682016-03-19 11:30:43 +0100606 wordlist *wl = malloc(sizeof(wordlist));
607 char st[100][max_size], sep[100];
608 long a, b=0, c=0, cn=0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200609 int unmerged;
610
Marc Kupietzdc22b982015-10-09 09:19:34 +0200611 while (1) {
612 st[cn][b] = st1[c];
613 b++;
614 c++;
615 st[cn][b] = 0;
616 if (st1[c] == 0) break;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100617 if (st1[c] == ' ' || st1[c] == '-') {
618 sep[cn++] = st1[c];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200619 b = 0;
620 c++;
621 }
622 }
623 cn++;
624 for (a = 0; a < cn; a++) {
Marc Kupietza2e64502016-04-27 09:53:51 +0200625 if(search_backw) {
626 for (b = words - 1; b >= 0; b--) if (!strcmp(&vocab[b * max_w], st[a])) break;
627 } else {
628 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
629 }
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100630 if (b == words) b = -1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100631 wl->wordi[a] = b;
632 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], wl->wordi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200633 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100634 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100635 cn--;
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100636 free(wl);
637 return NULL;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200638 }
639 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100640 wl->length=cn;
641 return(wl);
642}
643
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100644void *_get_neighbours(void *arg) {
645 knnpars *pars = arg;
Marc Kupietz48c29682016-03-19 11:30:43 +0100646 char *st1 = pars->token;
647 int N = pars->N;
648 long from = pars -> from;
649 unsigned long upto = pars -> upto;
650 char file_name[max_size], st[100][max_size], *sep;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100651 float dist, len, vec[max_size];
652 long long a, b, c, d, cn, *bi;
Marc Kupietz48c29682016-03-19 11:30:43 +0100653 char ch;
654 knn *nbs = NULL;
655 wordlist *wl = pars->wl;
656
Marc Kupietzc55c8872017-12-01 23:27:10 +0100657 collocator *best = pars->best;
Marc Kupietz48c29682016-03-19 11:30:43 +0100658
659 float worstbest=-1;
660
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100661 for (a = 0; a < N; a++) best[a].activation = 0;
Marc Kupietz48c29682016-03-19 11:30:43 +0100662 a = 0;
663 bi = wl->wordi;
664 cn = wl->length;
665 sep = wl->sep;
666 b = bi[0];
667 c = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100668 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100669 N = 0;
670 goto end;
671 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200672 for (a = 0; a < size; a++) vec[a] = 0;
673 for (b = 0; b < cn; b++) {
674 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100675 if(b>0 && sep[b-1] == '-')
676 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
677 else
678 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200679 }
680 len = 0;
681 for (a = 0; a < size; a++) len += vec[a] * vec[a];
682 len = sqrt(len);
683 for (a = 0; a < size; a++) vec[a] /= len;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100684 for (a = 0; a < N; a++) best[a].activation = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100685 for (c = from; c < upto; c++) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200686 if(garbage && garbage[c]) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200687 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100688// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100689// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
690// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200691 dist = 0;
692 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100693 if(dist > worstbest) {
694 for (a = 0; a < N; a++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100695 if (dist > best[a].activation) {
696 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
697 best[a].activation = dist;
698 best[a].wordi = c;
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100699 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200700 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200701 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100702 worstbest = best[N-1].activation;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200703 }
704 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100705
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100706end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100707 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200708}
709
Marc Kupietze0e42132017-11-24 16:44:34 +0100710int cmp_activation (const void * a, const void * b) {
711 float fb = ((collocator *)a)->activation;
712 float fa = ((collocator *)b)->activation;
713 return (fa > fb) - (fa < fb);
714}
715
716int cmp_probability (const void * a, const void * b) {
717 float fb = ((collocator *)a)->probability;
718 float fa = ((collocator *)b)->probability;
719 return (fa > fb) - (fa < fb);
720}
721
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100722
Marc Kupietzd91212f2017-11-13 10:05:09 +0100723SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100724 HV *result = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100725 float *target_sums, vec[max_size];
Marc Kupietza5f60042017-05-04 10:38:12 +0200726 long long old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100727 long a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100728 knn *para_nbs[MAX_THREADS];
729 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100730 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100731 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100732 wordlist *wl;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200733 int syn_threads = (M2? window * 2 : 0);
734 int para_threads = num_threads - syn_threads;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100735
Marc Kupietzc55c8872017-12-01 23:27:10 +0100736 collocator *best;
737 posix_memalign((void **) &best, 128, 10 * N * sizeof(collocator));
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100738 memset(best, 0, 10 * N * sizeof(collocator));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100739
Marc Kupietz000ad862016-02-26 14:59:12 +0100740 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
741
Marc Kupietzd5d05732018-01-30 11:57:35 +0100742 if(cutoff < 1 || cutoff > words)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100743 cutoff=words;
744
Marc Kupietza2e64502016-04-27 09:53:51 +0200745 wl = getTargetWords(st1, search_backw);
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100746 if(wl == NULL || wl->length < 1)
Marc Kupietz271e2a42016-03-22 11:37:43 +0100747 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100748
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100749 old_words = cutoff;
Marc Kupietza5f60042017-05-04 10:38:12 +0200750 if(merge_words > 0)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100751 cutoff = merge_words * 1.25; /* HACK */
752 slice = cutoff / para_threads;
Marc Kupietza5f60042017-05-04 10:38:12 +0200753
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100754 a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float));
755 for(a = 0; a < cutoff; a++)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100756 target_sums[a] = 0;
757
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200758 printf("Starting %d threads\n", para_threads);
759 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100760 for(a=0; a < para_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100761 pars[a].cutoff = cutoff;
Marc Kupietz000ad862016-02-26 14:59:12 +0100762 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100763 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100764 pars[a].N = N;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100765 pars[a].best = &best[N*a];
Marc Kupietz000ad862016-02-26 14:59:12 +0100766 pars[a].from = a*slice;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100767 pars[a].upto = ((a+1)*slice > cutoff? cutoff:(a+1)*slice);
Marc Kupietz000ad862016-02-26 14:59:12 +0100768 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
769 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200770 if(M2) {
771 for(a=0; a < syn_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100772 pars[a + para_threads].cutoff = cutoff;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200773 pars[a + para_threads].target_sums = target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100774 pars[a + para_threads].window_sums = window_sums;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200775 pars[a + para_threads].wl = wl;
776 pars[a + para_threads].N = N;
777 pars[a + para_threads].from = a;
778 pars[a + para_threads].upto = a+1;
779 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
780 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100781 }
782 printf("Waiting for para threads to join\n");
783 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100784 for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *) &para_nbs[a]);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100785 printf("Para threads joint\n");
786 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +0100787
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200788 /* if(!syn_nbs[0]) */
789 /* goto end; */
Marc Kupietz000ad862016-02-26 14:59:12 +0100790
Marc Kupietzc55c8872017-12-01 23:27:10 +0100791 qsort(best, N*para_threads, sizeof(collocator), cmp_activation);
Marc Kupietz000ad862016-02-26 14:59:12 +0100792
Marc Kupietz000ad862016-02-26 14:59:12 +0100793
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100794 long long chosen[MAX_NEIGHBOURS];
795 printf("N: %ld\n", N);
796
Marc Kupietz271e2a42016-03-22 11:37:43 +0100797 AV* array = newAV();
Marc Kupietzd91212f2017-11-13 10:05:09 +0100798 int i, j;
Marc Kupietza5f60042017-05-04 10:38:12 +0200799 int l1_words=0, l2_words=0;
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100800
801 for (a = 0, i = 0; i < N && a < N*para_threads; a++) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100802 int filtered=0;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100803 long long c = best[a].wordi;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100804 if (dedupe && i > 0) {
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100805 for (j=0; j<i && !filtered; j++)
806 if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) ||
807 strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) {
808 printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]);
Marc Kupietzd91212f2017-11-13 10:05:09 +0100809 filtered = 1;
810 }
811 if(filtered)
812 continue;
813 }
Marc Kupietza5f60042017-05-04 10:38:12 +0200814 if(merge_words > 0) {
815 if(c >= merge_words) {
816 if(l1_words > N / 2)
817 continue;
818 else
819 l1_words++;
820 } else {
821 if(l2_words > N / 2)
822 continue;
823 else
824 l2_words++;
825 }
826 }
Marc Kupietza5f60042017-05-04 10:38:12 +0200827 printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100828 fflush(stdout);
Marc Kupietza5f60042017-05-04 10:38:12 +0200829
Marc Kupietz271e2a42016-03-22 11:37:43 +0100830 HV* hash = newHV();
Marc Kupietza5f60042017-05-04 10:38:12 +0200831 SV* word = newSVpvf(&vocab[c * max_w], 0);
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100832 chosen[i] = c;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100833 if(latin_enc == 0) SvUTF8_on(word);
Marc Kupietza5f60042017-05-04 10:38:12 +0200834 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100835 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100836 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
837 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100838 AV *vector = newAV();
839 for (b = 0; b < size; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100840 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100841 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100842 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
843 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietza5f60042017-05-04 10:38:12 +0200844 i++;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100845 }
846 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
847
Marc Kupietz50485ba2016-03-23 09:13:14 +0100848 for(b=0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100849 best[b].wordi = -1L;
850 best[b].activation = 0;
851 best[b].probability = 0;
852 best[b].position = 0;
853 best[b].activation_sum = 0;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100854 }
855
Marc Kupietza77acce2017-11-30 16:59:07 +0100856 float total_activation = 0;
857
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200858 if (M2) {
859 printf("Waiting for syn threads to join\n");
860 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100861 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], (void *) &syn_nbs[a]);
Marc Kupietza77acce2017-11-30 16:59:07 +0100862 for (a = 0; a <= syn_threads; a++) {
Marc Kupietz63872182018-03-12 09:44:49 +0100863 if(a == window) continue;
Marc Kupietza77acce2017-11-30 16:59:07 +0100864 total_activation += window_sums[a];
865 printf("window pos: %d, sum: %f\n", a, window_sums[a]);
866 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200867 printf("syn threads joint\n");
868 fflush(stdout);
Marc Kupietz50485ba2016-03-23 09:13:14 +0100869
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200870 for(b=0; b < syn_nbs[0]->length; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100871 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
872 best[b].position = -1; // syn_nbs[0]->pos[b];
873 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100874 best[b].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +0100875 best[b].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100876 best[b].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +0100877 best[b].cprobability = syn_nbs[0]->best[b].cprobability;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200878 }
Marc Kupietz30ca4342017-11-22 21:21:20 +0100879
880 float best_window_sum[MAX_NEIGHBOURS];
881 int found_index=0, i=0, j, w;
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100882 for(a=0; a < syn_threads; a++) {
883 for(b=0; b < syn_nbs[a]->length; b++) {
884 for(i=0; i < found_index; i++)
885 if(best[i].wordi == syn_nbs[a]->best[b].wordi)
886 break;
887 if(i >= found_index) {
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100888 best[found_index].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +0100889 best[found_index].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100890 best[found_index].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +0100891 best[found_index].cprobability = syn_nbs[a]->best[b].cprobability;
892 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 +0100893 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
894 // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
Marc Kupietz30ca4342017-11-22 21:21:20 +0100895 }
896 }
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100897 }
Marc Kupietza77acce2017-11-30 16:59:07 +0100898 sort_by =0; // ALWAYS AUTO-FOCUS
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100899 if(sort_by != 1 && sort_by != 2) { // sort by auto focus mean
Marc Kupietz30ca4342017-11-22 21:21:20 +0100900 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) -1);
Marc Kupietzbd41da32017-11-24 10:26:43 +0100901 int wpos;
Marc Kupietz4116b432017-12-06 14:15:32 +0100902 int bits_set = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100903 for(i=0; i < found_index; i++) {
Marc Kupietz4116b432017-12-06 14:15:32 +0100904 best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100905 for(w=1; w < (1 << syn_threads); w++) { // loop through all possible windows
Marc Kupietz4116b432017-12-06 14:15:32 +0100906 float word_window_sum = 0, word_window_average=0, word_cprobability_sum=0, word_activation_sum = 0, total_window_sum = 0;
907 bits_set = 0;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100908 for(a=0; a < syn_threads; a++) {
Marc Kupietz30ca4342017-11-22 21:21:20 +0100909 if((1 << a) & w) {
Marc Kupietzbd41da32017-11-24 10:26:43 +0100910 wpos = (a >= window? a+1 : a);
911 total_window_sum += window_sums[wpos];
Marc Kupietz30ca4342017-11-22 21:21:20 +0100912 }
913 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100914// printf("%d window-sum %f\n", w, total_window_sum);
915 for(a=0; a < syn_threads; a++) {
916 if((1 << a) & w) {
917 wpos = (a >= window? a+1 : a);
918 bits_set++;
919 for(b=0; b < syn_nbs[a]->length; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100920 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
Marc Kupietza77acce2017-11-30 16:59:07 +0100921// float acti = syn_nbs[a]->best[b].activation / total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100922// 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 +0100923// word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
924// 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 +0100925
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100926 word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
Marc Kupietza77acce2017-11-30 16:59:07 +0100927// word_window_sum += acti - (word_window_sum * acti); syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
928
Marc Kupietz4116b432017-12-06 14:15:32 +0100929 word_window_average += syn_nbs[a]->best[b].activation; // - word_window_average * syn_nbs[a]->best[b].activation; // conormalied activation sum
930 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 +0100931 word_activation_sum += syn_nbs[a]->best[b].activation;
932 if(syn_nbs[a]->best[b].activation > best[i].max_activation)
933 best[i].max_activation = syn_nbs[a]->best[b].activation;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100934 if(syn_nbs[a]->best[b].activation > best[i].heat[wpos] )
Marc Kupietz728b8ed2017-12-02 11:13:49 +0100935 best[i].heat[wpos] = syn_nbs[a]->best[b].activation;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100936 }
937 }
938 }
Marc Kupietz4116b432017-12-06 14:15:32 +0100939 if(bits_set) {
940 word_window_average /= bits_set;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100941// word_activation_sum /= bits_set;
942// word_window_sum /= bits_set;
Marc Kupietz4116b432017-12-06 14:15:32 +0100943 }
Marc Kupietza77acce2017-11-30 16:59:07 +0100944
945 word_window_sum /= total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100946
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100947 if(word_window_sum > best[i].probability) {
Marc Kupietz4116b432017-12-06 14:15:32 +0100948// best[i].position = w;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100949 best[i].probability = word_window_sum;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100950 }
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100951
Marc Kupietz4116b432017-12-06 14:15:32 +0100952 if(word_cprobability_sum > best[i].cprobability_sum) {
953 best[i].position = w;
954 best[i].cprobability_sum = word_cprobability_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100955 }
Marc Kupietz4116b432017-12-06 14:15:32 +0100956
957 best[i].average = word_window_average;
958// best[i].activation = word_activation_sum;
959 }
Marc Kupietz30ca4342017-11-22 21:21:20 +0100960 }
Marc Kupietze0e42132017-11-24 16:44:34 +0100961 qsort(best, found_index, sizeof(collocator), cmp_probability);
Marc Kupietz30ca4342017-11-22 21:21:20 +0100962// for(i=0; i < found_index; i++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100963// 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 +0100964// }
965
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100966 } else if(sort_by == 1) { // responsiveness any window position
967 int wpos;
968 for(i=0; i < found_index; i++) {
969 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
970 for(a=0; a < syn_threads; a++) {
971 wpos = (a >= window? a+1 : a);
972 for(b=0; b < syn_nbs[a]->length; b++)
973 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
974 best[i].probability += syn_nbs[a]->best[b].probability;
975 if(syn_nbs[a]->best[b].activation > 0.25)
976 best[i].position |= 1 << wpos;
977 if(syn_nbs[a]->best[b].activation > best[i].activation) {
978 best[i].activation = syn_nbs[a]->best[b].activation;
979 }
980 }
981 }
982 }
983 qsort(best, found_index, sizeof(collocator), cmp_activation);
984 } else if(sort_by == 2) { // single window position
Marc Kupietz30ca4342017-11-22 21:21:20 +0100985 for(a=1; a < syn_threads; a++) {
986 for(b=0; b < syn_nbs[a]->length; b++) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200987 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100988 if(syn_nbs[a]->best[b].activation > best[c].activation) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200989 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100990 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200991 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100992 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
993 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 +0200994 break;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100995 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200996 }
997 }
998 }
999 } else { // sort by mean p
1000 for(a=1; a < syn_threads; a++) {
1001 for(b=0; b < syn_nbs[a]->length; b++) {
1002 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001003 if(target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001004 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001005 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001006 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001007 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
1008 best[c].position = (1 << 2*window) - 1; // syn_nbs[a]->pos[b];
1009 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001010 break;
1011 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001012 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +01001013 }
1014 }
1015 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001016 array = newAV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001017 for (a = 0, i=0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
1018 long long c = best[a].wordi;
Marc Kupietza77acce2017-11-30 16:59:07 +01001019/*
Marc Kupietzd91212f2017-11-13 10:05:09 +01001020 if (dedupe) {
1021 int filtered=0;
1022 for (j=0; j<i; j++)
1023 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
1024 strcasestr(chosen[j], &vocab[c * max_w])) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001025 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
1026 filtered = 1;
1027 }
Marc Kupietzd91212f2017-11-13 10:05:09 +01001028 if(filtered)
1029 continue;
1030 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001031*/
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001032 chosen[i++]=c;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001033 HV* hash = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001034 SV* word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001035 AV* heat = newAV();
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001036 if(latin_enc == 0) SvUTF8_on(word);
1037 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001038 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001039 hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001040 hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001041 hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001042 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 +01001043 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 +01001044 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001045 best[a].heat[5]=0;
1046 for(i=10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i]));
1047 hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV*)heat), 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001048 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietz271e2a42016-03-22 11:37:43 +01001049 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001050 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001051 }
Marc Kupietz000ad862016-02-26 14:59:12 +01001052end:
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001053 words = old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001054 free(best);
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001055 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +01001056}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +01001057
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001058int dump_vecs(char *fname) {
1059 long i, j;
1060 FILE *f;
1061 /* if(words>200000) */
1062 /* words=200000; */
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001063
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001064 if((f=fopen(fname, "w")) == NULL) {
1065 fprintf(stderr, "cannot open %s for writing\n", fname);
1066 return(-1);
1067 }
1068 fprintf(f, "%lld %lld\n", words, size);
1069 for (i=0; i < words; i++) {
1070 fprintf(f, "%s ", &vocab[i * max_w]);
1071 for(j=0; j < size - 1; j++)
1072 fprintf(f, "%f ", M[i*size + j]);
1073 fprintf(f, "%f\n", M[i*size + j]);
1074 }
1075 fclose(f);
1076 return(0);
1077}
Marc Kupietzdc22b982015-10-09 09:19:34 +02001078