Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1 | #include <collocatordb.h> |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 2 | #include <math.h> |
| 3 | #include <pthread.h> |
| 4 | #include <stdio.h> |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 5 | #include <stdlib.h> |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 6 | #include <string.h> |
| 7 | #include <sys/mman.h> |
Marc Kupietz | e288d8e | 2024-11-15 16:18:50 +0100 | [diff] [blame^] | 8 | #include <fcntl.h> |
| 9 | #include <unistd.h> |
| 10 | #include <perl.h> |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 11 | |
| 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 |
| 23 | void *connection_handler(void *); |
| 24 | |
| 25 | typedef struct { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 26 | 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 Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 36 | } collocator; |
| 37 | |
| 38 | typedef struct { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 39 | collocator *best; |
| 40 | int length; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 41 | } knn; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 42 | |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 43 | typedef struct { |
| 44 | long long wordi[MAX_NEIGHBOURS]; |
| 45 | char sep[MAX_NEIGHBOURS]; |
| 46 | int length; |
| 47 | } wordlist; |
| 48 | |
| 49 | typedef struct { |
| 50 | long cutoff; |
| 51 | wordlist *wl; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 52 | char *token; |
| 53 | int N; |
| 54 | long from; |
| 55 | unsigned long upto; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 56 | collocator *best; |
| 57 | float *target_sums; |
| 58 | float *window_sums; |
| 59 | float threshold; |
| 60 | } knnpars; |
| 61 | |
| 62 | typedef struct { |
| 63 | uint32_t index; |
| 64 | float value; |
| 65 | } sparse_t; |
| 66 | |
| 67 | typedef struct { |
| 68 | uint32_t len; |
| 69 | sparse_t nbr[100]; |
| 70 | } profile_t; |
| 71 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 72 | float *M, *M2 = 0L, *syn1neg_window, *expTable; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 73 | float *window_sums; |
| 74 | char *vocab; |
| 75 | char *garbage = NULL; |
| 76 | COLLOCATORDB *cdb = NULL; |
| 77 | profile_t *sprofiles = NULL; |
| 78 | size_t sprofiles_qty = 0; |
| 79 | |
| 80 | long long words, size, merged_end; |
| 81 | long long merge_words = 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 82 | int num_threads = 20; |
| 83 | int latin_enc = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 84 | int window; |
| 85 | |
| 86 | /* load collocation profiles if file exists */ |
| 87 | int load_sprofiles(char *vecsname) { |
| 88 | char *basename = strdup(vecsname); |
| 89 | char *pos = strstr(basename, ".vecs"); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 90 | if (pos) |
| 91 | *pos = 0; |
| 92 | |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 93 | char binsprofiles_fname[256]; |
| 94 | strcpy(binsprofiles_fname, basename); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 95 | strcat(binsprofiles_fname, ".sprofiles.bin"); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 96 | 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 Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 106 | sprofiles = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 107 | if (sprofiles == MAP_FAILED) { |
| 108 | close(fd); |
| 109 | fprintf(stderr, "Cannot mmap %s\n", binsprofiles_fname); |
| 110 | sprofiles = NULL; |
| 111 | return 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 112 | } else { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 113 | 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 Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 119 | char *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 Kupietz | 0efe49b | 2020-04-06 18:30:22 +0200 | [diff] [blame] | 131 | int init_net(char *file_name, char *net_name, int latin, int do_open_cdb) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 132 | FILE *f, *binvecs, *binwords; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 133 | int binwords_fd, binvecs_fd, net_fd, i; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 134 | long long a, b; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 135 | float len; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 136 | double val; |
| 137 | |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 138 | 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 Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 145 | strcat(binwords_fname, ".words"); |
| 146 | strcpy(binvecs_fname, file_name); |
| 147 | strcat(binvecs_fname, ".vecs"); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 148 | |
| 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 Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 156 | if (MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 157 | fscanf(f, "%lld", &size); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 158 | if ((binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 159 | printf("Converting %s to memory mappable structures\n", file_name); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 160 | 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 Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 167 | printf("%lld words in ascii vector file with vector size %lld\n", words, size); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 168 | 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 Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 181 | } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 182 | 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 Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 201 | 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 Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 206 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 207 | } |
| 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 Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 218 | fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname); |
| 219 | exit(-1); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 220 | } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 221 | fclose(f); |
| 222 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 223 | 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 Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 226 | // 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 Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 234 | syn1neg_window = M2 + words * size; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 235 | } 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 Kupietz | 0efe49b | 2020-04-06 18:30:22 +0200 | [diff] [blame] | 241 | 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 Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 251 | } else { |
| 252 | fprintf(stderr, "Cannot open collocator DB %s\n", collocatordb_name); |
Marc Kupietz | 0efe49b | 2020-04-06 18:30:22 +0200 | [diff] [blame] | 253 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 257 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 258 | 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 Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 268 | long mergeVectors(char *file_name) { |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 269 | FILE *f; |
| 270 | int binwords_fd, binvecs_fd; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 271 | float *merge_vecs; |
| 272 | char *merge_vocab; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 273 | /* long long merge_words, merge_size; */ |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 274 | long long merge_size; |
| 275 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 276 | char binvecs_fname[256], binwords_fname[256]; |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 277 | |
| 278 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 279 | strcpy(binwords_fname, file_name); |
| 280 | strcat(binwords_fname, ".words"); |
| 281 | strcpy(binvecs_fname, file_name); |
| 282 | strcat(binvecs_fname, ".vecs"); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 283 | |
| 284 | f = fopen(file_name, "rb"); |
| 285 | if (f == NULL) { |
| 286 | printf("Input file %s not found\n", file_name); |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 287 | exit(-1); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 288 | } |
| 289 | fscanf(f, "%lld", &merge_words); |
| 290 | fscanf(f, "%lld", &merge_size); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 291 | 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 Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 298 | if (merge_vecs == NULL || merge_vocab == NULL) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 299 | 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 Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 303 | } |
| 304 | read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float)); |
| 305 | read(binwords_fd, merge_vocab, merge_words * max_w); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 306 | } else { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 307 | fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname); |
| 308 | exit(-1); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 309 | } |
| 310 | printf("Successfully reallocated memory\nMerging...\n"); |
| 311 | fflush(stdout); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 312 | 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 Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 321 | printf("merged_end: %lld, words: %lld\n", merged_end, words); |
| 322 | //printBiggestMergedDifferences(); |
| 323 | return ((long)merged_end); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | void 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 Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 332 | w = (unsigned char *) vocab + i * max_w; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 333 | previous = 0; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 334 | if (strncmp("quot", (const char *)w, 4) == 0) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 335 | garbage[i] = 1; |
| 336 | // printf("Gargabe: %s\n", vocab + i * max_w); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 337 | } else { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 338 | while ((c = *w++) && !garbage[i]) { |
| 339 | if (((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) || |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 340 | (previous == '-' && (c & 32)) || |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 341 | (previous == 0xc2 && (c == 0xa4 || c == 0xb6)) || |
| 342 | (previous == 'q' && c == 'u' && *(w) == 'o' && *(w + 1) == 't') || /* quot */ |
| 343 | c == '<') { |
| 344 | garbage[i] = 1; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 345 | continue; |
| 346 | } |
| 347 | previous = c; |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | return; |
| 352 | } |
| 353 | |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 354 | knn *simpleGetCollocators(int word, int number, long cutoff, int *result) { |
| 355 | knnpars *pars = calloc(sizeof(knnpars), 1); |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 356 | float *target_sums = NULL; |
| 357 | float *my_window_sums = malloc(sizeof(float) * (window + 1) * 2); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 358 | pars->cutoff = (cutoff ? cutoff : 300000); |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 359 | long a; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 360 | for (a = 0; a < cutoff; a++) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 361 | target_sums[a] = 0; |
| 362 | pars->target_sums = target_sums; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 363 | pars->window_sums = my_window_sums; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 364 | pars->N = (number ? number : 20); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 365 | pars->from = 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 366 | pars->upto = window * 2 - 1; |
| 367 | knn *syn_nbs = NULL; // = (knn*) getCollocators(pars); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 368 | free(pars); |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 369 | free(my_window_sums); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 370 | free(target_sums); |
| 371 | return syn_nbs; |
| 372 | } |
| 373 | |
| 374 | void *getCollocators(void *args) { |
| 375 | knnpars *pars = args; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 376 | int N = pars->N; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 377 | |
| 378 | int cc = pars->wl->wordi[0]; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 379 | knn *nbs = NULL; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 380 | long window_layer_size = size * window * 2; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 381 | long a, b, c, d, window_offset, target, max_target = 0, maxmax_target; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 382 | float f, max_f, maxmax_f; |
| 383 | float *target_sums = NULL, worstbest, wpos_sum; |
| 384 | collocator *best; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 385 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 386 | if (M2 == NULL || cc == -1) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 387 | return NULL; |
| 388 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 389 | a = posix_memalign((void **)&target_sums, 128, pars->cutoff * sizeof(float)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 390 | memset(target_sums, 0, pars->cutoff * sizeof(float)); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 391 | best = malloc((N > 200 ? N : 200) * sizeof(collocator)); |
| 392 | memset(best, 0, (N > 200 ? N : 200) * sizeof(collocator)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 393 | worstbest = pars->threshold; |
| 394 | |
| 395 | for (b = 0; b < pars->cutoff; b++) |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 396 | target_sums[b] = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 397 | for (b = 0; b < N; b++) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 398 | best[b].wordi = -1; |
| 399 | best[b].probability = 1; |
| 400 | best[b].activation = worstbest; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | d = cc; |
| 404 | maxmax_f = -1; |
| 405 | maxmax_target = 0; |
| 406 | |
| 407 | for (a = pars->from; a < pars->upto; a++) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 408 | if (a >= window) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 409 | 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 Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 417 | for (target = 0; target < pars->cutoff; target++) { |
| 418 | if (garbage && garbage[target]) continue; |
| 419 | if (target == d) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 420 | continue; |
| 421 | f = 0; |
| 422 | for (c = 0; c < size; c++) |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 423 | f += M2[d * size + c] * syn1neg_window[target * window_layer_size + window_offset + c]; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 424 | if (f < -MAX_EXP) |
| 425 | continue; |
| 426 | else if (f > MAX_EXP) |
| 427 | continue; |
| 428 | else |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 429 | f = expTable[(int)((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))]; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 430 | wpos_sum += f; |
| 431 | |
| 432 | target_sums[target] += f; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 433 | if (f > worstbest) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 434 | for (b = 0; b < N; b++) { |
| 435 | if (f > best[b].activation) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 436 | 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 Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 441 | } |
| 442 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 443 | if (b == N - 1) |
| 444 | worstbest = best[N - 1].activation; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 445 | } |
| 446 | } |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 447 | printf("%ld %.2f\n", max_target, max_f); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 448 | printf("%s (%.2f) ", &vocab[max_target * max_w], max_f); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 449 | if (max_f > maxmax_f) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 450 | maxmax_f = max_f; |
| 451 | maxmax_target = max_target; |
| 452 | } |
| 453 | for (b = 0; b < N; b++) |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 454 | if (best[b].position == window - a) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 455 | best[b].cprobability = best[b].activation / wpos_sum; |
| 456 | } else { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 457 | printf("\x1b[1m%s\x1b[0m ", &vocab[d * max_w]); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 458 | } |
| 459 | pars->window_sums[a] = wpos_sum; |
| 460 | } |
| 461 | for (b = 0; b < pars->cutoff; b++) |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 462 | pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 463 | |
| 464 | free(target_sums); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 465 | for (b = 0; b < N && best[b].wordi >= 0; b++) |
| 466 | ; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 467 | // THIS LOOP IS NEEDED (b...) |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 468 | // 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 Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 470 | nbs = malloc(sizeof(knn)); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 471 | nbs->best = best; |
| 472 | nbs->length = b - 1; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 473 | pthread_exit(nbs); |
| 474 | } |
| 475 | |
Marc Kupietz | 0efe49b | 2020-04-06 18:30:22 +0200 | [diff] [blame] | 476 | float 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 Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 486 | fprintf(stderr, "hidden: %d - assert: hidden < %lld failed.\n", hidden, size); |
Marc Kupietz | 0efe49b | 2020-04-06 18:30:22 +0200 | [diff] [blame] | 487 | exit(-1); |
| 488 | } |
| 489 | |
| 490 | if (target >= words) { |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 491 | fprintf(stderr, "target: %ld - assert: target < %lld failed.\n", target, words); |
Marc Kupietz | 0efe49b | 2020-04-06 18:30:22 +0200 | [diff] [blame] | 492 | 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 Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 503 | AV *getVecs(AV *array) { |
| 504 | int i, b; |
| 505 | AV *result = newAV(); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 506 | for (i = 0; i <= av_len(array); i++) { |
| 507 | SV **elem = av_fetch(array, i, 0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 508 | if (elem != NULL) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 509 | long j = (long)SvNV(*elem); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 510 | AV *vector = newAV(); |
| 511 | for (b = 0; b < size; b++) { |
| 512 | av_push(vector, newSVnv(M[b + j * size])); |
| 513 | } |
Marc Kupietz | bdd779a | 2024-08-05 10:02:29 +0200 | [diff] [blame] | 514 | av_push(result, newRV_noinc((SV *)vector)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | return result; |
| 518 | } |
| 519 | |
| 520 | char *getSimilarProfiles(long node) { |
| 521 | int i; |
| 522 | char buffer[120000]; |
| 523 | char pair_buffer[2048]; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 524 | buffer[0] = '['; |
| 525 | buffer[1] = 0; |
| 526 | if (node >= sprofiles_qty) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 527 | printf("Not available in precomputed profile\n"); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 528 | return (strdup("[{\"w\":\"not available\", \"v\":0}]\n")); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | printf("******* %s ******\n", &vocab[max_w * node]); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 532 | |
| 533 | for (i = 0; i < 100 && i < sprofiles[node].len; i++) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 534 | 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 Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 537 | buffer[strlen(buffer) - 1] = ']'; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 538 | strcat(buffer, "\n"); |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 539 | printf("%s", buffer); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 540 | return (strdup(buffer)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 541 | } |
| 542 | |
Marc Kupietz | f608001 | 2021-03-12 09:14:42 +0100 | [diff] [blame] | 543 | char *getCollocationScores(long node, long collocate) { |
| 544 | char *res = (cdb ? strdup(get_collocation_scores_as_json(cdb, node, collocate)) : "[]"); |
| 545 | return res; |
| 546 | } |
| 547 | |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 548 | char *getClassicCollocators(long node) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 549 | char *res = (cdb ? strdup(get_collocators_as_json(cdb, node)) : "[]"); |
| 550 | return res; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | wordlist *getTargetWords(char *st1, int search_backw) { |
| 554 | wordlist *wl = malloc(sizeof(wordlist)); |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 555 | char st[100][max_size]; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 556 | long a, b = 0, c = 0, cn = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 557 | |
| 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 Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 564 | if (st1[c] == ' ' /*|| st1[c] == '-'*/) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 565 | b = 0; |
| 566 | c++; |
| 567 | } |
| 568 | } |
| 569 | cn++; |
| 570 | for (a = 0; a < cn; a++) { |
| 571 | if (search_backw) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 572 | for (b = words - 1; b >= (merge_words ? merge_words : 0) && strcmp(&vocab[b * max_w], st[a]) != 0; b--) |
| 573 | ; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 574 | } else { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 575 | for (b = 0; b < (merge_words ? merge_words : words) && strcmp(&vocab[b * max_w], st[a]) != 0; b++) |
| 576 | ; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 577 | } |
| 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 Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 584 | fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", &vocab[wl->wordi[a] * max_w], wl->wordi[a]); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 585 | } |
| 586 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 587 | wl->length = cn; |
| 588 | return (wl); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 589 | } |
| 590 | |
Marc Kupietz | cb43e49 | 2019-12-03 10:07:53 +0100 | [diff] [blame] | 591 | long 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 Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 598 | float 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 Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 605 | char *getBiggestMergedDifferences() { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 606 | static char *result = NULL; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 607 | float dist; |
| 608 | long long a, c; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 609 | int N = 1000; |
| 610 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 611 | if (merged_end == 0) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 612 | result = "[]"; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 613 | |
| 614 | if (result != NULL) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 615 | 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 Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 622 | float worstbest = 1000000; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 623 | |
| 624 | for (a = 0; a < N; a++) best[a].activation = worstbest; |
| 625 | |
| 626 | for (c = 0; c < 500000; c++) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 627 | if (garbage && garbage[c]) continue; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 628 | dist = 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 629 | for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + (c + merged_end) * size]; |
| 630 | if (dist < worstbest) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 631 | for (a = 0; a < N; a++) { |
| 632 | if (dist < best[a].activation) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 633 | memmove(best + a + 1, best + a, (N - a - 1) * sizeof(collocator)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 634 | best[a].activation = dist; |
| 635 | best[a].wordi = c; |
| 636 | break; |
| 637 | } |
| 638 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 639 | worstbest = best[N - 1].activation; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 643 | result = malloc(N * max_w); |
Marc Kupietz | bdd779a | 2024-08-05 10:02:29 +0200 | [diff] [blame] | 644 | char *p = (char *) result; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 645 | *p++ = '['; |
| 646 | *p = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 647 | for (a = 0; a < N; a++) { |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 648 | p += sprintf(p, "{\"rank\":%lld,\"word\":\"%s\",\"dist\":%.3f},", a, &vocab[best[a].wordi * max_w], 1 - best[a].activation); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 649 | } |
| 650 | *--p = ']'; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 651 | return (result); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 652 | } |
| 653 | |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 654 | float cos_similarity(long b, long c) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 655 | float dist = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 656 | long a; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 657 | for (a = 0; a < size; a++) dist += M[b * size + a] * M[c * size + a]; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 658 | return dist; |
| 659 | } |
| 660 | |
| 661 | char *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 Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 666 | if (a == NULL || b == NULL || a->length != 1 || b->length != 1) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 667 | 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 | |
| 676 | void *_get_neighbours(void *arg) { |
| 677 | knnpars *pars = arg; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 678 | int N = pars->N; |
| 679 | long from = pars->from; |
| 680 | unsigned long upto = pars->upto; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 681 | char *sep; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 682 | float dist, len, vec[max_size]; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 683 | long long a, b, c, cn, *bi; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 684 | knn *nbs = NULL; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 685 | wordlist *wl = pars->wl; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 686 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 687 | collocator *best = pars->best; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 688 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 689 | float worstbest = -1; |
| 690 | |
| 691 | for (a = 0; a < N; a++) best[a].activation = 0; |
| 692 | a = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 693 | bi = wl->wordi; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 694 | cn = wl->length; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 695 | sep = wl->sep; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 696 | b = bi[0]; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 697 | if (b == -1) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 698 | goto end; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 699 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 700 | 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 Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 704 | 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 Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 707 | } |
| 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 Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 733 | |
| 734 | end: |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 735 | pthread_exit(nbs); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 736 | } |
| 737 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 738 | int 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 Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 742 | } |
| 743 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 744 | int 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 Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 748 | } |
| 749 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 750 | char *getPosWiseW2VCollocatorsAsTsv(char *word, long maxPerPos, long cutoff, float threshold) { |
| 751 | HV *result = newHV(); |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 752 | float *target_sums = NULL; |
| 753 | long a, b; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 754 | 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 Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 758 | wordlist *wl; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 759 | int syn_threads = (M2 ? window * 2 : 0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 760 | int search_backw = 0; |
| 761 | collocator *best = NULL; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 762 | posix_memalign((void **)&best, 128, 10 * (maxPerPos >= 200 ? maxPerPos : 200) * sizeof(collocator)); |
| 763 | memset(best, 0, (maxPerPos >= 200 ? maxPerPos : 200) * sizeof(collocator)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 764 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 765 | if (cutoff < 1 || cutoff > words) |
| 766 | cutoff = words; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 767 | |
| 768 | wl = getTargetWords(word, search_backw); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 769 | if (wl == NULL || wl->length < 1) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 770 | return ""; |
| 771 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 772 | a = posix_memalign((void **)&target_sums, 128, cutoff * sizeof(float)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 773 | memset(target_sums, 0, cutoff * sizeof(float)); |
| 774 | |
| 775 | printf("Starting %d threads\n", syn_threads); |
| 776 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 777 | 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 Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 787 | } |
| 788 | printf("Waiting for syn threads to join\n"); |
| 789 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 790 | for (a = 0; a < syn_threads; a++) pthread_join(pt[a], (void *)&syn_nbs[a]); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 791 | printf("Syn threads joint\n"); |
| 792 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 793 | result = malloc(maxPerPos * 80 * syn_threads); |
Marc Kupietz | bdd779a | 2024-08-05 10:02:29 +0200 | [diff] [blame] | 794 | char *p = (char *) result; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 795 | *p = 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 796 | for (a = syn_threads - 1; a >= 0; a--) { |
| 797 | for (b = 0; b < syn_nbs[a]->length; b++) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 798 | 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); |
| 799 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 800 | } |
Marc Kupietz | bdd779a | 2024-08-05 10:02:29 +0200 | [diff] [blame] | 801 | return ((char *)result); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe, int no_similar_profiles) { |
| 805 | HV *result = newHV(); |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 806 | float *target_sums = NULL; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 807 | long a, b, c, d, slice; |
| 808 | knn *para_nbs[MAX_THREADS]; |
| 809 | knn *syn_nbs[MAX_THREADS]; |
| 810 | knnpars pars[MAX_THREADS]; |
| 811 | pthread_t *pt = (pthread_t *)malloc((num_threads + 1) * sizeof(pthread_t)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 812 | wordlist *wl; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 813 | int syn_threads = (M2 ? window * 2 : 0); |
| 814 | int para_threads = (no_similar_profiles ? 0 : num_threads - syn_threads); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 815 | |
| 816 | collocator *best = NULL; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 817 | posix_memalign((void **)&best, 128, 10 * (N >= 200 ? N : 200) * sizeof(collocator)); |
| 818 | memset(best, 0, (N >= 200 ? N : 200) * sizeof(collocator)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 819 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 820 | if (N > MAX_NEIGHBOURS) N = MAX_NEIGHBOURS; |
| 821 | |
| 822 | if (cutoff < 1 || cutoff > words) |
| 823 | cutoff = words; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 824 | |
| 825 | wl = getTargetWords(st1, search_backw); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 826 | if (wl == NULL || wl->length < 1) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 827 | goto end; |
| 828 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 829 | slice = cutoff / para_threads; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 830 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 831 | a = posix_memalign((void **)&target_sums, 128, cutoff * sizeof(float)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 832 | memset(target_sums, 0, cutoff * sizeof(float)); |
| 833 | |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 834 | printf("Starting %d threads for paradigmatic search\n", para_threads); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 835 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 836 | for (a = 0; a < para_threads; a++) { |
| 837 | pars[a].cutoff = cutoff; |
| 838 | pars[a].token = st1; |
| 839 | pars[a].wl = wl; |
| 840 | pars[a].N = N; |
| 841 | pars[a].best = &best[N * a]; |
| 842 | if (merge_words == 0 || search_backw == 0) { |
| 843 | pars[a].from = a * slice; |
| 844 | pars[a].upto = ((a + 1) * slice > cutoff ? cutoff : (a + 1) * slice); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 845 | } else { |
| 846 | pars[a].from = merge_words + a * slice; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 847 | pars[a].upto = merge_words + ((a + 1) * slice > cutoff ? cutoff : (a + 1) * slice); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 848 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 849 | printf("From: %ld, Upto: %ld\n", pars[a].from, pars[a].upto); |
| 850 | pthread_create(&pt[a], NULL, _get_neighbours, (void *)&pars[a]); |
| 851 | } |
| 852 | if (M2) { |
| 853 | for (a = 0; a < syn_threads; a++) { |
| 854 | pars[a + para_threads].cutoff = cutoff; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 855 | pars[a + para_threads].target_sums = target_sums; |
| 856 | pars[a + para_threads].window_sums = window_sums; |
| 857 | pars[a + para_threads].wl = wl; |
| 858 | pars[a + para_threads].N = N; |
| 859 | pars[a + para_threads].threshold = MIN_RESP; |
| 860 | pars[a + para_threads].from = a; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 861 | pars[a + para_threads].upto = a + 1; |
| 862 | pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *)&pars[a + para_threads]); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | printf("Waiting for para threads to join\n"); |
| 866 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 867 | for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *)¶_nbs[a]); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 868 | printf("Para threads joint\n"); |
| 869 | fflush(stdout); |
| 870 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 871 | /* if(!syn_nbs[0]) */ |
| 872 | /* goto end; */ |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 873 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 874 | qsort(best, N * para_threads, sizeof(collocator), cmp_activation); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 875 | |
| 876 | long long chosen[MAX_NEIGHBOURS]; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 877 | printf("N: %d\n", N); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 878 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 879 | AV *array = newAV(); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 880 | int i, j; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 881 | int l1_words = 0, l2_words = 0; |
| 882 | |
| 883 | for (a = 0, i = 0; i < N && a < N * para_threads; a++) { |
| 884 | int filtered = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 885 | long long c = best[a].wordi; |
| 886 | if ((merge_words && dedupe && i > 1) || (!merge_words && dedupe && i > 0)) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 887 | for (j = 0; j < i && !filtered; j++) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 888 | if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) || |
| 889 | strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 890 | printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]); |
| 891 | filtered = 1; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 892 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 893 | if (filtered) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 894 | continue; |
| 895 | } |
| 896 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 897 | if (0 && merge_words > 0) { |
| 898 | if (c >= merge_words) { |
| 899 | if (l1_words > N / 2) |
| 900 | continue; |
| 901 | else |
| 902 | l1_words++; |
| 903 | } else { |
| 904 | if (l2_words > N / 2) |
| 905 | continue; |
| 906 | else |
| 907 | l2_words++; |
| 908 | } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 909 | } |
| 910 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 911 | // printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a); |
| 912 | // fflush(stdout); |
| 913 | HV *hash = newHV(); |
| 914 | SV *word = newSVpvf(&vocab[c * max_w], 0); |
| 915 | chosen[i] = c; |
| 916 | if (latin_enc == 0) SvUTF8_on(word); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 917 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 918 | hv_store(hash, "word", strlen("word"), word, 0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 919 | hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0); |
| 920 | hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0); |
| 921 | AV *vector = newAV(); |
| 922 | for (b = 0; b < size; b++) { |
| 923 | av_push(vector, newSVnv(M[b + best[a].wordi * size])); |
| 924 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 925 | hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV *)vector), 0); |
| 926 | av_push(array, newRV_noinc((SV *)hash)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 927 | i++; |
| 928 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 929 | hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV *)array), 0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 930 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 931 | for (b = 0; b < MAX_NEIGHBOURS; b++) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 932 | best[b].wordi = -1L; |
| 933 | best[b].activation = 0; |
| 934 | best[b].probability = 0; |
| 935 | best[b].position = 0; |
| 936 | best[b].activation_sum = 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 937 | memset(best[b].heat, 0, sizeof(float) * 16); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 938 | } |
| 939 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 940 | float total_activation = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 941 | |
| 942 | if (M2) { |
| 943 | printf("Waiting for syn threads to join\n"); |
| 944 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 945 | for (a = 0; a < syn_threads; a++) pthread_join(pt[a + para_threads], (void *)&syn_nbs[a]); |
| 946 | for (a = 0; a <= syn_threads; a++) { |
| 947 | if (a == window) continue; |
| 948 | total_activation += window_sums[a]; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 949 | printf("window pos: %ld, sum: %f\n", a, window_sums[a]); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 950 | } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 951 | printf("syn threads joint\n"); |
| 952 | fflush(stdout); |
| 953 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 954 | for (b = 0; b < syn_nbs[0]->length; b++) { |
| 955 | memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator)); |
| 956 | best[b].position = -1; // syn_nbs[0]->pos[b]; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 957 | best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi]; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 958 | best[b].max_activation = 0.0; |
| 959 | best[b].average = 0.0; |
| 960 | best[b].probability = 0.0; |
| 961 | best[b].cprobability = syn_nbs[0]->best[b].cprobability; |
| 962 | memset(best[b].heat, 0, sizeof(float) * 16); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 963 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 964 | |
| 965 | float best_window_sum[MAX_NEIGHBOURS]; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 966 | int found_index = 0, i = 0, w; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 967 | for (a = 0; a < syn_threads; a++) { |
| 968 | for (b = 0; b < syn_nbs[a]->length; b++) { |
| 969 | for (i = 0; i < found_index; i++) |
| 970 | if (best[i].wordi == syn_nbs[a]->best[b].wordi) |
| 971 | break; |
| 972 | if (i >= found_index) { |
| 973 | best[found_index].max_activation = 0.0; |
| 974 | best[found_index].average = 0.0; |
| 975 | best[found_index].probability = 0.0; |
| 976 | memset(best[found_index].heat, 0, sizeof(float) * 16); |
| 977 | best[found_index].cprobability = syn_nbs[a]->best[b].cprobability; |
| 978 | best[found_index].activation_sum = target_sums[syn_nbs[a]->best[b].wordi]; // syn_nbs[a]->best[b].activation_sum; |
| 979 | best[found_index++].wordi = syn_nbs[a]->best[b].wordi; |
| 980 | // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]); |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | sort_by = 0; // ALWAYS AUTO-FOCUS |
| 985 | if (sort_by != 1 && sort_by != 2) { // sort by auto focus mean |
| 986 | printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) - 1); |
| 987 | int wpos; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 988 | int bits_set = 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 989 | for (i = 0; i < found_index; i++) { |
| 990 | best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0; |
| 991 | for (w = 1; w < (1 << syn_threads); w++) { // loop through all possible windows |
| 992 | float word_window_sum = 0, word_window_average = 0, word_cprobability_sum = 0, word_activation_sum = 0, total_window_sum = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 993 | bits_set = 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 994 | for (a = 0; a < syn_threads; a++) { |
| 995 | if ((1 << a) & w) { |
| 996 | wpos = (a >= window ? a + 1 : a); |
| 997 | total_window_sum += window_sums[wpos]; |
| 998 | } |
| 999 | } |
| 1000 | // printf("%d window-sum %f\n", w, total_window_sum); |
| 1001 | for (a = 0; a < syn_threads; a++) { |
| 1002 | if ((1 << a) & w) { |
| 1003 | wpos = (a >= window ? a + 1 : a); |
| 1004 | bits_set++; |
| 1005 | for (b = 0; b < syn_nbs[a]->length; b++) |
| 1006 | if (best[i].wordi == syn_nbs[a]->best[b].wordi) { |
| 1007 | // float acti = syn_nbs[a]->best[b].activation / total_window_sum; |
| 1008 | // word_window_sum += syn_nbs[a]->dist[b] * syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b]; |
| 1009 | // word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b]; |
| 1010 | // 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 Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1011 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1012 | word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b]; |
| 1013 | // word_window_sum += acti - (word_window_sum * acti); syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b]; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1014 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1015 | word_window_average += syn_nbs[a]->best[b].activation; // - word_window_average * syn_nbs[a]->best[b].activation; // conormalied activation sum |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1016 | word_cprobability_sum += syn_nbs[a]->best[b].cprobability - word_cprobability_sum * syn_nbs[a]->best[b].cprobability; // conormalied column probability sum |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1017 | word_activation_sum += syn_nbs[a]->best[b].activation; |
| 1018 | if (syn_nbs[a]->best[b].activation > best[i].max_activation) |
| 1019 | best[i].max_activation = syn_nbs[a]->best[b].activation; |
| 1020 | if (syn_nbs[a]->best[b].activation > best[i].heat[wpos]) |
| 1021 | best[i].heat[wpos] = syn_nbs[a]->best[b].activation; |
| 1022 | } |
| 1023 | } |
| 1024 | } |
| 1025 | if (bits_set) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1026 | word_window_average /= bits_set; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1027 | // word_activation_sum /= bits_set; |
| 1028 | // word_window_sum /= bits_set; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1029 | } |
| 1030 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1031 | word_window_sum /= total_window_sum; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1032 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1033 | if (word_window_sum > best[i].probability) { |
| 1034 | // best[i].position = w; |
| 1035 | best[i].probability = word_window_sum; |
| 1036 | } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1037 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1038 | if (word_cprobability_sum > best[i].cprobability_sum) { |
| 1039 | best[i].position = w; |
| 1040 | best[i].cprobability_sum = word_cprobability_sum; |
| 1041 | } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1042 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1043 | best[i].average = word_window_average; |
| 1044 | // best[i].activation = word_activation_sum; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1045 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1046 | } |
| 1047 | qsort(best, found_index, sizeof(collocator), cmp_probability); |
| 1048 | // for(i=0; i < found_index; i++) { |
| 1049 | // printf("found: %s - sum: %f - window: %d\n", &vocab[best[i].wordi * max_w], best[i].activation, best[i].position); |
| 1050 | // } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1051 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1052 | } else if (sort_by == 1) { // responsiveness any window position |
| 1053 | int wpos; |
| 1054 | for (i = 0; i < found_index; i++) { |
| 1055 | float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0; |
| 1056 | for (a = 0; a < syn_threads; a++) { |
| 1057 | wpos = (a >= window ? a + 1 : a); |
| 1058 | for (b = 0; b < syn_nbs[a]->length; b++) |
| 1059 | if (best[i].wordi == syn_nbs[a]->best[b].wordi) { |
| 1060 | best[i].probability += syn_nbs[a]->best[b].probability; |
| 1061 | if (syn_nbs[a]->best[b].activation > 0.25) |
| 1062 | best[i].position |= 1 << wpos; |
| 1063 | if (syn_nbs[a]->best[b].activation > best[i].activation) { |
| 1064 | best[i].activation = syn_nbs[a]->best[b].activation; |
| 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | } |
| 1069 | qsort(best, found_index, sizeof(collocator), cmp_activation); |
| 1070 | } else if (sort_by == 2) { // single window position |
| 1071 | for (a = 1; a < syn_threads; a++) { |
| 1072 | for (b = 0; b < syn_nbs[a]->length; b++) { |
| 1073 | for (c = 0; c < MAX_NEIGHBOURS; c++) { |
| 1074 | if (syn_nbs[a]->best[b].activation > best[c].activation) { |
| 1075 | for (d = MAX_NEIGHBOURS - 1; d > c; d--) { |
| 1076 | memmove(best + d, best + d - 1, sizeof(collocator)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1077 | } |
| 1078 | memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator)); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1079 | best[c].position = 1 << (-syn_nbs[a]->best[b].position + window - (syn_nbs[a]->best[b].position < 0 ? 1 : 0)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1080 | break; |
| 1081 | } |
| 1082 | } |
| 1083 | } |
| 1084 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1085 | } else { // sort by mean p |
| 1086 | for (a = 1; a < syn_threads; a++) { |
| 1087 | for (b = 0; b < syn_nbs[a]->length; b++) { |
| 1088 | for (c = 0; c < MAX_NEIGHBOURS; c++) { |
| 1089 | if (target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) { |
| 1090 | for (d = MAX_NEIGHBOURS - 1; d > c; d--) { |
| 1091 | memmove(best + d, best + d - 1, sizeof(collocator)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1092 | } |
| 1093 | memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator)); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1094 | best[c].position = (1 << 2 * window) - 1; // syn_nbs[a]->pos[b]; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1095 | best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi]; |
| 1096 | break; |
| 1097 | } |
| 1098 | } |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | array = newAV(); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1103 | for (a = 0, i = 0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1104 | long long c = best[a].wordi; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1105 | /* |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1106 | if (dedupe) { |
| 1107 | int filtered=0; |
| 1108 | for (j=0; j<i; j++) |
| 1109 | if (strcasestr(&vocab[c * max_w], chosen[j]) || |
| 1110 | strcasestr(chosen[j], &vocab[c * max_w])) { |
| 1111 | printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]); |
| 1112 | filtered = 1; |
| 1113 | } |
| 1114 | if(filtered) |
| 1115 | continue; |
| 1116 | } |
| 1117 | */ |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1118 | chosen[i++] = c; |
| 1119 | HV *hash = newHV(); |
| 1120 | SV *word = newSVpvf(&vocab[best[a].wordi * max_w], 0); |
| 1121 | AV *heat = newAV(); |
| 1122 | if (latin_enc == 0) SvUTF8_on(word); |
| 1123 | hv_store(hash, "word", strlen("word"), word, 0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1124 | hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0); |
| 1125 | hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0); |
| 1126 | hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0); |
| 1127 | hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1128 | hv_store(hash, "max", strlen("max"), newSVnv(best[a].max_activation), 0); // newSVnv(target_sums[best[a].wordi]), 0); |
| 1129 | hv_store(hash, "overall", strlen("overall"), newSVnv(best[a].activation_sum / total_activation), 0); // newSVnv(target_sums[best[a].wordi]), 0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1130 | hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1131 | best[a].heat[5] = 0; |
| 1132 | for (i = 10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i])); |
| 1133 | hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV *)heat), 0); |
| 1134 | av_push(array, newRV_noinc((SV *)hash)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1135 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1136 | hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV *)array), 0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1137 | } |
| 1138 | end: |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1139 | free(best); |
| 1140 | return newRV_noinc((SV *)result); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | int dump_vecs(char *fname) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1144 | long i, j; |
| 1145 | FILE *f; |
| 1146 | /* if(words>100000) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1147 | words=100000; |
| 1148 | */ |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1149 | if ((f = fopen(fname, "w")) == NULL) { |
| 1150 | fprintf(stderr, "cannot open %s for writing\n", fname); |
| 1151 | return (-1); |
| 1152 | } |
| 1153 | fprintf(f, "%lld %lld\n", words, size); |
| 1154 | for (i = 0; i < words; i++) { |
| 1155 | fprintf(f, "%s ", &vocab[i * max_w]); |
| 1156 | for (j = 0; j < size - 1; j++) |
| 1157 | fprintf(f, "%f ", M[i * size + j]); |
| 1158 | fprintf(f, "%f\n", M[i * size + j]); |
| 1159 | } |
| 1160 | fclose(f); |
| 1161 | return (0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | int dump_for_numpy(char *fname) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1165 | long i, j; |
| 1166 | FILE *f; |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 1167 | int max = words; // 300000; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1168 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1169 | if ((f = fopen(fname, "w")) == NULL) { |
| 1170 | fprintf(stderr, "cannot open %s for writing\n", fname); |
| 1171 | return (-1); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1172 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1173 | for (i = 0; i < max; i++) { |
| 1174 | for (j = 0; j < size - 1; j++) |
| 1175 | fprintf(f, "%f\t", M[i * size + j]); |
| 1176 | fprintf(f, "%f\n", M[i * size + j]); |
| 1177 | printf("%s\r\n", &vocab[i * max_w]); |
| 1178 | } |
| 1179 | if (merged_end > 0) { |
| 1180 | for (i = 0; i < max; i++) { |
| 1181 | for (j = 0; j < size - 1; j++) |
| 1182 | fprintf(f, "%f\t", M[(merged_end + i) * size + j]); |
| 1183 | fprintf(f, "%f\n", M[(merged_end + i) * size + j]); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1184 | printf("_%s\r\n", &vocab[i * max_w]); |
| 1185 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1186 | } |
| 1187 | fclose(f); |
| 1188 | return (0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1189 | } |
Marc Kupietz | 043db15 | 2023-11-05 17:47:53 +0100 | [diff] [blame] | 1190 | |
| 1191 | unsigned long getVocabSize() { |
| 1192 | return (unsigned long) words; |
| 1193 | } |