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 | 95aa1c0 | 2016-03-15 09:40:43 +0100 | [diff] [blame^] | 154 | char file_name[max_size], st[100][max_size], sep[100]; |
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; |
Marc Kupietz | 95aa1c0 | 2016-03-15 09:40:43 +0100 | [diff] [blame^] | 176 | if (st1[c] == ' ' || st1[c] == '-') { |
| 177 | sep[cn++] = st1[c]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 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; |
Marc Kupietz | 95aa1c0 | 2016-03-15 09:40:43 +0100 | [diff] [blame^] | 201 | if(b>0 && sep[b-1] == '-') |
| 202 | for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size]; |
| 203 | else |
| 204 | for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 205 | } |
| 206 | len = 0; |
| 207 | for (a = 0; a < size; a++) len += vec[a] * vec[a]; |
| 208 | len = sqrt(len); |
| 209 | for (a = 0; a < size; a++) vec[a] /= len; |
| 210 | for (a = 0; a < N; a++) bestd[a] = -1; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 211 | for (c = from; c < upto; c++) { |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 212 | a = 0; |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 213 | // do not skip taget word |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 214 | // for (b = 0; b < cn; b++) if (bi[b] == c) a = 1; |
| 215 | // if (a == 1) continue; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 216 | dist = 0; |
| 217 | 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] | 218 | if(dist > worstbest) { |
| 219 | for (a = 0; a < N; a++) { |
| 220 | if (dist > bestd[a]) { |
| 221 | for (d = N - 1; d > a; d--) { |
| 222 | bestd[d] = bestd[d - 1]; |
| 223 | besti[d] = besti[d - 1]; |
| 224 | } |
| 225 | bestd[a] = dist; |
| 226 | besti[a] = c; |
| 227 | break; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 228 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 229 | } |
Marc Kupietz | be1b9fc | 2016-02-26 10:34:30 +0100 | [diff] [blame] | 230 | worstbest = bestd[N-1]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 231 | } |
| 232 | } |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 233 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 234 | nbs = malloc(sizeof(knn)); |
| 235 | nbs->index = besti; |
| 236 | nbs->dist = bestd; |
| 237 | nbs->length = N; |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 238 | end: |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 239 | pthread_exit(nbs); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 242 | SV *get_neighbours(char *st1, int N) { |
| 243 | float bestd[MAX_NEIGHBOURS], vec[max_size]; |
| 244 | long long besti[MAX_NEIGHBOURS], a, b, c, d, slice; |
| 245 | char *bestw[MAX_NEIGHBOURS]; |
| 246 | knn *nbs[MAX_THREADS]; |
| 247 | knnpars pars[MAX_THREADS]; |
| 248 | pthread_t *pt = (pthread_t *)malloc(num_threads * sizeof(pthread_t)); |
| 249 | AV* array = newAV(); |
| 250 | |
| 251 | if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS; |
| 252 | |
| 253 | slice = words / num_threads; |
| 254 | |
| 255 | for(a=0; a < num_threads; a++) { |
| 256 | pars[a].token = st1; |
| 257 | pars[a].N = N; |
| 258 | pars[a].from = a*slice; |
| 259 | pars[a].upto = ((a+1)*slice > words? words:(a+1)*slice); |
| 260 | pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]); |
| 261 | } |
| 262 | for (a = 0; a < num_threads; a++) pthread_join(pt[a], &nbs[a]); |
| 263 | |
| 264 | if(!nbs[0]) |
| 265 | goto end; |
| 266 | |
| 267 | for(b=0; b < N; b++) { |
| 268 | besti[b] = nbs[0]->index[b]; |
| 269 | bestd[b] = nbs[0]->dist[b]; |
| 270 | } |
| 271 | |
| 272 | for(a=1; a < num_threads; a++) { |
| 273 | for(b=0; b < N; b++) { |
| 274 | for(c=0; c < N; c++) { |
| 275 | if(nbs[a]->dist[b] > bestd[c]) { |
| 276 | for(d=N-1; d>c; d--) { |
| 277 | bestd[d] = bestd[d-1]; |
| 278 | besti[d] = besti[d-1]; |
| 279 | } |
| 280 | besti[c] = nbs[a]->index[b]; |
| 281 | bestd[c] = nbs[a]->dist[b]; |
| 282 | break; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | if(nbs) { |
| 289 | for (a = 0; a < N; a++) { |
| 290 | bestw[a] = (char *)malloc(max_size * sizeof(char)); |
| 291 | } |
| 292 | for (a = 0; a < N; a++) { |
| 293 | strcpy(bestw[a], &vocab[besti[a] * max_w]); |
| 294 | HV* hash = newHV(); |
| 295 | hv_store(hash, "word", strlen("word"), newSVpvf(bestw[a], 0), 0); |
| 296 | hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0); |
| 297 | hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0); |
| 298 | AV *vector = newAV(); |
| 299 | for (b = 0; b < size; b++) { |
| 300 | av_push(vector, newSVnv(M[b + besti[a] * size])); |
| 301 | } |
| 302 | hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0); |
| 303 | av_push(array, newRV_noinc((SV*)hash)); |
| 304 | } |
| 305 | } |
| 306 | end: |
| 307 | return newRV_noinc((SV*)array); |
| 308 | } |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 309 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 310 | __DATA__ |
| 311 | |
| 312 | @@ index.html.ep |
| 313 | <!DOCTYPE html> |
| 314 | <html> |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 315 | <head> |
| 316 | <title>DeReKo-Word-Vector-Distances</title> |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 317 | <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] | 318 | <script src="http://code.jquery.com/jquery-latest.min.js"></script> |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 319 | <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> |
| 320 | <script> |
| 321 | $(function() { |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 322 | $( document ).tooltip({ |
| 323 | content: function() { |
| 324 | return $(this).attr('title'); |
| 325 | }} |
| 326 | ) |
| 327 | }) |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 328 | </script> |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 329 | <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script> |
| 330 | <script src="http://klinux10/word2vec/tsne.js"></script> |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 331 | <script src="http://klinux10/word2vec/som.js"></script> |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 332 | <script src="http://klinux10/word2vec/labeler.js"></script> |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 333 | <style> |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 334 | body, input { |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 335 | font-family: Arial, sans-serif; |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 336 | font-size: 11pt; |
| 337 | } |
| 338 | |
| 339 | .ui-tooltip-content { |
| 340 | font-size: 9pt; |
| 341 | colour: #222222; |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 342 | } |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 343 | |
| 344 | svg > .ui-tooltip-content { |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 345 | font-size: 8pt; |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 346 | colour: #222222; |
| 347 | } |
| 348 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 349 | #wrapper { |
| 350 | width: 100%; |
| 351 | // border: 1px solid red; |
| 352 | overflow: hidden; /* will contain if #first is longer than #second */ |
| 353 | } |
| 354 | #first { |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 355 | margin-right: 20px; |
| 356 | float: left; |
| 357 | // border: 1px solid green; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 358 | } |
| 359 | #second { |
| 360 | border: 1px solid #333; |
| 361 | overflow: hidden; /* if you don't want #second to wrap below #first */ |
| 362 | } |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 363 | #som2 svg { |
| 364 | border: 1px solid #333; |
| 365 | } |
| 366 | |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 367 | #cost { |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 368 | font-size: 8pt; |
| 369 | color: #222222; |
| 370 | margin-top: 4px; |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 371 | margin-bottom: 12px; |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 372 | } |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 373 | |
Marc Kupietz | 6c1ca44 | 2016-03-03 09:35:18 +0100 | [diff] [blame] | 374 | #sominfo1, #sominfo { |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 375 | font-size: 8pt; |
| 376 | color: #222222; |
| 377 | margin-top: 0px; |
| 378 | } |
| 379 | |
Marc Kupietz | 6c1ca44 | 2016-03-03 09:35:18 +0100 | [diff] [blame] | 380 | #somcolor1, #somcolor2, #somcolor3 { |
| 381 | display: inline-block; |
| 382 | height: 10px; |
| 383 | width: 10px; |
| 384 | } |
| 385 | |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 386 | #third { |
| 387 | border: 1px solid #333; |
| 388 | } |
| 389 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 390 | </style> |
| 391 | <script> |
| 392 | |
Marc Kupietz | c4d62f8 | 2016-03-01 11:04:24 +0100 | [diff] [blame] | 393 | var opt = {epsilon: <%= $epsilon %>, perplexity: <%= $perplexity %>}, |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 394 | mapWidth = 800, // width map |
| 395 | mapHeight = 800, |
| 396 | jitterRadius = 7; |
| 397 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 398 | var T = new tsnejs.tSNE(opt); // create a tSNE instance |
| 399 | |
| 400 | var Y; |
| 401 | |
| 402 | var data; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 403 | var labeler; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 404 | |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 405 | |
| 406 | function applyJitter() { |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 407 | svg.selectAll('.tsnet') |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 408 | .data(labels) |
| 409 | .transition() |
| 410 | .duration(50) |
| 411 | .attr("transform", function(d, i) { |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 412 | T.Y[i][0] = (d.x - mapWidth/2 - tx)/ss/20; |
| 413 | T.Y[i][1] = (d.y - mapHeight/2 - ty)/ss/20; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 414 | return "translate(" + |
| 415 | (d.x) + "," + |
| 416 | (d.y) + ")"; |
| 417 | }); |
| 418 | } |
| 419 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 420 | function updateEmbedding() { |
| 421 | var Y = T.getSolution(); |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 422 | svg.selectAll('.tsnet') |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 423 | .data(data.words) |
| 424 | .attr("transform", function(d, i) { |
| 425 | return "translate(" + |
| 426 | ((Y[i][0]*20*ss + tx) + mapWidth/2) + "," + |
| 427 | ((Y[i][1]*20*ss + ty) + mapHeight/2) + ")"; }); |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | var svg; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 431 | var labels = []; |
| 432 | var anchor_array = []; |
| 433 | var text; |
| 434 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 435 | function drawEmbedding() { |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 436 | $("#embed").empty(); |
| 437 | var div = d3.select("#embed"); |
| 438 | |
| 439 | // get min and max in each column of Y |
| 440 | var Y = T.Y; |
| 441 | |
| 442 | svg = div.append("svg") // svg is global |
| 443 | .attr("width", mapWidth) |
| 444 | .attr("height", mapHeight); |
| 445 | |
| 446 | var g = svg.selectAll(".b") |
| 447 | .data(data.words) |
| 448 | .enter().append("g") |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 449 | .attr("class", "tsnet"); |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 450 | |
| 451 | g.append("a") |
| 452 | .attr("xlink:href", function(word) {return "/?word="+word;}) |
| 453 | .attr("title", function(d, i) { |
Marc Kupietz | e50c816 | 2016-03-01 10:24:43 +0100 | [diff] [blame] | 454 | 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] | 455 | }) |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 456 | .append("text") |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 457 | .attr("text-anchor", "top") |
| 458 | .attr("font-size", 12) |
| 459 | .attr("fill", function(d) { |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 460 | if(data.target.indexOf(" "+d+" ") >= 0) { |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 461 | return "red"; |
| 462 | } else { |
| 463 | return "#333" |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 464 | } |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 465 | }) |
| 466 | .text(function(d) { return d; }); |
| 467 | |
| 468 | var zoomListener = d3.behavior.zoom() |
| 469 | .scaleExtent([0.1, 10]) |
| 470 | .center([0,0]) |
| 471 | .on("zoom", zoomHandler); |
| 472 | zoomListener(svg); |
| 473 | } |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 474 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 475 | var tx=0, ty=0; |
| 476 | var ss=1; |
| 477 | var iter_id=-1; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 478 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 479 | function zoomHandler() { |
| 480 | tx = d3.event.translate[0]; |
| 481 | ty = d3.event.translate[1]; |
| 482 | ss = d3.event.scale; |
| 483 | updateEmbedding(); |
| 484 | } |
| 485 | |
| 486 | var stepnum = 0; |
| 487 | |
| 488 | function stopStep() { |
| 489 | clearInterval(iter_id); |
| 490 | text = svg.selectAll("text"); |
| 491 | |
| 492 | // jitter function needs different data and co-ordinate representation |
| 493 | labels = d3.range(data.words.length).map(function(i) { |
| 494 | var x = (T.Y[i][0]*20*ss + tx) + mapWidth/2; |
| 495 | var y = (T.Y[i][1]*20*ss + ty) + mapHeight/2; |
| 496 | anchor_array.push({x: x, y: y, r: jitterRadius}); |
| 497 | return { |
| 498 | x: x, |
| 499 | y: y, |
| 500 | name: data.words[i] |
| 501 | }; |
| 502 | }); |
| 503 | |
| 504 | // get the actual label bounding boxes for the jitter function |
| 505 | var index = 0; |
| 506 | text.each(function() { |
| 507 | labels[index].width = this.getBBox().width; |
| 508 | labels[index].height = this.getBBox().height; |
| 509 | index += 1; |
| 510 | }); |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 511 | |
| 512 | |
| 513 | // setTimeout(updateEmbedding, 1); |
| 514 | // setTimeout( |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 515 | labeler = d3.labeler() |
| 516 | .label(labels) |
| 517 | .anchor(anchor_array) |
| 518 | .width(mapWidth) |
| 519 | .height(mapHeight) |
| 520 | .update(applyJitter); |
| 521 | // .start(1000); |
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 | iter_id = setInterval(jitterStep, 1); |
| 524 | } |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 525 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 526 | var jitter_i=0; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 527 | |
| 528 | function jitterStep() { |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 529 | if(jitter_i++ > 100) { |
| 530 | clearInterval(iter_id); |
| 531 | } else { |
| 532 | labeler.start2(10); |
| 533 | applyJitter(); |
| 534 | } |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 535 | } |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 536 | |
| 537 | var last_cost=1000; |
| 538 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 539 | function step() { |
| 540 | var i = T.iter; |
| 541 | |
| 542 | if(i > <%= $no_iterations %>) { |
| 543 | stopStep(); |
| 544 | } else { |
| 545 | var cost = Math.round(T.step() * 100000) / 100000; // do a few steps |
| 546 | $("#cost").html("tsne iteration " + i + ", cost: " + cost.toFixed(5)); |
| 547 | if(i % 250 == 0 && cost >= last_cost) { |
| 548 | stopStep(); |
| 549 | } else { |
| 550 | last_cost = cost; |
| 551 | updateEmbedding(); |
| 552 | } |
| 553 | } |
| 554 | } |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 555 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 556 | function showMap(j) { |
| 557 | data=j; |
| 558 | T.iter=0; |
| 559 | T.initDataRaw(data.vecs); // init embedding |
| 560 | drawEmbedding(); // draw initial embedding |
| 561 | |
| 562 | if(iter_id >= 0) { |
| 563 | clearInterval(iter_id); |
| 564 | } |
| 565 | //T.debugGrad(); |
| 566 | iter_id = setInterval(step, 1); |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 567 | if(<%= $show_som %>) { |
| 568 | makeSOM(j, <%= $no_iterations %>); |
| 569 | } |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 570 | } |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 571 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 572 | </script> |
| 573 | </head> |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 574 | <body> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 575 | <form action="<%=url_for('/')->to_abs%>" method="GET"> |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 576 | word(s): |
| 577 | <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."> |
| 578 | max. neighbours: <input type="text" size="8" name="n" value="<%= $no_nbs %>"> |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 579 | max. iterations: <input type="text" name="N" size="8" value="<%= $no_iterations %>"> |
| 580 | SOM <input type="checkbox" name="som" value="1" <%= ($show_som ? "checked" : "") %>> |
| 581 | <span> </span><input type="submit" value="Show"> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 582 | </form> |
| 583 | <br> |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 584 | % if($lists) { |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 585 | <div id="wrapper"> |
| 586 | <table id="first"> |
| 587 | <tr> |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 588 | <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] | 589 | </tr> |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 590 | % 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] | 591 | % my $i=1; for my $item (@$list) { |
| 592 | % if(!grep{$_ eq $item->{word}} @words) { |
| 593 | % push @vecs, $item->{vector}; |
| 594 | % push @words, $item->{word}; |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 595 | % push @ranks, $item->{rank}; |
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 | <tr> |
| 598 | <td align="right"> |
| 599 | <%= $i++ %>. |
| 600 | </td> |
| 601 | <td> |
| 602 | <a href="/?word=<%= $item->{word} %>"> |
| 603 | <%= $item->{word} %> |
| 604 | </a> |
| 605 | </td> |
| 606 | <td align="right"> |
| 607 | <%= sprintf("%.3f", $item->{dist}) %> |
| 608 | </td> |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 609 | <td align="right"> |
| 610 | <%= $item->{rank} %> |
| 611 | </td> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 612 | </tr> |
| 613 | % } |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 614 | % } |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 615 | </table> |
| 616 | <script> |
| 617 | % use Mojo::ByteStream 'b'; |
| 618 | $(window).load(function() { |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 619 | 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] | 620 | }); |
| 621 | </script> |
| 622 | % } |
| 623 | <div id="second" style="width:800px; height:800px; font-family: arial;"> |
| 624 | <div id="embed"> |
| 625 | </div> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 626 | </div> |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 627 | <div id="cost"></div> |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 628 | % if($show_som) { |
| 629 | <div id="som2"> |
| 630 | </div> |
Marc Kupietz | 6c1ca44 | 2016-03-03 09:35:18 +0100 | [diff] [blame] | 631 | <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] | 632 | <div id="sominfo">SOM iteration <span id="iterations">0</span></div> |
| 633 | % } |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 634 | </div> |
| 635 | <p> |
| 636 | 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] | 637 | <pre> |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 638 | -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] | 639 | </pre> |
| 640 | </p> |
| 641 | </body> |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 642 | </html> |
| 643 | |