blob: 2cd708cfe5b2c7412da98d7021b92708607c35e3 [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 Kupietz34a3ee92016-02-27 22:43:16 +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++) {
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100170 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
171 if (b == words) b = -1;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200172 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 {
Marc Kupietzb1029362016-02-27 21:38:55 +0100327 font-size: 8pt;
Marc Kupietz5f780672016-02-25 17:15:54 +0100328 colour: #222222;
329}
330
Marc Kupietzc4893362016-02-25 08:04:46 +0100331svg {
Marc Kupietzb1029362016-02-27 21:38:55 +0100332 margin-right: 10px;
333 margin-bottom: 10px;
Marc Kupietzc4893362016-02-25 08:04:46 +0100334}
335#wrapper {
336 width: 100%;
337// border: 1px solid red;
338 overflow: hidden; /* will contain if #first is longer than #second */
339}
340#first {
Marc Kupietzb1029362016-02-27 21:38:55 +0100341 margin-right: 20px;
342 float: left;
343 // border: 1px solid green;
Marc Kupietzc4893362016-02-25 08:04:46 +0100344}
345#second {
346 border: 1px solid #333;
347 overflow: hidden; /* if you don't want #second to wrap below #first */
348}
Marc Kupietz4aa62172016-02-25 10:39:27 +0100349#cost {
Marc Kupietzb1029362016-02-27 21:38:55 +0100350 font-size: 8pt;
351 color: #222222;
352 margin-top: 4px;
Marc Kupietz4aa62172016-02-25 10:39:27 +0100353}
Marc Kupietzc4893362016-02-25 08:04:46 +0100354</style>
355<script>
356
Marc Kupietz9fca1732016-02-29 09:07:04 +0100357var opt = {epsilon: 1, perplexity: 20},
358 mapWidth = 800, // width map
359 mapHeight = 800,
360 jitterRadius = 7;
361
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) {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100376 T.Y[i][0] = (d.x - mapWidth/2 - tx)/ss/20;
377 T.Y[i][1] = (d.y - mapHeight/2 - ty)/ss/20;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100378 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')
Marc Kupietz9fca1732016-02-29 09:07:04 +0100387 .data(data.words)
388 .attr("transform", function(d, i) {
389 return "translate(" +
390 ((Y[i][0]*20*ss + tx) + mapWidth/2) + "," +
391 ((Y[i][1]*20*ss + ty) + mapHeight/2) + ")"; });
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 Kupietz9fca1732016-02-29 09:07:04 +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", mapWidth)
408 .attr("height", mapHeight);
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;})
417 .attr("title", function(d, i) {
Marc Kupietze50c8162016-03-01 10:24:43 +0100418 return "rank: "+i +" "+"freq. rank: "+data.ranks[i].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
Marc Kupietz9fca1732016-02-29 09:07:04 +0100419 })
Marc Kupietza350bce2016-02-25 09:34:25 +0100420 .append("text")
Marc Kupietz9fca1732016-02-29 09:07:04 +0100421 .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 Kupietz9fca1732016-02-29 09:07:04 +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}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100438
Marc Kupietz9fca1732016-02-29 09:07:04 +0100439var tx=0, ty=0;
440var ss=1;
441var iter_id=-1;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100442
Marc Kupietz9fca1732016-02-29 09:07:04 +0100443function zoomHandler() {
444 tx = d3.event.translate[0];
445 ty = d3.event.translate[1];
446 ss = d3.event.scale;
447 updateEmbedding();
448}
449
450var stepnum = 0;
451
452function stopStep() {
453 clearInterval(iter_id);
454 text = svg.selectAll("text");
455
456 // jitter function needs different data and co-ordinate representation
457 labels = d3.range(data.words.length).map(function(i) {
458 var x = (T.Y[i][0]*20*ss + tx) + mapWidth/2;
459 var y = (T.Y[i][1]*20*ss + ty) + mapHeight/2;
460 anchor_array.push({x: x, y: y, r: jitterRadius});
461 return {
462 x: x,
463 y: y,
464 name: data.words[i]
465 };
466 });
467
468 // get the actual label bounding boxes for the jitter function
469 var index = 0;
470 text.each(function() {
471 labels[index].width = this.getBBox().width;
472 labels[index].height = this.getBBox().height;
473 index += 1;
474 });
Marc Kupietzc5990da2016-02-26 08:47:12 +0100475
476
477// setTimeout(updateEmbedding, 1);
478// setTimeout(
Marc Kupietz9fca1732016-02-29 09:07:04 +0100479 labeler = d3.labeler()
480 .label(labels)
481 .anchor(anchor_array)
482 .width(mapWidth)
483 .height(mapHeight)
484 .update(applyJitter);
485 // .start(1000);
Marc Kupietzc5990da2016-02-26 08:47:12 +0100486
Marc Kupietz9fca1732016-02-29 09:07:04 +0100487 iter_id = setInterval(jitterStep, 1);
488}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100489
Marc Kupietz9fca1732016-02-29 09:07:04 +0100490var jitter_i=0;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100491
492function jitterStep() {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100493 if(jitter_i++ > 100) {
494 clearInterval(iter_id);
495 } else {
496 labeler.start2(10);
497 applyJitter();
498 }
Marc Kupietzc5990da2016-02-26 08:47:12 +0100499}
Marc Kupietzb1029362016-02-27 21:38:55 +0100500
501var last_cost=1000;
502
Marc Kupietz9fca1732016-02-29 09:07:04 +0100503function step() {
504 var i = T.iter;
505
506 if(i > <%= $no_iterations %>) {
507 stopStep();
508 } else {
509 var cost = Math.round(T.step() * 100000) / 100000; // do a few steps
510 $("#cost").html("tsne iteration " + i + ", cost: " + cost.toFixed(5));
511 if(i % 250 == 0 && cost >= last_cost) {
512 stopStep();
513 } else {
514 last_cost = cost;
515 updateEmbedding();
516 }
517 }
518}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100519
Marc Kupietz9fca1732016-02-29 09:07:04 +0100520function showMap(j) {
521 data=j;
522 T.iter=0;
523 T.initDataRaw(data.vecs); // init embedding
524 drawEmbedding(); // draw initial embedding
525
526 if(iter_id >= 0) {
527 clearInterval(iter_id);
528 }
529 //T.debugGrad();
530 iter_id = setInterval(step, 1);
531 //step();
532}
Marc Kupietza350bce2016-02-25 09:34:25 +0100533
Marc Kupietzc4893362016-02-25 08:04:46 +0100534</script>
535</head>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200536<body>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100537 <form action="<%=url_for('/')->to_abs%>" method="GET">
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100538 word(s):
539 <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.">
540 max. neighbours: <input type="text" size="8" name="n" value="<%= $no_nbs %>">
541 iterations: <input type="text" name="N" size="8" value="<%= $no_iterations %>">
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100542 <input type="submit" value="Show">
Marc Kupietz4aa62172016-02-25 10:39:27 +0100543 </form>
544 <br>
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100545 % if($lists) {
Marc Kupietz4aa62172016-02-25 10:39:27 +0100546 <div id="wrapper">
547 <table id="first">
548 <tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100549 <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 +0100550 </tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100551 % my $j=0; my @words; my @vecs; my @ranks; for my $list (@$lists) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100552 % my $i=1; for my $item (@$list) {
553 % if(!grep{$_ eq $item->{word}} @words) {
554 % push @vecs, $item->{vector};
555 % push @words, $item->{word};
Marc Kupietz5f780672016-02-25 17:15:54 +0100556 % push @ranks, $item->{rank};
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100557 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100558 <tr>
559 <td align="right">
560 <%= $i++ %>.
561 </td>
562 <td>
563 <a href="/?word=<%= $item->{word} %>">
564 <%= $item->{word} %>
565 </a>
566 </td>
567 <td align="right">
568 <%= sprintf("%.3f", $item->{dist}) %>
569 </td>
Marc Kupietz5f780672016-02-25 17:15:54 +0100570 <td align="right">
571 <%= $item->{rank} %>
572 </td>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100573 </tr>
574 % }
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100575 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100576 </table>
577 <script>
578 % use Mojo::ByteStream 'b';
579 $(window).load(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100580 showMap(<%= b(Mojo::JSON::to_json({target => " $word ", words => \@words, vecs => \@vecs, ranks => \@ranks})); %>);
Marc Kupietz4aa62172016-02-25 10:39:27 +0100581 });
582 </script>
583 % }
584 <div id="second" style="width:800px; height:800px; font-family: arial;">
585 <div id="embed">
586 </div>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100587 </div>
Marc Kupietzb1029362016-02-27 21:38:55 +0100588 <div id="cost"></div>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100589 </div>
590 <p>
591 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 +0200592 <pre>
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100593-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 +0100594 </pre>
595 </p>
596</body>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200597</html>
598