blob: 5ef2014d44707b762b10d5a8871e010476123492 [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
374void *getCollocators(void *args) {
375 knnpars *pars = args;
Marc Kupietz969cab92019-08-05 11:13:42 +0200376 int N = pars->N;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200377
378 int cc = pars->wl->wordi[0];
Marc Kupietz969cab92019-08-05 11:13:42 +0200379 knn *nbs = NULL;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200380 long window_layer_size = size * window * 2;
Marc Kupietz59865a92021-03-11 17:16:51 +0100381 long a, b, c, d, window_offset, target, max_target = 0, maxmax_target;
Marc Kupietz969cab92019-08-05 11:13:42 +0200382 float f, max_f, maxmax_f;
383 float *target_sums = NULL, worstbest, wpos_sum;
384 collocator *best;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200385
Marc Kupietz969cab92019-08-05 11:13:42 +0200386 if (M2 == NULL || cc == -1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200387 return NULL;
388
Marc Kupietz969cab92019-08-05 11:13:42 +0200389 a = posix_memalign((void **)&target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200390 memset(target_sums, 0, pars->cutoff * sizeof(float));
Marc Kupietz969cab92019-08-05 11:13:42 +0200391 best = malloc((N > 200 ? N : 200) * sizeof(collocator));
392 memset(best, 0, (N > 200 ? N : 200) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200393 worstbest = pars->threshold;
394
395 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200396 target_sums[b] = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200397 for (b = 0; b < N; b++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200398 best[b].wordi = -1;
399 best[b].probability = 1;
400 best[b].activation = worstbest;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200401 }
402
403 d = cc;
404 maxmax_f = -1;
405 maxmax_target = 0;
406
407 for (a = pars->from; a < pars->upto; a++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200408 if (a >= window)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200409 a++;
410 wpos_sum = 0;
411 printf("window pos: %ld\n", a);
412 if (a != window) {
413 max_f = -1;
414 window_offset = a * size;
415 if (a > window)
416 window_offset -= size;
Marc Kupietz969cab92019-08-05 11:13:42 +0200417 for (target = 0; target < pars->cutoff; target++) {
418 if (garbage && garbage[target]) continue;
419 if (target == d)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200420 continue;
421 f = 0;
422 for (c = 0; c < size; c++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200423 f += M2[d * size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200424 if (f < -MAX_EXP)
425 continue;
426 else if (f > MAX_EXP)
427 continue;
428 else
Marc Kupietz969cab92019-08-05 11:13:42 +0200429 f = expTable[(int)((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200430 wpos_sum += f;
431
432 target_sums[target] += f;
Marc Kupietz969cab92019-08-05 11:13:42 +0200433 if (f > worstbest) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200434 for (b = 0; b < N; b++) {
435 if (f > best[b].activation) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200436 memmove(best + b + 1, best + b, (N - b - 1) * sizeof(collocator));
437 best[b].activation = f;
438 best[b].wordi = target;
439 best[b].position = window - a;
440 break;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200441 }
442 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200443 if (b == N - 1)
444 worstbest = best[N - 1].activation;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200445 }
446 }
Marc Kupietz59865a92021-03-11 17:16:51 +0100447 printf("%ld %.2f\n", max_target, max_f);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200448 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
Marc Kupietz969cab92019-08-05 11:13:42 +0200449 if (max_f > maxmax_f) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200450 maxmax_f = max_f;
451 maxmax_target = max_target;
452 }
453 for (b = 0; b < N; b++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200454 if (best[b].position == window - a)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200455 best[b].cprobability = best[b].activation / wpos_sum;
456 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200457 printf("\x1b[1m%s\x1b[0m ", &vocab[d * max_w]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200458 }
459 pars->window_sums[a] = wpos_sum;
460 }
461 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz969cab92019-08-05 11:13:42 +0200462 pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200463
464 free(target_sums);
Marc Kupietz969cab92019-08-05 11:13:42 +0200465 for (b = 0; b < N && best[b].wordi >= 0; b++)
466 ;
Marc Kupietz59865a92021-03-11 17:16:51 +0100467 // THIS LOOP IS NEEDED (b...)
Marc Kupietz969cab92019-08-05 11:13:42 +0200468 // printf("%d: best syn: %s %.2f %.5f\n", b, &vocab[best[b].wordi*max_w], best[b].activation, best[b].probability);
469 // printf("\n");
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200470 nbs = malloc(sizeof(knn));
Marc Kupietz969cab92019-08-05 11:13:42 +0200471 nbs->best = best;
472 nbs->length = b - 1;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200473 pthread_exit(nbs);
474}
475
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200476float getOutputWeight(int hidden, long target, int window_position) {
477 const long window_layer_size = size * window * 2;
478 int a;
479
480 if (window_position == 0 || window_position > window || window_position < -window) {
481 fprintf(stderr, "window_position: %d - assert: -%d <= window_position <= %d && window_position != 0 failed.\n", window_position, window, window);
482 exit(-1);
483 }
484
485 if (hidden >= size) {
Marc Kupietz59865a92021-03-11 17:16:51 +0100486 fprintf(stderr, "hidden: %d - assert: hidden < %lld failed.\n", hidden, size);
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200487 exit(-1);
488 }
489
490 if (target >= words) {
Marc Kupietz59865a92021-03-11 17:16:51 +0100491 fprintf(stderr, "target: %ld - assert: target < %lld failed.\n", target, words);
Marc Kupietz0efe49b2020-04-06 18:30:22 +0200492 exit(-1);
493 }
494
495 a = window_position + window;
496 if (a > window) {
497 --a;
498 }
499 long window_offset = a * size;
500 return syn1neg_window[target * window_layer_size + window_offset + hidden];
501}
502
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200503AV *getVecs(AV *array) {
504 int i, b;
505 AV *result = newAV();
Marc Kupietz969cab92019-08-05 11:13:42 +0200506 for (i = 0; i <= av_len(array); i++) {
507 SV **elem = av_fetch(array, i, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200508 if (elem != NULL) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200509 long j = (long)SvNV(*elem);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200510 AV *vector = newAV();
511 for (b = 0; b < size; b++) {
512 av_push(vector, newSVnv(M[b + j * size]));
513 }
Marc Kupietzbdd779a2024-08-05 10:02:29 +0200514 av_push(result, newRV_noinc((SV *)vector));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200515 }
516 }
517 return result;
518}
519
520char *getSimilarProfiles(long node) {
521 int i;
522 char buffer[120000];
523 char pair_buffer[2048];
Marc Kupietz969cab92019-08-05 11:13:42 +0200524 buffer[0] = '[';
525 buffer[1] = 0;
526 if (node >= sprofiles_qty) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200527 printf("Not available in precomputed profile\n");
Marc Kupietz969cab92019-08-05 11:13:42 +0200528 return (strdup("[{\"w\":\"not available\", \"v\":0}]\n"));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200529 }
530
531 printf("******* %s ******\n", &vocab[max_w * node]);
Marc Kupietz969cab92019-08-05 11:13:42 +0200532
533 for (i = 0; i < 100 && i < sprofiles[node].len; i++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200534 sprintf(pair_buffer, "{\"w\":\"%s\", \"v\":%f},", &vocab[max_w * (sprofiles[node].nbr[i].index)], sprofiles[node].nbr[i].value);
535 strcat(buffer, pair_buffer);
536 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200537 buffer[strlen(buffer) - 1] = ']';
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200538 strcat(buffer, "\n");
Marc Kupietz59865a92021-03-11 17:16:51 +0100539 printf("%s", buffer);
Marc Kupietz969cab92019-08-05 11:13:42 +0200540 return (strdup(buffer));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200541}
542
Marc Kupietzf6080012021-03-12 09:14:42 +0100543char *getCollocationScores(long node, long collocate) {
544 char *res = (cdb ? strdup(get_collocation_scores_as_json(cdb, node, collocate)) : "[]");
545 return res;
546}
547
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200548char *getClassicCollocators(long node) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200549 char *res = (cdb ? strdup(get_collocators_as_json(cdb, node)) : "[]");
550 return res;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200551}
552
553wordlist *getTargetWords(char *st1, int search_backw) {
554 wordlist *wl = malloc(sizeof(wordlist));
Marc Kupietz59865a92021-03-11 17:16:51 +0100555 char st[100][max_size];
Marc Kupietz969cab92019-08-05 11:13:42 +0200556 long a, b = 0, c = 0, cn = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200557
558 while (1) {
559 st[cn][b] = st1[c];
560 b++;
561 c++;
562 st[cn][b] = 0;
563 if (st1[c] == 0) break;
Marc Kupietzc0d41872021-02-25 16:33:22 +0100564 if (st1[c] == ' ' /*|| st1[c] == '-'*/) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200565 b = 0;
566 c++;
567 }
568 }
569 cn++;
570 for (a = 0; a < cn; a++) {
571 if (search_backw) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200572 for (b = words - 1; b >= (merge_words ? merge_words : 0) && strcmp(&vocab[b * max_w], st[a]) != 0; b--)
573 ;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200574 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200575 for (b = 0; b < (merge_words ? merge_words : words) && strcmp(&vocab[b * max_w], st[a]) != 0; b++)
576 ;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200577 }
578 if (b == words) b = -1;
579 wl->wordi[a] = b;
580 if (b == -1) {
581 fprintf(stderr, "Out of dictionary word!\n");
582 cn--;
583 } else {
Marc Kupietz969cab92019-08-05 11:13:42 +0200584 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 +0200585 }
586 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200587 wl->length = cn;
588 return (wl);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200589}
590
Marc Kupietzcb43e492019-12-03 10:07:53 +0100591long getWordNumber(char *word) {
592 wordlist *wl = getTargetWords(word, 0);
593 if(wl->length > 0)
594 return(wl->wordi[0]);
595 return(0);
596}
597
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200598float get_distance(long b, long c) {
599 long a;
600 float dist = 0;
601 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + b * size];
602 return dist;
603}
604
Marc Kupietz969cab92019-08-05 11:13:42 +0200605char *getBiggestMergedDifferences() {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200606 static char *result = NULL;
Marc Kupietz59865a92021-03-11 17:16:51 +0100607 float dist;
608 long long a, c;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200609 int N = 1000;
610
Marc Kupietz969cab92019-08-05 11:13:42 +0200611 if (merged_end == 0)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200612 result = "[]";
Marc Kupietz969cab92019-08-05 11:13:42 +0200613
614 if (result != NULL)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200615 return result;
616
617 printf("Looking for biggest distances between main and merged vectors ...\n");
618 collocator *best;
619 best = malloc(N * sizeof(collocator));
620 memset(best, 0, N * sizeof(collocator));
621
Marc Kupietz969cab92019-08-05 11:13:42 +0200622 float worstbest = 1000000;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200623
624 for (a = 0; a < N; a++) best[a].activation = worstbest;
625
626 for (c = 0; c < 500000; c++) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200627 if (garbage && garbage[c]) continue;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200628 dist = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200629 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + (c + merged_end) * size];
630 if (dist < worstbest) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200631 for (a = 0; a < N; a++) {
632 if (dist < best[a].activation) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200633 memmove(best + a + 1, best + a, (N - a - 1) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200634 best[a].activation = dist;
635 best[a].wordi = c;
636 break;
637 }
638 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200639 worstbest = best[N - 1].activation;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200640 }
641 }
642
Marc Kupietz969cab92019-08-05 11:13:42 +0200643 result = malloc(N * max_w);
Marc Kupietzbdd779a2024-08-05 10:02:29 +0200644 char *p = (char *) result;
Marc Kupietz969cab92019-08-05 11:13:42 +0200645 *p++ = '[';
646 *p = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200647 for (a = 0; a < N; a++) {
Marc Kupietz59865a92021-03-11 17:16:51 +0100648 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 +0200649 }
650 *--p = ']';
Marc Kupietz969cab92019-08-05 11:13:42 +0200651 return (result);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200652}
653
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200654float cos_similarity(long b, long c) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200655 float dist = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200656 long a;
Marc Kupietz969cab92019-08-05 11:13:42 +0200657 for (a = 0; a < size; a++) dist += M[b * size + a] * M[c * size + a];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200658 return dist;
659}
660
661char *cos_similarity_as_json(char *w1, char *w2) {
662 wordlist *a, *b;
663 float res;
664 a = getTargetWords(w1, 0);
665 b = getTargetWords(w2, 0);
Marc Kupietz969cab92019-08-05 11:13:42 +0200666 if (a == NULL || b == NULL || a->length != 1 || b->length != 1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200667 res = -1;
668 else
669 res = cos_similarity(a->wordi[0], b->wordi[0]);
670 fprintf(stderr, "a: %lld b: %lld res:%f\n", a->wordi[0], b->wordi[0], res);
671 char *json = malloc(16);
672 sprintf(json, "%.5f", res);
673 return json;
674}
675
676void *_get_neighbours(void *arg) {
677 knnpars *pars = arg;
Marc Kupietz969cab92019-08-05 11:13:42 +0200678 int N = pars->N;
679 long from = pars->from;
680 unsigned long upto = pars->upto;
Marc Kupietz59865a92021-03-11 17:16:51 +0100681 char *sep;
Marc Kupietz969cab92019-08-05 11:13:42 +0200682 float dist, len, vec[max_size];
Marc Kupietz59865a92021-03-11 17:16:51 +0100683 long long a, b, c, cn, *bi;
Marc Kupietz969cab92019-08-05 11:13:42 +0200684 knn *nbs = NULL;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200685 wordlist *wl = pars->wl;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200686
Marc Kupietz969cab92019-08-05 11:13:42 +0200687 collocator *best = pars->best;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200688
Marc Kupietz969cab92019-08-05 11:13:42 +0200689 float worstbest = -1;
690
691 for (a = 0; a < N; a++) best[a].activation = 0;
692 a = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200693 bi = wl->wordi;
Marc Kupietz969cab92019-08-05 11:13:42 +0200694 cn = wl->length;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200695 sep = wl->sep;
Marc Kupietz969cab92019-08-05 11:13:42 +0200696 b = bi[0];
Marc Kupietz969cab92019-08-05 11:13:42 +0200697 if (b == -1) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200698 goto end;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200699 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200700 for (a = 0; a < size; a++) vec[a] = 0;
701 for (b = 0; b < cn; b++) {
702 if (bi[b] == -1) continue;
703 if (b > 0 && sep[b - 1] == '-')
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200704 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
705 else
706 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietz969cab92019-08-05 11:13:42 +0200707 }
708 len = 0;
709 for (a = 0; a < size; a++) len += vec[a] * vec[a];
710 len = sqrt(len);
711 for (a = 0; a < size; a++) vec[a] /= len;
712 for (a = 0; a < N; a++) best[a].activation = -1;
713 for (c = from; c < upto; c++) {
714 if (garbage && garbage[c]) continue;
715 a = 0;
716 // do not skip taget word
717 // for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
718 // if (a == 1) continue;
719 dist = 0;
720 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
721 if (dist > worstbest) {
722 for (a = 0; a < N; a++) {
723 if (dist > best[a].activation) {
724 memmove(best + a + 1, best + a, (N - a - 1) * sizeof(collocator));
725 best[a].activation = dist;
726 best[a].wordi = c;
727 break;
728 }
729 }
730 worstbest = best[N - 1].activation;
731 }
732 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200733
734end:
Marc Kupietz969cab92019-08-05 11:13:42 +0200735 pthread_exit(nbs);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200736}
737
Marc Kupietz969cab92019-08-05 11:13:42 +0200738int cmp_activation(const void *a, const void *b) {
739 float fb = ((collocator *)a)->activation;
740 float fa = ((collocator *)b)->activation;
741 return (fa > fb) - (fa < fb);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200742}
743
Marc Kupietz969cab92019-08-05 11:13:42 +0200744int cmp_probability(const void *a, const void *b) {
745 float fb = ((collocator *)a)->probability;
746 float fa = ((collocator *)b)->probability;
747 return (fa > fb) - (fa < fb);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200748}
749
Marc Kupietz0ab97392024-12-10 16:16:32 +0100750char *getPosWiseW2VCollocators(char *word, long maxPerPos, long cutoff, float threshold, const char *format) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200751 HV *result = newHV();
Marc Kupietz59865a92021-03-11 17:16:51 +0100752 float *target_sums = NULL;
753 long a, b;
Marc Kupietz969cab92019-08-05 11:13:42 +0200754 knn *para_nbs[MAX_THREADS];
755 knn *syn_nbs[MAX_THREADS];
756 knnpars pars[MAX_THREADS];
757 pthread_t *pt = (pthread_t *)malloc((num_threads + 1) * sizeof(pthread_t));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200758 wordlist *wl;
Marc Kupietz969cab92019-08-05 11:13:42 +0200759 int syn_threads = (M2 ? window * 2 : 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200760 int search_backw = 0;
761 collocator *best = NULL;
Marc Kupietz969cab92019-08-05 11:13:42 +0200762 posix_memalign((void **)&best, 128, 10 * (maxPerPos >= 200 ? maxPerPos : 200) * sizeof(collocator));
763 memset(best, 0, (maxPerPos >= 200 ? maxPerPos : 200) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200764
Marc Kupietz969cab92019-08-05 11:13:42 +0200765 if (cutoff < 1 || cutoff > words)
766 cutoff = words;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200767
768 wl = getTargetWords(word, search_backw);
Marc Kupietz969cab92019-08-05 11:13:42 +0200769 if (wl == NULL || wl->length < 1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200770 return "";
771
Marc Kupietz969cab92019-08-05 11:13:42 +0200772 a = posix_memalign((void **)&target_sums, 128, cutoff * sizeof(float));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200773 memset(target_sums, 0, cutoff * sizeof(float));
774
775 printf("Starting %d threads\n", syn_threads);
776 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200777 for (a = 0; a < syn_threads; a++) {
778 pars[a].cutoff = cutoff;
779 pars[a].target_sums = target_sums;
780 pars[a].window_sums = window_sums;
781 pars[a].wl = wl;
782 pars[a].N = maxPerPos;
783 pars[a].threshold = threshold;
784 pars[a].from = a;
785 pars[a].upto = a + 1;
786 pthread_create(&pt[a], NULL, getCollocators, (void *)&pars[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200787 }
788 printf("Waiting for syn threads to join\n");
789 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200790 for (a = 0; a < syn_threads; a++) pthread_join(pt[a], (void *)&syn_nbs[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200791 printf("Syn threads joint\n");
792 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200793 result = malloc(maxPerPos * 80 * syn_threads);
Marc Kupietzbdd779a2024-08-05 10:02:29 +0200794 char *p = (char *) result;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200795 *p = 0;
Marc Kupietz0ab97392024-12-10 16:16:32 +0100796 if (strcmp(format, "tsv") == 0) {
797 for (a = syn_threads - 1; a >= 0; a--) {
798 for (b = 0; b < syn_nbs[a]->length; b++) {
799 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);
800 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200801 }
Marc Kupietz0ab97392024-12-10 16:16:32 +0100802 } else {
803 p += sprintf(p, "[");
804 for (a = syn_threads - 1; a >= 0; a--) {
805 for (b = 0; b < syn_nbs[a]->length; b++) {
806 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);
807 }
808 }
809 p-=2;
810 p += sprintf(p, "\n]");
Marc Kupietz969cab92019-08-05 11:13:42 +0200811 }
Marc Kupietz0ab97392024-12-10 16:16:32 +0100812
Marc Kupietzbdd779a2024-08-05 10:02:29 +0200813 return ((char *)result);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200814}
815
Marc Kupietz0ab97392024-12-10 16:16:32 +0100816char *getPosWiseW2VCollocatorsAsTsv(char *word, long maxPerPos, long cutoff, float threshold) {
817 return getPosWiseW2VCollocators(word, maxPerPos, cutoff, threshold, "tsv");
818}
819
820char *getPosWiseW2VCollocatorsAsJson(char *word, long maxPerPos, long cutoff, float threshold) {
821 return getPosWiseW2VCollocators(word, maxPerPos, cutoff, threshold, "json");
822}
823
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200824SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe, int no_similar_profiles) {
825 HV *result = newHV();
Marc Kupietz59865a92021-03-11 17:16:51 +0100826 float *target_sums = NULL;
Marc Kupietz969cab92019-08-05 11:13:42 +0200827 long a, b, c, d, slice;
828 knn *para_nbs[MAX_THREADS];
829 knn *syn_nbs[MAX_THREADS];
830 knnpars pars[MAX_THREADS];
831 pthread_t *pt = (pthread_t *)malloc((num_threads + 1) * sizeof(pthread_t));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200832 wordlist *wl;
Marc Kupietz969cab92019-08-05 11:13:42 +0200833 int syn_threads = (M2 ? window * 2 : 0);
834 int para_threads = (no_similar_profiles ? 0 : num_threads - syn_threads);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200835
836 collocator *best = NULL;
Marc Kupietz969cab92019-08-05 11:13:42 +0200837 posix_memalign((void **)&best, 128, 10 * (N >= 200 ? N : 200) * sizeof(collocator));
838 memset(best, 0, (N >= 200 ? N : 200) * sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200839
Marc Kupietz969cab92019-08-05 11:13:42 +0200840 if (N > MAX_NEIGHBOURS) N = MAX_NEIGHBOURS;
841
842 if (cutoff < 1 || cutoff > words)
843 cutoff = words;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200844
845 wl = getTargetWords(st1, search_backw);
Marc Kupietz969cab92019-08-05 11:13:42 +0200846 if (wl == NULL || wl->length < 1)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200847 goto end;
848
Marc Kupietz969cab92019-08-05 11:13:42 +0200849 slice = cutoff / para_threads;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200850
Marc Kupietz969cab92019-08-05 11:13:42 +0200851 a = posix_memalign((void **)&target_sums, 128, cutoff * sizeof(float));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200852 memset(target_sums, 0, cutoff * sizeof(float));
853
Marc Kupietzc0d41872021-02-25 16:33:22 +0100854 printf("Starting %d threads for paradigmatic search\n", para_threads);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200855 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200856 for (a = 0; a < para_threads; a++) {
857 pars[a].cutoff = cutoff;
858 pars[a].token = st1;
859 pars[a].wl = wl;
860 pars[a].N = N;
861 pars[a].best = &best[N * a];
862 if (merge_words == 0 || search_backw == 0) {
863 pars[a].from = a * slice;
864 pars[a].upto = ((a + 1) * slice > cutoff ? cutoff : (a + 1) * slice);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200865 } else {
866 pars[a].from = merge_words + a * slice;
Marc Kupietz969cab92019-08-05 11:13:42 +0200867 pars[a].upto = merge_words + ((a + 1) * slice > cutoff ? cutoff : (a + 1) * slice);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200868 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200869 printf("From: %ld, Upto: %ld\n", pars[a].from, pars[a].upto);
870 pthread_create(&pt[a], NULL, _get_neighbours, (void *)&pars[a]);
871 }
872 if (M2) {
873 for (a = 0; a < syn_threads; a++) {
874 pars[a + para_threads].cutoff = cutoff;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200875 pars[a + para_threads].target_sums = target_sums;
876 pars[a + para_threads].window_sums = window_sums;
877 pars[a + para_threads].wl = wl;
878 pars[a + para_threads].N = N;
879 pars[a + para_threads].threshold = MIN_RESP;
880 pars[a + para_threads].from = a;
Marc Kupietz969cab92019-08-05 11:13:42 +0200881 pars[a + para_threads].upto = a + 1;
882 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *)&pars[a + para_threads]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200883 }
884 }
885 printf("Waiting for para threads to join\n");
886 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200887 for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *)&para_nbs[a]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200888 printf("Para threads joint\n");
889 fflush(stdout);
890
Marc Kupietz969cab92019-08-05 11:13:42 +0200891 /* if(!syn_nbs[0]) */
892 /* goto end; */
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200893
Marc Kupietz969cab92019-08-05 11:13:42 +0200894 qsort(best, N * para_threads, sizeof(collocator), cmp_activation);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200895
896 long long chosen[MAX_NEIGHBOURS];
Marc Kupietz59865a92021-03-11 17:16:51 +0100897 printf("N: %d\n", N);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200898
Marc Kupietz969cab92019-08-05 11:13:42 +0200899 AV *array = newAV();
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200900 int i, j;
Marc Kupietz969cab92019-08-05 11:13:42 +0200901 int l1_words = 0, l2_words = 0;
902
903 for (a = 0, i = 0; i < N && a < N * para_threads; a++) {
904 int filtered = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200905 long long c = best[a].wordi;
906 if ((merge_words && dedupe && i > 1) || (!merge_words && dedupe && i > 0)) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200907 for (j = 0; j < i && !filtered; j++)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200908 if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) ||
909 strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) {
Marc Kupietz969cab92019-08-05 11:13:42 +0200910 printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]);
911 filtered = 1;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200912 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200913 if (filtered)
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200914 continue;
915 }
916
Marc Kupietz969cab92019-08-05 11:13:42 +0200917 if (0 && merge_words > 0) {
918 if (c >= merge_words) {
919 if (l1_words > N / 2)
920 continue;
921 else
922 l1_words++;
923 } else {
924 if (l2_words > N / 2)
925 continue;
926 else
927 l2_words++;
928 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200929 }
930
Marc Kupietz969cab92019-08-05 11:13:42 +0200931 // printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
932 // fflush(stdout);
933 HV *hash = newHV();
934 SV *word = newSVpvf(&vocab[c * max_w], 0);
935 chosen[i] = c;
936 if (latin_enc == 0) SvUTF8_on(word);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200937 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200938 hv_store(hash, "word", strlen("word"), word, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200939 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
940 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
941 AV *vector = newAV();
942 for (b = 0; b < size; b++) {
943 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
944 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200945 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV *)vector), 0);
946 av_push(array, newRV_noinc((SV *)hash));
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200947 i++;
948 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200949 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV *)array), 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200950
Marc Kupietz969cab92019-08-05 11:13:42 +0200951 for (b = 0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200952 best[b].wordi = -1L;
953 best[b].activation = 0;
954 best[b].probability = 0;
955 best[b].position = 0;
956 best[b].activation_sum = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +0200957 memset(best[b].heat, 0, sizeof(float) * 16);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200958 }
959
Marc Kupietz969cab92019-08-05 11:13:42 +0200960 float total_activation = 0;
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200961
962 if (M2) {
963 printf("Waiting for syn threads to join\n");
964 fflush(stdout);
Marc Kupietz969cab92019-08-05 11:13:42 +0200965 for (a = 0; a < syn_threads; a++) pthread_join(pt[a + para_threads], (void *)&syn_nbs[a]);
966 for (a = 0; a <= syn_threads; a++) {
967 if (a == window) continue;
968 total_activation += window_sums[a];
Marc Kupietz59865a92021-03-11 17:16:51 +0100969 printf("window pos: %ld, sum: %f\n", a, window_sums[a]);
Marc Kupietz969cab92019-08-05 11:13:42 +0200970 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200971 printf("syn threads joint\n");
972 fflush(stdout);
973
Marc Kupietz969cab92019-08-05 11:13:42 +0200974 for (b = 0; b < syn_nbs[0]->length; b++) {
975 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
976 best[b].position = -1; // syn_nbs[0]->pos[b];
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200977 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietz969cab92019-08-05 11:13:42 +0200978 best[b].max_activation = 0.0;
979 best[b].average = 0.0;
980 best[b].probability = 0.0;
981 best[b].cprobability = syn_nbs[0]->best[b].cprobability;
982 memset(best[b].heat, 0, sizeof(float) * 16);
Marc Kupietzf11d20c2019-08-02 15:42:04 +0200983 }
Marc Kupietz969cab92019-08-05 11:13:42 +0200984
985 float best_window_sum[MAX_NEIGHBOURS];
Marc Kupietz59865a92021-03-11 17:16:51 +0100986 int found_index = 0, i = 0, w;
Marc Kupietz969cab92019-08-05 11:13:42 +0200987 for (a = 0; a < syn_threads; a++) {
988 for (b = 0; b < syn_nbs[a]->length; b++) {
989 for (i = 0; i < found_index; i++)
990 if (best[i].wordi == syn_nbs[a]->best[b].wordi)
991 break;
992 if (i >= found_index) {
993 best[found_index].max_activation = 0.0;
994 best[found_index].average = 0.0;
995 best[found_index].probability = 0.0;
996 memset(best[found_index].heat, 0, sizeof(float) * 16);
997 best[found_index].cprobability = syn_nbs[a]->best[b].cprobability;
998 best[found_index].activation_sum = target_sums[syn_nbs[a]->best[b].wordi]; // syn_nbs[a]->best[b].activation_sum;
999 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
1000 // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
1001 }
1002 }
1003 }
1004 sort_by = 0; // ALWAYS AUTO-FOCUS
1005 if (sort_by != 1 && sort_by != 2) { // sort by auto focus mean
1006 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) - 1);
1007 int wpos;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001008 int bits_set = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +02001009 for (i = 0; i < found_index; i++) {
1010 best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0;
1011 for (w = 1; w < (1 << syn_threads); w++) { // loop through all possible windows
1012 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 +02001013 bits_set = 0;
Marc Kupietz969cab92019-08-05 11:13:42 +02001014 for (a = 0; a < syn_threads; a++) {
1015 if ((1 << a) & w) {
1016 wpos = (a >= window ? a + 1 : a);
1017 total_window_sum += window_sums[wpos];
1018 }
1019 }
1020 // printf("%d window-sum %f\n", w, total_window_sum);
1021 for (a = 0; a < syn_threads; a++) {
1022 if ((1 << a) & w) {
1023 wpos = (a >= window ? a + 1 : a);
1024 bits_set++;
1025 for (b = 0; b < syn_nbs[a]->length; b++)
1026 if (best[i].wordi == syn_nbs[a]->best[b].wordi) {
1027 // float acti = syn_nbs[a]->best[b].activation / total_window_sum;
1028 // word_window_sum += syn_nbs[a]->dist[b] * syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1029 // word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1030 // 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 +02001031
Marc Kupietz969cab92019-08-05 11:13:42 +02001032 word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1033 // 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 +02001034
Marc Kupietz969cab92019-08-05 11:13:42 +02001035 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 +02001036 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 +02001037 word_activation_sum += syn_nbs[a]->best[b].activation;
1038 if (syn_nbs[a]->best[b].activation > best[i].max_activation)
1039 best[i].max_activation = syn_nbs[a]->best[b].activation;
1040 if (syn_nbs[a]->best[b].activation > best[i].heat[wpos])
1041 best[i].heat[wpos] = syn_nbs[a]->best[b].activation;
1042 }
1043 }
1044 }
1045 if (bits_set) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001046 word_window_average /= bits_set;
Marc Kupietz969cab92019-08-05 11:13:42 +02001047 // word_activation_sum /= bits_set;
1048 // word_window_sum /= bits_set;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001049 }
1050
Marc Kupietz969cab92019-08-05 11:13:42 +02001051 word_window_sum /= total_window_sum;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001052
Marc Kupietz969cab92019-08-05 11:13:42 +02001053 if (word_window_sum > best[i].probability) {
1054 // best[i].position = w;
1055 best[i].probability = word_window_sum;
1056 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001057
Marc Kupietz969cab92019-08-05 11:13:42 +02001058 if (word_cprobability_sum > best[i].cprobability_sum) {
1059 best[i].position = w;
1060 best[i].cprobability_sum = word_cprobability_sum;
1061 }
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001062
Marc Kupietz969cab92019-08-05 11:13:42 +02001063 best[i].average = word_window_average;
1064 // best[i].activation = word_activation_sum;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001065 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001066 }
1067 qsort(best, found_index, sizeof(collocator), cmp_probability);
1068 // for(i=0; i < found_index; i++) {
1069 // printf("found: %s - sum: %f - window: %d\n", &vocab[best[i].wordi * max_w], best[i].activation, best[i].position);
1070 // }
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001071
Marc Kupietz969cab92019-08-05 11:13:42 +02001072 } else if (sort_by == 1) { // responsiveness any window position
1073 int wpos;
1074 for (i = 0; i < found_index; i++) {
1075 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
1076 for (a = 0; a < syn_threads; a++) {
1077 wpos = (a >= window ? a + 1 : a);
1078 for (b = 0; b < syn_nbs[a]->length; b++)
1079 if (best[i].wordi == syn_nbs[a]->best[b].wordi) {
1080 best[i].probability += syn_nbs[a]->best[b].probability;
1081 if (syn_nbs[a]->best[b].activation > 0.25)
1082 best[i].position |= 1 << wpos;
1083 if (syn_nbs[a]->best[b].activation > best[i].activation) {
1084 best[i].activation = syn_nbs[a]->best[b].activation;
1085 }
1086 }
1087 }
1088 }
1089 qsort(best, found_index, sizeof(collocator), cmp_activation);
1090 } else if (sort_by == 2) { // single window position
1091 for (a = 1; a < syn_threads; a++) {
1092 for (b = 0; b < syn_nbs[a]->length; b++) {
1093 for (c = 0; c < MAX_NEIGHBOURS; c++) {
1094 if (syn_nbs[a]->best[b].activation > best[c].activation) {
1095 for (d = MAX_NEIGHBOURS - 1; d > c; d--) {
1096 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001097 }
1098 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
Marc Kupietz969cab92019-08-05 11:13:42 +02001099 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 +02001100 break;
1101 }
1102 }
1103 }
1104 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001105 } else { // sort by mean p
1106 for (a = 1; a < syn_threads; a++) {
1107 for (b = 0; b < syn_nbs[a]->length; b++) {
1108 for (c = 0; c < MAX_NEIGHBOURS; c++) {
1109 if (target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
1110 for (d = MAX_NEIGHBOURS - 1; d > c; d--) {
1111 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001112 }
1113 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
Marc Kupietz969cab92019-08-05 11:13:42 +02001114 best[c].position = (1 << 2 * window) - 1; // syn_nbs[a]->pos[b];
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001115 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
1116 break;
1117 }
1118 }
1119 }
1120 }
1121 }
1122 array = newAV();
Marc Kupietz969cab92019-08-05 11:13:42 +02001123 for (a = 0, i = 0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001124 long long c = best[a].wordi;
Marc Kupietz969cab92019-08-05 11:13:42 +02001125 /*
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001126 if (dedupe) {
1127 int filtered=0;
1128 for (j=0; j<i; j++)
1129 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
1130 strcasestr(chosen[j], &vocab[c * max_w])) {
1131 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
1132 filtered = 1;
1133 }
1134 if(filtered)
1135 continue;
1136 }
1137*/
Marc Kupietz969cab92019-08-05 11:13:42 +02001138 chosen[i++] = c;
1139 HV *hash = newHV();
1140 SV *word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
1141 AV *heat = newAV();
1142 if (latin_enc == 0) SvUTF8_on(word);
1143 hv_store(hash, "word", strlen("word"), word, 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001144 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
1145 hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0);
1146 hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0);
1147 hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0);
Marc Kupietz969cab92019-08-05 11:13:42 +02001148 hv_store(hash, "max", strlen("max"), newSVnv(best[a].max_activation), 0); // newSVnv(target_sums[best[a].wordi]), 0);
1149 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 +02001150 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietz969cab92019-08-05 11:13:42 +02001151 best[a].heat[5] = 0;
1152 for (i = 10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i]));
1153 hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV *)heat), 0);
1154 av_push(array, newRV_noinc((SV *)hash));
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001155 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001156 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV *)array), 0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001157 }
1158end:
Marc Kupietz969cab92019-08-05 11:13:42 +02001159 free(best);
1160 return newRV_noinc((SV *)result);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001161}
1162
1163int dump_vecs(char *fname) {
Marc Kupietz969cab92019-08-05 11:13:42 +02001164 long i, j;
1165 FILE *f;
1166 /* if(words>100000)
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001167 words=100000;
1168*/
Marc Kupietz969cab92019-08-05 11:13:42 +02001169 if ((f = fopen(fname, "w")) == NULL) {
1170 fprintf(stderr, "cannot open %s for writing\n", fname);
1171 return (-1);
1172 }
1173 fprintf(f, "%lld %lld\n", words, size);
1174 for (i = 0; i < words; i++) {
1175 fprintf(f, "%s ", &vocab[i * max_w]);
1176 for (j = 0; j < size - 1; j++)
1177 fprintf(f, "%f ", M[i * size + j]);
1178 fprintf(f, "%f\n", M[i * size + j]);
1179 }
1180 fclose(f);
1181 return (0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001182}
1183
1184int dump_for_numpy(char *fname) {
Marc Kupietz969cab92019-08-05 11:13:42 +02001185 long i, j;
1186 FILE *f;
Marc Kupietzc0d41872021-02-25 16:33:22 +01001187 int max = words; // 300000;
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001188
Marc Kupietz969cab92019-08-05 11:13:42 +02001189 if ((f = fopen(fname, "w")) == NULL) {
1190 fprintf(stderr, "cannot open %s for writing\n", fname);
1191 return (-1);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001192 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001193 for (i = 0; i < max; i++) {
1194 for (j = 0; j < size - 1; j++)
1195 fprintf(f, "%f\t", M[i * size + j]);
1196 fprintf(f, "%f\n", M[i * size + j]);
1197 printf("%s\r\n", &vocab[i * max_w]);
1198 }
1199 if (merged_end > 0) {
1200 for (i = 0; i < max; i++) {
1201 for (j = 0; j < size - 1; j++)
1202 fprintf(f, "%f\t", M[(merged_end + i) * size + j]);
1203 fprintf(f, "%f\n", M[(merged_end + i) * size + j]);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001204 printf("_%s\r\n", &vocab[i * max_w]);
1205 }
Marc Kupietz969cab92019-08-05 11:13:42 +02001206 }
1207 fclose(f);
1208 return (0);
Marc Kupietzf11d20c2019-08-02 15:42:04 +02001209}
Marc Kupietz043db152023-11-05 17:47:53 +01001210
1211unsigned long getVocabSize() {
1212 return (unsigned long) words;
1213}