blob: df6637175d995fecd9d44d126fa3044885d31009 [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>
6#include <stdlib.h> //strlen
7#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 Kupietz0efe49b2020-04-06 18:30:22 +0200117int init_net(char *file_name, char *net_name, int latin, int do_open_cdb) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200118 FILE *f, *binvecs, *binwords;
Marc Kupietz969cab92019-08-05 11:13:42 +0200119 int binwords_fd, binvecs_fd, net_fd, i;
120 long long a, b, c, d, cn;
121 float len;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200122 double val;
123
Marc Kupietz969cab92019-08-05 11:13:42 +0200124 char binvecs_fname[256], binwords_fname[256];
125 strcpy(binwords_fname, file_name);
126 strcat(binwords_fname, ".words");
127 strcpy(binvecs_fname, file_name);
128 strcat(binvecs_fname, ".vecs");
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200129
130 latin_enc = latin;
131 f = fopen(file_name, "rb");
132 if (f == NULL) {
133 printf("Input file %s not found\n", file_name);
134 return -1;
135 }
136 fscanf(f, "%lld", &words);
Marc Kupietz969cab92019-08-05 11:13:42 +0200137 if (MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200138 fscanf(f, "%lld", &size);
Marc Kupietz969cab92019-08-05 11:13:42 +0200139 if ((binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200140 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietz969cab92019-08-05 11:13:42 +0200141 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
142 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
143 if (M == NULL) {
144 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
145 return -1;
146 }
147 if (strstr(file_name, ".txt")) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200148 for (b = 0; b < words; b++) {
149 a = 0;
150 while (1) {
151 vocab[b * max_w + a] = fgetc(f);
152 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
153 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
154 }
155 vocab[b * max_w + a] = 0;
156 len = 0;
157 for (a = 0; a < size; a++) {
158 fscanf(f, "%lf", &val);
159 M[a + b * size] = val;
160 len += val * val;
Marc Kupietz969cab92019-08-05 11:13:42 +0200161 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200162 len = sqrt(len);
163 for (a = 0; a < size; a++) M[a + b * size] /= len;
164 }
165 } else {
166 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 fread(&M[b * size], sizeof(float), size, f);
175 len = 0;
176 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
177 len = sqrt(len);
178 for (a = 0; a < size; a++) M[a + b * size] /= len;
179 }
180 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200181 if ((binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
182 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
183 fclose(binvecs);
184 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
185 fclose(binwords);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200186 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200187 }
188 if ((binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
189 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
190 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
191 if (M == MAP_FAILED || vocab == MAP_FAILED) {
192 close(binvecs_fd);
193 close(binwords_fd);
194 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
195 exit(-1);
196 }
197 } else {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200198 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
199 exit(-1);
Marc Kupietz969cab92019-08-05 11:13:42 +0200200 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200201 fclose(f);
202
Marc Kupietz969cab92019-08-05 11:13:42 +0200203 if (net_name && strlen(net_name) > 0) {
204 if ((net_fd = open(net_name, O_RDONLY)) >= 0) {
205 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200206 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
207 // munmap(M, sizeof(float) * words * size);
208 M2 = mmap(0, sizeof(float) * words * size + sizeof(float) * 2 * window * size * words, PROT_READ, MAP_SHARED, net_fd, 0);
209 if (M2 == MAP_FAILED) {
210 close(net_fd);
211 fprintf(stderr, "Cannot mmap %s\n", net_name);
212 exit(-1);
213 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200214 syn1neg_window = M2 + words * size;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200215 } else {
216 fprintf(stderr, "Cannot open %s\n", net_name);
217 exit(-1);
218 }
219 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
220
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200221 if (do_open_cdb) {
222 char collocatordb_name[2048];
223 strcpy(collocatordb_name, net_name);
224 char *ext = rindex(collocatordb_name, '.');
225 if (ext) {
226 strcpy(ext, ".rocksdb");
227 if (access(collocatordb_name, R_OK) == 0) {
228 *ext = 0;
229 fprintf(stderr, "Opening collocator DB %s\n", collocatordb_name);
230 cdb = open_collocatordb(collocatordb_name);
231 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200232 }
233 }
234 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200235
Marc Kupietz969cab92019-08-05 11:13:42 +0200236 expTable = (float *)malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
237 for (i = 0; i < EXP_TABLE_SIZE; i++) {
238 expTable[i] = exp((i / (float)EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
239 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
240 }
241 window_sums = malloc(sizeof(float) * (window + 1) * 2);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200242
243 return 0;
244}
245
Marc Kupietz969cab92019-08-05 11:13:42 +0200246long mergeVectors(char *file_name) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200247 FILE *f, *binvecs, *binwords;
Marc Kupietz969cab92019-08-05 11:13:42 +0200248 int binwords_fd, binvecs_fd, net_fd, i;
249 long long a, b, c, d, cn;
250 float len;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200251 float *merge_vecs;
252 char *merge_vocab;
Marc Kupietz969cab92019-08-05 11:13:42 +0200253 /* long long merge_words, merge_size; */
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200254 long long merge_size;
255
Marc Kupietz969cab92019-08-05 11:13:42 +0200256 char binvecs_fname[256], binwords_fname[256];
257 strcpy(binwords_fname, file_name);
258 strcat(binwords_fname, ".words");
259 strcpy(binvecs_fname, file_name);
260 strcat(binvecs_fname, ".vecs");
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200261
262 f = fopen(file_name, "rb");
263 if (f == NULL) {
264 printf("Input file %s not found\n", file_name);
Marc Kupietz969cab92019-08-05 11:13:42 +0200265 exit - 1;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200266 }
267 fscanf(f, "%lld", &merge_words);
268 fscanf(f, "%lld", &merge_size);
Marc Kupietz969cab92019-08-05 11:13:42 +0200269 if (merge_size != size) {
270 fprintf(stderr, "vectors must have the same length\n");
271 exit(-1);
272 }
273 if ((binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
274 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
275 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200276 if (merge_vecs == NULL || merge_vocab == NULL) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200277 close(binvecs_fd);
278 close(binwords_fd);
279 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
280 exit(-1);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200281 }
282 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
283 read(binwords_fd, merge_vocab, merge_words * max_w);
Marc Kupietz969cab92019-08-05 11:13:42 +0200284 } else {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200285 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
286 exit(-1);
Marc Kupietz969cab92019-08-05 11:13:42 +0200287 }
288 printf("Successfully reallocated memory\nMerging...\n");
289 fflush(stdout);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200290 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
291 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
292 munmap(M, words * size * sizeof(float));
293 munmap(vocab, words * max_w);
294 M = merge_vecs;
295 vocab = merge_vocab;
296 merged_end = merge_words;
297 words += merge_words;
298 fclose(f);
Marc Kupietz969cab92019-08-05 11:13:42 +0200299 printf("merged_end: %lld, words: %lld\n", merged_end, words);
300 //printBiggestMergedDifferences();
301 return ((long)merged_end);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200302}
303
304void filter_garbage() {
305 long i;
306 unsigned char *w, previous, c;
307 garbage = malloc(words);
308 memset(garbage, 0, words);
309 for (i = 0; i < words; i++) {
310 w = vocab + i * max_w;
311 previous = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200312 if (strncmp("quot", w, 4) == 0) {
313 garbage[i] = 1;
314 // printf("Gargabe: %s\n", vocab + i * max_w);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200315 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200316 while ((c = *w++) && !garbage[i]) {
317 if (((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200318 (previous == '-' && (c & 32)) ||
Marc Kupietz969cab92019-08-05 11:13:42 +0200319 (previous == 0xc2 && (c == 0xa4 || c == 0xb6)) ||
320 (previous == 'q' && c == 'u' && *(w) == 'o' && *(w + 1) == 't') || /* quot */
321 c == '<') {
322 garbage[i] = 1;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200323 continue;
324 }
325 previous = c;
326 }
327 }
328 }
329 return;
330}
331
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200332knn *simpleGetCollocators(int word, int number, long cutoff, int *result) {
333 knnpars *pars = calloc(sizeof(knnpars), 1);
334 float *target_sums;
Marc Kupietz969cab92019-08-05 11:13:42 +0200335 float *window_sums = malloc(sizeof(float) * (window + 1) * 2);
336 pars->cutoff = (cutoff ? cutoff : 300000);
337 long a = posix_memalign((void **)&target_sums, 128, pars->cutoff * sizeof(float));
338 for (a = 0; a < cutoff; a++)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200339 target_sums[a] = 0;
340 pars->target_sums = target_sums;
341 pars->window_sums = window_sums;
Marc Kupietz969cab92019-08-05 11:13:42 +0200342 pars->N = (number ? number : 20);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200343 pars->from = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200344 pars->upto = window * 2 - 1;
345 knn *syn_nbs = NULL; // = (knn*) getCollocators(pars);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200346 free(pars);
347 free(window_sums);
348 free(target_sums);
349 return syn_nbs;
350}
351
352void *getCollocators(void *args) {
353 knnpars *pars = args;
Marc Kupietz969cab92019-08-05 11:13:42 +0200354 int N = pars->N;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200355
356 int cc = pars->wl->wordi[0];
Marc Kupietz969cab92019-08-05 11:13:42 +0200357 knn *nbs = NULL;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200358 long window_layer_size = size * window * 2;
Marc Kupietz969cab92019-08-05 11:13:42 +0200359 long a, b, c, d, e, window_offset, target, max_target = 0, maxmax_target;
360 float f, max_f, maxmax_f;
361 float *target_sums = NULL, worstbest, wpos_sum;
362 collocator *best;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200363
Marc Kupietz969cab92019-08-05 11:13:42 +0200364 if (M2 == NULL || cc == -1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200365 return NULL;
366
Marc Kupietz969cab92019-08-05 11:13:42 +0200367 a = posix_memalign((void **)&target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200368 memset(target_sums, 0, pars->cutoff * sizeof(float));
Marc Kupietz969cab92019-08-05 11:13:42 +0200369 best = malloc((N > 200 ? N : 200) * sizeof(collocator));
370 memset(best, 0, (N > 200 ? N : 200) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200371 worstbest = pars->threshold;
372
373 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200374 target_sums[b] = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200375 for (b = 0; b < N; b++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200376 best[b].wordi = -1;
377 best[b].probability = 1;
378 best[b].activation = worstbest;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200379 }
380
381 d = cc;
382 maxmax_f = -1;
383 maxmax_target = 0;
384
385 for (a = pars->from; a < pars->upto; a++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200386 if (a >= window)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200387 a++;
388 wpos_sum = 0;
389 printf("window pos: %ld\n", a);
390 if (a != window) {
391 max_f = -1;
392 window_offset = a * size;
393 if (a > window)
394 window_offset -= size;
Marc Kupietz969cab92019-08-05 11:13:42 +0200395 for (target = 0; target < pars->cutoff; target++) {
396 if (garbage && garbage[target]) continue;
397 if (target == d)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200398 continue;
399 f = 0;
400 for (c = 0; c < size; c++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200401 f += M2[d * size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200402 if (f < -MAX_EXP)
403 continue;
404 else if (f > MAX_EXP)
405 continue;
406 else
Marc Kupietz969cab92019-08-05 11:13:42 +0200407 f = expTable[(int)((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200408 wpos_sum += f;
409
410 target_sums[target] += f;
Marc Kupietz969cab92019-08-05 11:13:42 +0200411 if (f > worstbest) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200412 for (b = 0; b < N; b++) {
413 if (f > best[b].activation) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200414 memmove(best + b + 1, best + b, (N - b - 1) * sizeof(collocator));
415 best[b].activation = f;
416 best[b].wordi = target;
417 best[b].position = window - a;
418 break;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200419 }
420 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200421 if (b == N - 1)
422 worstbest = best[N - 1].activation;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200423 }
424 }
425 printf("%d %.2f\n", max_target, max_f);
426 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
Marc Kupietz969cab92019-08-05 11:13:42 +0200427 if (max_f > maxmax_f) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200428 maxmax_f = max_f;
429 maxmax_target = max_target;
430 }
431 for (b = 0; b < N; b++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200432 if (best[b].position == window - a)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200433 best[b].cprobability = best[b].activation / wpos_sum;
434 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200435 printf("\x1b[1m%s\x1b[0m ", &vocab[d * max_w]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200436 }
437 pars->window_sums[a] = wpos_sum;
438 }
439 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200440 pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200441
442 free(target_sums);
Marc Kupietz969cab92019-08-05 11:13:42 +0200443 for (b = 0; b < N && best[b].wordi >= 0; b++)
444 ;
445 ; // THIS LOOP IS NEEDED (b...)
446 // printf("%d: best syn: %s %.2f %.5f\n", b, &vocab[best[b].wordi*max_w], best[b].activation, best[b].probability);
447 // printf("\n");
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200448 nbs = malloc(sizeof(knn));
Marc Kupietz969cab92019-08-05 11:13:42 +0200449 nbs->best = best;
450 nbs->length = b - 1;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200451 pthread_exit(nbs);
452}
453
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200454float getOutputWeight(int hidden, long target, int window_position) {
455 const long window_layer_size = size * window * 2;
456 int a;
457
458 if (window_position == 0 || window_position > window || window_position < -window) {
459 fprintf(stderr, "window_position: %d - assert: -%d <= window_position <= %d && window_position != 0 failed.\n", window_position, window, window);
460 exit(-1);
461 }
462
463 if (hidden >= size) {
464 fprintf(stderr, "hidden: %d - assert: hidden < %d failed.\n", hidden, size);
465 exit(-1);
466 }
467
468 if (target >= words) {
469 fprintf(stderr, "target: %ld - assert: target < %ld failed.\n", target, words);
470 exit(-1);
471 }
472
473 a = window_position + window;
474 if (a > window) {
475 --a;
476 }
477 long window_offset = a * size;
478 return syn1neg_window[target * window_layer_size + window_offset + hidden];
479}
480
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200481AV *getVecs(AV *array) {
482 int i, b;
483 AV *result = newAV();
Marc Kupietz969cab92019-08-05 11:13:42 +0200484 for (i = 0; i <= av_len(array); i++) {
485 SV **elem = av_fetch(array, i, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200486 if (elem != NULL) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200487 long j = (long)SvNV(*elem);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200488 AV *vector = newAV();
489 for (b = 0; b < size; b++) {
490 av_push(vector, newSVnv(M[b + j * size]));
491 }
492 av_push(result, newRV_noinc(vector));
493 }
494 }
495 return result;
496}
497
498char *getSimilarProfiles(long node) {
499 int i;
500 char buffer[120000];
501 char pair_buffer[2048];
Marc Kupietz969cab92019-08-05 11:13:42 +0200502 buffer[0] = '[';
503 buffer[1] = 0;
504 if (node >= sprofiles_qty) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200505 printf("Not available in precomputed profile\n");
Marc Kupietz969cab92019-08-05 11:13:42 +0200506 return (strdup("[{\"w\":\"not available\", \"v\":0}]\n"));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200507 }
508
509 printf("******* %s ******\n", &vocab[max_w * node]);
Marc Kupietz969cab92019-08-05 11:13:42 +0200510
511 for (i = 0; i < 100 && i < sprofiles[node].len; i++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200512 sprintf(pair_buffer, "{\"w\":\"%s\", \"v\":%f},", &vocab[max_w * (sprofiles[node].nbr[i].index)], sprofiles[node].nbr[i].value);
513 strcat(buffer, pair_buffer);
514 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200515 buffer[strlen(buffer) - 1] = ']';
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200516 strcat(buffer, "\n");
517 printf(buffer);
Marc Kupietz969cab92019-08-05 11:13:42 +0200518 return (strdup(buffer));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200519}
520
521char *getClassicCollocators(long node) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200522 char *res = (cdb ? strdup(get_collocators_as_json(cdb, node)) : "[]");
523 return res;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200524}
525
526wordlist *getTargetWords(char *st1, int search_backw) {
527 wordlist *wl = malloc(sizeof(wordlist));
528 char st[100][max_size], sep[100];
Marc Kupietz969cab92019-08-05 11:13:42 +0200529 long a, b = 0, c = 0, cn = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200530 int unmerged;
531
532 while (1) {
533 st[cn][b] = st1[c];
534 b++;
535 c++;
536 st[cn][b] = 0;
537 if (st1[c] == 0) break;
538 if (st1[c] == ' ' || st1[c] == '-') {
539 sep[cn++] = st1[c];
540 b = 0;
541 c++;
542 }
543 }
544 cn++;
545 for (a = 0; a < cn; a++) {
546 if (search_backw) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200547 for (b = words - 1; b >= (merge_words ? merge_words : 0) && strcmp(&vocab[b * max_w], st[a]) != 0; b--)
548 ;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200549 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200550 for (b = 0; b < (merge_words ? merge_words : words) && strcmp(&vocab[b * max_w], st[a]) != 0; b++)
551 ;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200552 }
553 if (b == words) b = -1;
554 wl->wordi[a] = b;
555 if (b == -1) {
556 fprintf(stderr, "Out of dictionary word!\n");
557 cn--;
558 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200559 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 +0200560 }
561 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200562 wl->length = cn;
563 return (wl);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200564}
565
Marc Kupietzcb43e492019-12-03 10:07:53 +0100566long getWordNumber(char *word) {
567 wordlist *wl = getTargetWords(word, 0);
568 if(wl->length > 0)
569 return(wl->wordi[0]);
570 return(0);
571}
572
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200573float get_distance(long b, long c) {
574 long a;
575 float dist = 0;
576 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + b * size];
577 return dist;
578}
579
Marc Kupietz969cab92019-08-05 11:13:42 +0200580char *getBiggestMergedDifferences() {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200581 static char *result = NULL;
582 float dist, len, vec[max_size];
583 long long a, b, c, d, cn, *bi;
584 char ch;
585 knn *nbs = NULL;
586 int N = 1000;
587
Marc Kupietz969cab92019-08-05 11:13:42 +0200588 if (merged_end == 0)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200589 result = "[]";
Marc Kupietz969cab92019-08-05 11:13:42 +0200590
591 if (result != NULL)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200592 return result;
593
594 printf("Looking for biggest distances between main and merged vectors ...\n");
595 collocator *best;
596 best = malloc(N * sizeof(collocator));
597 memset(best, 0, N * sizeof(collocator));
598
Marc Kupietz969cab92019-08-05 11:13:42 +0200599 float worstbest = 1000000;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200600
601 for (a = 0; a < N; a++) best[a].activation = worstbest;
602
603 for (c = 0; c < 500000; c++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200604 if (garbage && garbage[c]) continue;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200605 a = 0;
606 dist = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200607 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + (c + merged_end) * size];
608 if (dist < worstbest) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200609 for (a = 0; a < N; a++) {
610 if (dist < best[a].activation) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200611 memmove(best + a + 1, best + a, (N - a - 1) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200612 best[a].activation = dist;
613 best[a].wordi = c;
614 break;
615 }
616 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200617 worstbest = best[N - 1].activation;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200618 }
619 }
620
Marc Kupietz969cab92019-08-05 11:13:42 +0200621 result = malloc(N * max_w);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200622 char *p = result;
Marc Kupietz969cab92019-08-05 11:13:42 +0200623 *p++ = '[';
624 *p = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200625 for (a = 0; a < N; a++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200626 p += sprintf(p, "{\"rank\":%d,\"word\":\"%s\",\"dist\":%.3f},", a, &vocab[best[a].wordi * max_w], 1 - best[a].activation);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200627 }
628 *--p = ']';
Marc Kupietz969cab92019-08-05 11:13:42 +0200629 return (result);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200630}
631
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200632float cos_similarity(long b, long c) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200633 float dist = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200634 long a;
Marc Kupietz969cab92019-08-05 11:13:42 +0200635 for (a = 0; a < size; a++) dist += M[b * size + a] * M[c * size + a];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200636 return dist;
637}
638
639char *cos_similarity_as_json(char *w1, char *w2) {
640 wordlist *a, *b;
641 float res;
642 a = getTargetWords(w1, 0);
643 b = getTargetWords(w2, 0);
Marc Kupietz969cab92019-08-05 11:13:42 +0200644 if (a == NULL || b == NULL || a->length != 1 || b->length != 1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200645 res = -1;
646 else
647 res = cos_similarity(a->wordi[0], b->wordi[0]);
648 fprintf(stderr, "a: %lld b: %lld res:%f\n", a->wordi[0], b->wordi[0], res);
649 char *json = malloc(16);
650 sprintf(json, "%.5f", res);
651 return json;
652}
653
654void *_get_neighbours(void *arg) {
655 knnpars *pars = arg;
Marc Kupietz969cab92019-08-05 11:13:42 +0200656 char *st1 = pars->token;
657 int N = pars->N;
658 long from = pars->from;
659 unsigned long upto = pars->upto;
660 char file_name[max_size], st[100][max_size], *sep;
661 float dist, len, vec[max_size];
662 long long a, b, c, d, cn, *bi;
663 char ch;
664 knn *nbs = NULL;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200665 wordlist *wl = pars->wl;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200666
Marc Kupietz969cab92019-08-05 11:13:42 +0200667 collocator *best = pars->best;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200668
Marc Kupietz969cab92019-08-05 11:13:42 +0200669 float worstbest = -1;
670
671 for (a = 0; a < N; a++) best[a].activation = 0;
672 a = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200673 bi = wl->wordi;
Marc Kupietz969cab92019-08-05 11:13:42 +0200674 cn = wl->length;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200675 sep = wl->sep;
Marc Kupietz969cab92019-08-05 11:13:42 +0200676 b = bi[0];
677 c = 0;
678 if (b == -1) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200679 N = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200680 goto end;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200681 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200682 for (a = 0; a < size; a++) vec[a] = 0;
683 for (b = 0; b < cn; b++) {
684 if (bi[b] == -1) continue;
685 if (b > 0 && sep[b - 1] == '-')
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200686 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
687 else
688 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietz969cab92019-08-05 11:13:42 +0200689 }
690 len = 0;
691 for (a = 0; a < size; a++) len += vec[a] * vec[a];
692 len = sqrt(len);
693 for (a = 0; a < size; a++) vec[a] /= len;
694 for (a = 0; a < N; a++) best[a].activation = -1;
695 for (c = from; c < upto; c++) {
696 if (garbage && garbage[c]) continue;
697 a = 0;
698 // do not skip taget word
699 // for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
700 // if (a == 1) continue;
701 dist = 0;
702 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
703 if (dist > worstbest) {
704 for (a = 0; a < N; a++) {
705 if (dist > best[a].activation) {
706 memmove(best + a + 1, best + a, (N - a - 1) * sizeof(collocator));
707 best[a].activation = dist;
708 best[a].wordi = c;
709 break;
710 }
711 }
712 worstbest = best[N - 1].activation;
713 }
714 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200715
716end:
Marc Kupietz969cab92019-08-05 11:13:42 +0200717 pthread_exit(nbs);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200718}
719
Marc Kupietz969cab92019-08-05 11:13:42 +0200720int cmp_activation(const void *a, const void *b) {
721 float fb = ((collocator *)a)->activation;
722 float fa = ((collocator *)b)->activation;
723 return (fa > fb) - (fa < fb);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200724}
725
Marc Kupietz969cab92019-08-05 11:13:42 +0200726int cmp_probability(const void *a, const void *b) {
727 float fb = ((collocator *)a)->probability;
728 float fa = ((collocator *)b)->probability;
729 return (fa > fb) - (fa < fb);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200730}
731
Marc Kupietz969cab92019-08-05 11:13:42 +0200732char *getPosWiseW2VCollocatorsAsTsv(char *word, long maxPerPos, long cutoff, float threshold) {
733 HV *result = newHV();
734 float *target_sums = NULL, vec[max_size];
735 long long old_words;
736 long a, b, c, d;
737 knn *para_nbs[MAX_THREADS];
738 knn *syn_nbs[MAX_THREADS];
739 knnpars pars[MAX_THREADS];
740 pthread_t *pt = (pthread_t *)malloc((num_threads + 1) * sizeof(pthread_t));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200741 wordlist *wl;
Marc Kupietz969cab92019-08-05 11:13:42 +0200742 int syn_threads = (M2 ? window * 2 : 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200743 int search_backw = 0;
744 collocator *best = NULL;
Marc Kupietz969cab92019-08-05 11:13:42 +0200745 posix_memalign((void **)&best, 128, 10 * (maxPerPos >= 200 ? maxPerPos : 200) * sizeof(collocator));
746 memset(best, 0, (maxPerPos >= 200 ? maxPerPos : 200) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200747
Marc Kupietz969cab92019-08-05 11:13:42 +0200748 if (cutoff < 1 || cutoff > words)
749 cutoff = words;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200750
751 wl = getTargetWords(word, search_backw);
Marc Kupietz969cab92019-08-05 11:13:42 +0200752 if (wl == NULL || wl->length < 1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200753 return "";
754
Marc Kupietz969cab92019-08-05 11:13:42 +0200755 a = posix_memalign((void **)&target_sums, 128, cutoff * sizeof(float));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200756 memset(target_sums, 0, cutoff * sizeof(float));
757
758 printf("Starting %d threads\n", syn_threads);
759 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200760 for (a = 0; a < syn_threads; a++) {
761 pars[a].cutoff = cutoff;
762 pars[a].target_sums = target_sums;
763 pars[a].window_sums = window_sums;
764 pars[a].wl = wl;
765 pars[a].N = maxPerPos;
766 pars[a].threshold = threshold;
767 pars[a].from = a;
768 pars[a].upto = a + 1;
769 pthread_create(&pt[a], NULL, getCollocators, (void *)&pars[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200770 }
771 printf("Waiting for syn threads to join\n");
772 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200773 for (a = 0; a < syn_threads; a++) pthread_join(pt[a], (void *)&syn_nbs[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200774 printf("Syn threads joint\n");
775 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200776 result = malloc(maxPerPos * 80 * syn_threads);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200777 char *p = result;
778 *p = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200779 for (a = syn_threads - 1; a >= 0; a--) {
780 for (b = 0; b < syn_nbs[a]->length; b++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200781 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);
782 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200783 }
784 return (result);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200785}
786
787SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe, int no_similar_profiles) {
788 HV *result = newHV();
Marc Kupietz969cab92019-08-05 11:13:42 +0200789 float *target_sums = NULL, vec[max_size];
790 long long old_words;
791 long a, b, c, d, slice;
792 knn *para_nbs[MAX_THREADS];
793 knn *syn_nbs[MAX_THREADS];
794 knnpars pars[MAX_THREADS];
795 pthread_t *pt = (pthread_t *)malloc((num_threads + 1) * sizeof(pthread_t));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200796 wordlist *wl;
Marc Kupietz969cab92019-08-05 11:13:42 +0200797 int syn_threads = (M2 ? window * 2 : 0);
798 int para_threads = (no_similar_profiles ? 0 : num_threads - syn_threads);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200799
800 collocator *best = NULL;
Marc Kupietz969cab92019-08-05 11:13:42 +0200801 posix_memalign((void **)&best, 128, 10 * (N >= 200 ? N : 200) * sizeof(collocator));
802 memset(best, 0, (N >= 200 ? N : 200) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200803
Marc Kupietz969cab92019-08-05 11:13:42 +0200804 if (N > MAX_NEIGHBOURS) N = MAX_NEIGHBOURS;
805
806 if (cutoff < 1 || cutoff > words)
807 cutoff = words;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200808
809 wl = getTargetWords(st1, search_backw);
Marc Kupietz969cab92019-08-05 11:13:42 +0200810 if (wl == NULL || wl->length < 1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200811 goto end;
812
Marc Kupietz969cab92019-08-05 11:13:42 +0200813 old_words = cutoff;
814 slice = cutoff / para_threads;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200815
Marc Kupietz969cab92019-08-05 11:13:42 +0200816 a = posix_memalign((void **)&target_sums, 128, cutoff * sizeof(float));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200817 memset(target_sums, 0, cutoff * sizeof(float));
818
819 printf("Starting %d threads\n", para_threads);
820 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200821 for (a = 0; a < para_threads; a++) {
822 pars[a].cutoff = cutoff;
823 pars[a].token = st1;
824 pars[a].wl = wl;
825 pars[a].N = N;
826 pars[a].best = &best[N * a];
827 if (merge_words == 0 || search_backw == 0) {
828 pars[a].from = a * slice;
829 pars[a].upto = ((a + 1) * slice > cutoff ? cutoff : (a + 1) * slice);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200830 } else {
831 pars[a].from = merge_words + a * slice;
Marc Kupietz969cab92019-08-05 11:13:42 +0200832 pars[a].upto = merge_words + ((a + 1) * slice > cutoff ? cutoff : (a + 1) * slice);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200833 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200834 printf("From: %ld, Upto: %ld\n", pars[a].from, pars[a].upto);
835 pthread_create(&pt[a], NULL, _get_neighbours, (void *)&pars[a]);
836 }
837 if (M2) {
838 for (a = 0; a < syn_threads; a++) {
839 pars[a + para_threads].cutoff = cutoff;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200840 pars[a + para_threads].target_sums = target_sums;
841 pars[a + para_threads].window_sums = window_sums;
842 pars[a + para_threads].wl = wl;
843 pars[a + para_threads].N = N;
844 pars[a + para_threads].threshold = MIN_RESP;
845 pars[a + para_threads].from = a;
Marc Kupietz969cab92019-08-05 11:13:42 +0200846 pars[a + para_threads].upto = a + 1;
847 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *)&pars[a + para_threads]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200848 }
849 }
850 printf("Waiting for para threads to join\n");
851 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200852 for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *)&para_nbs[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200853 printf("Para threads joint\n");
854 fflush(stdout);
855
Marc Kupietz969cab92019-08-05 11:13:42 +0200856 /* if(!syn_nbs[0]) */
857 /* goto end; */
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200858
Marc Kupietz969cab92019-08-05 11:13:42 +0200859 qsort(best, N * para_threads, sizeof(collocator), cmp_activation);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200860
861 long long chosen[MAX_NEIGHBOURS];
862 printf("N: %ld\n", N);
863
Marc Kupietz969cab92019-08-05 11:13:42 +0200864 AV *array = newAV();
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200865 int i, j;
Marc Kupietz969cab92019-08-05 11:13:42 +0200866 int l1_words = 0, l2_words = 0;
867
868 for (a = 0, i = 0; i < N && a < N * para_threads; a++) {
869 int filtered = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200870 long long c = best[a].wordi;
871 if ((merge_words && dedupe && i > 1) || (!merge_words && dedupe && i > 0)) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200872 for (j = 0; j < i && !filtered; j++)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200873 if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) ||
874 strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200875 printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]);
876 filtered = 1;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200877 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200878 if (filtered)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200879 continue;
880 }
881
Marc Kupietz969cab92019-08-05 11:13:42 +0200882 if (0 && merge_words > 0) {
883 if (c >= merge_words) {
884 if (l1_words > N / 2)
885 continue;
886 else
887 l1_words++;
888 } else {
889 if (l2_words > N / 2)
890 continue;
891 else
892 l2_words++;
893 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200894 }
895
Marc Kupietz969cab92019-08-05 11:13:42 +0200896 // printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
897 // fflush(stdout);
898 HV *hash = newHV();
899 SV *word = newSVpvf(&vocab[c * max_w], 0);
900 chosen[i] = c;
901 if (latin_enc == 0) SvUTF8_on(word);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200902 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200903 hv_store(hash, "word", strlen("word"), word, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200904 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
905 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
906 AV *vector = newAV();
907 for (b = 0; b < size; b++) {
908 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
909 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200910 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV *)vector), 0);
911 av_push(array, newRV_noinc((SV *)hash));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200912 i++;
913 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200914 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV *)array), 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200915
Marc Kupietz969cab92019-08-05 11:13:42 +0200916 for (b = 0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200917 best[b].wordi = -1L;
918 best[b].activation = 0;
919 best[b].probability = 0;
920 best[b].position = 0;
921 best[b].activation_sum = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200922 memset(best[b].heat, 0, sizeof(float) * 16);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200923 }
924
Marc Kupietz969cab92019-08-05 11:13:42 +0200925 float total_activation = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200926
927 if (M2) {
928 printf("Waiting for syn threads to join\n");
929 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200930 for (a = 0; a < syn_threads; a++) pthread_join(pt[a + para_threads], (void *)&syn_nbs[a]);
931 for (a = 0; a <= syn_threads; a++) {
932 if (a == window) continue;
933 total_activation += window_sums[a];
934 printf("window pos: %d, sum: %f\n", a, window_sums[a]);
935 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200936 printf("syn threads joint\n");
937 fflush(stdout);
938
Marc Kupietz969cab92019-08-05 11:13:42 +0200939 for (b = 0; b < syn_nbs[0]->length; b++) {
940 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
941 best[b].position = -1; // syn_nbs[0]->pos[b];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200942 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietz969cab92019-08-05 11:13:42 +0200943 best[b].max_activation = 0.0;
944 best[b].average = 0.0;
945 best[b].probability = 0.0;
946 best[b].cprobability = syn_nbs[0]->best[b].cprobability;
947 memset(best[b].heat, 0, sizeof(float) * 16);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200948 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200949
950 float best_window_sum[MAX_NEIGHBOURS];
951 int found_index = 0, i = 0, j, w;
952 for (a = 0; a < syn_threads; a++) {
953 for (b = 0; b < syn_nbs[a]->length; b++) {
954 for (i = 0; i < found_index; i++)
955 if (best[i].wordi == syn_nbs[a]->best[b].wordi)
956 break;
957 if (i >= found_index) {
958 best[found_index].max_activation = 0.0;
959 best[found_index].average = 0.0;
960 best[found_index].probability = 0.0;
961 memset(best[found_index].heat, 0, sizeof(float) * 16);
962 best[found_index].cprobability = syn_nbs[a]->best[b].cprobability;
963 best[found_index].activation_sum = target_sums[syn_nbs[a]->best[b].wordi]; // syn_nbs[a]->best[b].activation_sum;
964 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
965 // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
966 }
967 }
968 }
969 sort_by = 0; // ALWAYS AUTO-FOCUS
970 if (sort_by != 1 && sort_by != 2) { // sort by auto focus mean
971 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) - 1);
972 int wpos;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200973 int bits_set = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200974 for (i = 0; i < found_index; i++) {
975 best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0;
976 for (w = 1; w < (1 << syn_threads); w++) { // loop through all possible windows
977 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 +0200978 bits_set = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200979 for (a = 0; a < syn_threads; a++) {
980 if ((1 << a) & w) {
981 wpos = (a >= window ? a + 1 : a);
982 total_window_sum += window_sums[wpos];
983 }
984 }
985 // printf("%d window-sum %f\n", w, total_window_sum);
986 for (a = 0; a < syn_threads; a++) {
987 if ((1 << a) & w) {
988 wpos = (a >= window ? a + 1 : a);
989 bits_set++;
990 for (b = 0; b < syn_nbs[a]->length; b++)
991 if (best[i].wordi == syn_nbs[a]->best[b].wordi) {
992 // float acti = syn_nbs[a]->best[b].activation / total_window_sum;
993 // word_window_sum += syn_nbs[a]->dist[b] * syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
994 // word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
995 // 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 +0200996
Marc Kupietz969cab92019-08-05 11:13:42 +0200997 word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
998 // 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 +0200999
Marc Kupietz969cab92019-08-05 11:13:42 +02001000 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 +02001001 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 +02001002 word_activation_sum += syn_nbs[a]->best[b].activation;
1003 if (syn_nbs[a]->best[b].activation > best[i].max_activation)
1004 best[i].max_activation = syn_nbs[a]->best[b].activation;
1005 if (syn_nbs[a]->best[b].activation > best[i].heat[wpos])
1006 best[i].heat[wpos] = syn_nbs[a]->best[b].activation;
1007 }
1008 }
1009 }
1010 if (bits_set) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001011 word_window_average /= bits_set;
Marc Kupietz969cab92019-08-05 11:13:42 +02001012 // word_activation_sum /= bits_set;
1013 // word_window_sum /= bits_set;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001014 }
1015
Marc Kupietz969cab92019-08-05 11:13:42 +02001016 word_window_sum /= total_window_sum;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001017
Marc Kupietz969cab92019-08-05 11:13:42 +02001018 if (word_window_sum > best[i].probability) {
1019 // best[i].position = w;
1020 best[i].probability = word_window_sum;
1021 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001022
Marc Kupietz969cab92019-08-05 11:13:42 +02001023 if (word_cprobability_sum > best[i].cprobability_sum) {
1024 best[i].position = w;
1025 best[i].cprobability_sum = word_cprobability_sum;
1026 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001027
Marc Kupietz969cab92019-08-05 11:13:42 +02001028 best[i].average = word_window_average;
1029 // best[i].activation = word_activation_sum;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001030 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001031 }
1032 qsort(best, found_index, sizeof(collocator), cmp_probability);
1033 // for(i=0; i < found_index; i++) {
1034 // printf("found: %s - sum: %f - window: %d\n", &vocab[best[i].wordi * max_w], best[i].activation, best[i].position);
1035 // }
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001036
Marc Kupietz969cab92019-08-05 11:13:42 +02001037 } else if (sort_by == 1) { // responsiveness any window position
1038 int wpos;
1039 for (i = 0; i < found_index; i++) {
1040 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
1041 for (a = 0; a < syn_threads; a++) {
1042 wpos = (a >= window ? a + 1 : a);
1043 for (b = 0; b < syn_nbs[a]->length; b++)
1044 if (best[i].wordi == syn_nbs[a]->best[b].wordi) {
1045 best[i].probability += syn_nbs[a]->best[b].probability;
1046 if (syn_nbs[a]->best[b].activation > 0.25)
1047 best[i].position |= 1 << wpos;
1048 if (syn_nbs[a]->best[b].activation > best[i].activation) {
1049 best[i].activation = syn_nbs[a]->best[b].activation;
1050 }
1051 }
1052 }
1053 }
1054 qsort(best, found_index, sizeof(collocator), cmp_activation);
1055 } else if (sort_by == 2) { // single window position
1056 for (a = 1; a < syn_threads; a++) {
1057 for (b = 0; b < syn_nbs[a]->length; b++) {
1058 for (c = 0; c < MAX_NEIGHBOURS; c++) {
1059 if (syn_nbs[a]->best[b].activation > best[c].activation) {
1060 for (d = MAX_NEIGHBOURS - 1; d > c; d--) {
1061 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001062 }
1063 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
Marc Kupietz969cab92019-08-05 11:13:42 +02001064 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 +02001065 break;
1066 }
1067 }
1068 }
1069 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001070 } else { // sort by mean p
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 (target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
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 << 2 * window) - 1; // syn_nbs[a]->pos[b];
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001080 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
1081 break;
1082 }
1083 }
1084 }
1085 }
1086 }
1087 array = newAV();
Marc Kupietz969cab92019-08-05 11:13:42 +02001088 for (a = 0, i = 0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001089 long long c = best[a].wordi;
Marc Kupietz969cab92019-08-05 11:13:42 +02001090 /*
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001091 if (dedupe) {
1092 int filtered=0;
1093 for (j=0; j<i; j++)
1094 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
1095 strcasestr(chosen[j], &vocab[c * max_w])) {
1096 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
1097 filtered = 1;
1098 }
1099 if(filtered)
1100 continue;
1101 }
1102*/
Marc Kupietz969cab92019-08-05 11:13:42 +02001103 chosen[i++] = c;
1104 HV *hash = newHV();
1105 SV *word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
1106 AV *heat = newAV();
1107 if (latin_enc == 0) SvUTF8_on(word);
1108 hv_store(hash, "word", strlen("word"), word, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001109 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
1110 hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0);
1111 hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0);
1112 hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0);
Marc Kupietz969cab92019-08-05 11:13:42 +02001113 hv_store(hash, "max", strlen("max"), newSVnv(best[a].max_activation), 0); // newSVnv(target_sums[best[a].wordi]), 0);
1114 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 +02001115 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietz969cab92019-08-05 11:13:42 +02001116 best[a].heat[5] = 0;
1117 for (i = 10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i]));
1118 hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV *)heat), 0);
1119 av_push(array, newRV_noinc((SV *)hash));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001120 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001121 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV *)array), 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001122 }
1123end:
1124 // words = old_words; // why was this here?
Marc Kupietz969cab92019-08-05 11:13:42 +02001125 free(best);
1126 return newRV_noinc((SV *)result);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001127}
1128
1129int dump_vecs(char *fname) {
Marc Kupietz969cab92019-08-05 11:13:42 +02001130 long i, j;
1131 FILE *f;
1132 /* if(words>100000)
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001133 words=100000;
1134*/
Marc Kupietz969cab92019-08-05 11:13:42 +02001135 if ((f = fopen(fname, "w")) == NULL) {
1136 fprintf(stderr, "cannot open %s for writing\n", fname);
1137 return (-1);
1138 }
1139 fprintf(f, "%lld %lld\n", words, size);
1140 for (i = 0; i < words; i++) {
1141 fprintf(f, "%s ", &vocab[i * max_w]);
1142 for (j = 0; j < size - 1; j++)
1143 fprintf(f, "%f ", M[i * size + j]);
1144 fprintf(f, "%f\n", M[i * size + j]);
1145 }
1146 fclose(f);
1147 return (0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001148}
1149
1150int dump_for_numpy(char *fname) {
Marc Kupietz969cab92019-08-05 11:13:42 +02001151 long i, j;
1152 FILE *f;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001153 int max = 300000;
1154
Marc Kupietz969cab92019-08-05 11:13:42 +02001155 if ((f = fopen(fname, "w")) == NULL) {
1156 fprintf(stderr, "cannot open %s for writing\n", fname);
1157 return (-1);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001158 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001159 for (i = 0; i < max; i++) {
1160 for (j = 0; j < size - 1; j++)
1161 fprintf(f, "%f\t", M[i * size + j]);
1162 fprintf(f, "%f\n", M[i * size + j]);
1163 printf("%s\r\n", &vocab[i * max_w]);
1164 }
1165 if (merged_end > 0) {
1166 for (i = 0; i < max; i++) {
1167 for (j = 0; j < size - 1; j++)
1168 fprintf(f, "%f\t", M[(merged_end + i) * size + j]);
1169 fprintf(f, "%f\n", M[(merged_end + i) * size + j]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001170 printf("_%s\r\n", &vocab[i * max_w]);
1171 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001172 }
1173 fclose(f);
1174 return (0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001175}