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