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 | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 6 | use Mojo::Server::Daemon; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 7 | |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 8 | print STDERR $ARGV[0]; |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 9 | # -cbow 1 -size 200 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 40 -binary 1 -iter 15 |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 10 | init_net("vectors15.bin"); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 11 | |
| 12 | get '/' => sub { |
| 13 | my $c = shift; |
| 14 | my $word=$c->param('word'); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 15 | my $no_nbs=$c->param('n') || 100; |
| 16 | my $no_iterations=$c->param('N') || 2000; |
Marc Kupietz | c4d62f8 | 2016-03-01 11:04:24 +0100 | [diff] [blame^] | 17 | my $perplexity=$c->param('perplexity') || 25; |
| 18 | my $epsilon=$c->param('epsilon') || 5; |
| 19 | |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 20 | my @lists; |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 21 | if(defined($word) && $word !~ /^\s*$/) { |
| 22 | $c->inactivity_timeout(300); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 23 | $word =~ s/\s+/ /g; |
| 24 | for my $w (split(' *\| *', $word)) { |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 25 | $c->app->log->debug('Looking for neighbours of '.$w); |
| 26 | push(@lists, get_neighbours(encode("iso-8859-1", $w), $no_nbs)); |
| 27 | } |
Marc Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 28 | } |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 29 | $word =~ s/ *\| */ | /g; |
Marc Kupietz | c4d62f8 | 2016-03-01 11:04:24 +0100 | [diff] [blame^] | 30 | $c->render(template=>"index", word=>$word, no_nbs=>$no_nbs, no_iterations => $no_iterations, epsilon=> $epsilon, perplexity=> $perplexity, lists=> \@lists); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | app->start; |
| 34 | |
| 35 | exit; |
| 36 | |
| 37 | __END__ |
| 38 | |
| 39 | __C__ |
| 40 | #include <stdio.h> |
| 41 | #include <string.h> |
| 42 | #include <math.h> |
| 43 | #include <malloc.h> |
| 44 | #include <stdlib.h> //strlen |
Marc Kupietz | f080976 | 2016-02-26 10:13:47 +0100 | [diff] [blame] | 45 | #include <sys/mman.h> |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 46 | #include <pthread.h> |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 47 | |
| 48 | #define max_size 2000 |
| 49 | #define max_w 50 |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 50 | #define MAX_NEIGHBOURS 1000 |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 51 | #define MAX_WORDS -1 |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 52 | #define MAX_THREADS 100 |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 53 | |
| 54 | //the thread function |
| 55 | void *connection_handler(void *); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 56 | |
| 57 | typedef struct { |
| 58 | long long *index; |
| 59 | float *dist; |
| 60 | unsigned int length; |
| 61 | } knn; |
| 62 | |
| 63 | |
| 64 | typedef struct { |
| 65 | char *token; |
| 66 | int N; |
| 67 | unsigned long from; |
| 68 | unsigned long upto; |
| 69 | } knnpars; |
| 70 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 71 | float *M; |
| 72 | char *vocab; |
Marc Kupietz | 82b0267 | 2016-02-26 12:32:25 +0100 | [diff] [blame] | 73 | long long words, size; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 74 | int num_threads=20; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 75 | |
| 76 | int init_net(char *file_name) { |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 77 | FILE *f, *binvecs, *binwords; |
Marc Kupietz | f080976 | 2016-02-26 10:13:47 +0100 | [diff] [blame] | 78 | int binwords_fd, binvecs_fd; |
Marc Kupietz | 82b0267 | 2016-02-26 12:32:25 +0100 | [diff] [blame] | 79 | long long a, b, c, d, cn; |
| 80 | float len; |
| 81 | |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 82 | char binvecs_fname[256], binwords_fname[256]; |
| 83 | strcpy(binwords_fname, file_name); |
| 84 | strcat(binwords_fname, ".words"); |
| 85 | strcpy(binvecs_fname, file_name); |
| 86 | strcat(binvecs_fname, ".vecs"); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 87 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 88 | f = fopen(file_name, "rb"); |
| 89 | if (f == NULL) { |
| 90 | printf("Input file %s not found\n", file_name); |
| 91 | return -1; |
| 92 | } |
| 93 | fscanf(f, "%lld", &words); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 94 | if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 95 | fscanf(f, "%lld", &size); |
Marc Kupietz | f080976 | 2016-02-26 10:13:47 +0100 | [diff] [blame] | 96 | if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) { |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 97 | M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0); |
| 98 | vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0); |
Marc Kupietz | f080976 | 2016-02-26 10:13:47 +0100 | [diff] [blame] | 99 | if (M == MAP_FAILED || vocab == MAP_FAILED) { |
| 100 | close(binvecs_fd); |
| 101 | close(binwords_fd); |
| 102 | fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname); |
| 103 | exit(-1); |
| 104 | } |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 105 | } else { |
Marc Kupietz | f080976 | 2016-02-26 10:13:47 +0100 | [diff] [blame] | 106 | vocab = (char *)malloc((long long)words * max_w * sizeof(char)); |
| 107 | M = (float *)malloc((long long)words * (long long)size * sizeof(float)); |
| 108 | if (M == NULL) { |
| 109 | printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size); |
| 110 | return -1; |
| 111 | } |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 112 | for (b = 0; b < words; b++) { |
| 113 | a = 0; |
| 114 | while (1) { |
| 115 | vocab[b * max_w + a] = fgetc(f); |
| 116 | if (feof(f) || (vocab[b * max_w + a] == ' ')) break; |
| 117 | if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++; |
| 118 | } |
| 119 | vocab[b * max_w + a] = 0; |
| 120 | fread(&M[b * size], sizeof(float), size, f); |
| 121 | len = 0; |
| 122 | for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size]; |
| 123 | len = sqrt(len); |
| 124 | for (a = 0; a < size; a++) M[a + b * size] /= len; |
| 125 | } |
| 126 | if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) { |
| 127 | fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs); |
| 128 | fclose(binvecs); |
| 129 | fwrite(vocab, sizeof(char), (long long)words * max_w, binwords); |
| 130 | fclose(binwords); |
| 131 | } |
| 132 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 133 | fclose(f); |
| 134 | return 0; |
| 135 | } |
| 136 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 137 | |
| 138 | void *_get_neighbours(knnpars *pars) { |
| 139 | char *st1 = pars->token; |
| 140 | int N = pars->N; |
| 141 | unsigned long from = pars -> from; |
| 142 | unsigned long upto = pars -> upto; |
Marc Kupietz | 82b0267 | 2016-02-26 12:32:25 +0100 | [diff] [blame] | 143 | char file_name[max_size], st[100][max_size]; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 144 | float dist, len, *bestd, vec[max_size]; |
| 145 | long long a, b, c, d, cn, bi[100], *besti; |
Marc Kupietz | 82b0267 | 2016-02-26 12:32:25 +0100 | [diff] [blame] | 146 | char ch; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 147 | knn *nbs = NULL; |
Marc Kupietz | 34a3ee9 | 2016-02-27 22:43:16 +0100 | [diff] [blame] | 148 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 149 | besti = malloc(N * sizeof(long long)); |
| 150 | bestd = malloc(N * sizeof(float)); |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 151 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 152 | float worstbest=-1; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 153 | |
| 154 | for (a = 0; a < N; a++) bestd[a] = 0; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 155 | a = 0; |
| 156 | cn = 0; |
| 157 | b = 0; |
| 158 | c = 0; |
| 159 | while (1) { |
| 160 | st[cn][b] = st1[c]; |
| 161 | b++; |
| 162 | c++; |
| 163 | st[cn][b] = 0; |
| 164 | if (st1[c] == 0) break; |
| 165 | if (st1[c] == ' ') { |
| 166 | cn++; |
| 167 | b = 0; |
| 168 | c++; |
| 169 | } |
| 170 | } |
| 171 | cn++; |
| 172 | for (a = 0; a < cn; a++) { |
Marc Kupietz | 34a3ee9 | 2016-02-27 22:43:16 +0100 | [diff] [blame] | 173 | for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break; |
| 174 | if (b == words) b = -1; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 175 | bi[a] = b; |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 176 | fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], bi[a]); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 177 | if (b == -1) { |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 178 | fprintf(stderr, "Out of dictionary word!\n"); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 179 | cn--; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 180 | break; |
| 181 | } |
| 182 | } |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 183 | if (b == -1) { |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 184 | N = 0; |
| 185 | goto end; |
| 186 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 187 | for (a = 0; a < size; a++) vec[a] = 0; |
| 188 | for (b = 0; b < cn; b++) { |
| 189 | if (bi[b] == -1) continue; |
| 190 | for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size]; |
| 191 | } |
| 192 | len = 0; |
| 193 | for (a = 0; a < size; a++) len += vec[a] * vec[a]; |
| 194 | len = sqrt(len); |
| 195 | for (a = 0; a < size; a++) vec[a] /= len; |
| 196 | for (a = 0; a < N; a++) bestd[a] = -1; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 197 | for (c = from; c < upto; c++) { |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 198 | a = 0; |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 199 | // do not skip taget word |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 200 | // for (b = 0; b < cn; b++) if (bi[b] == c) a = 1; |
| 201 | // if (a == 1) continue; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 202 | dist = 0; |
| 203 | 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] | 204 | if(dist > worstbest) { |
| 205 | for (a = 0; a < N; a++) { |
| 206 | if (dist > bestd[a]) { |
| 207 | for (d = N - 1; d > a; d--) { |
| 208 | bestd[d] = bestd[d - 1]; |
| 209 | besti[d] = besti[d - 1]; |
| 210 | } |
| 211 | bestd[a] = dist; |
| 212 | besti[a] = c; |
| 213 | break; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 214 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 215 | } |
Marc Kupietz | be1b9fc | 2016-02-26 10:34:30 +0100 | [diff] [blame] | 216 | worstbest = bestd[N-1]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 217 | } |
| 218 | } |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 219 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 220 | nbs = malloc(sizeof(knn)); |
| 221 | nbs->index = besti; |
| 222 | nbs->dist = bestd; |
| 223 | nbs->length = N; |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 224 | end: |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 225 | pthread_exit(nbs); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 226 | } |
| 227 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 228 | SV *get_neighbours(char *st1, int N) { |
| 229 | float bestd[MAX_NEIGHBOURS], vec[max_size]; |
| 230 | long long besti[MAX_NEIGHBOURS], a, b, c, d, slice; |
| 231 | char *bestw[MAX_NEIGHBOURS]; |
| 232 | knn *nbs[MAX_THREADS]; |
| 233 | knnpars pars[MAX_THREADS]; |
| 234 | pthread_t *pt = (pthread_t *)malloc(num_threads * sizeof(pthread_t)); |
| 235 | AV* array = newAV(); |
| 236 | |
| 237 | if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS; |
| 238 | |
| 239 | slice = words / num_threads; |
| 240 | |
| 241 | for(a=0; a < num_threads; a++) { |
| 242 | pars[a].token = st1; |
| 243 | pars[a].N = N; |
| 244 | pars[a].from = a*slice; |
| 245 | pars[a].upto = ((a+1)*slice > words? words:(a+1)*slice); |
| 246 | pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]); |
| 247 | } |
| 248 | for (a = 0; a < num_threads; a++) pthread_join(pt[a], &nbs[a]); |
| 249 | |
| 250 | if(!nbs[0]) |
| 251 | goto end; |
| 252 | |
| 253 | for(b=0; b < N; b++) { |
| 254 | besti[b] = nbs[0]->index[b]; |
| 255 | bestd[b] = nbs[0]->dist[b]; |
| 256 | } |
| 257 | |
| 258 | for(a=1; a < num_threads; a++) { |
| 259 | for(b=0; b < N; b++) { |
| 260 | for(c=0; c < N; c++) { |
| 261 | if(nbs[a]->dist[b] > bestd[c]) { |
| 262 | for(d=N-1; d>c; d--) { |
| 263 | bestd[d] = bestd[d-1]; |
| 264 | besti[d] = besti[d-1]; |
| 265 | } |
| 266 | besti[c] = nbs[a]->index[b]; |
| 267 | bestd[c] = nbs[a]->dist[b]; |
| 268 | break; |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | if(nbs) { |
| 275 | for (a = 0; a < N; a++) { |
| 276 | bestw[a] = (char *)malloc(max_size * sizeof(char)); |
| 277 | } |
| 278 | for (a = 0; a < N; a++) { |
| 279 | strcpy(bestw[a], &vocab[besti[a] * max_w]); |
| 280 | HV* hash = newHV(); |
| 281 | hv_store(hash, "word", strlen("word"), newSVpvf(bestw[a], 0), 0); |
| 282 | hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0); |
| 283 | hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0); |
| 284 | AV *vector = newAV(); |
| 285 | for (b = 0; b < size; b++) { |
| 286 | av_push(vector, newSVnv(M[b + besti[a] * size])); |
| 287 | } |
| 288 | hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0); |
| 289 | av_push(array, newRV_noinc((SV*)hash)); |
| 290 | } |
| 291 | } |
| 292 | end: |
| 293 | return newRV_noinc((SV*)array); |
| 294 | } |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 295 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 296 | __DATA__ |
| 297 | |
| 298 | @@ index.html.ep |
| 299 | <!DOCTYPE html> |
| 300 | <html> |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 301 | <head> |
| 302 | <title>DeReKo-Word-Vector-Distances</title> |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 303 | <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] | 304 | <script src="http://code.jquery.com/jquery-latest.min.js"></script> |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 305 | <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> |
| 306 | <script> |
| 307 | $(function() { |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 308 | $( document ).tooltip({ |
| 309 | content: function() { |
| 310 | return $(this).attr('title'); |
| 311 | }} |
| 312 | ) |
| 313 | }) |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 314 | </script> |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 315 | <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script> |
| 316 | <script src="http://klinux10/word2vec/tsne.js"></script> |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 317 | <script src="http://klinux10/word2vec/labeler.js"></script> |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 318 | <style> |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 319 | body, input { |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 320 | font-family: Arial, sans-serif; |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 321 | font-size: 11pt; |
| 322 | } |
| 323 | |
| 324 | .ui-tooltip-content { |
| 325 | font-size: 9pt; |
| 326 | colour: #222222; |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 327 | } |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 328 | |
| 329 | svg > .ui-tooltip-content { |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 330 | font-size: 8pt; |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 331 | colour: #222222; |
| 332 | } |
| 333 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 334 | svg { |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 335 | margin-right: 10px; |
| 336 | margin-bottom: 10px; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 337 | } |
| 338 | #wrapper { |
| 339 | width: 100%; |
| 340 | // border: 1px solid red; |
| 341 | overflow: hidden; /* will contain if #first is longer than #second */ |
| 342 | } |
| 343 | #first { |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 344 | margin-right: 20px; |
| 345 | float: left; |
| 346 | // border: 1px solid green; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 347 | } |
| 348 | #second { |
| 349 | border: 1px solid #333; |
| 350 | overflow: hidden; /* if you don't want #second to wrap below #first */ |
| 351 | } |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 352 | #cost { |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 353 | font-size: 8pt; |
| 354 | color: #222222; |
| 355 | margin-top: 4px; |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 356 | } |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 357 | </style> |
| 358 | <script> |
| 359 | |
Marc Kupietz | c4d62f8 | 2016-03-01 11:04:24 +0100 | [diff] [blame^] | 360 | var opt = {epsilon: <%= $epsilon %>, perplexity: <%= $perplexity %>}, |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 361 | mapWidth = 800, // width map |
| 362 | mapHeight = 800, |
| 363 | jitterRadius = 7; |
| 364 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 365 | var T = new tsnejs.tSNE(opt); // create a tSNE instance |
| 366 | |
| 367 | var Y; |
| 368 | |
| 369 | var data; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 370 | var labeler; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 371 | |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 372 | |
| 373 | function applyJitter() { |
| 374 | svg.selectAll('.u') |
| 375 | .data(labels) |
| 376 | .transition() |
| 377 | .duration(50) |
| 378 | .attr("transform", function(d, i) { |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 379 | T.Y[i][0] = (d.x - mapWidth/2 - tx)/ss/20; |
| 380 | T.Y[i][1] = (d.y - mapHeight/2 - ty)/ss/20; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 381 | return "translate(" + |
| 382 | (d.x) + "," + |
| 383 | (d.y) + ")"; |
| 384 | }); |
| 385 | } |
| 386 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 387 | function updateEmbedding() { |
| 388 | var Y = T.getSolution(); |
| 389 | svg.selectAll('.u') |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 390 | .data(data.words) |
| 391 | .attr("transform", function(d, i) { |
| 392 | return "translate(" + |
| 393 | ((Y[i][0]*20*ss + tx) + mapWidth/2) + "," + |
| 394 | ((Y[i][1]*20*ss + ty) + mapHeight/2) + ")"; }); |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | var svg; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 398 | var labels = []; |
| 399 | var anchor_array = []; |
| 400 | var text; |
| 401 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 402 | function drawEmbedding() { |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 403 | $("#embed").empty(); |
| 404 | var div = d3.select("#embed"); |
| 405 | |
| 406 | // get min and max in each column of Y |
| 407 | var Y = T.Y; |
| 408 | |
| 409 | svg = div.append("svg") // svg is global |
| 410 | .attr("width", mapWidth) |
| 411 | .attr("height", mapHeight); |
| 412 | |
| 413 | var g = svg.selectAll(".b") |
| 414 | .data(data.words) |
| 415 | .enter().append("g") |
| 416 | .attr("class", "u"); |
| 417 | |
| 418 | g.append("a") |
| 419 | .attr("xlink:href", function(word) {return "/?word="+word;}) |
| 420 | .attr("title", function(d, i) { |
Marc Kupietz | e50c816 | 2016-03-01 10:24:43 +0100 | [diff] [blame] | 421 | 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] | 422 | }) |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 423 | .append("text") |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 424 | .attr("text-anchor", "top") |
| 425 | .attr("font-size", 12) |
| 426 | .attr("fill", function(d) { |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 427 | if(data.target.indexOf(" "+d+" ") >= 0) { |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 428 | return "red"; |
| 429 | } else { |
| 430 | return "#333" |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 431 | } |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 432 | }) |
| 433 | .text(function(d) { return d; }); |
| 434 | |
| 435 | var zoomListener = d3.behavior.zoom() |
| 436 | .scaleExtent([0.1, 10]) |
| 437 | .center([0,0]) |
| 438 | .on("zoom", zoomHandler); |
| 439 | zoomListener(svg); |
| 440 | } |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 441 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 442 | var tx=0, ty=0; |
| 443 | var ss=1; |
| 444 | var iter_id=-1; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 445 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 446 | function zoomHandler() { |
| 447 | tx = d3.event.translate[0]; |
| 448 | ty = d3.event.translate[1]; |
| 449 | ss = d3.event.scale; |
| 450 | updateEmbedding(); |
| 451 | } |
| 452 | |
| 453 | var stepnum = 0; |
| 454 | |
| 455 | function stopStep() { |
| 456 | clearInterval(iter_id); |
| 457 | text = svg.selectAll("text"); |
| 458 | |
| 459 | // jitter function needs different data and co-ordinate representation |
| 460 | labels = d3.range(data.words.length).map(function(i) { |
| 461 | var x = (T.Y[i][0]*20*ss + tx) + mapWidth/2; |
| 462 | var y = (T.Y[i][1]*20*ss + ty) + mapHeight/2; |
| 463 | anchor_array.push({x: x, y: y, r: jitterRadius}); |
| 464 | return { |
| 465 | x: x, |
| 466 | y: y, |
| 467 | name: data.words[i] |
| 468 | }; |
| 469 | }); |
| 470 | |
| 471 | // get the actual label bounding boxes for the jitter function |
| 472 | var index = 0; |
| 473 | text.each(function() { |
| 474 | labels[index].width = this.getBBox().width; |
| 475 | labels[index].height = this.getBBox().height; |
| 476 | index += 1; |
| 477 | }); |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 478 | |
| 479 | |
| 480 | // setTimeout(updateEmbedding, 1); |
| 481 | // setTimeout( |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 482 | labeler = d3.labeler() |
| 483 | .label(labels) |
| 484 | .anchor(anchor_array) |
| 485 | .width(mapWidth) |
| 486 | .height(mapHeight) |
| 487 | .update(applyJitter); |
| 488 | // .start(1000); |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 489 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 490 | iter_id = setInterval(jitterStep, 1); |
| 491 | } |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 492 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 493 | var jitter_i=0; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 494 | |
| 495 | function jitterStep() { |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 496 | if(jitter_i++ > 100) { |
| 497 | clearInterval(iter_id); |
| 498 | } else { |
| 499 | labeler.start2(10); |
| 500 | applyJitter(); |
| 501 | } |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 502 | } |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 503 | |
| 504 | var last_cost=1000; |
| 505 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 506 | function step() { |
| 507 | var i = T.iter; |
| 508 | |
| 509 | if(i > <%= $no_iterations %>) { |
| 510 | stopStep(); |
| 511 | } else { |
| 512 | var cost = Math.round(T.step() * 100000) / 100000; // do a few steps |
| 513 | $("#cost").html("tsne iteration " + i + ", cost: " + cost.toFixed(5)); |
| 514 | if(i % 250 == 0 && cost >= last_cost) { |
| 515 | stopStep(); |
| 516 | } else { |
| 517 | last_cost = cost; |
| 518 | updateEmbedding(); |
| 519 | } |
| 520 | } |
| 521 | } |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 522 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 523 | function showMap(j) { |
| 524 | data=j; |
| 525 | T.iter=0; |
| 526 | T.initDataRaw(data.vecs); // init embedding |
| 527 | drawEmbedding(); // draw initial embedding |
| 528 | |
| 529 | if(iter_id >= 0) { |
| 530 | clearInterval(iter_id); |
| 531 | } |
| 532 | //T.debugGrad(); |
| 533 | iter_id = setInterval(step, 1); |
| 534 | //step(); |
| 535 | } |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 536 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 537 | </script> |
| 538 | </head> |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 539 | <body> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 540 | <form action="<%=url_for('/')->to_abs%>" method="GET"> |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 541 | word(s): |
| 542 | <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."> |
| 543 | max. neighbours: <input type="text" size="8" name="n" value="<%= $no_nbs %>"> |
| 544 | iterations: <input type="text" name="N" size="8" value="<%= $no_iterations %>"> |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 545 | <input type="submit" value="Show"> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 546 | </form> |
| 547 | <br> |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 548 | % if($lists) { |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 549 | <div id="wrapper"> |
| 550 | <table id="first"> |
| 551 | <tr> |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 552 | <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] | 553 | </tr> |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 554 | % 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] | 555 | % my $i=1; for my $item (@$list) { |
| 556 | % if(!grep{$_ eq $item->{word}} @words) { |
| 557 | % push @vecs, $item->{vector}; |
| 558 | % push @words, $item->{word}; |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 559 | % push @ranks, $item->{rank}; |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 560 | % } |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 561 | <tr> |
| 562 | <td align="right"> |
| 563 | <%= $i++ %>. |
| 564 | </td> |
| 565 | <td> |
| 566 | <a href="/?word=<%= $item->{word} %>"> |
| 567 | <%= $item->{word} %> |
| 568 | </a> |
| 569 | </td> |
| 570 | <td align="right"> |
| 571 | <%= sprintf("%.3f", $item->{dist}) %> |
| 572 | </td> |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 573 | <td align="right"> |
| 574 | <%= $item->{rank} %> |
| 575 | </td> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 576 | </tr> |
| 577 | % } |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 578 | % } |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 579 | </table> |
| 580 | <script> |
| 581 | % use Mojo::ByteStream 'b'; |
| 582 | $(window).load(function() { |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 583 | 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] | 584 | }); |
| 585 | </script> |
| 586 | % } |
| 587 | <div id="second" style="width:800px; height:800px; font-family: arial;"> |
| 588 | <div id="embed"> |
| 589 | </div> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 590 | </div> |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 591 | <div id="cost"></div> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 592 | </div> |
| 593 | <p> |
| 594 | 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] | 595 | <pre> |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 596 | -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] | 597 | </pre> |
| 598 | </p> |
| 599 | </body> |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 600 | </html> |
| 601 | |