blob: 9b0100142f0008ab2aaf2199171e99ca8ab34620 [file] [log] [blame]
Marc Kupietzdc22b982015-10-09 09:19:34 +02001#!/usr/local/bin/perl
2use Inline C;
3use Mojolicious::Lite;
Marc Kupietzc4893362016-02-25 08:04:46 +01004use Mojo::JSON qw(decode_json encode_json to_json);
Marc Kupietz247500f2015-10-09 11:29:01 +02005use Encode qw(decode encode);
Marc Kupietz7bc85fd2016-02-24 11:42:41 +01006use Mojo::Server::Daemon;
Marc Kupietzd4227392016-03-01 16:45:12 +01007plugin 'Log::Access';
Marc Kupietzdc22b982015-10-09 09:19:34 +02008
Marc Kupietzc5990da2016-02-26 08:47:12 +01009print STDERR $ARGV[0];
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010010# -cbow 1 -size 200 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 40 -binary 1 -iter 15
Marc Kupietz44bee3c2016-02-25 16:26:29 +010011init_net("vectors15.bin");
Marc Kupietzdc22b982015-10-09 09:19:34 +020012
13get '/' => sub {
14 my $c = shift;
15 my $word=$c->param('word');
Marc Kupietz44bee3c2016-02-25 16:26:29 +010016 my $no_nbs=$c->param('n') || 100;
17 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +010018 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +010019 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd4227392016-03-01 16:45:12 +010020
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010021 my @lists;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010022 if(defined($word) && $word !~ /^\s*$/) {
23 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +010024 $word =~ s/\s+/ /g;
25 for my $w (split(' *\| *', $word)) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010026 $c->app->log->debug('Looking for neighbours of '.$w);
27 push(@lists, get_neighbours(encode("iso-8859-1", $w), $no_nbs));
28 }
Marc Kupietz247500f2015-10-09 11:29:01 +020029 }
Marc Kupietz000ad862016-02-26 14:59:12 +010030 $word =~ s/ *\| */ | /g;
Marc Kupietzc4d62f82016-03-01 11:04:24 +010031 $c->render(template=>"index", word=>$word, no_nbs=>$no_nbs, no_iterations => $no_iterations, epsilon=> $epsilon, perplexity=> $perplexity, lists=> \@lists);
Marc Kupietzdc22b982015-10-09 09:19:34 +020032};
33
34app->start;
35
36exit;
37
38__END__
39
40__C__
41#include <stdio.h>
42#include <string.h>
43#include <math.h>
44#include <malloc.h>
45#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +010046#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +010047#include <pthread.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +020048
49#define max_size 2000
50#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010051#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +010052#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +010053#define MAX_THREADS 100
Marc Kupietzdc22b982015-10-09 09:19:34 +020054
55//the thread function
56void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +010057
58typedef struct {
59 long long *index;
60 float *dist;
61 unsigned int length;
62} knn;
63
64
65typedef struct {
66 char *token;
67 int N;
68 unsigned long from;
69 unsigned long upto;
70} knnpars;
71
Marc Kupietzdc22b982015-10-09 09:19:34 +020072float *M;
73char *vocab;
Marc Kupietz82b02672016-02-26 12:32:25 +010074long long words, size;
Marc Kupietz000ad862016-02-26 14:59:12 +010075int num_threads=20;
Marc Kupietzdc22b982015-10-09 09:19:34 +020076
77int init_net(char *file_name) {
Marc Kupietz67c20282016-02-26 09:42:00 +010078 FILE *f, *binvecs, *binwords;
Marc Kupietzf0809762016-02-26 10:13:47 +010079 int binwords_fd, binvecs_fd;
Marc Kupietz82b02672016-02-26 12:32:25 +010080 long long a, b, c, d, cn;
81 float len;
82
Marc Kupietz67c20282016-02-26 09:42:00 +010083 char binvecs_fname[256], binwords_fname[256];
84 strcpy(binwords_fname, file_name);
85 strcat(binwords_fname, ".words");
86 strcpy(binvecs_fname, file_name);
87 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +020088
Marc Kupietzdc22b982015-10-09 09:19:34 +020089 f = fopen(file_name, "rb");
90 if (f == NULL) {
91 printf("Input file %s not found\n", file_name);
92 return -1;
93 }
94 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +010095 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +020096 fscanf(f, "%lld", &size);
Marc Kupietzf0809762016-02-26 10:13:47 +010097 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
Marc Kupietz000ad862016-02-26 14:59:12 +010098 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
99 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
Marc Kupietzf0809762016-02-26 10:13:47 +0100100 if (M == MAP_FAILED || vocab == MAP_FAILED) {
101 close(binvecs_fd);
102 close(binwords_fd);
103 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
104 exit(-1);
105 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100106 } else {
Marc Kupietzf0809762016-02-26 10:13:47 +0100107 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
108 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
109 if (M == NULL) {
110 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
111 return -1;
112 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100113 for (b = 0; b < words; b++) {
114 a = 0;
115 while (1) {
116 vocab[b * max_w + a] = fgetc(f);
117 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
118 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
119 }
120 vocab[b * max_w + a] = 0;
121 fread(&M[b * size], sizeof(float), size, f);
122 len = 0;
123 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
124 len = sqrt(len);
125 for (a = 0; a < size; a++) M[a + b * size] /= len;
126 }
127 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
128 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
129 fclose(binvecs);
130 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
131 fclose(binwords);
132 }
133 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200134 fclose(f);
135 return 0;
136}
137
Marc Kupietz000ad862016-02-26 14:59:12 +0100138
139void *_get_neighbours(knnpars *pars) {
140 char *st1 = pars->token;
141 int N = pars->N;
142 unsigned long from = pars -> from;
143 unsigned long upto = pars -> upto;
Marc Kupietz82b02672016-02-26 12:32:25 +0100144 char file_name[max_size], st[100][max_size];
Marc Kupietz000ad862016-02-26 14:59:12 +0100145 float dist, len, *bestd, vec[max_size];
146 long long a, b, c, d, cn, bi[100], *besti;
Marc Kupietz82b02672016-02-26 12:32:25 +0100147 char ch;
Marc Kupietz000ad862016-02-26 14:59:12 +0100148 knn *nbs = NULL;
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100149
Marc Kupietz000ad862016-02-26 14:59:12 +0100150 besti = malloc(N * sizeof(long long));
151 bestd = malloc(N * sizeof(float));
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100152
Marc Kupietz000ad862016-02-26 14:59:12 +0100153 float worstbest=-1;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200154
155 for (a = 0; a < N; a++) bestd[a] = 0;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200156 a = 0;
157 cn = 0;
158 b = 0;
159 c = 0;
160 while (1) {
161 st[cn][b] = st1[c];
162 b++;
163 c++;
164 st[cn][b] = 0;
165 if (st1[c] == 0) break;
166 if (st1[c] == ' ') {
167 cn++;
168 b = 0;
169 c++;
170 }
171 }
172 cn++;
173 for (a = 0; a < cn; a++) {
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100174 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
175 if (b == words) b = -1;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200176 bi[a] = b;
Marc Kupietze8da3062016-02-25 08:37:53 +0100177 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], bi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200178 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100179 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100180 cn--;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200181 break;
182 }
183 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100184 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100185 N = 0;
186 goto end;
187 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200188 for (a = 0; a < size; a++) vec[a] = 0;
189 for (b = 0; b < cn; b++) {
190 if (bi[b] == -1) continue;
191 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
192 }
193 len = 0;
194 for (a = 0; a < size; a++) len += vec[a] * vec[a];
195 len = sqrt(len);
196 for (a = 0; a < size; a++) vec[a] /= len;
197 for (a = 0; a < N; a++) bestd[a] = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100198 for (c = from; c < upto; c++) {
Marc Kupietzdc22b982015-10-09 09:19:34 +0200199 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100200// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100201// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
202// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200203 dist = 0;
204 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100205 if(dist > worstbest) {
206 for (a = 0; a < N; a++) {
207 if (dist > bestd[a]) {
208 for (d = N - 1; d > a; d--) {
209 bestd[d] = bestd[d - 1];
210 besti[d] = besti[d - 1];
211 }
212 bestd[a] = dist;
213 besti[a] = c;
214 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200215 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200216 }
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100217 worstbest = bestd[N-1];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200218 }
219 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100220
Marc Kupietz000ad862016-02-26 14:59:12 +0100221 nbs = malloc(sizeof(knn));
222 nbs->index = besti;
223 nbs->dist = bestd;
224 nbs->length = N;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100225end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100226 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200227}
228
Marc Kupietz000ad862016-02-26 14:59:12 +0100229SV *get_neighbours(char *st1, int N) {
230 float bestd[MAX_NEIGHBOURS], vec[max_size];
231 long long besti[MAX_NEIGHBOURS], a, b, c, d, slice;
232 char *bestw[MAX_NEIGHBOURS];
233 knn *nbs[MAX_THREADS];
234 knnpars pars[MAX_THREADS];
235 pthread_t *pt = (pthread_t *)malloc(num_threads * sizeof(pthread_t));
236 AV* array = newAV();
237
238 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
239
240 slice = words / num_threads;
241
242 for(a=0; a < num_threads; a++) {
243 pars[a].token = st1;
244 pars[a].N = N;
245 pars[a].from = a*slice;
246 pars[a].upto = ((a+1)*slice > words? words:(a+1)*slice);
247 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
248 }
249 for (a = 0; a < num_threads; a++) pthread_join(pt[a], &nbs[a]);
250
251 if(!nbs[0])
252 goto end;
253
254 for(b=0; b < N; b++) {
255 besti[b] = nbs[0]->index[b];
256 bestd[b] = nbs[0]->dist[b];
257 }
258
259 for(a=1; a < num_threads; a++) {
260 for(b=0; b < N; b++) {
261 for(c=0; c < N; c++) {
262 if(nbs[a]->dist[b] > bestd[c]) {
263 for(d=N-1; d>c; d--) {
264 bestd[d] = bestd[d-1];
265 besti[d] = besti[d-1];
266 }
267 besti[c] = nbs[a]->index[b];
268 bestd[c] = nbs[a]->dist[b];
269 break;
270 }
271 }
272 }
273 }
274
275 if(nbs) {
276 for (a = 0; a < N; a++) {
277 bestw[a] = (char *)malloc(max_size * sizeof(char));
278 }
279 for (a = 0; a < N; a++) {
280 strcpy(bestw[a], &vocab[besti[a] * max_w]);
281 HV* hash = newHV();
282 hv_store(hash, "word", strlen("word"), newSVpvf(bestw[a], 0), 0);
283 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
284 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
285 AV *vector = newAV();
286 for (b = 0; b < size; b++) {
287 av_push(vector, newSVnv(M[b + besti[a] * size]));
288 }
289 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
290 av_push(array, newRV_noinc((SV*)hash));
291 }
292 }
293end:
294 return newRV_noinc((SV*)array);
295}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100296
Marc Kupietzdc22b982015-10-09 09:19:34 +0200297__DATA__
298
299@@ index.html.ep
300<!DOCTYPE html>
301<html>
Marc Kupietzc4893362016-02-25 08:04:46 +0100302<head>
303 <title>DeReKo-Word-Vector-Distances</title>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100304 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
Marc Kupietzc4893362016-02-25 08:04:46 +0100305 <script src="http://code.jquery.com/jquery-latest.min.js"></script>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100306 <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
307 <script>
308 $(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100309 $( document ).tooltip({
310 content: function() {
311 return $(this).attr('title');
312 }}
313 )
314 })
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100315 </script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100316 <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
317 <script src="http://klinux10/word2vec/tsne.js"></script>
Marc Kupietzc5990da2016-02-26 08:47:12 +0100318 <script src="http://klinux10/word2vec/labeler.js"></script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100319<style>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100320body, input {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100321 font-family: Arial, sans-serif;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100322 font-size: 11pt;
323}
324
325.ui-tooltip-content {
326 font-size: 9pt;
327 colour: #222222;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100328}
Marc Kupietz5f780672016-02-25 17:15:54 +0100329
330svg > .ui-tooltip-content {
Marc Kupietzb1029362016-02-27 21:38:55 +0100331 font-size: 8pt;
Marc Kupietz5f780672016-02-25 17:15:54 +0100332 colour: #222222;
333}
334
Marc Kupietzc4893362016-02-25 08:04:46 +0100335svg {
Marc Kupietzb1029362016-02-27 21:38:55 +0100336 margin-right: 10px;
337 margin-bottom: 10px;
Marc Kupietzc4893362016-02-25 08:04:46 +0100338}
339#wrapper {
340 width: 100%;
341// border: 1px solid red;
342 overflow: hidden; /* will contain if #first is longer than #second */
343}
344#first {
Marc Kupietzb1029362016-02-27 21:38:55 +0100345 margin-right: 20px;
346 float: left;
347 // border: 1px solid green;
Marc Kupietzc4893362016-02-25 08:04:46 +0100348}
349#second {
350 border: 1px solid #333;
351 overflow: hidden; /* if you don't want #second to wrap below #first */
352}
Marc Kupietz4aa62172016-02-25 10:39:27 +0100353#cost {
Marc Kupietzb1029362016-02-27 21:38:55 +0100354 font-size: 8pt;
355 color: #222222;
356 margin-top: 4px;
Marc Kupietz4aa62172016-02-25 10:39:27 +0100357}
Marc Kupietzc4893362016-02-25 08:04:46 +0100358</style>
359<script>
360
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100361var opt = {epsilon: <%= $epsilon %>, perplexity: <%= $perplexity %>},
Marc Kupietz9fca1732016-02-29 09:07:04 +0100362 mapWidth = 800, // width map
363 mapHeight = 800,
364 jitterRadius = 7;
365
Marc Kupietzc4893362016-02-25 08:04:46 +0100366var T = new tsnejs.tSNE(opt); // create a tSNE instance
367
368var Y;
369
370var data;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100371var labeler;
Marc Kupietzc4893362016-02-25 08:04:46 +0100372
Marc Kupietzc5990da2016-02-26 08:47:12 +0100373
374function applyJitter() {
375 svg.selectAll('.u')
376 .data(labels)
377 .transition()
378 .duration(50)
379 .attr("transform", function(d, i) {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100380 T.Y[i][0] = (d.x - mapWidth/2 - tx)/ss/20;
381 T.Y[i][1] = (d.y - mapHeight/2 - ty)/ss/20;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100382 return "translate(" +
383 (d.x) + "," +
384 (d.y) + ")";
385 });
386}
387
Marc Kupietzc4893362016-02-25 08:04:46 +0100388function updateEmbedding() {
389 var Y = T.getSolution();
390 svg.selectAll('.u')
Marc Kupietz9fca1732016-02-29 09:07:04 +0100391 .data(data.words)
392 .attr("transform", function(d, i) {
393 return "translate(" +
394 ((Y[i][0]*20*ss + tx) + mapWidth/2) + "," +
395 ((Y[i][1]*20*ss + ty) + mapHeight/2) + ")"; });
Marc Kupietzc4893362016-02-25 08:04:46 +0100396}
397
398var svg;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100399var labels = [];
400var anchor_array = [];
401var text;
402
Marc Kupietzc4893362016-02-25 08:04:46 +0100403function drawEmbedding() {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100404 $("#embed").empty();
405 var div = d3.select("#embed");
406
407 // get min and max in each column of Y
408 var Y = T.Y;
409
410 svg = div.append("svg") // svg is global
411 .attr("width", mapWidth)
412 .attr("height", mapHeight);
413
414 var g = svg.selectAll(".b")
415 .data(data.words)
416 .enter().append("g")
417 .attr("class", "u");
418
419 g.append("a")
420 .attr("xlink:href", function(word) {return "/?word="+word;})
421 .attr("title", function(d, i) {
Marc Kupietze50c8162016-03-01 10:24:43 +0100422 return "rank: "+i +" "+"freq. rank: "+data.ranks[i].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
Marc Kupietz9fca1732016-02-29 09:07:04 +0100423 })
Marc Kupietza350bce2016-02-25 09:34:25 +0100424 .append("text")
Marc Kupietz9fca1732016-02-29 09:07:04 +0100425 .attr("text-anchor", "top")
426 .attr("font-size", 12)
427 .attr("fill", function(d) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100428 if(data.target.indexOf(" "+d+" ") >= 0) {
Marc Kupietza350bce2016-02-25 09:34:25 +0100429 return "red";
430 } else {
431 return "#333"
Marc Kupietzc4893362016-02-25 08:04:46 +0100432 }
Marc Kupietz9fca1732016-02-29 09:07:04 +0100433 })
434 .text(function(d) { return d; });
435
436 var zoomListener = d3.behavior.zoom()
437 .scaleExtent([0.1, 10])
438 .center([0,0])
439 .on("zoom", zoomHandler);
440 zoomListener(svg);
441}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100442
Marc Kupietz9fca1732016-02-29 09:07:04 +0100443var tx=0, ty=0;
444var ss=1;
445var iter_id=-1;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100446
Marc Kupietz9fca1732016-02-29 09:07:04 +0100447function zoomHandler() {
448 tx = d3.event.translate[0];
449 ty = d3.event.translate[1];
450 ss = d3.event.scale;
451 updateEmbedding();
452}
453
454var stepnum = 0;
455
456function stopStep() {
457 clearInterval(iter_id);
458 text = svg.selectAll("text");
459
460 // jitter function needs different data and co-ordinate representation
461 labels = d3.range(data.words.length).map(function(i) {
462 var x = (T.Y[i][0]*20*ss + tx) + mapWidth/2;
463 var y = (T.Y[i][1]*20*ss + ty) + mapHeight/2;
464 anchor_array.push({x: x, y: y, r: jitterRadius});
465 return {
466 x: x,
467 y: y,
468 name: data.words[i]
469 };
470 });
471
472 // get the actual label bounding boxes for the jitter function
473 var index = 0;
474 text.each(function() {
475 labels[index].width = this.getBBox().width;
476 labels[index].height = this.getBBox().height;
477 index += 1;
478 });
Marc Kupietzc5990da2016-02-26 08:47:12 +0100479
480
481// setTimeout(updateEmbedding, 1);
482// setTimeout(
Marc Kupietz9fca1732016-02-29 09:07:04 +0100483 labeler = d3.labeler()
484 .label(labels)
485 .anchor(anchor_array)
486 .width(mapWidth)
487 .height(mapHeight)
488 .update(applyJitter);
489 // .start(1000);
Marc Kupietzc5990da2016-02-26 08:47:12 +0100490
Marc Kupietz9fca1732016-02-29 09:07:04 +0100491 iter_id = setInterval(jitterStep, 1);
492}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100493
Marc Kupietz9fca1732016-02-29 09:07:04 +0100494var jitter_i=0;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100495
496function jitterStep() {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100497 if(jitter_i++ > 100) {
498 clearInterval(iter_id);
499 } else {
500 labeler.start2(10);
501 applyJitter();
502 }
Marc Kupietzc5990da2016-02-26 08:47:12 +0100503}
Marc Kupietzb1029362016-02-27 21:38:55 +0100504
505var last_cost=1000;
506
Marc Kupietz9fca1732016-02-29 09:07:04 +0100507function step() {
508 var i = T.iter;
509
510 if(i > <%= $no_iterations %>) {
511 stopStep();
512 } else {
513 var cost = Math.round(T.step() * 100000) / 100000; // do a few steps
514 $("#cost").html("tsne iteration " + i + ", cost: " + cost.toFixed(5));
515 if(i % 250 == 0 && cost >= last_cost) {
516 stopStep();
517 } else {
518 last_cost = cost;
519 updateEmbedding();
520 }
521 }
522}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100523
Marc Kupietz9fca1732016-02-29 09:07:04 +0100524function showMap(j) {
525 data=j;
526 T.iter=0;
527 T.initDataRaw(data.vecs); // init embedding
528 drawEmbedding(); // draw initial embedding
529
530 if(iter_id >= 0) {
531 clearInterval(iter_id);
532 }
533 //T.debugGrad();
534 iter_id = setInterval(step, 1);
535 //step();
536}
Marc Kupietza350bce2016-02-25 09:34:25 +0100537
Marc Kupietzc4893362016-02-25 08:04:46 +0100538</script>
539</head>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200540<body>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100541 <form action="<%=url_for('/')->to_abs%>" method="GET">
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100542 word(s):
543 <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.">
544 max. neighbours: <input type="text" size="8" name="n" value="<%= $no_nbs %>">
545 iterations: <input type="text" name="N" size="8" value="<%= $no_iterations %>">
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100546 <input type="submit" value="Show">
Marc Kupietz4aa62172016-02-25 10:39:27 +0100547 </form>
548 <br>
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100549 % if($lists) {
Marc Kupietz4aa62172016-02-25 10:39:27 +0100550 <div id="wrapper">
551 <table id="first">
552 <tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100553 <th align="right">Pos.</th><th align="left">Word</th><th align="right">Cosine dist.</th><th>Freq. rank</th>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100554 </tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100555 % my $j=0; my @words; my @vecs; my @ranks; for my $list (@$lists) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100556 % my $i=1; for my $item (@$list) {
557 % if(!grep{$_ eq $item->{word}} @words) {
558 % push @vecs, $item->{vector};
559 % push @words, $item->{word};
Marc Kupietz5f780672016-02-25 17:15:54 +0100560 % push @ranks, $item->{rank};
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100561 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100562 <tr>
563 <td align="right">
564 <%= $i++ %>.
565 </td>
566 <td>
567 <a href="/?word=<%= $item->{word} %>">
568 <%= $item->{word} %>
569 </a>
570 </td>
571 <td align="right">
572 <%= sprintf("%.3f", $item->{dist}) %>
573 </td>
Marc Kupietz5f780672016-02-25 17:15:54 +0100574 <td align="right">
575 <%= $item->{rank} %>
576 </td>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100577 </tr>
578 % }
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100579 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100580 </table>
581 <script>
582 % use Mojo::ByteStream 'b';
583 $(window).load(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100584 showMap(<%= b(Mojo::JSON::to_json({target => " $word ", words => \@words, vecs => \@vecs, ranks => \@ranks})); %>);
Marc Kupietz4aa62172016-02-25 10:39:27 +0100585 });
586 </script>
587 % }
588 <div id="second" style="width:800px; height:800px; font-family: arial;">
589 <div id="embed">
590 </div>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100591 </div>
Marc Kupietzb1029362016-02-27 21:38:55 +0100592 <div id="cost"></div>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100593 </div>
594 <p>
595 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 Kupietz247500f2015-10-09 11:29:01 +0200596 <pre>
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100597-cbow 1 -size 300 -window 7 -negative 5 -hs 0 -sample 1e-5 -threads 44 -binary 1 -iter 5
Marc Kupietz4aa62172016-02-25 10:39:27 +0100598 </pre>
599 </p>
600</body>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200601</html>
602