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