blob: 6592bfac4ec41304b03b9010c440617ce055f4d8 [file] [log] [blame]
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001#include <collocatordb.h>
Marc Kupietz969cab92019-08-05 11:13:42 +02002#include <math.h>
3#include <pthread.h>
4#include <stdio.h>
Marc Kupietzc0d41872021-02-25 16:33:22 +01005#include <stdlib.h>
Marc Kupietz969cab92019-08-05 11:13:42 +02006#include <string.h>
7#include <sys/mman.h>
Marc Kupietze288d8e2024-11-15 16:18:50 +01008#include <fcntl.h>
9#include <unistd.h>
10#include <perl.h>
Marc Kupietzf11d20c2019-08-02 15:42:04 +020011
12#define max_size 2000
13#define max_w 50
14#define MAX_NEIGHBOURS 1000
15#define MAX_WORDS -1
16#define MAX_THREADS 100
17#define MAX_CC 50
18#define EXP_TABLE_SIZE 1000
19#define MAX_EXP 6
20#define MIN_RESP 0.50
21
22//the thread function
23void *connection_handler(void *);
24
25typedef struct {
Marc Kupietz969cab92019-08-05 11:13:42 +020026 long long wordi;
27 long position;
28 float activation;
29 float average;
30 float cprobability; // column wise probability
31 float cprobability_sum;
32 float probability;
33 float activation_sum;
34 float max_activation;
35 float heat[16];
Marc Kupietzf11d20c2019-08-02 15:42:04 +020036} collocator;
37
38typedef struct {
Marc Kupietz969cab92019-08-05 11:13:42 +020039 collocator *best;
40 int length;
Marc Kupietzf11d20c2019-08-02 15:42:04 +020041} knn;
Marc Kupietz969cab92019-08-05 11:13:42 +020042
Marc Kupietzf11d20c2019-08-02 15:42:04 +020043typedef struct {
44 long long wordi[MAX_NEIGHBOURS];
45 char sep[MAX_NEIGHBOURS];
46 int length;
47} wordlist;
48
49typedef struct {
50 long cutoff;
51 wordlist *wl;
Marc Kupietz969cab92019-08-05 11:13:42 +020052 char *token;
53 int N;
54 long from;
55 unsigned long upto;
Marc Kupietzf11d20c2019-08-02 15:42:04 +020056 collocator *best;
57 float *target_sums;
58 float *window_sums;
59 float threshold;
60} knnpars;
61
62typedef struct {
63 uint32_t index;
64 float value;
65} sparse_t;
66
67typedef struct {
68 uint32_t len;
69 sparse_t nbr[100];
70} profile_t;
71
Marc Kupietz969cab92019-08-05 11:13:42 +020072float *M, *M2 = 0L, *syn1neg_window, *expTable;
Marc Kupietzf11d20c2019-08-02 15:42:04 +020073float *window_sums;
74char *vocab;
75char *garbage = NULL;
76COLLOCATORDB *cdb = NULL;
77profile_t *sprofiles = NULL;
78size_t sprofiles_qty = 0;
79
80long long words, size, merged_end;
81long long merge_words = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +020082int num_threads = 20;
83int latin_enc = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +020084int window;
85
86/* load collocation profiles if file exists */
87int load_sprofiles(char *vecsname) {
88 char *basename = strdup(vecsname);
89 char *pos = strstr(basename, ".vecs");
Marc Kupietz969cab92019-08-05 11:13:42 +020090 if (pos)
91 *pos = 0;
92
Marc Kupietzf11d20c2019-08-02 15:42:04 +020093 char binsprofiles_fname[256];
94 strcpy(binsprofiles_fname, basename);
Marc Kupietz969cab92019-08-05 11:13:42 +020095 strcat(binsprofiles_fname, ".sprofiles.bin");
Marc Kupietzf11d20c2019-08-02 15:42:04 +020096 FILE *fp = fopen(binsprofiles_fname, "rb");
97 if (fp == NULL) {
98 printf("Collocation profiles %s not found. No problem.\n", binsprofiles_fname);
99 return 0;
100 }
101 fseek(fp, 0L, SEEK_END);
102 size_t sz = ftell(fp);
103 fclose(fp);
104
105 int fd = open(binsprofiles_fname, O_RDONLY);
Marc Kupietz969cab92019-08-05 11:13:42 +0200106 sprofiles = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200107 if (sprofiles == MAP_FAILED) {
108 close(fd);
109 fprintf(stderr, "Cannot mmap %s\n", binsprofiles_fname);
110 sprofiles = NULL;
111 return 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200112 } else {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200113 sprofiles_qty = sz / sizeof(profile_t);
114 fprintf(stderr, "Successfully mmaped %s containing similar profiles for %ld word forms.\n", binsprofiles_fname, sprofiles_qty);
115 }
116 return 1;
117}
118
Marc Kupietzc0d41872021-02-25 16:33:22 +0100119char *removeExtension(char* myStr) {
120 char *retStr;
121 char *lastExt;
122 if (myStr == NULL) return NULL;
123 if ((retStr = malloc (strlen (myStr) + 1)) == NULL) return NULL;
124 strcpy (retStr, myStr);
125 lastExt = strrchr (retStr, '.');
126 if (lastExt != NULL)
127 *lastExt = '\0';
128 return retStr;
129}
130
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200131int init_net(char *file_name, char *net_name, int latin, int do_open_cdb) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200132 FILE *f, *binvecs, *binwords;
Marc Kupietz969cab92019-08-05 11:13:42 +0200133 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz59865a92021-03-11 17:16:51 +0100134 long long a, b;
Marc Kupietz969cab92019-08-05 11:13:42 +0200135 float len;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200136 double val;
137
Marc Kupietzc0d41872021-02-25 16:33:22 +0100138 char binvecs_fname[1024], binwords_fname[1024];
139
140 if (strstr(file_name, ".txt")) {
141 strcpy(binwords_fname, removeExtension(file_name));
142 } else {
143 strcpy(binwords_fname, file_name);
144 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200145 strcat(binwords_fname, ".words");
146 strcpy(binvecs_fname, file_name);
147 strcat(binvecs_fname, ".vecs");
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200148
149 latin_enc = latin;
150 f = fopen(file_name, "rb");
151 if (f == NULL) {
152 printf("Input file %s not found\n", file_name);
153 return -1;
154 }
155 fscanf(f, "%lld", &words);
Marc Kupietz969cab92019-08-05 11:13:42 +0200156 if (MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200157 fscanf(f, "%lld", &size);
Marc Kupietz969cab92019-08-05 11:13:42 +0200158 if ((binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200159 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietz969cab92019-08-05 11:13:42 +0200160 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
161 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
162 if (M == NULL) {
163 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
164 return -1;
165 }
166 if (strstr(file_name, ".txt")) {
Marc Kupietzc0d41872021-02-25 16:33:22 +0100167 printf("%lld words in ascii vector file with vector size %lld\n", words, size);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200168 for (b = 0; b < words; b++) {
169 a = 0;
170 while (1) {
171 vocab[b * max_w + a] = fgetc(f);
172 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
173 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
174 }
175 vocab[b * max_w + a] = 0;
176 len = 0;
177 for (a = 0; a < size; a++) {
178 fscanf(f, "%lf", &val);
179 M[a + b * size] = val;
180 len += val * val;
Marc Kupietz969cab92019-08-05 11:13:42 +0200181 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200182 len = sqrt(len);
183 for (a = 0; a < size; a++) M[a + b * size] /= len;
184 }
185 } else {
186 for (b = 0; b < words; b++) {
187 a = 0;
188 while (1) {
189 vocab[b * max_w + a] = fgetc(f);
190 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
191 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
192 }
193 vocab[b * max_w + a] = 0;
194 fread(&M[b * size], sizeof(float), size, f);
195 len = 0;
196 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
197 len = sqrt(len);
198 for (a = 0; a < size; a++) M[a + b * size] /= len;
199 }
200 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200201 if ((binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
202 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
203 fclose(binvecs);
204 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
205 fclose(binwords);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200206 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200207 }
208 if ((binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
209 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
210 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
211 if (M == MAP_FAILED || vocab == MAP_FAILED) {
212 close(binvecs_fd);
213 close(binwords_fd);
214 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
215 exit(-1);
216 }
217 } else {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200218 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
219 exit(-1);
Marc Kupietz969cab92019-08-05 11:13:42 +0200220 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200221 fclose(f);
222
Marc Kupietz969cab92019-08-05 11:13:42 +0200223 if (net_name && strlen(net_name) > 0) {
224 if ((net_fd = open(net_name, O_RDONLY)) >= 0) {
225 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200226 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
227 // munmap(M, sizeof(float) * words * size);
228 M2 = mmap(0, sizeof(float) * words * size + sizeof(float) * 2 * window * size * words, PROT_READ, MAP_SHARED, net_fd, 0);
229 if (M2 == MAP_FAILED) {
230 close(net_fd);
231 fprintf(stderr, "Cannot mmap %s\n", net_name);
232 exit(-1);
233 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200234 syn1neg_window = M2 + words * size;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200235 } else {
236 fprintf(stderr, "Cannot open %s\n", net_name);
237 exit(-1);
238 }
239 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
240
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200241 if (do_open_cdb) {
242 char collocatordb_name[2048];
243 strcpy(collocatordb_name, net_name);
244 char *ext = rindex(collocatordb_name, '.');
245 if (ext) {
246 strcpy(ext, ".rocksdb");
247 if (access(collocatordb_name, R_OK) == 0) {
248 *ext = 0;
249 fprintf(stderr, "Opening collocator DB %s\n", collocatordb_name);
250 cdb = open_collocatordb(collocatordb_name);
Marc Kupietzc0d41872021-02-25 16:33:22 +0100251 } else {
252 fprintf(stderr, "Cannot open collocator DB %s\n", collocatordb_name);
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200253 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200254 }
255 }
256 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200257
Marc Kupietz969cab92019-08-05 11:13:42 +0200258 expTable = (float *)malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
259 for (i = 0; i < EXP_TABLE_SIZE; i++) {
260 expTable[i] = exp((i / (float)EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
261 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
262 }
263 window_sums = malloc(sizeof(float) * (window + 1) * 2);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200264
265 return 0;
266}
267
Marc Kupietz969cab92019-08-05 11:13:42 +0200268long mergeVectors(char *file_name) {
Marc Kupietz59865a92021-03-11 17:16:51 +0100269 FILE *f;
270 int binwords_fd, binvecs_fd;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200271 float *merge_vecs;
272 char *merge_vocab;
Marc Kupietz969cab92019-08-05 11:13:42 +0200273 /* long long merge_words, merge_size; */
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200274 long long merge_size;
275
Marc Kupietz969cab92019-08-05 11:13:42 +0200276 char binvecs_fname[256], binwords_fname[256];
Marc Kupietzc0d41872021-02-25 16:33:22 +0100277
278
Marc Kupietz969cab92019-08-05 11:13:42 +0200279 strcpy(binwords_fname, file_name);
280 strcat(binwords_fname, ".words");
281 strcpy(binvecs_fname, file_name);
282 strcat(binvecs_fname, ".vecs");
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200283
284 f = fopen(file_name, "rb");
285 if (f == NULL) {
286 printf("Input file %s not found\n", file_name);
Marc Kupietz59865a92021-03-11 17:16:51 +0100287 exit(-1);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200288 }
289 fscanf(f, "%lld", &merge_words);
290 fscanf(f, "%lld", &merge_size);
Marc Kupietz969cab92019-08-05 11:13:42 +0200291 if (merge_size != size) {
292 fprintf(stderr, "vectors must have the same length\n");
293 exit(-1);
294 }
295 if ((binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
296 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
297 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200298 if (merge_vecs == NULL || merge_vocab == NULL) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200299 close(binvecs_fd);
300 close(binwords_fd);
301 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
302 exit(-1);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200303 }
304 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
305 read(binwords_fd, merge_vocab, merge_words * max_w);
Marc Kupietz969cab92019-08-05 11:13:42 +0200306 } else {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200307 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
308 exit(-1);
Marc Kupietz969cab92019-08-05 11:13:42 +0200309 }
310 printf("Successfully reallocated memory\nMerging...\n");
311 fflush(stdout);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200312 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
313 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
314 munmap(M, words * size * sizeof(float));
315 munmap(vocab, words * max_w);
316 M = merge_vecs;
317 vocab = merge_vocab;
318 merged_end = merge_words;
319 words += merge_words;
320 fclose(f);
Marc Kupietz969cab92019-08-05 11:13:42 +0200321 printf("merged_end: %lld, words: %lld\n", merged_end, words);
322 //printBiggestMergedDifferences();
323 return ((long)merged_end);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200324}
325
326void filter_garbage() {
327 long i;
328 unsigned char *w, previous, c;
329 garbage = malloc(words);
330 memset(garbage, 0, words);
331 for (i = 0; i < words; i++) {
Marc Kupietz59865a92021-03-11 17:16:51 +0100332 w = (unsigned char *) vocab + i * max_w;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200333 previous = 0;
Marc Kupietz59865a92021-03-11 17:16:51 +0100334 if (strncmp("quot", (const char *)w, 4) == 0) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200335 garbage[i] = 1;
336 // printf("Gargabe: %s\n", vocab + i * max_w);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200337 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200338 while ((c = *w++) && !garbage[i]) {
339 if (((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200340 (previous == '-' && (c & 32)) ||
Marc Kupietz969cab92019-08-05 11:13:42 +0200341 (previous == 0xc2 && (c == 0xa4 || c == 0xb6)) ||
342 (previous == 'q' && c == 'u' && *(w) == 'o' && *(w + 1) == 't') || /* quot */
343 c == '<') {
344 garbage[i] = 1;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200345 continue;
346 }
347 previous = c;
348 }
349 }
350 }
351 return;
352}
353
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200354knn *simpleGetCollocators(int word, int number, long cutoff, int *result) {
355 knnpars *pars = calloc(sizeof(knnpars), 1);
Marc Kupietz59865a92021-03-11 17:16:51 +0100356 float *target_sums = NULL;
357 float *my_window_sums = malloc(sizeof(float) * (window + 1) * 2);
Marc Kupietz969cab92019-08-05 11:13:42 +0200358 pars->cutoff = (cutoff ? cutoff : 300000);
Marc Kupietz59865a92021-03-11 17:16:51 +0100359 long a;
Marc Kupietz969cab92019-08-05 11:13:42 +0200360 for (a = 0; a < cutoff; a++)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200361 target_sums[a] = 0;
362 pars->target_sums = target_sums;
Marc Kupietz59865a92021-03-11 17:16:51 +0100363 pars->window_sums = my_window_sums;
Marc Kupietz969cab92019-08-05 11:13:42 +0200364 pars->N = (number ? number : 20);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200365 pars->from = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200366 pars->upto = window * 2 - 1;
367 knn *syn_nbs = NULL; // = (knn*) getCollocators(pars);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200368 free(pars);
Marc Kupietz59865a92021-03-11 17:16:51 +0100369 free(my_window_sums);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200370 free(target_sums);
371 return syn_nbs;
372}
373
374void *getCollocators(void *args) {
375 knnpars *pars = args;
Marc Kupietz969cab92019-08-05 11:13:42 +0200376 int N = pars->N;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200377
378 int cc = pars->wl->wordi[0];
Marc Kupietz969cab92019-08-05 11:13:42 +0200379 knn *nbs = NULL;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200380 long window_layer_size = size * window * 2;
Marc Kupietz59865a92021-03-11 17:16:51 +0100381 long a, b, c, d, window_offset, target, max_target = 0, maxmax_target;
Marc Kupietz969cab92019-08-05 11:13:42 +0200382 float f, max_f, maxmax_f;
383 float *target_sums = NULL, worstbest, wpos_sum;
384 collocator *best;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200385
Marc Kupietz969cab92019-08-05 11:13:42 +0200386 if (M2 == NULL || cc == -1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200387 return NULL;
388
Marc Kupietz969cab92019-08-05 11:13:42 +0200389 a = posix_memalign((void **)&target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200390 memset(target_sums, 0, pars->cutoff * sizeof(float));
Marc Kupietz969cab92019-08-05 11:13:42 +0200391 best = malloc((N > 200 ? N : 200) * sizeof(collocator));
392 memset(best, 0, (N > 200 ? N : 200) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200393 worstbest = pars->threshold;
394
395 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200396 target_sums[b] = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200397 for (b = 0; b < N; b++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200398 best[b].wordi = -1;
399 best[b].probability = 1;
400 best[b].activation = worstbest;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200401 }
402
403 d = cc;
404 maxmax_f = -1;
405 maxmax_target = 0;
406
407 for (a = pars->from; a < pars->upto; a++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200408 if (a >= window)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200409 a++;
410 wpos_sum = 0;
411 printf("window pos: %ld\n", a);
412 if (a != window) {
413 max_f = -1;
414 window_offset = a * size;
415 if (a > window)
416 window_offset -= size;
Marc Kupietz969cab92019-08-05 11:13:42 +0200417 for (target = 0; target < pars->cutoff; target++) {
418 if (garbage && garbage[target]) continue;
419 if (target == d)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200420 continue;
421 f = 0;
422 for (c = 0; c < size; c++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200423 f += M2[d * size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200424 if (f < -MAX_EXP)
425 continue;
426 else if (f > MAX_EXP)
427 continue;
428 else
Marc Kupietz969cab92019-08-05 11:13:42 +0200429 f = expTable[(int)((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200430 wpos_sum += f;
431
432 target_sums[target] += f;
Marc Kupietz969cab92019-08-05 11:13:42 +0200433 if (f > worstbest) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200434 for (b = 0; b < N; b++) {
435 if (f > best[b].activation) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200436 memmove(best + b + 1, best + b, (N - b - 1) * sizeof(collocator));
437 best[b].activation = f;
438 best[b].wordi = target;
439 best[b].position = window - a;
440 break;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200441 }
442 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200443 if (b == N - 1)
444 worstbest = best[N - 1].activation;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200445 }
446 }
Marc Kupietz59865a92021-03-11 17:16:51 +0100447 printf("%ld %.2f\n", max_target, max_f);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200448 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
Marc Kupietz969cab92019-08-05 11:13:42 +0200449 if (max_f > maxmax_f) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200450 maxmax_f = max_f;
451 maxmax_target = max_target;
452 }
453 for (b = 0; b < N; b++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200454 if (best[b].position == window - a)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200455 best[b].cprobability = best[b].activation / wpos_sum;
456 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200457 printf("\x1b[1m%s\x1b[0m ", &vocab[d * max_w]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200458 }
459 pars->window_sums[a] = wpos_sum;
460 }
461 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200462 pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200463
464 free(target_sums);
Marc Kupietz969cab92019-08-05 11:13:42 +0200465 for (b = 0; b < N && best[b].wordi >= 0; b++)
466 ;
Marc Kupietz59865a92021-03-11 17:16:51 +0100467 // THIS LOOP IS NEEDED (b...)
Marc Kupietz969cab92019-08-05 11:13:42 +0200468 // printf("%d: best syn: %s %.2f %.5f\n", b, &vocab[best[b].wordi*max_w], best[b].activation, best[b].probability);
469 // printf("\n");
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200470 nbs = malloc(sizeof(knn));
Marc Kupietz969cab92019-08-05 11:13:42 +0200471 nbs->best = best;
472 nbs->length = b - 1;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200473 pthread_exit(nbs);
474}
475
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200476float getOutputWeight(int hidden, long target, int window_position) {
477 const long window_layer_size = size * window * 2;
478 int a;
479
480 if (window_position == 0 || window_position > window || window_position < -window) {
481 fprintf(stderr, "window_position: %d - assert: -%d <= window_position <= %d && window_position != 0 failed.\n", window_position, window, window);
482 exit(-1);
483 }
484
485 if (hidden >= size) {
Marc Kupietz59865a92021-03-11 17:16:51 +0100486 fprintf(stderr, "hidden: %d - assert: hidden < %lld failed.\n", hidden, size);
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200487 exit(-1);
488 }
489
490 if (target >= words) {
Marc Kupietz59865a92021-03-11 17:16:51 +0100491 fprintf(stderr, "target: %ld - assert: target < %lld failed.\n", target, words);
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200492 exit(-1);
493 }
494
495 a = window_position + window;
496 if (a > window) {
497 --a;
498 }
499 long window_offset = a * size;
500 return syn1neg_window[target * window_layer_size + window_offset + hidden];
501}
502
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200503AV *getVecs(AV *array) {
504 int i, b;
505 AV *result = newAV();
Marc Kupietz969cab92019-08-05 11:13:42 +0200506 for (i = 0; i <= av_len(array); i++) {
507 SV **elem = av_fetch(array, i, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200508 if (elem != NULL) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200509 long j = (long)SvNV(*elem);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200510 AV *vector = newAV();
511 for (b = 0; b < size; b++) {
512 av_push(vector, newSVnv(M[b + j * size]));
513 }
Marc Kupietzbdd779a2024-08-05 10:02:29 +0200514 av_push(result, newRV_noinc((SV *)vector));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200515 }
516 }
517 return result;
518}
519
520char *getSimilarProfiles(long node) {
521 int i;
522 char buffer[120000];
523 char pair_buffer[2048];
Marc Kupietz969cab92019-08-05 11:13:42 +0200524 buffer[0] = '[';
525 buffer[1] = 0;
526 if (node >= sprofiles_qty) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200527 printf("Not available in precomputed profile\n");
Marc Kupietz969cab92019-08-05 11:13:42 +0200528 return (strdup("[{\"w\":\"not available\", \"v\":0}]\n"));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200529 }
530
531 printf("******* %s ******\n", &vocab[max_w * node]);
Marc Kupietz969cab92019-08-05 11:13:42 +0200532
533 for (i = 0; i < 100 && i < sprofiles[node].len; i++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200534 sprintf(pair_buffer, "{\"w\":\"%s\", \"v\":%f},", &vocab[max_w * (sprofiles[node].nbr[i].index)], sprofiles[node].nbr[i].value);
535 strcat(buffer, pair_buffer);
536 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200537 buffer[strlen(buffer) - 1] = ']';
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200538 strcat(buffer, "\n");
Marc Kupietz59865a92021-03-11 17:16:51 +0100539 printf("%s", buffer);
Marc Kupietz969cab92019-08-05 11:13:42 +0200540 return (strdup(buffer));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200541}
542
Marc Kupietzf6080012021-03-12 09:14:42 +0100543char *getCollocationScores(long node, long collocate) {
544 char *res = (cdb ? strdup(get_collocation_scores_as_json(cdb, node, collocate)) : "[]");
545 return res;
546}
547
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200548char *getClassicCollocators(long node) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200549 char *res = (cdb ? strdup(get_collocators_as_json(cdb, node)) : "[]");
550 return res;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200551}
552
553wordlist *getTargetWords(char *st1, int search_backw) {
554 wordlist *wl = malloc(sizeof(wordlist));
Marc Kupietz59865a92021-03-11 17:16:51 +0100555 char st[100][max_size];
Marc Kupietz969cab92019-08-05 11:13:42 +0200556 long a, b = 0, c = 0, cn = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200557
558 while (1) {
559 st[cn][b] = st1[c];
560 b++;
561 c++;
562 st[cn][b] = 0;
563 if (st1[c] == 0) break;
Marc Kupietzc0d41872021-02-25 16:33:22 +0100564 if (st1[c] == ' ' /*|| st1[c] == '-'*/) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200565 b = 0;
566 c++;
567 }
568 }
569 cn++;
570 for (a = 0; a < cn; a++) {
571 if (search_backw) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200572 for (b = words - 1; b >= (merge_words ? merge_words : 0) && strcmp(&vocab[b * max_w], st[a]) != 0; b--)
573 ;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200574 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200575 for (b = 0; b < (merge_words ? merge_words : words) && strcmp(&vocab[b * max_w], st[a]) != 0; b++)
576 ;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200577 }
578 if (b == words) b = -1;
579 wl->wordi[a] = b;
580 if (b == -1) {
581 fprintf(stderr, "Out of dictionary word!\n");
582 cn--;
583 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200584 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", &vocab[wl->wordi[a] * max_w], wl->wordi[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200585 }
586 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200587 wl->length = cn;
588 return (wl);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200589}
590
Marc Kupietzcb43e492019-12-03 10:07:53 +0100591long getWordNumber(char *word) {
592 wordlist *wl = getTargetWords(word, 0);
593 if(wl->length > 0)
594 return(wl->wordi[0]);
595 return(0);
596}
597
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200598float get_distance(long b, long c) {
599 long a;
600 float dist = 0;
601 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + b * size];
602 return dist;
603}
604
Marc Kupietz969cab92019-08-05 11:13:42 +0200605char *getBiggestMergedDifferences() {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200606 static char *result = NULL;
Marc Kupietz59865a92021-03-11 17:16:51 +0100607 float dist;
608 long long a, c;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200609 int N = 1000;
610
Marc Kupietz969cab92019-08-05 11:13:42 +0200611 if (merged_end == 0)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200612 result = "[]";
Marc Kupietz969cab92019-08-05 11:13:42 +0200613
614 if (result != NULL)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200615 return result;
616
617 printf("Looking for biggest distances between main and merged vectors ...\n");
618 collocator *best;
619 best = malloc(N * sizeof(collocator));
620 memset(best, 0, N * sizeof(collocator));
621
Marc Kupietz969cab92019-08-05 11:13:42 +0200622 float worstbest = 1000000;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200623
624 for (a = 0; a < N; a++) best[a].activation = worstbest;
625
626 for (c = 0; c < 500000; c++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200627 if (garbage && garbage[c]) continue;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200628 dist = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200629 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + (c + merged_end) * size];
630 if (dist < worstbest) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200631 for (a = 0; a < N; a++) {
632 if (dist < best[a].activation) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200633 memmove(best + a + 1, best + a, (N - a - 1) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200634 best[a].activation = dist;
635 best[a].wordi = c;
636 break;
637 }
638 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200639 worstbest = best[N - 1].activation;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200640 }
641 }
642
Marc Kupietz969cab92019-08-05 11:13:42 +0200643 result = malloc(N * max_w);
Marc Kupietzbdd779a2024-08-05 10:02:29 +0200644 char *p = (char *) result;
Marc Kupietz969cab92019-08-05 11:13:42 +0200645 *p++ = '[';
646 *p = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200647 for (a = 0; a < N; a++) {
Marc Kupietz59865a92021-03-11 17:16:51 +0100648 p += sprintf(p, "{\"rank\":%lld,\"word\":\"%s\",\"dist\":%.3f},", a, &vocab[best[a].wordi * max_w], 1 - best[a].activation);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200649 }
650 *--p = ']';
Marc Kupietz969cab92019-08-05 11:13:42 +0200651 return (result);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200652}
653
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200654float cos_similarity(long b, long c) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200655 float dist = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200656 long a;
Marc Kupietz969cab92019-08-05 11:13:42 +0200657 for (a = 0; a < size; a++) dist += M[b * size + a] * M[c * size + a];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200658 return dist;
659}
660
661char *cos_similarity_as_json(char *w1, char *w2) {
662 wordlist *a, *b;
663 float res;
664 a = getTargetWords(w1, 0);
665 b = getTargetWords(w2, 0);
Marc Kupietz969cab92019-08-05 11:13:42 +0200666 if (a == NULL || b == NULL || a->length != 1 || b->length != 1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200667 res = -1;
668 else
669 res = cos_similarity(a->wordi[0], b->wordi[0]);
670 fprintf(stderr, "a: %lld b: %lld res:%f\n", a->wordi[0], b->wordi[0], res);
671 char *json = malloc(16);
672 sprintf(json, "%.5f", res);
673 return json;
674}
675
676void *_get_neighbours(void *arg) {
677 knnpars *pars = arg;
Marc Kupietz969cab92019-08-05 11:13:42 +0200678 int N = pars->N;
679 long from = pars->from;
680 unsigned long upto = pars->upto;
Marc Kupietz59865a92021-03-11 17:16:51 +0100681 char *sep;
Marc Kupietz969cab92019-08-05 11:13:42 +0200682 float dist, len, vec[max_size];
Marc Kupietz59865a92021-03-11 17:16:51 +0100683 long long a, b, c, cn, *bi;
Marc Kupietz969cab92019-08-05 11:13:42 +0200684 knn *nbs = NULL;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200685 wordlist *wl = pars->wl;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200686
Marc Kupietz969cab92019-08-05 11:13:42 +0200687 collocator *best = pars->best;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200688
Marc Kupietz969cab92019-08-05 11:13:42 +0200689 float worstbest = -1;
690
691 for (a = 0; a < N; a++) best[a].activation = 0;
692 a = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200693 bi = wl->wordi;
Marc Kupietz969cab92019-08-05 11:13:42 +0200694 cn = wl->length;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200695 sep = wl->sep;
Marc Kupietz969cab92019-08-05 11:13:42 +0200696 b = bi[0];
Marc Kupietz969cab92019-08-05 11:13:42 +0200697 if (b == -1) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200698 goto end;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200699 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200700 for (a = 0; a < size; a++) vec[a] = 0;
701 for (b = 0; b < cn; b++) {
702 if (bi[b] == -1) continue;
703 if (b > 0 && sep[b - 1] == '-')
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200704 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
705 else
706 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietz969cab92019-08-05 11:13:42 +0200707 }
708 len = 0;
709 for (a = 0; a < size; a++) len += vec[a] * vec[a];
710 len = sqrt(len);
711 for (a = 0; a < size; a++) vec[a] /= len;
712 for (a = 0; a < N; a++) best[a].activation = -1;
713 for (c = from; c < upto; c++) {
714 if (garbage && garbage[c]) continue;
715 a = 0;
716 // do not skip taget word
717 // for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
718 // if (a == 1) continue;
719 dist = 0;
720 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
721 if (dist > worstbest) {
722 for (a = 0; a < N; a++) {
723 if (dist > best[a].activation) {
724 memmove(best + a + 1, best + a, (N - a - 1) * sizeof(collocator));
725 best[a].activation = dist;
726 best[a].wordi = c;
727 break;
728 }
729 }
730 worstbest = best[N - 1].activation;
731 }
732 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200733
734end:
Marc Kupietz969cab92019-08-05 11:13:42 +0200735 pthread_exit(nbs);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200736}
737
Marc Kupietz969cab92019-08-05 11:13:42 +0200738int cmp_activation(const void *a, const void *b) {
739 float fb = ((collocator *)a)->activation;
740 float fa = ((collocator *)b)->activation;
741 return (fa > fb) - (fa < fb);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200742}
743
Marc Kupietz969cab92019-08-05 11:13:42 +0200744int cmp_probability(const void *a, const void *b) {
745 float fb = ((collocator *)a)->probability;
746 float fa = ((collocator *)b)->probability;
747 return (fa > fb) - (fa < fb);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200748}
749
Marc Kupietz969cab92019-08-05 11:13:42 +0200750char *getPosWiseW2VCollocatorsAsTsv(char *word, long maxPerPos, long cutoff, float threshold) {
751 HV *result = newHV();
Marc Kupietz59865a92021-03-11 17:16:51 +0100752 float *target_sums = NULL;
753 long a, b;
Marc Kupietz969cab92019-08-05 11:13:42 +0200754 knn *para_nbs[MAX_THREADS];
755 knn *syn_nbs[MAX_THREADS];
756 knnpars pars[MAX_THREADS];
757 pthread_t *pt = (pthread_t *)malloc((num_threads + 1) * sizeof(pthread_t));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200758 wordlist *wl;
Marc Kupietz969cab92019-08-05 11:13:42 +0200759 int syn_threads = (M2 ? window * 2 : 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200760 int search_backw = 0;
761 collocator *best = NULL;
Marc Kupietz969cab92019-08-05 11:13:42 +0200762 posix_memalign((void **)&best, 128, 10 * (maxPerPos >= 200 ? maxPerPos : 200) * sizeof(collocator));
763 memset(best, 0, (maxPerPos >= 200 ? maxPerPos : 200) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200764
Marc Kupietz969cab92019-08-05 11:13:42 +0200765 if (cutoff < 1 || cutoff > words)
766 cutoff = words;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200767
768 wl = getTargetWords(word, search_backw);
Marc Kupietz969cab92019-08-05 11:13:42 +0200769 if (wl == NULL || wl->length < 1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200770 return "";
771
Marc Kupietz969cab92019-08-05 11:13:42 +0200772 a = posix_memalign((void **)&target_sums, 128, cutoff * sizeof(float));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200773 memset(target_sums, 0, cutoff * sizeof(float));
774
775 printf("Starting %d threads\n", syn_threads);
776 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200777 for (a = 0; a < syn_threads; a++) {
778 pars[a].cutoff = cutoff;
779 pars[a].target_sums = target_sums;
780 pars[a].window_sums = window_sums;
781 pars[a].wl = wl;
782 pars[a].N = maxPerPos;
783 pars[a].threshold = threshold;
784 pars[a].from = a;
785 pars[a].upto = a + 1;
786 pthread_create(&pt[a], NULL, getCollocators, (void *)&pars[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200787 }
788 printf("Waiting for syn threads to join\n");
789 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200790 for (a = 0; a < syn_threads; a++) pthread_join(pt[a], (void *)&syn_nbs[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200791 printf("Syn threads joint\n");
792 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200793 result = malloc(maxPerPos * 80 * syn_threads);
Marc Kupietzbdd779a2024-08-05 10:02:29 +0200794 char *p = (char *) result;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200795 *p = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200796 for (a = syn_threads - 1; a >= 0; a--) {
797 for (b = 0; b < syn_nbs[a]->length; b++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200798 p += sprintf(p, "%ld\t%s\t%f\n", syn_nbs[a]->best[b].position, &vocab[syn_nbs[a]->best[b].wordi * max_w], syn_nbs[a]->best[b].activation);
799 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200800 }
Marc Kupietzbdd779a2024-08-05 10:02:29 +0200801 return ((char *)result);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200802}
803
804SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe, int no_similar_profiles) {
805 HV *result = newHV();
Marc Kupietz59865a92021-03-11 17:16:51 +0100806 float *target_sums = NULL;
Marc Kupietz969cab92019-08-05 11:13:42 +0200807 long a, b, c, d, slice;
808 knn *para_nbs[MAX_THREADS];
809 knn *syn_nbs[MAX_THREADS];
810 knnpars pars[MAX_THREADS];
811 pthread_t *pt = (pthread_t *)malloc((num_threads + 1) * sizeof(pthread_t));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200812 wordlist *wl;
Marc Kupietz969cab92019-08-05 11:13:42 +0200813 int syn_threads = (M2 ? window * 2 : 0);
814 int para_threads = (no_similar_profiles ? 0 : num_threads - syn_threads);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200815
816 collocator *best = NULL;
Marc Kupietz969cab92019-08-05 11:13:42 +0200817 posix_memalign((void **)&best, 128, 10 * (N >= 200 ? N : 200) * sizeof(collocator));
818 memset(best, 0, (N >= 200 ? N : 200) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200819
Marc Kupietz969cab92019-08-05 11:13:42 +0200820 if (N > MAX_NEIGHBOURS) N = MAX_NEIGHBOURS;
821
822 if (cutoff < 1 || cutoff > words)
823 cutoff = words;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200824
825 wl = getTargetWords(st1, search_backw);
Marc Kupietz969cab92019-08-05 11:13:42 +0200826 if (wl == NULL || wl->length < 1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200827 goto end;
828
Marc Kupietz969cab92019-08-05 11:13:42 +0200829 slice = cutoff / para_threads;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200830
Marc Kupietz969cab92019-08-05 11:13:42 +0200831 a = posix_memalign((void **)&target_sums, 128, cutoff * sizeof(float));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200832 memset(target_sums, 0, cutoff * sizeof(float));
833
Marc Kupietzc0d41872021-02-25 16:33:22 +0100834 printf("Starting %d threads for paradigmatic search\n", para_threads);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200835 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200836 for (a = 0; a < para_threads; a++) {
837 pars[a].cutoff = cutoff;
838 pars[a].token = st1;
839 pars[a].wl = wl;
840 pars[a].N = N;
841 pars[a].best = &best[N * a];
842 if (merge_words == 0 || search_backw == 0) {
843 pars[a].from = a * slice;
844 pars[a].upto = ((a + 1) * slice > cutoff ? cutoff : (a + 1) * slice);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200845 } else {
846 pars[a].from = merge_words + a * slice;
Marc Kupietz969cab92019-08-05 11:13:42 +0200847 pars[a].upto = merge_words + ((a + 1) * slice > cutoff ? cutoff : (a + 1) * slice);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200848 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200849 printf("From: %ld, Upto: %ld\n", pars[a].from, pars[a].upto);
850 pthread_create(&pt[a], NULL, _get_neighbours, (void *)&pars[a]);
851 }
852 if (M2) {
853 for (a = 0; a < syn_threads; a++) {
854 pars[a + para_threads].cutoff = cutoff;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200855 pars[a + para_threads].target_sums = target_sums;
856 pars[a + para_threads].window_sums = window_sums;
857 pars[a + para_threads].wl = wl;
858 pars[a + para_threads].N = N;
859 pars[a + para_threads].threshold = MIN_RESP;
860 pars[a + para_threads].from = a;
Marc Kupietz969cab92019-08-05 11:13:42 +0200861 pars[a + para_threads].upto = a + 1;
862 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *)&pars[a + para_threads]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200863 }
864 }
865 printf("Waiting for para threads to join\n");
866 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200867 for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *)&para_nbs[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200868 printf("Para threads joint\n");
869 fflush(stdout);
870
Marc Kupietz969cab92019-08-05 11:13:42 +0200871 /* if(!syn_nbs[0]) */
872 /* goto end; */
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200873
Marc Kupietz969cab92019-08-05 11:13:42 +0200874 qsort(best, N * para_threads, sizeof(collocator), cmp_activation);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200875
876 long long chosen[MAX_NEIGHBOURS];
Marc Kupietz59865a92021-03-11 17:16:51 +0100877 printf("N: %d\n", N);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200878
Marc Kupietz969cab92019-08-05 11:13:42 +0200879 AV *array = newAV();
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200880 int i, j;
Marc Kupietz969cab92019-08-05 11:13:42 +0200881 int l1_words = 0, l2_words = 0;
882
883 for (a = 0, i = 0; i < N && a < N * para_threads; a++) {
884 int filtered = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200885 long long c = best[a].wordi;
886 if ((merge_words && dedupe && i > 1) || (!merge_words && dedupe && i > 0)) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200887 for (j = 0; j < i && !filtered; j++)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200888 if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) ||
889 strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200890 printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]);
891 filtered = 1;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200892 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200893 if (filtered)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200894 continue;
895 }
896
Marc Kupietz969cab92019-08-05 11:13:42 +0200897 if (0 && merge_words > 0) {
898 if (c >= merge_words) {
899 if (l1_words > N / 2)
900 continue;
901 else
902 l1_words++;
903 } else {
904 if (l2_words > N / 2)
905 continue;
906 else
907 l2_words++;
908 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200909 }
910
Marc Kupietz969cab92019-08-05 11:13:42 +0200911 // printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
912 // fflush(stdout);
913 HV *hash = newHV();
914 SV *word = newSVpvf(&vocab[c * max_w], 0);
915 chosen[i] = c;
916 if (latin_enc == 0) SvUTF8_on(word);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200917 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200918 hv_store(hash, "word", strlen("word"), word, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200919 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
920 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
921 AV *vector = newAV();
922 for (b = 0; b < size; b++) {
923 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
924 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200925 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV *)vector), 0);
926 av_push(array, newRV_noinc((SV *)hash));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200927 i++;
928 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200929 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV *)array), 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200930
Marc Kupietz969cab92019-08-05 11:13:42 +0200931 for (b = 0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200932 best[b].wordi = -1L;
933 best[b].activation = 0;
934 best[b].probability = 0;
935 best[b].position = 0;
936 best[b].activation_sum = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200937 memset(best[b].heat, 0, sizeof(float) * 16);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200938 }
939
Marc Kupietz969cab92019-08-05 11:13:42 +0200940 float total_activation = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200941
942 if (M2) {
943 printf("Waiting for syn threads to join\n");
944 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200945 for (a = 0; a < syn_threads; a++) pthread_join(pt[a + para_threads], (void *)&syn_nbs[a]);
946 for (a = 0; a <= syn_threads; a++) {
947 if (a == window) continue;
948 total_activation += window_sums[a];
Marc Kupietz59865a92021-03-11 17:16:51 +0100949 printf("window pos: %ld, sum: %f\n", a, window_sums[a]);
Marc Kupietz969cab92019-08-05 11:13:42 +0200950 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200951 printf("syn threads joint\n");
952 fflush(stdout);
953
Marc Kupietz969cab92019-08-05 11:13:42 +0200954 for (b = 0; b < syn_nbs[0]->length; b++) {
955 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
956 best[b].position = -1; // syn_nbs[0]->pos[b];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200957 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietz969cab92019-08-05 11:13:42 +0200958 best[b].max_activation = 0.0;
959 best[b].average = 0.0;
960 best[b].probability = 0.0;
961 best[b].cprobability = syn_nbs[0]->best[b].cprobability;
962 memset(best[b].heat, 0, sizeof(float) * 16);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200963 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200964
965 float best_window_sum[MAX_NEIGHBOURS];
Marc Kupietz59865a92021-03-11 17:16:51 +0100966 int found_index = 0, i = 0, w;
Marc Kupietz969cab92019-08-05 11:13:42 +0200967 for (a = 0; a < syn_threads; a++) {
968 for (b = 0; b < syn_nbs[a]->length; b++) {
969 for (i = 0; i < found_index; i++)
970 if (best[i].wordi == syn_nbs[a]->best[b].wordi)
971 break;
972 if (i >= found_index) {
973 best[found_index].max_activation = 0.0;
974 best[found_index].average = 0.0;
975 best[found_index].probability = 0.0;
976 memset(best[found_index].heat, 0, sizeof(float) * 16);
977 best[found_index].cprobability = syn_nbs[a]->best[b].cprobability;
978 best[found_index].activation_sum = target_sums[syn_nbs[a]->best[b].wordi]; // syn_nbs[a]->best[b].activation_sum;
979 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
980 // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
981 }
982 }
983 }
984 sort_by = 0; // ALWAYS AUTO-FOCUS
985 if (sort_by != 1 && sort_by != 2) { // sort by auto focus mean
986 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) - 1);
987 int wpos;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200988 int bits_set = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200989 for (i = 0; i < found_index; i++) {
990 best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0;
991 for (w = 1; w < (1 << syn_threads); w++) { // loop through all possible windows
992 float word_window_sum = 0, word_window_average = 0, word_cprobability_sum = 0, word_activation_sum = 0, total_window_sum = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200993 bits_set = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200994 for (a = 0; a < syn_threads; a++) {
995 if ((1 << a) & w) {
996 wpos = (a >= window ? a + 1 : a);
997 total_window_sum += window_sums[wpos];
998 }
999 }
1000 // printf("%d window-sum %f\n", w, total_window_sum);
1001 for (a = 0; a < syn_threads; a++) {
1002 if ((1 << a) & w) {
1003 wpos = (a >= window ? a + 1 : a);
1004 bits_set++;
1005 for (b = 0; b < syn_nbs[a]->length; b++)
1006 if (best[i].wordi == syn_nbs[a]->best[b].wordi) {
1007 // float acti = syn_nbs[a]->best[b].activation / total_window_sum;
1008 // word_window_sum += syn_nbs[a]->dist[b] * syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1009 // word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1010 // 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 Kupietzf11d20c2019-08-02 15:42:04 +02001011
Marc Kupietz969cab92019-08-05 11:13:42 +02001012 word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1013 // word_window_sum += acti - (word_window_sum * acti); syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001014
Marc Kupietz969cab92019-08-05 11:13:42 +02001015 word_window_average += syn_nbs[a]->best[b].activation; // - word_window_average * syn_nbs[a]->best[b].activation; // conormalied activation sum
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001016 word_cprobability_sum += syn_nbs[a]->best[b].cprobability - word_cprobability_sum * syn_nbs[a]->best[b].cprobability; // conormalied column probability sum
Marc Kupietz969cab92019-08-05 11:13:42 +02001017 word_activation_sum += syn_nbs[a]->best[b].activation;
1018 if (syn_nbs[a]->best[b].activation > best[i].max_activation)
1019 best[i].max_activation = syn_nbs[a]->best[b].activation;
1020 if (syn_nbs[a]->best[b].activation > best[i].heat[wpos])
1021 best[i].heat[wpos] = syn_nbs[a]->best[b].activation;
1022 }
1023 }
1024 }
1025 if (bits_set) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001026 word_window_average /= bits_set;
Marc Kupietz969cab92019-08-05 11:13:42 +02001027 // word_activation_sum /= bits_set;
1028 // word_window_sum /= bits_set;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001029 }
1030
Marc Kupietz969cab92019-08-05 11:13:42 +02001031 word_window_sum /= total_window_sum;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001032
Marc Kupietz969cab92019-08-05 11:13:42 +02001033 if (word_window_sum > best[i].probability) {
1034 // best[i].position = w;
1035 best[i].probability = word_window_sum;
1036 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001037
Marc Kupietz969cab92019-08-05 11:13:42 +02001038 if (word_cprobability_sum > best[i].cprobability_sum) {
1039 best[i].position = w;
1040 best[i].cprobability_sum = word_cprobability_sum;
1041 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001042
Marc Kupietz969cab92019-08-05 11:13:42 +02001043 best[i].average = word_window_average;
1044 // best[i].activation = word_activation_sum;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001045 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001046 }
1047 qsort(best, found_index, sizeof(collocator), cmp_probability);
1048 // for(i=0; i < found_index; i++) {
1049 // printf("found: %s - sum: %f - window: %d\n", &vocab[best[i].wordi * max_w], best[i].activation, best[i].position);
1050 // }
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001051
Marc Kupietz969cab92019-08-05 11:13:42 +02001052 } else if (sort_by == 1) { // responsiveness any window position
1053 int wpos;
1054 for (i = 0; i < found_index; i++) {
1055 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
1056 for (a = 0; a < syn_threads; a++) {
1057 wpos = (a >= window ? a + 1 : a);
1058 for (b = 0; b < syn_nbs[a]->length; b++)
1059 if (best[i].wordi == syn_nbs[a]->best[b].wordi) {
1060 best[i].probability += syn_nbs[a]->best[b].probability;
1061 if (syn_nbs[a]->best[b].activation > 0.25)
1062 best[i].position |= 1 << wpos;
1063 if (syn_nbs[a]->best[b].activation > best[i].activation) {
1064 best[i].activation = syn_nbs[a]->best[b].activation;
1065 }
1066 }
1067 }
1068 }
1069 qsort(best, found_index, sizeof(collocator), cmp_activation);
1070 } else if (sort_by == 2) { // single window position
1071 for (a = 1; a < syn_threads; a++) {
1072 for (b = 0; b < syn_nbs[a]->length; b++) {
1073 for (c = 0; c < MAX_NEIGHBOURS; c++) {
1074 if (syn_nbs[a]->best[b].activation > best[c].activation) {
1075 for (d = MAX_NEIGHBOURS - 1; d > c; d--) {
1076 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001077 }
1078 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
Marc Kupietz969cab92019-08-05 11:13:42 +02001079 best[c].position = 1 << (-syn_nbs[a]->best[b].position + window - (syn_nbs[a]->best[b].position < 0 ? 1 : 0));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001080 break;
1081 }
1082 }
1083 }
1084 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001085 } else { // sort by mean p
1086 for (a = 1; a < syn_threads; a++) {
1087 for (b = 0; b < syn_nbs[a]->length; b++) {
1088 for (c = 0; c < MAX_NEIGHBOURS; c++) {
1089 if (target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
1090 for (d = MAX_NEIGHBOURS - 1; d > c; d--) {
1091 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001092 }
1093 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
Marc Kupietz969cab92019-08-05 11:13:42 +02001094 best[c].position = (1 << 2 * window) - 1; // syn_nbs[a]->pos[b];
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001095 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
1096 break;
1097 }
1098 }
1099 }
1100 }
1101 }
1102 array = newAV();
Marc Kupietz969cab92019-08-05 11:13:42 +02001103 for (a = 0, i = 0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001104 long long c = best[a].wordi;
Marc Kupietz969cab92019-08-05 11:13:42 +02001105 /*
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001106 if (dedupe) {
1107 int filtered=0;
1108 for (j=0; j<i; j++)
1109 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
1110 strcasestr(chosen[j], &vocab[c * max_w])) {
1111 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
1112 filtered = 1;
1113 }
1114 if(filtered)
1115 continue;
1116 }
1117*/
Marc Kupietz969cab92019-08-05 11:13:42 +02001118 chosen[i++] = c;
1119 HV *hash = newHV();
1120 SV *word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
1121 AV *heat = newAV();
1122 if (latin_enc == 0) SvUTF8_on(word);
1123 hv_store(hash, "word", strlen("word"), word, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001124 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
1125 hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0);
1126 hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0);
1127 hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0);
Marc Kupietz969cab92019-08-05 11:13:42 +02001128 hv_store(hash, "max", strlen("max"), newSVnv(best[a].max_activation), 0); // newSVnv(target_sums[best[a].wordi]), 0);
1129 hv_store(hash, "overall", strlen("overall"), newSVnv(best[a].activation_sum / total_activation), 0); // newSVnv(target_sums[best[a].wordi]), 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001130 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietz969cab92019-08-05 11:13:42 +02001131 best[a].heat[5] = 0;
1132 for (i = 10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i]));
1133 hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV *)heat), 0);
1134 av_push(array, newRV_noinc((SV *)hash));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001135 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001136 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV *)array), 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001137 }
1138end:
Marc Kupietz969cab92019-08-05 11:13:42 +02001139 free(best);
1140 return newRV_noinc((SV *)result);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001141}
1142
1143int dump_vecs(char *fname) {
Marc Kupietz969cab92019-08-05 11:13:42 +02001144 long i, j;
1145 FILE *f;
1146 /* if(words>100000)
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001147 words=100000;
1148*/
Marc Kupietz969cab92019-08-05 11:13:42 +02001149 if ((f = fopen(fname, "w")) == NULL) {
1150 fprintf(stderr, "cannot open %s for writing\n", fname);
1151 return (-1);
1152 }
1153 fprintf(f, "%lld %lld\n", words, size);
1154 for (i = 0; i < words; i++) {
1155 fprintf(f, "%s ", &vocab[i * max_w]);
1156 for (j = 0; j < size - 1; j++)
1157 fprintf(f, "%f ", M[i * size + j]);
1158 fprintf(f, "%f\n", M[i * size + j]);
1159 }
1160 fclose(f);
1161 return (0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001162}
1163
1164int dump_for_numpy(char *fname) {
Marc Kupietz969cab92019-08-05 11:13:42 +02001165 long i, j;
1166 FILE *f;
Marc Kupietzc0d41872021-02-25 16:33:22 +01001167 int max = words; // 300000;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001168
Marc Kupietz969cab92019-08-05 11:13:42 +02001169 if ((f = fopen(fname, "w")) == NULL) {
1170 fprintf(stderr, "cannot open %s for writing\n", fname);
1171 return (-1);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001172 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001173 for (i = 0; i < max; i++) {
1174 for (j = 0; j < size - 1; j++)
1175 fprintf(f, "%f\t", M[i * size + j]);
1176 fprintf(f, "%f\n", M[i * size + j]);
1177 printf("%s\r\n", &vocab[i * max_w]);
1178 }
1179 if (merged_end > 0) {
1180 for (i = 0; i < max; i++) {
1181 for (j = 0; j < size - 1; j++)
1182 fprintf(f, "%f\t", M[(merged_end + i) * size + j]);
1183 fprintf(f, "%f\n", M[(merged_end + i) * size + j]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001184 printf("_%s\r\n", &vocab[i * max_w]);
1185 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001186 }
1187 fclose(f);
1188 return (0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001189}
Marc Kupietz043db152023-11-05 17:47:53 +01001190
1191unsigned long getVocabSize() {
1192 return (unsigned long) words;
1193}