blob: e4cc096dbf8f667d421072269dc70bb5f57aa8eb [file] [log] [blame]
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001#include <collocatordb.h>
Marc Kupietz969cab92019-08-05 11:13:42 +02002#include <math.h>
3#include <pthread.h>
4#include <stdio.h>
Marc Kupietzc0d41872021-02-25 16:33:22 +01005#include <stdlib.h>
Marc Kupietz969cab92019-08-05 11:13:42 +02006#include <string.h>
7#include <sys/mman.h>
Marc Kupietze288d8e2024-11-15 16:18:50 +01008#include <fcntl.h>
9#include <unistd.h>
10#include <perl.h>
Marc Kupietzf11d20c2019-08-02 15:42:04 +020011
12#define max_size 2000
13#define max_w 50
14#define MAX_NEIGHBOURS 1000
15#define MAX_WORDS -1
16#define MAX_THREADS 100
17#define MAX_CC 50
18#define EXP_TABLE_SIZE 1000
19#define MAX_EXP 6
20#define MIN_RESP 0.50
21
22//the thread function
23void *connection_handler(void *);
24
25typedef struct {
Marc Kupietz969cab92019-08-05 11:13:42 +020026 long long wordi;
27 long position;
28 float activation;
29 float average;
30 float cprobability; // column wise probability
31 float cprobability_sum;
32 float probability;
33 float activation_sum;
34 float max_activation;
35 float heat[16];
Marc Kupietzf11d20c2019-08-02 15:42:04 +020036} collocator;
37
38typedef struct {
Marc Kupietz969cab92019-08-05 11:13:42 +020039 collocator *best;
40 int length;
Marc Kupietzf11d20c2019-08-02 15:42:04 +020041} knn;
Marc Kupietz969cab92019-08-05 11:13:42 +020042
Marc Kupietzf11d20c2019-08-02 15:42:04 +020043typedef struct {
44 long long wordi[MAX_NEIGHBOURS];
45 char sep[MAX_NEIGHBOURS];
46 int length;
47} wordlist;
48
49typedef struct {
50 long cutoff;
51 wordlist *wl;
Marc Kupietz969cab92019-08-05 11:13:42 +020052 char *token;
53 int N;
54 long from;
55 unsigned long upto;
Marc Kupietzf11d20c2019-08-02 15:42:04 +020056 collocator *best;
57 float *target_sums;
58 float *window_sums;
59 float threshold;
60} knnpars;
61
62typedef struct {
63 uint32_t index;
64 float value;
65} sparse_t;
66
67typedef struct {
68 uint32_t len;
69 sparse_t nbr[100];
70} profile_t;
71
Marc Kupietz969cab92019-08-05 11:13:42 +020072float *M, *M2 = 0L, *syn1neg_window, *expTable;
Marc Kupietzf11d20c2019-08-02 15:42:04 +020073float *window_sums;
74char *vocab;
75char *garbage = NULL;
76COLLOCATORDB *cdb = NULL;
77profile_t *sprofiles = NULL;
78size_t sprofiles_qty = 0;
79
80long long words, size, merged_end;
81long long merge_words = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +020082int num_threads = 20;
83int latin_enc = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +020084int window;
85
86/* load collocation profiles if file exists */
87int load_sprofiles(char *vecsname) {
88 char *basename = strdup(vecsname);
89 char *pos = strstr(basename, ".vecs");
Marc Kupietz969cab92019-08-05 11:13:42 +020090 if (pos)
91 *pos = 0;
92
Marc Kupietzf11d20c2019-08-02 15:42:04 +020093 char binsprofiles_fname[256];
94 strcpy(binsprofiles_fname, basename);
Marc Kupietz969cab92019-08-05 11:13:42 +020095 strcat(binsprofiles_fname, ".sprofiles.bin");
Marc Kupietzf11d20c2019-08-02 15:42:04 +020096 FILE *fp = fopen(binsprofiles_fname, "rb");
97 if (fp == NULL) {
98 printf("Collocation profiles %s not found. No problem.\n", binsprofiles_fname);
99 return 0;
100 }
101 fseek(fp, 0L, SEEK_END);
102 size_t sz = ftell(fp);
103 fclose(fp);
104
105 int fd = open(binsprofiles_fname, O_RDONLY);
Marc Kupietz969cab92019-08-05 11:13:42 +0200106 sprofiles = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200107 if (sprofiles == MAP_FAILED) {
108 close(fd);
109 fprintf(stderr, "Cannot mmap %s\n", binsprofiles_fname);
110 sprofiles = NULL;
111 return 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200112 } else {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200113 sprofiles_qty = sz / sizeof(profile_t);
114 fprintf(stderr, "Successfully mmaped %s containing similar profiles for %ld word forms.\n", binsprofiles_fname, sprofiles_qty);
115 }
116 return 1;
117}
118
Marc Kupietzc0d41872021-02-25 16:33:22 +0100119char *removeExtension(char* myStr) {
120 char *retStr;
121 char *lastExt;
122 if (myStr == NULL) return NULL;
123 if ((retStr = malloc (strlen (myStr) + 1)) == NULL) return NULL;
124 strcpy (retStr, myStr);
125 lastExt = strrchr (retStr, '.');
126 if (lastExt != NULL)
127 *lastExt = '\0';
128 return retStr;
129}
130
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200131int init_net(char *file_name, char *net_name, int latin, int do_open_cdb) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200132 FILE *f, *binvecs, *binwords;
Marc Kupietz969cab92019-08-05 11:13:42 +0200133 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz59865a92021-03-11 17:16:51 +0100134 long long a, b;
Marc Kupietz969cab92019-08-05 11:13:42 +0200135 float len;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200136 double val;
137
Marc Kupietzc0d41872021-02-25 16:33:22 +0100138 char binvecs_fname[1024], binwords_fname[1024];
139
140 if (strstr(file_name, ".txt")) {
141 strcpy(binwords_fname, removeExtension(file_name));
142 } else {
143 strcpy(binwords_fname, file_name);
144 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200145 strcat(binwords_fname, ".words");
146 strcpy(binvecs_fname, file_name);
147 strcat(binvecs_fname, ".vecs");
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200148
149 latin_enc = latin;
150 f = fopen(file_name, "rb");
151 if (f == NULL) {
152 printf("Input file %s not found\n", file_name);
153 return -1;
154 }
155 fscanf(f, "%lld", &words);
Marc Kupietz969cab92019-08-05 11:13:42 +0200156 if (MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200157 fscanf(f, "%lld", &size);
Marc Kupietz969cab92019-08-05 11:13:42 +0200158 if ((binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200159 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietz969cab92019-08-05 11:13:42 +0200160 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
161 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
162 if (M == NULL) {
163 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
164 return -1;
165 }
166 if (strstr(file_name, ".txt")) {
Marc Kupietzc0d41872021-02-25 16:33:22 +0100167 printf("%lld words in ascii vector file with vector size %lld\n", words, size);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200168 for (b = 0; b < words; b++) {
169 a = 0;
170 while (1) {
171 vocab[b * max_w + a] = fgetc(f);
172 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
173 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
174 }
175 vocab[b * max_w + a] = 0;
176 len = 0;
177 for (a = 0; a < size; a++) {
178 fscanf(f, "%lf", &val);
179 M[a + b * size] = val;
180 len += val * val;
Marc Kupietz969cab92019-08-05 11:13:42 +0200181 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200182 len = sqrt(len);
183 for (a = 0; a < size; a++) M[a + b * size] /= len;
184 }
185 } else {
186 for (b = 0; b < words; b++) {
187 a = 0;
188 while (1) {
189 vocab[b * max_w + a] = fgetc(f);
190 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
191 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
192 }
193 vocab[b * max_w + a] = 0;
194 fread(&M[b * size], sizeof(float), size, f);
195 len = 0;
196 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
197 len = sqrt(len);
198 for (a = 0; a < size; a++) M[a + b * size] /= len;
199 }
200 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200201 if ((binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
202 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
203 fclose(binvecs);
204 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
205 fclose(binwords);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200206 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200207 }
208 if ((binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
209 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
210 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
211 if (M == MAP_FAILED || vocab == MAP_FAILED) {
212 close(binvecs_fd);
213 close(binwords_fd);
214 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
215 exit(-1);
216 }
217 } else {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200218 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
219 exit(-1);
Marc Kupietz969cab92019-08-05 11:13:42 +0200220 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200221 fclose(f);
222
Marc Kupietz969cab92019-08-05 11:13:42 +0200223 if (net_name && strlen(net_name) > 0) {
224 if ((net_fd = open(net_name, O_RDONLY)) >= 0) {
225 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200226 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
227 // munmap(M, sizeof(float) * words * size);
228 M2 = mmap(0, sizeof(float) * words * size + sizeof(float) * 2 * window * size * words, PROT_READ, MAP_SHARED, net_fd, 0);
229 if (M2 == MAP_FAILED) {
230 close(net_fd);
231 fprintf(stderr, "Cannot mmap %s\n", net_name);
232 exit(-1);
233 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200234 syn1neg_window = M2 + words * size;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200235 } else {
236 fprintf(stderr, "Cannot open %s\n", net_name);
237 exit(-1);
238 }
239 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
240
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200241 if (do_open_cdb) {
242 char collocatordb_name[2048];
243 strcpy(collocatordb_name, net_name);
244 char *ext = rindex(collocatordb_name, '.');
245 if (ext) {
246 strcpy(ext, ".rocksdb");
247 if (access(collocatordb_name, R_OK) == 0) {
248 *ext = 0;
249 fprintf(stderr, "Opening collocator DB %s\n", collocatordb_name);
250 cdb = open_collocatordb(collocatordb_name);
Marc Kupietzc0d41872021-02-25 16:33:22 +0100251 } else {
252 fprintf(stderr, "Cannot open collocator DB %s\n", collocatordb_name);
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200253 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200254 }
255 }
256 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200257
Marc Kupietz969cab92019-08-05 11:13:42 +0200258 expTable = (float *)malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
259 for (i = 0; i < EXP_TABLE_SIZE; i++) {
260 expTable[i] = exp((i / (float)EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
261 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
262 }
263 window_sums = malloc(sizeof(float) * (window + 1) * 2);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200264
265 return 0;
266}
267
Marc Kupietz969cab92019-08-05 11:13:42 +0200268long mergeVectors(char *file_name) {
Marc Kupietz59865a92021-03-11 17:16:51 +0100269 FILE *f;
270 int binwords_fd, binvecs_fd;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200271 float *merge_vecs;
272 char *merge_vocab;
Marc Kupietz969cab92019-08-05 11:13:42 +0200273 /* long long merge_words, merge_size; */
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200274 long long merge_size;
275
Marc Kupietz969cab92019-08-05 11:13:42 +0200276 char binvecs_fname[256], binwords_fname[256];
Marc Kupietzc0d41872021-02-25 16:33:22 +0100277
278
Marc Kupietz969cab92019-08-05 11:13:42 +0200279 strcpy(binwords_fname, file_name);
280 strcat(binwords_fname, ".words");
281 strcpy(binvecs_fname, file_name);
282 strcat(binvecs_fname, ".vecs");
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200283
284 f = fopen(file_name, "rb");
285 if (f == NULL) {
286 printf("Input file %s not found\n", file_name);
Marc Kupietz59865a92021-03-11 17:16:51 +0100287 exit(-1);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200288 }
289 fscanf(f, "%lld", &merge_words);
290 fscanf(f, "%lld", &merge_size);
Marc Kupietz969cab92019-08-05 11:13:42 +0200291 if (merge_size != size) {
292 fprintf(stderr, "vectors must have the same length\n");
293 exit(-1);
294 }
295 if ((binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
296 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
297 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200298 if (merge_vecs == NULL || merge_vocab == NULL) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200299 close(binvecs_fd);
300 close(binwords_fd);
301 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
302 exit(-1);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200303 }
304 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
305 read(binwords_fd, merge_vocab, merge_words * max_w);
Marc Kupietz969cab92019-08-05 11:13:42 +0200306 } else {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200307 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
308 exit(-1);
Marc Kupietz969cab92019-08-05 11:13:42 +0200309 }
310 printf("Successfully reallocated memory\nMerging...\n");
311 fflush(stdout);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200312 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
313 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
314 munmap(M, words * size * sizeof(float));
315 munmap(vocab, words * max_w);
316 M = merge_vecs;
317 vocab = merge_vocab;
318 merged_end = merge_words;
319 words += merge_words;
320 fclose(f);
Marc Kupietz969cab92019-08-05 11:13:42 +0200321 printf("merged_end: %lld, words: %lld\n", merged_end, words);
322 //printBiggestMergedDifferences();
323 return ((long)merged_end);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200324}
325
326void filter_garbage() {
327 long i;
328 unsigned char *w, previous, c;
329 garbage = malloc(words);
330 memset(garbage, 0, words);
331 for (i = 0; i < words; i++) {
Marc Kupietz59865a92021-03-11 17:16:51 +0100332 w = (unsigned char *) vocab + i * max_w;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200333 previous = 0;
Marc Kupietz59865a92021-03-11 17:16:51 +0100334 if (strncmp("quot", (const char *)w, 4) == 0) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200335 garbage[i] = 1;
336 // printf("Gargabe: %s\n", vocab + i * max_w);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200337 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200338 while ((c = *w++) && !garbage[i]) {
339 if (((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200340 (previous == '-' && (c & 32)) ||
Marc Kupietz969cab92019-08-05 11:13:42 +0200341 (previous == 0xc2 && (c == 0xa4 || c == 0xb6)) ||
342 (previous == 'q' && c == 'u' && *(w) == 'o' && *(w + 1) == 't') || /* quot */
343 c == '<') {
344 garbage[i] = 1;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200345 continue;
346 }
347 previous = c;
348 }
349 }
350 }
351 return;
352}
353
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200354knn *simpleGetCollocators(int word, int number, long cutoff, int *result) {
355 knnpars *pars = calloc(sizeof(knnpars), 1);
Marc Kupietz59865a92021-03-11 17:16:51 +0100356 float *target_sums = NULL;
357 float *my_window_sums = malloc(sizeof(float) * (window + 1) * 2);
Marc Kupietz969cab92019-08-05 11:13:42 +0200358 pars->cutoff = (cutoff ? cutoff : 300000);
Marc Kupietz59865a92021-03-11 17:16:51 +0100359 long a;
Marc Kupietz969cab92019-08-05 11:13:42 +0200360 for (a = 0; a < cutoff; a++)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200361 target_sums[a] = 0;
362 pars->target_sums = target_sums;
Marc Kupietz59865a92021-03-11 17:16:51 +0100363 pars->window_sums = my_window_sums;
Marc Kupietz969cab92019-08-05 11:13:42 +0200364 pars->N = (number ? number : 20);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200365 pars->from = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200366 pars->upto = window * 2 - 1;
367 knn *syn_nbs = NULL; // = (knn*) getCollocators(pars);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200368 free(pars);
Marc Kupietz59865a92021-03-11 17:16:51 +0100369 free(my_window_sums);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200370 free(target_sums);
371 return syn_nbs;
372}
373
Marc Kupietz04135302026-07-30 14:40:02 +0900374/* Frees a knn result as returned by getCollocators() via pthread_exit(). */
375void free_knn(knn *nbs) {
376 if (nbs == NULL)
377 return;
378 free(nbs->best);
379 free(nbs);
380}
381
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200382void *getCollocators(void *args) {
383 knnpars *pars = args;
Marc Kupietz969cab92019-08-05 11:13:42 +0200384 int N = pars->N;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200385
386 int cc = pars->wl->wordi[0];
Marc Kupietz969cab92019-08-05 11:13:42 +0200387 knn *nbs = NULL;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200388 long window_layer_size = size * window * 2;
Marc Kupietz59865a92021-03-11 17:16:51 +0100389 long a, b, c, d, window_offset, target, max_target = 0, maxmax_target;
Marc Kupietz969cab92019-08-05 11:13:42 +0200390 float f, max_f, maxmax_f;
391 float *target_sums = NULL, worstbest, wpos_sum;
392 collocator *best;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200393
Marc Kupietz969cab92019-08-05 11:13:42 +0200394 if (M2 == NULL || cc == -1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200395 return NULL;
396
Marc Kupietz969cab92019-08-05 11:13:42 +0200397 a = posix_memalign((void **)&target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200398 memset(target_sums, 0, pars->cutoff * sizeof(float));
Marc Kupietz969cab92019-08-05 11:13:42 +0200399 best = malloc((N > 200 ? N : 200) * sizeof(collocator));
400 memset(best, 0, (N > 200 ? N : 200) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200401 worstbest = pars->threshold;
402
403 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200404 target_sums[b] = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200405 for (b = 0; b < N; b++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200406 best[b].wordi = -1;
407 best[b].probability = 1;
408 best[b].activation = worstbest;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200409 }
410
411 d = cc;
412 maxmax_f = -1;
413 maxmax_target = 0;
414
415 for (a = pars->from; a < pars->upto; a++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200416 if (a >= window)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200417 a++;
418 wpos_sum = 0;
419 printf("window pos: %ld\n", a);
420 if (a != window) {
421 max_f = -1;
422 window_offset = a * size;
423 if (a > window)
424 window_offset -= size;
Marc Kupietz969cab92019-08-05 11:13:42 +0200425 for (target = 0; target < pars->cutoff; target++) {
426 if (garbage && garbage[target]) continue;
427 if (target == d)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200428 continue;
429 f = 0;
430 for (c = 0; c < size; c++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200431 f += M2[d * size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200432 if (f < -MAX_EXP)
433 continue;
434 else if (f > MAX_EXP)
435 continue;
436 else
Marc Kupietz969cab92019-08-05 11:13:42 +0200437 f = expTable[(int)((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200438 wpos_sum += f;
439
440 target_sums[target] += f;
Marc Kupietz969cab92019-08-05 11:13:42 +0200441 if (f > worstbest) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200442 for (b = 0; b < N; b++) {
443 if (f > best[b].activation) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200444 memmove(best + b + 1, best + b, (N - b - 1) * sizeof(collocator));
445 best[b].activation = f;
446 best[b].wordi = target;
447 best[b].position = window - a;
448 break;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200449 }
450 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200451 if (b == N - 1)
452 worstbest = best[N - 1].activation;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200453 }
454 }
Marc Kupietz59865a92021-03-11 17:16:51 +0100455 printf("%ld %.2f\n", max_target, max_f);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200456 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
Marc Kupietz969cab92019-08-05 11:13:42 +0200457 if (max_f > maxmax_f) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200458 maxmax_f = max_f;
459 maxmax_target = max_target;
460 }
461 for (b = 0; b < N; b++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200462 if (best[b].position == window - a)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200463 best[b].cprobability = best[b].activation / wpos_sum;
464 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200465 printf("\x1b[1m%s\x1b[0m ", &vocab[d * max_w]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200466 }
467 pars->window_sums[a] = wpos_sum;
468 }
469 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200470 pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200471
472 free(target_sums);
Marc Kupietz969cab92019-08-05 11:13:42 +0200473 for (b = 0; b < N && best[b].wordi >= 0; b++)
474 ;
Marc Kupietz59865a92021-03-11 17:16:51 +0100475 // THIS LOOP IS NEEDED (b...)
Marc Kupietz969cab92019-08-05 11:13:42 +0200476 // printf("%d: best syn: %s %.2f %.5f\n", b, &vocab[best[b].wordi*max_w], best[b].activation, best[b].probability);
477 // printf("\n");
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200478 nbs = malloc(sizeof(knn));
Marc Kupietz969cab92019-08-05 11:13:42 +0200479 nbs->best = best;
480 nbs->length = b - 1;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200481 pthread_exit(nbs);
482}
483
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200484float getOutputWeight(int hidden, long target, int window_position) {
485 const long window_layer_size = size * window * 2;
486 int a;
487
488 if (window_position == 0 || window_position > window || window_position < -window) {
489 fprintf(stderr, "window_position: %d - assert: -%d <= window_position <= %d && window_position != 0 failed.\n", window_position, window, window);
490 exit(-1);
491 }
492
493 if (hidden >= size) {
Marc Kupietz59865a92021-03-11 17:16:51 +0100494 fprintf(stderr, "hidden: %d - assert: hidden < %lld failed.\n", hidden, size);
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200495 exit(-1);
496 }
497
498 if (target >= words) {
Marc Kupietz59865a92021-03-11 17:16:51 +0100499 fprintf(stderr, "target: %ld - assert: target < %lld failed.\n", target, words);
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200500 exit(-1);
501 }
502
503 a = window_position + window;
504 if (a > window) {
505 --a;
506 }
507 long window_offset = a * size;
508 return syn1neg_window[target * window_layer_size + window_offset + hidden];
509}
510
Marc Kupietz04135302026-07-30 14:40:02 +0900511/* Returns an SV* (not an AV*) on purpose: for an AV* return value Inline::C
512 generates newRV(), which leaves the array itself with a reference count of
513 one after the mortal reference is gone, i.e. it leaks the whole array on
514 every call. */
515SV *getVecs(AV *array) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200516 int i, b;
517 AV *result = newAV();
Marc Kupietz969cab92019-08-05 11:13:42 +0200518 for (i = 0; i <= av_len(array); i++) {
519 SV **elem = av_fetch(array, i, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200520 if (elem != NULL) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200521 long j = (long)SvNV(*elem);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200522 AV *vector = newAV();
523 for (b = 0; b < size; b++) {
524 av_push(vector, newSVnv(M[b + j * size]));
525 }
Marc Kupietzbdd779a2024-08-05 10:02:29 +0200526 av_push(result, newRV_noinc((SV *)vector));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200527 }
528 }
Marc Kupietz04135302026-07-30 14:40:02 +0900529 return newRV_noinc((SV *)result);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200530}
531
Marc Kupietz04135302026-07-30 14:40:02 +0900532/* All functions handing a string back to perl return an SV*, because for a
533 char* return value Inline::C only copies the string into the return SV and
534 never frees the buffer we allocated here. */
535SV *getSimilarProfiles(long node) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200536 int i;
537 char buffer[120000];
538 char pair_buffer[2048];
Marc Kupietz969cab92019-08-05 11:13:42 +0200539 buffer[0] = '[';
540 buffer[1] = 0;
541 if (node >= sprofiles_qty) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200542 printf("Not available in precomputed profile\n");
Marc Kupietz04135302026-07-30 14:40:02 +0900543 return newSVpv("[{\"w\":\"not available\", \"v\":0}]\n", 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200544 }
545
546 printf("******* %s ******\n", &vocab[max_w * node]);
Marc Kupietz969cab92019-08-05 11:13:42 +0200547
548 for (i = 0; i < 100 && i < sprofiles[node].len; i++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200549 sprintf(pair_buffer, "{\"w\":\"%s\", \"v\":%f},", &vocab[max_w * (sprofiles[node].nbr[i].index)], sprofiles[node].nbr[i].value);
550 strcat(buffer, pair_buffer);
551 }
Marc Kupietz04135302026-07-30 14:40:02 +0900552 if (i > 0)
553 buffer[strlen(buffer) - 1] = ']';
554 else
555 strcat(buffer, "]");
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200556 strcat(buffer, "\n");
Marc Kupietz59865a92021-03-11 17:16:51 +0100557 printf("%s", buffer);
Marc Kupietz04135302026-07-30 14:40:02 +0900558 return newSVpv(buffer, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200559}
560
Marc Kupietz04135302026-07-30 14:40:02 +0900561/* get_collocat*_as_json() hand out strdup()ed buffers that we own. */
562SV *getCollocationScores(long node, long collocate) {
563 char *json = (cdb ? (char *)get_collocation_scores_as_json(cdb, node, collocate) : NULL);
564 SV *res = newSVpv(json ? json : "[]", 0);
565 free(json);
566 return res;
Marc Kupietzf6080012021-03-12 09:14:42 +0100567}
568
Marc Kupietz04135302026-07-30 14:40:02 +0900569SV *getClassicCollocators(long node) {
570 char *json = (cdb ? (char *)get_collocators_as_json(cdb, node) : NULL);
571 SV *res = newSVpv(json ? json : "[]", 0);
572 free(json);
Marc Kupietz969cab92019-08-05 11:13:42 +0200573 return res;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200574}
575
576wordlist *getTargetWords(char *st1, int search_backw) {
577 wordlist *wl = malloc(sizeof(wordlist));
Marc Kupietz59865a92021-03-11 17:16:51 +0100578 char st[100][max_size];
Marc Kupietz969cab92019-08-05 11:13:42 +0200579 long a, b = 0, c = 0, cn = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200580
581 while (1) {
582 st[cn][b] = st1[c];
583 b++;
584 c++;
585 st[cn][b] = 0;
586 if (st1[c] == 0) break;
Marc Kupietzc0d41872021-02-25 16:33:22 +0100587 if (st1[c] == ' ' /*|| st1[c] == '-'*/) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200588 b = 0;
589 c++;
590 }
591 }
592 cn++;
593 for (a = 0; a < cn; a++) {
594 if (search_backw) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200595 for (b = words - 1; b >= (merge_words ? merge_words : 0) && strcmp(&vocab[b * max_w], st[a]) != 0; b--)
596 ;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200597 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200598 for (b = 0; b < (merge_words ? merge_words : words) && strcmp(&vocab[b * max_w], st[a]) != 0; b++)
599 ;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200600 }
601 if (b == words) b = -1;
602 wl->wordi[a] = b;
603 if (b == -1) {
604 fprintf(stderr, "Out of dictionary word!\n");
605 cn--;
606 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200607 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 +0200608 }
609 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200610 wl->length = cn;
611 return (wl);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200612}
613
Marc Kupietzcb43e492019-12-03 10:07:53 +0100614long getWordNumber(char *word) {
615 wordlist *wl = getTargetWords(word, 0);
Marc Kupietz04135302026-07-30 14:40:02 +0900616 long res = 0;
617 if (wl == NULL)
618 return(0);
Marc Kupietzcb43e492019-12-03 10:07:53 +0100619 if(wl->length > 0)
Marc Kupietz04135302026-07-30 14:40:02 +0900620 res = wl->wordi[0];
621 free(wl);
622 return(res);
Marc Kupietzcb43e492019-12-03 10:07:53 +0100623}
624
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200625float get_distance(long b, long c) {
626 long a;
627 float dist = 0;
628 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + b * size];
629 return dist;
630}
631
Marc Kupietz04135302026-07-30 14:40:02 +0900632/* The result is computed once and then kept in a static buffer for the
633 lifetime of the process. */
634SV *getBiggestMergedDifferences() {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200635 static char *result = NULL;
Marc Kupietz59865a92021-03-11 17:16:51 +0100636 float dist;
637 long long a, c;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200638 int N = 1000;
639
Marc Kupietz969cab92019-08-05 11:13:42 +0200640 if (merged_end == 0)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200641 result = "[]";
Marc Kupietz969cab92019-08-05 11:13:42 +0200642
643 if (result != NULL)
Marc Kupietz04135302026-07-30 14:40:02 +0900644 return newSVpv(result, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200645
646 printf("Looking for biggest distances between main and merged vectors ...\n");
647 collocator *best;
648 best = malloc(N * sizeof(collocator));
649 memset(best, 0, N * sizeof(collocator));
650
Marc Kupietz969cab92019-08-05 11:13:42 +0200651 float worstbest = 1000000;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200652
653 for (a = 0; a < N; a++) best[a].activation = worstbest;
654
655 for (c = 0; c < 500000; c++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200656 if (garbage && garbage[c]) continue;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200657 dist = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200658 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + (c + merged_end) * size];
659 if (dist < worstbest) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200660 for (a = 0; a < N; a++) {
661 if (dist < best[a].activation) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200662 memmove(best + a + 1, best + a, (N - a - 1) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200663 best[a].activation = dist;
664 best[a].wordi = c;
665 break;
666 }
667 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200668 worstbest = best[N - 1].activation;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200669 }
670 }
671
Marc Kupietz04135302026-07-30 14:40:02 +0900672 result = malloc(N * (max_w + 64));
Marc Kupietzbdd779a2024-08-05 10:02:29 +0200673 char *p = (char *) result;
Marc Kupietz969cab92019-08-05 11:13:42 +0200674 *p++ = '[';
675 *p = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200676 for (a = 0; a < N; a++) {
Marc Kupietz59865a92021-03-11 17:16:51 +0100677 p += sprintf(p, "{\"rank\":%lld,\"word\":\"%s\",\"dist\":%.3f},", a, &vocab[best[a].wordi * max_w], 1 - best[a].activation);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200678 }
679 *--p = ']';
Marc Kupietz04135302026-07-30 14:40:02 +0900680 free(best);
681 return newSVpv(result, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200682}
683
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200684float cos_similarity(long b, long c) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200685 float dist = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200686 long a;
Marc Kupietz969cab92019-08-05 11:13:42 +0200687 for (a = 0; a < size; a++) dist += M[b * size + a] * M[c * size + a];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200688 return dist;
689}
690
Marc Kupietz04135302026-07-30 14:40:02 +0900691SV *cos_similarity_as_json(char *w1, char *w2) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200692 wordlist *a, *b;
693 float res;
Marc Kupietz04135302026-07-30 14:40:02 +0900694 char json[32];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200695 a = getTargetWords(w1, 0);
696 b = getTargetWords(w2, 0);
Marc Kupietz969cab92019-08-05 11:13:42 +0200697 if (a == NULL || b == NULL || a->length != 1 || b->length != 1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200698 res = -1;
Marc Kupietz04135302026-07-30 14:40:02 +0900699 else {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200700 res = cos_similarity(a->wordi[0], b->wordi[0]);
Marc Kupietz04135302026-07-30 14:40:02 +0900701 fprintf(stderr, "a: %lld b: %lld res:%f\n", a->wordi[0], b->wordi[0], res);
702 }
703 free(a);
704 free(b);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200705 sprintf(json, "%.5f", res);
Marc Kupietz04135302026-07-30 14:40:02 +0900706 return newSVpv(json, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200707}
708
709void *_get_neighbours(void *arg) {
710 knnpars *pars = arg;
Marc Kupietz969cab92019-08-05 11:13:42 +0200711 int N = pars->N;
712 long from = pars->from;
713 unsigned long upto = pars->upto;
Marc Kupietz59865a92021-03-11 17:16:51 +0100714 char *sep;
Marc Kupietz969cab92019-08-05 11:13:42 +0200715 float dist, len, vec[max_size];
Marc Kupietz59865a92021-03-11 17:16:51 +0100716 long long a, b, c, cn, *bi;
Marc Kupietz969cab92019-08-05 11:13:42 +0200717 knn *nbs = NULL;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200718 wordlist *wl = pars->wl;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200719
Marc Kupietz969cab92019-08-05 11:13:42 +0200720 collocator *best = pars->best;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200721
Marc Kupietz969cab92019-08-05 11:13:42 +0200722 float worstbest = -1;
723
724 for (a = 0; a < N; a++) best[a].activation = 0;
725 a = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200726 bi = wl->wordi;
Marc Kupietz969cab92019-08-05 11:13:42 +0200727 cn = wl->length;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200728 sep = wl->sep;
Marc Kupietz969cab92019-08-05 11:13:42 +0200729 b = bi[0];
Marc Kupietz969cab92019-08-05 11:13:42 +0200730 if (b == -1) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200731 goto end;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200732 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200733 for (a = 0; a < size; a++) vec[a] = 0;
734 for (b = 0; b < cn; b++) {
735 if (bi[b] == -1) continue;
736 if (b > 0 && sep[b - 1] == '-')
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200737 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
738 else
739 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietz969cab92019-08-05 11:13:42 +0200740 }
741 len = 0;
742 for (a = 0; a < size; a++) len += vec[a] * vec[a];
743 len = sqrt(len);
744 for (a = 0; a < size; a++) vec[a] /= len;
745 for (a = 0; a < N; a++) best[a].activation = -1;
746 for (c = from; c < upto; c++) {
747 if (garbage && garbage[c]) continue;
748 a = 0;
749 // do not skip taget word
750 // for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
751 // if (a == 1) continue;
752 dist = 0;
753 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
754 if (dist > worstbest) {
755 for (a = 0; a < N; a++) {
756 if (dist > best[a].activation) {
757 memmove(best + a + 1, best + a, (N - a - 1) * sizeof(collocator));
758 best[a].activation = dist;
759 best[a].wordi = c;
760 break;
761 }
762 }
763 worstbest = best[N - 1].activation;
764 }
765 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200766
767end:
Marc Kupietz969cab92019-08-05 11:13:42 +0200768 pthread_exit(nbs);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200769}
770
Marc Kupietz969cab92019-08-05 11:13:42 +0200771int cmp_activation(const void *a, const void *b) {
772 float fb = ((collocator *)a)->activation;
773 float fa = ((collocator *)b)->activation;
774 return (fa > fb) - (fa < fb);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200775}
776
Marc Kupietz969cab92019-08-05 11:13:42 +0200777int cmp_probability(const void *a, const void *b) {
778 float fb = ((collocator *)a)->probability;
779 float fa = ((collocator *)b)->probability;
780 return (fa > fb) - (fa < fb);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200781}
782
Marc Kupietz04135302026-07-30 14:40:02 +0900783SV *getPosWiseW2VCollocators(char *word, long maxPerPos, long cutoff, float threshold, const char *format) {
Marc Kupietz59865a92021-03-11 17:16:51 +0100784 float *target_sums = NULL;
Marc Kupietz04135302026-07-30 14:40:02 +0900785 long a, b, entries = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200786 knn *syn_nbs[MAX_THREADS];
787 knnpars pars[MAX_THREADS];
Marc Kupietz04135302026-07-30 14:40:02 +0900788 pthread_t *pt = NULL;
789 wordlist *wl = NULL;
Marc Kupietz969cab92019-08-05 11:13:42 +0200790 int syn_threads = (M2 ? window * 2 : 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200791 int search_backw = 0;
Marc Kupietz04135302026-07-30 14:40:02 +0900792 char *result = NULL;
793 SV *res_sv;
794
795 for (a = 0; a < MAX_THREADS; a++) syn_nbs[a] = NULL;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200796
Marc Kupietz969cab92019-08-05 11:13:42 +0200797 if (cutoff < 1 || cutoff > words)
798 cutoff = words;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200799
800 wl = getTargetWords(word, search_backw);
Marc Kupietz04135302026-07-30 14:40:02 +0900801 if (wl == NULL || wl->length < 1 || wl->wordi[0] < 0 || syn_threads < 1) {
802 free(wl);
803 return newSVpv("", 0);
804 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200805
Marc Kupietz04135302026-07-30 14:40:02 +0900806 pt = (pthread_t *)malloc((num_threads + 1) * sizeof(pthread_t));
Marc Kupietz969cab92019-08-05 11:13:42 +0200807 a = posix_memalign((void **)&target_sums, 128, cutoff * sizeof(float));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200808 memset(target_sums, 0, cutoff * sizeof(float));
809
810 printf("Starting %d threads\n", syn_threads);
811 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200812 for (a = 0; a < syn_threads; a++) {
813 pars[a].cutoff = cutoff;
814 pars[a].target_sums = target_sums;
815 pars[a].window_sums = window_sums;
816 pars[a].wl = wl;
817 pars[a].N = maxPerPos;
Marc Kupietz04135302026-07-30 14:40:02 +0900818 pars[a].best = NULL; /* getCollocators() allocates its own result array */
Marc Kupietz969cab92019-08-05 11:13:42 +0200819 pars[a].threshold = threshold;
820 pars[a].from = a;
821 pars[a].upto = a + 1;
822 pthread_create(&pt[a], NULL, getCollocators, (void *)&pars[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200823 }
824 printf("Waiting for syn threads to join\n");
825 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200826 for (a = 0; a < syn_threads; a++) pthread_join(pt[a], (void *)&syn_nbs[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200827 printf("Syn threads joint\n");
828 fflush(stdout);
Marc Kupietz04135302026-07-30 14:40:02 +0900829 result = malloc((maxPerPos > 0 ? maxPerPos : 1) * (max_w + 96) * syn_threads + 16);
Marc Kupietzbdd779a2024-08-05 10:02:29 +0200830 char *p = (char *) result;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200831 *p = 0;
Marc Kupietz0ab97392024-12-10 16:16:32 +0100832 if (strcmp(format, "tsv") == 0) {
833 for (a = syn_threads - 1; a >= 0; a--) {
Marc Kupietz04135302026-07-30 14:40:02 +0900834 if (syn_nbs[a] == NULL) continue;
835 for (b = 0; b < syn_nbs[a]->length; b++, entries++) {
Marc Kupietz0ab97392024-12-10 16:16:32 +0100836 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);
837 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200838 }
Marc Kupietz0ab97392024-12-10 16:16:32 +0100839 } else {
840 p += sprintf(p, "[");
841 for (a = syn_threads - 1; a >= 0; a--) {
Marc Kupietz04135302026-07-30 14:40:02 +0900842 if (syn_nbs[a] == NULL) continue;
843 for (b = 0; b < syn_nbs[a]->length; b++, entries++) {
Marc Kupietz0ab97392024-12-10 16:16:32 +0100844 p += sprintf(p, "{\"pos\": %ld, \"word\":\"%s\",\"activation\": %f},\n", syn_nbs[a]->best[b].position, &vocab[syn_nbs[a]->best[b].wordi * max_w], syn_nbs[a]->best[b].activation);
845 }
846 }
Marc Kupietz04135302026-07-30 14:40:02 +0900847 if (entries > 0)
848 p -= 2; /* drop the trailing ",\n" */
Marc Kupietz0ab97392024-12-10 16:16:32 +0100849 p += sprintf(p, "\n]");
Marc Kupietz969cab92019-08-05 11:13:42 +0200850 }
Marc Kupietz0ab97392024-12-10 16:16:32 +0100851
Marc Kupietz04135302026-07-30 14:40:02 +0900852 res_sv = newSVpv(result, 0);
853
854 free(result);
855 free(target_sums);
856 free(pt);
857 free(wl);
858 for (a = 0; a < syn_threads; a++) free_knn(syn_nbs[a]);
859
860 return res_sv;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200861}
862
Marc Kupietz04135302026-07-30 14:40:02 +0900863SV *getPosWiseW2VCollocatorsAsTsv(char *word, long maxPerPos, long cutoff, float threshold) {
Marc Kupietz0ab97392024-12-10 16:16:32 +0100864 return getPosWiseW2VCollocators(word, maxPerPos, cutoff, threshold, "tsv");
865}
866
Marc Kupietz04135302026-07-30 14:40:02 +0900867SV *getPosWiseW2VCollocatorsAsJson(char *word, long maxPerPos, long cutoff, float threshold) {
Marc Kupietz0ab97392024-12-10 16:16:32 +0100868 return getPosWiseW2VCollocators(word, maxPerPos, cutoff, threshold, "json");
869}
870
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200871SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe, int no_similar_profiles) {
872 HV *result = newHV();
Marc Kupietz59865a92021-03-11 17:16:51 +0100873 float *target_sums = NULL;
Marc Kupietz969cab92019-08-05 11:13:42 +0200874 long a, b, c, d, slice;
875 knn *para_nbs[MAX_THREADS];
876 knn *syn_nbs[MAX_THREADS];
877 knnpars pars[MAX_THREADS];
878 pthread_t *pt = (pthread_t *)malloc((num_threads + 1) * sizeof(pthread_t));
Marc Kupietz04135302026-07-30 14:40:02 +0900879 wordlist *wl = NULL;
Marc Kupietz969cab92019-08-05 11:13:42 +0200880 int syn_threads = (M2 ? window * 2 : 0);
881 int para_threads = (no_similar_profiles ? 0 : num_threads - syn_threads);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200882
Marc Kupietz04135302026-07-30 14:40:02 +0900883 for (a = 0; a < MAX_THREADS; a++) para_nbs[a] = syn_nbs[a] = NULL;
884
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200885 collocator *best = NULL;
Marc Kupietz969cab92019-08-05 11:13:42 +0200886 posix_memalign((void **)&best, 128, 10 * (N >= 200 ? N : 200) * sizeof(collocator));
887 memset(best, 0, (N >= 200 ? N : 200) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200888
Marc Kupietz969cab92019-08-05 11:13:42 +0200889 if (N > MAX_NEIGHBOURS) N = MAX_NEIGHBOURS;
890
891 if (cutoff < 1 || cutoff > words)
892 cutoff = words;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200893
894 wl = getTargetWords(st1, search_backw);
Marc Kupietz969cab92019-08-05 11:13:42 +0200895 if (wl == NULL || wl->length < 1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200896 goto end;
897
Marc Kupietz04135302026-07-30 14:40:02 +0900898 slice = (para_threads > 0 ? cutoff / para_threads : cutoff);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200899
Marc Kupietz969cab92019-08-05 11:13:42 +0200900 a = posix_memalign((void **)&target_sums, 128, cutoff * sizeof(float));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200901 memset(target_sums, 0, cutoff * sizeof(float));
902
Marc Kupietzc0d41872021-02-25 16:33:22 +0100903 printf("Starting %d threads for paradigmatic search\n", para_threads);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200904 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200905 for (a = 0; a < para_threads; a++) {
906 pars[a].cutoff = cutoff;
907 pars[a].token = st1;
908 pars[a].wl = wl;
909 pars[a].N = N;
910 pars[a].best = &best[N * a];
911 if (merge_words == 0 || search_backw == 0) {
912 pars[a].from = a * slice;
913 pars[a].upto = ((a + 1) * slice > cutoff ? cutoff : (a + 1) * slice);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200914 } else {
915 pars[a].from = merge_words + a * slice;
Marc Kupietz969cab92019-08-05 11:13:42 +0200916 pars[a].upto = merge_words + ((a + 1) * slice > cutoff ? cutoff : (a + 1) * slice);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200917 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200918 printf("From: %ld, Upto: %ld\n", pars[a].from, pars[a].upto);
919 pthread_create(&pt[a], NULL, _get_neighbours, (void *)&pars[a]);
920 }
921 if (M2) {
922 for (a = 0; a < syn_threads; a++) {
923 pars[a + para_threads].cutoff = cutoff;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200924 pars[a + para_threads].target_sums = target_sums;
925 pars[a + para_threads].window_sums = window_sums;
926 pars[a + para_threads].wl = wl;
927 pars[a + para_threads].N = N;
928 pars[a + para_threads].threshold = MIN_RESP;
929 pars[a + para_threads].from = a;
Marc Kupietz969cab92019-08-05 11:13:42 +0200930 pars[a + para_threads].upto = a + 1;
931 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *)&pars[a + para_threads]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200932 }
933 }
934 printf("Waiting for para threads to join\n");
935 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200936 for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *)&para_nbs[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200937 printf("Para threads joint\n");
938 fflush(stdout);
939
Marc Kupietz969cab92019-08-05 11:13:42 +0200940 /* if(!syn_nbs[0]) */
941 /* goto end; */
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200942
Marc Kupietz969cab92019-08-05 11:13:42 +0200943 qsort(best, N * para_threads, sizeof(collocator), cmp_activation);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200944
945 long long chosen[MAX_NEIGHBOURS];
Marc Kupietz59865a92021-03-11 17:16:51 +0100946 printf("N: %d\n", N);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200947
Marc Kupietz969cab92019-08-05 11:13:42 +0200948 AV *array = newAV();
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200949 int i, j;
Marc Kupietz969cab92019-08-05 11:13:42 +0200950 int l1_words = 0, l2_words = 0;
951
952 for (a = 0, i = 0; i < N && a < N * para_threads; a++) {
953 int filtered = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200954 long long c = best[a].wordi;
955 if ((merge_words && dedupe && i > 1) || (!merge_words && dedupe && i > 0)) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200956 for (j = 0; j < i && !filtered; j++)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200957 if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) ||
958 strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200959 printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]);
960 filtered = 1;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200961 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200962 if (filtered)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200963 continue;
964 }
965
Marc Kupietz969cab92019-08-05 11:13:42 +0200966 if (0 && merge_words > 0) {
967 if (c >= merge_words) {
968 if (l1_words > N / 2)
969 continue;
970 else
971 l1_words++;
972 } else {
973 if (l2_words > N / 2)
974 continue;
975 else
976 l2_words++;
977 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200978 }
979
Marc Kupietz969cab92019-08-05 11:13:42 +0200980 // printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
981 // fflush(stdout);
982 HV *hash = newHV();
983 SV *word = newSVpvf(&vocab[c * max_w], 0);
984 chosen[i] = c;
985 if (latin_enc == 0) SvUTF8_on(word);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200986 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200987 hv_store(hash, "word", strlen("word"), word, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200988 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
989 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
990 AV *vector = newAV();
991 for (b = 0; b < size; b++) {
992 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
993 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200994 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV *)vector), 0);
995 av_push(array, newRV_noinc((SV *)hash));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200996 i++;
997 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200998 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV *)array), 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200999
Marc Kupietz969cab92019-08-05 11:13:42 +02001000 for (b = 0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001001 best[b].wordi = -1L;
1002 best[b].activation = 0;
1003 best[b].probability = 0;
1004 best[b].position = 0;
1005 best[b].activation_sum = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +02001006 memset(best[b].heat, 0, sizeof(float) * 16);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001007 }
1008
Marc Kupietz969cab92019-08-05 11:13:42 +02001009 float total_activation = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001010
1011 if (M2) {
1012 printf("Waiting for syn threads to join\n");
1013 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +02001014 for (a = 0; a < syn_threads; a++) pthread_join(pt[a + para_threads], (void *)&syn_nbs[a]);
1015 for (a = 0; a <= syn_threads; a++) {
1016 if (a == window) continue;
1017 total_activation += window_sums[a];
Marc Kupietz59865a92021-03-11 17:16:51 +01001018 printf("window pos: %ld, sum: %f\n", a, window_sums[a]);
Marc Kupietz969cab92019-08-05 11:13:42 +02001019 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001020 printf("syn threads joint\n");
1021 fflush(stdout);
1022
Marc Kupietz969cab92019-08-05 11:13:42 +02001023 for (b = 0; b < syn_nbs[0]->length; b++) {
1024 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
1025 best[b].position = -1; // syn_nbs[0]->pos[b];
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001026 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietz969cab92019-08-05 11:13:42 +02001027 best[b].max_activation = 0.0;
1028 best[b].average = 0.0;
1029 best[b].probability = 0.0;
1030 best[b].cprobability = syn_nbs[0]->best[b].cprobability;
1031 memset(best[b].heat, 0, sizeof(float) * 16);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001032 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001033
1034 float best_window_sum[MAX_NEIGHBOURS];
Marc Kupietz59865a92021-03-11 17:16:51 +01001035 int found_index = 0, i = 0, w;
Marc Kupietz969cab92019-08-05 11:13:42 +02001036 for (a = 0; a < syn_threads; a++) {
1037 for (b = 0; b < syn_nbs[a]->length; b++) {
1038 for (i = 0; i < found_index; i++)
1039 if (best[i].wordi == syn_nbs[a]->best[b].wordi)
1040 break;
1041 if (i >= found_index) {
1042 best[found_index].max_activation = 0.0;
1043 best[found_index].average = 0.0;
1044 best[found_index].probability = 0.0;
1045 memset(best[found_index].heat, 0, sizeof(float) * 16);
1046 best[found_index].cprobability = syn_nbs[a]->best[b].cprobability;
1047 best[found_index].activation_sum = target_sums[syn_nbs[a]->best[b].wordi]; // syn_nbs[a]->best[b].activation_sum;
1048 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
1049 // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
1050 }
1051 }
1052 }
1053 sort_by = 0; // ALWAYS AUTO-FOCUS
1054 if (sort_by != 1 && sort_by != 2) { // sort by auto focus mean
1055 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) - 1);
1056 int wpos;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001057 int bits_set = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +02001058 for (i = 0; i < found_index; i++) {
1059 best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0;
1060 for (w = 1; w < (1 << syn_threads); w++) { // loop through all possible windows
1061 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 +02001062 bits_set = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +02001063 for (a = 0; a < syn_threads; a++) {
1064 if ((1 << a) & w) {
1065 wpos = (a >= window ? a + 1 : a);
1066 total_window_sum += window_sums[wpos];
1067 }
1068 }
1069 // printf("%d window-sum %f\n", w, total_window_sum);
1070 for (a = 0; a < syn_threads; a++) {
1071 if ((1 << a) & w) {
1072 wpos = (a >= window ? a + 1 : a);
1073 bits_set++;
1074 for (b = 0; b < syn_nbs[a]->length; b++)
1075 if (best[i].wordi == syn_nbs[a]->best[b].wordi) {
1076 // float acti = syn_nbs[a]->best[b].activation / total_window_sum;
1077 // word_window_sum += syn_nbs[a]->dist[b] * syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1078 // word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1079 // 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 +02001080
Marc Kupietz969cab92019-08-05 11:13:42 +02001081 word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1082 // 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 +02001083
Marc Kupietz969cab92019-08-05 11:13:42 +02001084 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 +02001085 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 +02001086 word_activation_sum += syn_nbs[a]->best[b].activation;
1087 if (syn_nbs[a]->best[b].activation > best[i].max_activation)
1088 best[i].max_activation = syn_nbs[a]->best[b].activation;
1089 if (syn_nbs[a]->best[b].activation > best[i].heat[wpos])
1090 best[i].heat[wpos] = syn_nbs[a]->best[b].activation;
1091 }
1092 }
1093 }
1094 if (bits_set) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001095 word_window_average /= bits_set;
Marc Kupietz969cab92019-08-05 11:13:42 +02001096 // word_activation_sum /= bits_set;
1097 // word_window_sum /= bits_set;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001098 }
1099
Marc Kupietz969cab92019-08-05 11:13:42 +02001100 word_window_sum /= total_window_sum;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001101
Marc Kupietz969cab92019-08-05 11:13:42 +02001102 if (word_window_sum > best[i].probability) {
1103 // best[i].position = w;
1104 best[i].probability = word_window_sum;
1105 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001106
Marc Kupietz969cab92019-08-05 11:13:42 +02001107 if (word_cprobability_sum > best[i].cprobability_sum) {
1108 best[i].position = w;
1109 best[i].cprobability_sum = word_cprobability_sum;
1110 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001111
Marc Kupietz969cab92019-08-05 11:13:42 +02001112 best[i].average = word_window_average;
1113 // best[i].activation = word_activation_sum;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001114 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001115 }
1116 qsort(best, found_index, sizeof(collocator), cmp_probability);
1117 // for(i=0; i < found_index; i++) {
1118 // printf("found: %s - sum: %f - window: %d\n", &vocab[best[i].wordi * max_w], best[i].activation, best[i].position);
1119 // }
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001120
Marc Kupietz969cab92019-08-05 11:13:42 +02001121 } else if (sort_by == 1) { // responsiveness any window position
1122 int wpos;
1123 for (i = 0; i < found_index; i++) {
1124 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
1125 for (a = 0; a < syn_threads; a++) {
1126 wpos = (a >= window ? a + 1 : a);
1127 for (b = 0; b < syn_nbs[a]->length; b++)
1128 if (best[i].wordi == syn_nbs[a]->best[b].wordi) {
1129 best[i].probability += syn_nbs[a]->best[b].probability;
1130 if (syn_nbs[a]->best[b].activation > 0.25)
1131 best[i].position |= 1 << wpos;
1132 if (syn_nbs[a]->best[b].activation > best[i].activation) {
1133 best[i].activation = syn_nbs[a]->best[b].activation;
1134 }
1135 }
1136 }
1137 }
1138 qsort(best, found_index, sizeof(collocator), cmp_activation);
1139 } else if (sort_by == 2) { // single window position
1140 for (a = 1; a < syn_threads; a++) {
1141 for (b = 0; b < syn_nbs[a]->length; b++) {
1142 for (c = 0; c < MAX_NEIGHBOURS; c++) {
1143 if (syn_nbs[a]->best[b].activation > best[c].activation) {
1144 for (d = MAX_NEIGHBOURS - 1; d > c; d--) {
1145 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001146 }
1147 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
Marc Kupietz969cab92019-08-05 11:13:42 +02001148 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 +02001149 break;
1150 }
1151 }
1152 }
1153 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001154 } else { // sort by mean p
1155 for (a = 1; a < syn_threads; a++) {
1156 for (b = 0; b < syn_nbs[a]->length; b++) {
1157 for (c = 0; c < MAX_NEIGHBOURS; c++) {
1158 if (target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
1159 for (d = MAX_NEIGHBOURS - 1; d > c; d--) {
1160 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001161 }
1162 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
Marc Kupietz969cab92019-08-05 11:13:42 +02001163 best[c].position = (1 << 2 * window) - 1; // syn_nbs[a]->pos[b];
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001164 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
1165 break;
1166 }
1167 }
1168 }
1169 }
1170 }
1171 array = newAV();
Marc Kupietz969cab92019-08-05 11:13:42 +02001172 for (a = 0, i = 0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001173 long long c = best[a].wordi;
Marc Kupietz969cab92019-08-05 11:13:42 +02001174 /*
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001175 if (dedupe) {
1176 int filtered=0;
1177 for (j=0; j<i; j++)
1178 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
1179 strcasestr(chosen[j], &vocab[c * max_w])) {
1180 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
1181 filtered = 1;
1182 }
1183 if(filtered)
1184 continue;
1185 }
1186*/
Marc Kupietz969cab92019-08-05 11:13:42 +02001187 chosen[i++] = c;
1188 HV *hash = newHV();
1189 SV *word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
1190 AV *heat = newAV();
1191 if (latin_enc == 0) SvUTF8_on(word);
1192 hv_store(hash, "word", strlen("word"), word, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001193 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
1194 hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0);
1195 hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0);
1196 hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0);
Marc Kupietz969cab92019-08-05 11:13:42 +02001197 hv_store(hash, "max", strlen("max"), newSVnv(best[a].max_activation), 0); // newSVnv(target_sums[best[a].wordi]), 0);
1198 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 +02001199 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietz969cab92019-08-05 11:13:42 +02001200 best[a].heat[5] = 0;
1201 for (i = 10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i]));
1202 hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV *)heat), 0);
1203 av_push(array, newRV_noinc((SV *)hash));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001204 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001205 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV *)array), 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001206 }
1207end:
Marc Kupietz969cab92019-08-05 11:13:42 +02001208 free(best);
Marc Kupietz04135302026-07-30 14:40:02 +09001209 free(target_sums);
1210 free(pt);
1211 free(wl);
1212 for (a = 0; a < MAX_THREADS; a++) {
1213 free_knn(para_nbs[a]);
1214 free_knn(syn_nbs[a]);
1215 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001216 return newRV_noinc((SV *)result);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001217}
1218
1219int dump_vecs(char *fname) {
Marc Kupietz969cab92019-08-05 11:13:42 +02001220 long i, j;
1221 FILE *f;
1222 /* if(words>100000)
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001223 words=100000;
1224*/
Marc Kupietz969cab92019-08-05 11:13:42 +02001225 if ((f = fopen(fname, "w")) == NULL) {
1226 fprintf(stderr, "cannot open %s for writing\n", fname);
1227 return (-1);
1228 }
1229 fprintf(f, "%lld %lld\n", words, size);
1230 for (i = 0; i < words; i++) {
1231 fprintf(f, "%s ", &vocab[i * max_w]);
1232 for (j = 0; j < size - 1; j++)
1233 fprintf(f, "%f ", M[i * size + j]);
1234 fprintf(f, "%f\n", M[i * size + j]);
1235 }
1236 fclose(f);
1237 return (0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001238}
1239
1240int dump_for_numpy(char *fname) {
Marc Kupietz969cab92019-08-05 11:13:42 +02001241 long i, j;
1242 FILE *f;
Marc Kupietzc0d41872021-02-25 16:33:22 +01001243 int max = words; // 300000;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001244
Marc Kupietz969cab92019-08-05 11:13:42 +02001245 if ((f = fopen(fname, "w")) == NULL) {
1246 fprintf(stderr, "cannot open %s for writing\n", fname);
1247 return (-1);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001248 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001249 for (i = 0; i < max; i++) {
1250 for (j = 0; j < size - 1; j++)
1251 fprintf(f, "%f\t", M[i * size + j]);
1252 fprintf(f, "%f\n", M[i * size + j]);
1253 printf("%s\r\n", &vocab[i * max_w]);
1254 }
1255 if (merged_end > 0) {
1256 for (i = 0; i < max; i++) {
1257 for (j = 0; j < size - 1; j++)
1258 fprintf(f, "%f\t", M[(merged_end + i) * size + j]);
1259 fprintf(f, "%f\n", M[(merged_end + i) * size + j]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001260 printf("_%s\r\n", &vocab[i * max_w]);
1261 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001262 }
1263 fclose(f);
1264 return (0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001265}
Marc Kupietz043db152023-11-05 17:47:53 +01001266
1267unsigned long getVocabSize() {
1268 return (unsigned long) words;
1269}