blob: d7ec9009ccd5b8ca58bd1ce8cc201ec62931e4c5 [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 Kupietzbd41da32017-11-24 10:26:43 +0100202 float *window_sums;
Marc Kupietz000ad862016-02-26 14:59:12 +0100203} knnpars;
204
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200205float *M, *M2=0L, *syn1neg_window, *expTable;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100206float *window_sums;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200207char *vocab;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200208char *garbage = NULL;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100209
Marc Kupietza2e64502016-04-27 09:53:51 +0200210long long words, size, merged_end;
Marc Kupietza5f60042017-05-04 10:38:12 +0200211long long merge_words = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100212int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100213int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100214int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200215
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100216int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100217 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100218 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100219 long long a, b, c, d, cn;
220 float len;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200221 double val;
Marc Kupietz82b02672016-02-26 12:32:25 +0100222
Marc Kupietz67c20282016-02-26 09:42:00 +0100223 char binvecs_fname[256], binwords_fname[256];
224 strcpy(binwords_fname, file_name);
225 strcat(binwords_fname, ".words");
226 strcpy(binvecs_fname, file_name);
227 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200228
Marc Kupietza5b90152016-03-15 17:39:19 +0100229 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200230 f = fopen(file_name, "rb");
231 if (f == NULL) {
232 printf("Input file %s not found\n", file_name);
233 return -1;
234 }
235 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100236 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200237 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100238 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
239 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100240 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
241 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
242 if (M == NULL) {
243 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
244 return -1;
245 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200246 if(strstr(file_name, ".txt")) {
247 for (b = 0; b < words; b++) {
248 a = 0;
249 while (1) {
250 vocab[b * max_w + a] = fgetc(f);
251 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
252 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
253 }
254 vocab[b * max_w + a] = 0;
255 len = 0;
256 for (a = 0; a < size; a++) {
257 fscanf(f, "%lf", &val);
258 M[a + b * size] = val;
259 len += val * val;
260 }
261 len = sqrt(len);
262 for (a = 0; a < size; a++) M[a + b * size] /= len;
263 }
264 } else {
265 for (b = 0; b < words; b++) {
266 a = 0;
267 while (1) {
268 vocab[b * max_w + a] = fgetc(f);
269 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
270 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
271 }
272 vocab[b * max_w + a] = 0;
273 fread(&M[b * size], sizeof(float), size, f);
274 len = 0;
275 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
276 len = sqrt(len);
277 for (a = 0; a < size; a++) M[a + b * size] /= len;
278 }
279 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100280 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
281 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
282 fclose(binvecs);
283 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
284 fclose(binwords);
285 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100286 }
287 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
288 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
289 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
290 if (M == MAP_FAILED || vocab == MAP_FAILED) {
291 close(binvecs_fd);
292 close(binwords_fd);
293 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
294 exit(-1);
295 }
296 } else {
297 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
298 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100299 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200300 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100301
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200302 if(net_name && strlen(net_name) > 0) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100303 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
304 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
305 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100306 // munmap(M, sizeof(float) * words * size);
307 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 +0200308 if (M2 == MAP_FAILED) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100309 close(net_fd);
310 fprintf(stderr, "Cannot mmap %s\n", net_name);
311 exit(-1);
312 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100313 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100314 } else {
315 fprintf(stderr, "Cannot open %s\n", net_name);
316 exit(-1);
317 }
318 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
319 }
320
321 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
322 for (i = 0; i < EXP_TABLE_SIZE; i++) {
323 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
324 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
325 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100326 window_sums = malloc(sizeof(float) * (window+1) * 2);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200327 return 0;
328}
329
Marc Kupietza2e64502016-04-27 09:53:51 +0200330long mergeVectors(char *file_name){
331 FILE *f, *binvecs, *binwords;
332 int binwords_fd, binvecs_fd, net_fd, i;
333 long long a, b, c, d, cn;
334 float len;
335 float *merge_vecs;
336 char *merge_vocab;
Marc Kupietza5f60042017-05-04 10:38:12 +0200337 /* long long merge_words, merge_size; */
338 long long merge_size;
Marc Kupietza2e64502016-04-27 09:53:51 +0200339
340 char binvecs_fname[256], binwords_fname[256];
341 strcpy(binwords_fname, file_name);
342 strcat(binwords_fname, ".words");
343 strcpy(binvecs_fname, file_name);
344 strcat(binvecs_fname, ".vecs");
345
346 f = fopen(file_name, "rb");
347 if (f == NULL) {
348 printf("Input file %s not found\n", file_name);
349 exit -1;
350 }
351 fscanf(f, "%lld", &merge_words);
352 fscanf(f, "%lld", &merge_size);
353 if(merge_size != size){
354 fprintf(stderr, "vectors must have the same length\n");
355 exit(-1);
356 }
357 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
358 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
359 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
360 if (merge_vecs == NULL || merge_vocab == NULL) {
361 close(binvecs_fd);
362 close(binwords_fd);
363 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
364 exit(-1);
365 }
366 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
367 read(binwords_fd, merge_vocab, merge_words * max_w);
368 } else {
369 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
370 exit(-1);
371 }
372 printf("Successfully reallocated memory\nMerging...\n");
373 fflush(stdout);
374 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
375 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
376 munmap(M, words * size * sizeof(float));
377 munmap(vocab, words * max_w);
378 M = merge_vecs;
379 vocab = merge_vocab;
380 merged_end = merge_words;
381 words += merge_words;
382 fclose(f);
383 printf("merged_end: %lld, words: %lld\n", merged_end, words);
384 return((long) merged_end);
385}
386
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200387void filter_garbage() {
388 long i;
Marc Kupietzab591a82016-04-28 14:08:49 +0200389 unsigned char *w, previous, c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200390 garbage = malloc(words);
391 memset(garbage, 0, words);
392 for (i = 0; i < words; i++) {
393 w = vocab + i * max_w;
394 previous = 0;
Marc Kupietzab591a82016-04-28 14:08:49 +0200395 while((c = *w++) && !garbage[i]) {
396 if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
397 (previous == '-' && (c & 32)) ||
Marc Kupietz33e3aa12017-11-09 16:19:38 +0100398 (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) ||
399 c == '<'
Marc Kupietzab591a82016-04-28 14:08:49 +0200400 ) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200401 garbage[i]=1;
402 continue;
403 }
404 previous = c;
405 }
406 }
407 return;
408}
409
Marc Kupietz271e2a42016-03-22 11:37:43 +0100410void *getCollocators(knnpars *pars) {
411 int N = pars->N;
412 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100413 knn *nbs = NULL;
414 long window_layer_size = size * window * 2;
415 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
416 float f, max_f, maxmax_f;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100417 float *target_sums, *bestf, *bestn, worstbest, wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100418 long long *besti, *bestp;
Marc Kupietzd5642582016-03-19 22:23:13 +0100419
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200420 if(M2 == NULL || cc == -1)
Marc Kupietzd5642582016-03-19 22:23:13 +0100421 return NULL;
422
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100423 a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100424 besti = malloc(N * sizeof(long long));
425 bestp = malloc(N * sizeof(long long));
426 bestf = malloc(N * sizeof(float));
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100427 bestn = malloc(N * sizeof(float));
428
Marc Kupietz271e2a42016-03-22 11:37:43 +0100429 worstbest = MIN_RESP;
430
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100431 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100432 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100433 for (b = 0; b < N; b++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100434 besti[b] = -1;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100435 bestn[b] = 1;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100436 bestf[b] = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100437 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100438
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100439 d = cc;
440 maxmax_f = -1;
441 maxmax_target = 0;
442
Marc Kupietz271e2a42016-03-22 11:37:43 +0100443 for (a = pars->from; a < pars->upto; a++) {
444 if(a >= window)
445 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100446 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100447 printf("window pos: %ld\n", a);
448 if (a != window) {
449 max_f = -1;
450 window_offset = a * size;
451 if (a > window)
452 window_offset -= size;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100453 for(target = 0; target < pars->cutoff; target ++) {
454 if(garbage && garbage[target]) continue;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100455 if(target == d)
456 continue;
457 f = 0;
458 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100459 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100460 if (f < -MAX_EXP)
461 continue;
462 else if (f > MAX_EXP)
463 continue;
464 else
465 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100466 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100467
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100468 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100469 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100470 for (b = 0; b < N; b++) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100471 if (f > bestf[b]) {
Marc Kupietz33679a32016-03-22 08:49:39 +0100472 memmove(bestf + b + 1, bestf + b, (N - b -1) * sizeof(float));
473 memmove(besti + b + 1, besti + b, (N - b -1) * sizeof(long long));
474 memmove(bestp + b + 1, bestp + b, (N - b -1) * sizeof(long long));
475 bestf[b] = f;
476 besti[b] = target;
477 bestp[b] = window-a;
478 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100479 }
480 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100481 if(b == N - 1)
482 worstbest = bestf[N-1];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100483 }
484 }
485 printf("%d %.2f\n", max_target, max_f);
486 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
487 if(max_f > maxmax_f) {
488 maxmax_f = max_f;
489 maxmax_target = max_target;
490 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100491 for (b = 0; b < N; b++)
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100492 if(bestp[b] == window-a)
493 bestn[b] = bestf[b] / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100494 } else {
495 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
496 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100497 pars->window_sums[a] = wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100498 }
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100499 for (b = 0; b < pars->cutoff; b++)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100500 pars->target_sums[b] += (target_sums[b] / wpos_sum ) / (window * 2);
501 free(target_sums);
Marc Kupietzbd41da32017-11-24 10:26:43 +0100502 for(b=0; b<N && besti[b] >= 0; b++);; // THIS LOOP IS NEEDED (b...)
503// printf("%d: best syn: %s %.2f %.5f\n", b, &vocab[besti[b]*max_w], bestf[b], bestn[b]);
504// printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100505 nbs = malloc(sizeof(knn));
506 nbs->index = besti;
507 nbs->dist = bestf;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100508 nbs->norm = bestn;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100509 nbs->pos = bestp;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100510 nbs->length = b-1;
511 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100512}
513
Marc Kupietz30ca4342017-11-22 21:21:20 +0100514
Marc Kupietza2e64502016-04-27 09:53:51 +0200515wordlist *getTargetWords(char *st1, int search_backw) {
Marc Kupietz48c29682016-03-19 11:30:43 +0100516 wordlist *wl = malloc(sizeof(wordlist));
517 char st[100][max_size], sep[100];
518 long a, b=0, c=0, cn=0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200519 int unmerged;
520
Marc Kupietzdc22b982015-10-09 09:19:34 +0200521 while (1) {
522 st[cn][b] = st1[c];
523 b++;
524 c++;
525 st[cn][b] = 0;
526 if (st1[c] == 0) break;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100527 if (st1[c] == ' ' || st1[c] == '-') {
528 sep[cn++] = st1[c];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200529 b = 0;
530 c++;
531 }
532 }
533 cn++;
534 for (a = 0; a < cn; a++) {
Marc Kupietza2e64502016-04-27 09:53:51 +0200535 if(search_backw) {
536 for (b = words - 1; b >= 0; b--) if (!strcmp(&vocab[b * max_w], st[a])) break;
537 } else {
538 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
539 }
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100540 if (b == words) b = -1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100541 wl->wordi[a] = b;
542 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], wl->wordi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200543 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100544 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100545 cn--;
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100546 free(wl);
547 return NULL;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200548 }
549 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100550 wl->length=cn;
551 return(wl);
552}
553
554void *_get_neighbours(knnpars *pars) {
555 char *st1 = pars->token;
556 int N = pars->N;
557 long from = pars -> from;
558 unsigned long upto = pars -> upto;
559 char file_name[max_size], st[100][max_size], *sep;
560 float dist, len, *bestd, vec[max_size];
561 long long a, b, c, d, cn, *bi, *besti;
562 char ch;
563 knn *nbs = NULL;
564 wordlist *wl = pars->wl;
565
566 besti = malloc(N * sizeof(long long));
567 bestd = malloc(N * sizeof(float));
568
569 float worstbest=-1;
570
571 for (a = 0; a < N; a++) bestd[a] = 0;
572 a = 0;
573 bi = wl->wordi;
574 cn = wl->length;
575 sep = wl->sep;
576 b = bi[0];
577 c = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100578 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100579 N = 0;
580 goto end;
581 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200582 for (a = 0; a < size; a++) vec[a] = 0;
583 for (b = 0; b < cn; b++) {
584 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100585 if(b>0 && sep[b-1] == '-')
586 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
587 else
588 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200589 }
590 len = 0;
591 for (a = 0; a < size; a++) len += vec[a] * vec[a];
592 len = sqrt(len);
593 for (a = 0; a < size; a++) vec[a] /= len;
594 for (a = 0; a < N; a++) bestd[a] = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100595 for (c = from; c < upto; c++) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200596 if(garbage && garbage[c]) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200597 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100598// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100599// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
600// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200601 dist = 0;
602 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100603 if(dist > worstbest) {
604 for (a = 0; a < N; a++) {
605 if (dist > bestd[a]) {
Marc Kupietz33679a32016-03-22 08:49:39 +0100606 memmove(bestd + a + 1, bestd + a, (N - a -1) * sizeof(float));
607 memmove(besti + a + 1, besti + a, (N - a -1) * sizeof(long long));
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100608 bestd[a] = dist;
609 besti[a] = c;
610 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200611 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200612 }
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100613 worstbest = bestd[N-1];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200614 }
615 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100616
Marc Kupietz000ad862016-02-26 14:59:12 +0100617 nbs = malloc(sizeof(knn));
618 nbs->index = besti;
619 nbs->dist = bestd;
620 nbs->length = N;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100621end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100622 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200623}
624
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100625
Marc Kupietzd91212f2017-11-13 10:05:09 +0100626SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100627 HV *result = newHV();
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100628 float *target_sums, bestd[MAX_NEIGHBOURS], bestn[MAX_NEIGHBOURS], bests[MAX_NEIGHBOURS], vec[max_size];
Marc Kupietza5f60042017-05-04 10:38:12 +0200629 long long old_words;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100630 long besti[MAX_NEIGHBOURS], bestp[MAX_NEIGHBOURS], a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100631 knn *para_nbs[MAX_THREADS];
632 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100633 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100634 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100635 wordlist *wl;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200636 int syn_threads = (M2? window * 2 : 0);
637 int para_threads = num_threads - syn_threads;
Marc Kupietz48c29682016-03-19 11:30:43 +0100638
Marc Kupietz000ad862016-02-26 14:59:12 +0100639 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
640
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100641 if(cutoff < 1)
642 cutoff=words;
643
Marc Kupietza2e64502016-04-27 09:53:51 +0200644 wl = getTargetWords(st1, search_backw);
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100645 if(wl == NULL || wl->length < 1)
Marc Kupietz271e2a42016-03-22 11:37:43 +0100646 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100647
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100648 old_words = cutoff;
Marc Kupietza5f60042017-05-04 10:38:12 +0200649 if(merge_words > 0)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100650 cutoff = merge_words * 1.25; /* HACK */
651 slice = cutoff / para_threads;
Marc Kupietza5f60042017-05-04 10:38:12 +0200652
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100653 a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float));
654 for(a = 0; a < cutoff; a++)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100655 target_sums[a] = 0;
656
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200657 printf("Starting %d threads\n", para_threads);
658 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100659 for(a=0; a < para_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100660 pars[a].cutoff = cutoff;
Marc Kupietz000ad862016-02-26 14:59:12 +0100661 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100662 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100663 pars[a].N = N;
664 pars[a].from = a*slice;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100665 pars[a].upto = ((a+1)*slice > cutoff? cutoff:(a+1)*slice);
Marc Kupietz000ad862016-02-26 14:59:12 +0100666 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
667 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200668 if(M2) {
669 for(a=0; a < syn_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100670 pars[a + para_threads].cutoff = cutoff;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200671 pars[a + para_threads].target_sums = target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100672 pars[a + para_threads].window_sums = window_sums;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200673 pars[a + para_threads].wl = wl;
674 pars[a + para_threads].N = N;
675 pars[a + para_threads].from = a;
676 pars[a + para_threads].upto = a+1;
677 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
678 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100679 }
680 printf("Waiting for para threads to join\n");
681 fflush(stdout);
682 for (a = 0; a < para_threads; a++) pthread_join(pt[a], &para_nbs[a]);
683 printf("Para threads joint\n");
684 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +0100685
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200686 /* if(!syn_nbs[0]) */
687 /* goto end; */
Marc Kupietz000ad862016-02-26 14:59:12 +0100688
689 for(b=0; b < N; b++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100690 besti[b] = para_nbs[0]->index[b];
691 bestd[b] = para_nbs[0]->dist[b];
Marc Kupietz000ad862016-02-26 14:59:12 +0100692 }
693
Marc Kupietz271e2a42016-03-22 11:37:43 +0100694 for(a=1; a < para_threads; a++) {
695 for(b=0; b < para_nbs[a]->length && para_nbs[a]->index[b] >= 0; b++) {
Marc Kupietza5f60042017-05-04 10:38:12 +0200696 for(c=0; c < N * para_threads; c++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100697 if(para_nbs[a]->dist[b] > bestd[c]) {
Marc Kupietz000ad862016-02-26 14:59:12 +0100698 for(d=N-1; d>c; d--) {
699 bestd[d] = bestd[d-1];
700 besti[d] = besti[d-1];
701 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100702 besti[c] = para_nbs[a]->index[b];
703 bestd[c] = para_nbs[a]->dist[b];
Marc Kupietz000ad862016-02-26 14:59:12 +0100704 break;
705 }
706 }
707 }
708 }
709
Marc Kupietzd91212f2017-11-13 10:05:09 +0100710 char *chosen[600];
Marc Kupietz271e2a42016-03-22 11:37:43 +0100711 AV* array = newAV();
Marc Kupietzd91212f2017-11-13 10:05:09 +0100712 int i, j;
Marc Kupietza5f60042017-05-04 10:38:12 +0200713 int l1_words=0, l2_words=0;
714 for (a = 0, i = 0; i < N && a < 600; a++) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100715 int filtered=0;
Marc Kupietza5f60042017-05-04 10:38:12 +0200716 long long c = besti[a];
Marc Kupietzd91212f2017-11-13 10:05:09 +0100717 if (dedupe && i > 0) {
718 for (j=0; j<i; j++)
719 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
720 strcasestr(chosen[j], &vocab[c * max_w])) {
721 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
722 filtered = 1;
723 }
724 if(filtered)
725 continue;
726 }
Marc Kupietza5f60042017-05-04 10:38:12 +0200727 if(merge_words > 0) {
728 if(c >= merge_words) {
729 if(l1_words > N / 2)
730 continue;
731 else
732 l1_words++;
733 } else {
734 if(l2_words > N / 2)
735 continue;
736 else
737 l2_words++;
738 }
739 }
740 fflush(stdout);
741 printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
742
Marc Kupietz271e2a42016-03-22 11:37:43 +0100743 HV* hash = newHV();
Marc Kupietza5f60042017-05-04 10:38:12 +0200744 SV* word = newSVpvf(&vocab[c * max_w], 0);
Marc Kupietzd91212f2017-11-13 10:05:09 +0100745 chosen[i] = &vocab[c * max_w];
Marc Kupietz271e2a42016-03-22 11:37:43 +0100746 if(latin_enc == 0) SvUTF8_on(word);
Marc Kupietza5f60042017-05-04 10:38:12 +0200747 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100748 hv_store(hash, "word", strlen("word"), word , 0);
749 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
750 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
751 AV *vector = newAV();
752 for (b = 0; b < size; b++) {
753 av_push(vector, newSVnv(M[b + besti[a] * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100754 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100755 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
756 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietza5f60042017-05-04 10:38:12 +0200757 i++;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100758 }
759 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
760
Marc Kupietz50485ba2016-03-23 09:13:14 +0100761 for(b=0; b < MAX_NEIGHBOURS; b++) {
762 besti[b] = -1L;
763 bestd[b] = 0;
764 bestn[b] = 0;
765 bestp[b] = 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100766 bests[b] = 0;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100767 }
768
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200769 if (M2) {
770 printf("Waiting for syn threads to join\n");
771 fflush(stdout);
772 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], &syn_nbs[a]);
Marc Kupietzbd41da32017-11-24 10:26:43 +0100773 for (a = 0; a <= syn_threads; a++) printf("window pos: %d, sum: %f\n", a, window_sums[a]);
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200774 printf("syn threads joint\n");
775 fflush(stdout);
Marc Kupietz50485ba2016-03-23 09:13:14 +0100776
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200777 for(b=0; b < syn_nbs[0]->length; b++) {
778 besti[b] = syn_nbs[0]->index[b];
779 bestd[b] = syn_nbs[0]->dist[b];
780 bestn[b] = syn_nbs[0]->norm[b];
Marc Kupietz30ca4342017-11-22 21:21:20 +0100781 bestp[b] = -1; // syn_nbs[0]->pos[b];
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200782 bests[b] = target_sums[syn_nbs[0]->index[b]];
783 }
Marc Kupietz30ca4342017-11-22 21:21:20 +0100784
785 float best_window_sum[MAX_NEIGHBOURS];
786 int found_index=0, i=0, j, w;
787 if(sort_by != 1 && sort_by != 2) { // sort by auto focus mean
Marc Kupietzbd41da32017-11-24 10:26:43 +0100788 for(a=0; a < syn_threads; a++) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200789 for(b=0; b < syn_nbs[a]->length; b++) {
Marc Kupietz30ca4342017-11-22 21:21:20 +0100790 for(i=0; i < found_index; i++)
791 if(besti[i] == syn_nbs[a]->index[b])
792 break;
793 if(i >= found_index) {
794 besti[found_index++] = syn_nbs[a]->index[b];
795// printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
796 }
797 }
798 }
799 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) -1);
Marc Kupietzbd41da32017-11-24 10:26:43 +0100800 int wpos;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100801 for(i=0; i < found_index; i++) {
Marc Kupietzbd41da32017-11-24 10:26:43 +0100802 bestd[i] = 0; bestn[i] = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100803 for(w=1; w < (1 << syn_threads); w++) { // loop through all possible windows
Marc Kupietzbd41da32017-11-24 10:26:43 +0100804 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100805 int bits_set = 0;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100806 for(a=0; a < syn_threads; a++) {
Marc Kupietz30ca4342017-11-22 21:21:20 +0100807 if((1 << a) & w) {
Marc Kupietzbd41da32017-11-24 10:26:43 +0100808 wpos = (a >= window? a+1 : a);
809 total_window_sum += window_sums[wpos];
Marc Kupietz30ca4342017-11-22 21:21:20 +0100810 }
811 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100812// printf("%d window-sum %f\n", w, total_window_sum);
813 for(a=0; a < syn_threads; a++) {
814 if((1 << a) & w) {
815 wpos = (a >= window? a+1 : a);
816 bits_set++;
817 for(b=0; b < syn_nbs[a]->length; b++)
818 if(besti[i] == syn_nbs[a]->index[b]) {
819// word_window_sum += syn_nbs[a]->dist[b] * syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
820// word_window_sum += syn_nbs[a]->dist[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
821// word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
822// word_window_sum = (word_window_sum + syn_nbs[a]->norm[b]) - (word_window_sum * syn_nbs[a]->norm[b]); // syn_nbs[a]->norm[b];
823 word_window_sum += syn_nbs[a]->dist[b] - word_window_sum * syn_nbs[a]->dist[b]; // conormalied activation sum
824 word_activation_sum += syn_nbs[a]->dist[b];
825 }
826 }
827 }
828// if(bits_set) {
829// word_activation_sum /= bits_set;
830// word_window_sum /= bits_set;
831// }
832// word_window_sum /= total_window_sum;
833
834 if(word_window_sum > bestn[i]) {
835 bestn[i] = word_window_sum;
836 bestd[i] = word_activation_sum;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100837 bestp[i] = w;
838 }
839 }
840 }
841 for(i=0; i<found_index;i++) {
842 for(j=0;j<found_index-1;j++) {
Marc Kupietzbd41da32017-11-24 10:26:43 +0100843 if(bestn[j]<bestn[j+1]) {
844 float tempd=bestn[j];
845 bestn[j]=bestn[j+1];
846 bestn[j+1]=tempd;
847 tempd=bestd[j];
Marc Kupietz30ca4342017-11-22 21:21:20 +0100848 bestd[j]=bestd[j+1];
849 bestd[j+1]=tempd;
850 int tempi=besti[j];
851 besti[j]=besti[j+1];
852 besti[j+1]=tempi;
853 int tempp=bestp[j];
854 bestp[j]=bestp[j+1];
855 bestp[j+1]=tempp;
856 }
857 }
858 }
859// for(i=0; i < found_index; i++) {
860// printf("found: %s - sum: %f - window: %d\n", &vocab[besti[i] * max_w], bestd[i], bestp[i]);
861// }
862
863 } else if(sort_by ==1) { // single window position
864 for(a=1; a < syn_threads; a++) {
865 for(b=0; b < syn_nbs[a]->length; b++) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200866 for(c=0; c < MAX_NEIGHBOURS; c++) {
867 if(syn_nbs[a]->dist[b] > bestd[c]) {
868 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
869 bestd[d] = bestd[d-1];
870 besti[d] = besti[d-1];
871 bestn[d] = bestn[d-1];
872 bestp[d] = bestp[d-1];
873 }
874 besti[c] = syn_nbs[a]->index[b];
875 bestd[c] = syn_nbs[a]->dist[b];
876 bestn[c] = syn_nbs[a]->norm[b];
Marc Kupietz30ca4342017-11-22 21:21:20 +0100877 bestp[c] = 1 << (-syn_nbs[a]->pos[b]+window - (syn_nbs[a]->pos[b] < 0 ? 1:0));
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200878 break;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100879 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200880 }
881 }
882 }
883 } else { // sort by mean p
884 for(a=1; a < syn_threads; a++) {
885 for(b=0; b < syn_nbs[a]->length; b++) {
886 for(c=0; c < MAX_NEIGHBOURS; c++) {
887 if(target_sums[syn_nbs[a]->index[b]] > bests[c]) {
888 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
889 bestd[d] = bestd[d-1];
890 besti[d] = besti[d-1];
891 bestn[d] = bestn[d-1];
892 bestp[d] = bestp[d-1];
893 bests[d] = bests[d-1];
894 }
895 besti[c] = syn_nbs[a]->index[b];
896 bestd[c] = syn_nbs[a]->dist[b];
897 bestn[c] = syn_nbs[a]->norm[b];
Marc Kupietz30ca4342017-11-22 21:21:20 +0100898 bestp[c] = (1 << 2*window) - 1; // syn_nbs[a]->pos[b];
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200899 bests[c] = target_sums[syn_nbs[a]->index[b]];
900 break;
901 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100902 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100903 }
904 }
905 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200906 array = newAV();
Marc Kupietzd91212f2017-11-13 10:05:09 +0100907 for (a = 0, i=0; a < MAX_NEIGHBOURS && besti[a] >= 0; a++) {
908 long long c = besti[a];
909 if (dedupe) {
910 int filtered=0;
911 for (j=0; j<i; j++)
912 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
913 strcasestr(chosen[j], &vocab[c * max_w])) {
914 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
915 filtered = 1;
916 }
917 if(filtered)
918 continue;
919 }
920 chosen[i++]=&vocab[c * max_w];
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200921 HV* hash = newHV();
922 SV* word = newSVpvf(&vocab[besti[a] * max_w], 0);
923 if(latin_enc == 0) SvUTF8_on(word);
924 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzb18978b2017-11-09 14:51:17 +0100925 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200926 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
927 hv_store(hash, "norm", strlen("norm"), newSVnv(bestn[a]), 0);
928 hv_store(hash, "sum", strlen("sum"), newSVnv(target_sums[besti[a]]), 0);
929 hv_store(hash, "pos", strlen("pos"), newSVnv(bestp[a]), 0);
930 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietz271e2a42016-03-22 11:37:43 +0100931 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200932 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100933 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100934end:
Marc Kupietza5f60042017-05-04 10:38:12 +0200935 words = old_words;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100936 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +0100937}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100938
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200939int dump_vecs(char *fname) {
940 long i, j;
941 FILE *f;
942 /* if(words>200000) */
943 /* words=200000; */
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100944
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200945 if((f=fopen(fname, "w")) == NULL) {
946 fprintf(stderr, "cannot open %s for writing\n", fname);
947 return(-1);
948 }
949 fprintf(f, "%lld %lld\n", words, size);
950 for (i=0; i < words; i++) {
951 fprintf(f, "%s ", &vocab[i * max_w]);
952 for(j=0; j < size - 1; j++)
953 fprintf(f, "%f ", M[i*size + j]);
954 fprintf(f, "%f\n", M[i*size + j]);
955 }
956 fclose(f);
957 return(0);
958}
Marc Kupietzdc22b982015-10-09 09:19:34 +0200959