blob: cc8734c370fd50ce82ffe9fb535e8b581f68cc44 [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
117int init_net(char *file_name, char *net_name, int latin) {
118 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 Kupietz969cab92019-08-05 11:13:42 +0200221 char collocatordb_name[2048];
222 strcpy(collocatordb_name, net_name);
223 char *ext = rindex(collocatordb_name, '.');
224 if (ext) {
225 strcpy(ext, ".rocksdb");
226 if (access(collocatordb_name, R_OK) == 0) {
227 *ext = 0;
228 fprintf(stderr, "Opening collocator DB %s\n", collocatordb_name);
229 cdb = open_collocatordb(collocatordb_name);
230 }
231 }
232 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200233
Marc Kupietz969cab92019-08-05 11:13:42 +0200234 expTable = (float *)malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
235 for (i = 0; i < EXP_TABLE_SIZE; i++) {
236 expTable[i] = exp((i / (float)EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
237 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
238 }
239 window_sums = malloc(sizeof(float) * (window + 1) * 2);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200240
241 return 0;
242}
243
Marc Kupietz969cab92019-08-05 11:13:42 +0200244long mergeVectors(char *file_name) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200245 FILE *f, *binvecs, *binwords;
Marc Kupietz969cab92019-08-05 11:13:42 +0200246 int binwords_fd, binvecs_fd, net_fd, i;
247 long long a, b, c, d, cn;
248 float len;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200249 float *merge_vecs;
250 char *merge_vocab;
Marc Kupietz969cab92019-08-05 11:13:42 +0200251 /* long long merge_words, merge_size; */
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200252 long long merge_size;
253
Marc Kupietz969cab92019-08-05 11:13:42 +0200254 char binvecs_fname[256], binwords_fname[256];
255 strcpy(binwords_fname, file_name);
256 strcat(binwords_fname, ".words");
257 strcpy(binvecs_fname, file_name);
258 strcat(binvecs_fname, ".vecs");
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200259
260 f = fopen(file_name, "rb");
261 if (f == NULL) {
262 printf("Input file %s not found\n", file_name);
Marc Kupietz969cab92019-08-05 11:13:42 +0200263 exit - 1;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200264 }
265 fscanf(f, "%lld", &merge_words);
266 fscanf(f, "%lld", &merge_size);
Marc Kupietz969cab92019-08-05 11:13:42 +0200267 if (merge_size != size) {
268 fprintf(stderr, "vectors must have the same length\n");
269 exit(-1);
270 }
271 if ((binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
272 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
273 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200274 if (merge_vecs == NULL || merge_vocab == NULL) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200275 close(binvecs_fd);
276 close(binwords_fd);
277 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
278 exit(-1);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200279 }
280 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
281 read(binwords_fd, merge_vocab, merge_words * max_w);
Marc Kupietz969cab92019-08-05 11:13:42 +0200282 } else {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200283 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
284 exit(-1);
Marc Kupietz969cab92019-08-05 11:13:42 +0200285 }
286 printf("Successfully reallocated memory\nMerging...\n");
287 fflush(stdout);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200288 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
289 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
290 munmap(M, words * size * sizeof(float));
291 munmap(vocab, words * max_w);
292 M = merge_vecs;
293 vocab = merge_vocab;
294 merged_end = merge_words;
295 words += merge_words;
296 fclose(f);
Marc Kupietz969cab92019-08-05 11:13:42 +0200297 printf("merged_end: %lld, words: %lld\n", merged_end, words);
298 //printBiggestMergedDifferences();
299 return ((long)merged_end);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200300}
301
302void filter_garbage() {
303 long i;
304 unsigned char *w, previous, c;
305 garbage = malloc(words);
306 memset(garbage, 0, words);
307 for (i = 0; i < words; i++) {
308 w = vocab + i * max_w;
309 previous = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200310 if (strncmp("quot", w, 4) == 0) {
311 garbage[i] = 1;
312 // printf("Gargabe: %s\n", vocab + i * max_w);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200313 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200314 while ((c = *w++) && !garbage[i]) {
315 if (((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200316 (previous == '-' && (c & 32)) ||
Marc Kupietz969cab92019-08-05 11:13:42 +0200317 (previous == 0xc2 && (c == 0xa4 || c == 0xb6)) ||
318 (previous == 'q' && c == 'u' && *(w) == 'o' && *(w + 1) == 't') || /* quot */
319 c == '<') {
320 garbage[i] = 1;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200321 continue;
322 }
323 previous = c;
324 }
325 }
326 }
327 return;
328}
329
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200330knn *simpleGetCollocators(int word, int number, long cutoff, int *result) {
331 knnpars *pars = calloc(sizeof(knnpars), 1);
332 float *target_sums;
Marc Kupietz969cab92019-08-05 11:13:42 +0200333 float *window_sums = malloc(sizeof(float) * (window + 1) * 2);
334 pars->cutoff = (cutoff ? cutoff : 300000);
335 long a = posix_memalign((void **)&target_sums, 128, pars->cutoff * sizeof(float));
336 for (a = 0; a < cutoff; a++)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200337 target_sums[a] = 0;
338 pars->target_sums = target_sums;
339 pars->window_sums = window_sums;
Marc Kupietz969cab92019-08-05 11:13:42 +0200340 pars->N = (number ? number : 20);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200341 pars->from = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200342 pars->upto = window * 2 - 1;
343 knn *syn_nbs = NULL; // = (knn*) getCollocators(pars);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200344 free(pars);
345 free(window_sums);
346 free(target_sums);
347 return syn_nbs;
348}
349
350void *getCollocators(void *args) {
351 knnpars *pars = args;
Marc Kupietz969cab92019-08-05 11:13:42 +0200352 int N = pars->N;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200353
354 int cc = pars->wl->wordi[0];
Marc Kupietz969cab92019-08-05 11:13:42 +0200355 knn *nbs = NULL;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200356 long window_layer_size = size * window * 2;
Marc Kupietz969cab92019-08-05 11:13:42 +0200357 long a, b, c, d, e, window_offset, target, max_target = 0, maxmax_target;
358 float f, max_f, maxmax_f;
359 float *target_sums = NULL, worstbest, wpos_sum;
360 collocator *best;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200361
Marc Kupietz969cab92019-08-05 11:13:42 +0200362 if (M2 == NULL || cc == -1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200363 return NULL;
364
Marc Kupietz969cab92019-08-05 11:13:42 +0200365 a = posix_memalign((void **)&target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200366 memset(target_sums, 0, pars->cutoff * sizeof(float));
Marc Kupietz969cab92019-08-05 11:13:42 +0200367 best = malloc((N > 200 ? N : 200) * sizeof(collocator));
368 memset(best, 0, (N > 200 ? N : 200) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200369 worstbest = pars->threshold;
370
371 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200372 target_sums[b] = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200373 for (b = 0; b < N; b++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200374 best[b].wordi = -1;
375 best[b].probability = 1;
376 best[b].activation = worstbest;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200377 }
378
379 d = cc;
380 maxmax_f = -1;
381 maxmax_target = 0;
382
383 for (a = pars->from; a < pars->upto; a++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200384 if (a >= window)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200385 a++;
386 wpos_sum = 0;
387 printf("window pos: %ld\n", a);
388 if (a != window) {
389 max_f = -1;
390 window_offset = a * size;
391 if (a > window)
392 window_offset -= size;
Marc Kupietz969cab92019-08-05 11:13:42 +0200393 for (target = 0; target < pars->cutoff; target++) {
394 if (garbage && garbage[target]) continue;
395 if (target == d)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200396 continue;
397 f = 0;
398 for (c = 0; c < size; c++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200399 f += M2[d * size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200400 if (f < -MAX_EXP)
401 continue;
402 else if (f > MAX_EXP)
403 continue;
404 else
Marc Kupietz969cab92019-08-05 11:13:42 +0200405 f = expTable[(int)((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200406 wpos_sum += f;
407
408 target_sums[target] += f;
Marc Kupietz969cab92019-08-05 11:13:42 +0200409 if (f > worstbest) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200410 for (b = 0; b < N; b++) {
411 if (f > best[b].activation) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200412 memmove(best + b + 1, best + b, (N - b - 1) * sizeof(collocator));
413 best[b].activation = f;
414 best[b].wordi = target;
415 best[b].position = window - a;
416 break;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200417 }
418 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200419 if (b == N - 1)
420 worstbest = best[N - 1].activation;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200421 }
422 }
423 printf("%d %.2f\n", max_target, max_f);
424 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
Marc Kupietz969cab92019-08-05 11:13:42 +0200425 if (max_f > maxmax_f) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200426 maxmax_f = max_f;
427 maxmax_target = max_target;
428 }
429 for (b = 0; b < N; b++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200430 if (best[b].position == window - a)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200431 best[b].cprobability = best[b].activation / wpos_sum;
432 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200433 printf("\x1b[1m%s\x1b[0m ", &vocab[d * max_w]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200434 }
435 pars->window_sums[a] = wpos_sum;
436 }
437 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200438 pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200439
440 free(target_sums);
Marc Kupietz969cab92019-08-05 11:13:42 +0200441 for (b = 0; b < N && best[b].wordi >= 0; b++)
442 ;
443 ; // THIS LOOP IS NEEDED (b...)
444 // printf("%d: best syn: %s %.2f %.5f\n", b, &vocab[best[b].wordi*max_w], best[b].activation, best[b].probability);
445 // printf("\n");
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200446 nbs = malloc(sizeof(knn));
Marc Kupietz969cab92019-08-05 11:13:42 +0200447 nbs->best = best;
448 nbs->length = b - 1;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200449 pthread_exit(nbs);
450}
451
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200452AV *getVecs(AV *array) {
453 int i, b;
454 AV *result = newAV();
Marc Kupietz969cab92019-08-05 11:13:42 +0200455 for (i = 0; i <= av_len(array); i++) {
456 SV **elem = av_fetch(array, i, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200457 if (elem != NULL) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200458 long j = (long)SvNV(*elem);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200459 AV *vector = newAV();
460 for (b = 0; b < size; b++) {
461 av_push(vector, newSVnv(M[b + j * size]));
462 }
463 av_push(result, newRV_noinc(vector));
464 }
465 }
466 return result;
467}
468
469char *getSimilarProfiles(long node) {
470 int i;
471 char buffer[120000];
472 char pair_buffer[2048];
Marc Kupietz969cab92019-08-05 11:13:42 +0200473 buffer[0] = '[';
474 buffer[1] = 0;
475 if (node >= sprofiles_qty) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200476 printf("Not available in precomputed profile\n");
Marc Kupietz969cab92019-08-05 11:13:42 +0200477 return (strdup("[{\"w\":\"not available\", \"v\":0}]\n"));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200478 }
479
480 printf("******* %s ******\n", &vocab[max_w * node]);
Marc Kupietz969cab92019-08-05 11:13:42 +0200481
482 for (i = 0; i < 100 && i < sprofiles[node].len; i++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200483 sprintf(pair_buffer, "{\"w\":\"%s\", \"v\":%f},", &vocab[max_w * (sprofiles[node].nbr[i].index)], sprofiles[node].nbr[i].value);
484 strcat(buffer, pair_buffer);
485 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200486 buffer[strlen(buffer) - 1] = ']';
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200487 strcat(buffer, "\n");
488 printf(buffer);
Marc Kupietz969cab92019-08-05 11:13:42 +0200489 return (strdup(buffer));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200490}
491
492char *getClassicCollocators(long node) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200493 char *res = (cdb ? strdup(get_collocators_as_json(cdb, node)) : "[]");
494 return res;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200495}
496
497wordlist *getTargetWords(char *st1, int search_backw) {
498 wordlist *wl = malloc(sizeof(wordlist));
499 char st[100][max_size], sep[100];
Marc Kupietz969cab92019-08-05 11:13:42 +0200500 long a, b = 0, c = 0, cn = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200501 int unmerged;
502
503 while (1) {
504 st[cn][b] = st1[c];
505 b++;
506 c++;
507 st[cn][b] = 0;
508 if (st1[c] == 0) break;
509 if (st1[c] == ' ' || st1[c] == '-') {
510 sep[cn++] = st1[c];
511 b = 0;
512 c++;
513 }
514 }
515 cn++;
516 for (a = 0; a < cn; a++) {
517 if (search_backw) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200518 for (b = words - 1; b >= (merge_words ? merge_words : 0) && strcmp(&vocab[b * max_w], st[a]) != 0; b--)
519 ;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200520 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200521 for (b = 0; b < (merge_words ? merge_words : words) && strcmp(&vocab[b * max_w], st[a]) != 0; b++)
522 ;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200523 }
524 if (b == words) b = -1;
525 wl->wordi[a] = b;
526 if (b == -1) {
527 fprintf(stderr, "Out of dictionary word!\n");
528 cn--;
529 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200530 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 +0200531 }
532 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200533 wl->length = cn;
534 return (wl);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200535}
536
537float get_distance(long b, long c) {
538 long a;
539 float dist = 0;
540 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + b * size];
541 return dist;
542}
543
Marc Kupietz969cab92019-08-05 11:13:42 +0200544char *getBiggestMergedDifferences() {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200545 static char *result = NULL;
546 float dist, len, vec[max_size];
547 long long a, b, c, d, cn, *bi;
548 char ch;
549 knn *nbs = NULL;
550 int N = 1000;
551
Marc Kupietz969cab92019-08-05 11:13:42 +0200552 if (merged_end == 0)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200553 result = "[]";
Marc Kupietz969cab92019-08-05 11:13:42 +0200554
555 if (result != NULL)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200556 return result;
557
558 printf("Looking for biggest distances between main and merged vectors ...\n");
559 collocator *best;
560 best = malloc(N * sizeof(collocator));
561 memset(best, 0, N * sizeof(collocator));
562
Marc Kupietz969cab92019-08-05 11:13:42 +0200563 float worstbest = 1000000;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200564
565 for (a = 0; a < N; a++) best[a].activation = worstbest;
566
567 for (c = 0; c < 500000; c++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200568 if (garbage && garbage[c]) continue;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200569 a = 0;
570 dist = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200571 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + (c + merged_end) * size];
572 if (dist < worstbest) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200573 for (a = 0; a < N; a++) {
574 if (dist < best[a].activation) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200575 memmove(best + a + 1, best + a, (N - a - 1) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200576 best[a].activation = dist;
577 best[a].wordi = c;
578 break;
579 }
580 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200581 worstbest = best[N - 1].activation;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200582 }
583 }
584
Marc Kupietz969cab92019-08-05 11:13:42 +0200585 result = malloc(N * max_w);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200586 char *p = result;
Marc Kupietz969cab92019-08-05 11:13:42 +0200587 *p++ = '[';
588 *p = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200589 for (a = 0; a < N; a++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200590 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 +0200591 }
592 *--p = ']';
Marc Kupietz969cab92019-08-05 11:13:42 +0200593 return (result);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200594}
595
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200596float cos_similarity(long b, long c) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200597 float dist = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200598 long a;
Marc Kupietz969cab92019-08-05 11:13:42 +0200599 for (a = 0; a < size; a++) dist += M[b * size + a] * M[c * size + a];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200600 return dist;
601}
602
603char *cos_similarity_as_json(char *w1, char *w2) {
604 wordlist *a, *b;
605 float res;
606 a = getTargetWords(w1, 0);
607 b = getTargetWords(w2, 0);
Marc Kupietz969cab92019-08-05 11:13:42 +0200608 if (a == NULL || b == NULL || a->length != 1 || b->length != 1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200609 res = -1;
610 else
611 res = cos_similarity(a->wordi[0], b->wordi[0]);
612 fprintf(stderr, "a: %lld b: %lld res:%f\n", a->wordi[0], b->wordi[0], res);
613 char *json = malloc(16);
614 sprintf(json, "%.5f", res);
615 return json;
616}
617
618void *_get_neighbours(void *arg) {
619 knnpars *pars = arg;
Marc Kupietz969cab92019-08-05 11:13:42 +0200620 char *st1 = pars->token;
621 int N = pars->N;
622 long from = pars->from;
623 unsigned long upto = pars->upto;
624 char file_name[max_size], st[100][max_size], *sep;
625 float dist, len, vec[max_size];
626 long long a, b, c, d, cn, *bi;
627 char ch;
628 knn *nbs = NULL;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200629 wordlist *wl = pars->wl;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200630
Marc Kupietz969cab92019-08-05 11:13:42 +0200631 collocator *best = pars->best;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200632
Marc Kupietz969cab92019-08-05 11:13:42 +0200633 float worstbest = -1;
634
635 for (a = 0; a < N; a++) best[a].activation = 0;
636 a = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200637 bi = wl->wordi;
Marc Kupietz969cab92019-08-05 11:13:42 +0200638 cn = wl->length;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200639 sep = wl->sep;
Marc Kupietz969cab92019-08-05 11:13:42 +0200640 b = bi[0];
641 c = 0;
642 if (b == -1) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200643 N = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200644 goto end;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200645 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200646 for (a = 0; a < size; a++) vec[a] = 0;
647 for (b = 0; b < cn; b++) {
648 if (bi[b] == -1) continue;
649 if (b > 0 && sep[b - 1] == '-')
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200650 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
651 else
652 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietz969cab92019-08-05 11:13:42 +0200653 }
654 len = 0;
655 for (a = 0; a < size; a++) len += vec[a] * vec[a];
656 len = sqrt(len);
657 for (a = 0; a < size; a++) vec[a] /= len;
658 for (a = 0; a < N; a++) best[a].activation = -1;
659 for (c = from; c < upto; c++) {
660 if (garbage && garbage[c]) continue;
661 a = 0;
662 // do not skip taget word
663 // for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
664 // if (a == 1) continue;
665 dist = 0;
666 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
667 if (dist > worstbest) {
668 for (a = 0; a < N; a++) {
669 if (dist > best[a].activation) {
670 memmove(best + a + 1, best + a, (N - a - 1) * sizeof(collocator));
671 best[a].activation = dist;
672 best[a].wordi = c;
673 break;
674 }
675 }
676 worstbest = best[N - 1].activation;
677 }
678 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200679
680end:
Marc Kupietz969cab92019-08-05 11:13:42 +0200681 pthread_exit(nbs);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200682}
683
Marc Kupietz969cab92019-08-05 11:13:42 +0200684int cmp_activation(const void *a, const void *b) {
685 float fb = ((collocator *)a)->activation;
686 float fa = ((collocator *)b)->activation;
687 return (fa > fb) - (fa < fb);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200688}
689
Marc Kupietz969cab92019-08-05 11:13:42 +0200690int cmp_probability(const void *a, const void *b) {
691 float fb = ((collocator *)a)->probability;
692 float fa = ((collocator *)b)->probability;
693 return (fa > fb) - (fa < fb);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200694}
695
Marc Kupietz969cab92019-08-05 11:13:42 +0200696char *getPosWiseW2VCollocatorsAsTsv(char *word, long maxPerPos, long cutoff, float threshold) {
697 HV *result = newHV();
698 float *target_sums = NULL, vec[max_size];
699 long long old_words;
700 long a, b, c, d;
701 knn *para_nbs[MAX_THREADS];
702 knn *syn_nbs[MAX_THREADS];
703 knnpars pars[MAX_THREADS];
704 pthread_t *pt = (pthread_t *)malloc((num_threads + 1) * sizeof(pthread_t));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200705 wordlist *wl;
Marc Kupietz969cab92019-08-05 11:13:42 +0200706 int syn_threads = (M2 ? window * 2 : 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200707 int search_backw = 0;
708 collocator *best = NULL;
Marc Kupietz969cab92019-08-05 11:13:42 +0200709 posix_memalign((void **)&best, 128, 10 * (maxPerPos >= 200 ? maxPerPos : 200) * sizeof(collocator));
710 memset(best, 0, (maxPerPos >= 200 ? maxPerPos : 200) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200711
Marc Kupietz969cab92019-08-05 11:13:42 +0200712 if (cutoff < 1 || cutoff > words)
713 cutoff = words;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200714
715 wl = getTargetWords(word, search_backw);
Marc Kupietz969cab92019-08-05 11:13:42 +0200716 if (wl == NULL || wl->length < 1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200717 return "";
718
Marc Kupietz969cab92019-08-05 11:13:42 +0200719 a = posix_memalign((void **)&target_sums, 128, cutoff * sizeof(float));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200720 memset(target_sums, 0, cutoff * sizeof(float));
721
722 printf("Starting %d threads\n", syn_threads);
723 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200724 for (a = 0; a < syn_threads; a++) {
725 pars[a].cutoff = cutoff;
726 pars[a].target_sums = target_sums;
727 pars[a].window_sums = window_sums;
728 pars[a].wl = wl;
729 pars[a].N = maxPerPos;
730 pars[a].threshold = threshold;
731 pars[a].from = a;
732 pars[a].upto = a + 1;
733 pthread_create(&pt[a], NULL, getCollocators, (void *)&pars[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200734 }
735 printf("Waiting for syn threads to join\n");
736 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200737 for (a = 0; a < syn_threads; a++) pthread_join(pt[a], (void *)&syn_nbs[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200738 printf("Syn threads joint\n");
739 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200740 result = malloc(maxPerPos * 80 * syn_threads);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200741 char *p = result;
742 *p = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200743 for (a = syn_threads - 1; a >= 0; a--) {
744 for (b = 0; b < syn_nbs[a]->length; b++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200745 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);
746 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200747 }
748 return (result);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200749}
750
751SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe, int no_similar_profiles) {
752 HV *result = newHV();
Marc Kupietz969cab92019-08-05 11:13:42 +0200753 float *target_sums = NULL, vec[max_size];
754 long long old_words;
755 long a, b, c, d, slice;
756 knn *para_nbs[MAX_THREADS];
757 knn *syn_nbs[MAX_THREADS];
758 knnpars pars[MAX_THREADS];
759 pthread_t *pt = (pthread_t *)malloc((num_threads + 1) * sizeof(pthread_t));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200760 wordlist *wl;
Marc Kupietz969cab92019-08-05 11:13:42 +0200761 int syn_threads = (M2 ? window * 2 : 0);
762 int para_threads = (no_similar_profiles ? 0 : num_threads - syn_threads);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200763
764 collocator *best = NULL;
Marc Kupietz969cab92019-08-05 11:13:42 +0200765 posix_memalign((void **)&best, 128, 10 * (N >= 200 ? N : 200) * sizeof(collocator));
766 memset(best, 0, (N >= 200 ? N : 200) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200767
Marc Kupietz969cab92019-08-05 11:13:42 +0200768 if (N > MAX_NEIGHBOURS) N = MAX_NEIGHBOURS;
769
770 if (cutoff < 1 || cutoff > words)
771 cutoff = words;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200772
773 wl = getTargetWords(st1, search_backw);
Marc Kupietz969cab92019-08-05 11:13:42 +0200774 if (wl == NULL || wl->length < 1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200775 goto end;
776
Marc Kupietz969cab92019-08-05 11:13:42 +0200777 old_words = cutoff;
778 slice = cutoff / para_threads;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200779
Marc Kupietz969cab92019-08-05 11:13:42 +0200780 a = posix_memalign((void **)&target_sums, 128, cutoff * sizeof(float));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200781 memset(target_sums, 0, cutoff * sizeof(float));
782
783 printf("Starting %d threads\n", para_threads);
784 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200785 for (a = 0; a < para_threads; a++) {
786 pars[a].cutoff = cutoff;
787 pars[a].token = st1;
788 pars[a].wl = wl;
789 pars[a].N = N;
790 pars[a].best = &best[N * a];
791 if (merge_words == 0 || search_backw == 0) {
792 pars[a].from = a * slice;
793 pars[a].upto = ((a + 1) * slice > cutoff ? cutoff : (a + 1) * slice);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200794 } else {
795 pars[a].from = merge_words + a * slice;
Marc Kupietz969cab92019-08-05 11:13:42 +0200796 pars[a].upto = merge_words + ((a + 1) * slice > cutoff ? cutoff : (a + 1) * slice);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200797 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200798 printf("From: %ld, Upto: %ld\n", pars[a].from, pars[a].upto);
799 pthread_create(&pt[a], NULL, _get_neighbours, (void *)&pars[a]);
800 }
801 if (M2) {
802 for (a = 0; a < syn_threads; a++) {
803 pars[a + para_threads].cutoff = cutoff;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200804 pars[a + para_threads].target_sums = target_sums;
805 pars[a + para_threads].window_sums = window_sums;
806 pars[a + para_threads].wl = wl;
807 pars[a + para_threads].N = N;
808 pars[a + para_threads].threshold = MIN_RESP;
809 pars[a + para_threads].from = a;
Marc Kupietz969cab92019-08-05 11:13:42 +0200810 pars[a + para_threads].upto = a + 1;
811 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *)&pars[a + para_threads]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200812 }
813 }
814 printf("Waiting for para threads to join\n");
815 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200816 for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *)&para_nbs[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200817 printf("Para threads joint\n");
818 fflush(stdout);
819
Marc Kupietz969cab92019-08-05 11:13:42 +0200820 /* if(!syn_nbs[0]) */
821 /* goto end; */
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200822
Marc Kupietz969cab92019-08-05 11:13:42 +0200823 qsort(best, N * para_threads, sizeof(collocator), cmp_activation);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200824
825 long long chosen[MAX_NEIGHBOURS];
826 printf("N: %ld\n", N);
827
Marc Kupietz969cab92019-08-05 11:13:42 +0200828 AV *array = newAV();
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200829 int i, j;
Marc Kupietz969cab92019-08-05 11:13:42 +0200830 int l1_words = 0, l2_words = 0;
831
832 for (a = 0, i = 0; i < N && a < N * para_threads; a++) {
833 int filtered = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200834 long long c = best[a].wordi;
835 if ((merge_words && dedupe && i > 1) || (!merge_words && dedupe && i > 0)) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200836 for (j = 0; j < i && !filtered; j++)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200837 if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) ||
838 strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200839 printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]);
840 filtered = 1;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200841 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200842 if (filtered)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200843 continue;
844 }
845
Marc Kupietz969cab92019-08-05 11:13:42 +0200846 if (0 && merge_words > 0) {
847 if (c >= merge_words) {
848 if (l1_words > N / 2)
849 continue;
850 else
851 l1_words++;
852 } else {
853 if (l2_words > N / 2)
854 continue;
855 else
856 l2_words++;
857 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200858 }
859
Marc Kupietz969cab92019-08-05 11:13:42 +0200860 // printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
861 // fflush(stdout);
862 HV *hash = newHV();
863 SV *word = newSVpvf(&vocab[c * max_w], 0);
864 chosen[i] = c;
865 if (latin_enc == 0) SvUTF8_on(word);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200866 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200867 hv_store(hash, "word", strlen("word"), word, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200868 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
869 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
870 AV *vector = newAV();
871 for (b = 0; b < size; b++) {
872 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
873 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200874 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV *)vector), 0);
875 av_push(array, newRV_noinc((SV *)hash));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200876 i++;
877 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200878 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV *)array), 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200879
Marc Kupietz969cab92019-08-05 11:13:42 +0200880 for (b = 0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200881 best[b].wordi = -1L;
882 best[b].activation = 0;
883 best[b].probability = 0;
884 best[b].position = 0;
885 best[b].activation_sum = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200886 memset(best[b].heat, 0, sizeof(float) * 16);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200887 }
888
Marc Kupietz969cab92019-08-05 11:13:42 +0200889 float total_activation = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200890
891 if (M2) {
892 printf("Waiting for syn threads to join\n");
893 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200894 for (a = 0; a < syn_threads; a++) pthread_join(pt[a + para_threads], (void *)&syn_nbs[a]);
895 for (a = 0; a <= syn_threads; a++) {
896 if (a == window) continue;
897 total_activation += window_sums[a];
898 printf("window pos: %d, sum: %f\n", a, window_sums[a]);
899 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200900 printf("syn threads joint\n");
901 fflush(stdout);
902
Marc Kupietz969cab92019-08-05 11:13:42 +0200903 for (b = 0; b < syn_nbs[0]->length; b++) {
904 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
905 best[b].position = -1; // syn_nbs[0]->pos[b];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200906 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietz969cab92019-08-05 11:13:42 +0200907 best[b].max_activation = 0.0;
908 best[b].average = 0.0;
909 best[b].probability = 0.0;
910 best[b].cprobability = syn_nbs[0]->best[b].cprobability;
911 memset(best[b].heat, 0, sizeof(float) * 16);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200912 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200913
914 float best_window_sum[MAX_NEIGHBOURS];
915 int found_index = 0, i = 0, j, w;
916 for (a = 0; a < syn_threads; a++) {
917 for (b = 0; b < syn_nbs[a]->length; b++) {
918 for (i = 0; i < found_index; i++)
919 if (best[i].wordi == syn_nbs[a]->best[b].wordi)
920 break;
921 if (i >= found_index) {
922 best[found_index].max_activation = 0.0;
923 best[found_index].average = 0.0;
924 best[found_index].probability = 0.0;
925 memset(best[found_index].heat, 0, sizeof(float) * 16);
926 best[found_index].cprobability = syn_nbs[a]->best[b].cprobability;
927 best[found_index].activation_sum = target_sums[syn_nbs[a]->best[b].wordi]; // syn_nbs[a]->best[b].activation_sum;
928 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
929 // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
930 }
931 }
932 }
933 sort_by = 0; // ALWAYS AUTO-FOCUS
934 if (sort_by != 1 && sort_by != 2) { // sort by auto focus mean
935 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) - 1);
936 int wpos;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200937 int bits_set = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200938 for (i = 0; i < found_index; i++) {
939 best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0;
940 for (w = 1; w < (1 << syn_threads); w++) { // loop through all possible windows
941 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 +0200942 bits_set = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200943 for (a = 0; a < syn_threads; a++) {
944 if ((1 << a) & w) {
945 wpos = (a >= window ? a + 1 : a);
946 total_window_sum += window_sums[wpos];
947 }
948 }
949 // printf("%d window-sum %f\n", w, total_window_sum);
950 for (a = 0; a < syn_threads; a++) {
951 if ((1 << a) & w) {
952 wpos = (a >= window ? a + 1 : a);
953 bits_set++;
954 for (b = 0; b < syn_nbs[a]->length; b++)
955 if (best[i].wordi == syn_nbs[a]->best[b].wordi) {
956 // float acti = syn_nbs[a]->best[b].activation / total_window_sum;
957 // word_window_sum += syn_nbs[a]->dist[b] * syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
958 // word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
959 // 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 +0200960
Marc Kupietz969cab92019-08-05 11:13:42 +0200961 word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
962 // 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 +0200963
Marc Kupietz969cab92019-08-05 11:13:42 +0200964 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 +0200965 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 +0200966 word_activation_sum += syn_nbs[a]->best[b].activation;
967 if (syn_nbs[a]->best[b].activation > best[i].max_activation)
968 best[i].max_activation = syn_nbs[a]->best[b].activation;
969 if (syn_nbs[a]->best[b].activation > best[i].heat[wpos])
970 best[i].heat[wpos] = syn_nbs[a]->best[b].activation;
971 }
972 }
973 }
974 if (bits_set) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200975 word_window_average /= bits_set;
Marc Kupietz969cab92019-08-05 11:13:42 +0200976 // word_activation_sum /= bits_set;
977 // word_window_sum /= bits_set;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200978 }
979
Marc Kupietz969cab92019-08-05 11:13:42 +0200980 word_window_sum /= total_window_sum;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200981
Marc Kupietz969cab92019-08-05 11:13:42 +0200982 if (word_window_sum > best[i].probability) {
983 // best[i].position = w;
984 best[i].probability = word_window_sum;
985 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200986
Marc Kupietz969cab92019-08-05 11:13:42 +0200987 if (word_cprobability_sum > best[i].cprobability_sum) {
988 best[i].position = w;
989 best[i].cprobability_sum = word_cprobability_sum;
990 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200991
Marc Kupietz969cab92019-08-05 11:13:42 +0200992 best[i].average = word_window_average;
993 // best[i].activation = word_activation_sum;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200994 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200995 }
996 qsort(best, found_index, sizeof(collocator), cmp_probability);
997 // for(i=0; i < found_index; i++) {
998 // printf("found: %s - sum: %f - window: %d\n", &vocab[best[i].wordi * max_w], best[i].activation, best[i].position);
999 // }
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001000
Marc Kupietz969cab92019-08-05 11:13:42 +02001001 } else if (sort_by == 1) { // responsiveness any window position
1002 int wpos;
1003 for (i = 0; i < found_index; i++) {
1004 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
1005 for (a = 0; a < syn_threads; a++) {
1006 wpos = (a >= window ? a + 1 : a);
1007 for (b = 0; b < syn_nbs[a]->length; b++)
1008 if (best[i].wordi == syn_nbs[a]->best[b].wordi) {
1009 best[i].probability += syn_nbs[a]->best[b].probability;
1010 if (syn_nbs[a]->best[b].activation > 0.25)
1011 best[i].position |= 1 << wpos;
1012 if (syn_nbs[a]->best[b].activation > best[i].activation) {
1013 best[i].activation = syn_nbs[a]->best[b].activation;
1014 }
1015 }
1016 }
1017 }
1018 qsort(best, found_index, sizeof(collocator), cmp_activation);
1019 } else if (sort_by == 2) { // single window position
1020 for (a = 1; a < syn_threads; a++) {
1021 for (b = 0; b < syn_nbs[a]->length; b++) {
1022 for (c = 0; c < MAX_NEIGHBOURS; c++) {
1023 if (syn_nbs[a]->best[b].activation > best[c].activation) {
1024 for (d = MAX_NEIGHBOURS - 1; d > c; d--) {
1025 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001026 }
1027 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
Marc Kupietz969cab92019-08-05 11:13:42 +02001028 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 +02001029 break;
1030 }
1031 }
1032 }
1033 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001034 } else { // sort by mean p
1035 for (a = 1; a < syn_threads; a++) {
1036 for (b = 0; b < syn_nbs[a]->length; b++) {
1037 for (c = 0; c < MAX_NEIGHBOURS; c++) {
1038 if (target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
1039 for (d = MAX_NEIGHBOURS - 1; d > c; d--) {
1040 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001041 }
1042 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
Marc Kupietz969cab92019-08-05 11:13:42 +02001043 best[c].position = (1 << 2 * window) - 1; // syn_nbs[a]->pos[b];
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001044 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
1045 break;
1046 }
1047 }
1048 }
1049 }
1050 }
1051 array = newAV();
Marc Kupietz969cab92019-08-05 11:13:42 +02001052 for (a = 0, i = 0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001053 long long c = best[a].wordi;
Marc Kupietz969cab92019-08-05 11:13:42 +02001054 /*
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001055 if (dedupe) {
1056 int filtered=0;
1057 for (j=0; j<i; j++)
1058 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
1059 strcasestr(chosen[j], &vocab[c * max_w])) {
1060 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
1061 filtered = 1;
1062 }
1063 if(filtered)
1064 continue;
1065 }
1066*/
Marc Kupietz969cab92019-08-05 11:13:42 +02001067 chosen[i++] = c;
1068 HV *hash = newHV();
1069 SV *word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
1070 AV *heat = newAV();
1071 if (latin_enc == 0) SvUTF8_on(word);
1072 hv_store(hash, "word", strlen("word"), word, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001073 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
1074 hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0);
1075 hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0);
1076 hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0);
Marc Kupietz969cab92019-08-05 11:13:42 +02001077 hv_store(hash, "max", strlen("max"), newSVnv(best[a].max_activation), 0); // newSVnv(target_sums[best[a].wordi]), 0);
1078 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 +02001079 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietz969cab92019-08-05 11:13:42 +02001080 best[a].heat[5] = 0;
1081 for (i = 10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i]));
1082 hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV *)heat), 0);
1083 av_push(array, newRV_noinc((SV *)hash));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001084 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001085 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV *)array), 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001086 }
1087end:
1088 // words = old_words; // why was this here?
Marc Kupietz969cab92019-08-05 11:13:42 +02001089 free(best);
1090 return newRV_noinc((SV *)result);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001091}
1092
1093int dump_vecs(char *fname) {
Marc Kupietz969cab92019-08-05 11:13:42 +02001094 long i, j;
1095 FILE *f;
1096 /* if(words>100000)
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001097 words=100000;
1098*/
Marc Kupietz969cab92019-08-05 11:13:42 +02001099 if ((f = fopen(fname, "w")) == NULL) {
1100 fprintf(stderr, "cannot open %s for writing\n", fname);
1101 return (-1);
1102 }
1103 fprintf(f, "%lld %lld\n", words, size);
1104 for (i = 0; i < words; i++) {
1105 fprintf(f, "%s ", &vocab[i * max_w]);
1106 for (j = 0; j < size - 1; j++)
1107 fprintf(f, "%f ", M[i * size + j]);
1108 fprintf(f, "%f\n", M[i * size + j]);
1109 }
1110 fclose(f);
1111 return (0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001112}
1113
1114int dump_for_numpy(char *fname) {
Marc Kupietz969cab92019-08-05 11:13:42 +02001115 long i, j;
1116 FILE *f;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001117 int max = 300000;
1118
Marc Kupietz969cab92019-08-05 11:13:42 +02001119 if ((f = fopen(fname, "w")) == NULL) {
1120 fprintf(stderr, "cannot open %s for writing\n", fname);
1121 return (-1);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001122 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001123 for (i = 0; i < max; i++) {
1124 for (j = 0; j < size - 1; j++)
1125 fprintf(f, "%f\t", M[i * size + j]);
1126 fprintf(f, "%f\n", M[i * size + j]);
1127 printf("%s\r\n", &vocab[i * max_w]);
1128 }
1129 if (merged_end > 0) {
1130 for (i = 0; i < max; i++) {
1131 for (j = 0; j < size - 1; j++)
1132 fprintf(f, "%f\t", M[(merged_end + i) * size + j]);
1133 fprintf(f, "%f\n", M[(merged_end + i) * size + j]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001134 printf("_%s\r\n", &vocab[i * max_w]);
1135 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001136 }
1137 fclose(f);
1138 return (0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001139}