blob: 0b8f178ecbe922c572f4cd6110450bacec15d72d [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 Kupietzd51cc5c2016-02-27 22:42:49 +010070HV* wordhash;
Marc Kupietz82b02672016-02-26 12:32:25 +010071long long words, size;
Marc Kupietz000ad862016-02-26 14:59:12 +010072int num_threads=20;
Marc Kupietzdc22b982015-10-09 09:19:34 +020073
74int init_net(char *file_name) {
Marc Kupietz67c20282016-02-26 09:42:00 +010075 FILE *f, *binvecs, *binwords;
Marc Kupietzf0809762016-02-26 10:13:47 +010076 int binwords_fd, binvecs_fd;
Marc Kupietz82b02672016-02-26 12:32:25 +010077 long long a, b, c, d, cn;
78 float len;
79
Marc Kupietz67c20282016-02-26 09:42:00 +010080 char binvecs_fname[256], binwords_fname[256];
81 strcpy(binwords_fname, file_name);
82 strcat(binwords_fname, ".words");
83 strcpy(binvecs_fname, file_name);
84 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +020085
Marc Kupietzdc22b982015-10-09 09:19:34 +020086 f = fopen(file_name, "rb");
87 if (f == NULL) {
88 printf("Input file %s not found\n", file_name);
89 return -1;
90 }
91 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +010092 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +020093 fscanf(f, "%lld", &size);
Marc Kupietzf0809762016-02-26 10:13:47 +010094 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
Marc Kupietz000ad862016-02-26 14:59:12 +010095 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
96 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
Marc Kupietzf0809762016-02-26 10:13:47 +010097 if (M == MAP_FAILED || vocab == MAP_FAILED) {
98 close(binvecs_fd);
99 close(binwords_fd);
100 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
101 exit(-1);
102 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100103 } else {
Marc Kupietzf0809762016-02-26 10:13:47 +0100104 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
105 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
106 if (M == NULL) {
107 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
108 return -1;
109 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100110 for (b = 0; b < words; b++) {
111 a = 0;
112 while (1) {
113 vocab[b * max_w + a] = fgetc(f);
114 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
115 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
116 }
117 vocab[b * max_w + a] = 0;
118 fread(&M[b * size], sizeof(float), size, f);
119 len = 0;
120 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
121 len = sqrt(len);
122 for (a = 0; a < size; a++) M[a + b * size] /= len;
123 }
124 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
125 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
126 fclose(binvecs);
127 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
128 fclose(binwords);
129 }
130 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200131 fclose(f);
Marc Kupietzd51cc5c2016-02-27 22:42:49 +0100132
133 wordhash = newHV();
134 for (a = 0; a < words; a++) {
135 hv_store(wordhash, &vocab[a * max_w], strlen(&vocab[a * max_w]), newSVuv(a), 0);
136 // fprintf(stderr, "%lld: %s\n", a, &vocab[a * max_w]);
137 }
138
Marc Kupietzdc22b982015-10-09 09:19:34 +0200139 return 0;
140}
141
Marc Kupietz000ad862016-02-26 14:59:12 +0100142
143void *_get_neighbours(knnpars *pars) {
144 char *st1 = pars->token;
145 int N = pars->N;
146 unsigned long from = pars -> from;
147 unsigned long upto = pars -> upto;
Marc Kupietz82b02672016-02-26 12:32:25 +0100148 char file_name[max_size], st[100][max_size];
Marc Kupietz000ad862016-02-26 14:59:12 +0100149 float dist, len, *bestd, vec[max_size];
150 long long a, b, c, d, cn, bi[100], *besti;
Marc Kupietz82b02672016-02-26 12:32:25 +0100151 char ch;
Marc Kupietz000ad862016-02-26 14:59:12 +0100152 knn *nbs = NULL;
Marc Kupietzd51cc5c2016-02-27 22:42:49 +0100153 SV **svp;
154
Marc Kupietz000ad862016-02-26 14:59:12 +0100155 besti = malloc(N * sizeof(long long));
156 bestd = malloc(N * sizeof(float));
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100157
Marc Kupietz000ad862016-02-26 14:59:12 +0100158 float worstbest=-1;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200159
160 for (a = 0; a < N; a++) bestd[a] = 0;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200161 a = 0;
162 cn = 0;
163 b = 0;
164 c = 0;
165 while (1) {
166 st[cn][b] = st1[c];
167 b++;
168 c++;
169 st[cn][b] = 0;
170 if (st1[c] == 0) break;
171 if (st1[c] == ' ') {
172 cn++;
173 b = 0;
174 c++;
175 }
176 }
177 cn++;
178 for (a = 0; a < cn; a++) {
Marc Kupietzd51cc5c2016-02-27 22:42:49 +0100179 svp = hv_fetch(wordhash,st[a],strlen(st[a]),0);
180 if (svp) {
181 b = SvUV(*svp);
182 } else {
183 b = -1;
184 }
185 // for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
186 // if (b == words) b = -1;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200187 bi[a] = b;
Marc Kupietze8da3062016-02-25 08:37:53 +0100188 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], bi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200189 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100190 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100191 cn--;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200192 break;
193 }
194 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100195 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100196 N = 0;
197 goto end;
198 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200199 for (a = 0; a < size; a++) vec[a] = 0;
200 for (b = 0; b < cn; b++) {
201 if (bi[b] == -1) continue;
202 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
203 }
204 len = 0;
205 for (a = 0; a < size; a++) len += vec[a] * vec[a];
206 len = sqrt(len);
207 for (a = 0; a < size; a++) vec[a] /= len;
208 for (a = 0; a < N; a++) bestd[a] = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100209 for (c = from; c < upto; c++) {
Marc Kupietzdc22b982015-10-09 09:19:34 +0200210 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100211// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100212// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
213// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200214 dist = 0;
215 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100216 if(dist > worstbest) {
217 for (a = 0; a < N; a++) {
218 if (dist > bestd[a]) {
219 for (d = N - 1; d > a; d--) {
220 bestd[d] = bestd[d - 1];
221 besti[d] = besti[d - 1];
222 }
223 bestd[a] = dist;
224 besti[a] = c;
225 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200226 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200227 }
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100228 worstbest = bestd[N-1];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200229 }
230 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100231
Marc Kupietz000ad862016-02-26 14:59:12 +0100232 nbs = malloc(sizeof(knn));
233 nbs->index = besti;
234 nbs->dist = bestd;
235 nbs->length = N;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100236end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100237 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200238}
239
Marc Kupietz000ad862016-02-26 14:59:12 +0100240SV *get_neighbours(char *st1, int N) {
241 float bestd[MAX_NEIGHBOURS], vec[max_size];
242 long long besti[MAX_NEIGHBOURS], a, b, c, d, slice;
243 char *bestw[MAX_NEIGHBOURS];
244 knn *nbs[MAX_THREADS];
245 knnpars pars[MAX_THREADS];
246 pthread_t *pt = (pthread_t *)malloc(num_threads * sizeof(pthread_t));
247 AV* array = newAV();
248
249 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
250
251 slice = words / num_threads;
252
253 for(a=0; a < num_threads; a++) {
254 pars[a].token = st1;
255 pars[a].N = N;
256 pars[a].from = a*slice;
257 pars[a].upto = ((a+1)*slice > words? words:(a+1)*slice);
258 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
259 }
260 for (a = 0; a < num_threads; a++) pthread_join(pt[a], &nbs[a]);
261
262 if(!nbs[0])
263 goto end;
264
265 for(b=0; b < N; b++) {
266 besti[b] = nbs[0]->index[b];
267 bestd[b] = nbs[0]->dist[b];
268 }
269
270 for(a=1; a < num_threads; a++) {
271 for(b=0; b < N; b++) {
272 for(c=0; c < N; c++) {
273 if(nbs[a]->dist[b] > bestd[c]) {
274 for(d=N-1; d>c; d--) {
275 bestd[d] = bestd[d-1];
276 besti[d] = besti[d-1];
277 }
278 besti[c] = nbs[a]->index[b];
279 bestd[c] = nbs[a]->dist[b];
280 break;
281 }
282 }
283 }
284 }
285
286 if(nbs) {
287 for (a = 0; a < N; a++) {
288 bestw[a] = (char *)malloc(max_size * sizeof(char));
289 }
290 for (a = 0; a < N; a++) {
291 strcpy(bestw[a], &vocab[besti[a] * max_w]);
292 HV* hash = newHV();
293 hv_store(hash, "word", strlen("word"), newSVpvf(bestw[a], 0), 0);
294 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
295 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
296 AV *vector = newAV();
297 for (b = 0; b < size; b++) {
298 av_push(vector, newSVnv(M[b + besti[a] * size]));
299 }
300 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
301 av_push(array, newRV_noinc((SV*)hash));
302 }
303 }
304end:
305 return newRV_noinc((SV*)array);
306}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100307
Marc Kupietzdc22b982015-10-09 09:19:34 +0200308__DATA__
309
310@@ index.html.ep
311<!DOCTYPE html>
312<html>
Marc Kupietzc4893362016-02-25 08:04:46 +0100313<head>
314 <title>DeReKo-Word-Vector-Distances</title>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100315 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
Marc Kupietzc4893362016-02-25 08:04:46 +0100316 <script src="http://code.jquery.com/jquery-latest.min.js"></script>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100317 <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
318 <script>
319 $(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100320 $( document ).tooltip({
321 content: function() {
322 return $(this).attr('title');
323 }}
324 )
325 })
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100326 </script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100327 <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
328 <script src="http://klinux10/word2vec/tsne.js"></script>
Marc Kupietzc5990da2016-02-26 08:47:12 +0100329 <script src="http://klinux10/word2vec/labeler.js"></script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100330<style>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100331body, input {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100332 font-family: Arial, sans-serif;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100333 font-size: 11pt;
334}
335
336.ui-tooltip-content {
337 font-size: 9pt;
338 colour: #222222;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100339}
Marc Kupietz5f780672016-02-25 17:15:54 +0100340
341svg > .ui-tooltip-content {
Marc Kupietzb1029362016-02-27 21:38:55 +0100342 font-size: 8pt;
Marc Kupietz5f780672016-02-25 17:15:54 +0100343 colour: #222222;
344}
345
Marc Kupietzc4893362016-02-25 08:04:46 +0100346svg {
Marc Kupietzb1029362016-02-27 21:38:55 +0100347 margin-right: 10px;
348 margin-bottom: 10px;
Marc Kupietzc4893362016-02-25 08:04:46 +0100349}
350#wrapper {
351 width: 100%;
352// border: 1px solid red;
353 overflow: hidden; /* will contain if #first is longer than #second */
354}
355#first {
Marc Kupietzb1029362016-02-27 21:38:55 +0100356 margin-right: 20px;
357 float: left;
358 // border: 1px solid green;
Marc Kupietzc4893362016-02-25 08:04:46 +0100359}
360#second {
361 border: 1px solid #333;
362 overflow: hidden; /* if you don't want #second to wrap below #first */
363}
Marc Kupietz4aa62172016-02-25 10:39:27 +0100364#cost {
Marc Kupietzb1029362016-02-27 21:38:55 +0100365 font-size: 8pt;
366 color: #222222;
367 margin-top: 4px;
Marc Kupietz4aa62172016-02-25 10:39:27 +0100368}
Marc Kupietzc4893362016-02-25 08:04:46 +0100369</style>
370<script>
371
Marc Kupietz4aa62172016-02-25 10:39:27 +0100372var opt = {epsilon: 1, perplexity: 20};
Marc Kupietzc4893362016-02-25 08:04:46 +0100373var T = new tsnejs.tSNE(opt); // create a tSNE instance
374
375var Y;
376
377var data;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100378var labeler;
Marc Kupietzc4893362016-02-25 08:04:46 +0100379
Marc Kupietzc5990da2016-02-26 08:47:12 +0100380
381function applyJitter() {
382 svg.selectAll('.u')
383 .data(labels)
384 .transition()
385 .duration(50)
386 .attr("transform", function(d, i) {
387 T.Y[i][0] = (d.x - 400 - tx)/ss/20;
388 T.Y[i][1] = (d.y - 400 - ty)/ss/20;
389 return "translate(" +
390 (d.x) + "," +
391 (d.y) + ")";
392 });
393}
394
Marc Kupietzc4893362016-02-25 08:04:46 +0100395function updateEmbedding() {
396 var Y = T.getSolution();
397 svg.selectAll('.u')
398 .data(data.words)
Marc Kupietzc5990da2016-02-26 08:47:12 +0100399 .attr("transform", function(d, i) {
400 return "translate(" +
401 ((Y[i][0]*20*ss + tx) + 400) + "," +
402 ((Y[i][1]*20*ss + ty) + 400) + ")"; });
Marc Kupietzc4893362016-02-25 08:04:46 +0100403}
404
405var svg;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100406var labels = [];
407var anchor_array = [];
408var text;
409
Marc Kupietzc4893362016-02-25 08:04:46 +0100410function drawEmbedding() {
Marc Kupietza350bce2016-02-25 09:34:25 +0100411 $("#embed").empty();
412 var div = d3.select("#embed");
413
414 // get min and max in each column of Y
415 var Y = T.Y;
416
417 svg = div.append("svg") // svg is global
418 .attr("width", 800)
419 .attr("height", 800);
420
421 var g = svg.selectAll(".b")
422 .data(data.words)
423 .enter().append("g")
424 .attr("class", "u");
425
426 g.append("a")
427 .attr("xlink:href", function(word) {return "/?word="+word;})
Marc Kupietz5f780672016-02-25 17:15:54 +0100428 .attr("title", function(d, i) {
429 return "rank: "+i +" "+"freq. rank: "+data.ranks[i];
430 })
Marc Kupietza350bce2016-02-25 09:34:25 +0100431 .append("text")
432 .attr("text-anchor", "top")
433 .attr("font-size", 12)
434 .attr("fill", function(d) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100435 if(data.target.indexOf(" "+d+" ") >= 0) {
Marc Kupietza350bce2016-02-25 09:34:25 +0100436 return "red";
437 } else {
438 return "#333"
Marc Kupietzc4893362016-02-25 08:04:46 +0100439 }
Marc Kupietza350bce2016-02-25 09:34:25 +0100440 })
441 .text(function(d) { return d; });
442
443 var zoomListener = d3.behavior.zoom()
444 .scaleExtent([0.1, 10])
445 .center([0,0])
446 .on("zoom", zoomHandler);
447 zoomListener(svg);
448 }
449
450 var tx=0, ty=0;
451 var ss=1;
452 var iter_id=-1;
453
454 function zoomHandler() {
455 tx = d3.event.translate[0];
456 ty = d3.event.translate[1];
457 ss = d3.event.scale;
458 updateEmbedding();
459 }
460
461 var stepnum = 0;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100462
Marc Kupietza350bce2016-02-25 09:34:25 +0100463 function stopStep() {
464 clearInterval(iter_id);
Marc Kupietzc5990da2016-02-26 08:47:12 +0100465 text = svg.selectAll("text");
466
467 labels = d3.range(data.words.length).map(function(i) {
468 var x = (T.Y[i][0]*20*ss + tx) + 400;
469 var y = (T.Y[i][1]*20*ss + ty) + 400;
470 anchor_array.push({x: x, y: y, r: 5});
471 return {
472 x: x,
473 y: y,
474 name: data.words[i]
475 };
476 });
477
478 var index = 0;
479 text.each(function() {
480 labels[index].width = this.getBBox().width;
481 labels[index].height = this.getBBox().height;
482 index += 1;
483 });
484
485
486// setTimeout(updateEmbedding, 1);
487// setTimeout(
488 labeler = d3.labeler()
489 .label(labels)
490 .anchor(anchor_array)
491 .width(800)
492 .height(800)
493 .update(applyJitter);
494// .start(1000);
495
496 iter_id = setInterval(jitterStep, 1);
497
Marc Kupietza350bce2016-02-25 09:34:25 +0100498 }
Marc Kupietzc5990da2016-02-26 08:47:12 +0100499
500var jitter_i=0;;
501
502function jitterStep() {
503 if(jitter_i++ > 100) {
504 clearInterval(iter_id);
505 } else {
506 labeler.start2(10);
507 applyJitter();
508 }
509}
Marc Kupietzb1029362016-02-27 21:38:55 +0100510
511var last_cost=1000;
512
Marc Kupietza350bce2016-02-25 09:34:25 +0100513 function step() {
514 var i = T.iter;
Marc Kupietzb1029362016-02-27 21:38:55 +0100515
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100516 if(i > <%= $no_iterations %>) {
Marc Kupietza350bce2016-02-25 09:34:25 +0100517 stopStep();
518 } else {
Marc Kupietzb1029362016-02-27 21:38:55 +0100519 var cost = Math.round(T.step() *100000) / 100000; // do a few steps
520 $("#cost").html("tsne iteration " + i + ", cost: " + cost.toFixed(5));
521 if(i % 250 == 0 && cost >= last_cost) {
522 stopStep();
523 } else {
524 last_cost = cost;
525 updateEmbedding();
526 }
Marc Kupietza350bce2016-02-25 09:34:25 +0100527 }
528 }
Marc Kupietzc5990da2016-02-26 08:47:12 +0100529
Marc Kupietza350bce2016-02-25 09:34:25 +0100530 function showMap(j) {
531 data=j;
532 T.iter=0;
533 T.initDataRaw(data.vecs); // init embedding
534 drawEmbedding(); // draw initial embedding
535
536 if(iter_id >= 0) {
537 clearInterval(iter_id);
538 }
539 //T.debugGrad();
540 iter_id = setInterval(step, 1);
541 //step();
542 }
543
Marc Kupietzc5990da2016-02-26 08:47:12 +0100544 function update() {
545
546 text.transition().duration(800)
547 .attr("transform", transform);
548 }
549
550
Marc Kupietzc4893362016-02-25 08:04:46 +0100551</script>
552</head>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200553<body>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100554 <form action="<%=url_for('/')->to_abs%>" method="GET">
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100555 word(s):
556 <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.">
557 max. neighbours: <input type="text" size="8" name="n" value="<%= $no_nbs %>">
558 iterations: <input type="text" name="N" size="8" value="<%= $no_iterations %>">
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100559 <input type="submit" value="Show">
Marc Kupietz4aa62172016-02-25 10:39:27 +0100560 </form>
561 <br>
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100562 % if($lists) {
Marc Kupietz4aa62172016-02-25 10:39:27 +0100563 <div id="wrapper">
564 <table id="first">
565 <tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100566 <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 +0100567 </tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100568 % my $j=0; my @words; my @vecs; my @ranks; for my $list (@$lists) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100569 % my $i=1; for my $item (@$list) {
570 % if(!grep{$_ eq $item->{word}} @words) {
571 % push @vecs, $item->{vector};
572 % push @words, $item->{word};
Marc Kupietz5f780672016-02-25 17:15:54 +0100573 % push @ranks, $item->{rank};
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100574 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100575 <tr>
576 <td align="right">
577 <%= $i++ %>.
578 </td>
579 <td>
580 <a href="/?word=<%= $item->{word} %>">
581 <%= $item->{word} %>
582 </a>
583 </td>
584 <td align="right">
585 <%= sprintf("%.3f", $item->{dist}) %>
586 </td>
Marc Kupietz5f780672016-02-25 17:15:54 +0100587 <td align="right">
588 <%= $item->{rank} %>
589 </td>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100590 </tr>
591 % }
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100592 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100593 </table>
594 <script>
595 % use Mojo::ByteStream 'b';
596 $(window).load(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100597 showMap(<%= b(Mojo::JSON::to_json({target => " $word ", words => \@words, vecs => \@vecs, ranks => \@ranks})); %>);
Marc Kupietz4aa62172016-02-25 10:39:27 +0100598 });
599 </script>
600 % }
601 <div id="second" style="width:800px; height:800px; font-family: arial;">
602 <div id="embed">
603 </div>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100604 </div>
Marc Kupietzb1029362016-02-27 21:38:55 +0100605 <div id="cost"></div>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100606 </div>
607 <p>
608 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 +0200609 <pre>
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100610-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 +0100611 </pre>
612 </p>
613</body>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200614</html>
615