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 | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 357 | var opt = {epsilon: 1, perplexity: 20}; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 358 | var T = new tsnejs.tSNE(opt); // create a tSNE instance |
| 359 | |
| 360 | var Y; |
| 361 | |
| 362 | var data; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 363 | var labeler; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 364 | |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 365 | |
| 366 | function applyJitter() { |
| 367 | svg.selectAll('.u') |
| 368 | .data(labels) |
| 369 | .transition() |
| 370 | .duration(50) |
| 371 | .attr("transform", function(d, i) { |
| 372 | T.Y[i][0] = (d.x - 400 - tx)/ss/20; |
| 373 | T.Y[i][1] = (d.y - 400 - ty)/ss/20; |
| 374 | return "translate(" + |
| 375 | (d.x) + "," + |
| 376 | (d.y) + ")"; |
| 377 | }); |
| 378 | } |
| 379 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 380 | function updateEmbedding() { |
| 381 | var Y = T.getSolution(); |
| 382 | svg.selectAll('.u') |
| 383 | .data(data.words) |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 384 | .attr("transform", function(d, i) { |
| 385 | return "translate(" + |
| 386 | ((Y[i][0]*20*ss + tx) + 400) + "," + |
| 387 | ((Y[i][1]*20*ss + ty) + 400) + ")"; }); |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | var svg; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 391 | var labels = []; |
| 392 | var anchor_array = []; |
| 393 | var text; |
| 394 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 395 | function drawEmbedding() { |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 396 | $("#embed").empty(); |
| 397 | var div = d3.select("#embed"); |
| 398 | |
| 399 | // get min and max in each column of Y |
| 400 | var Y = T.Y; |
| 401 | |
| 402 | svg = div.append("svg") // svg is global |
| 403 | .attr("width", 800) |
| 404 | .attr("height", 800); |
| 405 | |
| 406 | var g = svg.selectAll(".b") |
| 407 | .data(data.words) |
| 408 | .enter().append("g") |
| 409 | .attr("class", "u"); |
| 410 | |
| 411 | g.append("a") |
| 412 | .attr("xlink:href", function(word) {return "/?word="+word;}) |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 413 | .attr("title", function(d, i) { |
| 414 | return "rank: "+i +" "+"freq. rank: "+data.ranks[i]; |
| 415 | }) |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 416 | .append("text") |
| 417 | .attr("text-anchor", "top") |
| 418 | .attr("font-size", 12) |
| 419 | .attr("fill", function(d) { |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 420 | if(data.target.indexOf(" "+d+" ") >= 0) { |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 421 | return "red"; |
| 422 | } else { |
| 423 | return "#333" |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 424 | } |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 425 | }) |
| 426 | .text(function(d) { return d; }); |
| 427 | |
| 428 | var zoomListener = d3.behavior.zoom() |
| 429 | .scaleExtent([0.1, 10]) |
| 430 | .center([0,0]) |
| 431 | .on("zoom", zoomHandler); |
| 432 | zoomListener(svg); |
| 433 | } |
| 434 | |
| 435 | var tx=0, ty=0; |
| 436 | var ss=1; |
| 437 | var iter_id=-1; |
| 438 | |
| 439 | function zoomHandler() { |
| 440 | tx = d3.event.translate[0]; |
| 441 | ty = d3.event.translate[1]; |
| 442 | ss = d3.event.scale; |
| 443 | updateEmbedding(); |
| 444 | } |
| 445 | |
| 446 | var stepnum = 0; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 447 | |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 448 | function stopStep() { |
| 449 | clearInterval(iter_id); |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 450 | text = svg.selectAll("text"); |
| 451 | |
| 452 | labels = d3.range(data.words.length).map(function(i) { |
| 453 | var x = (T.Y[i][0]*20*ss + tx) + 400; |
| 454 | var y = (T.Y[i][1]*20*ss + ty) + 400; |
| 455 | anchor_array.push({x: x, y: y, r: 5}); |
| 456 | return { |
| 457 | x: x, |
| 458 | y: y, |
| 459 | name: data.words[i] |
| 460 | }; |
| 461 | }); |
| 462 | |
| 463 | var index = 0; |
| 464 | text.each(function() { |
| 465 | labels[index].width = this.getBBox().width; |
| 466 | labels[index].height = this.getBBox().height; |
| 467 | index += 1; |
| 468 | }); |
| 469 | |
| 470 | |
| 471 | // setTimeout(updateEmbedding, 1); |
| 472 | // setTimeout( |
| 473 | labeler = d3.labeler() |
| 474 | .label(labels) |
| 475 | .anchor(anchor_array) |
| 476 | .width(800) |
| 477 | .height(800) |
| 478 | .update(applyJitter); |
| 479 | // .start(1000); |
| 480 | |
| 481 | iter_id = setInterval(jitterStep, 1); |
| 482 | |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 483 | } |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 484 | |
| 485 | var jitter_i=0;; |
| 486 | |
| 487 | function jitterStep() { |
| 488 | if(jitter_i++ > 100) { |
| 489 | clearInterval(iter_id); |
| 490 | } else { |
| 491 | labeler.start2(10); |
| 492 | applyJitter(); |
| 493 | } |
| 494 | } |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 495 | |
| 496 | var last_cost=1000; |
| 497 | |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 498 | function step() { |
| 499 | var i = T.iter; |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 500 | |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 501 | if(i > <%= $no_iterations %>) { |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 502 | stopStep(); |
| 503 | } else { |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 504 | var cost = Math.round(T.step() *100000) / 100000; // do a few steps |
| 505 | $("#cost").html("tsne iteration " + i + ", cost: " + cost.toFixed(5)); |
| 506 | if(i % 250 == 0 && cost >= last_cost) { |
| 507 | stopStep(); |
| 508 | } else { |
| 509 | last_cost = cost; |
| 510 | updateEmbedding(); |
| 511 | } |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 512 | } |
| 513 | } |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 514 | |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 515 | function showMap(j) { |
| 516 | data=j; |
| 517 | T.iter=0; |
| 518 | T.initDataRaw(data.vecs); // init embedding |
| 519 | drawEmbedding(); // draw initial embedding |
| 520 | |
| 521 | if(iter_id >= 0) { |
| 522 | clearInterval(iter_id); |
| 523 | } |
| 524 | //T.debugGrad(); |
| 525 | iter_id = setInterval(step, 1); |
| 526 | //step(); |
| 527 | } |
| 528 | |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 529 | function update() { |
| 530 | |
| 531 | text.transition().duration(800) |
| 532 | .attr("transform", transform); |
| 533 | } |
| 534 | |
| 535 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 536 | </script> |
| 537 | </head> |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 538 | <body> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 539 | <form action="<%=url_for('/')->to_abs%>" method="GET"> |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 540 | word(s): |
| 541 | <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."> |
| 542 | max. neighbours: <input type="text" size="8" name="n" value="<%= $no_nbs %>"> |
| 543 | iterations: <input type="text" name="N" size="8" value="<%= $no_iterations %>"> |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 544 | <input type="submit" value="Show"> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 545 | </form> |
| 546 | <br> |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 547 | % if($lists) { |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 548 | <div id="wrapper"> |
| 549 | <table id="first"> |
| 550 | <tr> |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 551 | <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] | 552 | </tr> |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 553 | % 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] | 554 | % my $i=1; for my $item (@$list) { |
| 555 | % if(!grep{$_ eq $item->{word}} @words) { |
| 556 | % push @vecs, $item->{vector}; |
| 557 | % push @words, $item->{word}; |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 558 | % push @ranks, $item->{rank}; |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 559 | % } |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 560 | <tr> |
| 561 | <td align="right"> |
| 562 | <%= $i++ %>. |
| 563 | </td> |
| 564 | <td> |
| 565 | <a href="/?word=<%= $item->{word} %>"> |
| 566 | <%= $item->{word} %> |
| 567 | </a> |
| 568 | </td> |
| 569 | <td align="right"> |
| 570 | <%= sprintf("%.3f", $item->{dist}) %> |
| 571 | </td> |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 572 | <td align="right"> |
| 573 | <%= $item->{rank} %> |
| 574 | </td> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 575 | </tr> |
| 576 | % } |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 577 | % } |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 578 | </table> |
| 579 | <script> |
| 580 | % use Mojo::ByteStream 'b'; |
| 581 | $(window).load(function() { |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 582 | 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] | 583 | }); |
| 584 | </script> |
| 585 | % } |
| 586 | <div id="second" style="width:800px; height:800px; font-family: arial;"> |
| 587 | <div id="embed"> |
| 588 | </div> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 589 | </div> |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 590 | <div id="cost"></div> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 591 | </div> |
| 592 | <p> |
| 593 | 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] | 594 | <pre> |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 595 | -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] | 596 | </pre> |
| 597 | </p> |
| 598 | </body> |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 599 | </html> |
| 600 | |