blob: 5b8915f5a7aa69ce3856a03303c9f3636168bef1 [file] [log] [blame]
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001#include <collocatordb.h>
Marc Kupietz969cab92019-08-05 11:13:42 +02002#include <malloc.h>
3#include <math.h>
4#include <pthread.h>
5#include <stdio.h>
Marc Kupietzc0d41872021-02-25 16:33:22 +01006#include <stdlib.h>
Marc Kupietz969cab92019-08-05 11:13:42 +02007#include <string.h>
8#include <sys/mman.h>
Marc Kupietzf11d20c2019-08-02 15:42:04 +02009
10#define max_size 2000
11#define max_w 50
12#define MAX_NEIGHBOURS 1000
13#define MAX_WORDS -1
14#define MAX_THREADS 100
15#define MAX_CC 50
16#define EXP_TABLE_SIZE 1000
17#define MAX_EXP 6
18#define MIN_RESP 0.50
19
20//the thread function
21void *connection_handler(void *);
22
23typedef struct {
Marc Kupietz969cab92019-08-05 11:13:42 +020024 long long wordi;
25 long position;
26 float activation;
27 float average;
28 float cprobability; // column wise probability
29 float cprobability_sum;
30 float probability;
31 float activation_sum;
32 float max_activation;
33 float heat[16];
Marc Kupietzf11d20c2019-08-02 15:42:04 +020034} collocator;
35
36typedef struct {
Marc Kupietz969cab92019-08-05 11:13:42 +020037 collocator *best;
38 int length;
Marc Kupietzf11d20c2019-08-02 15:42:04 +020039} knn;
Marc Kupietz969cab92019-08-05 11:13:42 +020040
Marc Kupietzf11d20c2019-08-02 15:42:04 +020041typedef struct {
42 long long wordi[MAX_NEIGHBOURS];
43 char sep[MAX_NEIGHBOURS];
44 int length;
45} wordlist;
46
47typedef struct {
48 long cutoff;
49 wordlist *wl;
Marc Kupietz969cab92019-08-05 11:13:42 +020050 char *token;
51 int N;
52 long from;
53 unsigned long upto;
Marc Kupietzf11d20c2019-08-02 15:42:04 +020054 collocator *best;
55 float *target_sums;
56 float *window_sums;
57 float threshold;
58} knnpars;
59
60typedef struct {
61 uint32_t index;
62 float value;
63} sparse_t;
64
65typedef struct {
66 uint32_t len;
67 sparse_t nbr[100];
68} profile_t;
69
Marc Kupietz969cab92019-08-05 11:13:42 +020070float *M, *M2 = 0L, *syn1neg_window, *expTable;
Marc Kupietzf11d20c2019-08-02 15:42:04 +020071float *window_sums;
72char *vocab;
73char *garbage = NULL;
74COLLOCATORDB *cdb = NULL;
75profile_t *sprofiles = NULL;
76size_t sprofiles_qty = 0;
77
78long long words, size, merged_end;
79long long merge_words = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +020080int num_threads = 20;
81int latin_enc = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +020082int window;
83
84/* load collocation profiles if file exists */
85int load_sprofiles(char *vecsname) {
86 char *basename = strdup(vecsname);
87 char *pos = strstr(basename, ".vecs");
Marc Kupietz969cab92019-08-05 11:13:42 +020088 if (pos)
89 *pos = 0;
90
Marc Kupietzf11d20c2019-08-02 15:42:04 +020091 char binsprofiles_fname[256];
92 strcpy(binsprofiles_fname, basename);
Marc Kupietz969cab92019-08-05 11:13:42 +020093 strcat(binsprofiles_fname, ".sprofiles.bin");
Marc Kupietzf11d20c2019-08-02 15:42:04 +020094 FILE *fp = fopen(binsprofiles_fname, "rb");
95 if (fp == NULL) {
96 printf("Collocation profiles %s not found. No problem.\n", binsprofiles_fname);
97 return 0;
98 }
99 fseek(fp, 0L, SEEK_END);
100 size_t sz = ftell(fp);
101 fclose(fp);
102
103 int fd = open(binsprofiles_fname, O_RDONLY);
Marc Kupietz969cab92019-08-05 11:13:42 +0200104 sprofiles = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200105 if (sprofiles == MAP_FAILED) {
106 close(fd);
107 fprintf(stderr, "Cannot mmap %s\n", binsprofiles_fname);
108 sprofiles = NULL;
109 return 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200110 } else {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200111 sprofiles_qty = sz / sizeof(profile_t);
112 fprintf(stderr, "Successfully mmaped %s containing similar profiles for %ld word forms.\n", binsprofiles_fname, sprofiles_qty);
113 }
114 return 1;
115}
116
Marc Kupietzc0d41872021-02-25 16:33:22 +0100117char *removeExtension(char* myStr) {
118 char *retStr;
119 char *lastExt;
120 if (myStr == NULL) return NULL;
121 if ((retStr = malloc (strlen (myStr) + 1)) == NULL) return NULL;
122 strcpy (retStr, myStr);
123 lastExt = strrchr (retStr, '.');
124 if (lastExt != NULL)
125 *lastExt = '\0';
126 return retStr;
127}
128
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200129int init_net(char *file_name, char *net_name, int latin, int do_open_cdb) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200130 FILE *f, *binvecs, *binwords;
Marc Kupietz969cab92019-08-05 11:13:42 +0200131 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz59865a92021-03-11 17:16:51 +0100132 long long a, b;
Marc Kupietz969cab92019-08-05 11:13:42 +0200133 float len;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200134 double val;
135
Marc Kupietzc0d41872021-02-25 16:33:22 +0100136 char binvecs_fname[1024], binwords_fname[1024];
137
138 if (strstr(file_name, ".txt")) {
139 strcpy(binwords_fname, removeExtension(file_name));
140 } else {
141 strcpy(binwords_fname, file_name);
142 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200143 strcat(binwords_fname, ".words");
144 strcpy(binvecs_fname, file_name);
145 strcat(binvecs_fname, ".vecs");
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200146
147 latin_enc = latin;
148 f = fopen(file_name, "rb");
149 if (f == NULL) {
150 printf("Input file %s not found\n", file_name);
151 return -1;
152 }
153 fscanf(f, "%lld", &words);
Marc Kupietz969cab92019-08-05 11:13:42 +0200154 if (MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200155 fscanf(f, "%lld", &size);
Marc Kupietz969cab92019-08-05 11:13:42 +0200156 if ((binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200157 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietz969cab92019-08-05 11:13:42 +0200158 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
159 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
160 if (M == NULL) {
161 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
162 return -1;
163 }
164 if (strstr(file_name, ".txt")) {
Marc Kupietzc0d41872021-02-25 16:33:22 +0100165 printf("%lld words in ascii vector file with vector size %lld\n", words, size);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200166 for (b = 0; b < words; b++) {
167 a = 0;
168 while (1) {
169 vocab[b * max_w + a] = fgetc(f);
170 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
171 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
172 }
173 vocab[b * max_w + a] = 0;
174 len = 0;
175 for (a = 0; a < size; a++) {
176 fscanf(f, "%lf", &val);
177 M[a + b * size] = val;
178 len += val * val;
Marc Kupietz969cab92019-08-05 11:13:42 +0200179 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200180 len = sqrt(len);
181 for (a = 0; a < size; a++) M[a + b * size] /= len;
182 }
183 } else {
184 for (b = 0; b < words; b++) {
185 a = 0;
186 while (1) {
187 vocab[b * max_w + a] = fgetc(f);
188 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
189 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
190 }
191 vocab[b * max_w + a] = 0;
192 fread(&M[b * size], sizeof(float), size, f);
193 len = 0;
194 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
195 len = sqrt(len);
196 for (a = 0; a < size; a++) M[a + b * size] /= len;
197 }
198 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200199 if ((binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
200 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
201 fclose(binvecs);
202 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
203 fclose(binwords);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200204 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200205 }
206 if ((binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
207 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
208 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
209 if (M == MAP_FAILED || vocab == MAP_FAILED) {
210 close(binvecs_fd);
211 close(binwords_fd);
212 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
213 exit(-1);
214 }
215 } else {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200216 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
217 exit(-1);
Marc Kupietz969cab92019-08-05 11:13:42 +0200218 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200219 fclose(f);
220
Marc Kupietz969cab92019-08-05 11:13:42 +0200221 if (net_name && strlen(net_name) > 0) {
222 if ((net_fd = open(net_name, O_RDONLY)) >= 0) {
223 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200224 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
225 // munmap(M, sizeof(float) * words * size);
226 M2 = mmap(0, sizeof(float) * words * size + sizeof(float) * 2 * window * size * words, PROT_READ, MAP_SHARED, net_fd, 0);
227 if (M2 == MAP_FAILED) {
228 close(net_fd);
229 fprintf(stderr, "Cannot mmap %s\n", net_name);
230 exit(-1);
231 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200232 syn1neg_window = M2 + words * size;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200233 } else {
234 fprintf(stderr, "Cannot open %s\n", net_name);
235 exit(-1);
236 }
237 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
238
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200239 if (do_open_cdb) {
240 char collocatordb_name[2048];
241 strcpy(collocatordb_name, net_name);
242 char *ext = rindex(collocatordb_name, '.');
243 if (ext) {
244 strcpy(ext, ".rocksdb");
245 if (access(collocatordb_name, R_OK) == 0) {
246 *ext = 0;
247 fprintf(stderr, "Opening collocator DB %s\n", collocatordb_name);
248 cdb = open_collocatordb(collocatordb_name);
Marc Kupietzc0d41872021-02-25 16:33:22 +0100249 } else {
250 fprintf(stderr, "Cannot open collocator DB %s\n", collocatordb_name);
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200251 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200252 }
253 }
254 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200255
Marc Kupietz969cab92019-08-05 11:13:42 +0200256 expTable = (float *)malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
257 for (i = 0; i < EXP_TABLE_SIZE; i++) {
258 expTable[i] = exp((i / (float)EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
259 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
260 }
261 window_sums = malloc(sizeof(float) * (window + 1) * 2);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200262
263 return 0;
264}
265
Marc Kupietz969cab92019-08-05 11:13:42 +0200266long mergeVectors(char *file_name) {
Marc Kupietz59865a92021-03-11 17:16:51 +0100267 FILE *f;
268 int binwords_fd, binvecs_fd;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200269 float *merge_vecs;
270 char *merge_vocab;
Marc Kupietz969cab92019-08-05 11:13:42 +0200271 /* long long merge_words, merge_size; */
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200272 long long merge_size;
273
Marc Kupietz969cab92019-08-05 11:13:42 +0200274 char binvecs_fname[256], binwords_fname[256];
Marc Kupietzc0d41872021-02-25 16:33:22 +0100275
276
Marc Kupietz969cab92019-08-05 11:13:42 +0200277 strcpy(binwords_fname, file_name);
278 strcat(binwords_fname, ".words");
279 strcpy(binvecs_fname, file_name);
280 strcat(binvecs_fname, ".vecs");
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200281
282 f = fopen(file_name, "rb");
283 if (f == NULL) {
284 printf("Input file %s not found\n", file_name);
Marc Kupietz59865a92021-03-11 17:16:51 +0100285 exit(-1);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200286 }
287 fscanf(f, "%lld", &merge_words);
288 fscanf(f, "%lld", &merge_size);
Marc Kupietz969cab92019-08-05 11:13:42 +0200289 if (merge_size != size) {
290 fprintf(stderr, "vectors must have the same length\n");
291 exit(-1);
292 }
293 if ((binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
294 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
295 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200296 if (merge_vecs == NULL || merge_vocab == NULL) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200297 close(binvecs_fd);
298 close(binwords_fd);
299 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
300 exit(-1);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200301 }
302 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
303 read(binwords_fd, merge_vocab, merge_words * max_w);
Marc Kupietz969cab92019-08-05 11:13:42 +0200304 } else {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200305 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
306 exit(-1);
Marc Kupietz969cab92019-08-05 11:13:42 +0200307 }
308 printf("Successfully reallocated memory\nMerging...\n");
309 fflush(stdout);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200310 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
311 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
312 munmap(M, words * size * sizeof(float));
313 munmap(vocab, words * max_w);
314 M = merge_vecs;
315 vocab = merge_vocab;
316 merged_end = merge_words;
317 words += merge_words;
318 fclose(f);
Marc Kupietz969cab92019-08-05 11:13:42 +0200319 printf("merged_end: %lld, words: %lld\n", merged_end, words);
320 //printBiggestMergedDifferences();
321 return ((long)merged_end);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200322}
323
324void filter_garbage() {
325 long i;
326 unsigned char *w, previous, c;
327 garbage = malloc(words);
328 memset(garbage, 0, words);
329 for (i = 0; i < words; i++) {
Marc Kupietz59865a92021-03-11 17:16:51 +0100330 w = (unsigned char *) vocab + i * max_w;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200331 previous = 0;
Marc Kupietz59865a92021-03-11 17:16:51 +0100332 if (strncmp("quot", (const char *)w, 4) == 0) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200333 garbage[i] = 1;
334 // printf("Gargabe: %s\n", vocab + i * max_w);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200335 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200336 while ((c = *w++) && !garbage[i]) {
337 if (((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200338 (previous == '-' && (c & 32)) ||
Marc Kupietz969cab92019-08-05 11:13:42 +0200339 (previous == 0xc2 && (c == 0xa4 || c == 0xb6)) ||
340 (previous == 'q' && c == 'u' && *(w) == 'o' && *(w + 1) == 't') || /* quot */
341 c == '<') {
342 garbage[i] = 1;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200343 continue;
344 }
345 previous = c;
346 }
347 }
348 }
349 return;
350}
351
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200352knn *simpleGetCollocators(int word, int number, long cutoff, int *result) {
353 knnpars *pars = calloc(sizeof(knnpars), 1);
Marc Kupietz59865a92021-03-11 17:16:51 +0100354 float *target_sums = NULL;
355 float *my_window_sums = malloc(sizeof(float) * (window + 1) * 2);
Marc Kupietz969cab92019-08-05 11:13:42 +0200356 pars->cutoff = (cutoff ? cutoff : 300000);
Marc Kupietz59865a92021-03-11 17:16:51 +0100357 long a;
Marc Kupietz969cab92019-08-05 11:13:42 +0200358 for (a = 0; a < cutoff; a++)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200359 target_sums[a] = 0;
360 pars->target_sums = target_sums;
Marc Kupietz59865a92021-03-11 17:16:51 +0100361 pars->window_sums = my_window_sums;
Marc Kupietz969cab92019-08-05 11:13:42 +0200362 pars->N = (number ? number : 20);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200363 pars->from = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200364 pars->upto = window * 2 - 1;
365 knn *syn_nbs = NULL; // = (knn*) getCollocators(pars);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200366 free(pars);
Marc Kupietz59865a92021-03-11 17:16:51 +0100367 free(my_window_sums);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200368 free(target_sums);
369 return syn_nbs;
370}
371
372void *getCollocators(void *args) {
373 knnpars *pars = args;
Marc Kupietz969cab92019-08-05 11:13:42 +0200374 int N = pars->N;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200375
376 int cc = pars->wl->wordi[0];
Marc Kupietz969cab92019-08-05 11:13:42 +0200377 knn *nbs = NULL;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200378 long window_layer_size = size * window * 2;
Marc Kupietz59865a92021-03-11 17:16:51 +0100379 long a, b, c, d, window_offset, target, max_target = 0, maxmax_target;
Marc Kupietz969cab92019-08-05 11:13:42 +0200380 float f, max_f, maxmax_f;
381 float *target_sums = NULL, worstbest, wpos_sum;
382 collocator *best;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200383
Marc Kupietz969cab92019-08-05 11:13:42 +0200384 if (M2 == NULL || cc == -1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200385 return NULL;
386
Marc Kupietz969cab92019-08-05 11:13:42 +0200387 a = posix_memalign((void **)&target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200388 memset(target_sums, 0, pars->cutoff * sizeof(float));
Marc Kupietz969cab92019-08-05 11:13:42 +0200389 best = malloc((N > 200 ? N : 200) * sizeof(collocator));
390 memset(best, 0, (N > 200 ? N : 200) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200391 worstbest = pars->threshold;
392
393 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200394 target_sums[b] = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200395 for (b = 0; b < N; b++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200396 best[b].wordi = -1;
397 best[b].probability = 1;
398 best[b].activation = worstbest;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200399 }
400
401 d = cc;
402 maxmax_f = -1;
403 maxmax_target = 0;
404
405 for (a = pars->from; a < pars->upto; a++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200406 if (a >= window)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200407 a++;
408 wpos_sum = 0;
409 printf("window pos: %ld\n", a);
410 if (a != window) {
411 max_f = -1;
412 window_offset = a * size;
413 if (a > window)
414 window_offset -= size;
Marc Kupietz969cab92019-08-05 11:13:42 +0200415 for (target = 0; target < pars->cutoff; target++) {
416 if (garbage && garbage[target]) continue;
417 if (target == d)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200418 continue;
419 f = 0;
420 for (c = 0; c < size; c++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200421 f += M2[d * size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200422 if (f < -MAX_EXP)
423 continue;
424 else if (f > MAX_EXP)
425 continue;
426 else
Marc Kupietz969cab92019-08-05 11:13:42 +0200427 f = expTable[(int)((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200428 wpos_sum += f;
429
430 target_sums[target] += f;
Marc Kupietz969cab92019-08-05 11:13:42 +0200431 if (f > worstbest) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200432 for (b = 0; b < N; b++) {
433 if (f > best[b].activation) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200434 memmove(best + b + 1, best + b, (N - b - 1) * sizeof(collocator));
435 best[b].activation = f;
436 best[b].wordi = target;
437 best[b].position = window - a;
438 break;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200439 }
440 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200441 if (b == N - 1)
442 worstbest = best[N - 1].activation;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200443 }
444 }
Marc Kupietz59865a92021-03-11 17:16:51 +0100445 printf("%ld %.2f\n", max_target, max_f);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200446 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
Marc Kupietz969cab92019-08-05 11:13:42 +0200447 if (max_f > maxmax_f) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200448 maxmax_f = max_f;
449 maxmax_target = max_target;
450 }
451 for (b = 0; b < N; b++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200452 if (best[b].position == window - a)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200453 best[b].cprobability = best[b].activation / wpos_sum;
454 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200455 printf("\x1b[1m%s\x1b[0m ", &vocab[d * max_w]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200456 }
457 pars->window_sums[a] = wpos_sum;
458 }
459 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200460 pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200461
462 free(target_sums);
Marc Kupietz969cab92019-08-05 11:13:42 +0200463 for (b = 0; b < N && best[b].wordi >= 0; b++)
464 ;
Marc Kupietz59865a92021-03-11 17:16:51 +0100465 // THIS LOOP IS NEEDED (b...)
Marc Kupietz969cab92019-08-05 11:13:42 +0200466 // printf("%d: best syn: %s %.2f %.5f\n", b, &vocab[best[b].wordi*max_w], best[b].activation, best[b].probability);
467 // printf("\n");
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200468 nbs = malloc(sizeof(knn));
Marc Kupietz969cab92019-08-05 11:13:42 +0200469 nbs->best = best;
470 nbs->length = b - 1;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200471 pthread_exit(nbs);
472}
473
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200474float getOutputWeight(int hidden, long target, int window_position) {
475 const long window_layer_size = size * window * 2;
476 int a;
477
478 if (window_position == 0 || window_position > window || window_position < -window) {
479 fprintf(stderr, "window_position: %d - assert: -%d <= window_position <= %d && window_position != 0 failed.\n", window_position, window, window);
480 exit(-1);
481 }
482
483 if (hidden >= size) {
Marc Kupietz59865a92021-03-11 17:16:51 +0100484 fprintf(stderr, "hidden: %d - assert: hidden < %lld failed.\n", hidden, size);
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200485 exit(-1);
486 }
487
488 if (target >= words) {
Marc Kupietz59865a92021-03-11 17:16:51 +0100489 fprintf(stderr, "target: %ld - assert: target < %lld failed.\n", target, words);
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200490 exit(-1);
491 }
492
493 a = window_position + window;
494 if (a > window) {
495 --a;
496 }
497 long window_offset = a * size;
498 return syn1neg_window[target * window_layer_size + window_offset + hidden];
499}
500
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200501AV *getVecs(AV *array) {
502 int i, b;
503 AV *result = newAV();
Marc Kupietz969cab92019-08-05 11:13:42 +0200504 for (i = 0; i <= av_len(array); i++) {
505 SV **elem = av_fetch(array, i, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200506 if (elem != NULL) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200507 long j = (long)SvNV(*elem);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200508 AV *vector = newAV();
509 for (b = 0; b < size; b++) {
510 av_push(vector, newSVnv(M[b + j * size]));
511 }
512 av_push(result, newRV_noinc(vector));
513 }
514 }
515 return result;
516}
517
518char *getSimilarProfiles(long node) {
519 int i;
520 char buffer[120000];
521 char pair_buffer[2048];
Marc Kupietz969cab92019-08-05 11:13:42 +0200522 buffer[0] = '[';
523 buffer[1] = 0;
524 if (node >= sprofiles_qty) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200525 printf("Not available in precomputed profile\n");
Marc Kupietz969cab92019-08-05 11:13:42 +0200526 return (strdup("[{\"w\":\"not available\", \"v\":0}]\n"));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200527 }
528
529 printf("******* %s ******\n", &vocab[max_w * node]);
Marc Kupietz969cab92019-08-05 11:13:42 +0200530
531 for (i = 0; i < 100 && i < sprofiles[node].len; i++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200532 sprintf(pair_buffer, "{\"w\":\"%s\", \"v\":%f},", &vocab[max_w * (sprofiles[node].nbr[i].index)], sprofiles[node].nbr[i].value);
533 strcat(buffer, pair_buffer);
534 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200535 buffer[strlen(buffer) - 1] = ']';
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200536 strcat(buffer, "\n");
Marc Kupietz59865a92021-03-11 17:16:51 +0100537 printf("%s", buffer);
Marc Kupietz969cab92019-08-05 11:13:42 +0200538 return (strdup(buffer));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200539}
540
541char *getClassicCollocators(long node) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200542 char *res = (cdb ? strdup(get_collocators_as_json(cdb, node)) : "[]");
543 return res;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200544}
545
546wordlist *getTargetWords(char *st1, int search_backw) {
547 wordlist *wl = malloc(sizeof(wordlist));
Marc Kupietz59865a92021-03-11 17:16:51 +0100548 char st[100][max_size];
Marc Kupietz969cab92019-08-05 11:13:42 +0200549 long a, b = 0, c = 0, cn = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200550
551 while (1) {
552 st[cn][b] = st1[c];
553 b++;
554 c++;
555 st[cn][b] = 0;
556 if (st1[c] == 0) break;
Marc Kupietzc0d41872021-02-25 16:33:22 +0100557 if (st1[c] == ' ' /*|| st1[c] == '-'*/) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200558 b = 0;
559 c++;
560 }
561 }
562 cn++;
563 for (a = 0; a < cn; a++) {
564 if (search_backw) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200565 for (b = words - 1; b >= (merge_words ? merge_words : 0) && strcmp(&vocab[b * max_w], st[a]) != 0; b--)
566 ;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200567 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200568 for (b = 0; b < (merge_words ? merge_words : words) && strcmp(&vocab[b * max_w], st[a]) != 0; b++)
569 ;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200570 }
571 if (b == words) b = -1;
572 wl->wordi[a] = b;
573 if (b == -1) {
574 fprintf(stderr, "Out of dictionary word!\n");
575 cn--;
576 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200577 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 +0200578 }
579 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200580 wl->length = cn;
581 return (wl);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200582}
583
Marc Kupietzcb43e492019-12-03 10:07:53 +0100584long getWordNumber(char *word) {
585 wordlist *wl = getTargetWords(word, 0);
586 if(wl->length > 0)
587 return(wl->wordi[0]);
588 return(0);
589}
590
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200591float get_distance(long b, long c) {
592 long a;
593 float dist = 0;
594 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + b * size];
595 return dist;
596}
597
Marc Kupietz969cab92019-08-05 11:13:42 +0200598char *getBiggestMergedDifferences() {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200599 static char *result = NULL;
Marc Kupietz59865a92021-03-11 17:16:51 +0100600 float dist;
601 long long a, c;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200602 int N = 1000;
603
Marc Kupietz969cab92019-08-05 11:13:42 +0200604 if (merged_end == 0)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200605 result = "[]";
Marc Kupietz969cab92019-08-05 11:13:42 +0200606
607 if (result != NULL)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200608 return result;
609
610 printf("Looking for biggest distances between main and merged vectors ...\n");
611 collocator *best;
612 best = malloc(N * sizeof(collocator));
613 memset(best, 0, N * sizeof(collocator));
614
Marc Kupietz969cab92019-08-05 11:13:42 +0200615 float worstbest = 1000000;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200616
617 for (a = 0; a < N; a++) best[a].activation = worstbest;
618
619 for (c = 0; c < 500000; c++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200620 if (garbage && garbage[c]) continue;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200621 dist = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200622 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + (c + merged_end) * size];
623 if (dist < worstbest) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200624 for (a = 0; a < N; a++) {
625 if (dist < best[a].activation) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200626 memmove(best + a + 1, best + a, (N - a - 1) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200627 best[a].activation = dist;
628 best[a].wordi = c;
629 break;
630 }
631 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200632 worstbest = best[N - 1].activation;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200633 }
634 }
635
Marc Kupietz969cab92019-08-05 11:13:42 +0200636 result = malloc(N * max_w);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200637 char *p = result;
Marc Kupietz969cab92019-08-05 11:13:42 +0200638 *p++ = '[';
639 *p = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200640 for (a = 0; a < N; a++) {
Marc Kupietz59865a92021-03-11 17:16:51 +0100641 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 +0200642 }
643 *--p = ']';
Marc Kupietz969cab92019-08-05 11:13:42 +0200644 return (result);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200645}
646
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200647float cos_similarity(long b, long c) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200648 float dist = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200649 long a;
Marc Kupietz969cab92019-08-05 11:13:42 +0200650 for (a = 0; a < size; a++) dist += M[b * size + a] * M[c * size + a];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200651 return dist;
652}
653
654char *cos_similarity_as_json(char *w1, char *w2) {
655 wordlist *a, *b;
656 float res;
657 a = getTargetWords(w1, 0);
658 b = getTargetWords(w2, 0);
Marc Kupietz969cab92019-08-05 11:13:42 +0200659 if (a == NULL || b == NULL || a->length != 1 || b->length != 1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200660 res = -1;
661 else
662 res = cos_similarity(a->wordi[0], b->wordi[0]);
663 fprintf(stderr, "a: %lld b: %lld res:%f\n", a->wordi[0], b->wordi[0], res);
664 char *json = malloc(16);
665 sprintf(json, "%.5f", res);
666 return json;
667}
668
669void *_get_neighbours(void *arg) {
670 knnpars *pars = arg;
Marc Kupietz969cab92019-08-05 11:13:42 +0200671 int N = pars->N;
672 long from = pars->from;
673 unsigned long upto = pars->upto;
Marc Kupietz59865a92021-03-11 17:16:51 +0100674 char *sep;
Marc Kupietz969cab92019-08-05 11:13:42 +0200675 float dist, len, vec[max_size];
Marc Kupietz59865a92021-03-11 17:16:51 +0100676 long long a, b, c, cn, *bi;
Marc Kupietz969cab92019-08-05 11:13:42 +0200677 knn *nbs = NULL;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200678 wordlist *wl = pars->wl;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200679
Marc Kupietz969cab92019-08-05 11:13:42 +0200680 collocator *best = pars->best;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200681
Marc Kupietz969cab92019-08-05 11:13:42 +0200682 float worstbest = -1;
683
684 for (a = 0; a < N; a++) best[a].activation = 0;
685 a = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200686 bi = wl->wordi;
Marc Kupietz969cab92019-08-05 11:13:42 +0200687 cn = wl->length;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200688 sep = wl->sep;
Marc Kupietz969cab92019-08-05 11:13:42 +0200689 b = bi[0];
Marc Kupietz969cab92019-08-05 11:13:42 +0200690 if (b == -1) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200691 goto end;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200692 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200693 for (a = 0; a < size; a++) vec[a] = 0;
694 for (b = 0; b < cn; b++) {
695 if (bi[b] == -1) continue;
696 if (b > 0 && sep[b - 1] == '-')
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200697 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
698 else
699 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietz969cab92019-08-05 11:13:42 +0200700 }
701 len = 0;
702 for (a = 0; a < size; a++) len += vec[a] * vec[a];
703 len = sqrt(len);
704 for (a = 0; a < size; a++) vec[a] /= len;
705 for (a = 0; a < N; a++) best[a].activation = -1;
706 for (c = from; c < upto; c++) {
707 if (garbage && garbage[c]) continue;
708 a = 0;
709 // do not skip taget word
710 // for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
711 // if (a == 1) continue;
712 dist = 0;
713 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
714 if (dist > worstbest) {
715 for (a = 0; a < N; a++) {
716 if (dist > best[a].activation) {
717 memmove(best + a + 1, best + a, (N - a - 1) * sizeof(collocator));
718 best[a].activation = dist;
719 best[a].wordi = c;
720 break;
721 }
722 }
723 worstbest = best[N - 1].activation;
724 }
725 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200726
727end:
Marc Kupietz969cab92019-08-05 11:13:42 +0200728 pthread_exit(nbs);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200729}
730
Marc Kupietz969cab92019-08-05 11:13:42 +0200731int cmp_activation(const void *a, const void *b) {
732 float fb = ((collocator *)a)->activation;
733 float fa = ((collocator *)b)->activation;
734 return (fa > fb) - (fa < fb);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200735}
736
Marc Kupietz969cab92019-08-05 11:13:42 +0200737int cmp_probability(const void *a, const void *b) {
738 float fb = ((collocator *)a)->probability;
739 float fa = ((collocator *)b)->probability;
740 return (fa > fb) - (fa < fb);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200741}
742
Marc Kupietz969cab92019-08-05 11:13:42 +0200743char *getPosWiseW2VCollocatorsAsTsv(char *word, long maxPerPos, long cutoff, float threshold) {
744 HV *result = newHV();
Marc Kupietz59865a92021-03-11 17:16:51 +0100745 float *target_sums = NULL;
746 long a, b;
Marc Kupietz969cab92019-08-05 11:13:42 +0200747 knn *para_nbs[MAX_THREADS];
748 knn *syn_nbs[MAX_THREADS];
749 knnpars pars[MAX_THREADS];
750 pthread_t *pt = (pthread_t *)malloc((num_threads + 1) * sizeof(pthread_t));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200751 wordlist *wl;
Marc Kupietz969cab92019-08-05 11:13:42 +0200752 int syn_threads = (M2 ? window * 2 : 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200753 int search_backw = 0;
754 collocator *best = NULL;
Marc Kupietz969cab92019-08-05 11:13:42 +0200755 posix_memalign((void **)&best, 128, 10 * (maxPerPos >= 200 ? maxPerPos : 200) * sizeof(collocator));
756 memset(best, 0, (maxPerPos >= 200 ? maxPerPos : 200) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200757
Marc Kupietz969cab92019-08-05 11:13:42 +0200758 if (cutoff < 1 || cutoff > words)
759 cutoff = words;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200760
761 wl = getTargetWords(word, search_backw);
Marc Kupietz969cab92019-08-05 11:13:42 +0200762 if (wl == NULL || wl->length < 1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200763 return "";
764
Marc Kupietz969cab92019-08-05 11:13:42 +0200765 a = posix_memalign((void **)&target_sums, 128, cutoff * sizeof(float));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200766 memset(target_sums, 0, cutoff * sizeof(float));
767
768 printf("Starting %d threads\n", syn_threads);
769 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200770 for (a = 0; a < syn_threads; a++) {
771 pars[a].cutoff = cutoff;
772 pars[a].target_sums = target_sums;
773 pars[a].window_sums = window_sums;
774 pars[a].wl = wl;
775 pars[a].N = maxPerPos;
776 pars[a].threshold = threshold;
777 pars[a].from = a;
778 pars[a].upto = a + 1;
779 pthread_create(&pt[a], NULL, getCollocators, (void *)&pars[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200780 }
781 printf("Waiting for syn threads to join\n");
782 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200783 for (a = 0; a < syn_threads; a++) pthread_join(pt[a], (void *)&syn_nbs[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200784 printf("Syn threads joint\n");
785 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200786 result = malloc(maxPerPos * 80 * syn_threads);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200787 char *p = result;
788 *p = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200789 for (a = syn_threads - 1; a >= 0; a--) {
790 for (b = 0; b < syn_nbs[a]->length; b++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200791 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);
792 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200793 }
794 return (result);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200795}
796
797SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe, int no_similar_profiles) {
798 HV *result = newHV();
Marc Kupietz59865a92021-03-11 17:16:51 +0100799 float *target_sums = NULL;
Marc Kupietz969cab92019-08-05 11:13:42 +0200800 long a, b, c, d, slice;
801 knn *para_nbs[MAX_THREADS];
802 knn *syn_nbs[MAX_THREADS];
803 knnpars pars[MAX_THREADS];
804 pthread_t *pt = (pthread_t *)malloc((num_threads + 1) * sizeof(pthread_t));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200805 wordlist *wl;
Marc Kupietz969cab92019-08-05 11:13:42 +0200806 int syn_threads = (M2 ? window * 2 : 0);
807 int para_threads = (no_similar_profiles ? 0 : num_threads - syn_threads);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200808
809 collocator *best = NULL;
Marc Kupietz969cab92019-08-05 11:13:42 +0200810 posix_memalign((void **)&best, 128, 10 * (N >= 200 ? N : 200) * sizeof(collocator));
811 memset(best, 0, (N >= 200 ? N : 200) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200812
Marc Kupietz969cab92019-08-05 11:13:42 +0200813 if (N > MAX_NEIGHBOURS) N = MAX_NEIGHBOURS;
814
815 if (cutoff < 1 || cutoff > words)
816 cutoff = words;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200817
818 wl = getTargetWords(st1, search_backw);
Marc Kupietz969cab92019-08-05 11:13:42 +0200819 if (wl == NULL || wl->length < 1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200820 goto end;
821
Marc Kupietz969cab92019-08-05 11:13:42 +0200822 slice = cutoff / para_threads;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200823
Marc Kupietz969cab92019-08-05 11:13:42 +0200824 a = posix_memalign((void **)&target_sums, 128, cutoff * sizeof(float));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200825 memset(target_sums, 0, cutoff * sizeof(float));
826
Marc Kupietzc0d41872021-02-25 16:33:22 +0100827 printf("Starting %d threads for paradigmatic search\n", para_threads);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200828 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200829 for (a = 0; a < para_threads; a++) {
830 pars[a].cutoff = cutoff;
831 pars[a].token = st1;
832 pars[a].wl = wl;
833 pars[a].N = N;
834 pars[a].best = &best[N * a];
835 if (merge_words == 0 || search_backw == 0) {
836 pars[a].from = a * slice;
837 pars[a].upto = ((a + 1) * slice > cutoff ? cutoff : (a + 1) * slice);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200838 } else {
839 pars[a].from = merge_words + a * slice;
Marc Kupietz969cab92019-08-05 11:13:42 +0200840 pars[a].upto = merge_words + ((a + 1) * slice > cutoff ? cutoff : (a + 1) * slice);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200841 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200842 printf("From: %ld, Upto: %ld\n", pars[a].from, pars[a].upto);
843 pthread_create(&pt[a], NULL, _get_neighbours, (void *)&pars[a]);
844 }
845 if (M2) {
846 for (a = 0; a < syn_threads; a++) {
847 pars[a + para_threads].cutoff = cutoff;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200848 pars[a + para_threads].target_sums = target_sums;
849 pars[a + para_threads].window_sums = window_sums;
850 pars[a + para_threads].wl = wl;
851 pars[a + para_threads].N = N;
852 pars[a + para_threads].threshold = MIN_RESP;
853 pars[a + para_threads].from = a;
Marc Kupietz969cab92019-08-05 11:13:42 +0200854 pars[a + para_threads].upto = a + 1;
855 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *)&pars[a + para_threads]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200856 }
857 }
858 printf("Waiting for para threads to join\n");
859 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200860 for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *)&para_nbs[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200861 printf("Para threads joint\n");
862 fflush(stdout);
863
Marc Kupietz969cab92019-08-05 11:13:42 +0200864 /* if(!syn_nbs[0]) */
865 /* goto end; */
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200866
Marc Kupietz969cab92019-08-05 11:13:42 +0200867 qsort(best, N * para_threads, sizeof(collocator), cmp_activation);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200868
869 long long chosen[MAX_NEIGHBOURS];
Marc Kupietz59865a92021-03-11 17:16:51 +0100870 printf("N: %d\n", N);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200871
Marc Kupietz969cab92019-08-05 11:13:42 +0200872 AV *array = newAV();
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200873 int i, j;
Marc Kupietz969cab92019-08-05 11:13:42 +0200874 int l1_words = 0, l2_words = 0;
875
876 for (a = 0, i = 0; i < N && a < N * para_threads; a++) {
877 int filtered = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200878 long long c = best[a].wordi;
879 if ((merge_words && dedupe && i > 1) || (!merge_words && dedupe && i > 0)) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200880 for (j = 0; j < i && !filtered; j++)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200881 if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) ||
882 strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200883 printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]);
884 filtered = 1;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200885 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200886 if (filtered)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200887 continue;
888 }
889
Marc Kupietz969cab92019-08-05 11:13:42 +0200890 if (0 && merge_words > 0) {
891 if (c >= merge_words) {
892 if (l1_words > N / 2)
893 continue;
894 else
895 l1_words++;
896 } else {
897 if (l2_words > N / 2)
898 continue;
899 else
900 l2_words++;
901 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200902 }
903
Marc Kupietz969cab92019-08-05 11:13:42 +0200904 // printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
905 // fflush(stdout);
906 HV *hash = newHV();
907 SV *word = newSVpvf(&vocab[c * max_w], 0);
908 chosen[i] = c;
909 if (latin_enc == 0) SvUTF8_on(word);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200910 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200911 hv_store(hash, "word", strlen("word"), word, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200912 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
913 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
914 AV *vector = newAV();
915 for (b = 0; b < size; b++) {
916 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
917 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200918 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV *)vector), 0);
919 av_push(array, newRV_noinc((SV *)hash));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200920 i++;
921 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200922 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV *)array), 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200923
Marc Kupietz969cab92019-08-05 11:13:42 +0200924 for (b = 0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200925 best[b].wordi = -1L;
926 best[b].activation = 0;
927 best[b].probability = 0;
928 best[b].position = 0;
929 best[b].activation_sum = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200930 memset(best[b].heat, 0, sizeof(float) * 16);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200931 }
932
Marc Kupietz969cab92019-08-05 11:13:42 +0200933 float total_activation = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200934
935 if (M2) {
936 printf("Waiting for syn threads to join\n");
937 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200938 for (a = 0; a < syn_threads; a++) pthread_join(pt[a + para_threads], (void *)&syn_nbs[a]);
939 for (a = 0; a <= syn_threads; a++) {
940 if (a == window) continue;
941 total_activation += window_sums[a];
Marc Kupietz59865a92021-03-11 17:16:51 +0100942 printf("window pos: %ld, sum: %f\n", a, window_sums[a]);
Marc Kupietz969cab92019-08-05 11:13:42 +0200943 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200944 printf("syn threads joint\n");
945 fflush(stdout);
946
Marc Kupietz969cab92019-08-05 11:13:42 +0200947 for (b = 0; b < syn_nbs[0]->length; b++) {
948 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
949 best[b].position = -1; // syn_nbs[0]->pos[b];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200950 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietz969cab92019-08-05 11:13:42 +0200951 best[b].max_activation = 0.0;
952 best[b].average = 0.0;
953 best[b].probability = 0.0;
954 best[b].cprobability = syn_nbs[0]->best[b].cprobability;
955 memset(best[b].heat, 0, sizeof(float) * 16);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200956 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200957
958 float best_window_sum[MAX_NEIGHBOURS];
Marc Kupietz59865a92021-03-11 17:16:51 +0100959 int found_index = 0, i = 0, w;
Marc Kupietz969cab92019-08-05 11:13:42 +0200960 for (a = 0; a < syn_threads; a++) {
961 for (b = 0; b < syn_nbs[a]->length; b++) {
962 for (i = 0; i < found_index; i++)
963 if (best[i].wordi == syn_nbs[a]->best[b].wordi)
964 break;
965 if (i >= found_index) {
966 best[found_index].max_activation = 0.0;
967 best[found_index].average = 0.0;
968 best[found_index].probability = 0.0;
969 memset(best[found_index].heat, 0, sizeof(float) * 16);
970 best[found_index].cprobability = syn_nbs[a]->best[b].cprobability;
971 best[found_index].activation_sum = target_sums[syn_nbs[a]->best[b].wordi]; // syn_nbs[a]->best[b].activation_sum;
972 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
973 // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
974 }
975 }
976 }
977 sort_by = 0; // ALWAYS AUTO-FOCUS
978 if (sort_by != 1 && sort_by != 2) { // sort by auto focus mean
979 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) - 1);
980 int wpos;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200981 int bits_set = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200982 for (i = 0; i < found_index; i++) {
983 best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0;
984 for (w = 1; w < (1 << syn_threads); w++) { // loop through all possible windows
985 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 +0200986 bits_set = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200987 for (a = 0; a < syn_threads; a++) {
988 if ((1 << a) & w) {
989 wpos = (a >= window ? a + 1 : a);
990 total_window_sum += window_sums[wpos];
991 }
992 }
993 // printf("%d window-sum %f\n", w, total_window_sum);
994 for (a = 0; a < syn_threads; a++) {
995 if ((1 << a) & w) {
996 wpos = (a >= window ? a + 1 : a);
997 bits_set++;
998 for (b = 0; b < syn_nbs[a]->length; b++)
999 if (best[i].wordi == syn_nbs[a]->best[b].wordi) {
1000 // float acti = syn_nbs[a]->best[b].activation / total_window_sum;
1001 // word_window_sum += syn_nbs[a]->dist[b] * syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1002 // word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1003 // 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 +02001004
Marc Kupietz969cab92019-08-05 11:13:42 +02001005 word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1006 // 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 +02001007
Marc Kupietz969cab92019-08-05 11:13:42 +02001008 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 +02001009 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 +02001010 word_activation_sum += syn_nbs[a]->best[b].activation;
1011 if (syn_nbs[a]->best[b].activation > best[i].max_activation)
1012 best[i].max_activation = syn_nbs[a]->best[b].activation;
1013 if (syn_nbs[a]->best[b].activation > best[i].heat[wpos])
1014 best[i].heat[wpos] = syn_nbs[a]->best[b].activation;
1015 }
1016 }
1017 }
1018 if (bits_set) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001019 word_window_average /= bits_set;
Marc Kupietz969cab92019-08-05 11:13:42 +02001020 // word_activation_sum /= bits_set;
1021 // word_window_sum /= bits_set;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001022 }
1023
Marc Kupietz969cab92019-08-05 11:13:42 +02001024 word_window_sum /= total_window_sum;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001025
Marc Kupietz969cab92019-08-05 11:13:42 +02001026 if (word_window_sum > best[i].probability) {
1027 // best[i].position = w;
1028 best[i].probability = word_window_sum;
1029 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001030
Marc Kupietz969cab92019-08-05 11:13:42 +02001031 if (word_cprobability_sum > best[i].cprobability_sum) {
1032 best[i].position = w;
1033 best[i].cprobability_sum = word_cprobability_sum;
1034 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001035
Marc Kupietz969cab92019-08-05 11:13:42 +02001036 best[i].average = word_window_average;
1037 // best[i].activation = word_activation_sum;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001038 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001039 }
1040 qsort(best, found_index, sizeof(collocator), cmp_probability);
1041 // for(i=0; i < found_index; i++) {
1042 // printf("found: %s - sum: %f - window: %d\n", &vocab[best[i].wordi * max_w], best[i].activation, best[i].position);
1043 // }
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001044
Marc Kupietz969cab92019-08-05 11:13:42 +02001045 } else if (sort_by == 1) { // responsiveness any window position
1046 int wpos;
1047 for (i = 0; i < found_index; i++) {
1048 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
1049 for (a = 0; a < syn_threads; a++) {
1050 wpos = (a >= window ? a + 1 : a);
1051 for (b = 0; b < syn_nbs[a]->length; b++)
1052 if (best[i].wordi == syn_nbs[a]->best[b].wordi) {
1053 best[i].probability += syn_nbs[a]->best[b].probability;
1054 if (syn_nbs[a]->best[b].activation > 0.25)
1055 best[i].position |= 1 << wpos;
1056 if (syn_nbs[a]->best[b].activation > best[i].activation) {
1057 best[i].activation = syn_nbs[a]->best[b].activation;
1058 }
1059 }
1060 }
1061 }
1062 qsort(best, found_index, sizeof(collocator), cmp_activation);
1063 } else if (sort_by == 2) { // single window position
1064 for (a = 1; a < syn_threads; a++) {
1065 for (b = 0; b < syn_nbs[a]->length; b++) {
1066 for (c = 0; c < MAX_NEIGHBOURS; c++) {
1067 if (syn_nbs[a]->best[b].activation > best[c].activation) {
1068 for (d = MAX_NEIGHBOURS - 1; d > c; d--) {
1069 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001070 }
1071 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
Marc Kupietz969cab92019-08-05 11:13:42 +02001072 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 +02001073 break;
1074 }
1075 }
1076 }
1077 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001078 } else { // sort by mean p
1079 for (a = 1; a < syn_threads; a++) {
1080 for (b = 0; b < syn_nbs[a]->length; b++) {
1081 for (c = 0; c < MAX_NEIGHBOURS; c++) {
1082 if (target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
1083 for (d = MAX_NEIGHBOURS - 1; d > c; d--) {
1084 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001085 }
1086 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
Marc Kupietz969cab92019-08-05 11:13:42 +02001087 best[c].position = (1 << 2 * window) - 1; // syn_nbs[a]->pos[b];
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001088 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
1089 break;
1090 }
1091 }
1092 }
1093 }
1094 }
1095 array = newAV();
Marc Kupietz969cab92019-08-05 11:13:42 +02001096 for (a = 0, i = 0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001097 long long c = best[a].wordi;
Marc Kupietz969cab92019-08-05 11:13:42 +02001098 /*
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001099 if (dedupe) {
1100 int filtered=0;
1101 for (j=0; j<i; j++)
1102 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
1103 strcasestr(chosen[j], &vocab[c * max_w])) {
1104 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
1105 filtered = 1;
1106 }
1107 if(filtered)
1108 continue;
1109 }
1110*/
Marc Kupietz969cab92019-08-05 11:13:42 +02001111 chosen[i++] = c;
1112 HV *hash = newHV();
1113 SV *word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
1114 AV *heat = newAV();
1115 if (latin_enc == 0) SvUTF8_on(word);
1116 hv_store(hash, "word", strlen("word"), word, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001117 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
1118 hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0);
1119 hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0);
1120 hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0);
Marc Kupietz969cab92019-08-05 11:13:42 +02001121 hv_store(hash, "max", strlen("max"), newSVnv(best[a].max_activation), 0); // newSVnv(target_sums[best[a].wordi]), 0);
1122 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 +02001123 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietz969cab92019-08-05 11:13:42 +02001124 best[a].heat[5] = 0;
1125 for (i = 10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i]));
1126 hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV *)heat), 0);
1127 av_push(array, newRV_noinc((SV *)hash));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001128 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001129 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV *)array), 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001130 }
1131end:
Marc Kupietz969cab92019-08-05 11:13:42 +02001132 free(best);
1133 return newRV_noinc((SV *)result);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001134}
1135
1136int dump_vecs(char *fname) {
Marc Kupietz969cab92019-08-05 11:13:42 +02001137 long i, j;
1138 FILE *f;
1139 /* if(words>100000)
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001140 words=100000;
1141*/
Marc Kupietz969cab92019-08-05 11:13:42 +02001142 if ((f = fopen(fname, "w")) == NULL) {
1143 fprintf(stderr, "cannot open %s for writing\n", fname);
1144 return (-1);
1145 }
1146 fprintf(f, "%lld %lld\n", words, size);
1147 for (i = 0; i < words; i++) {
1148 fprintf(f, "%s ", &vocab[i * max_w]);
1149 for (j = 0; j < size - 1; j++)
1150 fprintf(f, "%f ", M[i * size + j]);
1151 fprintf(f, "%f\n", M[i * size + j]);
1152 }
1153 fclose(f);
1154 return (0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001155}
1156
1157int dump_for_numpy(char *fname) {
Marc Kupietz969cab92019-08-05 11:13:42 +02001158 long i, j;
1159 FILE *f;
Marc Kupietzc0d41872021-02-25 16:33:22 +01001160 int max = words; // 300000;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001161
Marc Kupietz969cab92019-08-05 11:13:42 +02001162 if ((f = fopen(fname, "w")) == NULL) {
1163 fprintf(stderr, "cannot open %s for writing\n", fname);
1164 return (-1);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001165 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001166 for (i = 0; i < max; i++) {
1167 for (j = 0; j < size - 1; j++)
1168 fprintf(f, "%f\t", M[i * size + j]);
1169 fprintf(f, "%f\n", M[i * size + j]);
1170 printf("%s\r\n", &vocab[i * max_w]);
1171 }
1172 if (merged_end > 0) {
1173 for (i = 0; i < max; i++) {
1174 for (j = 0; j < size - 1; j++)
1175 fprintf(f, "%f\t", M[(merged_end + i) * size + j]);
1176 fprintf(f, "%f\n", M[(merged_end + i) * size + j]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001177 printf("_%s\r\n", &vocab[i * max_w]);
1178 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001179 }
1180 fclose(f);
1181 return (0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001182}