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