Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 1 | #!/usr/local/bin/perl |
| 2 | use Inline C; |
| 3 | use Mojolicious::Lite; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 4 | use Mojo::JSON qw(decode_json encode_json to_json); |
Marc Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 5 | use Encode qw(decode encode); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 6 | use Getopt::Std; |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 7 | use Mojo::Server::Daemon; |
Marc Kupietz | d422739 | 2016-03-01 16:45:12 +0100 | [diff] [blame] | 8 | plugin 'Log::Access'; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 9 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 10 | our $opt_i = 0; # latin1-input? |
| 11 | our $opt_l = undef; |
| 12 | our $opt_p = 5676; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 13 | our $opt_n = undef; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 14 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 15 | getopt('il:p:n:'); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 16 | |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 17 | # -cbow 1 -size 200 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 40 -binary 1 -iter 15 |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 18 | if(!$ARGV[0]) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 19 | init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0)); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 20 | } else { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 21 | init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0)); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 22 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 23 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 24 | my $daemon = Mojo::Server::Daemon->new( |
| 25 | app => app, |
| 26 | listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"] |
| 27 | ); |
| 28 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 29 | get '/' => sub { |
| 30 | my $c = shift; |
| 31 | my $word=$c->param('word'); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 32 | my $no_nbs=$c->param('n') || 100; |
| 33 | my $no_iterations=$c->param('N') || 2000; |
Marc Kupietz | d422739 | 2016-03-01 16:45:12 +0100 | [diff] [blame] | 34 | my $perplexity=$c->param('perplexity') || 20; |
Marc Kupietz | c4d62f8 | 2016-03-01 11:04:24 +0100 | [diff] [blame] | 35 | my $epsilon=$c->param('epsilon') || 5; |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 36 | my $som=$c->param('som') || 0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 37 | my $res; |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 38 | my @lists; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 39 | my @collocations; |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 40 | if(defined($word) && $word !~ /^\s*$/) { |
| 41 | $c->inactivity_timeout(300); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 42 | $word =~ s/\s+/ /g; |
| 43 | for my $w (split(' *\| *', $word)) { |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 44 | $c->app->log->debug('Looking for neighbours of '.$w); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 45 | if($opt_i) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 46 | $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 47 | } else { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 48 | $res = get_neighbours($w, $no_nbs); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 49 | } |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 50 | push(@lists, $res->{paradigmatic}); |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 51 | } |
Marc Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 52 | } |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 53 | $word =~ s/ *\| */ | /g; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 54 | $c->render(template=>"index", word=>$word, no_nbs=>$no_nbs, no_iterations => $no_iterations, epsilon=> $epsilon, perplexity=> $perplexity, show_som=>$som, lists=> \@lists, collocators=> $res->{syntagmatic}); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 57 | $daemon->run; # app->start; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 58 | |
| 59 | exit; |
| 60 | |
| 61 | __END__ |
| 62 | |
| 63 | __C__ |
| 64 | #include <stdio.h> |
| 65 | #include <string.h> |
| 66 | #include <math.h> |
| 67 | #include <malloc.h> |
| 68 | #include <stdlib.h> //strlen |
Marc Kupietz | f080976 | 2016-02-26 10:13:47 +0100 | [diff] [blame] | 69 | #include <sys/mman.h> |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 70 | #include <pthread.h> |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 71 | |
| 72 | #define max_size 2000 |
| 73 | #define max_w 50 |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 74 | #define MAX_NEIGHBOURS 1000 |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 75 | #define MAX_WORDS -1 |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 76 | #define MAX_THREADS 100 |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 77 | #define MAX_CC 50 |
| 78 | #define EXP_TABLE_SIZE 1000 |
| 79 | #define MAX_EXP 6 |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 80 | |
| 81 | //the thread function |
| 82 | void *connection_handler(void *); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 83 | |
| 84 | typedef struct { |
| 85 | long long *index; |
| 86 | float *dist; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 87 | long long *pos; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 88 | unsigned int length; |
| 89 | } knn; |
| 90 | |
| 91 | |
| 92 | typedef struct { |
| 93 | char *token; |
| 94 | int N; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 95 | long from; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 96 | unsigned long upto; |
| 97 | } knnpars; |
| 98 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 99 | float *M, *syn1neg_window, *expTable; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 100 | char *vocab; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 101 | |
Marc Kupietz | 82b0267 | 2016-02-26 12:32:25 +0100 | [diff] [blame] | 102 | long long words, size; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 103 | int num_threads=20; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 104 | int latin_enc=0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 105 | int window; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 106 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 107 | int init_net(char *file_name, char *net_name, int latin) { |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 108 | FILE *f, *binvecs, *binwords; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 109 | int binwords_fd, binvecs_fd, net_fd, i; |
Marc Kupietz | 82b0267 | 2016-02-26 12:32:25 +0100 | [diff] [blame] | 110 | long long a, b, c, d, cn; |
| 111 | float len; |
| 112 | |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 113 | char binvecs_fname[256], binwords_fname[256]; |
| 114 | strcpy(binwords_fname, file_name); |
| 115 | strcat(binwords_fname, ".words"); |
| 116 | strcpy(binvecs_fname, file_name); |
| 117 | strcat(binvecs_fname, ".vecs"); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 118 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 119 | latin_enc = latin; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 120 | f = fopen(file_name, "rb"); |
| 121 | if (f == NULL) { |
| 122 | printf("Input file %s not found\n", file_name); |
| 123 | return -1; |
| 124 | } |
| 125 | fscanf(f, "%lld", &words); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 126 | if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 127 | fscanf(f, "%lld", &size); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 128 | if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) { |
| 129 | printf("Converting %s to memory mappable structures\n", file_name); |
Marc Kupietz | f080976 | 2016-02-26 10:13:47 +0100 | [diff] [blame] | 130 | vocab = (char *)malloc((long long)words * max_w * sizeof(char)); |
| 131 | M = (float *)malloc((long long)words * (long long)size * sizeof(float)); |
| 132 | if (M == NULL) { |
| 133 | printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size); |
| 134 | return -1; |
| 135 | } |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 136 | for (b = 0; b < words; b++) { |
| 137 | a = 0; |
| 138 | while (1) { |
| 139 | vocab[b * max_w + a] = fgetc(f); |
| 140 | if (feof(f) || (vocab[b * max_w + a] == ' ')) break; |
| 141 | if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++; |
| 142 | } |
| 143 | vocab[b * max_w + a] = 0; |
| 144 | fread(&M[b * size], sizeof(float), size, f); |
| 145 | len = 0; |
| 146 | for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size]; |
| 147 | len = sqrt(len); |
| 148 | for (a = 0; a < size; a++) M[a + b * size] /= len; |
| 149 | } |
| 150 | if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) { |
| 151 | fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs); |
| 152 | fclose(binvecs); |
| 153 | fwrite(vocab, sizeof(char), (long long)words * max_w, binwords); |
| 154 | fclose(binwords); |
| 155 | } |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 156 | } |
| 157 | if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) { |
| 158 | M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0); |
| 159 | vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0); |
| 160 | if (M == MAP_FAILED || vocab == MAP_FAILED) { |
| 161 | close(binvecs_fd); |
| 162 | close(binwords_fd); |
| 163 | fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname); |
| 164 | exit(-1); |
| 165 | } |
| 166 | } else { |
| 167 | fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname); |
| 168 | exit(-1); |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 169 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 170 | fclose(f); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 171 | |
| 172 | if(net_name) { |
| 173 | if( (net_fd = open(net_name, O_RDONLY)) >= 0) { |
| 174 | window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2; |
| 175 | // lseek(net_fd, sizeof(float) * words * size, SEEK_SET); |
| 176 | munmap(M, sizeof(float) * words * size); |
| 177 | M = mmap(0, sizeof(float) * words * size + sizeof(float) * 2 * window * size * words, PROT_READ, MAP_SHARED, net_fd, 0); |
| 178 | if (M == MAP_FAILED) { |
| 179 | close(net_fd); |
| 180 | fprintf(stderr, "Cannot mmap %s\n", net_name); |
| 181 | exit(-1); |
| 182 | } |
| 183 | syn1neg_window = M + words * size; |
| 184 | } else { |
| 185 | fprintf(stderr, "Cannot open %s\n", net_name); |
| 186 | exit(-1); |
| 187 | } |
| 188 | fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window); |
| 189 | } |
| 190 | |
| 191 | expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float)); |
| 192 | for (i = 0; i < EXP_TABLE_SIZE; i++) { |
| 193 | expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table |
| 194 | expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1) |
| 195 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 196 | return 0; |
| 197 | } |
| 198 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 199 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 200 | knn *getCollocators(int cc) { |
| 201 | knn *nbs = NULL; |
| 202 | long window_layer_size = size * window * 2; |
| 203 | long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target; |
| 204 | float f, max_f, maxmax_f; |
| 205 | float *target_sums, *bestf, worstbest; |
| 206 | long long *besti, *bestp; |
| 207 | int N = 10; |
| 208 | a = posix_memalign((void **) &target_sums, 128, words * sizeof(float)); |
| 209 | besti = malloc(N * sizeof(long long)); |
| 210 | bestp = malloc(N * sizeof(long long)); |
| 211 | bestf = malloc(N * sizeof(float)); |
| 212 | for (b = 0; b < words; b++) |
| 213 | target_sums[b]=0; |
| 214 | for (b = 0; b < N; b++) |
| 215 | bestf[b]=-1; |
| 216 | worstbest = -1; |
| 217 | d = cc; |
| 218 | maxmax_f = -1; |
| 219 | maxmax_target = 0; |
| 220 | |
| 221 | |
| 222 | for (a = window * 2 + 1; a >=0; a--) { |
| 223 | printf("window pos: %ld\n", a); |
| 224 | if (a != window) { |
| 225 | max_f = -1; |
| 226 | window_offset = a * size; |
| 227 | if (a > window) |
| 228 | window_offset -= size; |
| 229 | for(target = 0; target < words; target ++) { |
| 230 | if(target == d) |
| 231 | continue; |
| 232 | f = 0; |
| 233 | for (c = 0; c < size; c++) |
| 234 | f += M[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c]; |
| 235 | if (f < -MAX_EXP) |
| 236 | continue; |
| 237 | else if (f > MAX_EXP) |
| 238 | continue; |
| 239 | else |
| 240 | f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))]; |
| 241 | if(f > max_f) { |
| 242 | max_f = f; |
| 243 | max_target = target; |
| 244 | } |
| 245 | target_sums[target] += (1-target_sums[target]) * f; |
| 246 | if(f > worstbest) { |
| 247 | for (b = 0; b < N; b++) { |
| 248 | if (f > bestf[b]) { |
| 249 | for (e = N - 1; e > b; e--) { |
| 250 | bestf[e] = bestf[e - 1]; |
| 251 | besti[e] = besti[e - 1]; |
| 252 | bestp[e] = bestp[e - 1]; |
| 253 | } |
| 254 | bestf[b] = f; |
| 255 | besti[b] = target; |
| 256 | bestp[b] = window-a; |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | worstbest = bestf[N-1]; |
| 261 | } |
| 262 | } |
| 263 | printf("%d %.2f\n", max_target, max_f); |
| 264 | printf("%s (%.2f) ", &vocab[max_target * max_w], max_f); |
| 265 | if(max_f > maxmax_f) { |
| 266 | maxmax_f = max_f; |
| 267 | maxmax_target = max_target; |
| 268 | } |
| 269 | } else { |
| 270 | printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]); |
| 271 | } |
| 272 | } |
| 273 | max_f = -1; |
| 274 | for (b = 0; b < words; b++) { |
| 275 | if(target_sums[b] > max_f) { |
| 276 | max_f = target_sums[b]; |
| 277 | max_target = b; |
| 278 | } |
| 279 | } |
| 280 | printf(" -- max sum: %s (%.2f), max resp.: \x1b[1m%s\x1b[0m (%.2f)\n", |
| 281 | &vocab[max_target * max_w], max_f, |
| 282 | &vocab[maxmax_target * max_w], maxmax_f); |
| 283 | for(b=0; b<N && bestf[b]>-1; b++) |
| 284 | printf("%-32s %.2f %d\n", &vocab[besti[b]*max_w], bestf[b], bestp[b]); |
| 285 | printf("\n"); |
| 286 | free(target_sums); |
| 287 | nbs = malloc(sizeof(knn)); |
| 288 | nbs->index = besti; |
| 289 | nbs->dist = bestf; |
| 290 | nbs->pos = bestp; |
| 291 | nbs->length = N; |
| 292 | return(nbs); |
| 293 | } |
| 294 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 295 | void *_get_neighbours(knnpars *pars) { |
| 296 | char *st1 = pars->token; |
| 297 | int N = pars->N; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 298 | long from = pars -> from; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 299 | unsigned long upto = pars -> upto; |
Marc Kupietz | 95aa1c0 | 2016-03-15 09:40:43 +0100 | [diff] [blame] | 300 | char file_name[max_size], st[100][max_size], sep[100]; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 301 | float dist, len, *bestd, vec[max_size]; |
| 302 | long long a, b, c, d, cn, bi[100], *besti; |
Marc Kupietz | 82b0267 | 2016-02-26 12:32:25 +0100 | [diff] [blame] | 303 | char ch; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 304 | knn *nbs = NULL; |
Marc Kupietz | 34a3ee9 | 2016-02-27 22:43:16 +0100 | [diff] [blame] | 305 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 306 | besti = malloc(N * sizeof(long long)); |
| 307 | bestd = malloc(N * sizeof(float)); |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 308 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 309 | float worstbest=-1; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 310 | |
| 311 | for (a = 0; a < N; a++) bestd[a] = 0; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 312 | a = 0; |
| 313 | cn = 0; |
| 314 | b = 0; |
| 315 | c = 0; |
| 316 | while (1) { |
| 317 | st[cn][b] = st1[c]; |
| 318 | b++; |
| 319 | c++; |
| 320 | st[cn][b] = 0; |
| 321 | if (st1[c] == 0) break; |
Marc Kupietz | 95aa1c0 | 2016-03-15 09:40:43 +0100 | [diff] [blame] | 322 | if (st1[c] == ' ' || st1[c] == '-') { |
| 323 | sep[cn++] = st1[c]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 324 | b = 0; |
| 325 | c++; |
| 326 | } |
| 327 | } |
| 328 | cn++; |
| 329 | for (a = 0; a < cn; a++) { |
Marc Kupietz | 34a3ee9 | 2016-02-27 22:43:16 +0100 | [diff] [blame] | 330 | for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break; |
| 331 | if (b == words) b = -1; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 332 | bi[a] = b; |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 333 | fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], bi[a]); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 334 | if(from < 0) { |
| 335 | nbs = getCollocators(b); |
| 336 | pthread_exit(nbs); |
| 337 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 338 | if (b == -1) { |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 339 | fprintf(stderr, "Out of dictionary word!\n"); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 340 | cn--; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 341 | break; |
| 342 | } |
| 343 | } |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 344 | if (b == -1) { |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 345 | N = 0; |
| 346 | goto end; |
| 347 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 348 | for (a = 0; a < size; a++) vec[a] = 0; |
| 349 | for (b = 0; b < cn; b++) { |
| 350 | if (bi[b] == -1) continue; |
Marc Kupietz | 95aa1c0 | 2016-03-15 09:40:43 +0100 | [diff] [blame] | 351 | if(b>0 && sep[b-1] == '-') |
| 352 | for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size]; |
| 353 | else |
| 354 | for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 355 | } |
| 356 | len = 0; |
| 357 | for (a = 0; a < size; a++) len += vec[a] * vec[a]; |
| 358 | len = sqrt(len); |
| 359 | for (a = 0; a < size; a++) vec[a] /= len; |
| 360 | for (a = 0; a < N; a++) bestd[a] = -1; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 361 | for (c = from; c < upto; c++) { |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 362 | a = 0; |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 363 | // do not skip taget word |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 364 | // for (b = 0; b < cn; b++) if (bi[b] == c) a = 1; |
| 365 | // if (a == 1) continue; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 366 | dist = 0; |
| 367 | for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size]; |
Marc Kupietz | be1b9fc | 2016-02-26 10:34:30 +0100 | [diff] [blame] | 368 | if(dist > worstbest) { |
| 369 | for (a = 0; a < N; a++) { |
| 370 | if (dist > bestd[a]) { |
| 371 | for (d = N - 1; d > a; d--) { |
| 372 | bestd[d] = bestd[d - 1]; |
| 373 | besti[d] = besti[d - 1]; |
| 374 | } |
| 375 | bestd[a] = dist; |
| 376 | besti[a] = c; |
| 377 | break; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 378 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 379 | } |
Marc Kupietz | be1b9fc | 2016-02-26 10:34:30 +0100 | [diff] [blame] | 380 | worstbest = bestd[N-1]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 381 | } |
| 382 | } |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 383 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 384 | nbs = malloc(sizeof(knn)); |
| 385 | nbs->index = besti; |
| 386 | nbs->dist = bestd; |
| 387 | nbs->length = N; |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 388 | end: |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 389 | pthread_exit(nbs); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 390 | } |
| 391 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 392 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 393 | SV *get_neighbours(char *st1, int N) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 394 | HV *result = newHV(); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 395 | float bestd[MAX_NEIGHBOURS], vec[max_size]; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 396 | long long besti[MAX_NEIGHBOURS], bestp[MAX_NEIGHBOURS], a, b, c, d, slice; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 397 | char *bestw[MAX_NEIGHBOURS]; |
| 398 | knn *nbs[MAX_THREADS]; |
| 399 | knnpars pars[MAX_THREADS]; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 400 | pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t)); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 401 | |
| 402 | if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS; |
| 403 | |
| 404 | slice = words / num_threads; |
| 405 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 406 | a = num_threads; |
| 407 | pars[a].token = st1; |
| 408 | pars[a].N = N; |
| 409 | pars[a].from = -1; |
| 410 | pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]); |
| 411 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 412 | for(a=0; a < num_threads; a++) { |
| 413 | pars[a].token = st1; |
| 414 | pars[a].N = N; |
| 415 | pars[a].from = a*slice; |
| 416 | pars[a].upto = ((a+1)*slice > words? words:(a+1)*slice); |
| 417 | pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]); |
| 418 | } |
| 419 | for (a = 0; a < num_threads; a++) pthread_join(pt[a], &nbs[a]); |
| 420 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 421 | pthread_join(pt[a], &nbs[a]); |
| 422 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 423 | if(!nbs[0]) |
| 424 | goto end; |
| 425 | |
| 426 | for(b=0; b < N; b++) { |
| 427 | besti[b] = nbs[0]->index[b]; |
| 428 | bestd[b] = nbs[0]->dist[b]; |
| 429 | } |
| 430 | |
| 431 | for(a=1; a < num_threads; a++) { |
| 432 | for(b=0; b < N; b++) { |
| 433 | for(c=0; c < N; c++) { |
| 434 | if(nbs[a]->dist[b] > bestd[c]) { |
| 435 | for(d=N-1; d>c; d--) { |
| 436 | bestd[d] = bestd[d-1]; |
| 437 | besti[d] = besti[d-1]; |
| 438 | } |
| 439 | besti[c] = nbs[a]->index[b]; |
| 440 | bestd[c] = nbs[a]->dist[b]; |
| 441 | break; |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 447 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 448 | if(nbs) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 449 | AV* array = newAV(); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 450 | for (a = 0; a < N; a++) { |
| 451 | bestw[a] = (char *)malloc(max_size * sizeof(char)); |
| 452 | } |
| 453 | for (a = 0; a < N; a++) { |
| 454 | strcpy(bestw[a], &vocab[besti[a] * max_w]); |
| 455 | HV* hash = newHV(); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 456 | SV* word = newSVpvf(bestw[a], 0); |
| 457 | if(latin_enc == 0) SvUTF8_on(word); |
| 458 | hv_store(hash, "word", strlen("word"), word , 0); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 459 | hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0); |
| 460 | hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0); |
| 461 | AV *vector = newAV(); |
| 462 | for (b = 0; b < size; b++) { |
| 463 | av_push(vector, newSVnv(M[b + besti[a] * size])); |
| 464 | } |
| 465 | hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0); |
| 466 | av_push(array, newRV_noinc((SV*)hash)); |
| 467 | } |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 468 | hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0); |
| 469 | |
| 470 | for(b=0; b < 10; b++) { |
| 471 | besti[b] = nbs[num_threads]->index[b]; |
| 472 | bestd[b] = nbs[num_threads]->dist[b]; |
| 473 | bestp[b] = nbs[num_threads]->pos[b]; |
| 474 | } |
| 475 | array = newAV(); |
| 476 | for (a = 0; a < 10; a++) { |
| 477 | strcpy(bestw[a], &vocab[besti[a] * max_w]); |
| 478 | HV* hash = newHV(); |
| 479 | SV* word = newSVpvf(bestw[a], 0); |
| 480 | if(latin_enc == 0) SvUTF8_on(word); |
| 481 | hv_store(hash, "word", strlen("word"), word , 0); |
| 482 | hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0); |
| 483 | hv_store(hash, "pos", strlen("pos"), newSVnv(bestp[a]), 0); |
| 484 | av_push(array, newRV_noinc((SV*)hash)); |
| 485 | } |
| 486 | hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 487 | } |
| 488 | end: |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 489 | return newRV_noinc((SV*)result); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 490 | } |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 491 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 492 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 493 | __DATA__ |
| 494 | |
| 495 | @@ index.html.ep |
| 496 | <!DOCTYPE html> |
| 497 | <html> |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 498 | <head> |
| 499 | <title>DeReKo-Word-Vector-Distances</title> |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 500 | <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 501 | <script src="http://code.jquery.com/jquery-latest.min.js"></script> |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 502 | <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> |
| 503 | <script> |
| 504 | $(function() { |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 505 | $( document ).tooltip({ |
| 506 | content: function() { |
| 507 | return $(this).attr('title'); |
| 508 | }} |
| 509 | ) |
| 510 | }) |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 511 | </script> |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 512 | <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script> |
| 513 | <script src="http://klinux10/word2vec/tsne.js"></script> |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 514 | <script src="http://klinux10/word2vec/som.js"></script> |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 515 | <script src="http://klinux10/word2vec/labeler.js"></script> |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 516 | <style> |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 517 | body, input { |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 518 | font-family: Arial, sans-serif; |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 519 | font-size: 11pt; |
| 520 | } |
| 521 | |
| 522 | .ui-tooltip-content { |
| 523 | font-size: 9pt; |
| 524 | colour: #222222; |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 525 | } |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 526 | |
| 527 | svg > .ui-tooltip-content { |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 528 | font-size: 8pt; |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 529 | colour: #222222; |
| 530 | } |
| 531 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 532 | #collocators { |
| 533 | margin-bottom: 15px; |
| 534 | } |
| 535 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 536 | #wrapper { |
| 537 | width: 100%; |
| 538 | // border: 1px solid red; |
| 539 | overflow: hidden; /* will contain if #first is longer than #second */ |
| 540 | } |
| 541 | #first { |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 542 | margin-right: 20px; |
| 543 | float: left; |
| 544 | // border: 1px solid green; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 545 | } |
| 546 | #second { |
| 547 | border: 1px solid #333; |
| 548 | overflow: hidden; /* if you don't want #second to wrap below #first */ |
| 549 | } |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 550 | #som2 svg { |
| 551 | border: 1px solid #333; |
| 552 | } |
| 553 | |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 554 | #cost { |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 555 | font-size: 8pt; |
| 556 | color: #222222; |
| 557 | margin-top: 4px; |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 558 | margin-bottom: 12px; |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 559 | } |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 560 | |
Marc Kupietz | 6c1ca44 | 2016-03-03 09:35:18 +0100 | [diff] [blame] | 561 | #sominfo1, #sominfo { |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 562 | font-size: 8pt; |
| 563 | color: #222222; |
| 564 | margin-top: 0px; |
| 565 | } |
| 566 | |
Marc Kupietz | 6c1ca44 | 2016-03-03 09:35:18 +0100 | [diff] [blame] | 567 | #somcolor1, #somcolor2, #somcolor3 { |
| 568 | display: inline-block; |
| 569 | height: 10px; |
| 570 | width: 10px; |
| 571 | } |
| 572 | |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 573 | #third { |
| 574 | border: 1px solid #333; |
| 575 | } |
| 576 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 577 | </style> |
| 578 | <script> |
| 579 | |
Marc Kupietz | c4d62f8 | 2016-03-01 11:04:24 +0100 | [diff] [blame] | 580 | var opt = {epsilon: <%= $epsilon %>, perplexity: <%= $perplexity %>}, |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 581 | mapWidth = 800, // width map |
| 582 | mapHeight = 800, |
| 583 | jitterRadius = 7; |
| 584 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 585 | var T = new tsnejs.tSNE(opt); // create a tSNE instance |
| 586 | |
| 587 | var Y; |
| 588 | |
| 589 | var data; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 590 | var labeler; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 591 | |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 592 | |
| 593 | function applyJitter() { |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 594 | svg.selectAll('.tsnet') |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 595 | .data(labels) |
| 596 | .transition() |
| 597 | .duration(50) |
| 598 | .attr("transform", function(d, i) { |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 599 | T.Y[i][0] = (d.x - mapWidth/2 - tx)/ss/20; |
| 600 | T.Y[i][1] = (d.y - mapHeight/2 - ty)/ss/20; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 601 | return "translate(" + |
| 602 | (d.x) + "," + |
| 603 | (d.y) + ")"; |
| 604 | }); |
| 605 | } |
| 606 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 607 | function updateEmbedding() { |
| 608 | var Y = T.getSolution(); |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 609 | svg.selectAll('.tsnet') |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 610 | .data(data.words) |
| 611 | .attr("transform", function(d, i) { |
| 612 | return "translate(" + |
| 613 | ((Y[i][0]*20*ss + tx) + mapWidth/2) + "," + |
| 614 | ((Y[i][1]*20*ss + ty) + mapHeight/2) + ")"; }); |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | var svg; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 618 | var labels = []; |
| 619 | var anchor_array = []; |
| 620 | var text; |
| 621 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 622 | function drawEmbedding() { |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 623 | $("#embed").empty(); |
| 624 | var div = d3.select("#embed"); |
| 625 | |
| 626 | // get min and max in each column of Y |
| 627 | var Y = T.Y; |
| 628 | |
| 629 | svg = div.append("svg") // svg is global |
| 630 | .attr("width", mapWidth) |
| 631 | .attr("height", mapHeight); |
| 632 | |
| 633 | var g = svg.selectAll(".b") |
| 634 | .data(data.words) |
| 635 | .enter().append("g") |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 636 | .attr("class", "tsnet"); |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 637 | |
| 638 | g.append("a") |
| 639 | .attr("xlink:href", function(word) {return "/?word="+word;}) |
| 640 | .attr("title", function(d, i) { |
Marc Kupietz | e50c816 | 2016-03-01 10:24:43 +0100 | [diff] [blame] | 641 | return "rank: "+i +" "+"freq. rank: "+data.ranks[i].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 642 | }) |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 643 | .append("text") |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 644 | .attr("text-anchor", "top") |
| 645 | .attr("font-size", 12) |
| 646 | .attr("fill", function(d) { |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 647 | if(data.target.indexOf(" "+d+" ") >= 0) { |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 648 | return "red"; |
| 649 | } else { |
| 650 | return "#333" |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 651 | } |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 652 | }) |
| 653 | .text(function(d) { return d; }); |
| 654 | |
| 655 | var zoomListener = d3.behavior.zoom() |
| 656 | .scaleExtent([0.1, 10]) |
| 657 | .center([0,0]) |
| 658 | .on("zoom", zoomHandler); |
| 659 | zoomListener(svg); |
| 660 | } |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 661 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 662 | var tx=0, ty=0; |
| 663 | var ss=1; |
| 664 | var iter_id=-1; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 665 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 666 | function zoomHandler() { |
| 667 | tx = d3.event.translate[0]; |
| 668 | ty = d3.event.translate[1]; |
| 669 | ss = d3.event.scale; |
| 670 | updateEmbedding(); |
| 671 | } |
| 672 | |
| 673 | var stepnum = 0; |
| 674 | |
| 675 | function stopStep() { |
| 676 | clearInterval(iter_id); |
| 677 | text = svg.selectAll("text"); |
| 678 | |
| 679 | // jitter function needs different data and co-ordinate representation |
| 680 | labels = d3.range(data.words.length).map(function(i) { |
| 681 | var x = (T.Y[i][0]*20*ss + tx) + mapWidth/2; |
| 682 | var y = (T.Y[i][1]*20*ss + ty) + mapHeight/2; |
| 683 | anchor_array.push({x: x, y: y, r: jitterRadius}); |
| 684 | return { |
| 685 | x: x, |
| 686 | y: y, |
| 687 | name: data.words[i] |
| 688 | }; |
| 689 | }); |
| 690 | |
| 691 | // get the actual label bounding boxes for the jitter function |
| 692 | var index = 0; |
| 693 | text.each(function() { |
| 694 | labels[index].width = this.getBBox().width; |
| 695 | labels[index].height = this.getBBox().height; |
| 696 | index += 1; |
| 697 | }); |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 698 | |
| 699 | |
| 700 | // setTimeout(updateEmbedding, 1); |
| 701 | // setTimeout( |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 702 | labeler = d3.labeler() |
| 703 | .label(labels) |
| 704 | .anchor(anchor_array) |
| 705 | .width(mapWidth) |
| 706 | .height(mapHeight) |
| 707 | .update(applyJitter); |
| 708 | // .start(1000); |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 709 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 710 | iter_id = setInterval(jitterStep, 1); |
| 711 | } |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 712 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 713 | var jitter_i=0; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 714 | |
| 715 | function jitterStep() { |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 716 | if(jitter_i++ > 100) { |
| 717 | clearInterval(iter_id); |
| 718 | } else { |
| 719 | labeler.start2(10); |
| 720 | applyJitter(); |
| 721 | } |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 722 | } |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 723 | |
| 724 | var last_cost=1000; |
| 725 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 726 | function step() { |
| 727 | var i = T.iter; |
| 728 | |
| 729 | if(i > <%= $no_iterations %>) { |
| 730 | stopStep(); |
| 731 | } else { |
| 732 | var cost = Math.round(T.step() * 100000) / 100000; // do a few steps |
| 733 | $("#cost").html("tsne iteration " + i + ", cost: " + cost.toFixed(5)); |
| 734 | if(i % 250 == 0 && cost >= last_cost) { |
| 735 | stopStep(); |
| 736 | } else { |
| 737 | last_cost = cost; |
| 738 | updateEmbedding(); |
| 739 | } |
| 740 | } |
| 741 | } |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 742 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 743 | function showMap(j) { |
| 744 | data=j; |
| 745 | T.iter=0; |
| 746 | T.initDataRaw(data.vecs); // init embedding |
| 747 | drawEmbedding(); // draw initial embedding |
| 748 | |
| 749 | if(iter_id >= 0) { |
| 750 | clearInterval(iter_id); |
| 751 | } |
| 752 | //T.debugGrad(); |
| 753 | iter_id = setInterval(step, 1); |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 754 | if(<%= $show_som %>) { |
| 755 | makeSOM(j, <%= $no_iterations %>); |
| 756 | } |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 757 | } |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 758 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 759 | </script> |
| 760 | </head> |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 761 | <body> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 762 | <form action="<%=url_for('/')->to_abs%>" method="GET"> |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 763 | word(s): |
| 764 | <input type="text" name="word" size="20" value="<%= $word %>" title="When looking for multiple words use spaces as separators to search around the average vector and | as separator to get the neighbours for each word."> |
| 765 | max. neighbours: <input type="text" size="8" name="n" value="<%= $no_nbs %>"> |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 766 | max. iterations: <input type="text" name="N" size="8" value="<%= $no_iterations %>"> |
| 767 | SOM <input type="checkbox" name="som" value="1" <%= ($show_som ? "checked" : "") %>> |
| 768 | <span> </span><input type="submit" value="Show"> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 769 | </form> |
| 770 | <br> |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame^] | 771 | % if($collocators) { |
| 772 | <div id="collocators"> |
| 773 | % for my $item (@$collocators) { |
| 774 | <i><%= $item->{word} %></i> (<%= $item->{pos} %>: <%= sprintf("%.2f", $item->{dist}) %>) |
| 775 | % } |
| 776 | </div> |
| 777 | % } |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 778 | % if($lists) { |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 779 | <div id="wrapper"> |
| 780 | <table id="first"> |
| 781 | <tr> |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 782 | <th align="right">Pos.</th><th align="left">Word</th><th align="right">Cosine dist.</th><th>Freq. rank</th> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 783 | </tr> |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 784 | % my $j=0; my @words; my @vecs; my @ranks; for my $list (@$lists) { |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 785 | % my $i=1; for my $item (@$list) { |
| 786 | % if(!grep{$_ eq $item->{word}} @words) { |
| 787 | % push @vecs, $item->{vector}; |
| 788 | % push @words, $item->{word}; |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 789 | % push @ranks, $item->{rank}; |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 790 | % } |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 791 | <tr> |
| 792 | <td align="right"> |
| 793 | <%= $i++ %>. |
| 794 | </td> |
| 795 | <td> |
| 796 | <a href="/?word=<%= $item->{word} %>"> |
| 797 | <%= $item->{word} %> |
| 798 | </a> |
| 799 | </td> |
| 800 | <td align="right"> |
| 801 | <%= sprintf("%.3f", $item->{dist}) %> |
| 802 | </td> |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 803 | <td align="right"> |
| 804 | <%= $item->{rank} %> |
| 805 | </td> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 806 | </tr> |
| 807 | % } |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 808 | % } |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 809 | </table> |
| 810 | <script> |
| 811 | % use Mojo::ByteStream 'b'; |
| 812 | $(window).load(function() { |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 813 | showMap(<%= b(Mojo::JSON::to_json({target => " $word ", words => \@words, vecs => \@vecs, ranks => \@ranks})); %>); |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 814 | }); |
| 815 | </script> |
| 816 | % } |
| 817 | <div id="second" style="width:800px; height:800px; font-family: arial;"> |
| 818 | <div id="embed"> |
| 819 | </div> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 820 | </div> |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 821 | <div id="cost"></div> |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 822 | % if($show_som) { |
| 823 | <div id="som2"> |
| 824 | </div> |
Marc Kupietz | 6c1ca44 | 2016-03-03 09:35:18 +0100 | [diff] [blame] | 825 | <div id="sominfo1"><span id="somcolor1"> </span> <span id="somword1"> </span> <span id="somcolor2"> </span> <span id="somword2"> </span> <span id="somcolor3"> </span></div> |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 826 | <div id="sominfo">SOM iteration <span id="iterations">0</span></div> |
| 827 | % } |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 828 | </div> |
| 829 | <p> |
| 830 | Word vector model based on DeReKo-2015-II. Trained with <a href="https://code.google.com/p/word2vec/">word2vec</a> using the following parameters:</p> |
Marc Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 831 | <pre> |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 832 | -cbow 1 -size 300 -window 7 -negative 5 -hs 0 -sample 1e-5 -threads 44 -binary 1 -iter 5 |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 833 | </pre> |
| 834 | </p> |
| 835 | </body> |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 836 | </html> |
| 837 | |