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