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