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