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 | |
| 537 | float get_distance(long b, long c) { |
| 538 | long a; |
| 539 | float dist = 0; |
| 540 | for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + b * size]; |
| 541 | return dist; |
| 542 | } |
| 543 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 544 | char *getBiggestMergedDifferences() { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 545 | static char *result = NULL; |
| 546 | float dist, len, vec[max_size]; |
| 547 | long long a, b, c, d, cn, *bi; |
| 548 | char ch; |
| 549 | knn *nbs = NULL; |
| 550 | int N = 1000; |
| 551 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 552 | if (merged_end == 0) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 553 | result = "[]"; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 554 | |
| 555 | if (result != NULL) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 556 | return result; |
| 557 | |
| 558 | printf("Looking for biggest distances between main and merged vectors ...\n"); |
| 559 | collocator *best; |
| 560 | best = malloc(N * sizeof(collocator)); |
| 561 | memset(best, 0, N * sizeof(collocator)); |
| 562 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 563 | float worstbest = 1000000; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 564 | |
| 565 | for (a = 0; a < N; a++) best[a].activation = worstbest; |
| 566 | |
| 567 | for (c = 0; c < 500000; c++) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 568 | if (garbage && garbage[c]) continue; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 569 | a = 0; |
| 570 | dist = 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 571 | for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + (c + merged_end) * size]; |
| 572 | if (dist < worstbest) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 573 | for (a = 0; a < N; a++) { |
| 574 | if (dist < best[a].activation) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 575 | memmove(best + a + 1, best + a, (N - a - 1) * sizeof(collocator)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 576 | best[a].activation = dist; |
| 577 | best[a].wordi = c; |
| 578 | break; |
| 579 | } |
| 580 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 581 | worstbest = best[N - 1].activation; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 585 | result = malloc(N * max_w); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 586 | char *p = result; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 587 | *p++ = '['; |
| 588 | *p = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 589 | for (a = 0; a < N; a++) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 590 | 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] | 591 | } |
| 592 | *--p = ']'; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 593 | return (result); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 594 | } |
| 595 | |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 596 | float cos_similarity(long b, long c) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 597 | float dist = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 598 | long a; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 599 | 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] | 600 | return dist; |
| 601 | } |
| 602 | |
| 603 | char *cos_similarity_as_json(char *w1, char *w2) { |
| 604 | wordlist *a, *b; |
| 605 | float res; |
| 606 | a = getTargetWords(w1, 0); |
| 607 | b = getTargetWords(w2, 0); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 608 | if (a == NULL || b == NULL || a->length != 1 || b->length != 1) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 609 | res = -1; |
| 610 | else |
| 611 | res = cos_similarity(a->wordi[0], b->wordi[0]); |
| 612 | fprintf(stderr, "a: %lld b: %lld res:%f\n", a->wordi[0], b->wordi[0], res); |
| 613 | char *json = malloc(16); |
| 614 | sprintf(json, "%.5f", res); |
| 615 | return json; |
| 616 | } |
| 617 | |
| 618 | void *_get_neighbours(void *arg) { |
| 619 | knnpars *pars = arg; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 620 | char *st1 = pars->token; |
| 621 | int N = pars->N; |
| 622 | long from = pars->from; |
| 623 | unsigned long upto = pars->upto; |
| 624 | char file_name[max_size], st[100][max_size], *sep; |
| 625 | float dist, len, vec[max_size]; |
| 626 | long long a, b, c, d, cn, *bi; |
| 627 | char ch; |
| 628 | knn *nbs = NULL; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 629 | wordlist *wl = pars->wl; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 630 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 631 | collocator *best = pars->best; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 632 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 633 | float worstbest = -1; |
| 634 | |
| 635 | for (a = 0; a < N; a++) best[a].activation = 0; |
| 636 | a = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 637 | bi = wl->wordi; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 638 | cn = wl->length; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 639 | sep = wl->sep; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 640 | b = bi[0]; |
| 641 | c = 0; |
| 642 | if (b == -1) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 643 | N = 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 644 | goto end; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 645 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 646 | for (a = 0; a < size; a++) vec[a] = 0; |
| 647 | for (b = 0; b < cn; b++) { |
| 648 | if (bi[b] == -1) continue; |
| 649 | if (b > 0 && sep[b - 1] == '-') |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 650 | for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size]; |
| 651 | else |
| 652 | 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^] | 653 | } |
| 654 | len = 0; |
| 655 | for (a = 0; a < size; a++) len += vec[a] * vec[a]; |
| 656 | len = sqrt(len); |
| 657 | for (a = 0; a < size; a++) vec[a] /= len; |
| 658 | for (a = 0; a < N; a++) best[a].activation = -1; |
| 659 | for (c = from; c < upto; c++) { |
| 660 | if (garbage && garbage[c]) continue; |
| 661 | a = 0; |
| 662 | // do not skip taget word |
| 663 | // for (b = 0; b < cn; b++) if (bi[b] == c) a = 1; |
| 664 | // if (a == 1) continue; |
| 665 | dist = 0; |
| 666 | for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size]; |
| 667 | if (dist > worstbest) { |
| 668 | for (a = 0; a < N; a++) { |
| 669 | if (dist > best[a].activation) { |
| 670 | memmove(best + a + 1, best + a, (N - a - 1) * sizeof(collocator)); |
| 671 | best[a].activation = dist; |
| 672 | best[a].wordi = c; |
| 673 | break; |
| 674 | } |
| 675 | } |
| 676 | worstbest = best[N - 1].activation; |
| 677 | } |
| 678 | } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 679 | |
| 680 | end: |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 681 | pthread_exit(nbs); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 682 | } |
| 683 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 684 | int cmp_activation(const void *a, const void *b) { |
| 685 | float fb = ((collocator *)a)->activation; |
| 686 | float fa = ((collocator *)b)->activation; |
| 687 | return (fa > fb) - (fa < fb); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 688 | } |
| 689 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 690 | int cmp_probability(const void *a, const void *b) { |
| 691 | float fb = ((collocator *)a)->probability; |
| 692 | float fa = ((collocator *)b)->probability; |
| 693 | return (fa > fb) - (fa < fb); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 694 | } |
| 695 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 696 | char *getPosWiseW2VCollocatorsAsTsv(char *word, long maxPerPos, long cutoff, float threshold) { |
| 697 | HV *result = newHV(); |
| 698 | float *target_sums = NULL, vec[max_size]; |
| 699 | long long old_words; |
| 700 | long a, b, c, d; |
| 701 | knn *para_nbs[MAX_THREADS]; |
| 702 | knn *syn_nbs[MAX_THREADS]; |
| 703 | knnpars pars[MAX_THREADS]; |
| 704 | pthread_t *pt = (pthread_t *)malloc((num_threads + 1) * sizeof(pthread_t)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 705 | wordlist *wl; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 706 | int syn_threads = (M2 ? window * 2 : 0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 707 | int search_backw = 0; |
| 708 | collocator *best = NULL; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 709 | posix_memalign((void **)&best, 128, 10 * (maxPerPos >= 200 ? maxPerPos : 200) * sizeof(collocator)); |
| 710 | memset(best, 0, (maxPerPos >= 200 ? maxPerPos : 200) * sizeof(collocator)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 711 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 712 | if (cutoff < 1 || cutoff > words) |
| 713 | cutoff = words; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 714 | |
| 715 | wl = getTargetWords(word, search_backw); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 716 | if (wl == NULL || wl->length < 1) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 717 | return ""; |
| 718 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 719 | a = posix_memalign((void **)&target_sums, 128, cutoff * sizeof(float)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 720 | memset(target_sums, 0, cutoff * sizeof(float)); |
| 721 | |
| 722 | printf("Starting %d threads\n", syn_threads); |
| 723 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 724 | for (a = 0; a < syn_threads; a++) { |
| 725 | pars[a].cutoff = cutoff; |
| 726 | pars[a].target_sums = target_sums; |
| 727 | pars[a].window_sums = window_sums; |
| 728 | pars[a].wl = wl; |
| 729 | pars[a].N = maxPerPos; |
| 730 | pars[a].threshold = threshold; |
| 731 | pars[a].from = a; |
| 732 | pars[a].upto = a + 1; |
| 733 | pthread_create(&pt[a], NULL, getCollocators, (void *)&pars[a]); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 734 | } |
| 735 | printf("Waiting for syn threads to join\n"); |
| 736 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 737 | 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] | 738 | printf("Syn threads joint\n"); |
| 739 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 740 | result = malloc(maxPerPos * 80 * syn_threads); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 741 | char *p = result; |
| 742 | *p = 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 743 | for (a = syn_threads - 1; a >= 0; a--) { |
| 744 | for (b = 0; b < syn_nbs[a]->length; b++) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 745 | 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); |
| 746 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 747 | } |
| 748 | return (result); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe, int no_similar_profiles) { |
| 752 | HV *result = newHV(); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 753 | float *target_sums = NULL, vec[max_size]; |
| 754 | long long old_words; |
| 755 | long a, b, c, d, slice; |
| 756 | knn *para_nbs[MAX_THREADS]; |
| 757 | knn *syn_nbs[MAX_THREADS]; |
| 758 | knnpars pars[MAX_THREADS]; |
| 759 | pthread_t *pt = (pthread_t *)malloc((num_threads + 1) * sizeof(pthread_t)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 760 | wordlist *wl; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 761 | int syn_threads = (M2 ? window * 2 : 0); |
| 762 | int para_threads = (no_similar_profiles ? 0 : num_threads - syn_threads); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 763 | |
| 764 | collocator *best = NULL; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 765 | posix_memalign((void **)&best, 128, 10 * (N >= 200 ? N : 200) * sizeof(collocator)); |
| 766 | memset(best, 0, (N >= 200 ? N : 200) * sizeof(collocator)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 767 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 768 | if (N > MAX_NEIGHBOURS) N = MAX_NEIGHBOURS; |
| 769 | |
| 770 | if (cutoff < 1 || cutoff > words) |
| 771 | cutoff = words; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 772 | |
| 773 | wl = getTargetWords(st1, search_backw); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 774 | if (wl == NULL || wl->length < 1) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 775 | goto end; |
| 776 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 777 | old_words = cutoff; |
| 778 | slice = cutoff / para_threads; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 779 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 780 | a = posix_memalign((void **)&target_sums, 128, cutoff * sizeof(float)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 781 | memset(target_sums, 0, cutoff * sizeof(float)); |
| 782 | |
| 783 | printf("Starting %d threads\n", para_threads); |
| 784 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 785 | for (a = 0; a < para_threads; a++) { |
| 786 | pars[a].cutoff = cutoff; |
| 787 | pars[a].token = st1; |
| 788 | pars[a].wl = wl; |
| 789 | pars[a].N = N; |
| 790 | pars[a].best = &best[N * a]; |
| 791 | if (merge_words == 0 || search_backw == 0) { |
| 792 | pars[a].from = a * slice; |
| 793 | pars[a].upto = ((a + 1) * slice > cutoff ? cutoff : (a + 1) * slice); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 794 | } else { |
| 795 | pars[a].from = merge_words + a * slice; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 796 | 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] | 797 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 798 | printf("From: %ld, Upto: %ld\n", pars[a].from, pars[a].upto); |
| 799 | pthread_create(&pt[a], NULL, _get_neighbours, (void *)&pars[a]); |
| 800 | } |
| 801 | if (M2) { |
| 802 | for (a = 0; a < syn_threads; a++) { |
| 803 | pars[a + para_threads].cutoff = cutoff; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 804 | pars[a + para_threads].target_sums = target_sums; |
| 805 | pars[a + para_threads].window_sums = window_sums; |
| 806 | pars[a + para_threads].wl = wl; |
| 807 | pars[a + para_threads].N = N; |
| 808 | pars[a + para_threads].threshold = MIN_RESP; |
| 809 | pars[a + para_threads].from = a; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 810 | pars[a + para_threads].upto = a + 1; |
| 811 | 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] | 812 | } |
| 813 | } |
| 814 | printf("Waiting for para threads to join\n"); |
| 815 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 816 | 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] | 817 | printf("Para threads joint\n"); |
| 818 | fflush(stdout); |
| 819 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 820 | /* if(!syn_nbs[0]) */ |
| 821 | /* goto end; */ |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 822 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 823 | qsort(best, N * para_threads, sizeof(collocator), cmp_activation); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 824 | |
| 825 | long long chosen[MAX_NEIGHBOURS]; |
| 826 | printf("N: %ld\n", N); |
| 827 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 828 | AV *array = newAV(); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 829 | int i, j; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 830 | int l1_words = 0, l2_words = 0; |
| 831 | |
| 832 | for (a = 0, i = 0; i < N && a < N * para_threads; a++) { |
| 833 | int filtered = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 834 | long long c = best[a].wordi; |
| 835 | if ((merge_words && dedupe && i > 1) || (!merge_words && dedupe && i > 0)) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 836 | for (j = 0; j < i && !filtered; j++) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 837 | if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) || |
| 838 | strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 839 | printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]); |
| 840 | filtered = 1; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 841 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 842 | if (filtered) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 843 | continue; |
| 844 | } |
| 845 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 846 | if (0 && merge_words > 0) { |
| 847 | if (c >= merge_words) { |
| 848 | if (l1_words > N / 2) |
| 849 | continue; |
| 850 | else |
| 851 | l1_words++; |
| 852 | } else { |
| 853 | if (l2_words > N / 2) |
| 854 | continue; |
| 855 | else |
| 856 | l2_words++; |
| 857 | } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 858 | } |
| 859 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 860 | // printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a); |
| 861 | // fflush(stdout); |
| 862 | HV *hash = newHV(); |
| 863 | SV *word = newSVpvf(&vocab[c * max_w], 0); |
| 864 | chosen[i] = c; |
| 865 | if (latin_enc == 0) SvUTF8_on(word); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 866 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 867 | hv_store(hash, "word", strlen("word"), word, 0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 868 | hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0); |
| 869 | hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0); |
| 870 | AV *vector = newAV(); |
| 871 | for (b = 0; b < size; b++) { |
| 872 | av_push(vector, newSVnv(M[b + best[a].wordi * size])); |
| 873 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 874 | hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV *)vector), 0); |
| 875 | av_push(array, newRV_noinc((SV *)hash)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 876 | i++; |
| 877 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 878 | hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV *)array), 0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 879 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 880 | for (b = 0; b < MAX_NEIGHBOURS; b++) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 881 | best[b].wordi = -1L; |
| 882 | best[b].activation = 0; |
| 883 | best[b].probability = 0; |
| 884 | best[b].position = 0; |
| 885 | best[b].activation_sum = 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 886 | memset(best[b].heat, 0, sizeof(float) * 16); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 887 | } |
| 888 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 889 | float total_activation = 0; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 890 | |
| 891 | if (M2) { |
| 892 | printf("Waiting for syn threads to join\n"); |
| 893 | fflush(stdout); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 894 | for (a = 0; a < syn_threads; a++) pthread_join(pt[a + para_threads], (void *)&syn_nbs[a]); |
| 895 | for (a = 0; a <= syn_threads; a++) { |
| 896 | if (a == window) continue; |
| 897 | total_activation += window_sums[a]; |
| 898 | printf("window pos: %d, sum: %f\n", a, window_sums[a]); |
| 899 | } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 900 | printf("syn threads joint\n"); |
| 901 | fflush(stdout); |
| 902 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 903 | for (b = 0; b < syn_nbs[0]->length; b++) { |
| 904 | memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator)); |
| 905 | best[b].position = -1; // syn_nbs[0]->pos[b]; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 906 | best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi]; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 907 | best[b].max_activation = 0.0; |
| 908 | best[b].average = 0.0; |
| 909 | best[b].probability = 0.0; |
| 910 | best[b].cprobability = syn_nbs[0]->best[b].cprobability; |
| 911 | memset(best[b].heat, 0, sizeof(float) * 16); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 912 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 913 | |
| 914 | float best_window_sum[MAX_NEIGHBOURS]; |
| 915 | int found_index = 0, i = 0, j, w; |
| 916 | for (a = 0; a < syn_threads; a++) { |
| 917 | for (b = 0; b < syn_nbs[a]->length; b++) { |
| 918 | for (i = 0; i < found_index; i++) |
| 919 | if (best[i].wordi == syn_nbs[a]->best[b].wordi) |
| 920 | break; |
| 921 | if (i >= found_index) { |
| 922 | best[found_index].max_activation = 0.0; |
| 923 | best[found_index].average = 0.0; |
| 924 | best[found_index].probability = 0.0; |
| 925 | memset(best[found_index].heat, 0, sizeof(float) * 16); |
| 926 | best[found_index].cprobability = syn_nbs[a]->best[b].cprobability; |
| 927 | best[found_index].activation_sum = target_sums[syn_nbs[a]->best[b].wordi]; // syn_nbs[a]->best[b].activation_sum; |
| 928 | best[found_index++].wordi = syn_nbs[a]->best[b].wordi; |
| 929 | // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]); |
| 930 | } |
| 931 | } |
| 932 | } |
| 933 | sort_by = 0; // ALWAYS AUTO-FOCUS |
| 934 | if (sort_by != 1 && sort_by != 2) { // sort by auto focus mean |
| 935 | printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) - 1); |
| 936 | int wpos; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 937 | int bits_set = 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 938 | for (i = 0; i < found_index; i++) { |
| 939 | best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0; |
| 940 | for (w = 1; w < (1 << syn_threads); w++) { // loop through all possible windows |
| 941 | 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] | 942 | bits_set = 0; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 943 | for (a = 0; a < syn_threads; a++) { |
| 944 | if ((1 << a) & w) { |
| 945 | wpos = (a >= window ? a + 1 : a); |
| 946 | total_window_sum += window_sums[wpos]; |
| 947 | } |
| 948 | } |
| 949 | // printf("%d window-sum %f\n", w, total_window_sum); |
| 950 | for (a = 0; a < syn_threads; a++) { |
| 951 | if ((1 << a) & w) { |
| 952 | wpos = (a >= window ? a + 1 : a); |
| 953 | bits_set++; |
| 954 | for (b = 0; b < syn_nbs[a]->length; b++) |
| 955 | if (best[i].wordi == syn_nbs[a]->best[b].wordi) { |
| 956 | // float acti = syn_nbs[a]->best[b].activation / total_window_sum; |
| 957 | // word_window_sum += syn_nbs[a]->dist[b] * syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b]; |
| 958 | // word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b]; |
| 959 | // 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] | 960 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 961 | word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b]; |
| 962 | // 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] | 963 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 964 | 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] | 965 | 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^] | 966 | word_activation_sum += syn_nbs[a]->best[b].activation; |
| 967 | if (syn_nbs[a]->best[b].activation > best[i].max_activation) |
| 968 | best[i].max_activation = syn_nbs[a]->best[b].activation; |
| 969 | if (syn_nbs[a]->best[b].activation > best[i].heat[wpos]) |
| 970 | best[i].heat[wpos] = syn_nbs[a]->best[b].activation; |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | if (bits_set) { |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 975 | word_window_average /= bits_set; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 976 | // word_activation_sum /= bits_set; |
| 977 | // word_window_sum /= bits_set; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 978 | } |
| 979 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 980 | word_window_sum /= total_window_sum; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 981 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 982 | if (word_window_sum > best[i].probability) { |
| 983 | // best[i].position = w; |
| 984 | best[i].probability = word_window_sum; |
| 985 | } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 986 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 987 | if (word_cprobability_sum > best[i].cprobability_sum) { |
| 988 | best[i].position = w; |
| 989 | best[i].cprobability_sum = word_cprobability_sum; |
| 990 | } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 991 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 992 | best[i].average = word_window_average; |
| 993 | // best[i].activation = word_activation_sum; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 994 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 995 | } |
| 996 | qsort(best, found_index, sizeof(collocator), cmp_probability); |
| 997 | // for(i=0; i < found_index; i++) { |
| 998 | // printf("found: %s - sum: %f - window: %d\n", &vocab[best[i].wordi * max_w], best[i].activation, best[i].position); |
| 999 | // } |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1000 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 1001 | } else if (sort_by == 1) { // responsiveness any window position |
| 1002 | int wpos; |
| 1003 | for (i = 0; i < found_index; i++) { |
| 1004 | float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0; |
| 1005 | for (a = 0; a < syn_threads; a++) { |
| 1006 | wpos = (a >= window ? a + 1 : a); |
| 1007 | for (b = 0; b < syn_nbs[a]->length; b++) |
| 1008 | if (best[i].wordi == syn_nbs[a]->best[b].wordi) { |
| 1009 | best[i].probability += syn_nbs[a]->best[b].probability; |
| 1010 | if (syn_nbs[a]->best[b].activation > 0.25) |
| 1011 | best[i].position |= 1 << wpos; |
| 1012 | if (syn_nbs[a]->best[b].activation > best[i].activation) { |
| 1013 | best[i].activation = syn_nbs[a]->best[b].activation; |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | } |
| 1018 | qsort(best, found_index, sizeof(collocator), cmp_activation); |
| 1019 | } else if (sort_by == 2) { // single window position |
| 1020 | for (a = 1; a < syn_threads; a++) { |
| 1021 | for (b = 0; b < syn_nbs[a]->length; b++) { |
| 1022 | for (c = 0; c < MAX_NEIGHBOURS; c++) { |
| 1023 | if (syn_nbs[a]->best[b].activation > best[c].activation) { |
| 1024 | for (d = MAX_NEIGHBOURS - 1; d > c; d--) { |
| 1025 | memmove(best + d, best + d - 1, sizeof(collocator)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1026 | } |
| 1027 | memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator)); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 1028 | 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] | 1029 | break; |
| 1030 | } |
| 1031 | } |
| 1032 | } |
| 1033 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 1034 | } else { // sort by mean p |
| 1035 | for (a = 1; a < syn_threads; a++) { |
| 1036 | for (b = 0; b < syn_nbs[a]->length; b++) { |
| 1037 | for (c = 0; c < MAX_NEIGHBOURS; c++) { |
| 1038 | if (target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) { |
| 1039 | for (d = MAX_NEIGHBOURS - 1; d > c; d--) { |
| 1040 | memmove(best + d, best + d - 1, sizeof(collocator)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1041 | } |
| 1042 | memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator)); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 1043 | best[c].position = (1 << 2 * window) - 1; // syn_nbs[a]->pos[b]; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1044 | best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi]; |
| 1045 | break; |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | array = newAV(); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 1052 | 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] | 1053 | long long c = best[a].wordi; |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 1054 | /* |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1055 | if (dedupe) { |
| 1056 | int filtered=0; |
| 1057 | for (j=0; j<i; j++) |
| 1058 | if (strcasestr(&vocab[c * max_w], chosen[j]) || |
| 1059 | strcasestr(chosen[j], &vocab[c * max_w])) { |
| 1060 | printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]); |
| 1061 | filtered = 1; |
| 1062 | } |
| 1063 | if(filtered) |
| 1064 | continue; |
| 1065 | } |
| 1066 | */ |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 1067 | chosen[i++] = c; |
| 1068 | HV *hash = newHV(); |
| 1069 | SV *word = newSVpvf(&vocab[best[a].wordi * max_w], 0); |
| 1070 | AV *heat = newAV(); |
| 1071 | if (latin_enc == 0) SvUTF8_on(word); |
| 1072 | hv_store(hash, "word", strlen("word"), word, 0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1073 | hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0); |
| 1074 | hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0); |
| 1075 | hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0); |
| 1076 | hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 1077 | hv_store(hash, "max", strlen("max"), newSVnv(best[a].max_activation), 0); // newSVnv(target_sums[best[a].wordi]), 0); |
| 1078 | 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] | 1079 | hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0); |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 1080 | best[a].heat[5] = 0; |
| 1081 | for (i = 10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i])); |
| 1082 | hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV *)heat), 0); |
| 1083 | av_push(array, newRV_noinc((SV *)hash)); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1084 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 1085 | hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV *)array), 0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1086 | } |
| 1087 | end: |
| 1088 | // words = old_words; // why was this here? |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 1089 | free(best); |
| 1090 | return newRV_noinc((SV *)result); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | int dump_vecs(char *fname) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 1094 | long i, j; |
| 1095 | FILE *f; |
| 1096 | /* if(words>100000) |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1097 | words=100000; |
| 1098 | */ |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 1099 | if ((f = fopen(fname, "w")) == NULL) { |
| 1100 | fprintf(stderr, "cannot open %s for writing\n", fname); |
| 1101 | return (-1); |
| 1102 | } |
| 1103 | fprintf(f, "%lld %lld\n", words, size); |
| 1104 | for (i = 0; i < words; i++) { |
| 1105 | fprintf(f, "%s ", &vocab[i * max_w]); |
| 1106 | for (j = 0; j < size - 1; j++) |
| 1107 | fprintf(f, "%f ", M[i * size + j]); |
| 1108 | fprintf(f, "%f\n", M[i * size + j]); |
| 1109 | } |
| 1110 | fclose(f); |
| 1111 | return (0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | int dump_for_numpy(char *fname) { |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 1115 | long i, j; |
| 1116 | FILE *f; |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1117 | int max = 300000; |
| 1118 | |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 1119 | if ((f = fopen(fname, "w")) == NULL) { |
| 1120 | fprintf(stderr, "cannot open %s for writing\n", fname); |
| 1121 | return (-1); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1122 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 1123 | for (i = 0; i < max; i++) { |
| 1124 | for (j = 0; j < size - 1; j++) |
| 1125 | fprintf(f, "%f\t", M[i * size + j]); |
| 1126 | fprintf(f, "%f\n", M[i * size + j]); |
| 1127 | printf("%s\r\n", &vocab[i * max_w]); |
| 1128 | } |
| 1129 | if (merged_end > 0) { |
| 1130 | for (i = 0; i < max; i++) { |
| 1131 | for (j = 0; j < size - 1; j++) |
| 1132 | fprintf(f, "%f\t", M[(merged_end + i) * size + j]); |
| 1133 | fprintf(f, "%f\n", M[(merged_end + i) * size + j]); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1134 | printf("_%s\r\n", &vocab[i * max_w]); |
| 1135 | } |
Marc Kupietz | 969cab9 | 2019-08-05 11:13:42 +0200 | [diff] [blame^] | 1136 | } |
| 1137 | fclose(f); |
| 1138 | return (0); |
Marc Kupietz | f11d20c | 2019-08-02 15:42:04 +0200 | [diff] [blame] | 1139 | } |