blob: a956b22edb1e70bdc9ade9ef6b382586e1a2024a [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 Kupietzc469f3b2017-11-13 14:07:36 +010094 my $csv=$c->param('csv') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +020095 my $json=$c->param('json') || 0;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +010096 my $cutoff=$c->param('cutoff') || 1000000;
Marc Kupietzd91212f2017-11-13 10:05:09 +010097 my $dedupe=$c->param('dedupe') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +010098 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010099 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100100 my @collocations;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100101 if(defined($word) && $word !~ /^\s*$/) {
102 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100103 $word =~ s/\s+/ /g;
104 for my $w (split(' *\| *', $word)) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100105 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100106 $c->app->log->info("Getting $w results from cache");
Marc Kupietzd91212f2017-11-13 10:05:09 +0100107 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe}
Marc Kupietza5b90152016-03-15 17:39:19 +0100108 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100109 $c->app->log->info('Looking for neighbours of '.$w);
110 if($opt_i) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100111 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe);
Marc Kupietz15987412017-11-07 15:56:58 +0100112 } else {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100113 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe);
Marc Kupietz15987412017-11-07 15:56:58 +0100114 }
115 $cache{$w} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100116 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100117 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100118 }
119 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100120 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200121 if($json) {
122 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100123 } elsif($csv) {
124 my $csv_data="";
125 for (my $i=0; $i <= $no_nbs; $i++) {
126 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
127 }
128 for (my $i=0; $i < $no_nbs; $i++) {
129 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
130 }
131 chop $csv_data;
132 chop $csv_data;
133 $csv_data .= "\n";
134 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200135 } else {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100136 $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 +0200137 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200138};
139
Marc Kupietza5b90152016-03-15 17:39:19 +0100140$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200141
142exit;
143
144__END__
145
146__C__
147#include <stdio.h>
148#include <string.h>
149#include <math.h>
150#include <malloc.h>
151#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +0100152#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +0100153#include <pthread.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200154
155#define max_size 2000
156#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100157#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100158#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +0100159#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100160#define MAX_CC 50
161#define EXP_TABLE_SIZE 1000
162#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +0100163#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +0200164
165//the thread function
166void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +0100167
168typedef struct {
169 long long *index;
170 float *dist;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100171 float *norm;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100172 long long *pos;
Marc Kupietz80abb442016-03-23 21:04:08 +0100173 int length;
Marc Kupietz000ad862016-02-26 14:59:12 +0100174} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100175
Marc Kupietz000ad862016-02-26 14:59:12 +0100176typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +0100177 long long wordi[MAX_NEIGHBOURS];
178 char sep[MAX_NEIGHBOURS];
179 int length;
180} wordlist;
181
182typedef struct {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100183 long cutoff;
Marc Kupietz48c29682016-03-19 11:30:43 +0100184 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100185 char *token;
186 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100187 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100188 unsigned long upto;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100189 float *target_sums;
Marc Kupietz000ad862016-02-26 14:59:12 +0100190} knnpars;
191
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200192float *M, *M2=0L, *syn1neg_window, *expTable;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200193char *vocab;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200194char *garbage = NULL;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100195
Marc Kupietza2e64502016-04-27 09:53:51 +0200196long long words, size, merged_end;
Marc Kupietza5f60042017-05-04 10:38:12 +0200197long long merge_words = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100198int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100199int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100200int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200201
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100202int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100203 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100204 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100205 long long a, b, c, d, cn;
206 float len;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200207 double val;
Marc Kupietz82b02672016-02-26 12:32:25 +0100208
Marc Kupietz67c20282016-02-26 09:42:00 +0100209 char binvecs_fname[256], binwords_fname[256];
210 strcpy(binwords_fname, file_name);
211 strcat(binwords_fname, ".words");
212 strcpy(binvecs_fname, file_name);
213 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200214
Marc Kupietza5b90152016-03-15 17:39:19 +0100215 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200216 f = fopen(file_name, "rb");
217 if (f == NULL) {
218 printf("Input file %s not found\n", file_name);
219 return -1;
220 }
221 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100222 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200223 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100224 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
225 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100226 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
227 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
228 if (M == NULL) {
229 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
230 return -1;
231 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200232 if(strstr(file_name, ".txt")) {
233 for (b = 0; b < words; b++) {
234 a = 0;
235 while (1) {
236 vocab[b * max_w + a] = fgetc(f);
237 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
238 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
239 }
240 vocab[b * max_w + a] = 0;
241 len = 0;
242 for (a = 0; a < size; a++) {
243 fscanf(f, "%lf", &val);
244 M[a + b * size] = val;
245 len += val * val;
246 }
247 len = sqrt(len);
248 for (a = 0; a < size; a++) M[a + b * size] /= len;
249 }
250 } else {
251 for (b = 0; b < words; b++) {
252 a = 0;
253 while (1) {
254 vocab[b * max_w + a] = fgetc(f);
255 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
256 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
257 }
258 vocab[b * max_w + a] = 0;
259 fread(&M[b * size], sizeof(float), size, f);
260 len = 0;
261 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
262 len = sqrt(len);
263 for (a = 0; a < size; a++) M[a + b * size] /= len;
264 }
265 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100266 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
267 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
268 fclose(binvecs);
269 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
270 fclose(binwords);
271 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100272 }
273 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
274 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
275 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
276 if (M == MAP_FAILED || vocab == MAP_FAILED) {
277 close(binvecs_fd);
278 close(binwords_fd);
279 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
280 exit(-1);
281 }
282 } else {
283 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
284 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100285 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200286 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100287
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200288 if(net_name && strlen(net_name) > 0) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100289 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
290 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
291 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100292 // munmap(M, sizeof(float) * words * size);
293 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 +0200294 if (M2 == MAP_FAILED) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100295 close(net_fd);
296 fprintf(stderr, "Cannot mmap %s\n", net_name);
297 exit(-1);
298 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100299 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100300 } else {
301 fprintf(stderr, "Cannot open %s\n", net_name);
302 exit(-1);
303 }
304 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
305 }
306
307 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
308 for (i = 0; i < EXP_TABLE_SIZE; i++) {
309 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
310 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
311 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200312 return 0;
313}
314
Marc Kupietza2e64502016-04-27 09:53:51 +0200315long mergeVectors(char *file_name){
316 FILE *f, *binvecs, *binwords;
317 int binwords_fd, binvecs_fd, net_fd, i;
318 long long a, b, c, d, cn;
319 float len;
320 float *merge_vecs;
321 char *merge_vocab;
Marc Kupietza5f60042017-05-04 10:38:12 +0200322 /* long long merge_words, merge_size; */
323 long long merge_size;
Marc Kupietza2e64502016-04-27 09:53:51 +0200324
325 char binvecs_fname[256], binwords_fname[256];
326 strcpy(binwords_fname, file_name);
327 strcat(binwords_fname, ".words");
328 strcpy(binvecs_fname, file_name);
329 strcat(binvecs_fname, ".vecs");
330
331 f = fopen(file_name, "rb");
332 if (f == NULL) {
333 printf("Input file %s not found\n", file_name);
334 exit -1;
335 }
336 fscanf(f, "%lld", &merge_words);
337 fscanf(f, "%lld", &merge_size);
338 if(merge_size != size){
339 fprintf(stderr, "vectors must have the same length\n");
340 exit(-1);
341 }
342 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
343 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
344 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
345 if (merge_vecs == NULL || merge_vocab == NULL) {
346 close(binvecs_fd);
347 close(binwords_fd);
348 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
349 exit(-1);
350 }
351 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
352 read(binwords_fd, merge_vocab, merge_words * max_w);
353 } else {
354 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
355 exit(-1);
356 }
357 printf("Successfully reallocated memory\nMerging...\n");
358 fflush(stdout);
359 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
360 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
361 munmap(M, words * size * sizeof(float));
362 munmap(vocab, words * max_w);
363 M = merge_vecs;
364 vocab = merge_vocab;
365 merged_end = merge_words;
366 words += merge_words;
367 fclose(f);
368 printf("merged_end: %lld, words: %lld\n", merged_end, words);
369 return((long) merged_end);
370}
371
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200372void filter_garbage() {
373 long i;
Marc Kupietzab591a82016-04-28 14:08:49 +0200374 unsigned char *w, previous, c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200375 garbage = malloc(words);
376 memset(garbage, 0, words);
377 for (i = 0; i < words; i++) {
378 w = vocab + i * max_w;
379 previous = 0;
Marc Kupietzab591a82016-04-28 14:08:49 +0200380 while((c = *w++) && !garbage[i]) {
381 if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
382 (previous == '-' && (c & 32)) ||
Marc Kupietz33e3aa12017-11-09 16:19:38 +0100383 (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) ||
384 c == '<'
Marc Kupietzab591a82016-04-28 14:08:49 +0200385 ) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200386 garbage[i]=1;
387 continue;
388 }
389 previous = c;
390 }
391 }
392 return;
393}
394
Marc Kupietz271e2a42016-03-22 11:37:43 +0100395void *getCollocators(knnpars *pars) {
396 int N = pars->N;
397 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100398 knn *nbs = NULL;
399 long window_layer_size = size * window * 2;
400 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
401 float f, max_f, maxmax_f;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100402 float *target_sums, *bestf, *bestn, worstbest, wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100403 long long *besti, *bestp;
Marc Kupietzd5642582016-03-19 22:23:13 +0100404
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200405 if(M2 == NULL || cc == -1)
Marc Kupietzd5642582016-03-19 22:23:13 +0100406 return NULL;
407
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100408 a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100409 besti = malloc(N * sizeof(long long));
410 bestp = malloc(N * sizeof(long long));
411 bestf = malloc(N * sizeof(float));
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100412 bestn = malloc(N * sizeof(float));
413
Marc Kupietz271e2a42016-03-22 11:37:43 +0100414 worstbest = MIN_RESP;
415
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100416 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100417 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100418 for (b = 0; b < N; b++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100419 besti[b] = -1;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100420 bestn[b] = 1;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100421 bestf[b] = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100422 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100423
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100424 d = cc;
425 maxmax_f = -1;
426 maxmax_target = 0;
427
Marc Kupietz271e2a42016-03-22 11:37:43 +0100428 for (a = pars->from; a < pars->upto; a++) {
429 if(a >= window)
430 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100431 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100432 printf("window pos: %ld\n", a);
433 if (a != window) {
434 max_f = -1;
435 window_offset = a * size;
436 if (a > window)
437 window_offset -= size;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100438 for(target = 0; target < pars->cutoff; target ++) {
439 if(garbage && garbage[target]) continue;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100440 if(target == d)
441 continue;
442 f = 0;
443 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100444 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100445 if (f < -MAX_EXP)
446 continue;
447 else if (f > MAX_EXP)
448 continue;
449 else
450 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100451 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100452
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100453 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100454 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100455 for (b = 0; b < N; b++) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100456 if (f > bestf[b]) {
Marc Kupietz33679a32016-03-22 08:49:39 +0100457 memmove(bestf + b + 1, bestf + b, (N - b -1) * sizeof(float));
458 memmove(besti + b + 1, besti + b, (N - b -1) * sizeof(long long));
459 memmove(bestp + b + 1, bestp + b, (N - b -1) * sizeof(long long));
460 bestf[b] = f;
461 besti[b] = target;
462 bestp[b] = window-a;
463 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100464 }
465 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100466 if(b == N - 1)
467 worstbest = bestf[N-1];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100468 }
469 }
470 printf("%d %.2f\n", max_target, max_f);
471 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
472 if(max_f > maxmax_f) {
473 maxmax_f = max_f;
474 maxmax_target = max_target;
475 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100476 for (b = 0; b < N; b++)
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100477 if(bestp[b] == window-a)
478 bestn[b] = bestf[b] / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100479 } else {
480 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
481 }
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100482
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100483 }
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100484 for (b = 0; b < pars->cutoff; b++)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100485 pars->target_sums[b] += (target_sums[b] / wpos_sum ) / (window * 2);
486 free(target_sums);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100487 for(b=0; b<N && besti[b] >= 0; b++) // THIS LOOP IS NEEDED (b...)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100488 printf("%s %.2f %d * ", &vocab[besti[b]*max_w], bestf[b], bestp[b]);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100489 printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100490 nbs = malloc(sizeof(knn));
491 nbs->index = besti;
492 nbs->dist = bestf;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100493 nbs->norm = bestn;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100494 nbs->pos = bestp;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100495 nbs->length = b-1;
496 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100497}
498
Marc Kupietza2e64502016-04-27 09:53:51 +0200499wordlist *getTargetWords(char *st1, int search_backw) {
Marc Kupietz48c29682016-03-19 11:30:43 +0100500 wordlist *wl = malloc(sizeof(wordlist));
501 char st[100][max_size], sep[100];
502 long a, b=0, c=0, cn=0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200503 int unmerged;
504
Marc Kupietzdc22b982015-10-09 09:19:34 +0200505 while (1) {
506 st[cn][b] = st1[c];
507 b++;
508 c++;
509 st[cn][b] = 0;
510 if (st1[c] == 0) break;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100511 if (st1[c] == ' ' || st1[c] == '-') {
512 sep[cn++] = st1[c];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200513 b = 0;
514 c++;
515 }
516 }
517 cn++;
518 for (a = 0; a < cn; a++) {
Marc Kupietza2e64502016-04-27 09:53:51 +0200519 if(search_backw) {
520 for (b = words - 1; b >= 0; b--) if (!strcmp(&vocab[b * max_w], st[a])) break;
521 } else {
522 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
523 }
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100524 if (b == words) b = -1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100525 wl->wordi[a] = b;
526 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], wl->wordi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200527 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100528 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100529 cn--;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200530 break;
531 }
532 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100533 wl->length=cn;
534 return(wl);
535}
536
537void *_get_neighbours(knnpars *pars) {
538 char *st1 = pars->token;
539 int N = pars->N;
540 long from = pars -> from;
541 unsigned long upto = pars -> upto;
542 char file_name[max_size], st[100][max_size], *sep;
543 float dist, len, *bestd, vec[max_size];
544 long long a, b, c, d, cn, *bi, *besti;
545 char ch;
546 knn *nbs = NULL;
547 wordlist *wl = pars->wl;
548
549 besti = malloc(N * sizeof(long long));
550 bestd = malloc(N * sizeof(float));
551
552 float worstbest=-1;
553
554 for (a = 0; a < N; a++) bestd[a] = 0;
555 a = 0;
556 bi = wl->wordi;
557 cn = wl->length;
558 sep = wl->sep;
559 b = bi[0];
560 c = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100561 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100562 N = 0;
563 goto end;
564 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200565 for (a = 0; a < size; a++) vec[a] = 0;
566 for (b = 0; b < cn; b++) {
567 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100568 if(b>0 && sep[b-1] == '-')
569 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
570 else
571 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200572 }
573 len = 0;
574 for (a = 0; a < size; a++) len += vec[a] * vec[a];
575 len = sqrt(len);
576 for (a = 0; a < size; a++) vec[a] /= len;
577 for (a = 0; a < N; a++) bestd[a] = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100578 for (c = from; c < upto; c++) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200579 if(garbage && garbage[c]) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200580 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100581// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100582// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
583// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200584 dist = 0;
585 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100586 if(dist > worstbest) {
587 for (a = 0; a < N; a++) {
588 if (dist > bestd[a]) {
Marc Kupietz33679a32016-03-22 08:49:39 +0100589 memmove(bestd + a + 1, bestd + a, (N - a -1) * sizeof(float));
590 memmove(besti + a + 1, besti + a, (N - a -1) * sizeof(long long));
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100591 bestd[a] = dist;
592 besti[a] = c;
593 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200594 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200595 }
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100596 worstbest = bestd[N-1];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200597 }
598 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100599
Marc Kupietz000ad862016-02-26 14:59:12 +0100600 nbs = malloc(sizeof(knn));
601 nbs->index = besti;
602 nbs->dist = bestd;
603 nbs->length = N;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100604end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100605 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200606}
607
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100608
Marc Kupietzd91212f2017-11-13 10:05:09 +0100609SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100610 HV *result = newHV();
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100611 float *target_sums, bestd[MAX_NEIGHBOURS], bestn[MAX_NEIGHBOURS], bests[MAX_NEIGHBOURS], vec[max_size];
Marc Kupietza5f60042017-05-04 10:38:12 +0200612 long long old_words;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100613 long besti[MAX_NEIGHBOURS], bestp[MAX_NEIGHBOURS], a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100614 knn *para_nbs[MAX_THREADS];
615 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100616 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100617 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100618 wordlist *wl;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200619 int syn_threads = (M2? window * 2 : 0);
620 int para_threads = num_threads - syn_threads;
Marc Kupietz48c29682016-03-19 11:30:43 +0100621
Marc Kupietz000ad862016-02-26 14:59:12 +0100622 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
623
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100624 if(cutoff < 1)
625 cutoff=words;
626
Marc Kupietza2e64502016-04-27 09:53:51 +0200627 wl = getTargetWords(st1, search_backw);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100628 if(wl->length < 1)
629 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100630
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100631 old_words = cutoff;
Marc Kupietza5f60042017-05-04 10:38:12 +0200632 if(merge_words > 0)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100633 cutoff = merge_words * 1.25; /* HACK */
634 slice = cutoff / para_threads;
Marc Kupietza5f60042017-05-04 10:38:12 +0200635
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100636 a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float));
637 for(a = 0; a < cutoff; a++)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100638 target_sums[a] = 0;
639
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200640 printf("Starting %d threads\n", para_threads);
641 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100642 for(a=0; a < para_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100643 pars[a].cutoff = cutoff;
Marc Kupietz000ad862016-02-26 14:59:12 +0100644 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100645 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100646 pars[a].N = N;
647 pars[a].from = a*slice;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100648 pars[a].upto = ((a+1)*slice > cutoff? cutoff:(a+1)*slice);
Marc Kupietz000ad862016-02-26 14:59:12 +0100649 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
650 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200651 if(M2) {
652 for(a=0; a < syn_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100653 pars[a + para_threads].cutoff = cutoff;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200654 pars[a + para_threads].target_sums = target_sums;
655 pars[a + para_threads].wl = wl;
656 pars[a + para_threads].N = N;
657 pars[a + para_threads].from = a;
658 pars[a + para_threads].upto = a+1;
659 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
660 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100661 }
662 printf("Waiting for para threads to join\n");
663 fflush(stdout);
664 for (a = 0; a < para_threads; a++) pthread_join(pt[a], &para_nbs[a]);
665 printf("Para threads joint\n");
666 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +0100667
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200668 /* if(!syn_nbs[0]) */
669 /* goto end; */
Marc Kupietz000ad862016-02-26 14:59:12 +0100670
671 for(b=0; b < N; b++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100672 besti[b] = para_nbs[0]->index[b];
673 bestd[b] = para_nbs[0]->dist[b];
Marc Kupietz000ad862016-02-26 14:59:12 +0100674 }
675
Marc Kupietz271e2a42016-03-22 11:37:43 +0100676 for(a=1; a < para_threads; a++) {
677 for(b=0; b < para_nbs[a]->length && para_nbs[a]->index[b] >= 0; b++) {
Marc Kupietza5f60042017-05-04 10:38:12 +0200678 for(c=0; c < N * para_threads; c++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100679 if(para_nbs[a]->dist[b] > bestd[c]) {
Marc Kupietz000ad862016-02-26 14:59:12 +0100680 for(d=N-1; d>c; d--) {
681 bestd[d] = bestd[d-1];
682 besti[d] = besti[d-1];
683 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100684 besti[c] = para_nbs[a]->index[b];
685 bestd[c] = para_nbs[a]->dist[b];
Marc Kupietz000ad862016-02-26 14:59:12 +0100686 break;
687 }
688 }
689 }
690 }
691
Marc Kupietzd91212f2017-11-13 10:05:09 +0100692 char *chosen[600];
Marc Kupietz271e2a42016-03-22 11:37:43 +0100693 AV* array = newAV();
Marc Kupietzd91212f2017-11-13 10:05:09 +0100694 int i, j;
Marc Kupietza5f60042017-05-04 10:38:12 +0200695 int l1_words=0, l2_words=0;
696 for (a = 0, i = 0; i < N && a < 600; a++) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100697 int filtered=0;
Marc Kupietza5f60042017-05-04 10:38:12 +0200698 long long c = besti[a];
Marc Kupietzd91212f2017-11-13 10:05:09 +0100699 if (dedupe && i > 0) {
700 for (j=0; j<i; j++)
701 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
702 strcasestr(chosen[j], &vocab[c * max_w])) {
703 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
704 filtered = 1;
705 }
706 if(filtered)
707 continue;
708 }
Marc Kupietza5f60042017-05-04 10:38:12 +0200709 if(merge_words > 0) {
710 if(c >= merge_words) {
711 if(l1_words > N / 2)
712 continue;
713 else
714 l1_words++;
715 } else {
716 if(l2_words > N / 2)
717 continue;
718 else
719 l2_words++;
720 }
721 }
722 fflush(stdout);
723 printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
724
Marc Kupietz271e2a42016-03-22 11:37:43 +0100725 HV* hash = newHV();
Marc Kupietza5f60042017-05-04 10:38:12 +0200726 SV* word = newSVpvf(&vocab[c * max_w], 0);
Marc Kupietzd91212f2017-11-13 10:05:09 +0100727 chosen[i] = &vocab[c * max_w];
Marc Kupietz271e2a42016-03-22 11:37:43 +0100728 if(latin_enc == 0) SvUTF8_on(word);
Marc Kupietza5f60042017-05-04 10:38:12 +0200729 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100730 hv_store(hash, "word", strlen("word"), word , 0);
731 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
732 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
733 AV *vector = newAV();
734 for (b = 0; b < size; b++) {
735 av_push(vector, newSVnv(M[b + besti[a] * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100736 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100737 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
738 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietza5f60042017-05-04 10:38:12 +0200739 i++;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100740 }
741 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
742
Marc Kupietz50485ba2016-03-23 09:13:14 +0100743 for(b=0; b < MAX_NEIGHBOURS; b++) {
744 besti[b] = -1L;
745 bestd[b] = 0;
746 bestn[b] = 0;
747 bestp[b] = 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100748 bests[b] = 0;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100749 }
750
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200751 if (M2) {
752 printf("Waiting for syn threads to join\n");
753 fflush(stdout);
754 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], &syn_nbs[a]);
755 printf("syn threads joint\n");
756 fflush(stdout);
Marc Kupietz50485ba2016-03-23 09:13:14 +0100757
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200758 for(b=0; b < syn_nbs[0]->length; b++) {
759 besti[b] = syn_nbs[0]->index[b];
760 bestd[b] = syn_nbs[0]->dist[b];
761 bestn[b] = syn_nbs[0]->norm[b];
762 bestp[b] = syn_nbs[0]->pos[b];
763 bests[b] = target_sums[syn_nbs[0]->index[b]];
764 }
765
766 if(sort_by != 1) { // sort by responsiveness
767 for(a=1; a < syn_threads; a++) {
768 for(b=0; b < syn_nbs[a]->length; b++) {
769 for(c=0; c < MAX_NEIGHBOURS; c++) {
770 if(syn_nbs[a]->dist[b] > bestd[c]) {
771 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
772 bestd[d] = bestd[d-1];
773 besti[d] = besti[d-1];
774 bestn[d] = bestn[d-1];
775 bestp[d] = bestp[d-1];
776 }
777 besti[c] = syn_nbs[a]->index[b];
778 bestd[c] = syn_nbs[a]->dist[b];
779 bestn[c] = syn_nbs[a]->norm[b];
780 bestp[c] = syn_nbs[a]->pos[b];
781 break;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100782 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200783 }
784 }
785 }
786 } else { // sort by mean p
787 for(a=1; a < syn_threads; a++) {
788 for(b=0; b < syn_nbs[a]->length; b++) {
789 for(c=0; c < MAX_NEIGHBOURS; c++) {
790 if(target_sums[syn_nbs[a]->index[b]] > bests[c]) {
791 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
792 bestd[d] = bestd[d-1];
793 besti[d] = besti[d-1];
794 bestn[d] = bestn[d-1];
795 bestp[d] = bestp[d-1];
796 bests[d] = bests[d-1];
797 }
798 besti[c] = syn_nbs[a]->index[b];
799 bestd[c] = syn_nbs[a]->dist[b];
800 bestn[c] = syn_nbs[a]->norm[b];
801 bestp[c] = syn_nbs[a]->pos[b];
802 bests[c] = target_sums[syn_nbs[a]->index[b]];
803 break;
804 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100805 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100806 }
807 }
808 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200809 array = newAV();
Marc Kupietzd91212f2017-11-13 10:05:09 +0100810 for (a = 0, i=0; a < MAX_NEIGHBOURS && besti[a] >= 0; a++) {
811 long long c = besti[a];
812 if (dedupe) {
813 int filtered=0;
814 for (j=0; j<i; j++)
815 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
816 strcasestr(chosen[j], &vocab[c * max_w])) {
817 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
818 filtered = 1;
819 }
820 if(filtered)
821 continue;
822 }
823 chosen[i++]=&vocab[c * max_w];
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200824 HV* hash = newHV();
825 SV* word = newSVpvf(&vocab[besti[a] * max_w], 0);
826 if(latin_enc == 0) SvUTF8_on(word);
827 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzb18978b2017-11-09 14:51:17 +0100828 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200829 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
830 hv_store(hash, "norm", strlen("norm"), newSVnv(bestn[a]), 0);
831 hv_store(hash, "sum", strlen("sum"), newSVnv(target_sums[besti[a]]), 0);
832 hv_store(hash, "pos", strlen("pos"), newSVnv(bestp[a]), 0);
833 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietz271e2a42016-03-22 11:37:43 +0100834 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200835 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100836 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100837end:
Marc Kupietza5f60042017-05-04 10:38:12 +0200838 words = old_words;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100839 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +0100840}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100841
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200842int dump_vecs(char *fname) {
843 long i, j;
844 FILE *f;
845 /* if(words>200000) */
846 /* words=200000; */
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100847
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200848 if((f=fopen(fname, "w")) == NULL) {
849 fprintf(stderr, "cannot open %s for writing\n", fname);
850 return(-1);
851 }
852 fprintf(f, "%lld %lld\n", words, size);
853 for (i=0; i < words; i++) {
854 fprintf(f, "%s ", &vocab[i * max_w]);
855 for(j=0; j < size - 1; j++)
856 fprintf(f, "%f ", M[i*size + j]);
857 fprintf(f, "%f\n", M[i*size + j]);
858 }
859 fclose(f);
860 return(0);
861}
Marc Kupietzdc22b982015-10-09 09:19:34 +0200862