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