blob: e37ef3bf309d96ba3159325bc37e9a82350ad18f [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 Kupietzdc22b982015-10-09 09:19:34 +02007
Marc Kupietzc5990da2016-02-26 08:47:12 +01008print STDERR $ARGV[0];
Marc Kupietz7bc85fd2016-02-24 11:42:41 +01009# -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 +010010init_net("vectors15.bin");
Marc Kupietzdc22b982015-10-09 09:19:34 +020011
12get '/' => sub {
13 my $c = shift;
14 my $word=$c->param('word');
Marc Kupietz44bee3c2016-02-25 16:26:29 +010015 my $no_nbs=$c->param('n') || 100;
16 my $no_iterations=$c->param('N') || 2000;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010017 my @lists;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010018 if(defined($word) && $word !~ /^\s*$/) {
19 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +010020 $word =~ s/\s+/ /g;
21 for my $w (split(' *\| *', $word)) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010022 $c->app->log->debug('Looking for neighbours of '.$w);
23 push(@lists, get_neighbours(encode("iso-8859-1", $w), $no_nbs));
24 }
Marc Kupietz247500f2015-10-09 11:29:01 +020025 }
Marc Kupietz000ad862016-02-26 14:59:12 +010026 $word =~ s/ *\| */ | /g;
Marc Kupietz44bee3c2016-02-25 16:26:29 +010027 $c->render(template=>"index", word=>$word, no_nbs=>$no_nbs, no_iterations => $no_iterations, lists=> \@lists);
Marc Kupietzdc22b982015-10-09 09:19:34 +020028};
29
30app->start;
31
32exit;
33
34__END__
35
36__C__
37#include <stdio.h>
38#include <string.h>
39#include <math.h>
40#include <malloc.h>
41#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +010042#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +010043#include <pthread.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +020044
45#define max_size 2000
46#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010047#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +010048#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +010049#define MAX_THREADS 100
Marc Kupietzdc22b982015-10-09 09:19:34 +020050
51//the thread function
52void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +010053
54typedef struct {
55 long long *index;
56 float *dist;
57 unsigned int length;
58} knn;
59
60
61typedef struct {
62 char *token;
63 int N;
64 unsigned long from;
65 unsigned long upto;
66} knnpars;
67
Marc Kupietzdc22b982015-10-09 09:19:34 +020068float *M;
69char *vocab;
Marc Kupietz82b02672016-02-26 12:32:25 +010070long long words, size;
Marc Kupietz000ad862016-02-26 14:59:12 +010071int num_threads=20;
Marc Kupietzdc22b982015-10-09 09:19:34 +020072
73int init_net(char *file_name) {
Marc Kupietz67c20282016-02-26 09:42:00 +010074 FILE *f, *binvecs, *binwords;
Marc Kupietzf0809762016-02-26 10:13:47 +010075 int binwords_fd, binvecs_fd;
Marc Kupietz82b02672016-02-26 12:32:25 +010076 long long a, b, c, d, cn;
77 float len;
78
Marc Kupietz67c20282016-02-26 09:42:00 +010079 char binvecs_fname[256], binwords_fname[256];
80 strcpy(binwords_fname, file_name);
81 strcat(binwords_fname, ".words");
82 strcpy(binvecs_fname, file_name);
83 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +020084
Marc Kupietzdc22b982015-10-09 09:19:34 +020085 f = fopen(file_name, "rb");
86 if (f == NULL) {
87 printf("Input file %s not found\n", file_name);
88 return -1;
89 }
90 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +010091 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +020092 fscanf(f, "%lld", &size);
Marc Kupietzf0809762016-02-26 10:13:47 +010093 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
Marc Kupietz000ad862016-02-26 14:59:12 +010094 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
95 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
Marc Kupietzf0809762016-02-26 10:13:47 +010096 if (M == MAP_FAILED || vocab == MAP_FAILED) {
97 close(binvecs_fd);
98 close(binwords_fd);
99 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
100 exit(-1);
101 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100102 } else {
Marc Kupietzf0809762016-02-26 10:13:47 +0100103 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
104 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
105 if (M == NULL) {
106 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
107 return -1;
108 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100109 for (b = 0; b < words; b++) {
110 a = 0;
111 while (1) {
112 vocab[b * max_w + a] = fgetc(f);
113 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
114 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
115 }
116 vocab[b * max_w + a] = 0;
117 fread(&M[b * size], sizeof(float), size, f);
118 len = 0;
119 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
120 len = sqrt(len);
121 for (a = 0; a < size; a++) M[a + b * size] /= len;
122 }
123 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
124 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
125 fclose(binvecs);
126 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
127 fclose(binwords);
128 }
129 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200130 fclose(f);
131 return 0;
132}
133
Marc Kupietz000ad862016-02-26 14:59:12 +0100134
135void *_get_neighbours(knnpars *pars) {
136 char *st1 = pars->token;
137 int N = pars->N;
138 unsigned long from = pars -> from;
139 unsigned long upto = pars -> upto;
Marc Kupietz82b02672016-02-26 12:32:25 +0100140 char file_name[max_size], st[100][max_size];
Marc Kupietz000ad862016-02-26 14:59:12 +0100141 float dist, len, *bestd, vec[max_size];
142 long long a, b, c, d, cn, bi[100], *besti;
Marc Kupietz82b02672016-02-26 12:32:25 +0100143 char ch;
Marc Kupietz000ad862016-02-26 14:59:12 +0100144 knn *nbs = NULL;
Marc Kupietz82b02672016-02-26 12:32:25 +0100145
Marc Kupietz000ad862016-02-26 14:59:12 +0100146 besti = malloc(N * sizeof(long long));
147 bestd = malloc(N * sizeof(float));
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100148
Marc Kupietz000ad862016-02-26 14:59:12 +0100149 float worstbest=-1;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200150
151 for (a = 0; a < N; a++) bestd[a] = 0;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200152 a = 0;
153 cn = 0;
154 b = 0;
155 c = 0;
156 while (1) {
157 st[cn][b] = st1[c];
158 b++;
159 c++;
160 st[cn][b] = 0;
161 if (st1[c] == 0) break;
162 if (st1[c] == ' ') {
163 cn++;
164 b = 0;
165 c++;
166 }
167 }
168 cn++;
169 for (a = 0; a < cn; a++) {
170 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
171 if (b == words) b = -1;
172 bi[a] = b;
Marc Kupietze8da3062016-02-25 08:37:53 +0100173 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], bi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200174 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100175 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100176 cn--;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200177 break;
178 }
179 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100180 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100181 N = 0;
182 goto end;
183 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200184 for (a = 0; a < size; a++) vec[a] = 0;
185 for (b = 0; b < cn; b++) {
186 if (bi[b] == -1) continue;
187 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
188 }
189 len = 0;
190 for (a = 0; a < size; a++) len += vec[a] * vec[a];
191 len = sqrt(len);
192 for (a = 0; a < size; a++) vec[a] /= len;
193 for (a = 0; a < N; a++) bestd[a] = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100194 for (c = from; c < upto; c++) {
Marc Kupietzdc22b982015-10-09 09:19:34 +0200195 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100196// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100197// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
198// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200199 dist = 0;
200 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100201 if(dist > worstbest) {
202 for (a = 0; a < N; a++) {
203 if (dist > bestd[a]) {
204 for (d = N - 1; d > a; d--) {
205 bestd[d] = bestd[d - 1];
206 besti[d] = besti[d - 1];
207 }
208 bestd[a] = dist;
209 besti[a] = c;
210 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200211 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200212 }
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100213 worstbest = bestd[N-1];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200214 }
215 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100216
Marc Kupietz000ad862016-02-26 14:59:12 +0100217 nbs = malloc(sizeof(knn));
218 nbs->index = besti;
219 nbs->dist = bestd;
220 nbs->length = N;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100221end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100222 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200223}
224
Marc Kupietz000ad862016-02-26 14:59:12 +0100225SV *get_neighbours(char *st1, int N) {
226 float bestd[MAX_NEIGHBOURS], vec[max_size];
227 long long besti[MAX_NEIGHBOURS], a, b, c, d, slice;
228 char *bestw[MAX_NEIGHBOURS];
229 knn *nbs[MAX_THREADS];
230 knnpars pars[MAX_THREADS];
231 pthread_t *pt = (pthread_t *)malloc(num_threads * sizeof(pthread_t));
232 AV* array = newAV();
233
234 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
235
236 slice = words / num_threads;
237
238 for(a=0; a < num_threads; a++) {
239 pars[a].token = st1;
240 pars[a].N = N;
241 pars[a].from = a*slice;
242 pars[a].upto = ((a+1)*slice > words? words:(a+1)*slice);
243 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
244 }
245 for (a = 0; a < num_threads; a++) pthread_join(pt[a], &nbs[a]);
246
247 if(!nbs[0])
248 goto end;
249
250 for(b=0; b < N; b++) {
251 besti[b] = nbs[0]->index[b];
252 bestd[b] = nbs[0]->dist[b];
253 }
254
255 for(a=1; a < num_threads; a++) {
256 for(b=0; b < N; b++) {
257 for(c=0; c < N; c++) {
258 if(nbs[a]->dist[b] > bestd[c]) {
259 for(d=N-1; d>c; d--) {
260 bestd[d] = bestd[d-1];
261 besti[d] = besti[d-1];
262 }
263 besti[c] = nbs[a]->index[b];
264 bestd[c] = nbs[a]->dist[b];
265 break;
266 }
267 }
268 }
269 }
270
271 if(nbs) {
272 for (a = 0; a < N; a++) {
273 bestw[a] = (char *)malloc(max_size * sizeof(char));
274 }
275 for (a = 0; a < N; a++) {
276 strcpy(bestw[a], &vocab[besti[a] * max_w]);
277 HV* hash = newHV();
278 hv_store(hash, "word", strlen("word"), newSVpvf(bestw[a], 0), 0);
279 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
280 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
281 AV *vector = newAV();
282 for (b = 0; b < size; b++) {
283 av_push(vector, newSVnv(M[b + besti[a] * size]));
284 }
285 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
286 av_push(array, newRV_noinc((SV*)hash));
287 }
288 }
289end:
290 return newRV_noinc((SV*)array);
291}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100292
Marc Kupietzdc22b982015-10-09 09:19:34 +0200293__DATA__
294
295@@ index.html.ep
296<!DOCTYPE html>
297<html>
Marc Kupietzc4893362016-02-25 08:04:46 +0100298<head>
299 <title>DeReKo-Word-Vector-Distances</title>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100300 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
Marc Kupietzc4893362016-02-25 08:04:46 +0100301 <script src="http://code.jquery.com/jquery-latest.min.js"></script>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100302 <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
303 <script>
304 $(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100305 $( document ).tooltip({
306 content: function() {
307 return $(this).attr('title');
308 }}
309 )
310 })
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100311 </script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100312 <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
313 <script src="http://klinux10/word2vec/tsne.js"></script>
Marc Kupietzc5990da2016-02-26 08:47:12 +0100314 <script src="http://klinux10/word2vec/labeler.js"></script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100315<style>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100316body, input {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100317 font-family: Arial, sans-serif;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100318 font-size: 11pt;
319}
320
321.ui-tooltip-content {
322 font-size: 9pt;
323 colour: #222222;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100324}
Marc Kupietz5f780672016-02-25 17:15:54 +0100325
326svg > .ui-tooltip-content {
327 font-size: 7pt;
328 colour: #222222;
329}
330
Marc Kupietzc4893362016-02-25 08:04:46 +0100331svg {
332// border: 1px solid #333;
Marc Kupietz4aa62172016-02-25 10:39:27 +0100333 margin-right: 10px;
334 margin-bottom:10px;
Marc Kupietzc4893362016-02-25 08:04:46 +0100335}
336#wrapper {
337 width: 100%;
338// border: 1px solid red;
339 overflow: hidden; /* will contain if #first is longer than #second */
340}
341#first {
Marc Kupietzc4893362016-02-25 08:04:46 +0100342 margin-right: 20px;
343 float:left; /* add this */
344// border: 1px solid green;
345}
346#second {
347 border: 1px solid #333;
Marc Kupietz4aa62172016-02-25 10:39:27 +0100348 height: 850px;
Marc Kupietzc4893362016-02-25 08:04:46 +0100349 overflow: hidden; /* if you don't want #second to wrap below #first */
350}
Marc Kupietz4aa62172016-02-25 10:39:27 +0100351#cost {
352 z-index: 1;
353 position: fixed;
354 font-size: 10px;
355 color: #222222;
356 margin-bottom: 10px;
357}
Marc Kupietzc4893362016-02-25 08:04:46 +0100358</style>
359<script>
360
Marc Kupietz4aa62172016-02-25 10:39:27 +0100361var opt = {epsilon: 1, perplexity: 20};
Marc Kupietzc4893362016-02-25 08:04:46 +0100362var T = new tsnejs.tSNE(opt); // create a tSNE instance
363
364var Y;
365
366var data;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100367var labeler;
Marc Kupietzc4893362016-02-25 08:04:46 +0100368
Marc Kupietzc5990da2016-02-26 08:47:12 +0100369
370function applyJitter() {
371 svg.selectAll('.u')
372 .data(labels)
373 .transition()
374 .duration(50)
375 .attr("transform", function(d, i) {
376 T.Y[i][0] = (d.x - 400 - tx)/ss/20;
377 T.Y[i][1] = (d.y - 400 - ty)/ss/20;
378 return "translate(" +
379 (d.x) + "," +
380 (d.y) + ")";
381 });
382}
383
Marc Kupietzc4893362016-02-25 08:04:46 +0100384function updateEmbedding() {
385 var Y = T.getSolution();
386 svg.selectAll('.u')
387 .data(data.words)
Marc Kupietzc5990da2016-02-26 08:47:12 +0100388 .attr("transform", function(d, i) {
389 return "translate(" +
390 ((Y[i][0]*20*ss + tx) + 400) + "," +
391 ((Y[i][1]*20*ss + ty) + 400) + ")"; });
Marc Kupietzc4893362016-02-25 08:04:46 +0100392}
393
394var svg;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100395var labels = [];
396var anchor_array = [];
397var text;
398
Marc Kupietzc4893362016-02-25 08:04:46 +0100399function drawEmbedding() {
Marc Kupietza350bce2016-02-25 09:34:25 +0100400 $("#embed").empty();
401 var div = d3.select("#embed");
402
403 // get min and max in each column of Y
404 var Y = T.Y;
405
406 svg = div.append("svg") // svg is global
407 .attr("width", 800)
408 .attr("height", 800);
409
410 var g = svg.selectAll(".b")
411 .data(data.words)
412 .enter().append("g")
413 .attr("class", "u");
414
415 g.append("a")
416 .attr("xlink:href", function(word) {return "/?word="+word;})
Marc Kupietz5f780672016-02-25 17:15:54 +0100417 .attr("title", function(d, i) {
418 return "rank: "+i +" "+"freq. rank: "+data.ranks[i];
419 })
Marc Kupietza350bce2016-02-25 09:34:25 +0100420 .append("text")
421 .attr("text-anchor", "top")
422 .attr("font-size", 12)
423 .attr("fill", function(d) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100424 if(data.target.indexOf(" "+d+" ") >= 0) {
Marc Kupietza350bce2016-02-25 09:34:25 +0100425 return "red";
426 } else {
427 return "#333"
Marc Kupietzc4893362016-02-25 08:04:46 +0100428 }
Marc Kupietza350bce2016-02-25 09:34:25 +0100429 })
430 .text(function(d) { return d; });
431
432 var zoomListener = d3.behavior.zoom()
433 .scaleExtent([0.1, 10])
434 .center([0,0])
435 .on("zoom", zoomHandler);
436 zoomListener(svg);
437 }
438
439 var tx=0, ty=0;
440 var ss=1;
441 var iter_id=-1;
442
443 function zoomHandler() {
444 tx = d3.event.translate[0];
445 ty = d3.event.translate[1];
446 ss = d3.event.scale;
447 updateEmbedding();
448 }
449
450 var stepnum = 0;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100451
Marc Kupietza350bce2016-02-25 09:34:25 +0100452 function stopStep() {
453 clearInterval(iter_id);
Marc Kupietzc5990da2016-02-26 08:47:12 +0100454 text = svg.selectAll("text");
455
456 labels = d3.range(data.words.length).map(function(i) {
457 var x = (T.Y[i][0]*20*ss + tx) + 400;
458 var y = (T.Y[i][1]*20*ss + ty) + 400;
459 anchor_array.push({x: x, y: y, r: 5});
460 return {
461 x: x,
462 y: y,
463 name: data.words[i]
464 };
465 });
466
467 var index = 0;
468 text.each(function() {
469 labels[index].width = this.getBBox().width;
470 labels[index].height = this.getBBox().height;
471 index += 1;
472 });
473
474
475// setTimeout(updateEmbedding, 1);
476// setTimeout(
477 labeler = d3.labeler()
478 .label(labels)
479 .anchor(anchor_array)
480 .width(800)
481 .height(800)
482 .update(applyJitter);
483// .start(1000);
484
485 iter_id = setInterval(jitterStep, 1);
486
Marc Kupietza350bce2016-02-25 09:34:25 +0100487 }
Marc Kupietzc5990da2016-02-26 08:47:12 +0100488
489var jitter_i=0;;
490
491function jitterStep() {
492 if(jitter_i++ > 100) {
493 clearInterval(iter_id);
494 } else {
495 labeler.start2(10);
496 applyJitter();
497 }
498}
Marc Kupietza350bce2016-02-25 09:34:25 +0100499
500 function step() {
501 var i = T.iter;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100502 if(i > <%= $no_iterations %>) {
Marc Kupietza350bce2016-02-25 09:34:25 +0100503 stopStep();
504 } else {
Marc Kupietz4aa62172016-02-25 10:39:27 +0100505 var cost = Math.round(T.step() *1000) / 1000; // do a few steps
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100506 $("#cost").html("tsne iteration " + i + ", cost: " + cost.toFixed(3));
Marc Kupietza350bce2016-02-25 09:34:25 +0100507 updateEmbedding();
508 }
509 }
Marc Kupietzc5990da2016-02-26 08:47:12 +0100510
Marc Kupietza350bce2016-02-25 09:34:25 +0100511 function showMap(j) {
512 data=j;
513 T.iter=0;
514 T.initDataRaw(data.vecs); // init embedding
515 drawEmbedding(); // draw initial embedding
516
517 if(iter_id >= 0) {
518 clearInterval(iter_id);
519 }
520 //T.debugGrad();
521 iter_id = setInterval(step, 1);
522 //step();
523 }
524
Marc Kupietzc5990da2016-02-26 08:47:12 +0100525 function update() {
526
527 text.transition().duration(800)
528 .attr("transform", transform);
529 }
530
531
Marc Kupietzc4893362016-02-25 08:04:46 +0100532</script>
533</head>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200534<body>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100535 <form action="<%=url_for('/')->to_abs%>" method="GET">
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100536 word(s):
537 <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.">
538 max. neighbours: <input type="text" size="8" name="n" value="<%= $no_nbs %>">
539 iterations: <input type="text" name="N" size="8" value="<%= $no_iterations %>">
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100540 <input type="submit" value="Show">
Marc Kupietz4aa62172016-02-25 10:39:27 +0100541 </form>
542 <br>
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100543 % if($lists) {
Marc Kupietz4aa62172016-02-25 10:39:27 +0100544 <div id="wrapper">
545 <table id="first">
546 <tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100547 <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 +0100548 </tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100549 % my $j=0; my @words; my @vecs; my @ranks; for my $list (@$lists) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100550 % my $i=1; for my $item (@$list) {
551 % if(!grep{$_ eq $item->{word}} @words) {
552 % push @vecs, $item->{vector};
553 % push @words, $item->{word};
Marc Kupietz5f780672016-02-25 17:15:54 +0100554 % push @ranks, $item->{rank};
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100555 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100556 <tr>
557 <td align="right">
558 <%= $i++ %>.
559 </td>
560 <td>
561 <a href="/?word=<%= $item->{word} %>">
562 <%= $item->{word} %>
563 </a>
564 </td>
565 <td align="right">
566 <%= sprintf("%.3f", $item->{dist}) %>
567 </td>
Marc Kupietz5f780672016-02-25 17:15:54 +0100568 <td align="right">
569 <%= $item->{rank} %>
570 </td>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100571 </tr>
572 % }
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100573 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100574 </table>
575 <script>
576 % use Mojo::ByteStream 'b';
577 $(window).load(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100578 showMap(<%= b(Mojo::JSON::to_json({target => " $word ", words => \@words, vecs => \@vecs, ranks => \@ranks})); %>);
Marc Kupietz4aa62172016-02-25 10:39:27 +0100579 });
580 </script>
581 % }
582 <div id="second" style="width:800px; height:800px; font-family: arial;">
583 <div id="embed">
584 </div>
585 <div id="cost"></div>
586 </div>
587 </div>
588 <p>
589 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 +0200590 <pre>
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100591-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 +0100592 </pre>
593 </p>
594</body>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200595</html>
596