blob: 65058a25a35414262bfdcdc973abf04439bc70d1 [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 Kupietzc4d62f82016-03-01 11:04:24 +010017 my $perplexity=$c->param('perplexity') || 25;
18 my $epsilon=$c->param('epsilon') || 5;
19
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010020 my @lists;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010021 if(defined($word) && $word !~ /^\s*$/) {
22 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +010023 $word =~ s/\s+/ /g;
24 for my $w (split(' *\| *', $word)) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010025 $c->app->log->debug('Looking for neighbours of '.$w);
26 push(@lists, get_neighbours(encode("iso-8859-1", $w), $no_nbs));
27 }
Marc Kupietz247500f2015-10-09 11:29:01 +020028 }
Marc Kupietz000ad862016-02-26 14:59:12 +010029 $word =~ s/ *\| */ | /g;
Marc Kupietzc4d62f82016-03-01 11:04:24 +010030 $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 +020031};
32
33app->start;
34
35exit;
36
37__END__
38
39__C__
40#include <stdio.h>
41#include <string.h>
42#include <math.h>
43#include <malloc.h>
44#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +010045#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +010046#include <pthread.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +020047
48#define max_size 2000
49#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010050#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +010051#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +010052#define MAX_THREADS 100
Marc Kupietzdc22b982015-10-09 09:19:34 +020053
54//the thread function
55void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +010056
57typedef struct {
58 long long *index;
59 float *dist;
60 unsigned int length;
61} knn;
62
63
64typedef struct {
65 char *token;
66 int N;
67 unsigned long from;
68 unsigned long upto;
69} knnpars;
70
Marc Kupietzdc22b982015-10-09 09:19:34 +020071float *M;
72char *vocab;
Marc Kupietz82b02672016-02-26 12:32:25 +010073long long words, size;
Marc Kupietz000ad862016-02-26 14:59:12 +010074int num_threads=20;
Marc Kupietzdc22b982015-10-09 09:19:34 +020075
76int init_net(char *file_name) {
Marc Kupietz67c20282016-02-26 09:42:00 +010077 FILE *f, *binvecs, *binwords;
Marc Kupietzf0809762016-02-26 10:13:47 +010078 int binwords_fd, binvecs_fd;
Marc Kupietz82b02672016-02-26 12:32:25 +010079 long long a, b, c, d, cn;
80 float len;
81
Marc Kupietz67c20282016-02-26 09:42:00 +010082 char binvecs_fname[256], binwords_fname[256];
83 strcpy(binwords_fname, file_name);
84 strcat(binwords_fname, ".words");
85 strcpy(binvecs_fname, file_name);
86 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +020087
Marc Kupietzdc22b982015-10-09 09:19:34 +020088 f = fopen(file_name, "rb");
89 if (f == NULL) {
90 printf("Input file %s not found\n", file_name);
91 return -1;
92 }
93 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +010094 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +020095 fscanf(f, "%lld", &size);
Marc Kupietzf0809762016-02-26 10:13:47 +010096 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
Marc Kupietz000ad862016-02-26 14:59:12 +010097 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
98 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
Marc Kupietzf0809762016-02-26 10:13:47 +010099 if (M == MAP_FAILED || vocab == MAP_FAILED) {
100 close(binvecs_fd);
101 close(binwords_fd);
102 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
103 exit(-1);
104 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100105 } else {
Marc Kupietzf0809762016-02-26 10:13:47 +0100106 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
107 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
108 if (M == NULL) {
109 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
110 return -1;
111 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100112 for (b = 0; b < words; b++) {
113 a = 0;
114 while (1) {
115 vocab[b * max_w + a] = fgetc(f);
116 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
117 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
118 }
119 vocab[b * max_w + a] = 0;
120 fread(&M[b * size], sizeof(float), size, f);
121 len = 0;
122 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
123 len = sqrt(len);
124 for (a = 0; a < size; a++) M[a + b * size] /= len;
125 }
126 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
127 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
128 fclose(binvecs);
129 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
130 fclose(binwords);
131 }
132 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200133 fclose(f);
134 return 0;
135}
136
Marc Kupietz000ad862016-02-26 14:59:12 +0100137
138void *_get_neighbours(knnpars *pars) {
139 char *st1 = pars->token;
140 int N = pars->N;
141 unsigned long from = pars -> from;
142 unsigned long upto = pars -> upto;
Marc Kupietz82b02672016-02-26 12:32:25 +0100143 char file_name[max_size], st[100][max_size];
Marc Kupietz000ad862016-02-26 14:59:12 +0100144 float dist, len, *bestd, vec[max_size];
145 long long a, b, c, d, cn, bi[100], *besti;
Marc Kupietz82b02672016-02-26 12:32:25 +0100146 char ch;
Marc Kupietz000ad862016-02-26 14:59:12 +0100147 knn *nbs = NULL;
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100148
Marc Kupietz000ad862016-02-26 14:59:12 +0100149 besti = malloc(N * sizeof(long long));
150 bestd = malloc(N * sizeof(float));
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100151
Marc Kupietz000ad862016-02-26 14:59:12 +0100152 float worstbest=-1;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200153
154 for (a = 0; a < N; a++) bestd[a] = 0;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200155 a = 0;
156 cn = 0;
157 b = 0;
158 c = 0;
159 while (1) {
160 st[cn][b] = st1[c];
161 b++;
162 c++;
163 st[cn][b] = 0;
164 if (st1[c] == 0) break;
165 if (st1[c] == ' ') {
166 cn++;
167 b = 0;
168 c++;
169 }
170 }
171 cn++;
172 for (a = 0; a < cn; a++) {
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100173 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
174 if (b == words) b = -1;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200175 bi[a] = b;
Marc Kupietze8da3062016-02-25 08:37:53 +0100176 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], bi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200177 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100178 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100179 cn--;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200180 break;
181 }
182 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100183 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100184 N = 0;
185 goto end;
186 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200187 for (a = 0; a < size; a++) vec[a] = 0;
188 for (b = 0; b < cn; b++) {
189 if (bi[b] == -1) continue;
190 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
191 }
192 len = 0;
193 for (a = 0; a < size; a++) len += vec[a] * vec[a];
194 len = sqrt(len);
195 for (a = 0; a < size; a++) vec[a] /= len;
196 for (a = 0; a < N; a++) bestd[a] = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100197 for (c = from; c < upto; c++) {
Marc Kupietzdc22b982015-10-09 09:19:34 +0200198 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100199// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100200// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
201// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200202 dist = 0;
203 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100204 if(dist > worstbest) {
205 for (a = 0; a < N; a++) {
206 if (dist > bestd[a]) {
207 for (d = N - 1; d > a; d--) {
208 bestd[d] = bestd[d - 1];
209 besti[d] = besti[d - 1];
210 }
211 bestd[a] = dist;
212 besti[a] = c;
213 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200214 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200215 }
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100216 worstbest = bestd[N-1];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200217 }
218 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100219
Marc Kupietz000ad862016-02-26 14:59:12 +0100220 nbs = malloc(sizeof(knn));
221 nbs->index = besti;
222 nbs->dist = bestd;
223 nbs->length = N;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100224end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100225 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200226}
227
Marc Kupietz000ad862016-02-26 14:59:12 +0100228SV *get_neighbours(char *st1, int N) {
229 float bestd[MAX_NEIGHBOURS], vec[max_size];
230 long long besti[MAX_NEIGHBOURS], a, b, c, d, slice;
231 char *bestw[MAX_NEIGHBOURS];
232 knn *nbs[MAX_THREADS];
233 knnpars pars[MAX_THREADS];
234 pthread_t *pt = (pthread_t *)malloc(num_threads * sizeof(pthread_t));
235 AV* array = newAV();
236
237 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
238
239 slice = words / num_threads;
240
241 for(a=0; a < num_threads; a++) {
242 pars[a].token = st1;
243 pars[a].N = N;
244 pars[a].from = a*slice;
245 pars[a].upto = ((a+1)*slice > words? words:(a+1)*slice);
246 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
247 }
248 for (a = 0; a < num_threads; a++) pthread_join(pt[a], &nbs[a]);
249
250 if(!nbs[0])
251 goto end;
252
253 for(b=0; b < N; b++) {
254 besti[b] = nbs[0]->index[b];
255 bestd[b] = nbs[0]->dist[b];
256 }
257
258 for(a=1; a < num_threads; a++) {
259 for(b=0; b < N; b++) {
260 for(c=0; c < N; c++) {
261 if(nbs[a]->dist[b] > bestd[c]) {
262 for(d=N-1; d>c; d--) {
263 bestd[d] = bestd[d-1];
264 besti[d] = besti[d-1];
265 }
266 besti[c] = nbs[a]->index[b];
267 bestd[c] = nbs[a]->dist[b];
268 break;
269 }
270 }
271 }
272 }
273
274 if(nbs) {
275 for (a = 0; a < N; a++) {
276 bestw[a] = (char *)malloc(max_size * sizeof(char));
277 }
278 for (a = 0; a < N; a++) {
279 strcpy(bestw[a], &vocab[besti[a] * max_w]);
280 HV* hash = newHV();
281 hv_store(hash, "word", strlen("word"), newSVpvf(bestw[a], 0), 0);
282 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
283 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
284 AV *vector = newAV();
285 for (b = 0; b < size; b++) {
286 av_push(vector, newSVnv(M[b + besti[a] * size]));
287 }
288 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
289 av_push(array, newRV_noinc((SV*)hash));
290 }
291 }
292end:
293 return newRV_noinc((SV*)array);
294}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100295
Marc Kupietzdc22b982015-10-09 09:19:34 +0200296__DATA__
297
298@@ index.html.ep
299<!DOCTYPE html>
300<html>
Marc Kupietzc4893362016-02-25 08:04:46 +0100301<head>
302 <title>DeReKo-Word-Vector-Distances</title>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100303 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
Marc Kupietzc4893362016-02-25 08:04:46 +0100304 <script src="http://code.jquery.com/jquery-latest.min.js"></script>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100305 <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
306 <script>
307 $(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100308 $( document ).tooltip({
309 content: function() {
310 return $(this).attr('title');
311 }}
312 )
313 })
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100314 </script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100315 <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
316 <script src="http://klinux10/word2vec/tsne.js"></script>
Marc Kupietzc5990da2016-02-26 08:47:12 +0100317 <script src="http://klinux10/word2vec/labeler.js"></script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100318<style>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100319body, input {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100320 font-family: Arial, sans-serif;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100321 font-size: 11pt;
322}
323
324.ui-tooltip-content {
325 font-size: 9pt;
326 colour: #222222;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100327}
Marc Kupietz5f780672016-02-25 17:15:54 +0100328
329svg > .ui-tooltip-content {
Marc Kupietzb1029362016-02-27 21:38:55 +0100330 font-size: 8pt;
Marc Kupietz5f780672016-02-25 17:15:54 +0100331 colour: #222222;
332}
333
Marc Kupietzc4893362016-02-25 08:04:46 +0100334svg {
Marc Kupietzb1029362016-02-27 21:38:55 +0100335 margin-right: 10px;
336 margin-bottom: 10px;
Marc Kupietzc4893362016-02-25 08:04:46 +0100337}
338#wrapper {
339 width: 100%;
340// border: 1px solid red;
341 overflow: hidden; /* will contain if #first is longer than #second */
342}
343#first {
Marc Kupietzb1029362016-02-27 21:38:55 +0100344 margin-right: 20px;
345 float: left;
346 // border: 1px solid green;
Marc Kupietzc4893362016-02-25 08:04:46 +0100347}
348#second {
349 border: 1px solid #333;
350 overflow: hidden; /* if you don't want #second to wrap below #first */
351}
Marc Kupietz4aa62172016-02-25 10:39:27 +0100352#cost {
Marc Kupietzb1029362016-02-27 21:38:55 +0100353 font-size: 8pt;
354 color: #222222;
355 margin-top: 4px;
Marc Kupietz4aa62172016-02-25 10:39:27 +0100356}
Marc Kupietzc4893362016-02-25 08:04:46 +0100357</style>
358<script>
359
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100360var opt = {epsilon: <%= $epsilon %>, perplexity: <%= $perplexity %>},
Marc Kupietz9fca1732016-02-29 09:07:04 +0100361 mapWidth = 800, // width map
362 mapHeight = 800,
363 jitterRadius = 7;
364
Marc Kupietzc4893362016-02-25 08:04:46 +0100365var T = new tsnejs.tSNE(opt); // create a tSNE instance
366
367var Y;
368
369var data;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100370var labeler;
Marc Kupietzc4893362016-02-25 08:04:46 +0100371
Marc Kupietzc5990da2016-02-26 08:47:12 +0100372
373function applyJitter() {
374 svg.selectAll('.u')
375 .data(labels)
376 .transition()
377 .duration(50)
378 .attr("transform", function(d, i) {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100379 T.Y[i][0] = (d.x - mapWidth/2 - tx)/ss/20;
380 T.Y[i][1] = (d.y - mapHeight/2 - ty)/ss/20;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100381 return "translate(" +
382 (d.x) + "," +
383 (d.y) + ")";
384 });
385}
386
Marc Kupietzc4893362016-02-25 08:04:46 +0100387function updateEmbedding() {
388 var Y = T.getSolution();
389 svg.selectAll('.u')
Marc Kupietz9fca1732016-02-29 09:07:04 +0100390 .data(data.words)
391 .attr("transform", function(d, i) {
392 return "translate(" +
393 ((Y[i][0]*20*ss + tx) + mapWidth/2) + "," +
394 ((Y[i][1]*20*ss + ty) + mapHeight/2) + ")"; });
Marc Kupietzc4893362016-02-25 08:04:46 +0100395}
396
397var svg;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100398var labels = [];
399var anchor_array = [];
400var text;
401
Marc Kupietzc4893362016-02-25 08:04:46 +0100402function drawEmbedding() {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100403 $("#embed").empty();
404 var div = d3.select("#embed");
405
406 // get min and max in each column of Y
407 var Y = T.Y;
408
409 svg = div.append("svg") // svg is global
410 .attr("width", mapWidth)
411 .attr("height", mapHeight);
412
413 var g = svg.selectAll(".b")
414 .data(data.words)
415 .enter().append("g")
416 .attr("class", "u");
417
418 g.append("a")
419 .attr("xlink:href", function(word) {return "/?word="+word;})
420 .attr("title", function(d, i) {
Marc Kupietze50c8162016-03-01 10:24:43 +0100421 return "rank: "+i +" "+"freq. rank: "+data.ranks[i].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
Marc Kupietz9fca1732016-02-29 09:07:04 +0100422 })
Marc Kupietza350bce2016-02-25 09:34:25 +0100423 .append("text")
Marc Kupietz9fca1732016-02-29 09:07:04 +0100424 .attr("text-anchor", "top")
425 .attr("font-size", 12)
426 .attr("fill", function(d) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100427 if(data.target.indexOf(" "+d+" ") >= 0) {
Marc Kupietza350bce2016-02-25 09:34:25 +0100428 return "red";
429 } else {
430 return "#333"
Marc Kupietzc4893362016-02-25 08:04:46 +0100431 }
Marc Kupietz9fca1732016-02-29 09:07:04 +0100432 })
433 .text(function(d) { return d; });
434
435 var zoomListener = d3.behavior.zoom()
436 .scaleExtent([0.1, 10])
437 .center([0,0])
438 .on("zoom", zoomHandler);
439 zoomListener(svg);
440}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100441
Marc Kupietz9fca1732016-02-29 09:07:04 +0100442var tx=0, ty=0;
443var ss=1;
444var iter_id=-1;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100445
Marc Kupietz9fca1732016-02-29 09:07:04 +0100446function zoomHandler() {
447 tx = d3.event.translate[0];
448 ty = d3.event.translate[1];
449 ss = d3.event.scale;
450 updateEmbedding();
451}
452
453var stepnum = 0;
454
455function stopStep() {
456 clearInterval(iter_id);
457 text = svg.selectAll("text");
458
459 // jitter function needs different data and co-ordinate representation
460 labels = d3.range(data.words.length).map(function(i) {
461 var x = (T.Y[i][0]*20*ss + tx) + mapWidth/2;
462 var y = (T.Y[i][1]*20*ss + ty) + mapHeight/2;
463 anchor_array.push({x: x, y: y, r: jitterRadius});
464 return {
465 x: x,
466 y: y,
467 name: data.words[i]
468 };
469 });
470
471 // get the actual label bounding boxes for the jitter function
472 var index = 0;
473 text.each(function() {
474 labels[index].width = this.getBBox().width;
475 labels[index].height = this.getBBox().height;
476 index += 1;
477 });
Marc Kupietzc5990da2016-02-26 08:47:12 +0100478
479
480// setTimeout(updateEmbedding, 1);
481// setTimeout(
Marc Kupietz9fca1732016-02-29 09:07:04 +0100482 labeler = d3.labeler()
483 .label(labels)
484 .anchor(anchor_array)
485 .width(mapWidth)
486 .height(mapHeight)
487 .update(applyJitter);
488 // .start(1000);
Marc Kupietzc5990da2016-02-26 08:47:12 +0100489
Marc Kupietz9fca1732016-02-29 09:07:04 +0100490 iter_id = setInterval(jitterStep, 1);
491}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100492
Marc Kupietz9fca1732016-02-29 09:07:04 +0100493var jitter_i=0;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100494
495function jitterStep() {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100496 if(jitter_i++ > 100) {
497 clearInterval(iter_id);
498 } else {
499 labeler.start2(10);
500 applyJitter();
501 }
Marc Kupietzc5990da2016-02-26 08:47:12 +0100502}
Marc Kupietzb1029362016-02-27 21:38:55 +0100503
504var last_cost=1000;
505
Marc Kupietz9fca1732016-02-29 09:07:04 +0100506function step() {
507 var i = T.iter;
508
509 if(i > <%= $no_iterations %>) {
510 stopStep();
511 } else {
512 var cost = Math.round(T.step() * 100000) / 100000; // do a few steps
513 $("#cost").html("tsne iteration " + i + ", cost: " + cost.toFixed(5));
514 if(i % 250 == 0 && cost >= last_cost) {
515 stopStep();
516 } else {
517 last_cost = cost;
518 updateEmbedding();
519 }
520 }
521}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100522
Marc Kupietz9fca1732016-02-29 09:07:04 +0100523function showMap(j) {
524 data=j;
525 T.iter=0;
526 T.initDataRaw(data.vecs); // init embedding
527 drawEmbedding(); // draw initial embedding
528
529 if(iter_id >= 0) {
530 clearInterval(iter_id);
531 }
532 //T.debugGrad();
533 iter_id = setInterval(step, 1);
534 //step();
535}
Marc Kupietza350bce2016-02-25 09:34:25 +0100536
Marc Kupietzc4893362016-02-25 08:04:46 +0100537</script>
538</head>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200539<body>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100540 <form action="<%=url_for('/')->to_abs%>" method="GET">
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100541 word(s):
542 <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.">
543 max. neighbours: <input type="text" size="8" name="n" value="<%= $no_nbs %>">
544 iterations: <input type="text" name="N" size="8" value="<%= $no_iterations %>">
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100545 <input type="submit" value="Show">
Marc Kupietz4aa62172016-02-25 10:39:27 +0100546 </form>
547 <br>
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100548 % if($lists) {
Marc Kupietz4aa62172016-02-25 10:39:27 +0100549 <div id="wrapper">
550 <table id="first">
551 <tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100552 <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 +0100553 </tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100554 % my $j=0; my @words; my @vecs; my @ranks; for my $list (@$lists) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100555 % my $i=1; for my $item (@$list) {
556 % if(!grep{$_ eq $item->{word}} @words) {
557 % push @vecs, $item->{vector};
558 % push @words, $item->{word};
Marc Kupietz5f780672016-02-25 17:15:54 +0100559 % push @ranks, $item->{rank};
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100560 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100561 <tr>
562 <td align="right">
563 <%= $i++ %>.
564 </td>
565 <td>
566 <a href="/?word=<%= $item->{word} %>">
567 <%= $item->{word} %>
568 </a>
569 </td>
570 <td align="right">
571 <%= sprintf("%.3f", $item->{dist}) %>
572 </td>
Marc Kupietz5f780672016-02-25 17:15:54 +0100573 <td align="right">
574 <%= $item->{rank} %>
575 </td>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100576 </tr>
577 % }
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100578 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100579 </table>
580 <script>
581 % use Mojo::ByteStream 'b';
582 $(window).load(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100583 showMap(<%= b(Mojo::JSON::to_json({target => " $word ", words => \@words, vecs => \@vecs, ranks => \@ranks})); %>);
Marc Kupietz4aa62172016-02-25 10:39:27 +0100584 });
585 </script>
586 % }
587 <div id="second" style="width:800px; height:800px; font-family: arial;">
588 <div id="embed">
589 </div>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100590 </div>
Marc Kupietzb1029362016-02-27 21:38:55 +0100591 <div id="cost"></div>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100592 </div>
593 <p>
594 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 +0200595 <pre>
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100596-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 +0100597 </pre>
598 </p>
599</body>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200600</html>
601