blob: 3a8092caab84d7472e204e905a66db3f396de011 [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
Marc Kupietzcb43e492019-12-03 10:07:53 +0100537long getWordNumber(char *word) {
538 wordlist *wl = getTargetWords(word, 0);
539 if(wl->length > 0)
540 return(wl->wordi[0]);
541 return(0);
542}
543
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200544float get_distance(long b, long c) {
545 long a;
546 float dist = 0;
547 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + b * size];
548 return dist;
549}
550
Marc Kupietz969cab92019-08-05 11:13:42 +0200551char *getBiggestMergedDifferences() {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200552 static char *result = NULL;
553 float dist, len, vec[max_size];
554 long long a, b, c, d, cn, *bi;
555 char ch;
556 knn *nbs = NULL;
557 int N = 1000;
558
Marc Kupietz969cab92019-08-05 11:13:42 +0200559 if (merged_end == 0)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200560 result = "[]";
Marc Kupietz969cab92019-08-05 11:13:42 +0200561
562 if (result != NULL)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200563 return result;
564
565 printf("Looking for biggest distances between main and merged vectors ...\n");
566 collocator *best;
567 best = malloc(N * sizeof(collocator));
568 memset(best, 0, N * sizeof(collocator));
569
Marc Kupietz969cab92019-08-05 11:13:42 +0200570 float worstbest = 1000000;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200571
572 for (a = 0; a < N; a++) best[a].activation = worstbest;
573
574 for (c = 0; c < 500000; c++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200575 if (garbage && garbage[c]) continue;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200576 a = 0;
577 dist = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200578 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + (c + merged_end) * size];
579 if (dist < worstbest) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200580 for (a = 0; a < N; a++) {
581 if (dist < best[a].activation) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200582 memmove(best + a + 1, best + a, (N - a - 1) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200583 best[a].activation = dist;
584 best[a].wordi = c;
585 break;
586 }
587 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200588 worstbest = best[N - 1].activation;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200589 }
590 }
591
Marc Kupietz969cab92019-08-05 11:13:42 +0200592 result = malloc(N * max_w);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200593 char *p = result;
Marc Kupietz969cab92019-08-05 11:13:42 +0200594 *p++ = '[';
595 *p = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200596 for (a = 0; a < N; a++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200597 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 +0200598 }
599 *--p = ']';
Marc Kupietz969cab92019-08-05 11:13:42 +0200600 return (result);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200601}
602
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200603float cos_similarity(long b, long c) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200604 float dist = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200605 long a;
Marc Kupietz969cab92019-08-05 11:13:42 +0200606 for (a = 0; a < size; a++) dist += M[b * size + a] * M[c * size + a];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200607 return dist;
608}
609
610char *cos_similarity_as_json(char *w1, char *w2) {
611 wordlist *a, *b;
612 float res;
613 a = getTargetWords(w1, 0);
614 b = getTargetWords(w2, 0);
Marc Kupietz969cab92019-08-05 11:13:42 +0200615 if (a == NULL || b == NULL || a->length != 1 || b->length != 1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200616 res = -1;
617 else
618 res = cos_similarity(a->wordi[0], b->wordi[0]);
619 fprintf(stderr, "a: %lld b: %lld res:%f\n", a->wordi[0], b->wordi[0], res);
620 char *json = malloc(16);
621 sprintf(json, "%.5f", res);
622 return json;
623}
624
625void *_get_neighbours(void *arg) {
626 knnpars *pars = arg;
Marc Kupietz969cab92019-08-05 11:13:42 +0200627 char *st1 = pars->token;
628 int N = pars->N;
629 long from = pars->from;
630 unsigned long upto = pars->upto;
631 char file_name[max_size], st[100][max_size], *sep;
632 float dist, len, vec[max_size];
633 long long a, b, c, d, cn, *bi;
634 char ch;
635 knn *nbs = NULL;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200636 wordlist *wl = pars->wl;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200637
Marc Kupietz969cab92019-08-05 11:13:42 +0200638 collocator *best = pars->best;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200639
Marc Kupietz969cab92019-08-05 11:13:42 +0200640 float worstbest = -1;
641
642 for (a = 0; a < N; a++) best[a].activation = 0;
643 a = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200644 bi = wl->wordi;
Marc Kupietz969cab92019-08-05 11:13:42 +0200645 cn = wl->length;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200646 sep = wl->sep;
Marc Kupietz969cab92019-08-05 11:13:42 +0200647 b = bi[0];
648 c = 0;
649 if (b == -1) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200650 N = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200651 goto end;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200652 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200653 for (a = 0; a < size; a++) vec[a] = 0;
654 for (b = 0; b < cn; b++) {
655 if (bi[b] == -1) continue;
656 if (b > 0 && sep[b - 1] == '-')
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200657 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
658 else
659 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietz969cab92019-08-05 11:13:42 +0200660 }
661 len = 0;
662 for (a = 0; a < size; a++) len += vec[a] * vec[a];
663 len = sqrt(len);
664 for (a = 0; a < size; a++) vec[a] /= len;
665 for (a = 0; a < N; a++) best[a].activation = -1;
666 for (c = from; c < upto; c++) {
667 if (garbage && garbage[c]) continue;
668 a = 0;
669 // do not skip taget word
670 // for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
671 // if (a == 1) continue;
672 dist = 0;
673 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
674 if (dist > worstbest) {
675 for (a = 0; a < N; a++) {
676 if (dist > best[a].activation) {
677 memmove(best + a + 1, best + a, (N - a - 1) * sizeof(collocator));
678 best[a].activation = dist;
679 best[a].wordi = c;
680 break;
681 }
682 }
683 worstbest = best[N - 1].activation;
684 }
685 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200686
687end:
Marc Kupietz969cab92019-08-05 11:13:42 +0200688 pthread_exit(nbs);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200689}
690
Marc Kupietz969cab92019-08-05 11:13:42 +0200691int cmp_activation(const void *a, const void *b) {
692 float fb = ((collocator *)a)->activation;
693 float fa = ((collocator *)b)->activation;
694 return (fa > fb) - (fa < fb);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200695}
696
Marc Kupietz969cab92019-08-05 11:13:42 +0200697int cmp_probability(const void *a, const void *b) {
698 float fb = ((collocator *)a)->probability;
699 float fa = ((collocator *)b)->probability;
700 return (fa > fb) - (fa < fb);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200701}
702
Marc Kupietz969cab92019-08-05 11:13:42 +0200703char *getPosWiseW2VCollocatorsAsTsv(char *word, long maxPerPos, long cutoff, float threshold) {
704 HV *result = newHV();
705 float *target_sums = NULL, vec[max_size];
706 long long old_words;
707 long a, b, c, d;
708 knn *para_nbs[MAX_THREADS];
709 knn *syn_nbs[MAX_THREADS];
710 knnpars pars[MAX_THREADS];
711 pthread_t *pt = (pthread_t *)malloc((num_threads + 1) * sizeof(pthread_t));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200712 wordlist *wl;
Marc Kupietz969cab92019-08-05 11:13:42 +0200713 int syn_threads = (M2 ? window * 2 : 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200714 int search_backw = 0;
715 collocator *best = NULL;
Marc Kupietz969cab92019-08-05 11:13:42 +0200716 posix_memalign((void **)&best, 128, 10 * (maxPerPos >= 200 ? maxPerPos : 200) * sizeof(collocator));
717 memset(best, 0, (maxPerPos >= 200 ? maxPerPos : 200) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200718
Marc Kupietz969cab92019-08-05 11:13:42 +0200719 if (cutoff < 1 || cutoff > words)
720 cutoff = words;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200721
722 wl = getTargetWords(word, search_backw);
Marc Kupietz969cab92019-08-05 11:13:42 +0200723 if (wl == NULL || wl->length < 1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200724 return "";
725
Marc Kupietz969cab92019-08-05 11:13:42 +0200726 a = posix_memalign((void **)&target_sums, 128, cutoff * sizeof(float));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200727 memset(target_sums, 0, cutoff * sizeof(float));
728
729 printf("Starting %d threads\n", syn_threads);
730 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200731 for (a = 0; a < syn_threads; a++) {
732 pars[a].cutoff = cutoff;
733 pars[a].target_sums = target_sums;
734 pars[a].window_sums = window_sums;
735 pars[a].wl = wl;
736 pars[a].N = maxPerPos;
737 pars[a].threshold = threshold;
738 pars[a].from = a;
739 pars[a].upto = a + 1;
740 pthread_create(&pt[a], NULL, getCollocators, (void *)&pars[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200741 }
742 printf("Waiting for syn threads to join\n");
743 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200744 for (a = 0; a < syn_threads; a++) pthread_join(pt[a], (void *)&syn_nbs[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200745 printf("Syn threads joint\n");
746 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200747 result = malloc(maxPerPos * 80 * syn_threads);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200748 char *p = result;
749 *p = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200750 for (a = syn_threads - 1; a >= 0; a--) {
751 for (b = 0; b < syn_nbs[a]->length; b++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200752 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);
753 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200754 }
755 return (result);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200756}
757
758SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe, int no_similar_profiles) {
759 HV *result = newHV();
Marc Kupietz969cab92019-08-05 11:13:42 +0200760 float *target_sums = NULL, vec[max_size];
761 long long old_words;
762 long a, b, c, d, slice;
763 knn *para_nbs[MAX_THREADS];
764 knn *syn_nbs[MAX_THREADS];
765 knnpars pars[MAX_THREADS];
766 pthread_t *pt = (pthread_t *)malloc((num_threads + 1) * sizeof(pthread_t));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200767 wordlist *wl;
Marc Kupietz969cab92019-08-05 11:13:42 +0200768 int syn_threads = (M2 ? window * 2 : 0);
769 int para_threads = (no_similar_profiles ? 0 : num_threads - syn_threads);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200770
771 collocator *best = NULL;
Marc Kupietz969cab92019-08-05 11:13:42 +0200772 posix_memalign((void **)&best, 128, 10 * (N >= 200 ? N : 200) * sizeof(collocator));
773 memset(best, 0, (N >= 200 ? N : 200) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200774
Marc Kupietz969cab92019-08-05 11:13:42 +0200775 if (N > MAX_NEIGHBOURS) N = MAX_NEIGHBOURS;
776
777 if (cutoff < 1 || cutoff > words)
778 cutoff = words;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200779
780 wl = getTargetWords(st1, search_backw);
Marc Kupietz969cab92019-08-05 11:13:42 +0200781 if (wl == NULL || wl->length < 1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200782 goto end;
783
Marc Kupietz969cab92019-08-05 11:13:42 +0200784 old_words = cutoff;
785 slice = cutoff / para_threads;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200786
Marc Kupietz969cab92019-08-05 11:13:42 +0200787 a = posix_memalign((void **)&target_sums, 128, cutoff * sizeof(float));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200788 memset(target_sums, 0, cutoff * sizeof(float));
789
790 printf("Starting %d threads\n", para_threads);
791 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200792 for (a = 0; a < para_threads; a++) {
793 pars[a].cutoff = cutoff;
794 pars[a].token = st1;
795 pars[a].wl = wl;
796 pars[a].N = N;
797 pars[a].best = &best[N * a];
798 if (merge_words == 0 || search_backw == 0) {
799 pars[a].from = a * slice;
800 pars[a].upto = ((a + 1) * slice > cutoff ? cutoff : (a + 1) * slice);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200801 } else {
802 pars[a].from = merge_words + a * slice;
Marc Kupietz969cab92019-08-05 11:13:42 +0200803 pars[a].upto = merge_words + ((a + 1) * slice > cutoff ? cutoff : (a + 1) * slice);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200804 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200805 printf("From: %ld, Upto: %ld\n", pars[a].from, pars[a].upto);
806 pthread_create(&pt[a], NULL, _get_neighbours, (void *)&pars[a]);
807 }
808 if (M2) {
809 for (a = 0; a < syn_threads; a++) {
810 pars[a + para_threads].cutoff = cutoff;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200811 pars[a + para_threads].target_sums = target_sums;
812 pars[a + para_threads].window_sums = window_sums;
813 pars[a + para_threads].wl = wl;
814 pars[a + para_threads].N = N;
815 pars[a + para_threads].threshold = MIN_RESP;
816 pars[a + para_threads].from = a;
Marc Kupietz969cab92019-08-05 11:13:42 +0200817 pars[a + para_threads].upto = a + 1;
818 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *)&pars[a + para_threads]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200819 }
820 }
821 printf("Waiting for para threads to join\n");
822 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200823 for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *)&para_nbs[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200824 printf("Para threads joint\n");
825 fflush(stdout);
826
Marc Kupietz969cab92019-08-05 11:13:42 +0200827 /* if(!syn_nbs[0]) */
828 /* goto end; */
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200829
Marc Kupietz969cab92019-08-05 11:13:42 +0200830 qsort(best, N * para_threads, sizeof(collocator), cmp_activation);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200831
832 long long chosen[MAX_NEIGHBOURS];
833 printf("N: %ld\n", N);
834
Marc Kupietz969cab92019-08-05 11:13:42 +0200835 AV *array = newAV();
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200836 int i, j;
Marc Kupietz969cab92019-08-05 11:13:42 +0200837 int l1_words = 0, l2_words = 0;
838
839 for (a = 0, i = 0; i < N && a < N * para_threads; a++) {
840 int filtered = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200841 long long c = best[a].wordi;
842 if ((merge_words && dedupe && i > 1) || (!merge_words && dedupe && i > 0)) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200843 for (j = 0; j < i && !filtered; j++)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200844 if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) ||
845 strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200846 printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]);
847 filtered = 1;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200848 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200849 if (filtered)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200850 continue;
851 }
852
Marc Kupietz969cab92019-08-05 11:13:42 +0200853 if (0 && merge_words > 0) {
854 if (c >= merge_words) {
855 if (l1_words > N / 2)
856 continue;
857 else
858 l1_words++;
859 } else {
860 if (l2_words > N / 2)
861 continue;
862 else
863 l2_words++;
864 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200865 }
866
Marc Kupietz969cab92019-08-05 11:13:42 +0200867 // printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
868 // fflush(stdout);
869 HV *hash = newHV();
870 SV *word = newSVpvf(&vocab[c * max_w], 0);
871 chosen[i] = c;
872 if (latin_enc == 0) SvUTF8_on(word);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200873 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200874 hv_store(hash, "word", strlen("word"), word, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200875 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
876 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
877 AV *vector = newAV();
878 for (b = 0; b < size; b++) {
879 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
880 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200881 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV *)vector), 0);
882 av_push(array, newRV_noinc((SV *)hash));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200883 i++;
884 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200885 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV *)array), 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200886
Marc Kupietz969cab92019-08-05 11:13:42 +0200887 for (b = 0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200888 best[b].wordi = -1L;
889 best[b].activation = 0;
890 best[b].probability = 0;
891 best[b].position = 0;
892 best[b].activation_sum = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200893 memset(best[b].heat, 0, sizeof(float) * 16);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200894 }
895
Marc Kupietz969cab92019-08-05 11:13:42 +0200896 float total_activation = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200897
898 if (M2) {
899 printf("Waiting for syn threads to join\n");
900 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200901 for (a = 0; a < syn_threads; a++) pthread_join(pt[a + para_threads], (void *)&syn_nbs[a]);
902 for (a = 0; a <= syn_threads; a++) {
903 if (a == window) continue;
904 total_activation += window_sums[a];
905 printf("window pos: %d, sum: %f\n", a, window_sums[a]);
906 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200907 printf("syn threads joint\n");
908 fflush(stdout);
909
Marc Kupietz969cab92019-08-05 11:13:42 +0200910 for (b = 0; b < syn_nbs[0]->length; b++) {
911 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
912 best[b].position = -1; // syn_nbs[0]->pos[b];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200913 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietz969cab92019-08-05 11:13:42 +0200914 best[b].max_activation = 0.0;
915 best[b].average = 0.0;
916 best[b].probability = 0.0;
917 best[b].cprobability = syn_nbs[0]->best[b].cprobability;
918 memset(best[b].heat, 0, sizeof(float) * 16);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200919 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200920
921 float best_window_sum[MAX_NEIGHBOURS];
922 int found_index = 0, i = 0, j, w;
923 for (a = 0; a < syn_threads; a++) {
924 for (b = 0; b < syn_nbs[a]->length; b++) {
925 for (i = 0; i < found_index; i++)
926 if (best[i].wordi == syn_nbs[a]->best[b].wordi)
927 break;
928 if (i >= found_index) {
929 best[found_index].max_activation = 0.0;
930 best[found_index].average = 0.0;
931 best[found_index].probability = 0.0;
932 memset(best[found_index].heat, 0, sizeof(float) * 16);
933 best[found_index].cprobability = syn_nbs[a]->best[b].cprobability;
934 best[found_index].activation_sum = target_sums[syn_nbs[a]->best[b].wordi]; // syn_nbs[a]->best[b].activation_sum;
935 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
936 // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
937 }
938 }
939 }
940 sort_by = 0; // ALWAYS AUTO-FOCUS
941 if (sort_by != 1 && sort_by != 2) { // sort by auto focus mean
942 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) - 1);
943 int wpos;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200944 int bits_set = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200945 for (i = 0; i < found_index; i++) {
946 best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0;
947 for (w = 1; w < (1 << syn_threads); w++) { // loop through all possible windows
948 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 +0200949 bits_set = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200950 for (a = 0; a < syn_threads; a++) {
951 if ((1 << a) & w) {
952 wpos = (a >= window ? a + 1 : a);
953 total_window_sum += window_sums[wpos];
954 }
955 }
956 // printf("%d window-sum %f\n", w, total_window_sum);
957 for (a = 0; a < syn_threads; a++) {
958 if ((1 << a) & w) {
959 wpos = (a >= window ? a + 1 : a);
960 bits_set++;
961 for (b = 0; b < syn_nbs[a]->length; b++)
962 if (best[i].wordi == syn_nbs[a]->best[b].wordi) {
963 // float acti = syn_nbs[a]->best[b].activation / total_window_sum;
964 // word_window_sum += syn_nbs[a]->dist[b] * syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
965 // word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
966 // 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 +0200967
Marc Kupietz969cab92019-08-05 11:13:42 +0200968 word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
969 // 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 +0200970
Marc Kupietz969cab92019-08-05 11:13:42 +0200971 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 +0200972 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 +0200973 word_activation_sum += syn_nbs[a]->best[b].activation;
974 if (syn_nbs[a]->best[b].activation > best[i].max_activation)
975 best[i].max_activation = syn_nbs[a]->best[b].activation;
976 if (syn_nbs[a]->best[b].activation > best[i].heat[wpos])
977 best[i].heat[wpos] = syn_nbs[a]->best[b].activation;
978 }
979 }
980 }
981 if (bits_set) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200982 word_window_average /= bits_set;
Marc Kupietz969cab92019-08-05 11:13:42 +0200983 // word_activation_sum /= bits_set;
984 // word_window_sum /= bits_set;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200985 }
986
Marc Kupietz969cab92019-08-05 11:13:42 +0200987 word_window_sum /= total_window_sum;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200988
Marc Kupietz969cab92019-08-05 11:13:42 +0200989 if (word_window_sum > best[i].probability) {
990 // best[i].position = w;
991 best[i].probability = word_window_sum;
992 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200993
Marc Kupietz969cab92019-08-05 11:13:42 +0200994 if (word_cprobability_sum > best[i].cprobability_sum) {
995 best[i].position = w;
996 best[i].cprobability_sum = word_cprobability_sum;
997 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200998
Marc Kupietz969cab92019-08-05 11:13:42 +0200999 best[i].average = word_window_average;
1000 // best[i].activation = word_activation_sum;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001001 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001002 }
1003 qsort(best, found_index, sizeof(collocator), cmp_probability);
1004 // for(i=0; i < found_index; i++) {
1005 // printf("found: %s - sum: %f - window: %d\n", &vocab[best[i].wordi * max_w], best[i].activation, best[i].position);
1006 // }
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001007
Marc Kupietz969cab92019-08-05 11:13:42 +02001008 } else if (sort_by == 1) { // responsiveness any window position
1009 int wpos;
1010 for (i = 0; i < found_index; i++) {
1011 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
1012 for (a = 0; a < syn_threads; a++) {
1013 wpos = (a >= window ? a + 1 : a);
1014 for (b = 0; b < syn_nbs[a]->length; b++)
1015 if (best[i].wordi == syn_nbs[a]->best[b].wordi) {
1016 best[i].probability += syn_nbs[a]->best[b].probability;
1017 if (syn_nbs[a]->best[b].activation > 0.25)
1018 best[i].position |= 1 << wpos;
1019 if (syn_nbs[a]->best[b].activation > best[i].activation) {
1020 best[i].activation = syn_nbs[a]->best[b].activation;
1021 }
1022 }
1023 }
1024 }
1025 qsort(best, found_index, sizeof(collocator), cmp_activation);
1026 } else if (sort_by == 2) { // single window position
1027 for (a = 1; a < syn_threads; a++) {
1028 for (b = 0; b < syn_nbs[a]->length; b++) {
1029 for (c = 0; c < MAX_NEIGHBOURS; c++) {
1030 if (syn_nbs[a]->best[b].activation > best[c].activation) {
1031 for (d = MAX_NEIGHBOURS - 1; d > c; d--) {
1032 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001033 }
1034 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
Marc Kupietz969cab92019-08-05 11:13:42 +02001035 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 +02001036 break;
1037 }
1038 }
1039 }
1040 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001041 } else { // sort by mean p
1042 for (a = 1; a < syn_threads; a++) {
1043 for (b = 0; b < syn_nbs[a]->length; b++) {
1044 for (c = 0; c < MAX_NEIGHBOURS; c++) {
1045 if (target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
1046 for (d = MAX_NEIGHBOURS - 1; d > c; d--) {
1047 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001048 }
1049 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
Marc Kupietz969cab92019-08-05 11:13:42 +02001050 best[c].position = (1 << 2 * window) - 1; // syn_nbs[a]->pos[b];
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001051 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
1052 break;
1053 }
1054 }
1055 }
1056 }
1057 }
1058 array = newAV();
Marc Kupietz969cab92019-08-05 11:13:42 +02001059 for (a = 0, i = 0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001060 long long c = best[a].wordi;
Marc Kupietz969cab92019-08-05 11:13:42 +02001061 /*
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001062 if (dedupe) {
1063 int filtered=0;
1064 for (j=0; j<i; j++)
1065 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
1066 strcasestr(chosen[j], &vocab[c * max_w])) {
1067 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
1068 filtered = 1;
1069 }
1070 if(filtered)
1071 continue;
1072 }
1073*/
Marc Kupietz969cab92019-08-05 11:13:42 +02001074 chosen[i++] = c;
1075 HV *hash = newHV();
1076 SV *word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
1077 AV *heat = newAV();
1078 if (latin_enc == 0) SvUTF8_on(word);
1079 hv_store(hash, "word", strlen("word"), word, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001080 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
1081 hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0);
1082 hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0);
1083 hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0);
Marc Kupietz969cab92019-08-05 11:13:42 +02001084 hv_store(hash, "max", strlen("max"), newSVnv(best[a].max_activation), 0); // newSVnv(target_sums[best[a].wordi]), 0);
1085 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 +02001086 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietz969cab92019-08-05 11:13:42 +02001087 best[a].heat[5] = 0;
1088 for (i = 10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i]));
1089 hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV *)heat), 0);
1090 av_push(array, newRV_noinc((SV *)hash));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001091 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001092 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV *)array), 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001093 }
1094end:
1095 // words = old_words; // why was this here?
Marc Kupietz969cab92019-08-05 11:13:42 +02001096 free(best);
1097 return newRV_noinc((SV *)result);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001098}
1099
1100int dump_vecs(char *fname) {
Marc Kupietz969cab92019-08-05 11:13:42 +02001101 long i, j;
1102 FILE *f;
1103 /* if(words>100000)
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001104 words=100000;
1105*/
Marc Kupietz969cab92019-08-05 11:13:42 +02001106 if ((f = fopen(fname, "w")) == NULL) {
1107 fprintf(stderr, "cannot open %s for writing\n", fname);
1108 return (-1);
1109 }
1110 fprintf(f, "%lld %lld\n", words, size);
1111 for (i = 0; i < words; i++) {
1112 fprintf(f, "%s ", &vocab[i * max_w]);
1113 for (j = 0; j < size - 1; j++)
1114 fprintf(f, "%f ", M[i * size + j]);
1115 fprintf(f, "%f\n", M[i * size + j]);
1116 }
1117 fclose(f);
1118 return (0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001119}
1120
1121int dump_for_numpy(char *fname) {
Marc Kupietz969cab92019-08-05 11:13:42 +02001122 long i, j;
1123 FILE *f;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001124 int max = 300000;
1125
Marc Kupietz969cab92019-08-05 11:13:42 +02001126 if ((f = fopen(fname, "w")) == NULL) {
1127 fprintf(stderr, "cannot open %s for writing\n", fname);
1128 return (-1);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001129 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001130 for (i = 0; i < max; i++) {
1131 for (j = 0; j < size - 1; j++)
1132 fprintf(f, "%f\t", M[i * size + j]);
1133 fprintf(f, "%f\n", M[i * size + j]);
1134 printf("%s\r\n", &vocab[i * max_w]);
1135 }
1136 if (merged_end > 0) {
1137 for (i = 0; i < max; i++) {
1138 for (j = 0; j < size - 1; j++)
1139 fprintf(f, "%f\t", M[(merged_end + i) * size + j]);
1140 fprintf(f, "%f\n", M[(merged_end + i) * size + j]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001141 printf("_%s\r\n", &vocab[i * max_w]);
1142 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001143 }
1144 fclose(f);
1145 return (0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001146}