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