blob: a5ef690ee9ddaea2a2b145e6bf5aa47ece1fa34e [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 Kupietz19c68242018-03-12 09:42:21 +010087sub getClassicCollocatorsCached {
88 my ($c, $word) = @_;
89 if(!$cccache{$word}) {
90 $cccache{$word} = getClassicCollocators($word);
91 } else {
92 $c->app->log->info("Getting classic collocators for $word from cache.");
93 }
94 return $cccache{$word};
95}
96
Marc Kupietz66bfd952017-12-11 09:59:45 +010097post '/derekovecs/getVecsByRanks' => sub {
98 my $self = shift;
99 my $vec = getVecs($self->req->json);
100 $self->render(json => $vec);
101};
102
Marc Kupietze13a3552018-01-25 08:48:34 +0100103any '*/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100104 my $self = shift;
Marc Kupietz19c68242018-03-12 09:42:21 +0100105 $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100106};
107
Marc Kupietze13a3552018-01-25 08:48:34 +0100108any '/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100109 my $self = shift;
Marc Kupietz19c68242018-03-12 09:42:21 +0100110 $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100111};
112
Marc Kupietzdf3d4b52017-11-29 16:57:27 +0100113get '*/img/*' => sub {
114 my $c = shift;
115 my $url = $c->req->url;
116 $url =~ s@/derekovecs@@g;
117 $c->app->log->info("GET: " . $url);
118 $c->reply->static($url);
119};
120
Marc Kupietzdc22b982015-10-09 09:19:34 +0200121get '/' => sub {
122 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +0200123 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200124 my $word=$c->param('word');
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100125 my $no_nbs=$c->param('n') || 100;
126 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +0100127 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100128 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100129 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200130 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100131 my $sort=$c->param('sort') || 0;
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100132 my $csv=$c->param('csv') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +0200133 my $json=$c->param('json') || 0;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100134 my $cutoff=$c->param('cutoff') || 500000;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100135 my $dedupe=$c->param('dedupe') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100136 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100137 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100138 my @collocations;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100139 if(defined($word) && $word !~ /^\s*$/) {
140 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100141 $word =~ s/\s+/ /g;
142 for my $w (split(' *\| *', $word)) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100143 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100144 $c->app->log->info("Getting $w results from cache");
Marc Kupietzd91212f2017-11-13 10:05:09 +0100145 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe}
Marc Kupietza5b90152016-03-15 17:39:19 +0100146 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100147 $c->app->log->info('Looking for neighbours of '.$w);
148 if($opt_i) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100149 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe);
Marc Kupietz15987412017-11-07 15:56:58 +0100150 } else {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100151 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe);
Marc Kupietz15987412017-11-07 15:56:58 +0100152 }
Marc Kupietz2dd2dd72017-12-01 22:08:14 +0100153 $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100154 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100155 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100156 }
157 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100158 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200159 if($json) {
160 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100161 } elsif($csv) {
162 my $csv_data="";
163 for (my $i=0; $i <= $no_nbs; $i++) {
164 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
165 }
166 for (my $i=0; $i < $no_nbs; $i++) {
167 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
168 }
169 chop $csv_data;
170 chop $csv_data;
171 $csv_data .= "\n";
172 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200173 } else {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100174 $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 +0200175 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200176};
177
Marc Kupietz30ca4342017-11-22 21:21:20 +0100178helper(bitvec2window => sub {
179 my ($self, $n) = @_;
180 my $str = unpack("B32", pack("N", $n));
181 $str =~ s/^\d{22}//;
182 $str =~ s/^(\d{5})/$1x/;
183 $str =~ s/0/·/g;
184 $str =~ s/1/+/g;
185 return $str;
186 });
187
Marc Kupietza5b90152016-03-15 17:39:19 +0100188$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200189
190exit;
191
192__END__
193
194__C__
195#include <stdio.h>
196#include <string.h>
197#include <math.h>
198#include <malloc.h>
199#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +0100200#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +0100201#include <pthread.h>
Marc Kupietze243efd2018-01-11 22:19:24 +0100202#include <collocatordb.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200203
204#define max_size 2000
205#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100206#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100207#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +0100208#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100209#define MAX_CC 50
210#define EXP_TABLE_SIZE 1000
211#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +0100212#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +0200213
214//the thread function
215void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +0100216
217typedef struct {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100218 long long wordi;
219 long position;
220 float activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100221 float average;
Marc Kupietza77acce2017-11-30 16:59:07 +0100222 float cprobability; // column wise probability
Marc Kupietz4116b432017-12-06 14:15:32 +0100223 float cprobability_sum;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100224 float probability;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100225 float activation_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100226 float max_activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100227 float heat[16];
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100228} collocator;
229
230typedef struct {
231 collocator *best;
Marc Kupietz80abb442016-03-23 21:04:08 +0100232 int length;
Marc Kupietz000ad862016-02-26 14:59:12 +0100233} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100234
Marc Kupietz000ad862016-02-26 14:59:12 +0100235typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +0100236 long long wordi[MAX_NEIGHBOURS];
237 char sep[MAX_NEIGHBOURS];
238 int length;
239} wordlist;
240
241typedef struct {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100242 long cutoff;
Marc Kupietz48c29682016-03-19 11:30:43 +0100243 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100244 char *token;
245 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100246 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100247 unsigned long upto;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100248 collocator *best;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100249 float *target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100250 float *window_sums;
Marc Kupietz000ad862016-02-26 14:59:12 +0100251} knnpars;
252
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200253float *M, *M2=0L, *syn1neg_window, *expTable;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100254float *window_sums;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200255char *vocab;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200256char *garbage = NULL;
Marc Kupietze243efd2018-01-11 22:19:24 +0100257COLLOCATORDB *cdb = NULL;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100258
Marc Kupietza2e64502016-04-27 09:53:51 +0200259long long words, size, merged_end;
Marc Kupietza5f60042017-05-04 10:38:12 +0200260long long merge_words = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100261int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100262int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100263int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200264
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100265int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100266 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100267 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100268 long long a, b, c, d, cn;
269 float len;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200270 double val;
Marc Kupietz82b02672016-02-26 12:32:25 +0100271
Marc Kupietz67c20282016-02-26 09:42:00 +0100272 char binvecs_fname[256], binwords_fname[256];
273 strcpy(binwords_fname, file_name);
274 strcat(binwords_fname, ".words");
275 strcpy(binvecs_fname, file_name);
276 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200277
Marc Kupietza5b90152016-03-15 17:39:19 +0100278 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200279 f = fopen(file_name, "rb");
280 if (f == NULL) {
281 printf("Input file %s not found\n", file_name);
282 return -1;
283 }
284 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100285 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200286 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100287 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
288 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100289 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
290 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
291 if (M == NULL) {
292 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
293 return -1;
294 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200295 if(strstr(file_name, ".txt")) {
296 for (b = 0; b < words; b++) {
297 a = 0;
298 while (1) {
299 vocab[b * max_w + a] = fgetc(f);
300 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
301 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
302 }
303 vocab[b * max_w + a] = 0;
304 len = 0;
305 for (a = 0; a < size; a++) {
306 fscanf(f, "%lf", &val);
307 M[a + b * size] = val;
308 len += val * val;
309 }
310 len = sqrt(len);
311 for (a = 0; a < size; a++) M[a + b * size] /= len;
312 }
313 } else {
314 for (b = 0; b < words; b++) {
315 a = 0;
316 while (1) {
317 vocab[b * max_w + a] = fgetc(f);
318 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
319 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
320 }
321 vocab[b * max_w + a] = 0;
322 fread(&M[b * size], sizeof(float), size, f);
323 len = 0;
324 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
325 len = sqrt(len);
326 for (a = 0; a < size; a++) M[a + b * size] /= len;
327 }
328 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100329 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
330 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
331 fclose(binvecs);
332 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
333 fclose(binwords);
334 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100335 }
336 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
337 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
338 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
339 if (M == MAP_FAILED || vocab == MAP_FAILED) {
340 close(binvecs_fd);
341 close(binwords_fd);
342 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
343 exit(-1);
344 }
345 } else {
346 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
347 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100348 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200349 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100350
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200351 if(net_name && strlen(net_name) > 0) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100352 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
353 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
354 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100355 // munmap(M, sizeof(float) * words * size);
356 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 +0200357 if (M2 == MAP_FAILED) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100358 close(net_fd);
359 fprintf(stderr, "Cannot mmap %s\n", net_name);
360 exit(-1);
361 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100362 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100363 } else {
364 fprintf(stderr, "Cannot open %s\n", net_name);
365 exit(-1);
366 }
367 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
Marc Kupietze243efd2018-01-11 22:19:24 +0100368
369 char collocatordb_name[2048];
370 strcpy(collocatordb_name, net_name);
371 char *ext = rindex(collocatordb_name, '.');
372 if(ext) {
373 strcpy(ext, ".rocksdb");
374 if(access(collocatordb_name, R_OK) == 0) {
375 *ext = 0;
376 fprintf(stderr, "Opening collocator DB %s\n", collocatordb_name);
377 cdb = open_collocatordb(collocatordb_name);
378 }
379 }
380 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100381
382 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
383 for (i = 0; i < EXP_TABLE_SIZE; i++) {
384 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
385 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
386 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100387 window_sums = malloc(sizeof(float) * (window+1) * 2);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200388 return 0;
389}
390
Marc Kupietza2e64502016-04-27 09:53:51 +0200391long mergeVectors(char *file_name){
392 FILE *f, *binvecs, *binwords;
393 int binwords_fd, binvecs_fd, net_fd, i;
394 long long a, b, c, d, cn;
395 float len;
396 float *merge_vecs;
397 char *merge_vocab;
Marc Kupietza5f60042017-05-04 10:38:12 +0200398 /* long long merge_words, merge_size; */
399 long long merge_size;
Marc Kupietza2e64502016-04-27 09:53:51 +0200400
401 char binvecs_fname[256], binwords_fname[256];
402 strcpy(binwords_fname, file_name);
403 strcat(binwords_fname, ".words");
404 strcpy(binvecs_fname, file_name);
405 strcat(binvecs_fname, ".vecs");
406
407 f = fopen(file_name, "rb");
408 if (f == NULL) {
409 printf("Input file %s not found\n", file_name);
410 exit -1;
411 }
412 fscanf(f, "%lld", &merge_words);
413 fscanf(f, "%lld", &merge_size);
414 if(merge_size != size){
415 fprintf(stderr, "vectors must have the same length\n");
416 exit(-1);
417 }
418 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
419 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
420 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
421 if (merge_vecs == NULL || merge_vocab == NULL) {
422 close(binvecs_fd);
423 close(binwords_fd);
424 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
425 exit(-1);
426 }
427 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
428 read(binwords_fd, merge_vocab, merge_words * max_w);
429 } else {
430 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
431 exit(-1);
432 }
433 printf("Successfully reallocated memory\nMerging...\n");
434 fflush(stdout);
435 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
436 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
437 munmap(M, words * size * sizeof(float));
438 munmap(vocab, words * max_w);
439 M = merge_vecs;
440 vocab = merge_vocab;
441 merged_end = merge_words;
442 words += merge_words;
443 fclose(f);
444 printf("merged_end: %lld, words: %lld\n", merged_end, words);
445 return((long) merged_end);
446}
447
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200448void filter_garbage() {
449 long i;
Marc Kupietzab591a82016-04-28 14:08:49 +0200450 unsigned char *w, previous, c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200451 garbage = malloc(words);
452 memset(garbage, 0, words);
453 for (i = 0; i < words; i++) {
454 w = vocab + i * max_w;
455 previous = 0;
Marc Kupietzab591a82016-04-28 14:08:49 +0200456 while((c = *w++) && !garbage[i]) {
457 if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
458 (previous == '-' && (c & 32)) ||
Marc Kupietz33e3aa12017-11-09 16:19:38 +0100459 (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) ||
460 c == '<'
Marc Kupietzab591a82016-04-28 14:08:49 +0200461 ) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200462 garbage[i]=1;
463 continue;
464 }
465 previous = c;
466 }
467 }
468 return;
469}
470
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100471void *getCollocators(void *args) {
472 knnpars *pars = args;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100473 int N = pars->N;
474 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100475 knn *nbs = NULL;
476 long window_layer_size = size * window * 2;
477 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
478 float f, max_f, maxmax_f;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100479 float *target_sums, worstbest, wpos_sum;
480 collocator *best;
Marc Kupietzd5642582016-03-19 22:23:13 +0100481
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200482 if(M2 == NULL || cc == -1)
Marc Kupietzd5642582016-03-19 22:23:13 +0100483 return NULL;
484
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100485 a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100486 best = malloc(N * sizeof(collocator));
Marc Kupietz271e2a42016-03-22 11:37:43 +0100487 worstbest = MIN_RESP;
488
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100489 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100490 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100491 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100492 best[b].wordi = -1;
493 best[b].probability = 1;
494 best[b].activation = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100495 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100496
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100497 d = cc;
498 maxmax_f = -1;
499 maxmax_target = 0;
500
Marc Kupietz271e2a42016-03-22 11:37:43 +0100501 for (a = pars->from; a < pars->upto; a++) {
502 if(a >= window)
503 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100504 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100505 printf("window pos: %ld\n", a);
506 if (a != window) {
507 max_f = -1;
508 window_offset = a * size;
509 if (a > window)
510 window_offset -= size;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100511 for(target = 0; target < pars->cutoff; target ++) {
512 if(garbage && garbage[target]) continue;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100513 if(target == d)
514 continue;
515 f = 0;
516 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100517 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100518 if (f < -MAX_EXP)
519 continue;
520 else if (f > MAX_EXP)
521 continue;
522 else
523 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100524 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100525
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100526 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100527 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100528 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100529 if (f > best[b].activation) {
530 memmove(best + b + 1, best + b, (N - b -1) * sizeof(collocator));
531 best[b].activation = f;
532 best[b].wordi = target;
533 best[b].position = window-a;
Marc Kupietz33679a32016-03-22 08:49:39 +0100534 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100535 }
536 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100537 if(b == N - 1)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100538 worstbest = best[N-1].activation;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100539 }
540 }
541 printf("%d %.2f\n", max_target, max_f);
542 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
543 if(max_f > maxmax_f) {
544 maxmax_f = max_f;
545 maxmax_target = max_target;
546 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100547 for (b = 0; b < N; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100548 if(best[b].position == window-a)
Marc Kupietza77acce2017-11-30 16:59:07 +0100549 best[b].cprobability = best[b].activation / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100550 } else {
551 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
552 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100553 pars->window_sums[a] = wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100554 }
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100555 for (b = 0; b < pars->cutoff; b++)
Marc Kupietza77acce2017-11-30 16:59:07 +0100556 pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2);
557 printf("Target-Summe von 0: %f\n", pars->target_sums[150298]);
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100558 free(target_sums);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100559 for(b=0; b<N && best[b].wordi >= 0; b++);; // THIS LOOP IS NEEDED (b...)
560// 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 +0100561// printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100562 nbs = malloc(sizeof(knn));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100563 nbs->best = best;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100564 nbs->length = b-1;
565 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100566}
567
Marc Kupietz30ca4342017-11-22 21:21:20 +0100568
Marc Kupietz66bfd952017-12-11 09:59:45 +0100569AV *getVecs(AV *array) {
570 int i, b;
571 AV *result = newAV();
572 for (i=0; i<=av_len(array); i++) {
573 SV** elem = av_fetch(array, i, 0);
574 if (elem != NULL) {
575 long j = (long) SvNV(*elem);
576 AV *vector = newAV();
577 for (b = 0; b < size; b++) {
578 av_push(vector, newSVnv(M[b + j * size]));
579 }
580 av_push(result, newRV_noinc(vector));
581 }
582 }
583 return result;
584}
585
Marc Kupietze243efd2018-01-11 22:19:24 +0100586char *getClassicCollocators(long node) {
587 char *res = (cdb? strdup(get_collocators_as_json(cdb, node)) : "[]");
588 return res;
589}
590
Marc Kupietza2e64502016-04-27 09:53:51 +0200591wordlist *getTargetWords(char *st1, int search_backw) {
Marc Kupietz48c29682016-03-19 11:30:43 +0100592 wordlist *wl = malloc(sizeof(wordlist));
593 char st[100][max_size], sep[100];
594 long a, b=0, c=0, cn=0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200595 int unmerged;
596
Marc Kupietzdc22b982015-10-09 09:19:34 +0200597 while (1) {
598 st[cn][b] = st1[c];
599 b++;
600 c++;
601 st[cn][b] = 0;
602 if (st1[c] == 0) break;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100603 if (st1[c] == ' ' || st1[c] == '-') {
604 sep[cn++] = st1[c];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200605 b = 0;
606 c++;
607 }
608 }
609 cn++;
610 for (a = 0; a < cn; a++) {
Marc Kupietza2e64502016-04-27 09:53:51 +0200611 if(search_backw) {
612 for (b = words - 1; b >= 0; b--) if (!strcmp(&vocab[b * max_w], st[a])) break;
613 } else {
614 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
615 }
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100616 if (b == words) b = -1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100617 wl->wordi[a] = b;
618 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], wl->wordi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200619 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100620 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100621 cn--;
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100622 free(wl);
623 return NULL;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200624 }
625 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100626 wl->length=cn;
627 return(wl);
628}
629
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100630void *_get_neighbours(void *arg) {
631 knnpars *pars = arg;
Marc Kupietz48c29682016-03-19 11:30:43 +0100632 char *st1 = pars->token;
633 int N = pars->N;
634 long from = pars -> from;
635 unsigned long upto = pars -> upto;
636 char file_name[max_size], st[100][max_size], *sep;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100637 float dist, len, vec[max_size];
638 long long a, b, c, d, cn, *bi;
Marc Kupietz48c29682016-03-19 11:30:43 +0100639 char ch;
640 knn *nbs = NULL;
641 wordlist *wl = pars->wl;
642
Marc Kupietzc55c8872017-12-01 23:27:10 +0100643 collocator *best = pars->best;
Marc Kupietz48c29682016-03-19 11:30:43 +0100644
645 float worstbest=-1;
646
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100647 for (a = 0; a < N; a++) best[a].activation = 0;
Marc Kupietz48c29682016-03-19 11:30:43 +0100648 a = 0;
649 bi = wl->wordi;
650 cn = wl->length;
651 sep = wl->sep;
652 b = bi[0];
653 c = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100654 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100655 N = 0;
656 goto end;
657 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200658 for (a = 0; a < size; a++) vec[a] = 0;
659 for (b = 0; b < cn; b++) {
660 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100661 if(b>0 && sep[b-1] == '-')
662 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
663 else
664 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200665 }
666 len = 0;
667 for (a = 0; a < size; a++) len += vec[a] * vec[a];
668 len = sqrt(len);
669 for (a = 0; a < size; a++) vec[a] /= len;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100670 for (a = 0; a < N; a++) best[a].activation = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100671 for (c = from; c < upto; c++) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200672 if(garbage && garbage[c]) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200673 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100674// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100675// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
676// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200677 dist = 0;
678 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100679 if(dist > worstbest) {
680 for (a = 0; a < N; a++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100681 if (dist > best[a].activation) {
682 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
683 best[a].activation = dist;
684 best[a].wordi = c;
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100685 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200686 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200687 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100688 worstbest = best[N-1].activation;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200689 }
690 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100691
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100692end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100693 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200694}
695
Marc Kupietze0e42132017-11-24 16:44:34 +0100696int cmp_activation (const void * a, const void * b) {
697 float fb = ((collocator *)a)->activation;
698 float fa = ((collocator *)b)->activation;
699 return (fa > fb) - (fa < fb);
700}
701
702int cmp_probability (const void * a, const void * b) {
703 float fb = ((collocator *)a)->probability;
704 float fa = ((collocator *)b)->probability;
705 return (fa > fb) - (fa < fb);
706}
707
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100708
Marc Kupietzd91212f2017-11-13 10:05:09 +0100709SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100710 HV *result = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100711 float *target_sums, vec[max_size];
Marc Kupietza5f60042017-05-04 10:38:12 +0200712 long long old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100713 long a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100714 knn *para_nbs[MAX_THREADS];
715 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100716 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100717 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100718 wordlist *wl;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200719 int syn_threads = (M2? window * 2 : 0);
720 int para_threads = num_threads - syn_threads;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100721
Marc Kupietzc55c8872017-12-01 23:27:10 +0100722 collocator *best;
723 posix_memalign((void **) &best, 128, 10 * N * sizeof(collocator));
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100724 memset(best, 0, 10 * N * sizeof(collocator));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100725
Marc Kupietz000ad862016-02-26 14:59:12 +0100726 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
727
Marc Kupietzd5d05732018-01-30 11:57:35 +0100728 if(cutoff < 1 || cutoff > words)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100729 cutoff=words;
730
Marc Kupietza2e64502016-04-27 09:53:51 +0200731 wl = getTargetWords(st1, search_backw);
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100732 if(wl == NULL || wl->length < 1)
Marc Kupietz271e2a42016-03-22 11:37:43 +0100733 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100734
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100735 old_words = cutoff;
Marc Kupietza5f60042017-05-04 10:38:12 +0200736 if(merge_words > 0)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100737 cutoff = merge_words * 1.25; /* HACK */
738 slice = cutoff / para_threads;
Marc Kupietza5f60042017-05-04 10:38:12 +0200739
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100740 a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float));
741 for(a = 0; a < cutoff; a++)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100742 target_sums[a] = 0;
743
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200744 printf("Starting %d threads\n", para_threads);
745 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100746 for(a=0; a < para_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100747 pars[a].cutoff = cutoff;
Marc Kupietz000ad862016-02-26 14:59:12 +0100748 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100749 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100750 pars[a].N = N;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100751 pars[a].best = &best[N*a];
Marc Kupietz000ad862016-02-26 14:59:12 +0100752 pars[a].from = a*slice;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100753 pars[a].upto = ((a+1)*slice > cutoff? cutoff:(a+1)*slice);
Marc Kupietz000ad862016-02-26 14:59:12 +0100754 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
755 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200756 if(M2) {
757 for(a=0; a < syn_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100758 pars[a + para_threads].cutoff = cutoff;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200759 pars[a + para_threads].target_sums = target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100760 pars[a + para_threads].window_sums = window_sums;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200761 pars[a + para_threads].wl = wl;
762 pars[a + para_threads].N = N;
763 pars[a + para_threads].from = a;
764 pars[a + para_threads].upto = a+1;
765 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
766 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100767 }
768 printf("Waiting for para threads to join\n");
769 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100770 for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *) &para_nbs[a]);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100771 printf("Para threads joint\n");
772 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +0100773
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200774 /* if(!syn_nbs[0]) */
775 /* goto end; */
Marc Kupietz000ad862016-02-26 14:59:12 +0100776
Marc Kupietzc55c8872017-12-01 23:27:10 +0100777 qsort(best, N*para_threads, sizeof(collocator), cmp_activation);
Marc Kupietz000ad862016-02-26 14:59:12 +0100778
Marc Kupietz000ad862016-02-26 14:59:12 +0100779
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100780 long long chosen[MAX_NEIGHBOURS];
781 printf("N: %ld\n", N);
782
Marc Kupietz271e2a42016-03-22 11:37:43 +0100783 AV* array = newAV();
Marc Kupietzd91212f2017-11-13 10:05:09 +0100784 int i, j;
Marc Kupietza5f60042017-05-04 10:38:12 +0200785 int l1_words=0, l2_words=0;
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100786
787 for (a = 0, i = 0; i < N && a < N*para_threads; a++) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100788 int filtered=0;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100789 long long c = best[a].wordi;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100790 if (dedupe && i > 0) {
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100791 for (j=0; j<i && !filtered; j++)
792 if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) ||
793 strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) {
794 printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]);
Marc Kupietzd91212f2017-11-13 10:05:09 +0100795 filtered = 1;
796 }
797 if(filtered)
798 continue;
799 }
Marc Kupietza5f60042017-05-04 10:38:12 +0200800 if(merge_words > 0) {
801 if(c >= merge_words) {
802 if(l1_words > N / 2)
803 continue;
804 else
805 l1_words++;
806 } else {
807 if(l2_words > N / 2)
808 continue;
809 else
810 l2_words++;
811 }
812 }
Marc Kupietza5f60042017-05-04 10:38:12 +0200813 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 +0100814 fflush(stdout);
Marc Kupietza5f60042017-05-04 10:38:12 +0200815
Marc Kupietz271e2a42016-03-22 11:37:43 +0100816 HV* hash = newHV();
Marc Kupietza5f60042017-05-04 10:38:12 +0200817 SV* word = newSVpvf(&vocab[c * max_w], 0);
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100818 chosen[i] = c;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100819 if(latin_enc == 0) SvUTF8_on(word);
Marc Kupietza5f60042017-05-04 10:38:12 +0200820 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100821 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100822 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
823 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100824 AV *vector = newAV();
825 for (b = 0; b < size; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100826 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100827 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100828 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
829 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietza5f60042017-05-04 10:38:12 +0200830 i++;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100831 }
832 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
833
Marc Kupietz50485ba2016-03-23 09:13:14 +0100834 for(b=0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100835 best[b].wordi = -1L;
836 best[b].activation = 0;
837 best[b].probability = 0;
838 best[b].position = 0;
839 best[b].activation_sum = 0;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100840 }
841
Marc Kupietza77acce2017-11-30 16:59:07 +0100842 float total_activation = 0;
843
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200844 if (M2) {
845 printf("Waiting for syn threads to join\n");
846 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100847 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], (void *) &syn_nbs[a]);
Marc Kupietza77acce2017-11-30 16:59:07 +0100848 for (a = 0; a <= syn_threads; a++) {
849 total_activation += window_sums[a];
850 printf("window pos: %d, sum: %f\n", a, window_sums[a]);
851 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200852 printf("syn threads joint\n");
853 fflush(stdout);
Marc Kupietz50485ba2016-03-23 09:13:14 +0100854
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200855 for(b=0; b < syn_nbs[0]->length; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100856 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
857 best[b].position = -1; // syn_nbs[0]->pos[b];
858 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100859 best[b].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +0100860 best[b].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100861 best[b].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +0100862 best[b].cprobability = syn_nbs[0]->best[b].cprobability;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200863 }
Marc Kupietz30ca4342017-11-22 21:21:20 +0100864
865 float best_window_sum[MAX_NEIGHBOURS];
866 int found_index=0, i=0, j, w;
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100867 for(a=0; a < syn_threads; a++) {
868 for(b=0; b < syn_nbs[a]->length; b++) {
869 for(i=0; i < found_index; i++)
870 if(best[i].wordi == syn_nbs[a]->best[b].wordi)
871 break;
872 if(i >= found_index) {
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100873 best[found_index].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +0100874 best[found_index].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100875 best[found_index].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +0100876 best[found_index].cprobability = syn_nbs[a]->best[b].cprobability;
877 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 +0100878 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
879 // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
Marc Kupietz30ca4342017-11-22 21:21:20 +0100880 }
881 }
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100882 }
Marc Kupietza77acce2017-11-30 16:59:07 +0100883 sort_by =0; // ALWAYS AUTO-FOCUS
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100884 if(sort_by != 1 && sort_by != 2) { // sort by auto focus mean
Marc Kupietz30ca4342017-11-22 21:21:20 +0100885 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) -1);
Marc Kupietzbd41da32017-11-24 10:26:43 +0100886 int wpos;
Marc Kupietz4116b432017-12-06 14:15:32 +0100887 int bits_set = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100888 for(i=0; i < found_index; i++) {
Marc Kupietz4116b432017-12-06 14:15:32 +0100889 best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100890 for(w=1; w < (1 << syn_threads); w++) { // loop through all possible windows
Marc Kupietz4116b432017-12-06 14:15:32 +0100891 float word_window_sum = 0, word_window_average=0, word_cprobability_sum=0, word_activation_sum = 0, total_window_sum = 0;
892 bits_set = 0;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100893 for(a=0; a < syn_threads; a++) {
Marc Kupietz30ca4342017-11-22 21:21:20 +0100894 if((1 << a) & w) {
Marc Kupietzbd41da32017-11-24 10:26:43 +0100895 wpos = (a >= window? a+1 : a);
896 total_window_sum += window_sums[wpos];
Marc Kupietz30ca4342017-11-22 21:21:20 +0100897 }
898 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100899// printf("%d window-sum %f\n", w, total_window_sum);
900 for(a=0; a < syn_threads; a++) {
901 if((1 << a) & w) {
902 wpos = (a >= window? a+1 : a);
903 bits_set++;
904 for(b=0; b < syn_nbs[a]->length; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100905 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
Marc Kupietza77acce2017-11-30 16:59:07 +0100906// float acti = syn_nbs[a]->best[b].activation / total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100907// 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 +0100908// word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
909// 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 +0100910
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100911 word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
Marc Kupietza77acce2017-11-30 16:59:07 +0100912// word_window_sum += acti - (word_window_sum * acti); syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
913
Marc Kupietz4116b432017-12-06 14:15:32 +0100914 word_window_average += syn_nbs[a]->best[b].activation; // - word_window_average * syn_nbs[a]->best[b].activation; // conormalied activation sum
915 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 +0100916 word_activation_sum += syn_nbs[a]->best[b].activation;
917 if(syn_nbs[a]->best[b].activation > best[i].max_activation)
918 best[i].max_activation = syn_nbs[a]->best[b].activation;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100919 if(syn_nbs[a]->best[b].activation > best[i].heat[wpos] )
Marc Kupietz728b8ed2017-12-02 11:13:49 +0100920 best[i].heat[wpos] = syn_nbs[a]->best[b].activation;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100921 }
922 }
923 }
Marc Kupietz4116b432017-12-06 14:15:32 +0100924 if(bits_set) {
925 word_window_average /= bits_set;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100926// word_activation_sum /= bits_set;
927// word_window_sum /= bits_set;
Marc Kupietz4116b432017-12-06 14:15:32 +0100928 }
Marc Kupietza77acce2017-11-30 16:59:07 +0100929
930 word_window_sum /= total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100931
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100932 if(word_window_sum > best[i].probability) {
Marc Kupietz4116b432017-12-06 14:15:32 +0100933// best[i].position = w;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100934 best[i].probability = word_window_sum;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100935 }
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100936
Marc Kupietz4116b432017-12-06 14:15:32 +0100937 if(word_cprobability_sum > best[i].cprobability_sum) {
938 best[i].position = w;
939 best[i].cprobability_sum = word_cprobability_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100940 }
Marc Kupietz4116b432017-12-06 14:15:32 +0100941
942 best[i].average = word_window_average;
943// best[i].activation = word_activation_sum;
944 }
Marc Kupietz30ca4342017-11-22 21:21:20 +0100945 }
Marc Kupietze0e42132017-11-24 16:44:34 +0100946 qsort(best, found_index, sizeof(collocator), cmp_probability);
Marc Kupietz30ca4342017-11-22 21:21:20 +0100947// for(i=0; i < found_index; i++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100948// 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 +0100949// }
950
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100951 } else if(sort_by == 1) { // responsiveness any window position
952 int wpos;
953 for(i=0; i < found_index; i++) {
954 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
955 for(a=0; a < syn_threads; a++) {
956 wpos = (a >= window? a+1 : a);
957 for(b=0; b < syn_nbs[a]->length; b++)
958 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
959 best[i].probability += syn_nbs[a]->best[b].probability;
960 if(syn_nbs[a]->best[b].activation > 0.25)
961 best[i].position |= 1 << wpos;
962 if(syn_nbs[a]->best[b].activation > best[i].activation) {
963 best[i].activation = syn_nbs[a]->best[b].activation;
964 }
965 }
966 }
967 }
968 qsort(best, found_index, sizeof(collocator), cmp_activation);
969 } else if(sort_by == 2) { // single window position
Marc Kupietz30ca4342017-11-22 21:21:20 +0100970 for(a=1; a < syn_threads; a++) {
971 for(b=0; b < syn_nbs[a]->length; b++) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200972 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100973 if(syn_nbs[a]->best[b].activation > best[c].activation) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200974 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100975 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200976 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100977 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
978 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 +0200979 break;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100980 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200981 }
982 }
983 }
984 } else { // sort by mean p
985 for(a=1; a < syn_threads; a++) {
986 for(b=0; b < syn_nbs[a]->length; b++) {
987 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100988 if(target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
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 << 2*window) - 1; // syn_nbs[a]->pos[b];
994 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200995 break;
996 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100997 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100998 }
999 }
1000 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001001 array = newAV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001002 for (a = 0, i=0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
1003 long long c = best[a].wordi;
Marc Kupietza77acce2017-11-30 16:59:07 +01001004/*
Marc Kupietzd91212f2017-11-13 10:05:09 +01001005 if (dedupe) {
1006 int filtered=0;
1007 for (j=0; j<i; j++)
1008 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
1009 strcasestr(chosen[j], &vocab[c * max_w])) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001010 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
1011 filtered = 1;
1012 }
Marc Kupietzd91212f2017-11-13 10:05:09 +01001013 if(filtered)
1014 continue;
1015 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001016*/
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001017 chosen[i++]=c;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001018 HV* hash = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001019 SV* word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001020 AV* heat = newAV();
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001021 if(latin_enc == 0) SvUTF8_on(word);
1022 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001023 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001024 hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001025 hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001026 hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001027 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 +01001028 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 +01001029 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001030 best[a].heat[5]=0;
1031 for(i=10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i]));
1032 hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV*)heat), 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001033 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietz271e2a42016-03-22 11:37:43 +01001034 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001035 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001036 }
Marc Kupietz000ad862016-02-26 14:59:12 +01001037end:
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001038 words = old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001039 free(best);
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001040 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +01001041}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +01001042
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001043int dump_vecs(char *fname) {
1044 long i, j;
1045 FILE *f;
1046 /* if(words>200000) */
1047 /* words=200000; */
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001048
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001049 if((f=fopen(fname, "w")) == NULL) {
1050 fprintf(stderr, "cannot open %s for writing\n", fname);
1051 return(-1);
1052 }
1053 fprintf(f, "%lld %lld\n", words, size);
1054 for (i=0; i < words; i++) {
1055 fprintf(f, "%s ", &vocab[i * max_w]);
1056 for(j=0; j < size - 1; j++)
1057 fprintf(f, "%f ", M[i*size + j]);
1058 fprintf(f, "%f\n", M[i*size + j]);
1059 }
1060 fclose(f);
1061 return(0);
1062}
Marc Kupietzdc22b982015-10-09 09:19:34 +02001063