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