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