blob: 402a0d5ffbdb0c14a2328cf3444c87bd02c5548c [file] [log] [blame]
Marc Kupietzdc22b982015-10-09 09:19:34 +02001#!/usr/local/bin/perl
2use Inline C;
Marc Kupietza2e64502016-04-27 09:53:51 +02003use Inline C => Config => CLEAN_AFTER_BUILD => 0, ccflags => $Config{ccflags}." -O4";
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 Kupietz247500f2015-10-09 11:29:01 +02008use Encode qw(decode encode);
Marc Kupietza5b90152016-03-15 17:39:19 +01009use Getopt::Std;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010010use Mojo::Server::Daemon;
Marc Kupietzffef9302017-11-07 15:58:01 +010011use Cwd;
12app->static->paths->[0] = getcwd;
13
Marc Kupietzd4227392016-03-01 16:45:12 +010014plugin 'Log::Access';
Marc Kupietzb3422c12017-07-04 14:12:11 +020015plugin "RequestBase";
Marc Kupietzdc22b982015-10-09 09:19:34 +020016
Marc Kupietza5b90152016-03-15 17:39:19 +010017our $opt_i = 0; # latin1-input?
18our $opt_l = undef;
19our $opt_p = 5676;
Marc Kupietza2e64502016-04-27 09:53:51 +020020our $opt_m;
Marc Kupietz6ed81872016-04-27 14:04:04 +020021our $opt_M;
Marc Kupietz43ee87e2016-04-25 10:50:08 +020022our $opt_n = '';
23our $opt_d;
Marc Kupietz5c3887d2016-04-28 08:53:35 +020024our $opt_G;
Marc Kupietza5b90152016-03-15 17:39:19 +010025
Marc Kupietz6ed81872016-04-27 14:04:04 +020026my %marked;
Marc Kupietz793413b2016-04-02 21:48:57 +020027my $training_args="";
Marc Kupietza2e64502016-04-27 09:53:51 +020028my $mergedEnd=0;
Marc Kupietz15987412017-11-07 15:56:58 +010029my %cache;
Marc Kupietz793413b2016-04-02 21:48:57 +020030
Marc Kupietza5f60042017-05-04 10:38:12 +020031getopts('d:Gil:p:m:n:M:');
Marc Kupietz6ed81872016-04-27 14:04:04 +020032
33if($opt_M) {
Marc Kupietzed930212016-04-27 15:42:38 +020034 open my $handle, '<:encoding(UTF-8)', $opt_M
35 or die "Can't open '$opt_M' for reading: $!";
36 while(<$handle>) {
Marc Kupietz6ed81872016-04-27 14:04:04 +020037 foreach my $mw (split /\s+/) {
38 $marked{$mw}=1
39 }
40 }
Marc Kupietzed930212016-04-27 15:42:38 +020041 close($handle);
Marc Kupietz6ed81872016-04-27 14:04:04 +020042}
Marc Kupietza5b90152016-03-15 17:39:19 +010043
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010044# -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 +010045if(!$ARGV[0]) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010046 init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0));
Marc Kupietz2cb667e2016-03-10 09:44:12 +010047} else {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010048 init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0));
Marc Kupietz793413b2016-04-02 21:48:57 +020049 if(open(FILE, "$ARGV[0].args")) {
50 $training_args = <FILE>;
51 }
52 close(FILE);
Marc Kupietz2cb667e2016-03-10 09:44:12 +010053}
Marc Kupietzdc22b982015-10-09 09:19:34 +020054
Marc Kupietza2e64502016-04-27 09:53:51 +020055if($opt_m) {
56 $mergedEnd = mergeVectors($opt_m);
57}
58
Marc Kupietz6ed81872016-04-27 14:04:04 +020059
Marc Kupietz43ee87e2016-04-25 10:50:08 +020060if($opt_d) { # -d: dump vecs and exit
61 dump_vecs($opt_d);
62 exit;
63}
64
Marc Kupietza5b90152016-03-15 17:39:19 +010065my $daemon = Mojo::Server::Daemon->new(
66 app => app,
67 listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"]
68);
69
Marc Kupietz5c3887d2016-04-28 08:53:35 +020070if($opt_G) {
71 print "Filtering garbage\n";
72 filter_garbage();
73}
74
Marc Kupietz554aff52017-11-09 14:42:09 +010075get '*/js/*' => sub {
Marc Kupietzffef9302017-11-07 15:58:01 +010076 my $c = shift;
77 my $url = $c->req->url;
78 $url =~ s@/derekovecs@@g;
79 $c->app->log->info("GET: " . $url);
80 $c->reply->static($url);
81};
82
Marc Kupietzdc22b982015-10-09 09:19:34 +020083get '/' => sub {
84 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +020085 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +020086 my $word=$c->param('word');
Marc Kupietz44bee3c2016-02-25 16:26:29 +010087 my $no_nbs=$c->param('n') || 100;
88 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +010089 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +010090 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +010091 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +020092 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +010093 my $sort=$c->param('sort') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +020094 my $json=$c->param('json') || 0;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +010095 my $cutoff=$c->param('cutoff') || 1000000;
Marc Kupietz6b2975c2016-03-18 21:59:33 +010096 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010097 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +010098 my @collocations;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010099 if(defined($word) && $word !~ /^\s*$/) {
100 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100101 $word =~ s/\s+/ /g;
102 for my $w (split(' *\| *', $word)) {
Marc Kupietz15987412017-11-07 15:56:58 +0100103 if ($cache{$w}) {
104 $c->app->log->info("Getting $w results from cache");
105 $res = $cache{$w}
Marc Kupietza5b90152016-03-15 17:39:19 +0100106 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100107 $c->app->log->info('Looking for neighbours of '.$w);
108 if($opt_i) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100109 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff);
Marc Kupietz15987412017-11-07 15:56:58 +0100110 } else {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100111 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff);
Marc Kupietz15987412017-11-07 15:56:58 +0100112 }
113 $cache{$w} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100114 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100115 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100116 }
117 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100118 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200119 if($json) {
120 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
121 } else {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100122 $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, marked=>\%marked, lists=> \@lists, collocators=> $res->{syntagmatic});
Marc Kupietzb613b052016-04-28 14:11:59 +0200123 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200124};
125
Marc Kupietza5b90152016-03-15 17:39:19 +0100126$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200127
128exit;
129
130__END__
131
132__C__
133#include <stdio.h>
134#include <string.h>
135#include <math.h>
136#include <malloc.h>
137#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +0100138#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +0100139#include <pthread.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200140
141#define max_size 2000
142#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100143#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100144#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +0100145#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100146#define MAX_CC 50
147#define EXP_TABLE_SIZE 1000
148#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +0100149#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +0200150
151//the thread function
152void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +0100153
154typedef struct {
155 long long *index;
156 float *dist;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100157 float *norm;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100158 long long *pos;
Marc Kupietz80abb442016-03-23 21:04:08 +0100159 int length;
Marc Kupietz000ad862016-02-26 14:59:12 +0100160} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100161
Marc Kupietz000ad862016-02-26 14:59:12 +0100162typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +0100163 long long wordi[MAX_NEIGHBOURS];
164 char sep[MAX_NEIGHBOURS];
165 int length;
166} wordlist;
167
168typedef struct {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100169 long cutoff;
Marc Kupietz48c29682016-03-19 11:30:43 +0100170 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100171 char *token;
172 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100173 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100174 unsigned long upto;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100175 float *target_sums;
Marc Kupietz000ad862016-02-26 14:59:12 +0100176} knnpars;
177
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200178float *M, *M2=0L, *syn1neg_window, *expTable;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200179char *vocab;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200180char *garbage = NULL;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100181
Marc Kupietza2e64502016-04-27 09:53:51 +0200182long long words, size, merged_end;
Marc Kupietza5f60042017-05-04 10:38:12 +0200183long long merge_words = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100184int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100185int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100186int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200187
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100188int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100189 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100190 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100191 long long a, b, c, d, cn;
192 float len;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200193 double val;
Marc Kupietz82b02672016-02-26 12:32:25 +0100194
Marc Kupietz67c20282016-02-26 09:42:00 +0100195 char binvecs_fname[256], binwords_fname[256];
196 strcpy(binwords_fname, file_name);
197 strcat(binwords_fname, ".words");
198 strcpy(binvecs_fname, file_name);
199 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200200
Marc Kupietza5b90152016-03-15 17:39:19 +0100201 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200202 f = fopen(file_name, "rb");
203 if (f == NULL) {
204 printf("Input file %s not found\n", file_name);
205 return -1;
206 }
207 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100208 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200209 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100210 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
211 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100212 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
213 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
214 if (M == NULL) {
215 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
216 return -1;
217 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200218 if(strstr(file_name, ".txt")) {
219 for (b = 0; b < words; b++) {
220 a = 0;
221 while (1) {
222 vocab[b * max_w + a] = fgetc(f);
223 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
224 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
225 }
226 vocab[b * max_w + a] = 0;
227 len = 0;
228 for (a = 0; a < size; a++) {
229 fscanf(f, "%lf", &val);
230 M[a + b * size] = val;
231 len += val * val;
232 }
233 len = sqrt(len);
234 for (a = 0; a < size; a++) M[a + b * size] /= len;
235 }
236 } else {
237 for (b = 0; b < words; b++) {
238 a = 0;
239 while (1) {
240 vocab[b * max_w + a] = fgetc(f);
241 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
242 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
243 }
244 vocab[b * max_w + a] = 0;
245 fread(&M[b * size], sizeof(float), size, f);
246 len = 0;
247 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
248 len = sqrt(len);
249 for (a = 0; a < size; a++) M[a + b * size] /= len;
250 }
251 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100252 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
253 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
254 fclose(binvecs);
255 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
256 fclose(binwords);
257 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100258 }
259 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
260 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
261 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
262 if (M == MAP_FAILED || vocab == MAP_FAILED) {
263 close(binvecs_fd);
264 close(binwords_fd);
265 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
266 exit(-1);
267 }
268 } else {
269 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
270 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100271 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200272 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100273
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200274 if(net_name && strlen(net_name) > 0) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100275 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
276 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
277 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100278 // munmap(M, sizeof(float) * words * size);
279 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 +0200280 if (M2 == MAP_FAILED) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100281 close(net_fd);
282 fprintf(stderr, "Cannot mmap %s\n", net_name);
283 exit(-1);
284 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100285 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100286 } else {
287 fprintf(stderr, "Cannot open %s\n", net_name);
288 exit(-1);
289 }
290 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
291 }
292
293 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
294 for (i = 0; i < EXP_TABLE_SIZE; i++) {
295 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
296 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
297 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200298 return 0;
299}
300
Marc Kupietza2e64502016-04-27 09:53:51 +0200301long mergeVectors(char *file_name){
302 FILE *f, *binvecs, *binwords;
303 int binwords_fd, binvecs_fd, net_fd, i;
304 long long a, b, c, d, cn;
305 float len;
306 float *merge_vecs;
307 char *merge_vocab;
Marc Kupietza5f60042017-05-04 10:38:12 +0200308 /* long long merge_words, merge_size; */
309 long long merge_size;
Marc Kupietza2e64502016-04-27 09:53:51 +0200310
311 char binvecs_fname[256], binwords_fname[256];
312 strcpy(binwords_fname, file_name);
313 strcat(binwords_fname, ".words");
314 strcpy(binvecs_fname, file_name);
315 strcat(binvecs_fname, ".vecs");
316
317 f = fopen(file_name, "rb");
318 if (f == NULL) {
319 printf("Input file %s not found\n", file_name);
320 exit -1;
321 }
322 fscanf(f, "%lld", &merge_words);
323 fscanf(f, "%lld", &merge_size);
324 if(merge_size != size){
325 fprintf(stderr, "vectors must have the same length\n");
326 exit(-1);
327 }
328 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
329 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
330 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
331 if (merge_vecs == NULL || merge_vocab == NULL) {
332 close(binvecs_fd);
333 close(binwords_fd);
334 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
335 exit(-1);
336 }
337 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
338 read(binwords_fd, merge_vocab, merge_words * max_w);
339 } else {
340 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
341 exit(-1);
342 }
343 printf("Successfully reallocated memory\nMerging...\n");
344 fflush(stdout);
345 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
346 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
347 munmap(M, words * size * sizeof(float));
348 munmap(vocab, words * max_w);
349 M = merge_vecs;
350 vocab = merge_vocab;
351 merged_end = merge_words;
352 words += merge_words;
353 fclose(f);
354 printf("merged_end: %lld, words: %lld\n", merged_end, words);
355 return((long) merged_end);
356}
357
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200358void filter_garbage() {
359 long i;
Marc Kupietzab591a82016-04-28 14:08:49 +0200360 unsigned char *w, previous, c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200361 garbage = malloc(words);
362 memset(garbage, 0, words);
363 for (i = 0; i < words; i++) {
364 w = vocab + i * max_w;
365 previous = 0;
Marc Kupietzab591a82016-04-28 14:08:49 +0200366 while((c = *w++) && !garbage[i]) {
367 if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
368 (previous == '-' && (c & 32)) ||
369 (previous == 0xc2 && (c == 0xa4 || c == 0xb6 ))
370 ) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200371 garbage[i]=1;
372 continue;
373 }
374 previous = c;
375 }
376 }
377 return;
378}
379
Marc Kupietz271e2a42016-03-22 11:37:43 +0100380void *getCollocators(knnpars *pars) {
381 int N = pars->N;
382 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100383 knn *nbs = NULL;
384 long window_layer_size = size * window * 2;
385 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
386 float f, max_f, maxmax_f;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100387 float *target_sums, *bestf, *bestn, worstbest, wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100388 long long *besti, *bestp;
Marc Kupietzd5642582016-03-19 22:23:13 +0100389
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200390 if(M2 == NULL || cc == -1)
Marc Kupietzd5642582016-03-19 22:23:13 +0100391 return NULL;
392
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100393 a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100394 besti = malloc(N * sizeof(long long));
395 bestp = malloc(N * sizeof(long long));
396 bestf = malloc(N * sizeof(float));
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100397 bestn = malloc(N * sizeof(float));
398
Marc Kupietz271e2a42016-03-22 11:37:43 +0100399 worstbest = MIN_RESP;
400
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100401 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100402 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100403 for (b = 0; b < N; b++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100404 besti[b] = -1;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100405 bestn[b] = 1;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100406 bestf[b] = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100407 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100408
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100409 d = cc;
410 maxmax_f = -1;
411 maxmax_target = 0;
412
Marc Kupietz271e2a42016-03-22 11:37:43 +0100413 for (a = pars->from; a < pars->upto; a++) {
414 if(a >= window)
415 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100416 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100417 printf("window pos: %ld\n", a);
418 if (a != window) {
419 max_f = -1;
420 window_offset = a * size;
421 if (a > window)
422 window_offset -= size;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100423 for(target = 0; target < pars->cutoff; target ++) {
424 if(garbage && garbage[target]) continue;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100425 if(target == d)
426 continue;
427 f = 0;
428 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100429 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100430 if (f < -MAX_EXP)
431 continue;
432 else if (f > MAX_EXP)
433 continue;
434 else
435 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100436 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100437
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100438 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100439 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100440 for (b = 0; b < N; b++) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100441 if (f > bestf[b]) {
Marc Kupietz33679a32016-03-22 08:49:39 +0100442 memmove(bestf + b + 1, bestf + b, (N - b -1) * sizeof(float));
443 memmove(besti + b + 1, besti + b, (N - b -1) * sizeof(long long));
444 memmove(bestp + b + 1, bestp + b, (N - b -1) * sizeof(long long));
445 bestf[b] = f;
446 besti[b] = target;
447 bestp[b] = window-a;
448 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100449 }
450 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100451 if(b == N - 1)
452 worstbest = bestf[N-1];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100453 }
454 }
455 printf("%d %.2f\n", max_target, max_f);
456 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
457 if(max_f > maxmax_f) {
458 maxmax_f = max_f;
459 maxmax_target = max_target;
460 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100461 for (b = 0; b < N; b++)
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100462 if(bestp[b] == window-a)
463 bestn[b] = bestf[b] / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100464 } else {
465 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
466 }
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100467
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100468 }
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100469 for (b = 0; b < pars->cutoff; b++)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100470 pars->target_sums[b] += (target_sums[b] / wpos_sum ) / (window * 2);
471 free(target_sums);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100472 for(b=0; b<N && besti[b] >= 0; b++) // THIS LOOP IS NEEDED (b...)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100473 printf("%s %.2f %d * ", &vocab[besti[b]*max_w], bestf[b], bestp[b]);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100474 printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100475 nbs = malloc(sizeof(knn));
476 nbs->index = besti;
477 nbs->dist = bestf;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100478 nbs->norm = bestn;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100479 nbs->pos = bestp;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100480 nbs->length = b-1;
481 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100482}
483
Marc Kupietza2e64502016-04-27 09:53:51 +0200484wordlist *getTargetWords(char *st1, int search_backw) {
Marc Kupietz48c29682016-03-19 11:30:43 +0100485 wordlist *wl = malloc(sizeof(wordlist));
486 char st[100][max_size], sep[100];
487 long a, b=0, c=0, cn=0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200488 int unmerged;
489
Marc Kupietzdc22b982015-10-09 09:19:34 +0200490 while (1) {
491 st[cn][b] = st1[c];
492 b++;
493 c++;
494 st[cn][b] = 0;
495 if (st1[c] == 0) break;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100496 if (st1[c] == ' ' || st1[c] == '-') {
497 sep[cn++] = st1[c];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200498 b = 0;
499 c++;
500 }
501 }
502 cn++;
503 for (a = 0; a < cn; a++) {
Marc Kupietza2e64502016-04-27 09:53:51 +0200504 if(search_backw) {
505 for (b = words - 1; b >= 0; b--) if (!strcmp(&vocab[b * max_w], st[a])) break;
506 } else {
507 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
508 }
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100509 if (b == words) b = -1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100510 wl->wordi[a] = b;
511 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], wl->wordi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200512 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100513 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100514 cn--;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200515 break;
516 }
517 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100518 wl->length=cn;
519 return(wl);
520}
521
522void *_get_neighbours(knnpars *pars) {
523 char *st1 = pars->token;
524 int N = pars->N;
525 long from = pars -> from;
526 unsigned long upto = pars -> upto;
527 char file_name[max_size], st[100][max_size], *sep;
528 float dist, len, *bestd, vec[max_size];
529 long long a, b, c, d, cn, *bi, *besti;
530 char ch;
531 knn *nbs = NULL;
532 wordlist *wl = pars->wl;
533
534 besti = malloc(N * sizeof(long long));
535 bestd = malloc(N * sizeof(float));
536
537 float worstbest=-1;
538
539 for (a = 0; a < N; a++) bestd[a] = 0;
540 a = 0;
541 bi = wl->wordi;
542 cn = wl->length;
543 sep = wl->sep;
544 b = bi[0];
545 c = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100546 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100547 N = 0;
548 goto end;
549 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200550 for (a = 0; a < size; a++) vec[a] = 0;
551 for (b = 0; b < cn; b++) {
552 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100553 if(b>0 && sep[b-1] == '-')
554 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
555 else
556 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200557 }
558 len = 0;
559 for (a = 0; a < size; a++) len += vec[a] * vec[a];
560 len = sqrt(len);
561 for (a = 0; a < size; a++) vec[a] /= len;
562 for (a = 0; a < N; a++) bestd[a] = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100563 for (c = from; c < upto; c++) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200564 if(garbage && garbage[c]) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200565 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100566// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100567// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
568// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200569 dist = 0;
570 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100571 if(dist > worstbest) {
572 for (a = 0; a < N; a++) {
573 if (dist > bestd[a]) {
Marc Kupietz33679a32016-03-22 08:49:39 +0100574 memmove(bestd + a + 1, bestd + a, (N - a -1) * sizeof(float));
575 memmove(besti + a + 1, besti + a, (N - a -1) * sizeof(long long));
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100576 bestd[a] = dist;
577 besti[a] = c;
578 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200579 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200580 }
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100581 worstbest = bestd[N-1];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200582 }
583 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100584
Marc Kupietz000ad862016-02-26 14:59:12 +0100585 nbs = malloc(sizeof(knn));
586 nbs->index = besti;
587 nbs->dist = bestd;
588 nbs->length = N;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100589end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100590 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200591}
592
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100593
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100594SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100595 HV *result = newHV();
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100596 float *target_sums, bestd[MAX_NEIGHBOURS], bestn[MAX_NEIGHBOURS], bests[MAX_NEIGHBOURS], vec[max_size];
Marc Kupietza5f60042017-05-04 10:38:12 +0200597 long long old_words;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100598 long besti[MAX_NEIGHBOURS], bestp[MAX_NEIGHBOURS], a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100599 knn *para_nbs[MAX_THREADS];
600 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100601 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100602 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100603 wordlist *wl;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200604 int syn_threads = (M2? window * 2 : 0);
605 int para_threads = num_threads - syn_threads;
Marc Kupietz48c29682016-03-19 11:30:43 +0100606
Marc Kupietz000ad862016-02-26 14:59:12 +0100607 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
608
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100609 if(cutoff < 1)
610 cutoff=words;
611
Marc Kupietza2e64502016-04-27 09:53:51 +0200612 wl = getTargetWords(st1, search_backw);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100613 if(wl->length < 1)
614 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100615
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100616 old_words = cutoff;
Marc Kupietza5f60042017-05-04 10:38:12 +0200617 if(merge_words > 0)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100618 cutoff = merge_words * 1.25; /* HACK */
619 slice = cutoff / para_threads;
Marc Kupietza5f60042017-05-04 10:38:12 +0200620
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100621 a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float));
622 for(a = 0; a < cutoff; a++)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100623 target_sums[a] = 0;
624
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200625 printf("Starting %d threads\n", para_threads);
626 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100627 for(a=0; a < para_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100628 pars[a].cutoff = cutoff;
Marc Kupietz000ad862016-02-26 14:59:12 +0100629 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100630 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100631 pars[a].N = N;
632 pars[a].from = a*slice;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100633 pars[a].upto = ((a+1)*slice > cutoff? cutoff:(a+1)*slice);
Marc Kupietz000ad862016-02-26 14:59:12 +0100634 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
635 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200636 if(M2) {
637 for(a=0; a < syn_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100638 pars[a + para_threads].cutoff = cutoff;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200639 pars[a + para_threads].target_sums = target_sums;
640 pars[a + para_threads].wl = wl;
641 pars[a + para_threads].N = N;
642 pars[a + para_threads].from = a;
643 pars[a + para_threads].upto = a+1;
644 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
645 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100646 }
647 printf("Waiting for para threads to join\n");
648 fflush(stdout);
649 for (a = 0; a < para_threads; a++) pthread_join(pt[a], &para_nbs[a]);
650 printf("Para threads joint\n");
651 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +0100652
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200653 /* if(!syn_nbs[0]) */
654 /* goto end; */
Marc Kupietz000ad862016-02-26 14:59:12 +0100655
656 for(b=0; b < N; b++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100657 besti[b] = para_nbs[0]->index[b];
658 bestd[b] = para_nbs[0]->dist[b];
Marc Kupietz000ad862016-02-26 14:59:12 +0100659 }
660
Marc Kupietz271e2a42016-03-22 11:37:43 +0100661 for(a=1; a < para_threads; a++) {
662 for(b=0; b < para_nbs[a]->length && para_nbs[a]->index[b] >= 0; b++) {
Marc Kupietza5f60042017-05-04 10:38:12 +0200663 for(c=0; c < N * para_threads; c++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100664 if(para_nbs[a]->dist[b] > bestd[c]) {
Marc Kupietz000ad862016-02-26 14:59:12 +0100665 for(d=N-1; d>c; d--) {
666 bestd[d] = bestd[d-1];
667 besti[d] = besti[d-1];
668 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100669 besti[c] = para_nbs[a]->index[b];
670 bestd[c] = para_nbs[a]->dist[b];
Marc Kupietz000ad862016-02-26 14:59:12 +0100671 break;
672 }
673 }
674 }
675 }
676
Marc Kupietz271e2a42016-03-22 11:37:43 +0100677 AV* array = newAV();
Marc Kupietza5f60042017-05-04 10:38:12 +0200678 int i;
679 int l1_words=0, l2_words=0;
680 for (a = 0, i = 0; i < N && a < 600; a++) {
681 long long c = besti[a];
682 if(merge_words > 0) {
683 if(c >= merge_words) {
684 if(l1_words > N / 2)
685 continue;
686 else
687 l1_words++;
688 } else {
689 if(l2_words > N / 2)
690 continue;
691 else
692 l2_words++;
693 }
694 }
695 fflush(stdout);
696 printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
697
Marc Kupietz271e2a42016-03-22 11:37:43 +0100698 HV* hash = newHV();
Marc Kupietza5f60042017-05-04 10:38:12 +0200699 SV* word = newSVpvf(&vocab[c * max_w], 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100700 if(latin_enc == 0) SvUTF8_on(word);
Marc Kupietza5f60042017-05-04 10:38:12 +0200701 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100702 hv_store(hash, "word", strlen("word"), word , 0);
703 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
704 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
705 AV *vector = newAV();
706 for (b = 0; b < size; b++) {
707 av_push(vector, newSVnv(M[b + besti[a] * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100708 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100709 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
710 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietza5f60042017-05-04 10:38:12 +0200711 i++;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100712 }
713 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
714
Marc Kupietz50485ba2016-03-23 09:13:14 +0100715 for(b=0; b < MAX_NEIGHBOURS; b++) {
716 besti[b] = -1L;
717 bestd[b] = 0;
718 bestn[b] = 0;
719 bestp[b] = 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100720 bests[b] = 0;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100721 }
722
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200723 if (M2) {
724 printf("Waiting for syn threads to join\n");
725 fflush(stdout);
726 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], &syn_nbs[a]);
727 printf("syn threads joint\n");
728 fflush(stdout);
Marc Kupietz50485ba2016-03-23 09:13:14 +0100729
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200730 for(b=0; b < syn_nbs[0]->length; b++) {
731 besti[b] = syn_nbs[0]->index[b];
732 bestd[b] = syn_nbs[0]->dist[b];
733 bestn[b] = syn_nbs[0]->norm[b];
734 bestp[b] = syn_nbs[0]->pos[b];
735 bests[b] = target_sums[syn_nbs[0]->index[b]];
736 }
737
738 if(sort_by != 1) { // sort by responsiveness
739 for(a=1; a < syn_threads; a++) {
740 for(b=0; b < syn_nbs[a]->length; b++) {
741 for(c=0; c < MAX_NEIGHBOURS; c++) {
742 if(syn_nbs[a]->dist[b] > bestd[c]) {
743 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
744 bestd[d] = bestd[d-1];
745 besti[d] = besti[d-1];
746 bestn[d] = bestn[d-1];
747 bestp[d] = bestp[d-1];
748 }
749 besti[c] = syn_nbs[a]->index[b];
750 bestd[c] = syn_nbs[a]->dist[b];
751 bestn[c] = syn_nbs[a]->norm[b];
752 bestp[c] = syn_nbs[a]->pos[b];
753 break;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100754 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200755 }
756 }
757 }
758 } else { // sort by mean p
759 for(a=1; a < syn_threads; a++) {
760 for(b=0; b < syn_nbs[a]->length; b++) {
761 for(c=0; c < MAX_NEIGHBOURS; c++) {
762 if(target_sums[syn_nbs[a]->index[b]] > bests[c]) {
763 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
764 bestd[d] = bestd[d-1];
765 besti[d] = besti[d-1];
766 bestn[d] = bestn[d-1];
767 bestp[d] = bestp[d-1];
768 bests[d] = bests[d-1];
769 }
770 besti[c] = syn_nbs[a]->index[b];
771 bestd[c] = syn_nbs[a]->dist[b];
772 bestn[c] = syn_nbs[a]->norm[b];
773 bestp[c] = syn_nbs[a]->pos[b];
774 bests[c] = target_sums[syn_nbs[a]->index[b]];
775 break;
776 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100777 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100778 }
779 }
780 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200781 array = newAV();
782 for (a = 0; a < MAX_NEIGHBOURS && besti[a] >= 0; a++) {
783 HV* hash = newHV();
784 SV* word = newSVpvf(&vocab[besti[a] * max_w], 0);
785 if(latin_enc == 0) SvUTF8_on(word);
786 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzb18978b2017-11-09 14:51:17 +0100787 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200788 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
789 hv_store(hash, "norm", strlen("norm"), newSVnv(bestn[a]), 0);
790 hv_store(hash, "sum", strlen("sum"), newSVnv(target_sums[besti[a]]), 0);
791 hv_store(hash, "pos", strlen("pos"), newSVnv(bestp[a]), 0);
792 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietz271e2a42016-03-22 11:37:43 +0100793 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200794 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100795 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100796end:
Marc Kupietza5f60042017-05-04 10:38:12 +0200797 words = old_words;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100798 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +0100799}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100800
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200801int dump_vecs(char *fname) {
802 long i, j;
803 FILE *f;
804 /* if(words>200000) */
805 /* words=200000; */
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100806
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200807 if((f=fopen(fname, "w")) == NULL) {
808 fprintf(stderr, "cannot open %s for writing\n", fname);
809 return(-1);
810 }
811 fprintf(f, "%lld %lld\n", words, size);
812 for (i=0; i < words; i++) {
813 fprintf(f, "%s ", &vocab[i * max_w]);
814 for(j=0; j < size - 1; j++)
815 fprintf(f, "%f ", M[i*size + j]);
816 fprintf(f, "%f\n", M[i*size + j]);
817 }
818 fclose(f);
819 return(0);
820}
Marc Kupietzdc22b982015-10-09 09:19:34 +0200821