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