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