blob: a71ee6ea59f6b24c49ba8f899462624f64853805 [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 Kupietz793413b2016-04-02 21:48:57 +020026
Marc Kupietza5f60042017-05-04 10:38:12 +020027getopts('d:Gil:p:m:n:M:');
Marc Kupietz6ed81872016-04-27 14:04:04 +020028
29if($opt_M) {
Marc Kupietzed930212016-04-27 15:42:38 +020030 open my $handle, '<:encoding(UTF-8)', $opt_M
31 or die "Can't open '$opt_M' for reading: $!";
32 while(<$handle>) {
Marc Kupietz6ed81872016-04-27 14:04:04 +020033 foreach my $mw (split /\s+/) {
34 $marked{$mw}=1
35 }
36 }
Marc Kupietzed930212016-04-27 15:42:38 +020037 close($handle);
Marc Kupietz6ed81872016-04-27 14:04:04 +020038}
Marc Kupietza5b90152016-03-15 17:39:19 +010039
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010040# -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 +010041if(!$ARGV[0]) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010042 init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0));
Marc Kupietz2cb667e2016-03-10 09:44:12 +010043} else {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010044 init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0));
Marc Kupietz793413b2016-04-02 21:48:57 +020045 if(open(FILE, "$ARGV[0].args")) {
46 $training_args = <FILE>;
47 }
48 close(FILE);
Marc Kupietz2cb667e2016-03-10 09:44:12 +010049}
Marc Kupietzdc22b982015-10-09 09:19:34 +020050
Marc Kupietza2e64502016-04-27 09:53:51 +020051if($opt_m) {
52 $mergedEnd = mergeVectors($opt_m);
53}
54
Marc Kupietz6ed81872016-04-27 14:04:04 +020055
Marc Kupietz43ee87e2016-04-25 10:50:08 +020056if($opt_d) { # -d: dump vecs and exit
57 dump_vecs($opt_d);
58 exit;
59}
60
Marc Kupietza5b90152016-03-15 17:39:19 +010061my $daemon = Mojo::Server::Daemon->new(
62 app => app,
63 listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"]
64);
65
Marc Kupietz5c3887d2016-04-28 08:53:35 +020066if($opt_G) {
67 print "Filtering garbage\n";
68 filter_garbage();
69}
70
Marc Kupietzdc22b982015-10-09 09:19:34 +020071get '/' => sub {
72 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +020073 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +020074 my $word=$c->param('word');
Marc Kupietz44bee3c2016-02-25 16:26:29 +010075 my $no_nbs=$c->param('n') || 100;
76 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +010077 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +010078 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +010079 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +020080 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +010081 my $sort=$c->param('sort') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +020082 my $json=$c->param('json') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +010083 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010084 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +010085 my @collocations;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010086 if(defined($word) && $word !~ /^\s*$/) {
87 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +010088 $word =~ s/\s+/ /g;
89 for my $w (split(' *\| *', $word)) {
Marc Kupietza5f60042017-05-04 10:38:12 +020090 $c->app->log->info('Looking for neighbours of '.$w);
Marc Kupietza5b90152016-03-15 17:39:19 +010091 if($opt_i) {
Marc Kupietza2e64502016-04-27 09:53:51 +020092 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst);
Marc Kupietza5b90152016-03-15 17:39:19 +010093 } else {
Marc Kupietza2e64502016-04-27 09:53:51 +020094 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst);
Marc Kupietza5b90152016-03-15 17:39:19 +010095 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +010096 push(@lists, $res->{paradigmatic});
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010097 }
Marc Kupietz247500f2015-10-09 11:29:01 +020098 }
Marc Kupietz000ad862016-02-26 14:59:12 +010099 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200100 if($json) {
101 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
102 } else {
103 $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});
104 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200105};
106
Marc Kupietza5b90152016-03-15 17:39:19 +0100107$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200108
109exit;
110
111__END__
112
113__C__
114#include <stdio.h>
115#include <string.h>
116#include <math.h>
117#include <malloc.h>
118#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +0100119#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +0100120#include <pthread.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200121
122#define max_size 2000
123#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100124#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100125#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +0100126#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100127#define MAX_CC 50
128#define EXP_TABLE_SIZE 1000
129#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +0100130#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +0200131
132//the thread function
133void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +0100134
135typedef struct {
136 long long *index;
137 float *dist;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100138 float *norm;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100139 long long *pos;
Marc Kupietz80abb442016-03-23 21:04:08 +0100140 int length;
Marc Kupietz000ad862016-02-26 14:59:12 +0100141} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100142
Marc Kupietz000ad862016-02-26 14:59:12 +0100143typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +0100144 long long wordi[MAX_NEIGHBOURS];
145 char sep[MAX_NEIGHBOURS];
146 int length;
147} wordlist;
148
149typedef struct {
150 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100151 char *token;
152 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100153 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100154 unsigned long upto;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100155 float *target_sums;
Marc Kupietz000ad862016-02-26 14:59:12 +0100156} knnpars;
157
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200158float *M, *M2=0L, *syn1neg_window, *expTable;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200159char *vocab;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200160char *garbage = NULL;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100161
Marc Kupietza2e64502016-04-27 09:53:51 +0200162long long words, size, merged_end;
Marc Kupietza5f60042017-05-04 10:38:12 +0200163long long merge_words = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100164int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100165int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100166int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200167
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100168int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100169 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100170 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100171 long long a, b, c, d, cn;
172 float len;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200173 double val;
Marc Kupietz82b02672016-02-26 12:32:25 +0100174
Marc Kupietz67c20282016-02-26 09:42:00 +0100175 char binvecs_fname[256], binwords_fname[256];
176 strcpy(binwords_fname, file_name);
177 strcat(binwords_fname, ".words");
178 strcpy(binvecs_fname, file_name);
179 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200180
Marc Kupietza5b90152016-03-15 17:39:19 +0100181 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200182 f = fopen(file_name, "rb");
183 if (f == NULL) {
184 printf("Input file %s not found\n", file_name);
185 return -1;
186 }
187 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100188 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200189 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100190 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
191 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100192 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
193 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
194 if (M == NULL) {
195 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
196 return -1;
197 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200198 if(strstr(file_name, ".txt")) {
199 for (b = 0; b < words; b++) {
200 a = 0;
201 while (1) {
202 vocab[b * max_w + a] = fgetc(f);
203 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
204 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
205 }
206 vocab[b * max_w + a] = 0;
207 len = 0;
208 for (a = 0; a < size; a++) {
209 fscanf(f, "%lf", &val);
210 M[a + b * size] = val;
211 len += val * val;
212 }
213 len = sqrt(len);
214 for (a = 0; a < size; a++) M[a + b * size] /= len;
215 }
216 } else {
217 for (b = 0; b < words; b++) {
218 a = 0;
219 while (1) {
220 vocab[b * max_w + a] = fgetc(f);
221 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
222 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
223 }
224 vocab[b * max_w + a] = 0;
225 fread(&M[b * size], sizeof(float), size, f);
226 len = 0;
227 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
228 len = sqrt(len);
229 for (a = 0; a < size; a++) M[a + b * size] /= len;
230 }
231 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100232 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
233 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
234 fclose(binvecs);
235 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
236 fclose(binwords);
237 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100238 }
239 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
240 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
241 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
242 if (M == MAP_FAILED || vocab == MAP_FAILED) {
243 close(binvecs_fd);
244 close(binwords_fd);
245 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
246 exit(-1);
247 }
248 } else {
249 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
250 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100251 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200252 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100253
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200254 if(net_name && strlen(net_name) > 0) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100255 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
256 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
257 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100258 // munmap(M, sizeof(float) * words * size);
259 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 +0200260 if (M2 == MAP_FAILED) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100261 close(net_fd);
262 fprintf(stderr, "Cannot mmap %s\n", net_name);
263 exit(-1);
264 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100265 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100266 } else {
267 fprintf(stderr, "Cannot open %s\n", net_name);
268 exit(-1);
269 }
270 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
271 }
272
273 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
274 for (i = 0; i < EXP_TABLE_SIZE; i++) {
275 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
276 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
277 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200278 return 0;
279}
280
Marc Kupietza2e64502016-04-27 09:53:51 +0200281long mergeVectors(char *file_name){
282 FILE *f, *binvecs, *binwords;
283 int binwords_fd, binvecs_fd, net_fd, i;
284 long long a, b, c, d, cn;
285 float len;
286 float *merge_vecs;
287 char *merge_vocab;
Marc Kupietza5f60042017-05-04 10:38:12 +0200288 /* long long merge_words, merge_size; */
289 long long merge_size;
Marc Kupietza2e64502016-04-27 09:53:51 +0200290
291 char binvecs_fname[256], binwords_fname[256];
292 strcpy(binwords_fname, file_name);
293 strcat(binwords_fname, ".words");
294 strcpy(binvecs_fname, file_name);
295 strcat(binvecs_fname, ".vecs");
296
297 f = fopen(file_name, "rb");
298 if (f == NULL) {
299 printf("Input file %s not found\n", file_name);
300 exit -1;
301 }
302 fscanf(f, "%lld", &merge_words);
303 fscanf(f, "%lld", &merge_size);
304 if(merge_size != size){
305 fprintf(stderr, "vectors must have the same length\n");
306 exit(-1);
307 }
308 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
309 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
310 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
311 if (merge_vecs == NULL || merge_vocab == NULL) {
312 close(binvecs_fd);
313 close(binwords_fd);
314 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
315 exit(-1);
316 }
317 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
318 read(binwords_fd, merge_vocab, merge_words * max_w);
319 } else {
320 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
321 exit(-1);
322 }
323 printf("Successfully reallocated memory\nMerging...\n");
324 fflush(stdout);
325 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
326 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
327 munmap(M, words * size * sizeof(float));
328 munmap(vocab, words * max_w);
329 M = merge_vecs;
330 vocab = merge_vocab;
331 merged_end = merge_words;
332 words += merge_words;
333 fclose(f);
334 printf("merged_end: %lld, words: %lld\n", merged_end, words);
335 return((long) merged_end);
336}
337
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200338void filter_garbage() {
339 long i;
Marc Kupietzab591a82016-04-28 14:08:49 +0200340 unsigned char *w, previous, c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200341 garbage = malloc(words);
342 memset(garbage, 0, words);
343 for (i = 0; i < words; i++) {
344 w = vocab + i * max_w;
345 previous = 0;
Marc Kupietzab591a82016-04-28 14:08:49 +0200346 while((c = *w++) && !garbage[i]) {
347 if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
348 (previous == '-' && (c & 32)) ||
349 (previous == 0xc2 && (c == 0xa4 || c == 0xb6 ))
350 ) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200351 garbage[i]=1;
352 continue;
353 }
354 previous = c;
355 }
356 }
357 return;
358}
359
Marc Kupietz271e2a42016-03-22 11:37:43 +0100360void *getCollocators(knnpars *pars) {
361 int N = pars->N;
362 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100363 knn *nbs = NULL;
364 long window_layer_size = size * window * 2;
365 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
366 float f, max_f, maxmax_f;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100367 float *target_sums, *bestf, *bestn, worstbest, wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100368 long long *besti, *bestp;
Marc Kupietzd5642582016-03-19 22:23:13 +0100369
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200370 if(M2 == NULL || cc == -1)
Marc Kupietzd5642582016-03-19 22:23:13 +0100371 return NULL;
372
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100373 a = posix_memalign((void **) &target_sums, 128, words * sizeof(float));
374 besti = malloc(N * sizeof(long long));
375 bestp = malloc(N * sizeof(long long));
376 bestf = malloc(N * sizeof(float));
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100377 bestn = malloc(N * sizeof(float));
378
Marc Kupietz271e2a42016-03-22 11:37:43 +0100379 worstbest = MIN_RESP;
380
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100381 for (b = 0; b < words; b++)
382 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100383 for (b = 0; b < N; b++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100384 besti[b] = -1;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100385 bestn[b] = 1;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100386 bestf[b] = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100387 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100388
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100389 d = cc;
390 maxmax_f = -1;
391 maxmax_target = 0;
392
Marc Kupietz271e2a42016-03-22 11:37:43 +0100393 for (a = pars->from; a < pars->upto; a++) {
394 if(a >= window)
395 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100396 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100397 printf("window pos: %ld\n", a);
398 if (a != window) {
399 max_f = -1;
400 window_offset = a * size;
401 if (a > window)
402 window_offset -= size;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100403 for(target = 0; target < words; target ++) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100404 if(target == d)
405 continue;
406 f = 0;
407 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100408 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100409 if (f < -MAX_EXP)
410 continue;
411 else if (f > MAX_EXP)
412 continue;
413 else
414 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100415 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100416
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100417 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100418 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100419 for (b = 0; b < N; b++) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100420 if (f > bestf[b]) {
Marc Kupietz33679a32016-03-22 08:49:39 +0100421 memmove(bestf + b + 1, bestf + b, (N - b -1) * sizeof(float));
422 memmove(besti + b + 1, besti + b, (N - b -1) * sizeof(long long));
423 memmove(bestp + b + 1, bestp + b, (N - b -1) * sizeof(long long));
424 bestf[b] = f;
425 besti[b] = target;
426 bestp[b] = window-a;
427 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100428 }
429 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100430 if(b == N - 1)
431 worstbest = bestf[N-1];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100432 }
433 }
434 printf("%d %.2f\n", max_target, max_f);
435 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
436 if(max_f > maxmax_f) {
437 maxmax_f = max_f;
438 maxmax_target = max_target;
439 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100440 for (b = 0; b < N; b++)
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100441 if(bestp[b] == window-a)
442 bestn[b] = bestf[b] / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100443 } else {
444 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
445 }
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100446
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100447 }
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100448 for (b = 0; b < words; b++)
449 pars->target_sums[b] += (target_sums[b] / wpos_sum ) / (window * 2);
450 free(target_sums);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100451 for(b=0; b<N && besti[b] >= 0; b++) // THIS LOOP IS NEEDED (b...)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100452 printf("%s %.2f %d * ", &vocab[besti[b]*max_w], bestf[b], bestp[b]);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100453 printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100454 nbs = malloc(sizeof(knn));
455 nbs->index = besti;
456 nbs->dist = bestf;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100457 nbs->norm = bestn;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100458 nbs->pos = bestp;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100459 nbs->length = b-1;
460 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100461}
462
Marc Kupietza2e64502016-04-27 09:53:51 +0200463wordlist *getTargetWords(char *st1, int search_backw) {
Marc Kupietz48c29682016-03-19 11:30:43 +0100464 wordlist *wl = malloc(sizeof(wordlist));
465 char st[100][max_size], sep[100];
466 long a, b=0, c=0, cn=0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200467 int unmerged;
468
Marc Kupietzdc22b982015-10-09 09:19:34 +0200469 while (1) {
470 st[cn][b] = st1[c];
471 b++;
472 c++;
473 st[cn][b] = 0;
474 if (st1[c] == 0) break;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100475 if (st1[c] == ' ' || st1[c] == '-') {
476 sep[cn++] = st1[c];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200477 b = 0;
478 c++;
479 }
480 }
481 cn++;
482 for (a = 0; a < cn; a++) {
Marc Kupietza2e64502016-04-27 09:53:51 +0200483 if(search_backw) {
484 for (b = words - 1; b >= 0; b--) if (!strcmp(&vocab[b * max_w], st[a])) break;
485 } else {
486 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
487 }
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100488 if (b == words) b = -1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100489 wl->wordi[a] = b;
490 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], wl->wordi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200491 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100492 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100493 cn--;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200494 break;
495 }
496 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100497 wl->length=cn;
498 return(wl);
499}
500
501void *_get_neighbours(knnpars *pars) {
502 char *st1 = pars->token;
503 int N = pars->N;
504 long from = pars -> from;
505 unsigned long upto = pars -> upto;
506 char file_name[max_size], st[100][max_size], *sep;
507 float dist, len, *bestd, vec[max_size];
508 long long a, b, c, d, cn, *bi, *besti;
509 char ch;
510 knn *nbs = NULL;
511 wordlist *wl = pars->wl;
512
513 besti = malloc(N * sizeof(long long));
514 bestd = malloc(N * sizeof(float));
515
516 float worstbest=-1;
517
518 for (a = 0; a < N; a++) bestd[a] = 0;
519 a = 0;
520 bi = wl->wordi;
521 cn = wl->length;
522 sep = wl->sep;
523 b = bi[0];
524 c = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100525 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100526 N = 0;
527 goto end;
528 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200529 for (a = 0; a < size; a++) vec[a] = 0;
530 for (b = 0; b < cn; b++) {
531 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100532 if(b>0 && sep[b-1] == '-')
533 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
534 else
535 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200536 }
537 len = 0;
538 for (a = 0; a < size; a++) len += vec[a] * vec[a];
539 len = sqrt(len);
540 for (a = 0; a < size; a++) vec[a] /= len;
541 for (a = 0; a < N; a++) bestd[a] = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100542 for (c = from; c < upto; c++) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200543 if(garbage && garbage[c]) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200544 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100545// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100546// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
547// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200548 dist = 0;
549 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100550 if(dist > worstbest) {
551 for (a = 0; a < N; a++) {
552 if (dist > bestd[a]) {
Marc Kupietz33679a32016-03-22 08:49:39 +0100553 memmove(bestd + a + 1, bestd + a, (N - a -1) * sizeof(float));
554 memmove(besti + a + 1, besti + a, (N - a -1) * sizeof(long long));
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100555 bestd[a] = dist;
556 besti[a] = c;
557 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200558 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200559 }
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100560 worstbest = bestd[N-1];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200561 }
562 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100563
Marc Kupietz000ad862016-02-26 14:59:12 +0100564 nbs = malloc(sizeof(knn));
565 nbs->index = besti;
566 nbs->dist = bestd;
567 nbs->length = N;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100568end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100569 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200570}
571
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100572
Marc Kupietza2e64502016-04-27 09:53:51 +0200573SV *get_neighbours(char *st1, int N, int sort_by, int search_backw) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100574 HV *result = newHV();
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100575 float *target_sums, bestd[MAX_NEIGHBOURS], bestn[MAX_NEIGHBOURS], bests[MAX_NEIGHBOURS], vec[max_size];
Marc Kupietza5f60042017-05-04 10:38:12 +0200576 long long old_words;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100577 long besti[MAX_NEIGHBOURS], bestp[MAX_NEIGHBOURS], a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100578 knn *para_nbs[MAX_THREADS];
579 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100580 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100581 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100582 wordlist *wl;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200583 int syn_threads = (M2? window * 2 : 0);
584 int para_threads = num_threads - syn_threads;
Marc Kupietz48c29682016-03-19 11:30:43 +0100585
Marc Kupietz000ad862016-02-26 14:59:12 +0100586 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
587
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200588
Marc Kupietza2e64502016-04-27 09:53:51 +0200589 wl = getTargetWords(st1, search_backw);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100590 if(wl->length < 1)
591 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100592
Marc Kupietza5f60042017-05-04 10:38:12 +0200593 old_words = words;
594 if(merge_words > 0)
595 words = merge_words * 1.25; /* HACK */
596 slice = words / para_threads;
597
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100598 a = posix_memalign((void **) &target_sums, 128, words * sizeof(float));
599 for(a = 0; a < words; a++)
600 target_sums[a] = 0;
601
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200602 printf("Starting %d threads\n", para_threads);
603 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100604 for(a=0; a < para_threads; a++) {
Marc Kupietz000ad862016-02-26 14:59:12 +0100605 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100606 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100607 pars[a].N = N;
608 pars[a].from = a*slice;
609 pars[a].upto = ((a+1)*slice > words? words:(a+1)*slice);
610 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
611 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200612 if(M2) {
613 for(a=0; a < syn_threads; a++) {
614 pars[a + para_threads].target_sums = target_sums;
615 pars[a + para_threads].wl = wl;
616 pars[a + para_threads].N = N;
617 pars[a + para_threads].from = a;
618 pars[a + para_threads].upto = a+1;
619 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
620 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100621 }
622 printf("Waiting for para threads to join\n");
623 fflush(stdout);
624 for (a = 0; a < para_threads; a++) pthread_join(pt[a], &para_nbs[a]);
625 printf("Para threads joint\n");
626 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +0100627
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200628 /* if(!syn_nbs[0]) */
629 /* goto end; */
Marc Kupietz000ad862016-02-26 14:59:12 +0100630
631 for(b=0; b < N; b++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100632 besti[b] = para_nbs[0]->index[b];
633 bestd[b] = para_nbs[0]->dist[b];
Marc Kupietz000ad862016-02-26 14:59:12 +0100634 }
635
Marc Kupietz271e2a42016-03-22 11:37:43 +0100636 for(a=1; a < para_threads; a++) {
637 for(b=0; b < para_nbs[a]->length && para_nbs[a]->index[b] >= 0; b++) {
Marc Kupietza5f60042017-05-04 10:38:12 +0200638 for(c=0; c < N * para_threads; c++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100639 if(para_nbs[a]->dist[b] > bestd[c]) {
Marc Kupietz000ad862016-02-26 14:59:12 +0100640 for(d=N-1; d>c; d--) {
641 bestd[d] = bestd[d-1];
642 besti[d] = besti[d-1];
643 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100644 besti[c] = para_nbs[a]->index[b];
645 bestd[c] = para_nbs[a]->dist[b];
Marc Kupietz000ad862016-02-26 14:59:12 +0100646 break;
647 }
648 }
649 }
650 }
651
Marc Kupietz271e2a42016-03-22 11:37:43 +0100652 AV* array = newAV();
Marc Kupietza5f60042017-05-04 10:38:12 +0200653 int i;
654 int l1_words=0, l2_words=0;
655 for (a = 0, i = 0; i < N && a < 600; a++) {
656 long long c = besti[a];
657 if(merge_words > 0) {
658 if(c >= merge_words) {
659 if(l1_words > N / 2)
660 continue;
661 else
662 l1_words++;
663 } else {
664 if(l2_words > N / 2)
665 continue;
666 else
667 l2_words++;
668 }
669 }
670 fflush(stdout);
671 printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
672
Marc Kupietz271e2a42016-03-22 11:37:43 +0100673 HV* hash = newHV();
Marc Kupietza5f60042017-05-04 10:38:12 +0200674 SV* word = newSVpvf(&vocab[c * max_w], 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100675 if(latin_enc == 0) SvUTF8_on(word);
Marc Kupietza5f60042017-05-04 10:38:12 +0200676 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100677 hv_store(hash, "word", strlen("word"), word , 0);
678 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
679 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
680 AV *vector = newAV();
681 for (b = 0; b < size; b++) {
682 av_push(vector, newSVnv(M[b + besti[a] * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100683 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100684 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
685 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietza5f60042017-05-04 10:38:12 +0200686 i++;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100687 }
688 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
689
Marc Kupietz50485ba2016-03-23 09:13:14 +0100690 for(b=0; b < MAX_NEIGHBOURS; b++) {
691 besti[b] = -1L;
692 bestd[b] = 0;
693 bestn[b] = 0;
694 bestp[b] = 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100695 bests[b] = 0;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100696 }
697
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200698 if (M2) {
699 printf("Waiting for syn threads to join\n");
700 fflush(stdout);
701 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], &syn_nbs[a]);
702 printf("syn threads joint\n");
703 fflush(stdout);
Marc Kupietz50485ba2016-03-23 09:13:14 +0100704
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200705 for(b=0; b < syn_nbs[0]->length; b++) {
706 besti[b] = syn_nbs[0]->index[b];
707 bestd[b] = syn_nbs[0]->dist[b];
708 bestn[b] = syn_nbs[0]->norm[b];
709 bestp[b] = syn_nbs[0]->pos[b];
710 bests[b] = target_sums[syn_nbs[0]->index[b]];
711 }
712
713 if(sort_by != 1) { // sort by responsiveness
714 for(a=1; a < syn_threads; a++) {
715 for(b=0; b < syn_nbs[a]->length; b++) {
716 for(c=0; c < MAX_NEIGHBOURS; c++) {
717 if(syn_nbs[a]->dist[b] > bestd[c]) {
718 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
719 bestd[d] = bestd[d-1];
720 besti[d] = besti[d-1];
721 bestn[d] = bestn[d-1];
722 bestp[d] = bestp[d-1];
723 }
724 besti[c] = syn_nbs[a]->index[b];
725 bestd[c] = syn_nbs[a]->dist[b];
726 bestn[c] = syn_nbs[a]->norm[b];
727 bestp[c] = syn_nbs[a]->pos[b];
728 break;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100729 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200730 }
731 }
732 }
733 } else { // sort by mean p
734 for(a=1; a < syn_threads; a++) {
735 for(b=0; b < syn_nbs[a]->length; b++) {
736 for(c=0; c < MAX_NEIGHBOURS; c++) {
737 if(target_sums[syn_nbs[a]->index[b]] > bests[c]) {
738 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
739 bestd[d] = bestd[d-1];
740 besti[d] = besti[d-1];
741 bestn[d] = bestn[d-1];
742 bestp[d] = bestp[d-1];
743 bests[d] = bests[d-1];
744 }
745 besti[c] = syn_nbs[a]->index[b];
746 bestd[c] = syn_nbs[a]->dist[b];
747 bestn[c] = syn_nbs[a]->norm[b];
748 bestp[c] = syn_nbs[a]->pos[b];
749 bests[c] = target_sums[syn_nbs[a]->index[b]];
750 break;
751 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100752 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100753 }
754 }
755 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200756 array = newAV();
757 for (a = 0; a < MAX_NEIGHBOURS && besti[a] >= 0; a++) {
758 HV* hash = newHV();
759 SV* word = newSVpvf(&vocab[besti[a] * max_w], 0);
760 if(latin_enc == 0) SvUTF8_on(word);
761 hv_store(hash, "word", strlen("word"), word , 0);
762 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
763 hv_store(hash, "norm", strlen("norm"), newSVnv(bestn[a]), 0);
764 hv_store(hash, "sum", strlen("sum"), newSVnv(target_sums[besti[a]]), 0);
765 hv_store(hash, "pos", strlen("pos"), newSVnv(bestp[a]), 0);
766 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietz271e2a42016-03-22 11:37:43 +0100767 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200768 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100769 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100770end:
Marc Kupietza5f60042017-05-04 10:38:12 +0200771 words = old_words;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100772 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +0100773}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100774
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200775int dump_vecs(char *fname) {
776 long i, j;
777 FILE *f;
778 /* if(words>200000) */
779 /* words=200000; */
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100780
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200781 if((f=fopen(fname, "w")) == NULL) {
782 fprintf(stderr, "cannot open %s for writing\n", fname);
783 return(-1);
784 }
785 fprintf(f, "%lld %lld\n", words, size);
786 for (i=0; i < words; i++) {
787 fprintf(f, "%s ", &vocab[i * max_w]);
788 for(j=0; j < size - 1; j++)
789 fprintf(f, "%f ", M[i*size + j]);
790 fprintf(f, "%f\n", M[i*size + j]);
791 }
792 fclose(f);
793 return(0);
794}
Marc Kupietzdc22b982015-10-09 09:19:34 +0200795