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 | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 8 | |
| 9 | #define max_size 2000 |
| 10 | #define max_w 50 |
| 11 | #define MAX_NEIGHBOURS 1000 |
| 12 | #define MAX_WORDS -1 |
| 13 | #define MAX_THREADS 100 |
| 14 | #define MAX_CC 50 |
| 15 | #define EXP_TABLE_SIZE 1000 |
| 16 | #define MAX_EXP 6 |
| 17 | #define MIN_RESP 0.50 |
| 18 | |
| 19 | //the thread function |
| 20 | void *connection_handler(void *); |
| 21 | |
| 22 | typedef struct { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 23 | long long wordi; |
| 24 | long position; |
| 25 | float activation; |
| 26 | float average; |
| 27 | float cprobability; // column wise probability |
| 28 | float cprobability_sum; |
| 29 | float probability; |
| 30 | float activation_sum; |
| 31 | float max_activation; |
| 32 | float heat[16]; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 33 | } collocator; |
| 34 | |
| 35 | typedef struct { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 36 | collocator *best; |
| 37 | int length; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 38 | } knn; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 39 | |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 40 | typedef struct { |
| 41 | long long wordi[MAX_NEIGHBOURS]; |
| 42 | char sep[MAX_NEIGHBOURS]; |
| 43 | int length; |
| 44 | } wordlist; |
| 45 | |
| 46 | typedef struct { |
| 47 | long cutoff; |
| 48 | wordlist *wl; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 49 | char *token; |
| 50 | int N; |
| 51 | long from; |
| 52 | unsigned long upto; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 53 | collocator *best; |
| 54 | float *target_sums; |
| 55 | float *window_sums; |
| 56 | float threshold; |
| 57 | } knnpars; |
| 58 | |
| 59 | typedef struct { |
| 60 | uint32_t index; |
| 61 | float value; |
| 62 | } sparse_t; |
| 63 | |
| 64 | typedef struct { |
| 65 | uint32_t len; |
| 66 | sparse_t nbr[100]; |
| 67 | } profile_t; |
| 68 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 69 | float *M, *M2 = 0L, *syn1neg_window, *expTable; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 70 | float *window_sums; |
| 71 | char *vocab; |
| 72 | char *garbage = NULL; |
| 73 | COLLOCATORDB *cdb = NULL; |
| 74 | profile_t *sprofiles = NULL; |
| 75 | size_t sprofiles_qty = 0; |
| 76 | |
| 77 | long long words, size, merged_end; |
| 78 | long long merge_words = 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 79 | int num_threads = 20; |
| 80 | int latin_enc = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 81 | int window; |
| 82 | |
| 83 | /* load collocation profiles if file exists */ |
| 84 | int load_sprofiles(char *vecsname) { |
| 85 | char *basename = strdup(vecsname); |
| 86 | char *pos = strstr(basename, ".vecs"); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 87 | if (pos) |
| 88 | *pos = 0; |
| 89 | |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 90 | char binsprofiles_fname[256]; |
| 91 | strcpy(binsprofiles_fname, basename); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 92 | strcat(binsprofiles_fname, ".sprofiles.bin"); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 93 | FILE *fp = fopen(binsprofiles_fname, "rb"); |
| 94 | if (fp == NULL) { |
| 95 | printf("Collocation profiles %s not found. No problem.\n", binsprofiles_fname); |
| 96 | return 0; |
| 97 | } |
| 98 | fseek(fp, 0L, SEEK_END); |
| 99 | size_t sz = ftell(fp); |
| 100 | fclose(fp); |
| 101 | |
| 102 | int fd = open(binsprofiles_fname, O_RDONLY); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 103 | sprofiles = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 104 | if (sprofiles == MAP_FAILED) { |
| 105 | close(fd); |
| 106 | fprintf(stderr, "Cannot mmap %s\n", binsprofiles_fname); |
| 107 | sprofiles = NULL; |
| 108 | return 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 109 | } else { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 110 | sprofiles_qty = sz / sizeof(profile_t); |
| 111 | fprintf(stderr, "Successfully mmaped %s containing similar profiles for %ld word forms.\n", binsprofiles_fname, sprofiles_qty); |
| 112 | } |
| 113 | return 1; |
| 114 | } |
| 115 | |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 116 | char *removeExtension(char* myStr) { |
| 117 | char *retStr; |
| 118 | char *lastExt; |
| 119 | if (myStr == NULL) return NULL; |
| 120 | if ((retStr = malloc (strlen (myStr) + 1)) == NULL) return NULL; |
| 121 | strcpy (retStr, myStr); |
| 122 | lastExt = strrchr (retStr, '.'); |
| 123 | if (lastExt != NULL) |
| 124 | *lastExt = '\0'; |
| 125 | return retStr; |
| 126 | } |
| 127 | |
Marc Kupietz | 0efe49b | 2020-04-06 18:30:22 +0200 | [diff] [blame] | 128 | 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] | 129 | FILE *f, *binvecs, *binwords; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 130 | int binwords_fd, binvecs_fd, net_fd, i; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 131 | long long a, b; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 132 | float len; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 133 | double val; |
| 134 | |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 135 | char binvecs_fname[1024], binwords_fname[1024]; |
| 136 | |
| 137 | if (strstr(file_name, ".txt")) { |
| 138 | strcpy(binwords_fname, removeExtension(file_name)); |
| 139 | } else { |
| 140 | strcpy(binwords_fname, file_name); |
| 141 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 142 | strcat(binwords_fname, ".words"); |
| 143 | strcpy(binvecs_fname, file_name); |
| 144 | strcat(binvecs_fname, ".vecs"); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 145 | |
| 146 | latin_enc = latin; |
| 147 | f = fopen(file_name, "rb"); |
| 148 | if (f == NULL) { |
| 149 | printf("Input file %s not found\n", file_name); |
| 150 | return -1; |
| 151 | } |
| 152 | fscanf(f, "%lld", &words); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 153 | if (MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 154 | fscanf(f, "%lld", &size); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 155 | 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] | 156 | printf("Converting %s to memory mappable structures\n", file_name); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 157 | vocab = (char *)malloc((long long)words * max_w * sizeof(char)); |
| 158 | M = (float *)malloc((long long)words * (long long)size * sizeof(float)); |
| 159 | if (M == NULL) { |
| 160 | printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size); |
| 161 | return -1; |
| 162 | } |
| 163 | if (strstr(file_name, ".txt")) { |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 164 | 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] | 165 | for (b = 0; b < words; b++) { |
| 166 | a = 0; |
| 167 | while (1) { |
| 168 | vocab[b * max_w + a] = fgetc(f); |
| 169 | if (feof(f) || (vocab[b * max_w + a] == ' ')) break; |
| 170 | if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++; |
| 171 | } |
| 172 | vocab[b * max_w + a] = 0; |
| 173 | len = 0; |
| 174 | for (a = 0; a < size; a++) { |
| 175 | fscanf(f, "%lf", &val); |
| 176 | M[a + b * size] = val; |
| 177 | len += val * val; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 178 | } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 179 | len = sqrt(len); |
| 180 | for (a = 0; a < size; a++) M[a + b * size] /= len; |
| 181 | } |
| 182 | } else { |
| 183 | for (b = 0; b < words; b++) { |
| 184 | a = 0; |
| 185 | while (1) { |
| 186 | vocab[b * max_w + a] = fgetc(f); |
| 187 | if (feof(f) || (vocab[b * max_w + a] == ' ')) break; |
| 188 | if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++; |
| 189 | } |
| 190 | vocab[b * max_w + a] = 0; |
| 191 | fread(&M[b * size], sizeof(float), size, f); |
| 192 | len = 0; |
| 193 | for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size]; |
| 194 | len = sqrt(len); |
| 195 | for (a = 0; a < size; a++) M[a + b * size] /= len; |
| 196 | } |
| 197 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 198 | if ((binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) { |
| 199 | fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs); |
| 200 | fclose(binvecs); |
| 201 | fwrite(vocab, sizeof(char), (long long)words * max_w, binwords); |
| 202 | fclose(binwords); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 203 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 204 | } |
| 205 | if ((binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) { |
| 206 | M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0); |
| 207 | vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0); |
| 208 | if (M == MAP_FAILED || vocab == MAP_FAILED) { |
| 209 | close(binvecs_fd); |
| 210 | close(binwords_fd); |
| 211 | fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname); |
| 212 | exit(-1); |
| 213 | } |
| 214 | } else { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 215 | fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname); |
| 216 | exit(-1); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 217 | } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 218 | fclose(f); |
| 219 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 220 | if (net_name && strlen(net_name) > 0) { |
| 221 | if ((net_fd = open(net_name, O_RDONLY)) >= 0) { |
| 222 | 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] | 223 | // lseek(net_fd, sizeof(float) * words * size, SEEK_SET); |
| 224 | // munmap(M, sizeof(float) * words * size); |
| 225 | M2 = mmap(0, sizeof(float) * words * size + sizeof(float) * 2 * window * size * words, PROT_READ, MAP_SHARED, net_fd, 0); |
| 226 | if (M2 == MAP_FAILED) { |
| 227 | close(net_fd); |
| 228 | fprintf(stderr, "Cannot mmap %s\n", net_name); |
| 229 | exit(-1); |
| 230 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 231 | syn1neg_window = M2 + words * size; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 232 | } else { |
| 233 | fprintf(stderr, "Cannot open %s\n", net_name); |
| 234 | exit(-1); |
| 235 | } |
| 236 | fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window); |
| 237 | |
Marc Kupietz | 0efe49b | 2020-04-06 18:30:22 +0200 | [diff] [blame] | 238 | if (do_open_cdb) { |
| 239 | char collocatordb_name[2048]; |
| 240 | strcpy(collocatordb_name, net_name); |
| 241 | char *ext = rindex(collocatordb_name, '.'); |
| 242 | if (ext) { |
| 243 | strcpy(ext, ".rocksdb"); |
| 244 | if (access(collocatordb_name, R_OK) == 0) { |
| 245 | *ext = 0; |
| 246 | fprintf(stderr, "Opening collocator DB %s\n", collocatordb_name); |
| 247 | cdb = open_collocatordb(collocatordb_name); |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 248 | } else { |
| 249 | fprintf(stderr, "Cannot open collocator DB %s\n", collocatordb_name); |
Marc Kupietz | 0efe49b | 2020-04-06 18:30:22 +0200 | [diff] [blame] | 250 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 254 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 255 | expTable = (float *)malloc((EXP_TABLE_SIZE + 1) * sizeof(float)); |
| 256 | for (i = 0; i < EXP_TABLE_SIZE; i++) { |
| 257 | expTable[i] = exp((i / (float)EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table |
| 258 | expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1) |
| 259 | } |
| 260 | window_sums = malloc(sizeof(float) * (window + 1) * 2); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 265 | long mergeVectors(char *file_name) { |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 266 | FILE *f; |
| 267 | int binwords_fd, binvecs_fd; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 268 | float *merge_vecs; |
| 269 | char *merge_vocab; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 270 | /* long long merge_words, merge_size; */ |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 271 | long long merge_size; |
| 272 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 273 | char binvecs_fname[256], binwords_fname[256]; |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 274 | |
| 275 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 276 | strcpy(binwords_fname, file_name); |
| 277 | strcat(binwords_fname, ".words"); |
| 278 | strcpy(binvecs_fname, file_name); |
| 279 | strcat(binvecs_fname, ".vecs"); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 280 | |
| 281 | f = fopen(file_name, "rb"); |
| 282 | if (f == NULL) { |
| 283 | printf("Input file %s not found\n", file_name); |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 284 | exit(-1); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 285 | } |
| 286 | fscanf(f, "%lld", &merge_words); |
| 287 | fscanf(f, "%lld", &merge_size); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 288 | if (merge_size != size) { |
| 289 | fprintf(stderr, "vectors must have the same length\n"); |
| 290 | exit(-1); |
| 291 | } |
| 292 | if ((binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) { |
| 293 | merge_vecs = malloc(sizeof(float) * (words + merge_words) * size); |
| 294 | merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 295 | if (merge_vecs == NULL || merge_vocab == NULL) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 296 | close(binvecs_fd); |
| 297 | close(binwords_fd); |
| 298 | fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname); |
| 299 | exit(-1); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 300 | } |
| 301 | read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float)); |
| 302 | read(binwords_fd, merge_vocab, merge_words * max_w); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 303 | } else { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 304 | fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname); |
| 305 | exit(-1); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 306 | } |
| 307 | printf("Successfully reallocated memory\nMerging...\n"); |
| 308 | fflush(stdout); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 309 | memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float)); |
| 310 | memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w); |
| 311 | munmap(M, words * size * sizeof(float)); |
| 312 | munmap(vocab, words * max_w); |
| 313 | M = merge_vecs; |
| 314 | vocab = merge_vocab; |
| 315 | merged_end = merge_words; |
| 316 | words += merge_words; |
| 317 | fclose(f); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 318 | printf("merged_end: %lld, words: %lld\n", merged_end, words); |
| 319 | //printBiggestMergedDifferences(); |
| 320 | return ((long)merged_end); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void filter_garbage() { |
| 324 | long i; |
| 325 | unsigned char *w, previous, c; |
| 326 | garbage = malloc(words); |
| 327 | memset(garbage, 0, words); |
| 328 | for (i = 0; i < words; i++) { |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 329 | w = (unsigned char *) vocab + i * max_w; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 330 | previous = 0; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 331 | if (strncmp("quot", (const char *)w, 4) == 0) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 332 | garbage[i] = 1; |
| 333 | // printf("Gargabe: %s\n", vocab + i * max_w); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 334 | } else { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 335 | while ((c = *w++) && !garbage[i]) { |
| 336 | if (((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) || |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 337 | (previous == '-' && (c & 32)) || |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 338 | (previous == 0xc2 && (c == 0xa4 || c == 0xb6)) || |
| 339 | (previous == 'q' && c == 'u' && *(w) == 'o' && *(w + 1) == 't') || /* quot */ |
| 340 | c == '<') { |
| 341 | garbage[i] = 1; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 342 | continue; |
| 343 | } |
| 344 | previous = c; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | return; |
| 349 | } |
| 350 | |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 351 | knn *simpleGetCollocators(int word, int number, long cutoff, int *result) { |
| 352 | knnpars *pars = calloc(sizeof(knnpars), 1); |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 353 | float *target_sums = NULL; |
| 354 | float *my_window_sums = malloc(sizeof(float) * (window + 1) * 2); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 355 | pars->cutoff = (cutoff ? cutoff : 300000); |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 356 | long a; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 357 | for (a = 0; a < cutoff; a++) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 358 | target_sums[a] = 0; |
| 359 | pars->target_sums = target_sums; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 360 | pars->window_sums = my_window_sums; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 361 | pars->N = (number ? number : 20); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 362 | pars->from = 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 363 | pars->upto = window * 2 - 1; |
| 364 | knn *syn_nbs = NULL; // = (knn*) getCollocators(pars); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 365 | free(pars); |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 366 | free(my_window_sums); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 367 | free(target_sums); |
| 368 | return syn_nbs; |
| 369 | } |
| 370 | |
| 371 | void *getCollocators(void *args) { |
| 372 | knnpars *pars = args; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 373 | int N = pars->N; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 374 | |
| 375 | int cc = pars->wl->wordi[0]; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 376 | knn *nbs = NULL; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 377 | long window_layer_size = size * window * 2; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 378 | 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] | 379 | float f, max_f, maxmax_f; |
| 380 | float *target_sums = NULL, worstbest, wpos_sum; |
| 381 | collocator *best; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 382 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 383 | if (M2 == NULL || cc == -1) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 384 | return NULL; |
| 385 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 386 | a = posix_memalign((void **)&target_sums, 128, pars->cutoff * sizeof(float)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 387 | memset(target_sums, 0, pars->cutoff * sizeof(float)); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 388 | best = malloc((N > 200 ? N : 200) * sizeof(collocator)); |
| 389 | memset(best, 0, (N > 200 ? N : 200) * sizeof(collocator)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 390 | worstbest = pars->threshold; |
| 391 | |
| 392 | for (b = 0; b < pars->cutoff; b++) |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 393 | target_sums[b] = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 394 | for (b = 0; b < N; b++) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 395 | best[b].wordi = -1; |
| 396 | best[b].probability = 1; |
| 397 | best[b].activation = worstbest; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | d = cc; |
| 401 | maxmax_f = -1; |
| 402 | maxmax_target = 0; |
| 403 | |
| 404 | for (a = pars->from; a < pars->upto; a++) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 405 | if (a >= window) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 406 | a++; |
| 407 | wpos_sum = 0; |
| 408 | printf("window pos: %ld\n", a); |
| 409 | if (a != window) { |
| 410 | max_f = -1; |
| 411 | window_offset = a * size; |
| 412 | if (a > window) |
| 413 | window_offset -= size; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 414 | for (target = 0; target < pars->cutoff; target++) { |
| 415 | if (garbage && garbage[target]) continue; |
| 416 | if (target == d) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 417 | continue; |
| 418 | f = 0; |
| 419 | for (c = 0; c < size; c++) |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 420 | 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] | 421 | if (f < -MAX_EXP) |
| 422 | continue; |
| 423 | else if (f > MAX_EXP) |
| 424 | continue; |
| 425 | else |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 426 | f = expTable[(int)((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))]; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 427 | wpos_sum += f; |
| 428 | |
| 429 | target_sums[target] += f; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 430 | if (f > worstbest) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 431 | for (b = 0; b < N; b++) { |
| 432 | if (f > best[b].activation) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 433 | memmove(best + b + 1, best + b, (N - b - 1) * sizeof(collocator)); |
| 434 | best[b].activation = f; |
| 435 | best[b].wordi = target; |
| 436 | best[b].position = window - a; |
| 437 | break; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 438 | } |
| 439 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 440 | if (b == N - 1) |
| 441 | worstbest = best[N - 1].activation; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 442 | } |
| 443 | } |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 444 | printf("%ld %.2f\n", max_target, max_f); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 445 | printf("%s (%.2f) ", &vocab[max_target * max_w], max_f); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 446 | if (max_f > maxmax_f) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 447 | maxmax_f = max_f; |
| 448 | maxmax_target = max_target; |
| 449 | } |
| 450 | for (b = 0; b < N; b++) |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 451 | if (best[b].position == window - a) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 452 | best[b].cprobability = best[b].activation / wpos_sum; |
| 453 | } else { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 454 | printf("\x1b[1m%s\x1b[0m ", &vocab[d * max_w]); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 455 | } |
| 456 | pars->window_sums[a] = wpos_sum; |
| 457 | } |
| 458 | for (b = 0; b < pars->cutoff; b++) |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 459 | 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] | 460 | |
| 461 | free(target_sums); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 462 | for (b = 0; b < N && best[b].wordi >= 0; b++) |
| 463 | ; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 464 | // THIS LOOP IS NEEDED (b...) |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 465 | // printf("%d: best syn: %s %.2f %.5f\n", b, &vocab[best[b].wordi*max_w], best[b].activation, best[b].probability); |
| 466 | // printf("\n"); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 467 | nbs = malloc(sizeof(knn)); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 468 | nbs->best = best; |
| 469 | nbs->length = b - 1; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 470 | pthread_exit(nbs); |
| 471 | } |
| 472 | |
Marc Kupietz | 0efe49b | 2020-04-06 18:30:22 +0200 | [diff] [blame] | 473 | float getOutputWeight(int hidden, long target, int window_position) { |
| 474 | const long window_layer_size = size * window * 2; |
| 475 | int a; |
| 476 | |
| 477 | if (window_position == 0 || window_position > window || window_position < -window) { |
| 478 | fprintf(stderr, "window_position: %d - assert: -%d <= window_position <= %d && window_position != 0 failed.\n", window_position, window, window); |
| 479 | exit(-1); |
| 480 | } |
| 481 | |
| 482 | if (hidden >= size) { |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 483 | fprintf(stderr, "hidden: %d - assert: hidden < %lld failed.\n", hidden, size); |
Marc Kupietz | 0efe49b | 2020-04-06 18:30:22 +0200 | [diff] [blame] | 484 | exit(-1); |
| 485 | } |
| 486 | |
| 487 | if (target >= words) { |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 488 | fprintf(stderr, "target: %ld - assert: target < %lld failed.\n", target, words); |
Marc Kupietz | 0efe49b | 2020-04-06 18:30:22 +0200 | [diff] [blame] | 489 | exit(-1); |
| 490 | } |
| 491 | |
| 492 | a = window_position + window; |
| 493 | if (a > window) { |
| 494 | --a; |
| 495 | } |
| 496 | long window_offset = a * size; |
| 497 | return syn1neg_window[target * window_layer_size + window_offset + hidden]; |
| 498 | } |
| 499 | |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 500 | AV *getVecs(AV *array) { |
| 501 | int i, b; |
| 502 | AV *result = newAV(); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 503 | for (i = 0; i <= av_len(array); i++) { |
| 504 | SV **elem = av_fetch(array, i, 0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 505 | if (elem != NULL) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 506 | long j = (long)SvNV(*elem); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 507 | AV *vector = newAV(); |
| 508 | for (b = 0; b < size; b++) { |
| 509 | av_push(vector, newSVnv(M[b + j * size])); |
| 510 | } |
Marc Kupietz | bdd779a | 2024-08-05 10:02:29 +0200 | [diff] [blame] | 511 | av_push(result, newRV_noinc((SV *)vector)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | return result; |
| 515 | } |
| 516 | |
| 517 | char *getSimilarProfiles(long node) { |
| 518 | int i; |
| 519 | char buffer[120000]; |
| 520 | char pair_buffer[2048]; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 521 | buffer[0] = '['; |
| 522 | buffer[1] = 0; |
| 523 | if (node >= sprofiles_qty) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 524 | printf("Not available in precomputed profile\n"); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 525 | return (strdup("[{\"w\":\"not available\", \"v\":0}]\n")); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | printf("******* %s ******\n", &vocab[max_w * node]); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 529 | |
| 530 | for (i = 0; i < 100 && i < sprofiles[node].len; i++) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 531 | sprintf(pair_buffer, "{\"w\":\"%s\", \"v\":%f},", &vocab[max_w * (sprofiles[node].nbr[i].index)], sprofiles[node].nbr[i].value); |
| 532 | strcat(buffer, pair_buffer); |
| 533 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 534 | buffer[strlen(buffer) - 1] = ']'; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 535 | strcat(buffer, "\n"); |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 536 | printf("%s", buffer); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 537 | return (strdup(buffer)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 538 | } |
| 539 | |
Marc Kupietz | f608001 | 2021-03-12 09:14:42 +0100 | [diff] [blame] | 540 | char *getCollocationScores(long node, long collocate) { |
| 541 | char *res = (cdb ? strdup(get_collocation_scores_as_json(cdb, node, collocate)) : "[]"); |
| 542 | return res; |
| 543 | } |
| 544 | |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 545 | char *getClassicCollocators(long node) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 546 | char *res = (cdb ? strdup(get_collocators_as_json(cdb, node)) : "[]"); |
| 547 | return res; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | wordlist *getTargetWords(char *st1, int search_backw) { |
| 551 | wordlist *wl = malloc(sizeof(wordlist)); |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 552 | char st[100][max_size]; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 553 | long a, b = 0, c = 0, cn = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 554 | |
| 555 | while (1) { |
| 556 | st[cn][b] = st1[c]; |
| 557 | b++; |
| 558 | c++; |
| 559 | st[cn][b] = 0; |
| 560 | if (st1[c] == 0) break; |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 561 | if (st1[c] == ' ' /*|| st1[c] == '-'*/) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 562 | b = 0; |
| 563 | c++; |
| 564 | } |
| 565 | } |
| 566 | cn++; |
| 567 | for (a = 0; a < cn; a++) { |
| 568 | if (search_backw) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 569 | for (b = words - 1; b >= (merge_words ? merge_words : 0) && strcmp(&vocab[b * max_w], st[a]) != 0; b--) |
| 570 | ; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 571 | } else { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 572 | for (b = 0; b < (merge_words ? merge_words : words) && strcmp(&vocab[b * max_w], st[a]) != 0; b++) |
| 573 | ; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 574 | } |
| 575 | if (b == words) b = -1; |
| 576 | wl->wordi[a] = b; |
| 577 | if (b == -1) { |
| 578 | fprintf(stderr, "Out of dictionary word!\n"); |
| 579 | cn--; |
| 580 | } else { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 581 | 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] | 582 | } |
| 583 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 584 | wl->length = cn; |
| 585 | return (wl); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 586 | } |
| 587 | |
Marc Kupietz | cb43e49 | 2019-12-03 10:07:53 +0100 | [diff] [blame] | 588 | long getWordNumber(char *word) { |
| 589 | wordlist *wl = getTargetWords(word, 0); |
| 590 | if(wl->length > 0) |
| 591 | return(wl->wordi[0]); |
| 592 | return(0); |
| 593 | } |
| 594 | |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 595 | float get_distance(long b, long c) { |
| 596 | long a; |
| 597 | float dist = 0; |
| 598 | for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + b * size]; |
| 599 | return dist; |
| 600 | } |
| 601 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 602 | char *getBiggestMergedDifferences() { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 603 | static char *result = NULL; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 604 | float dist; |
| 605 | long long a, c; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 606 | int N = 1000; |
| 607 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 608 | if (merged_end == 0) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 609 | result = "[]"; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 610 | |
| 611 | if (result != NULL) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 612 | return result; |
| 613 | |
| 614 | printf("Looking for biggest distances between main and merged vectors ...\n"); |
| 615 | collocator *best; |
| 616 | best = malloc(N * sizeof(collocator)); |
| 617 | memset(best, 0, N * sizeof(collocator)); |
| 618 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 619 | float worstbest = 1000000; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 620 | |
| 621 | for (a = 0; a < N; a++) best[a].activation = worstbest; |
| 622 | |
| 623 | for (c = 0; c < 500000; c++) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 624 | if (garbage && garbage[c]) continue; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 625 | dist = 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 626 | for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + (c + merged_end) * size]; |
| 627 | if (dist < worstbest) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 628 | for (a = 0; a < N; a++) { |
| 629 | if (dist < best[a].activation) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 630 | memmove(best + a + 1, best + a, (N - a - 1) * sizeof(collocator)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 631 | best[a].activation = dist; |
| 632 | best[a].wordi = c; |
| 633 | break; |
| 634 | } |
| 635 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 636 | worstbest = best[N - 1].activation; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 640 | result = malloc(N * max_w); |
Marc Kupietz | bdd779a | 2024-08-05 10:02:29 +0200 | [diff] [blame] | 641 | char *p = (char *) result; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 642 | *p++ = '['; |
| 643 | *p = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 644 | for (a = 0; a < N; a++) { |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 645 | 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] | 646 | } |
| 647 | *--p = ']'; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 648 | return (result); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 649 | } |
| 650 | |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 651 | float cos_similarity(long b, long c) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 652 | float dist = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 653 | long a; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 654 | 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] | 655 | return dist; |
| 656 | } |
| 657 | |
| 658 | char *cos_similarity_as_json(char *w1, char *w2) { |
| 659 | wordlist *a, *b; |
| 660 | float res; |
| 661 | a = getTargetWords(w1, 0); |
| 662 | b = getTargetWords(w2, 0); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 663 | if (a == NULL || b == NULL || a->length != 1 || b->length != 1) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 664 | res = -1; |
| 665 | else |
| 666 | res = cos_similarity(a->wordi[0], b->wordi[0]); |
| 667 | fprintf(stderr, "a: %lld b: %lld res:%f\n", a->wordi[0], b->wordi[0], res); |
| 668 | char *json = malloc(16); |
| 669 | sprintf(json, "%.5f", res); |
| 670 | return json; |
| 671 | } |
| 672 | |
| 673 | void *_get_neighbours(void *arg) { |
| 674 | knnpars *pars = arg; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 675 | int N = pars->N; |
| 676 | long from = pars->from; |
| 677 | unsigned long upto = pars->upto; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 678 | char *sep; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 679 | float dist, len, vec[max_size]; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 680 | long long a, b, c, cn, *bi; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 681 | knn *nbs = NULL; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 682 | wordlist *wl = pars->wl; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 683 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 684 | collocator *best = pars->best; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 685 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 686 | float worstbest = -1; |
| 687 | |
| 688 | for (a = 0; a < N; a++) best[a].activation = 0; |
| 689 | a = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 690 | bi = wl->wordi; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 691 | cn = wl->length; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 692 | sep = wl->sep; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 693 | b = bi[0]; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 694 | if (b == -1) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 695 | goto end; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 696 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 697 | for (a = 0; a < size; a++) vec[a] = 0; |
| 698 | for (b = 0; b < cn; b++) { |
| 699 | if (bi[b] == -1) continue; |
| 700 | if (b > 0 && sep[b - 1] == '-') |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 701 | for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size]; |
| 702 | else |
| 703 | 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] | 704 | } |
| 705 | len = 0; |
| 706 | for (a = 0; a < size; a++) len += vec[a] * vec[a]; |
| 707 | len = sqrt(len); |
| 708 | for (a = 0; a < size; a++) vec[a] /= len; |
| 709 | for (a = 0; a < N; a++) best[a].activation = -1; |
| 710 | for (c = from; c < upto; c++) { |
| 711 | if (garbage && garbage[c]) continue; |
| 712 | a = 0; |
| 713 | // do not skip taget word |
| 714 | // for (b = 0; b < cn; b++) if (bi[b] == c) a = 1; |
| 715 | // if (a == 1) continue; |
| 716 | dist = 0; |
| 717 | for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size]; |
| 718 | if (dist > worstbest) { |
| 719 | for (a = 0; a < N; a++) { |
| 720 | if (dist > best[a].activation) { |
| 721 | memmove(best + a + 1, best + a, (N - a - 1) * sizeof(collocator)); |
| 722 | best[a].activation = dist; |
| 723 | best[a].wordi = c; |
| 724 | break; |
| 725 | } |
| 726 | } |
| 727 | worstbest = best[N - 1].activation; |
| 728 | } |
| 729 | } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 730 | |
| 731 | end: |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 732 | pthread_exit(nbs); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 733 | } |
| 734 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 735 | int cmp_activation(const void *a, const void *b) { |
| 736 | float fb = ((collocator *)a)->activation; |
| 737 | float fa = ((collocator *)b)->activation; |
| 738 | return (fa > fb) - (fa < fb); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 739 | } |
| 740 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 741 | int cmp_probability(const void *a, const void *b) { |
| 742 | float fb = ((collocator *)a)->probability; |
| 743 | float fa = ((collocator *)b)->probability; |
| 744 | return (fa > fb) - (fa < fb); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 745 | } |
| 746 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 747 | char *getPosWiseW2VCollocatorsAsTsv(char *word, long maxPerPos, long cutoff, float threshold) { |
| 748 | HV *result = newHV(); |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 749 | float *target_sums = NULL; |
| 750 | long a, b; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 751 | knn *para_nbs[MAX_THREADS]; |
| 752 | knn *syn_nbs[MAX_THREADS]; |
| 753 | knnpars pars[MAX_THREADS]; |
| 754 | pthread_t *pt = (pthread_t *)malloc((num_threads + 1) * sizeof(pthread_t)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 755 | wordlist *wl; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 756 | int syn_threads = (M2 ? window * 2 : 0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 757 | int search_backw = 0; |
| 758 | collocator *best = NULL; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 759 | posix_memalign((void **)&best, 128, 10 * (maxPerPos >= 200 ? maxPerPos : 200) * sizeof(collocator)); |
| 760 | memset(best, 0, (maxPerPos >= 200 ? maxPerPos : 200) * sizeof(collocator)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 761 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 762 | if (cutoff < 1 || cutoff > words) |
| 763 | cutoff = words; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 764 | |
| 765 | wl = getTargetWords(word, search_backw); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 766 | if (wl == NULL || wl->length < 1) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 767 | return ""; |
| 768 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 769 | a = posix_memalign((void **)&target_sums, 128, cutoff * sizeof(float)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 770 | memset(target_sums, 0, cutoff * sizeof(float)); |
| 771 | |
| 772 | printf("Starting %d threads\n", syn_threads); |
| 773 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 774 | for (a = 0; a < syn_threads; a++) { |
| 775 | pars[a].cutoff = cutoff; |
| 776 | pars[a].target_sums = target_sums; |
| 777 | pars[a].window_sums = window_sums; |
| 778 | pars[a].wl = wl; |
| 779 | pars[a].N = maxPerPos; |
| 780 | pars[a].threshold = threshold; |
| 781 | pars[a].from = a; |
| 782 | pars[a].upto = a + 1; |
| 783 | pthread_create(&pt[a], NULL, getCollocators, (void *)&pars[a]); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 784 | } |
| 785 | printf("Waiting for syn threads to join\n"); |
| 786 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 787 | 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] | 788 | printf("Syn threads joint\n"); |
| 789 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 790 | result = malloc(maxPerPos * 80 * syn_threads); |
Marc Kupietz | bdd779a | 2024-08-05 10:02:29 +0200 | [diff] [blame] | 791 | char *p = (char *) result; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 792 | *p = 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 793 | for (a = syn_threads - 1; a >= 0; a--) { |
| 794 | for (b = 0; b < syn_nbs[a]->length; b++) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 795 | 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); |
| 796 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 797 | } |
Marc Kupietz | bdd779a | 2024-08-05 10:02:29 +0200 | [diff] [blame] | 798 | return ((char *)result); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe, int no_similar_profiles) { |
| 802 | HV *result = newHV(); |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 803 | float *target_sums = NULL; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 804 | long a, b, c, d, slice; |
| 805 | knn *para_nbs[MAX_THREADS]; |
| 806 | knn *syn_nbs[MAX_THREADS]; |
| 807 | knnpars pars[MAX_THREADS]; |
| 808 | pthread_t *pt = (pthread_t *)malloc((num_threads + 1) * sizeof(pthread_t)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 809 | wordlist *wl; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 810 | int syn_threads = (M2 ? window * 2 : 0); |
| 811 | int para_threads = (no_similar_profiles ? 0 : num_threads - syn_threads); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 812 | |
| 813 | collocator *best = NULL; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 814 | posix_memalign((void **)&best, 128, 10 * (N >= 200 ? N : 200) * sizeof(collocator)); |
| 815 | memset(best, 0, (N >= 200 ? N : 200) * sizeof(collocator)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 816 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 817 | if (N > MAX_NEIGHBOURS) N = MAX_NEIGHBOURS; |
| 818 | |
| 819 | if (cutoff < 1 || cutoff > words) |
| 820 | cutoff = words; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 821 | |
| 822 | wl = getTargetWords(st1, search_backw); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 823 | if (wl == NULL || wl->length < 1) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 824 | goto end; |
| 825 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 826 | slice = cutoff / para_threads; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 827 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 828 | a = posix_memalign((void **)&target_sums, 128, cutoff * sizeof(float)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 829 | memset(target_sums, 0, cutoff * sizeof(float)); |
| 830 | |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 831 | printf("Starting %d threads for paradigmatic search\n", para_threads); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 832 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 833 | for (a = 0; a < para_threads; a++) { |
| 834 | pars[a].cutoff = cutoff; |
| 835 | pars[a].token = st1; |
| 836 | pars[a].wl = wl; |
| 837 | pars[a].N = N; |
| 838 | pars[a].best = &best[N * a]; |
| 839 | if (merge_words == 0 || search_backw == 0) { |
| 840 | pars[a].from = a * slice; |
| 841 | pars[a].upto = ((a + 1) * slice > cutoff ? cutoff : (a + 1) * slice); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 842 | } else { |
| 843 | pars[a].from = merge_words + a * slice; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 844 | 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] | 845 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 846 | printf("From: %ld, Upto: %ld\n", pars[a].from, pars[a].upto); |
| 847 | pthread_create(&pt[a], NULL, _get_neighbours, (void *)&pars[a]); |
| 848 | } |
| 849 | if (M2) { |
| 850 | for (a = 0; a < syn_threads; a++) { |
| 851 | pars[a + para_threads].cutoff = cutoff; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 852 | pars[a + para_threads].target_sums = target_sums; |
| 853 | pars[a + para_threads].window_sums = window_sums; |
| 854 | pars[a + para_threads].wl = wl; |
| 855 | pars[a + para_threads].N = N; |
| 856 | pars[a + para_threads].threshold = MIN_RESP; |
| 857 | pars[a + para_threads].from = a; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 858 | pars[a + para_threads].upto = a + 1; |
| 859 | 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] | 860 | } |
| 861 | } |
| 862 | printf("Waiting for para threads to join\n"); |
| 863 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 864 | 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] | 865 | printf("Para threads joint\n"); |
| 866 | fflush(stdout); |
| 867 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 868 | /* if(!syn_nbs[0]) */ |
| 869 | /* goto end; */ |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 870 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 871 | qsort(best, N * para_threads, sizeof(collocator), cmp_activation); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 872 | |
| 873 | long long chosen[MAX_NEIGHBOURS]; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 874 | printf("N: %d\n", N); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 875 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 876 | AV *array = newAV(); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 877 | int i, j; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 878 | int l1_words = 0, l2_words = 0; |
| 879 | |
| 880 | for (a = 0, i = 0; i < N && a < N * para_threads; a++) { |
| 881 | int filtered = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 882 | long long c = best[a].wordi; |
| 883 | if ((merge_words && dedupe && i > 1) || (!merge_words && dedupe && i > 0)) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 884 | for (j = 0; j < i && !filtered; j++) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 885 | if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) || |
| 886 | strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 887 | printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]); |
| 888 | filtered = 1; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 889 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 890 | if (filtered) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 891 | continue; |
| 892 | } |
| 893 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 894 | if (0 && merge_words > 0) { |
| 895 | if (c >= merge_words) { |
| 896 | if (l1_words > N / 2) |
| 897 | continue; |
| 898 | else |
| 899 | l1_words++; |
| 900 | } else { |
| 901 | if (l2_words > N / 2) |
| 902 | continue; |
| 903 | else |
| 904 | l2_words++; |
| 905 | } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 906 | } |
| 907 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 908 | // printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a); |
| 909 | // fflush(stdout); |
| 910 | HV *hash = newHV(); |
| 911 | SV *word = newSVpvf(&vocab[c * max_w], 0); |
| 912 | chosen[i] = c; |
| 913 | if (latin_enc == 0) SvUTF8_on(word); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 914 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 915 | hv_store(hash, "word", strlen("word"), word, 0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 916 | hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0); |
| 917 | hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0); |
| 918 | AV *vector = newAV(); |
| 919 | for (b = 0; b < size; b++) { |
| 920 | av_push(vector, newSVnv(M[b + best[a].wordi * size])); |
| 921 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 922 | hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV *)vector), 0); |
| 923 | av_push(array, newRV_noinc((SV *)hash)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 924 | i++; |
| 925 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 926 | hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV *)array), 0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 927 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 928 | for (b = 0; b < MAX_NEIGHBOURS; b++) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 929 | best[b].wordi = -1L; |
| 930 | best[b].activation = 0; |
| 931 | best[b].probability = 0; |
| 932 | best[b].position = 0; |
| 933 | best[b].activation_sum = 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 934 | memset(best[b].heat, 0, sizeof(float) * 16); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 935 | } |
| 936 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 937 | float total_activation = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 938 | |
| 939 | if (M2) { |
| 940 | printf("Waiting for syn threads to join\n"); |
| 941 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 942 | for (a = 0; a < syn_threads; a++) pthread_join(pt[a + para_threads], (void *)&syn_nbs[a]); |
| 943 | for (a = 0; a <= syn_threads; a++) { |
| 944 | if (a == window) continue; |
| 945 | total_activation += window_sums[a]; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 946 | printf("window pos: %ld, sum: %f\n", a, window_sums[a]); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 947 | } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 948 | printf("syn threads joint\n"); |
| 949 | fflush(stdout); |
| 950 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 951 | for (b = 0; b < syn_nbs[0]->length; b++) { |
| 952 | memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator)); |
| 953 | best[b].position = -1; // syn_nbs[0]->pos[b]; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 954 | best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi]; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 955 | best[b].max_activation = 0.0; |
| 956 | best[b].average = 0.0; |
| 957 | best[b].probability = 0.0; |
| 958 | best[b].cprobability = syn_nbs[0]->best[b].cprobability; |
| 959 | memset(best[b].heat, 0, sizeof(float) * 16); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 960 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 961 | |
| 962 | float best_window_sum[MAX_NEIGHBOURS]; |
Marc Kupietz | 59865a9 | 2021-03-11 17:16:51 +0100 | [diff] [blame] | 963 | int found_index = 0, i = 0, w; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 964 | for (a = 0; a < syn_threads; a++) { |
| 965 | for (b = 0; b < syn_nbs[a]->length; b++) { |
| 966 | for (i = 0; i < found_index; i++) |
| 967 | if (best[i].wordi == syn_nbs[a]->best[b].wordi) |
| 968 | break; |
| 969 | if (i >= found_index) { |
| 970 | best[found_index].max_activation = 0.0; |
| 971 | best[found_index].average = 0.0; |
| 972 | best[found_index].probability = 0.0; |
| 973 | memset(best[found_index].heat, 0, sizeof(float) * 16); |
| 974 | best[found_index].cprobability = syn_nbs[a]->best[b].cprobability; |
| 975 | best[found_index].activation_sum = target_sums[syn_nbs[a]->best[b].wordi]; // syn_nbs[a]->best[b].activation_sum; |
| 976 | best[found_index++].wordi = syn_nbs[a]->best[b].wordi; |
| 977 | // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]); |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | sort_by = 0; // ALWAYS AUTO-FOCUS |
| 982 | if (sort_by != 1 && sort_by != 2) { // sort by auto focus mean |
| 983 | printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) - 1); |
| 984 | int wpos; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 985 | int bits_set = 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 986 | for (i = 0; i < found_index; i++) { |
| 987 | best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0; |
| 988 | for (w = 1; w < (1 << syn_threads); w++) { // loop through all possible windows |
| 989 | 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] | 990 | bits_set = 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 991 | for (a = 0; a < syn_threads; a++) { |
| 992 | if ((1 << a) & w) { |
| 993 | wpos = (a >= window ? a + 1 : a); |
| 994 | total_window_sum += window_sums[wpos]; |
| 995 | } |
| 996 | } |
| 997 | // printf("%d window-sum %f\n", w, total_window_sum); |
| 998 | for (a = 0; a < syn_threads; a++) { |
| 999 | if ((1 << a) & w) { |
| 1000 | wpos = (a >= window ? a + 1 : a); |
| 1001 | bits_set++; |
| 1002 | for (b = 0; b < syn_nbs[a]->length; b++) |
| 1003 | if (best[i].wordi == syn_nbs[a]->best[b].wordi) { |
| 1004 | // float acti = syn_nbs[a]->best[b].activation / total_window_sum; |
| 1005 | // word_window_sum += syn_nbs[a]->dist[b] * syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b]; |
| 1006 | // word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b]; |
| 1007 | // 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] | 1008 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1009 | word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b]; |
| 1010 | // 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] | 1011 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1012 | 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] | 1013 | 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] | 1014 | word_activation_sum += syn_nbs[a]->best[b].activation; |
| 1015 | if (syn_nbs[a]->best[b].activation > best[i].max_activation) |
| 1016 | best[i].max_activation = syn_nbs[a]->best[b].activation; |
| 1017 | if (syn_nbs[a]->best[b].activation > best[i].heat[wpos]) |
| 1018 | best[i].heat[wpos] = syn_nbs[a]->best[b].activation; |
| 1019 | } |
| 1020 | } |
| 1021 | } |
| 1022 | if (bits_set) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1023 | word_window_average /= bits_set; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1024 | // word_activation_sum /= bits_set; |
| 1025 | // word_window_sum /= bits_set; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1026 | } |
| 1027 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1028 | word_window_sum /= total_window_sum; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1029 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1030 | if (word_window_sum > best[i].probability) { |
| 1031 | // best[i].position = w; |
| 1032 | best[i].probability = word_window_sum; |
| 1033 | } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1034 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1035 | if (word_cprobability_sum > best[i].cprobability_sum) { |
| 1036 | best[i].position = w; |
| 1037 | best[i].cprobability_sum = word_cprobability_sum; |
| 1038 | } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1039 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1040 | best[i].average = word_window_average; |
| 1041 | // best[i].activation = word_activation_sum; |
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 | } |
| 1044 | qsort(best, found_index, sizeof(collocator), cmp_probability); |
| 1045 | // for(i=0; i < found_index; i++) { |
| 1046 | // printf("found: %s - sum: %f - window: %d\n", &vocab[best[i].wordi * max_w], best[i].activation, best[i].position); |
| 1047 | // } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1048 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1049 | } else if (sort_by == 1) { // responsiveness any window position |
| 1050 | int wpos; |
| 1051 | for (i = 0; i < found_index; i++) { |
| 1052 | float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0; |
| 1053 | for (a = 0; a < syn_threads; a++) { |
| 1054 | wpos = (a >= window ? a + 1 : a); |
| 1055 | for (b = 0; b < syn_nbs[a]->length; b++) |
| 1056 | if (best[i].wordi == syn_nbs[a]->best[b].wordi) { |
| 1057 | best[i].probability += syn_nbs[a]->best[b].probability; |
| 1058 | if (syn_nbs[a]->best[b].activation > 0.25) |
| 1059 | best[i].position |= 1 << wpos; |
| 1060 | if (syn_nbs[a]->best[b].activation > best[i].activation) { |
| 1061 | best[i].activation = syn_nbs[a]->best[b].activation; |
| 1062 | } |
| 1063 | } |
| 1064 | } |
| 1065 | } |
| 1066 | qsort(best, found_index, sizeof(collocator), cmp_activation); |
| 1067 | } else if (sort_by == 2) { // single window position |
| 1068 | for (a = 1; a < syn_threads; a++) { |
| 1069 | for (b = 0; b < syn_nbs[a]->length; b++) { |
| 1070 | for (c = 0; c < MAX_NEIGHBOURS; c++) { |
| 1071 | if (syn_nbs[a]->best[b].activation > best[c].activation) { |
| 1072 | for (d = MAX_NEIGHBOURS - 1; d > c; d--) { |
| 1073 | memmove(best + d, best + d - 1, sizeof(collocator)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1074 | } |
| 1075 | memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator)); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1076 | 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] | 1077 | break; |
| 1078 | } |
| 1079 | } |
| 1080 | } |
| 1081 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1082 | } else { // sort by mean p |
| 1083 | for (a = 1; a < syn_threads; a++) { |
| 1084 | for (b = 0; b < syn_nbs[a]->length; b++) { |
| 1085 | for (c = 0; c < MAX_NEIGHBOURS; c++) { |
| 1086 | if (target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) { |
| 1087 | for (d = MAX_NEIGHBOURS - 1; d > c; d--) { |
| 1088 | memmove(best + d, best + d - 1, sizeof(collocator)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1089 | } |
| 1090 | memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator)); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1091 | best[c].position = (1 << 2 * window) - 1; // syn_nbs[a]->pos[b]; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1092 | best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi]; |
| 1093 | break; |
| 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | } |
| 1099 | array = newAV(); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1100 | 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] | 1101 | long long c = best[a].wordi; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1102 | /* |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1103 | if (dedupe) { |
| 1104 | int filtered=0; |
| 1105 | for (j=0; j<i; j++) |
| 1106 | if (strcasestr(&vocab[c * max_w], chosen[j]) || |
| 1107 | strcasestr(chosen[j], &vocab[c * max_w])) { |
| 1108 | printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]); |
| 1109 | filtered = 1; |
| 1110 | } |
| 1111 | if(filtered) |
| 1112 | continue; |
| 1113 | } |
| 1114 | */ |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1115 | chosen[i++] = c; |
| 1116 | HV *hash = newHV(); |
| 1117 | SV *word = newSVpvf(&vocab[best[a].wordi * max_w], 0); |
| 1118 | AV *heat = newAV(); |
| 1119 | if (latin_enc == 0) SvUTF8_on(word); |
| 1120 | hv_store(hash, "word", strlen("word"), word, 0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1121 | hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0); |
| 1122 | hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0); |
| 1123 | hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0); |
| 1124 | hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1125 | hv_store(hash, "max", strlen("max"), newSVnv(best[a].max_activation), 0); // newSVnv(target_sums[best[a].wordi]), 0); |
| 1126 | 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] | 1127 | hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1128 | best[a].heat[5] = 0; |
| 1129 | for (i = 10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i])); |
| 1130 | hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV *)heat), 0); |
| 1131 | av_push(array, newRV_noinc((SV *)hash)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1132 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1133 | hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV *)array), 0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1134 | } |
| 1135 | end: |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1136 | free(best); |
| 1137 | return newRV_noinc((SV *)result); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | int dump_vecs(char *fname) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1141 | long i, j; |
| 1142 | FILE *f; |
| 1143 | /* if(words>100000) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1144 | words=100000; |
| 1145 | */ |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1146 | if ((f = fopen(fname, "w")) == NULL) { |
| 1147 | fprintf(stderr, "cannot open %s for writing\n", fname); |
| 1148 | return (-1); |
| 1149 | } |
| 1150 | fprintf(f, "%lld %lld\n", words, size); |
| 1151 | for (i = 0; i < words; i++) { |
| 1152 | fprintf(f, "%s ", &vocab[i * max_w]); |
| 1153 | for (j = 0; j < size - 1; j++) |
| 1154 | fprintf(f, "%f ", M[i * size + j]); |
| 1155 | fprintf(f, "%f\n", M[i * size + j]); |
| 1156 | } |
| 1157 | fclose(f); |
| 1158 | return (0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | int dump_for_numpy(char *fname) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1162 | long i, j; |
| 1163 | FILE *f; |
Marc Kupietz | c0d4187 | 2021-02-25 16:33:22 +0100 | [diff] [blame] | 1164 | int max = words; // 300000; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1165 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1166 | if ((f = fopen(fname, "w")) == NULL) { |
| 1167 | fprintf(stderr, "cannot open %s for writing\n", fname); |
| 1168 | return (-1); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1169 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1170 | for (i = 0; i < max; i++) { |
| 1171 | for (j = 0; j < size - 1; j++) |
| 1172 | fprintf(f, "%f\t", M[i * size + j]); |
| 1173 | fprintf(f, "%f\n", M[i * size + j]); |
| 1174 | printf("%s\r\n", &vocab[i * max_w]); |
| 1175 | } |
| 1176 | if (merged_end > 0) { |
| 1177 | for (i = 0; i < max; i++) { |
| 1178 | for (j = 0; j < size - 1; j++) |
| 1179 | fprintf(f, "%f\t", M[(merged_end + i) * size + j]); |
| 1180 | fprintf(f, "%f\n", M[(merged_end + i) * size + j]); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1181 | printf("_%s\r\n", &vocab[i * max_w]); |
| 1182 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame] | 1183 | } |
| 1184 | fclose(f); |
| 1185 | return (0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1186 | } |
Marc Kupietz | 043db15 | 2023-11-05 17:47:53 +0100 | [diff] [blame] | 1187 | |
| 1188 | unsigned long getVocabSize() { |
| 1189 | return (unsigned long) words; |
| 1190 | } |