blob: 6d6f4214c0431dfc55f9c44a06bf16a45d5da20e [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 {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100181 long long wordi;
182 long position;
183 float activation;
184 float activation_sum;
185 float probability_sum;
186 float probability;
187} collocator;
188
189typedef struct {
190 collocator *best;
Marc Kupietz80abb442016-03-23 21:04:08 +0100191 int length;
Marc Kupietz000ad862016-02-26 14:59:12 +0100192} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100193
Marc Kupietz000ad862016-02-26 14:59:12 +0100194typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +0100195 long long wordi[MAX_NEIGHBOURS];
196 char sep[MAX_NEIGHBOURS];
197 int length;
198} wordlist;
199
200typedef struct {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100201 long cutoff;
Marc Kupietz48c29682016-03-19 11:30:43 +0100202 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100203 char *token;
204 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100205 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100206 unsigned long upto;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100207 float *target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100208 float *window_sums;
Marc Kupietz000ad862016-02-26 14:59:12 +0100209} knnpars;
210
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200211float *M, *M2=0L, *syn1neg_window, *expTable;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100212float *window_sums;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200213char *vocab;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200214char *garbage = NULL;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100215
Marc Kupietza2e64502016-04-27 09:53:51 +0200216long long words, size, merged_end;
Marc Kupietza5f60042017-05-04 10:38:12 +0200217long long merge_words = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100218int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100219int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100220int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200221
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100222int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100223 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100224 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100225 long long a, b, c, d, cn;
226 float len;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200227 double val;
Marc Kupietz82b02672016-02-26 12:32:25 +0100228
Marc Kupietz67c20282016-02-26 09:42:00 +0100229 char binvecs_fname[256], binwords_fname[256];
230 strcpy(binwords_fname, file_name);
231 strcat(binwords_fname, ".words");
232 strcpy(binvecs_fname, file_name);
233 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200234
Marc Kupietza5b90152016-03-15 17:39:19 +0100235 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200236 f = fopen(file_name, "rb");
237 if (f == NULL) {
238 printf("Input file %s not found\n", file_name);
239 return -1;
240 }
241 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100242 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200243 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100244 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
245 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100246 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
247 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
248 if (M == NULL) {
249 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
250 return -1;
251 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200252 if(strstr(file_name, ".txt")) {
253 for (b = 0; b < words; b++) {
254 a = 0;
255 while (1) {
256 vocab[b * max_w + a] = fgetc(f);
257 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
258 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
259 }
260 vocab[b * max_w + a] = 0;
261 len = 0;
262 for (a = 0; a < size; a++) {
263 fscanf(f, "%lf", &val);
264 M[a + b * size] = val;
265 len += val * val;
266 }
267 len = sqrt(len);
268 for (a = 0; a < size; a++) M[a + b * size] /= len;
269 }
270 } else {
271 for (b = 0; b < words; b++) {
272 a = 0;
273 while (1) {
274 vocab[b * max_w + a] = fgetc(f);
275 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
276 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
277 }
278 vocab[b * max_w + a] = 0;
279 fread(&M[b * size], sizeof(float), size, f);
280 len = 0;
281 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
282 len = sqrt(len);
283 for (a = 0; a < size; a++) M[a + b * size] /= len;
284 }
285 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100286 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
287 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
288 fclose(binvecs);
289 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
290 fclose(binwords);
291 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100292 }
293 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
294 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
295 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
296 if (M == MAP_FAILED || vocab == MAP_FAILED) {
297 close(binvecs_fd);
298 close(binwords_fd);
299 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
300 exit(-1);
301 }
302 } else {
303 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
304 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100305 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200306 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100307
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200308 if(net_name && strlen(net_name) > 0) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100309 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
310 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
311 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100312 // munmap(M, sizeof(float) * words * size);
313 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 +0200314 if (M2 == MAP_FAILED) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100315 close(net_fd);
316 fprintf(stderr, "Cannot mmap %s\n", net_name);
317 exit(-1);
318 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100319 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100320 } else {
321 fprintf(stderr, "Cannot open %s\n", net_name);
322 exit(-1);
323 }
324 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
325 }
326
327 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
328 for (i = 0; i < EXP_TABLE_SIZE; i++) {
329 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
330 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
331 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100332 window_sums = malloc(sizeof(float) * (window+1) * 2);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200333 return 0;
334}
335
Marc Kupietza2e64502016-04-27 09:53:51 +0200336long mergeVectors(char *file_name){
337 FILE *f, *binvecs, *binwords;
338 int binwords_fd, binvecs_fd, net_fd, i;
339 long long a, b, c, d, cn;
340 float len;
341 float *merge_vecs;
342 char *merge_vocab;
Marc Kupietza5f60042017-05-04 10:38:12 +0200343 /* long long merge_words, merge_size; */
344 long long merge_size;
Marc Kupietza2e64502016-04-27 09:53:51 +0200345
346 char binvecs_fname[256], binwords_fname[256];
347 strcpy(binwords_fname, file_name);
348 strcat(binwords_fname, ".words");
349 strcpy(binvecs_fname, file_name);
350 strcat(binvecs_fname, ".vecs");
351
352 f = fopen(file_name, "rb");
353 if (f == NULL) {
354 printf("Input file %s not found\n", file_name);
355 exit -1;
356 }
357 fscanf(f, "%lld", &merge_words);
358 fscanf(f, "%lld", &merge_size);
359 if(merge_size != size){
360 fprintf(stderr, "vectors must have the same length\n");
361 exit(-1);
362 }
363 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
364 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
365 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
366 if (merge_vecs == NULL || merge_vocab == NULL) {
367 close(binvecs_fd);
368 close(binwords_fd);
369 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
370 exit(-1);
371 }
372 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
373 read(binwords_fd, merge_vocab, merge_words * max_w);
374 } else {
375 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
376 exit(-1);
377 }
378 printf("Successfully reallocated memory\nMerging...\n");
379 fflush(stdout);
380 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
381 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
382 munmap(M, words * size * sizeof(float));
383 munmap(vocab, words * max_w);
384 M = merge_vecs;
385 vocab = merge_vocab;
386 merged_end = merge_words;
387 words += merge_words;
388 fclose(f);
389 printf("merged_end: %lld, words: %lld\n", merged_end, words);
390 return((long) merged_end);
391}
392
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200393void filter_garbage() {
394 long i;
Marc Kupietzab591a82016-04-28 14:08:49 +0200395 unsigned char *w, previous, c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200396 garbage = malloc(words);
397 memset(garbage, 0, words);
398 for (i = 0; i < words; i++) {
399 w = vocab + i * max_w;
400 previous = 0;
Marc Kupietzab591a82016-04-28 14:08:49 +0200401 while((c = *w++) && !garbage[i]) {
402 if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
403 (previous == '-' && (c & 32)) ||
Marc Kupietz33e3aa12017-11-09 16:19:38 +0100404 (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) ||
405 c == '<'
Marc Kupietzab591a82016-04-28 14:08:49 +0200406 ) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200407 garbage[i]=1;
408 continue;
409 }
410 previous = c;
411 }
412 }
413 return;
414}
415
Marc Kupietz271e2a42016-03-22 11:37:43 +0100416void *getCollocators(knnpars *pars) {
417 int N = pars->N;
418 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100419 knn *nbs = NULL;
420 long window_layer_size = size * window * 2;
421 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
422 float f, max_f, maxmax_f;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100423 float *target_sums, worstbest, wpos_sum;
424 collocator *best;
Marc Kupietzd5642582016-03-19 22:23:13 +0100425
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200426 if(M2 == NULL || cc == -1)
Marc Kupietzd5642582016-03-19 22:23:13 +0100427 return NULL;
428
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100429 a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100430 best = malloc(N * sizeof(collocator));
Marc Kupietz271e2a42016-03-22 11:37:43 +0100431 worstbest = MIN_RESP;
432
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100433 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100434 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100435 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100436 best[b].wordi = -1;
437 best[b].probability = 1;
438 best[b].activation = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100439 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100440
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100441 d = cc;
442 maxmax_f = -1;
443 maxmax_target = 0;
444
Marc Kupietz271e2a42016-03-22 11:37:43 +0100445 for (a = pars->from; a < pars->upto; a++) {
446 if(a >= window)
447 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100448 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100449 printf("window pos: %ld\n", a);
450 if (a != window) {
451 max_f = -1;
452 window_offset = a * size;
453 if (a > window)
454 window_offset -= size;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100455 for(target = 0; target < pars->cutoff; target ++) {
456 if(garbage && garbage[target]) continue;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100457 if(target == d)
458 continue;
459 f = 0;
460 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100461 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100462 if (f < -MAX_EXP)
463 continue;
464 else if (f > MAX_EXP)
465 continue;
466 else
467 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100468 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100469
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100470 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100471 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100472 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100473 if (f > best[b].activation) {
474 memmove(best + b + 1, best + b, (N - b -1) * sizeof(collocator));
475 best[b].activation = f;
476 best[b].wordi = target;
477 best[b].position = window-a;
Marc Kupietz33679a32016-03-22 08:49:39 +0100478 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100479 }
480 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100481 if(b == N - 1)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100482 worstbest = best[N-1].activation;
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 Kupietzc8fdf3c2017-11-24 15:32:19 +0100492 if(best[b].position == window-a)
493 best[b].probability = best[b].activation / 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 Kupietzc8fdf3c2017-11-24 15:32:19 +0100502 for(b=0; b<N && best[b].wordi >= 0; b++);; // THIS LOOP IS NEEDED (b...)
503// printf("%d: best syn: %s %.2f %.5f\n", b, &vocab[best[b].wordi*max_w], best[b].activation, best[b].probability);
Marc Kupietzbd41da32017-11-24 10:26:43 +0100504// printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100505 nbs = malloc(sizeof(knn));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100506 nbs->best = best;
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;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100557 float dist, len, vec[max_size];
558 long long a, b, c, d, cn, *bi;
Marc Kupietz48c29682016-03-19 11:30:43 +0100559 char ch;
560 knn *nbs = NULL;
561 wordlist *wl = pars->wl;
562
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100563 collocator *best = malloc(N * sizeof(collocator));
Marc Kupietz48c29682016-03-19 11:30:43 +0100564
565 float worstbest=-1;
566
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100567 for (a = 0; a < N; a++) best[a].activation = 0;
Marc Kupietz48c29682016-03-19 11:30:43 +0100568 a = 0;
569 bi = wl->wordi;
570 cn = wl->length;
571 sep = wl->sep;
572 b = bi[0];
573 c = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100574 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100575 N = 0;
576 goto end;
577 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200578 for (a = 0; a < size; a++) vec[a] = 0;
579 for (b = 0; b < cn; b++) {
580 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100581 if(b>0 && sep[b-1] == '-')
582 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
583 else
584 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200585 }
586 len = 0;
587 for (a = 0; a < size; a++) len += vec[a] * vec[a];
588 len = sqrt(len);
589 for (a = 0; a < size; a++) vec[a] /= len;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100590 for (a = 0; a < N; a++) best[a].activation = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100591 for (c = from; c < upto; c++) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200592 if(garbage && garbage[c]) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200593 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100594// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100595// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
596// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200597 dist = 0;
598 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100599 if(dist > worstbest) {
600 for (a = 0; a < N; a++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100601 if (dist > best[a].activation) {
602 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
603 best[a].activation = dist;
604 best[a].wordi = c;
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100605 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200606 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200607 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100608 worstbest = best[N-1].activation;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200609 }
610 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100611
Marc Kupietz000ad862016-02-26 14:59:12 +0100612 nbs = malloc(sizeof(knn));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100613 nbs->best = best;
Marc Kupietz000ad862016-02-26 14:59:12 +0100614 nbs->length = N;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100615end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100616 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200617}
618
Marc Kupietze0e42132017-11-24 16:44:34 +0100619int cmp_activation (const void * a, const void * b) {
620 float fb = ((collocator *)a)->activation;
621 float fa = ((collocator *)b)->activation;
622 return (fa > fb) - (fa < fb);
623}
624
625int cmp_probability (const void * a, const void * b) {
626 float fb = ((collocator *)a)->probability;
627 float fa = ((collocator *)b)->probability;
628 return (fa > fb) - (fa < fb);
629}
630
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100631
Marc Kupietzd91212f2017-11-13 10:05:09 +0100632SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100633 HV *result = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100634 float *target_sums, vec[max_size];
Marc Kupietza5f60042017-05-04 10:38:12 +0200635 long long old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100636 long a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100637 knn *para_nbs[MAX_THREADS];
638 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100639 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100640 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100641 wordlist *wl;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200642 int syn_threads = (M2? window * 2 : 0);
643 int para_threads = num_threads - syn_threads;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100644
645 collocator *best = malloc(MAX_NEIGHBOURS * sizeof(collocator));
646
Marc Kupietz000ad862016-02-26 14:59:12 +0100647 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
648
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100649 if(cutoff < 1)
650 cutoff=words;
651
Marc Kupietza2e64502016-04-27 09:53:51 +0200652 wl = getTargetWords(st1, search_backw);
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100653 if(wl == NULL || wl->length < 1)
Marc Kupietz271e2a42016-03-22 11:37:43 +0100654 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100655
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100656 old_words = cutoff;
Marc Kupietza5f60042017-05-04 10:38:12 +0200657 if(merge_words > 0)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100658 cutoff = merge_words * 1.25; /* HACK */
659 slice = cutoff / para_threads;
Marc Kupietza5f60042017-05-04 10:38:12 +0200660
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100661 a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float));
662 for(a = 0; a < cutoff; a++)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100663 target_sums[a] = 0;
664
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200665 printf("Starting %d threads\n", para_threads);
666 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100667 for(a=0; a < para_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100668 pars[a].cutoff = cutoff;
Marc Kupietz000ad862016-02-26 14:59:12 +0100669 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100670 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100671 pars[a].N = N;
672 pars[a].from = a*slice;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100673 pars[a].upto = ((a+1)*slice > cutoff? cutoff:(a+1)*slice);
Marc Kupietz000ad862016-02-26 14:59:12 +0100674 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
675 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200676 if(M2) {
677 for(a=0; a < syn_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100678 pars[a + para_threads].cutoff = cutoff;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200679 pars[a + para_threads].target_sums = target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100680 pars[a + para_threads].window_sums = window_sums;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200681 pars[a + para_threads].wl = wl;
682 pars[a + para_threads].N = N;
683 pars[a + para_threads].from = a;
684 pars[a + para_threads].upto = a+1;
685 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
686 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100687 }
688 printf("Waiting for para threads to join\n");
689 fflush(stdout);
690 for (a = 0; a < para_threads; a++) pthread_join(pt[a], &para_nbs[a]);
691 printf("Para threads joint\n");
692 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +0100693
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200694 /* if(!syn_nbs[0]) */
695 /* goto end; */
Marc Kupietz000ad862016-02-26 14:59:12 +0100696
697 for(b=0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100698 best[b].wordi = para_nbs[0]->best[b].wordi;
699 best[b].activation = para_nbs[0]->best[b].activation;
Marc Kupietz000ad862016-02-26 14:59:12 +0100700 }
701
Marc Kupietz271e2a42016-03-22 11:37:43 +0100702 for(a=1; a < para_threads; a++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100703 for(b=0; b < para_nbs[a]->length && para_nbs[a]->best[b].wordi >= 0; b++) {
Marc Kupietza5f60042017-05-04 10:38:12 +0200704 for(c=0; c < N * para_threads; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100705 if(para_nbs[a]->best[b].activation > best[c].activation) {
Marc Kupietz000ad862016-02-26 14:59:12 +0100706 for(d=N-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100707 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz000ad862016-02-26 14:59:12 +0100708 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100709 memcpy(best + c, &para_nbs[a]->best[b], sizeof(collocator));
Marc Kupietz000ad862016-02-26 14:59:12 +0100710 break;
711 }
712 }
713 }
714 }
715
Marc Kupietzd91212f2017-11-13 10:05:09 +0100716 char *chosen[600];
Marc Kupietz271e2a42016-03-22 11:37:43 +0100717 AV* array = newAV();
Marc Kupietzd91212f2017-11-13 10:05:09 +0100718 int i, j;
Marc Kupietza5f60042017-05-04 10:38:12 +0200719 int l1_words=0, l2_words=0;
720 for (a = 0, i = 0; i < N && a < 600; a++) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100721 int filtered=0;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100722 long long c = best[a].wordi;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100723 if (dedupe && i > 0) {
724 for (j=0; j<i; j++)
725 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
726 strcasestr(chosen[j], &vocab[c * max_w])) {
727 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
728 filtered = 1;
729 }
730 if(filtered)
731 continue;
732 }
Marc Kupietza5f60042017-05-04 10:38:12 +0200733 if(merge_words > 0) {
734 if(c >= merge_words) {
735 if(l1_words > N / 2)
736 continue;
737 else
738 l1_words++;
739 } else {
740 if(l2_words > N / 2)
741 continue;
742 else
743 l2_words++;
744 }
745 }
746 fflush(stdout);
747 printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
748
Marc Kupietz271e2a42016-03-22 11:37:43 +0100749 HV* hash = newHV();
Marc Kupietza5f60042017-05-04 10:38:12 +0200750 SV* word = newSVpvf(&vocab[c * max_w], 0);
Marc Kupietzd91212f2017-11-13 10:05:09 +0100751 chosen[i] = &vocab[c * max_w];
Marc Kupietz271e2a42016-03-22 11:37:43 +0100752 if(latin_enc == 0) SvUTF8_on(word);
Marc Kupietza5f60042017-05-04 10:38:12 +0200753 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100754 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100755 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
756 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100757 AV *vector = newAV();
758 for (b = 0; b < size; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100759 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100760 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100761 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
762 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietza5f60042017-05-04 10:38:12 +0200763 i++;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100764 }
765 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
766
Marc Kupietz50485ba2016-03-23 09:13:14 +0100767 for(b=0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100768 best[b].wordi = -1L;
769 best[b].activation = 0;
770 best[b].probability = 0;
771 best[b].position = 0;
772 best[b].activation_sum = 0;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100773 }
774
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200775 if (M2) {
776 printf("Waiting for syn threads to join\n");
777 fflush(stdout);
778 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], &syn_nbs[a]);
Marc Kupietzbd41da32017-11-24 10:26:43 +0100779 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 +0200780 printf("syn threads joint\n");
781 fflush(stdout);
Marc Kupietz50485ba2016-03-23 09:13:14 +0100782
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200783 for(b=0; b < syn_nbs[0]->length; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100784 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
785 best[b].position = -1; // syn_nbs[0]->pos[b];
786 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200787 }
Marc Kupietz30ca4342017-11-22 21:21:20 +0100788
789 float best_window_sum[MAX_NEIGHBOURS];
790 int found_index=0, i=0, j, w;
791 if(sort_by != 1 && sort_by != 2) { // sort by auto focus mean
Marc Kupietzbd41da32017-11-24 10:26:43 +0100792 for(a=0; a < syn_threads; a++) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200793 for(b=0; b < syn_nbs[a]->length; b++) {
Marc Kupietz30ca4342017-11-22 21:21:20 +0100794 for(i=0; i < found_index; i++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100795 if(best[i].wordi == syn_nbs[a]->best[b].wordi)
Marc Kupietz30ca4342017-11-22 21:21:20 +0100796 break;
797 if(i >= found_index) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100798 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100799// printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
800 }
801 }
802 }
803 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) -1);
Marc Kupietzbd41da32017-11-24 10:26:43 +0100804 int wpos;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100805 for(i=0; i < found_index; i++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100806 best[i].activation = 0; best[i].probability = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100807 for(w=1; w < (1 << syn_threads); w++) { // loop through all possible windows
Marc Kupietzbd41da32017-11-24 10:26:43 +0100808 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100809 int bits_set = 0;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100810 for(a=0; a < syn_threads; a++) {
Marc Kupietz30ca4342017-11-22 21:21:20 +0100811 if((1 << a) & w) {
Marc Kupietzbd41da32017-11-24 10:26:43 +0100812 wpos = (a >= window? a+1 : a);
813 total_window_sum += window_sums[wpos];
Marc Kupietz30ca4342017-11-22 21:21:20 +0100814 }
815 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100816// printf("%d window-sum %f\n", w, total_window_sum);
817 for(a=0; a < syn_threads; a++) {
818 if((1 << a) & w) {
819 wpos = (a >= window? a+1 : a);
820 bits_set++;
821 for(b=0; b < syn_nbs[a]->length; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100822 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
Marc Kupietzbd41da32017-11-24 10:26:43 +0100823// word_window_sum += syn_nbs[a]->dist[b] * syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
824// word_window_sum += syn_nbs[a]->dist[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
825// word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
826// word_window_sum = (word_window_sum + syn_nbs[a]->norm[b]) - (word_window_sum * syn_nbs[a]->norm[b]); // syn_nbs[a]->norm[b];
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100827 word_window_sum += syn_nbs[a]->best[b].activation - word_window_sum * syn_nbs[a]->best[b].activation; // conormalied activation sum
828 word_activation_sum += syn_nbs[a]->best[b].activation;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100829 }
830 }
831 }
832// if(bits_set) {
833// word_activation_sum /= bits_set;
834// word_window_sum /= bits_set;
835// }
836// word_window_sum /= total_window_sum;
837
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100838 if(word_window_sum > best[i].probability) {
839 best[i].probability = word_window_sum;
840 best[i].activation = word_activation_sum;
841 best[i].position = w;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100842 }
843 }
844 }
Marc Kupietze0e42132017-11-24 16:44:34 +0100845 qsort(best, found_index, sizeof(collocator), cmp_probability);
Marc Kupietz30ca4342017-11-22 21:21:20 +0100846// for(i=0; i < found_index; i++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100847// printf("found: %s - sum: %f - window: %d\n", &vocab[best[i].wordi * max_w], best[i].activation, best[i].position);
Marc Kupietz30ca4342017-11-22 21:21:20 +0100848// }
849
850 } else if(sort_by ==1) { // single window position
851 for(a=1; a < syn_threads; a++) {
852 for(b=0; b < syn_nbs[a]->length; b++) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200853 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100854 if(syn_nbs[a]->best[b].activation > best[c].activation) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200855 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100856 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200857 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100858 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
859 best[c].position = 1 << (-syn_nbs[a]->best[b].position+window - (syn_nbs[a]->best[b].position < 0 ? 1:0));
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200860 break;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100861 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200862 }
863 }
864 }
865 } else { // sort by mean p
866 for(a=1; a < syn_threads; a++) {
867 for(b=0; b < syn_nbs[a]->length; b++) {
868 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100869 if(target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200870 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100871 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200872 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100873 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
874 best[c].position = (1 << 2*window) - 1; // syn_nbs[a]->pos[b];
875 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200876 break;
877 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100878 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100879 }
880 }
881 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200882 array = newAV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100883 for (a = 0, i=0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
884 long long c = best[a].wordi;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100885 if (dedupe) {
886 int filtered=0;
887 for (j=0; j<i; j++)
888 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
889 strcasestr(chosen[j], &vocab[c * max_w])) {
890 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
891 filtered = 1;
892 }
893 if(filtered)
894 continue;
895 }
896 chosen[i++]=&vocab[c * max_w];
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200897 HV* hash = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100898 SV* word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200899 if(latin_enc == 0) SvUTF8_on(word);
900 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100901 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
902 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
903 hv_store(hash, "norm", strlen("norm"), newSVnv(best[a].probability), 0);
904 hv_store(hash, "sum", strlen("sum"), newSVnv(target_sums[best[a].wordi]), 0);
905 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200906 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietz271e2a42016-03-22 11:37:43 +0100907 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200908 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100909 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100910end:
Marc Kupietza5f60042017-05-04 10:38:12 +0200911 words = old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100912 free(best);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100913 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +0100914}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100915
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200916int dump_vecs(char *fname) {
917 long i, j;
918 FILE *f;
919 /* if(words>200000) */
920 /* words=200000; */
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100921
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200922 if((f=fopen(fname, "w")) == NULL) {
923 fprintf(stderr, "cannot open %s for writing\n", fname);
924 return(-1);
925 }
926 fprintf(f, "%lld %lld\n", words, size);
927 for (i=0; i < words; i++) {
928 fprintf(f, "%s ", &vocab[i * max_w]);
929 for(j=0; j < size - 1; j++)
930 fprintf(f, "%f ", M[i*size + j]);
931 fprintf(f, "%f\n", M[i*size + j]);
932 }
933 fclose(f);
934 return(0);
935}
Marc Kupietzdc22b982015-10-09 09:19:34 +0200936