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