blob: 85a7e2525502c141540bd5cc8e0992572c61cc75 [file] [log] [blame]
Marc Kupietzdc22b982015-10-09 09:19:34 +02001#!/usr/local/bin/perl
2use Inline C;
Marc Kupietza2e64502016-04-27 09:53:51 +02003use Inline C => Config => CLEAN_AFTER_BUILD => 0, ccflags => $Config{ccflags}." -O4";
Marc Kupietza5f60042017-05-04 10:38:12 +02004#use Inline C => Config => BUILD_NOISY => 1, CFLAGS => $Config{cflags}." -O4 -mtune k9";
5#use Inline C => Config => CLEAN_AFTER_BUILD => 0, ccflags => $Config{ccflags}." -Ofast -march k8 -mtune k8 ";
Marc Kupietzdc22b982015-10-09 09:19:34 +02006use Mojolicious::Lite;
Marc Kupietzc4893362016-02-25 08:04:46 +01007use Mojo::JSON qw(decode_json encode_json to_json);
Marc Kupietz247500f2015-10-09 11:29:01 +02008use Encode qw(decode encode);
Marc Kupietza5b90152016-03-15 17:39:19 +01009use Getopt::Std;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010010use Mojo::Server::Daemon;
Marc Kupietzd4227392016-03-01 16:45:12 +010011plugin 'Log::Access';
Marc Kupietzb3422c12017-07-04 14:12:11 +020012plugin "RequestBase";
Marc Kupietzdc22b982015-10-09 09:19:34 +020013
Marc Kupietza5b90152016-03-15 17:39:19 +010014our $opt_i = 0; # latin1-input?
15our $opt_l = undef;
16our $opt_p = 5676;
Marc Kupietza2e64502016-04-27 09:53:51 +020017our $opt_m;
Marc Kupietz6ed81872016-04-27 14:04:04 +020018our $opt_M;
Marc Kupietz43ee87e2016-04-25 10:50:08 +020019our $opt_n = '';
20our $opt_d;
Marc Kupietz5c3887d2016-04-28 08:53:35 +020021our $opt_G;
Marc Kupietza5b90152016-03-15 17:39:19 +010022
Marc Kupietz6ed81872016-04-27 14:04:04 +020023my %marked;
Marc Kupietz793413b2016-04-02 21:48:57 +020024my $training_args="";
Marc Kupietza2e64502016-04-27 09:53:51 +020025my $mergedEnd=0;
Marc Kupietz15987412017-11-07 15:56:58 +010026my %cache;
Marc Kupietz793413b2016-04-02 21:48:57 +020027
Marc Kupietza5f60042017-05-04 10:38:12 +020028getopts('d:Gil:p:m:n:M:');
Marc Kupietz6ed81872016-04-27 14:04:04 +020029
30if($opt_M) {
Marc Kupietzed930212016-04-27 15:42:38 +020031 open my $handle, '<:encoding(UTF-8)', $opt_M
32 or die "Can't open '$opt_M' for reading: $!";
33 while(<$handle>) {
Marc Kupietz6ed81872016-04-27 14:04:04 +020034 foreach my $mw (split /\s+/) {
35 $marked{$mw}=1
36 }
37 }
Marc Kupietzed930212016-04-27 15:42:38 +020038 close($handle);
Marc Kupietz6ed81872016-04-27 14:04:04 +020039}
Marc Kupietza5b90152016-03-15 17:39:19 +010040
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010041# -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 +010042if(!$ARGV[0]) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010043 init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0));
Marc Kupietz2cb667e2016-03-10 09:44:12 +010044} else {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010045 init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0));
Marc Kupietz793413b2016-04-02 21:48:57 +020046 if(open(FILE, "$ARGV[0].args")) {
47 $training_args = <FILE>;
48 }
49 close(FILE);
Marc Kupietz2cb667e2016-03-10 09:44:12 +010050}
Marc Kupietzdc22b982015-10-09 09:19:34 +020051
Marc Kupietza2e64502016-04-27 09:53:51 +020052if($opt_m) {
53 $mergedEnd = mergeVectors($opt_m);
54}
55
Marc Kupietz6ed81872016-04-27 14:04:04 +020056
Marc Kupietz43ee87e2016-04-25 10:50:08 +020057if($opt_d) { # -d: dump vecs and exit
58 dump_vecs($opt_d);
59 exit;
60}
61
Marc Kupietza5b90152016-03-15 17:39:19 +010062my $daemon = Mojo::Server::Daemon->new(
63 app => app,
64 listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"]
65);
66
Marc Kupietz5c3887d2016-04-28 08:53:35 +020067if($opt_G) {
68 print "Filtering garbage\n";
69 filter_garbage();
70}
71
Marc Kupietzdc22b982015-10-09 09:19:34 +020072get '/' => sub {
73 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +020074 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +020075 my $word=$c->param('word');
Marc Kupietz44bee3c2016-02-25 16:26:29 +010076 my $no_nbs=$c->param('n') || 100;
77 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +010078 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +010079 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +010080 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +020081 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +010082 my $sort=$c->param('sort') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +020083 my $json=$c->param('json') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +010084 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010085 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +010086 my @collocations;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010087 if(defined($word) && $word !~ /^\s*$/) {
88 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +010089 $word =~ s/\s+/ /g;
90 for my $w (split(' *\| *', $word)) {
Marc Kupietz15987412017-11-07 15:56:58 +010091 if ($cache{$w}) {
92 $c->app->log->info("Getting $w results from cache");
93 $res = $cache{$w}
Marc Kupietza5b90152016-03-15 17:39:19 +010094 } else {
Marc Kupietz15987412017-11-07 15:56:58 +010095 $c->app->log->info('Looking for neighbours of '.$w);
96 if($opt_i) {
97 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst);
98 } else {
99 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst);
100 }
101 $cache{$w} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100102 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100103 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100104 }
105 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100106 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200107 if($json) {
108 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
109 } else {
110 $c->render(template=>"index", word=>$word, no_nbs=>$no_nbs, no_iterations => $no_iterations, epsilon=> $epsilon, perplexity=> $perplexity, show_som=>$som, searchBaseVocabFirst=>$searchBaseVocabFirst, sort=>$sort, training_args=>$training_args, mergedEnd=> $mergedEnd, marked=>\%marked, lists=> \@lists, collocators=> $res->{syntagmatic});
111 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200112};
113
Marc Kupietza5b90152016-03-15 17:39:19 +0100114$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200115
116exit;
117
118__END__
119
120__C__
121#include <stdio.h>
122#include <string.h>
123#include <math.h>
124#include <malloc.h>
125#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +0100126#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +0100127#include <pthread.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200128
129#define max_size 2000
130#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100131#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100132#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +0100133#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100134#define MAX_CC 50
135#define EXP_TABLE_SIZE 1000
136#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +0100137#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +0200138
139//the thread function
140void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +0100141
142typedef struct {
143 long long *index;
144 float *dist;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100145 float *norm;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100146 long long *pos;
Marc Kupietz80abb442016-03-23 21:04:08 +0100147 int length;
Marc Kupietz000ad862016-02-26 14:59:12 +0100148} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100149
Marc Kupietz000ad862016-02-26 14:59:12 +0100150typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +0100151 long long wordi[MAX_NEIGHBOURS];
152 char sep[MAX_NEIGHBOURS];
153 int length;
154} wordlist;
155
156typedef struct {
157 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100158 char *token;
159 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100160 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100161 unsigned long upto;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100162 float *target_sums;
Marc Kupietz000ad862016-02-26 14:59:12 +0100163} knnpars;
164
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200165float *M, *M2=0L, *syn1neg_window, *expTable;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200166char *vocab;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200167char *garbage = NULL;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100168
Marc Kupietza2e64502016-04-27 09:53:51 +0200169long long words, size, merged_end;
Marc Kupietza5f60042017-05-04 10:38:12 +0200170long long merge_words = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100171int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100172int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100173int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200174
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100175int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100176 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100177 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100178 long long a, b, c, d, cn;
179 float len;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200180 double val;
Marc Kupietz82b02672016-02-26 12:32:25 +0100181
Marc Kupietz67c20282016-02-26 09:42:00 +0100182 char binvecs_fname[256], binwords_fname[256];
183 strcpy(binwords_fname, file_name);
184 strcat(binwords_fname, ".words");
185 strcpy(binvecs_fname, file_name);
186 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200187
Marc Kupietza5b90152016-03-15 17:39:19 +0100188 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200189 f = fopen(file_name, "rb");
190 if (f == NULL) {
191 printf("Input file %s not found\n", file_name);
192 return -1;
193 }
194 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100195 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200196 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100197 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
198 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100199 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
200 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
201 if (M == NULL) {
202 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
203 return -1;
204 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200205 if(strstr(file_name, ".txt")) {
206 for (b = 0; b < words; b++) {
207 a = 0;
208 while (1) {
209 vocab[b * max_w + a] = fgetc(f);
210 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
211 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
212 }
213 vocab[b * max_w + a] = 0;
214 len = 0;
215 for (a = 0; a < size; a++) {
216 fscanf(f, "%lf", &val);
217 M[a + b * size] = val;
218 len += val * val;
219 }
220 len = sqrt(len);
221 for (a = 0; a < size; a++) M[a + b * size] /= len;
222 }
223 } else {
224 for (b = 0; b < words; b++) {
225 a = 0;
226 while (1) {
227 vocab[b * max_w + a] = fgetc(f);
228 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
229 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
230 }
231 vocab[b * max_w + a] = 0;
232 fread(&M[b * size], sizeof(float), size, f);
233 len = 0;
234 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
235 len = sqrt(len);
236 for (a = 0; a < size; a++) M[a + b * size] /= len;
237 }
238 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100239 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
240 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
241 fclose(binvecs);
242 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
243 fclose(binwords);
244 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100245 }
246 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
247 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
248 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
249 if (M == MAP_FAILED || vocab == MAP_FAILED) {
250 close(binvecs_fd);
251 close(binwords_fd);
252 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
253 exit(-1);
254 }
255 } else {
256 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
257 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100258 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200259 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100260
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200261 if(net_name && strlen(net_name) > 0) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100262 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
263 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
264 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100265 // munmap(M, sizeof(float) * words * size);
266 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 +0200267 if (M2 == MAP_FAILED) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100268 close(net_fd);
269 fprintf(stderr, "Cannot mmap %s\n", net_name);
270 exit(-1);
271 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100272 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100273 } else {
274 fprintf(stderr, "Cannot open %s\n", net_name);
275 exit(-1);
276 }
277 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
278 }
279
280 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
281 for (i = 0; i < EXP_TABLE_SIZE; i++) {
282 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
283 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
284 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200285 return 0;
286}
287
Marc Kupietza2e64502016-04-27 09:53:51 +0200288long mergeVectors(char *file_name){
289 FILE *f, *binvecs, *binwords;
290 int binwords_fd, binvecs_fd, net_fd, i;
291 long long a, b, c, d, cn;
292 float len;
293 float *merge_vecs;
294 char *merge_vocab;
Marc Kupietza5f60042017-05-04 10:38:12 +0200295 /* long long merge_words, merge_size; */
296 long long merge_size;
Marc Kupietza2e64502016-04-27 09:53:51 +0200297
298 char binvecs_fname[256], binwords_fname[256];
299 strcpy(binwords_fname, file_name);
300 strcat(binwords_fname, ".words");
301 strcpy(binvecs_fname, file_name);
302 strcat(binvecs_fname, ".vecs");
303
304 f = fopen(file_name, "rb");
305 if (f == NULL) {
306 printf("Input file %s not found\n", file_name);
307 exit -1;
308 }
309 fscanf(f, "%lld", &merge_words);
310 fscanf(f, "%lld", &merge_size);
311 if(merge_size != size){
312 fprintf(stderr, "vectors must have the same length\n");
313 exit(-1);
314 }
315 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
316 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
317 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
318 if (merge_vecs == NULL || merge_vocab == NULL) {
319 close(binvecs_fd);
320 close(binwords_fd);
321 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
322 exit(-1);
323 }
324 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
325 read(binwords_fd, merge_vocab, merge_words * max_w);
326 } else {
327 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
328 exit(-1);
329 }
330 printf("Successfully reallocated memory\nMerging...\n");
331 fflush(stdout);
332 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
333 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
334 munmap(M, words * size * sizeof(float));
335 munmap(vocab, words * max_w);
336 M = merge_vecs;
337 vocab = merge_vocab;
338 merged_end = merge_words;
339 words += merge_words;
340 fclose(f);
341 printf("merged_end: %lld, words: %lld\n", merged_end, words);
342 return((long) merged_end);
343}
344
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200345void filter_garbage() {
346 long i;
Marc Kupietzab591a82016-04-28 14:08:49 +0200347 unsigned char *w, previous, c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200348 garbage = malloc(words);
349 memset(garbage, 0, words);
350 for (i = 0; i < words; i++) {
351 w = vocab + i * max_w;
352 previous = 0;
Marc Kupietzab591a82016-04-28 14:08:49 +0200353 while((c = *w++) && !garbage[i]) {
354 if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
355 (previous == '-' && (c & 32)) ||
356 (previous == 0xc2 && (c == 0xa4 || c == 0xb6 ))
357 ) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200358 garbage[i]=1;
359 continue;
360 }
361 previous = c;
362 }
363 }
364 return;
365}
366
Marc Kupietz271e2a42016-03-22 11:37:43 +0100367void *getCollocators(knnpars *pars) {
368 int N = pars->N;
369 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100370 knn *nbs = NULL;
371 long window_layer_size = size * window * 2;
372 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
373 float f, max_f, maxmax_f;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100374 float *target_sums, *bestf, *bestn, worstbest, wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100375 long long *besti, *bestp;
Marc Kupietzd5642582016-03-19 22:23:13 +0100376
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200377 if(M2 == NULL || cc == -1)
Marc Kupietzd5642582016-03-19 22:23:13 +0100378 return NULL;
379
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100380 a = posix_memalign((void **) &target_sums, 128, words * sizeof(float));
381 besti = malloc(N * sizeof(long long));
382 bestp = malloc(N * sizeof(long long));
383 bestf = malloc(N * sizeof(float));
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100384 bestn = malloc(N * sizeof(float));
385
Marc Kupietz271e2a42016-03-22 11:37:43 +0100386 worstbest = MIN_RESP;
387
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100388 for (b = 0; b < words; b++)
389 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100390 for (b = 0; b < N; b++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100391 besti[b] = -1;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100392 bestn[b] = 1;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100393 bestf[b] = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100394 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100395
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100396 d = cc;
397 maxmax_f = -1;
398 maxmax_target = 0;
399
Marc Kupietz271e2a42016-03-22 11:37:43 +0100400 for (a = pars->from; a < pars->upto; a++) {
401 if(a >= window)
402 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100403 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100404 printf("window pos: %ld\n", a);
405 if (a != window) {
406 max_f = -1;
407 window_offset = a * size;
408 if (a > window)
409 window_offset -= size;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100410 for(target = 0; target < words; target ++) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100411 if(target == d)
412 continue;
413 f = 0;
414 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100415 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100416 if (f < -MAX_EXP)
417 continue;
418 else if (f > MAX_EXP)
419 continue;
420 else
421 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100422 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100423
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100424 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100425 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100426 for (b = 0; b < N; b++) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100427 if (f > bestf[b]) {
Marc Kupietz33679a32016-03-22 08:49:39 +0100428 memmove(bestf + b + 1, bestf + b, (N - b -1) * sizeof(float));
429 memmove(besti + b + 1, besti + b, (N - b -1) * sizeof(long long));
430 memmove(bestp + b + 1, bestp + b, (N - b -1) * sizeof(long long));
431 bestf[b] = f;
432 besti[b] = target;
433 bestp[b] = window-a;
434 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100435 }
436 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100437 if(b == N - 1)
438 worstbest = bestf[N-1];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100439 }
440 }
441 printf("%d %.2f\n", max_target, max_f);
442 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
443 if(max_f > maxmax_f) {
444 maxmax_f = max_f;
445 maxmax_target = max_target;
446 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100447 for (b = 0; b < N; b++)
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100448 if(bestp[b] == window-a)
449 bestn[b] = bestf[b] / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100450 } else {
451 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
452 }
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100453
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100454 }
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100455 for (b = 0; b < words; b++)
456 pars->target_sums[b] += (target_sums[b] / wpos_sum ) / (window * 2);
457 free(target_sums);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100458 for(b=0; b<N && besti[b] >= 0; b++) // THIS LOOP IS NEEDED (b...)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100459 printf("%s %.2f %d * ", &vocab[besti[b]*max_w], bestf[b], bestp[b]);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100460 printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100461 nbs = malloc(sizeof(knn));
462 nbs->index = besti;
463 nbs->dist = bestf;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100464 nbs->norm = bestn;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100465 nbs->pos = bestp;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100466 nbs->length = b-1;
467 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100468}
469
Marc Kupietza2e64502016-04-27 09:53:51 +0200470wordlist *getTargetWords(char *st1, int search_backw) {
Marc Kupietz48c29682016-03-19 11:30:43 +0100471 wordlist *wl = malloc(sizeof(wordlist));
472 char st[100][max_size], sep[100];
473 long a, b=0, c=0, cn=0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200474 int unmerged;
475
Marc Kupietzdc22b982015-10-09 09:19:34 +0200476 while (1) {
477 st[cn][b] = st1[c];
478 b++;
479 c++;
480 st[cn][b] = 0;
481 if (st1[c] == 0) break;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100482 if (st1[c] == ' ' || st1[c] == '-') {
483 sep[cn++] = st1[c];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200484 b = 0;
485 c++;
486 }
487 }
488 cn++;
489 for (a = 0; a < cn; a++) {
Marc Kupietza2e64502016-04-27 09:53:51 +0200490 if(search_backw) {
491 for (b = words - 1; b >= 0; b--) if (!strcmp(&vocab[b * max_w], st[a])) break;
492 } else {
493 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
494 }
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100495 if (b == words) b = -1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100496 wl->wordi[a] = b;
497 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], wl->wordi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200498 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100499 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100500 cn--;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200501 break;
502 }
503 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100504 wl->length=cn;
505 return(wl);
506}
507
508void *_get_neighbours(knnpars *pars) {
509 char *st1 = pars->token;
510 int N = pars->N;
511 long from = pars -> from;
512 unsigned long upto = pars -> upto;
513 char file_name[max_size], st[100][max_size], *sep;
514 float dist, len, *bestd, vec[max_size];
515 long long a, b, c, d, cn, *bi, *besti;
516 char ch;
517 knn *nbs = NULL;
518 wordlist *wl = pars->wl;
519
520 besti = malloc(N * sizeof(long long));
521 bestd = malloc(N * sizeof(float));
522
523 float worstbest=-1;
524
525 for (a = 0; a < N; a++) bestd[a] = 0;
526 a = 0;
527 bi = wl->wordi;
528 cn = wl->length;
529 sep = wl->sep;
530 b = bi[0];
531 c = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100532 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100533 N = 0;
534 goto end;
535 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200536 for (a = 0; a < size; a++) vec[a] = 0;
537 for (b = 0; b < cn; b++) {
538 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100539 if(b>0 && sep[b-1] == '-')
540 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
541 else
542 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200543 }
544 len = 0;
545 for (a = 0; a < size; a++) len += vec[a] * vec[a];
546 len = sqrt(len);
547 for (a = 0; a < size; a++) vec[a] /= len;
548 for (a = 0; a < N; a++) bestd[a] = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100549 for (c = from; c < upto; c++) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200550 if(garbage && garbage[c]) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200551 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100552// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100553// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
554// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200555 dist = 0;
556 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100557 if(dist > worstbest) {
558 for (a = 0; a < N; a++) {
559 if (dist > bestd[a]) {
Marc Kupietz33679a32016-03-22 08:49:39 +0100560 memmove(bestd + a + 1, bestd + a, (N - a -1) * sizeof(float));
561 memmove(besti + a + 1, besti + a, (N - a -1) * sizeof(long long));
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100562 bestd[a] = dist;
563 besti[a] = c;
564 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200565 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200566 }
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100567 worstbest = bestd[N-1];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200568 }
569 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100570
Marc Kupietz000ad862016-02-26 14:59:12 +0100571 nbs = malloc(sizeof(knn));
572 nbs->index = besti;
573 nbs->dist = bestd;
574 nbs->length = N;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100575end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100576 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200577}
578
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100579
Marc Kupietza2e64502016-04-27 09:53:51 +0200580SV *get_neighbours(char *st1, int N, int sort_by, int search_backw) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100581 HV *result = newHV();
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100582 float *target_sums, bestd[MAX_NEIGHBOURS], bestn[MAX_NEIGHBOURS], bests[MAX_NEIGHBOURS], vec[max_size];
Marc Kupietza5f60042017-05-04 10:38:12 +0200583 long long old_words;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100584 long besti[MAX_NEIGHBOURS], bestp[MAX_NEIGHBOURS], a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100585 knn *para_nbs[MAX_THREADS];
586 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100587 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100588 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100589 wordlist *wl;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200590 int syn_threads = (M2? window * 2 : 0);
591 int para_threads = num_threads - syn_threads;
Marc Kupietz48c29682016-03-19 11:30:43 +0100592
Marc Kupietz000ad862016-02-26 14:59:12 +0100593 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
594
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200595
Marc Kupietza2e64502016-04-27 09:53:51 +0200596 wl = getTargetWords(st1, search_backw);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100597 if(wl->length < 1)
598 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100599
Marc Kupietza5f60042017-05-04 10:38:12 +0200600 old_words = words;
601 if(merge_words > 0)
602 words = merge_words * 1.25; /* HACK */
603 slice = words / para_threads;
604
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100605 a = posix_memalign((void **) &target_sums, 128, words * sizeof(float));
606 for(a = 0; a < words; a++)
607 target_sums[a] = 0;
608
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200609 printf("Starting %d threads\n", para_threads);
610 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100611 for(a=0; a < para_threads; a++) {
Marc Kupietz000ad862016-02-26 14:59:12 +0100612 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100613 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100614 pars[a].N = N;
615 pars[a].from = a*slice;
616 pars[a].upto = ((a+1)*slice > words? words:(a+1)*slice);
617 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
618 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200619 if(M2) {
620 for(a=0; a < syn_threads; a++) {
621 pars[a + para_threads].target_sums = target_sums;
622 pars[a + para_threads].wl = wl;
623 pars[a + para_threads].N = N;
624 pars[a + para_threads].from = a;
625 pars[a + para_threads].upto = a+1;
626 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
627 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100628 }
629 printf("Waiting for para threads to join\n");
630 fflush(stdout);
631 for (a = 0; a < para_threads; a++) pthread_join(pt[a], &para_nbs[a]);
632 printf("Para threads joint\n");
633 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +0100634
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200635 /* if(!syn_nbs[0]) */
636 /* goto end; */
Marc Kupietz000ad862016-02-26 14:59:12 +0100637
638 for(b=0; b < N; b++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100639 besti[b] = para_nbs[0]->index[b];
640 bestd[b] = para_nbs[0]->dist[b];
Marc Kupietz000ad862016-02-26 14:59:12 +0100641 }
642
Marc Kupietz271e2a42016-03-22 11:37:43 +0100643 for(a=1; a < para_threads; a++) {
644 for(b=0; b < para_nbs[a]->length && para_nbs[a]->index[b] >= 0; b++) {
Marc Kupietza5f60042017-05-04 10:38:12 +0200645 for(c=0; c < N * para_threads; c++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100646 if(para_nbs[a]->dist[b] > bestd[c]) {
Marc Kupietz000ad862016-02-26 14:59:12 +0100647 for(d=N-1; d>c; d--) {
648 bestd[d] = bestd[d-1];
649 besti[d] = besti[d-1];
650 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100651 besti[c] = para_nbs[a]->index[b];
652 bestd[c] = para_nbs[a]->dist[b];
Marc Kupietz000ad862016-02-26 14:59:12 +0100653 break;
654 }
655 }
656 }
657 }
658
Marc Kupietz271e2a42016-03-22 11:37:43 +0100659 AV* array = newAV();
Marc Kupietza5f60042017-05-04 10:38:12 +0200660 int i;
661 int l1_words=0, l2_words=0;
662 for (a = 0, i = 0; i < N && a < 600; a++) {
663 long long c = besti[a];
664 if(merge_words > 0) {
665 if(c >= merge_words) {
666 if(l1_words > N / 2)
667 continue;
668 else
669 l1_words++;
670 } else {
671 if(l2_words > N / 2)
672 continue;
673 else
674 l2_words++;
675 }
676 }
677 fflush(stdout);
678 printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
679
Marc Kupietz271e2a42016-03-22 11:37:43 +0100680 HV* hash = newHV();
Marc Kupietza5f60042017-05-04 10:38:12 +0200681 SV* word = newSVpvf(&vocab[c * max_w], 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100682 if(latin_enc == 0) SvUTF8_on(word);
Marc Kupietza5f60042017-05-04 10:38:12 +0200683 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100684 hv_store(hash, "word", strlen("word"), word , 0);
685 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
686 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
687 AV *vector = newAV();
688 for (b = 0; b < size; b++) {
689 av_push(vector, newSVnv(M[b + besti[a] * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100690 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100691 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
692 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietza5f60042017-05-04 10:38:12 +0200693 i++;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100694 }
695 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
696
Marc Kupietz50485ba2016-03-23 09:13:14 +0100697 for(b=0; b < MAX_NEIGHBOURS; b++) {
698 besti[b] = -1L;
699 bestd[b] = 0;
700 bestn[b] = 0;
701 bestp[b] = 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100702 bests[b] = 0;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100703 }
704
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200705 if (M2) {
706 printf("Waiting for syn threads to join\n");
707 fflush(stdout);
708 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], &syn_nbs[a]);
709 printf("syn threads joint\n");
710 fflush(stdout);
Marc Kupietz50485ba2016-03-23 09:13:14 +0100711
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200712 for(b=0; b < syn_nbs[0]->length; b++) {
713 besti[b] = syn_nbs[0]->index[b];
714 bestd[b] = syn_nbs[0]->dist[b];
715 bestn[b] = syn_nbs[0]->norm[b];
716 bestp[b] = syn_nbs[0]->pos[b];
717 bests[b] = target_sums[syn_nbs[0]->index[b]];
718 }
719
720 if(sort_by != 1) { // sort by responsiveness
721 for(a=1; a < syn_threads; a++) {
722 for(b=0; b < syn_nbs[a]->length; b++) {
723 for(c=0; c < MAX_NEIGHBOURS; c++) {
724 if(syn_nbs[a]->dist[b] > bestd[c]) {
725 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
726 bestd[d] = bestd[d-1];
727 besti[d] = besti[d-1];
728 bestn[d] = bestn[d-1];
729 bestp[d] = bestp[d-1];
730 }
731 besti[c] = syn_nbs[a]->index[b];
732 bestd[c] = syn_nbs[a]->dist[b];
733 bestn[c] = syn_nbs[a]->norm[b];
734 bestp[c] = syn_nbs[a]->pos[b];
735 break;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100736 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200737 }
738 }
739 }
740 } else { // sort by mean p
741 for(a=1; a < syn_threads; a++) {
742 for(b=0; b < syn_nbs[a]->length; b++) {
743 for(c=0; c < MAX_NEIGHBOURS; c++) {
744 if(target_sums[syn_nbs[a]->index[b]] > bests[c]) {
745 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
746 bestd[d] = bestd[d-1];
747 besti[d] = besti[d-1];
748 bestn[d] = bestn[d-1];
749 bestp[d] = bestp[d-1];
750 bests[d] = bests[d-1];
751 }
752 besti[c] = syn_nbs[a]->index[b];
753 bestd[c] = syn_nbs[a]->dist[b];
754 bestn[c] = syn_nbs[a]->norm[b];
755 bestp[c] = syn_nbs[a]->pos[b];
756 bests[c] = target_sums[syn_nbs[a]->index[b]];
757 break;
758 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100759 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100760 }
761 }
762 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200763 array = newAV();
764 for (a = 0; a < MAX_NEIGHBOURS && besti[a] >= 0; a++) {
765 HV* hash = newHV();
766 SV* word = newSVpvf(&vocab[besti[a] * max_w], 0);
767 if(latin_enc == 0) SvUTF8_on(word);
768 hv_store(hash, "word", strlen("word"), word , 0);
769 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
770 hv_store(hash, "norm", strlen("norm"), newSVnv(bestn[a]), 0);
771 hv_store(hash, "sum", strlen("sum"), newSVnv(target_sums[besti[a]]), 0);
772 hv_store(hash, "pos", strlen("pos"), newSVnv(bestp[a]), 0);
773 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietz271e2a42016-03-22 11:37:43 +0100774 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200775 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100776 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100777end:
Marc Kupietza5f60042017-05-04 10:38:12 +0200778 words = old_words;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100779 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +0100780}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100781
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200782int dump_vecs(char *fname) {
783 long i, j;
784 FILE *f;
785 /* if(words>200000) */
786 /* words=200000; */
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100787
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200788 if((f=fopen(fname, "w")) == NULL) {
789 fprintf(stderr, "cannot open %s for writing\n", fname);
790 return(-1);
791 }
792 fprintf(f, "%lld %lld\n", words, size);
793 for (i=0; i < words; i++) {
794 fprintf(f, "%s ", &vocab[i * max_w]);
795 for(j=0; j < size - 1; j++)
796 fprintf(f, "%f ", M[i*size + j]);
797 fprintf(f, "%f\n", M[i*size + j]);
798 }
799 fclose(f);
800 return(0);
801}
Marc Kupietzdc22b982015-10-09 09:19:34 +0200802