blob: a64982069e252b4e7a92e5c1f872173654aa9859 [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 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;
14app->static->paths->[0] = getcwd;
15
Marc Kupietzd4227392016-03-01 16:45:12 +010016plugin 'Log::Access';
Marc Kupietzb3422c12017-07-04 14:12:11 +020017plugin "RequestBase";
Marc Kupietzdc22b982015-10-09 09:19:34 +020018
Marc Kupietza5b90152016-03-15 17:39:19 +010019our $opt_i = 0; # latin1-input?
20our $opt_l = undef;
21our $opt_p = 5676;
Marc Kupietza2e64502016-04-27 09:53:51 +020022our $opt_m;
Marc Kupietz6ed81872016-04-27 14:04:04 +020023our $opt_M;
Marc Kupietz43ee87e2016-04-25 10:50:08 +020024our $opt_n = '';
25our $opt_d;
Marc Kupietz5c3887d2016-04-28 08:53:35 +020026our $opt_G;
Marc Kupietza5b90152016-03-15 17:39:19 +010027
Marc Kupietz6ed81872016-04-27 14:04:04 +020028my %marked;
Marc Kupietz793413b2016-04-02 21:48:57 +020029my $training_args="";
Marc Kupietza2e64502016-04-27 09:53:51 +020030my $mergedEnd=0;
Marc Kupietz15987412017-11-07 15:56:58 +010031my %cache;
Marc Kupietz793413b2016-04-02 21:48:57 +020032
Marc Kupietza5f60042017-05-04 10:38:12 +020033getopts('d:Gil:p:m:n:M:');
Marc Kupietz6ed81872016-04-27 14:04:04 +020034
35if($opt_M) {
Marc Kupietzed930212016-04-27 15:42:38 +020036 open my $handle, '<:encoding(UTF-8)', $opt_M
37 or die "Can't open '$opt_M' for reading: $!";
38 while(<$handle>) {
Marc Kupietz6ed81872016-04-27 14:04:04 +020039 foreach my $mw (split /\s+/) {
40 $marked{$mw}=1
41 }
42 }
Marc Kupietzed930212016-04-27 15:42:38 +020043 close($handle);
Marc Kupietz6ed81872016-04-27 14:04:04 +020044}
Marc Kupietza5b90152016-03-15 17:39:19 +010045
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010046# -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 +010047if(!$ARGV[0]) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010048 init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0));
Marc Kupietz2cb667e2016-03-10 09:44:12 +010049} else {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010050 init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0));
Marc Kupietz793413b2016-04-02 21:48:57 +020051 if(open(FILE, "$ARGV[0].args")) {
52 $training_args = <FILE>;
53 }
54 close(FILE);
Marc Kupietz2cb667e2016-03-10 09:44:12 +010055}
Marc Kupietzdc22b982015-10-09 09:19:34 +020056
Marc Kupietza2e64502016-04-27 09:53:51 +020057if($opt_m) {
58 $mergedEnd = mergeVectors($opt_m);
59}
60
Marc Kupietz6ed81872016-04-27 14:04:04 +020061
Marc Kupietz43ee87e2016-04-25 10:50:08 +020062if($opt_d) { # -d: dump vecs and exit
63 dump_vecs($opt_d);
64 exit;
65}
66
Marc Kupietza5b90152016-03-15 17:39:19 +010067my $daemon = Mojo::Server::Daemon->new(
68 app => app,
69 listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"]
70);
71
Marc Kupietz5c3887d2016-04-28 08:53:35 +020072if($opt_G) {
73 print "Filtering garbage\n";
74 filter_garbage();
75}
76
Marc Kupietz554aff52017-11-09 14:42:09 +010077get '*/js/*' => sub {
Marc Kupietzffef9302017-11-07 15:58:01 +010078 my $c = shift;
79 my $url = $c->req->url;
80 $url =~ s@/derekovecs@@g;
81 $c->app->log->info("GET: " . $url);
82 $c->reply->static($url);
83};
84
Marc Kupietzdc22b982015-10-09 09:19:34 +020085get '/' => sub {
86 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +020087 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +020088 my $word=$c->param('word');
Marc Kupietz44bee3c2016-02-25 16:26:29 +010089 my $no_nbs=$c->param('n') || 100;
90 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +010091 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +010092 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +010093 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +020094 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +010095 my $sort=$c->param('sort') || 0;
Marc Kupietzc469f3b2017-11-13 14:07:36 +010096 my $csv=$c->param('csv') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +020097 my $json=$c->param('json') || 0;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +010098 my $cutoff=$c->param('cutoff') || 1000000;
Marc Kupietzd91212f2017-11-13 10:05:09 +010099 my $dedupe=$c->param('dedupe') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100100 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100101 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100102 my @collocations;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100103 if(defined($word) && $word !~ /^\s*$/) {
104 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100105 $word =~ s/\s+/ /g;
106 for my $w (split(' *\| *', $word)) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100107 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100108 $c->app->log->info("Getting $w results from cache");
Marc Kupietzd91212f2017-11-13 10:05:09 +0100109 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe}
Marc Kupietza5b90152016-03-15 17:39:19 +0100110 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100111 $c->app->log->info('Looking for neighbours of '.$w);
112 if($opt_i) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100113 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe);
Marc Kupietz15987412017-11-07 15:56:58 +0100114 } else {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100115 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe);
Marc Kupietz15987412017-11-07 15:56:58 +0100116 }
117 $cache{$w} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100118 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100119 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100120 }
121 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100122 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200123 if($json) {
124 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100125 } elsif($csv) {
126 my $csv_data="";
127 for (my $i=0; $i <= $no_nbs; $i++) {
128 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
129 }
130 for (my $i=0; $i < $no_nbs; $i++) {
131 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
132 }
133 chop $csv_data;
134 chop $csv_data;
135 $csv_data .= "\n";
136 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200137 } else {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100138 $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 +0200139 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200140};
141
Marc Kupietz30ca4342017-11-22 21:21:20 +0100142helper(bitvec2window => sub {
143 my ($self, $n) = @_;
144 my $str = unpack("B32", pack("N", $n));
145 $str =~ s/^\d{22}//;
146 $str =~ s/^(\d{5})/$1x/;
147 $str =~ s/0/·/g;
148 $str =~ s/1/+/g;
149 return $str;
150 });
151
Marc Kupietza5b90152016-03-15 17:39:19 +0100152$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200153
154exit;
155
156__END__
157
158__C__
159#include <stdio.h>
160#include <string.h>
161#include <math.h>
162#include <malloc.h>
163#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +0100164#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +0100165#include <pthread.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200166
167#define max_size 2000
168#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100169#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100170#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +0100171#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100172#define MAX_CC 50
173#define EXP_TABLE_SIZE 1000
174#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +0100175#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +0200176
177//the thread function
178void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +0100179
180typedef struct {
181 long long *index;
182 float *dist;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100183 float *norm;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100184 long long *pos;
Marc Kupietz80abb442016-03-23 21:04:08 +0100185 int length;
Marc Kupietz000ad862016-02-26 14:59:12 +0100186} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100187
Marc Kupietz000ad862016-02-26 14:59:12 +0100188typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +0100189 long long wordi[MAX_NEIGHBOURS];
190 char sep[MAX_NEIGHBOURS];
191 int length;
192} wordlist;
193
194typedef struct {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100195 long cutoff;
Marc Kupietz48c29682016-03-19 11:30:43 +0100196 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100197 char *token;
198 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100199 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100200 unsigned long upto;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100201 float *target_sums;
Marc Kupietz000ad862016-02-26 14:59:12 +0100202} knnpars;
203
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200204float *M, *M2=0L, *syn1neg_window, *expTable;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200205char *vocab;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200206char *garbage = NULL;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100207
Marc Kupietza2e64502016-04-27 09:53:51 +0200208long long words, size, merged_end;
Marc Kupietza5f60042017-05-04 10:38:12 +0200209long long merge_words = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100210int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100211int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100212int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200213
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100214int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100215 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100216 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100217 long long a, b, c, d, cn;
218 float len;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200219 double val;
Marc Kupietz82b02672016-02-26 12:32:25 +0100220
Marc Kupietz67c20282016-02-26 09:42:00 +0100221 char binvecs_fname[256], binwords_fname[256];
222 strcpy(binwords_fname, file_name);
223 strcat(binwords_fname, ".words");
224 strcpy(binvecs_fname, file_name);
225 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200226
Marc Kupietza5b90152016-03-15 17:39:19 +0100227 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200228 f = fopen(file_name, "rb");
229 if (f == NULL) {
230 printf("Input file %s not found\n", file_name);
231 return -1;
232 }
233 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100234 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200235 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100236 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
237 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100238 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
239 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
240 if (M == NULL) {
241 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
242 return -1;
243 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200244 if(strstr(file_name, ".txt")) {
245 for (b = 0; b < words; b++) {
246 a = 0;
247 while (1) {
248 vocab[b * max_w + a] = fgetc(f);
249 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
250 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
251 }
252 vocab[b * max_w + a] = 0;
253 len = 0;
254 for (a = 0; a < size; a++) {
255 fscanf(f, "%lf", &val);
256 M[a + b * size] = val;
257 len += val * val;
258 }
259 len = sqrt(len);
260 for (a = 0; a < size; a++) M[a + b * size] /= len;
261 }
262 } else {
263 for (b = 0; b < words; b++) {
264 a = 0;
265 while (1) {
266 vocab[b * max_w + a] = fgetc(f);
267 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
268 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
269 }
270 vocab[b * max_w + a] = 0;
271 fread(&M[b * size], sizeof(float), size, f);
272 len = 0;
273 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
274 len = sqrt(len);
275 for (a = 0; a < size; a++) M[a + b * size] /= len;
276 }
277 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100278 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
279 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
280 fclose(binvecs);
281 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
282 fclose(binwords);
283 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100284 }
285 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
286 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
287 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
288 if (M == MAP_FAILED || vocab == MAP_FAILED) {
289 close(binvecs_fd);
290 close(binwords_fd);
291 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
292 exit(-1);
293 }
294 } else {
295 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
296 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100297 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200298 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100299
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200300 if(net_name && strlen(net_name) > 0) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100301 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
302 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
303 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100304 // munmap(M, sizeof(float) * words * size);
305 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 +0200306 if (M2 == MAP_FAILED) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100307 close(net_fd);
308 fprintf(stderr, "Cannot mmap %s\n", net_name);
309 exit(-1);
310 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100311 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100312 } else {
313 fprintf(stderr, "Cannot open %s\n", net_name);
314 exit(-1);
315 }
316 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
317 }
318
319 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
320 for (i = 0; i < EXP_TABLE_SIZE; i++) {
321 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
322 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
323 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200324 return 0;
325}
326
Marc Kupietza2e64502016-04-27 09:53:51 +0200327long mergeVectors(char *file_name){
328 FILE *f, *binvecs, *binwords;
329 int binwords_fd, binvecs_fd, net_fd, i;
330 long long a, b, c, d, cn;
331 float len;
332 float *merge_vecs;
333 char *merge_vocab;
Marc Kupietza5f60042017-05-04 10:38:12 +0200334 /* long long merge_words, merge_size; */
335 long long merge_size;
Marc Kupietza2e64502016-04-27 09:53:51 +0200336
337 char binvecs_fname[256], binwords_fname[256];
338 strcpy(binwords_fname, file_name);
339 strcat(binwords_fname, ".words");
340 strcpy(binvecs_fname, file_name);
341 strcat(binvecs_fname, ".vecs");
342
343 f = fopen(file_name, "rb");
344 if (f == NULL) {
345 printf("Input file %s not found\n", file_name);
346 exit -1;
347 }
348 fscanf(f, "%lld", &merge_words);
349 fscanf(f, "%lld", &merge_size);
350 if(merge_size != size){
351 fprintf(stderr, "vectors must have the same length\n");
352 exit(-1);
353 }
354 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
355 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
356 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
357 if (merge_vecs == NULL || merge_vocab == NULL) {
358 close(binvecs_fd);
359 close(binwords_fd);
360 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
361 exit(-1);
362 }
363 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
364 read(binwords_fd, merge_vocab, merge_words * max_w);
365 } else {
366 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
367 exit(-1);
368 }
369 printf("Successfully reallocated memory\nMerging...\n");
370 fflush(stdout);
371 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
372 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
373 munmap(M, words * size * sizeof(float));
374 munmap(vocab, words * max_w);
375 M = merge_vecs;
376 vocab = merge_vocab;
377 merged_end = merge_words;
378 words += merge_words;
379 fclose(f);
380 printf("merged_end: %lld, words: %lld\n", merged_end, words);
381 return((long) merged_end);
382}
383
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200384void filter_garbage() {
385 long i;
Marc Kupietzab591a82016-04-28 14:08:49 +0200386 unsigned char *w, previous, c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200387 garbage = malloc(words);
388 memset(garbage, 0, words);
389 for (i = 0; i < words; i++) {
390 w = vocab + i * max_w;
391 previous = 0;
Marc Kupietzab591a82016-04-28 14:08:49 +0200392 while((c = *w++) && !garbage[i]) {
393 if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
394 (previous == '-' && (c & 32)) ||
Marc Kupietz33e3aa12017-11-09 16:19:38 +0100395 (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) ||
396 c == '<'
Marc Kupietzab591a82016-04-28 14:08:49 +0200397 ) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200398 garbage[i]=1;
399 continue;
400 }
401 previous = c;
402 }
403 }
404 return;
405}
406
Marc Kupietz271e2a42016-03-22 11:37:43 +0100407void *getCollocators(knnpars *pars) {
408 int N = pars->N;
409 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100410 knn *nbs = NULL;
411 long window_layer_size = size * window * 2;
412 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
413 float f, max_f, maxmax_f;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100414 float *target_sums, *bestf, *bestn, worstbest, wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100415 long long *besti, *bestp;
Marc Kupietzd5642582016-03-19 22:23:13 +0100416
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200417 if(M2 == NULL || cc == -1)
Marc Kupietzd5642582016-03-19 22:23:13 +0100418 return NULL;
419
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100420 a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100421 besti = malloc(N * sizeof(long long));
422 bestp = malloc(N * sizeof(long long));
423 bestf = malloc(N * sizeof(float));
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100424 bestn = malloc(N * sizeof(float));
425
Marc Kupietz271e2a42016-03-22 11:37:43 +0100426 worstbest = MIN_RESP;
427
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100428 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100429 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100430 for (b = 0; b < N; b++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100431 besti[b] = -1;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100432 bestn[b] = 1;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100433 bestf[b] = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100434 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100435
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100436 d = cc;
437 maxmax_f = -1;
438 maxmax_target = 0;
439
Marc Kupietz271e2a42016-03-22 11:37:43 +0100440 for (a = pars->from; a < pars->upto; a++) {
441 if(a >= window)
442 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100443 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100444 printf("window pos: %ld\n", a);
445 if (a != window) {
446 max_f = -1;
447 window_offset = a * size;
448 if (a > window)
449 window_offset -= size;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100450 for(target = 0; target < pars->cutoff; target ++) {
451 if(garbage && garbage[target]) continue;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100452 if(target == d)
453 continue;
454 f = 0;
455 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100456 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100457 if (f < -MAX_EXP)
458 continue;
459 else if (f > MAX_EXP)
460 continue;
461 else
462 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100463 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100464
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100465 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100466 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100467 for (b = 0; b < N; b++) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100468 if (f > bestf[b]) {
Marc Kupietz33679a32016-03-22 08:49:39 +0100469 memmove(bestf + b + 1, bestf + b, (N - b -1) * sizeof(float));
470 memmove(besti + b + 1, besti + b, (N - b -1) * sizeof(long long));
471 memmove(bestp + b + 1, bestp + b, (N - b -1) * sizeof(long long));
472 bestf[b] = f;
473 besti[b] = target;
474 bestp[b] = window-a;
475 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100476 }
477 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100478 if(b == N - 1)
479 worstbest = bestf[N-1];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100480 }
481 }
482 printf("%d %.2f\n", max_target, max_f);
483 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
484 if(max_f > maxmax_f) {
485 maxmax_f = max_f;
486 maxmax_target = max_target;
487 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100488 for (b = 0; b < N; b++)
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100489 if(bestp[b] == window-a)
490 bestn[b] = bestf[b] / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100491 } else {
492 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
493 }
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100494
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100495 }
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100496 for (b = 0; b < pars->cutoff; b++)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100497 pars->target_sums[b] += (target_sums[b] / wpos_sum ) / (window * 2);
498 free(target_sums);
Marc Kupietz30ca4342017-11-22 21:21:20 +0100499 for(b=0; b<N && besti[b] >= 0; b++); // THIS LOOP IS NEEDED (b...)
500// printf("%s %.2f %d * ", &vocab[besti[b]*max_w], bestf[b], bestp[b]);
501// printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100502 nbs = malloc(sizeof(knn));
503 nbs->index = besti;
504 nbs->dist = bestf;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100505 nbs->norm = bestn;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100506 nbs->pos = bestp;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100507 nbs->length = b-1;
508 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100509}
510
Marc Kupietz30ca4342017-11-22 21:21:20 +0100511
Marc Kupietza2e64502016-04-27 09:53:51 +0200512wordlist *getTargetWords(char *st1, int search_backw) {
Marc Kupietz48c29682016-03-19 11:30:43 +0100513 wordlist *wl = malloc(sizeof(wordlist));
514 char st[100][max_size], sep[100];
515 long a, b=0, c=0, cn=0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200516 int unmerged;
517
Marc Kupietzdc22b982015-10-09 09:19:34 +0200518 while (1) {
519 st[cn][b] = st1[c];
520 b++;
521 c++;
522 st[cn][b] = 0;
523 if (st1[c] == 0) break;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100524 if (st1[c] == ' ' || st1[c] == '-') {
525 sep[cn++] = st1[c];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200526 b = 0;
527 c++;
528 }
529 }
530 cn++;
531 for (a = 0; a < cn; a++) {
Marc Kupietza2e64502016-04-27 09:53:51 +0200532 if(search_backw) {
533 for (b = words - 1; b >= 0; b--) if (!strcmp(&vocab[b * max_w], st[a])) break;
534 } else {
535 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
536 }
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100537 if (b == words) b = -1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100538 wl->wordi[a] = b;
539 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], wl->wordi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200540 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100541 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100542 cn--;
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100543 free(wl);
544 return NULL;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200545 }
546 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100547 wl->length=cn;
548 return(wl);
549}
550
551void *_get_neighbours(knnpars *pars) {
552 char *st1 = pars->token;
553 int N = pars->N;
554 long from = pars -> from;
555 unsigned long upto = pars -> upto;
556 char file_name[max_size], st[100][max_size], *sep;
557 float dist, len, *bestd, vec[max_size];
558 long long a, b, c, d, cn, *bi, *besti;
559 char ch;
560 knn *nbs = NULL;
561 wordlist *wl = pars->wl;
562
563 besti = malloc(N * sizeof(long long));
564 bestd = malloc(N * sizeof(float));
565
566 float worstbest=-1;
567
568 for (a = 0; a < N; a++) bestd[a] = 0;
569 a = 0;
570 bi = wl->wordi;
571 cn = wl->length;
572 sep = wl->sep;
573 b = bi[0];
574 c = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100575 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100576 N = 0;
577 goto end;
578 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200579 for (a = 0; a < size; a++) vec[a] = 0;
580 for (b = 0; b < cn; b++) {
581 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100582 if(b>0 && sep[b-1] == '-')
583 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
584 else
585 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200586 }
587 len = 0;
588 for (a = 0; a < size; a++) len += vec[a] * vec[a];
589 len = sqrt(len);
590 for (a = 0; a < size; a++) vec[a] /= len;
591 for (a = 0; a < N; a++) bestd[a] = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100592 for (c = from; c < upto; c++) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200593 if(garbage && garbage[c]) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200594 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100595// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100596// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
597// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200598 dist = 0;
599 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100600 if(dist > worstbest) {
601 for (a = 0; a < N; a++) {
602 if (dist > bestd[a]) {
Marc Kupietz33679a32016-03-22 08:49:39 +0100603 memmove(bestd + a + 1, bestd + a, (N - a -1) * sizeof(float));
604 memmove(besti + a + 1, besti + a, (N - a -1) * sizeof(long long));
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100605 bestd[a] = dist;
606 besti[a] = c;
607 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200608 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200609 }
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100610 worstbest = bestd[N-1];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200611 }
612 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100613
Marc Kupietz000ad862016-02-26 14:59:12 +0100614 nbs = malloc(sizeof(knn));
615 nbs->index = besti;
616 nbs->dist = bestd;
617 nbs->length = N;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100618end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100619 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200620}
621
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100622
Marc Kupietzd91212f2017-11-13 10:05:09 +0100623SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100624 HV *result = newHV();
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100625 float *target_sums, bestd[MAX_NEIGHBOURS], bestn[MAX_NEIGHBOURS], bests[MAX_NEIGHBOURS], vec[max_size];
Marc Kupietza5f60042017-05-04 10:38:12 +0200626 long long old_words;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100627 long besti[MAX_NEIGHBOURS], bestp[MAX_NEIGHBOURS], a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100628 knn *para_nbs[MAX_THREADS];
629 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100630 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100631 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100632 wordlist *wl;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200633 int syn_threads = (M2? window * 2 : 0);
634 int para_threads = num_threads - syn_threads;
Marc Kupietz48c29682016-03-19 11:30:43 +0100635
Marc Kupietz000ad862016-02-26 14:59:12 +0100636 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
637
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100638 if(cutoff < 1)
639 cutoff=words;
640
Marc Kupietza2e64502016-04-27 09:53:51 +0200641 wl = getTargetWords(st1, search_backw);
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100642 if(wl == NULL || wl->length < 1)
Marc Kupietz271e2a42016-03-22 11:37:43 +0100643 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100644
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100645 old_words = cutoff;
Marc Kupietza5f60042017-05-04 10:38:12 +0200646 if(merge_words > 0)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100647 cutoff = merge_words * 1.25; /* HACK */
648 slice = cutoff / para_threads;
Marc Kupietza5f60042017-05-04 10:38:12 +0200649
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100650 a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float));
651 for(a = 0; a < cutoff; a++)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100652 target_sums[a] = 0;
653
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200654 printf("Starting %d threads\n", para_threads);
655 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100656 for(a=0; a < para_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100657 pars[a].cutoff = cutoff;
Marc Kupietz000ad862016-02-26 14:59:12 +0100658 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100659 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100660 pars[a].N = N;
661 pars[a].from = a*slice;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100662 pars[a].upto = ((a+1)*slice > cutoff? cutoff:(a+1)*slice);
Marc Kupietz000ad862016-02-26 14:59:12 +0100663 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
664 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200665 if(M2) {
666 for(a=0; a < syn_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100667 pars[a + para_threads].cutoff = cutoff;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200668 pars[a + para_threads].target_sums = target_sums;
669 pars[a + para_threads].wl = wl;
670 pars[a + para_threads].N = N;
671 pars[a + para_threads].from = a;
672 pars[a + para_threads].upto = a+1;
673 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
674 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100675 }
676 printf("Waiting for para threads to join\n");
677 fflush(stdout);
678 for (a = 0; a < para_threads; a++) pthread_join(pt[a], &para_nbs[a]);
679 printf("Para threads joint\n");
680 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +0100681
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200682 /* if(!syn_nbs[0]) */
683 /* goto end; */
Marc Kupietz000ad862016-02-26 14:59:12 +0100684
685 for(b=0; b < N; b++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100686 besti[b] = para_nbs[0]->index[b];
687 bestd[b] = para_nbs[0]->dist[b];
Marc Kupietz000ad862016-02-26 14:59:12 +0100688 }
689
Marc Kupietz271e2a42016-03-22 11:37:43 +0100690 for(a=1; a < para_threads; a++) {
691 for(b=0; b < para_nbs[a]->length && para_nbs[a]->index[b] >= 0; b++) {
Marc Kupietza5f60042017-05-04 10:38:12 +0200692 for(c=0; c < N * para_threads; c++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100693 if(para_nbs[a]->dist[b] > bestd[c]) {
Marc Kupietz000ad862016-02-26 14:59:12 +0100694 for(d=N-1; d>c; d--) {
695 bestd[d] = bestd[d-1];
696 besti[d] = besti[d-1];
697 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100698 besti[c] = para_nbs[a]->index[b];
699 bestd[c] = para_nbs[a]->dist[b];
Marc Kupietz000ad862016-02-26 14:59:12 +0100700 break;
701 }
702 }
703 }
704 }
705
Marc Kupietzd91212f2017-11-13 10:05:09 +0100706 char *chosen[600];
Marc Kupietz271e2a42016-03-22 11:37:43 +0100707 AV* array = newAV();
Marc Kupietzd91212f2017-11-13 10:05:09 +0100708 int i, j;
Marc Kupietza5f60042017-05-04 10:38:12 +0200709 int l1_words=0, l2_words=0;
710 for (a = 0, i = 0; i < N && a < 600; a++) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100711 int filtered=0;
Marc Kupietza5f60042017-05-04 10:38:12 +0200712 long long c = besti[a];
Marc Kupietzd91212f2017-11-13 10:05:09 +0100713 if (dedupe && i > 0) {
714 for (j=0; j<i; j++)
715 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
716 strcasestr(chosen[j], &vocab[c * max_w])) {
717 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
718 filtered = 1;
719 }
720 if(filtered)
721 continue;
722 }
Marc Kupietza5f60042017-05-04 10:38:12 +0200723 if(merge_words > 0) {
724 if(c >= merge_words) {
725 if(l1_words > N / 2)
726 continue;
727 else
728 l1_words++;
729 } else {
730 if(l2_words > N / 2)
731 continue;
732 else
733 l2_words++;
734 }
735 }
736 fflush(stdout);
737 printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
738
Marc Kupietz271e2a42016-03-22 11:37:43 +0100739 HV* hash = newHV();
Marc Kupietza5f60042017-05-04 10:38:12 +0200740 SV* word = newSVpvf(&vocab[c * max_w], 0);
Marc Kupietzd91212f2017-11-13 10:05:09 +0100741 chosen[i] = &vocab[c * max_w];
Marc Kupietz271e2a42016-03-22 11:37:43 +0100742 if(latin_enc == 0) SvUTF8_on(word);
Marc Kupietza5f60042017-05-04 10:38:12 +0200743 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100744 hv_store(hash, "word", strlen("word"), word , 0);
745 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
746 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
747 AV *vector = newAV();
748 for (b = 0; b < size; b++) {
749 av_push(vector, newSVnv(M[b + besti[a] * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100750 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100751 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
752 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietza5f60042017-05-04 10:38:12 +0200753 i++;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100754 }
755 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
756
Marc Kupietz50485ba2016-03-23 09:13:14 +0100757 for(b=0; b < MAX_NEIGHBOURS; b++) {
758 besti[b] = -1L;
759 bestd[b] = 0;
760 bestn[b] = 0;
761 bestp[b] = 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100762 bests[b] = 0;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100763 }
764
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200765 if (M2) {
766 printf("Waiting for syn threads to join\n");
767 fflush(stdout);
768 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], &syn_nbs[a]);
769 printf("syn threads joint\n");
770 fflush(stdout);
Marc Kupietz50485ba2016-03-23 09:13:14 +0100771
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200772 for(b=0; b < syn_nbs[0]->length; b++) {
773 besti[b] = syn_nbs[0]->index[b];
774 bestd[b] = syn_nbs[0]->dist[b];
775 bestn[b] = syn_nbs[0]->norm[b];
Marc Kupietz30ca4342017-11-22 21:21:20 +0100776 bestp[b] = -1; // syn_nbs[0]->pos[b];
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200777 bests[b] = target_sums[syn_nbs[0]->index[b]];
778 }
Marc Kupietz30ca4342017-11-22 21:21:20 +0100779
780 float best_window_sum[MAX_NEIGHBOURS];
781 int found_index=0, i=0, j, w;
782 if(sort_by != 1 && sort_by != 2) { // sort by auto focus mean
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200783 for(a=1; a < syn_threads; a++) {
784 for(b=0; b < syn_nbs[a]->length; b++) {
Marc Kupietz30ca4342017-11-22 21:21:20 +0100785 for(i=0; i < found_index; i++)
786 if(besti[i] == syn_nbs[a]->index[b])
787 break;
788 if(i >= found_index) {
789 besti[found_index++] = syn_nbs[a]->index[b];
790// printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
791 }
792 }
793 }
794 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) -1);
795 for(i=0; i < found_index; i++) {
796 for(w=1; w < (1 << syn_threads); w++) { // loop through all possible windows
797 float word_window_sum = 0;
798 int bits_set = 0;
799 for(a=1; a < syn_threads; a++) {
800 if((1 << a) & w) {
801 bits_set++;
802 for(b=0; b < syn_nbs[a]->length; b++)
803 if(besti[i] == syn_nbs[a]->index[b])
804 word_window_sum += syn_nbs[a]->dist[b];
805 }
806 }
807 if(bits_set)
808 word_window_sum /= bits_set;
809 if(word_window_sum > bestd[i]) {
810 bestd[i] = word_window_sum;
811 bestp[i] = w;
812 }
813 }
814 }
815 for(i=0; i<found_index;i++) {
816 for(j=0;j<found_index-1;j++) {
817 if(bestd[j]<bestd[j+1]) {
818 float tempd=bestd[j];
819 bestd[j]=bestd[j+1];
820 bestd[j+1]=tempd;
821 int tempi=besti[j];
822 besti[j]=besti[j+1];
823 besti[j+1]=tempi;
824 int tempp=bestp[j];
825 bestp[j]=bestp[j+1];
826 bestp[j+1]=tempp;
827 }
828 }
829 }
830// for(i=0; i < found_index; i++) {
831// printf("found: %s - sum: %f - window: %d\n", &vocab[besti[i] * max_w], bestd[i], bestp[i]);
832// }
833
834 } else if(sort_by ==1) { // single window position
835 for(a=1; a < syn_threads; a++) {
836 for(b=0; b < syn_nbs[a]->length; b++) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200837 for(c=0; c < MAX_NEIGHBOURS; c++) {
838 if(syn_nbs[a]->dist[b] > bestd[c]) {
839 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
840 bestd[d] = bestd[d-1];
841 besti[d] = besti[d-1];
842 bestn[d] = bestn[d-1];
843 bestp[d] = bestp[d-1];
844 }
845 besti[c] = syn_nbs[a]->index[b];
846 bestd[c] = syn_nbs[a]->dist[b];
847 bestn[c] = syn_nbs[a]->norm[b];
Marc Kupietz30ca4342017-11-22 21:21:20 +0100848 bestp[c] = 1 << (-syn_nbs[a]->pos[b]+window - (syn_nbs[a]->pos[b] < 0 ? 1:0));
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200849 break;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100850 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200851 }
852 }
853 }
854 } else { // sort by mean p
855 for(a=1; a < syn_threads; a++) {
856 for(b=0; b < syn_nbs[a]->length; b++) {
857 for(c=0; c < MAX_NEIGHBOURS; c++) {
858 if(target_sums[syn_nbs[a]->index[b]] > bests[c]) {
859 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
860 bestd[d] = bestd[d-1];
861 besti[d] = besti[d-1];
862 bestn[d] = bestn[d-1];
863 bestp[d] = bestp[d-1];
864 bests[d] = bests[d-1];
865 }
866 besti[c] = syn_nbs[a]->index[b];
867 bestd[c] = syn_nbs[a]->dist[b];
868 bestn[c] = syn_nbs[a]->norm[b];
Marc Kupietz30ca4342017-11-22 21:21:20 +0100869 bestp[c] = (1 << 2*window) - 1; // syn_nbs[a]->pos[b];
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200870 bests[c] = target_sums[syn_nbs[a]->index[b]];
871 break;
872 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100873 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100874 }
875 }
876 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200877 array = newAV();
Marc Kupietzd91212f2017-11-13 10:05:09 +0100878 for (a = 0, i=0; a < MAX_NEIGHBOURS && besti[a] >= 0; a++) {
879 long long c = besti[a];
880 if (dedupe) {
881 int filtered=0;
882 for (j=0; j<i; j++)
883 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
884 strcasestr(chosen[j], &vocab[c * max_w])) {
885 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
886 filtered = 1;
887 }
888 if(filtered)
889 continue;
890 }
891 chosen[i++]=&vocab[c * max_w];
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200892 HV* hash = newHV();
893 SV* word = newSVpvf(&vocab[besti[a] * max_w], 0);
894 if(latin_enc == 0) SvUTF8_on(word);
895 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzb18978b2017-11-09 14:51:17 +0100896 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200897 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
898 hv_store(hash, "norm", strlen("norm"), newSVnv(bestn[a]), 0);
899 hv_store(hash, "sum", strlen("sum"), newSVnv(target_sums[besti[a]]), 0);
900 hv_store(hash, "pos", strlen("pos"), newSVnv(bestp[a]), 0);
901 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietz271e2a42016-03-22 11:37:43 +0100902 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200903 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100904 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100905end:
Marc Kupietza5f60042017-05-04 10:38:12 +0200906 words = old_words;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100907 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +0100908}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100909
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200910int dump_vecs(char *fname) {
911 long i, j;
912 FILE *f;
913 /* if(words>200000) */
914 /* words=200000; */
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100915
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200916 if((f=fopen(fname, "w")) == NULL) {
917 fprintf(stderr, "cannot open %s for writing\n", fname);
918 return(-1);
919 }
920 fprintf(f, "%lld %lld\n", words, size);
921 for (i=0; i < words; i++) {
922 fprintf(f, "%s ", &vocab[i * max_w]);
923 for(j=0; j < size - 1; j++)
924 fprintf(f, "%f ", M[i*size + j]);
925 fprintf(f, "%f\n", M[i*size + j]);
926 }
927 fclose(f);
928 return(0);
929}
Marc Kupietzdc22b982015-10-09 09:19:34 +0200930