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