blob: 2df0a532c07b396f8b934fb4bdc3fe21dea67201 [file] [log] [blame]
Marc Kupietzdc22b982015-10-09 09:19:34 +02001#!/usr/local/bin/perl
2use Inline C;
3use Mojolicious::Lite;
Marc Kupietzc4893362016-02-25 08:04:46 +01004use Mojo::JSON qw(decode_json encode_json to_json);
Marc Kupietz247500f2015-10-09 11:29:01 +02005use Encode qw(decode encode);
Marc Kupietz7bc85fd2016-02-24 11:42:41 +01006use Mojo::Server::Daemon;
Marc Kupietzd4227392016-03-01 16:45:12 +01007plugin 'Log::Access';
Marc Kupietzdc22b982015-10-09 09:19:34 +02008
Marc Kupietzc5990da2016-02-26 08:47:12 +01009print STDERR $ARGV[0];
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010010# -cbow 1 -size 200 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 40 -binary 1 -iter 15
Marc Kupietz44bee3c2016-02-25 16:26:29 +010011init_net("vectors15.bin");
Marc Kupietzdc22b982015-10-09 09:19:34 +020012
13get '/' => sub {
14 my $c = shift;
15 my $word=$c->param('word');
Marc Kupietz44bee3c2016-02-25 16:26:29 +010016 my $no_nbs=$c->param('n') || 100;
17 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +010018 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +010019 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +010020 my $som=$c->param('som') || 0;
Marc Kupietzd4227392016-03-01 16:45:12 +010021
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010022 my @lists;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010023 if(defined($word) && $word !~ /^\s*$/) {
24 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +010025 $word =~ s/\s+/ /g;
26 for my $w (split(' *\| *', $word)) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010027 $c->app->log->debug('Looking for neighbours of '.$w);
28 push(@lists, get_neighbours(encode("iso-8859-1", $w), $no_nbs));
29 }
Marc Kupietz247500f2015-10-09 11:29:01 +020030 }
Marc Kupietz000ad862016-02-26 14:59:12 +010031 $word =~ s/ *\| */ | /g;
Marc Kupietzd7aea722016-03-02 11:59:12 +010032 $c->render(template=>"index", word=>$word, no_nbs=>$no_nbs, no_iterations => $no_iterations, epsilon=> $epsilon, perplexity=> $perplexity, show_som=>$som, lists=> \@lists);
Marc Kupietzdc22b982015-10-09 09:19:34 +020033};
34
35app->start;
36
37exit;
38
39__END__
40
41__C__
42#include <stdio.h>
43#include <string.h>
44#include <math.h>
45#include <malloc.h>
46#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +010047#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +010048#include <pthread.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +020049
50#define max_size 2000
51#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010052#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +010053#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +010054#define MAX_THREADS 100
Marc Kupietzdc22b982015-10-09 09:19:34 +020055
56//the thread function
57void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +010058
59typedef struct {
60 long long *index;
61 float *dist;
62 unsigned int length;
63} knn;
64
65
66typedef struct {
67 char *token;
68 int N;
69 unsigned long from;
70 unsigned long upto;
71} knnpars;
72
Marc Kupietzdc22b982015-10-09 09:19:34 +020073float *M;
74char *vocab;
Marc Kupietz82b02672016-02-26 12:32:25 +010075long long words, size;
Marc Kupietz000ad862016-02-26 14:59:12 +010076int num_threads=20;
Marc Kupietzdc22b982015-10-09 09:19:34 +020077
78int init_net(char *file_name) {
Marc Kupietz67c20282016-02-26 09:42:00 +010079 FILE *f, *binvecs, *binwords;
Marc Kupietzf0809762016-02-26 10:13:47 +010080 int binwords_fd, binvecs_fd;
Marc Kupietz82b02672016-02-26 12:32:25 +010081 long long a, b, c, d, cn;
82 float len;
83
Marc Kupietz67c20282016-02-26 09:42:00 +010084 char binvecs_fname[256], binwords_fname[256];
85 strcpy(binwords_fname, file_name);
86 strcat(binwords_fname, ".words");
87 strcpy(binvecs_fname, file_name);
88 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +020089
Marc Kupietzdc22b982015-10-09 09:19:34 +020090 f = fopen(file_name, "rb");
91 if (f == NULL) {
92 printf("Input file %s not found\n", file_name);
93 return -1;
94 }
95 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +010096 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +020097 fscanf(f, "%lld", &size);
Marc Kupietzf0809762016-02-26 10:13:47 +010098 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
Marc Kupietz000ad862016-02-26 14:59:12 +010099 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
100 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
Marc Kupietzf0809762016-02-26 10:13:47 +0100101 if (M == MAP_FAILED || vocab == MAP_FAILED) {
102 close(binvecs_fd);
103 close(binwords_fd);
104 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
105 exit(-1);
106 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100107 } else {
Marc Kupietzf0809762016-02-26 10:13:47 +0100108 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
109 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
110 if (M == NULL) {
111 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
112 return -1;
113 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100114 for (b = 0; b < words; b++) {
115 a = 0;
116 while (1) {
117 vocab[b * max_w + a] = fgetc(f);
118 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
119 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
120 }
121 vocab[b * max_w + a] = 0;
122 fread(&M[b * size], sizeof(float), size, f);
123 len = 0;
124 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
125 len = sqrt(len);
126 for (a = 0; a < size; a++) M[a + b * size] /= len;
127 }
128 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
129 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
130 fclose(binvecs);
131 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
132 fclose(binwords);
133 }
134 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200135 fclose(f);
136 return 0;
137}
138
Marc Kupietz000ad862016-02-26 14:59:12 +0100139
140void *_get_neighbours(knnpars *pars) {
141 char *st1 = pars->token;
142 int N = pars->N;
143 unsigned long from = pars -> from;
144 unsigned long upto = pars -> upto;
Marc Kupietz82b02672016-02-26 12:32:25 +0100145 char file_name[max_size], st[100][max_size];
Marc Kupietz000ad862016-02-26 14:59:12 +0100146 float dist, len, *bestd, vec[max_size];
147 long long a, b, c, d, cn, bi[100], *besti;
Marc Kupietz82b02672016-02-26 12:32:25 +0100148 char ch;
Marc Kupietz000ad862016-02-26 14:59:12 +0100149 knn *nbs = NULL;
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100150
Marc Kupietz000ad862016-02-26 14:59:12 +0100151 besti = malloc(N * sizeof(long long));
152 bestd = malloc(N * sizeof(float));
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100153
Marc Kupietz000ad862016-02-26 14:59:12 +0100154 float worstbest=-1;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200155
156 for (a = 0; a < N; a++) bestd[a] = 0;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200157 a = 0;
158 cn = 0;
159 b = 0;
160 c = 0;
161 while (1) {
162 st[cn][b] = st1[c];
163 b++;
164 c++;
165 st[cn][b] = 0;
166 if (st1[c] == 0) break;
167 if (st1[c] == ' ') {
168 cn++;
169 b = 0;
170 c++;
171 }
172 }
173 cn++;
174 for (a = 0; a < cn; a++) {
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100175 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
176 if (b == words) b = -1;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200177 bi[a] = b;
Marc Kupietze8da3062016-02-25 08:37:53 +0100178 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], bi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200179 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100180 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100181 cn--;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200182 break;
183 }
184 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100185 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100186 N = 0;
187 goto end;
188 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200189 for (a = 0; a < size; a++) vec[a] = 0;
190 for (b = 0; b < cn; b++) {
191 if (bi[b] == -1) continue;
192 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
193 }
194 len = 0;
195 for (a = 0; a < size; a++) len += vec[a] * vec[a];
196 len = sqrt(len);
197 for (a = 0; a < size; a++) vec[a] /= len;
198 for (a = 0; a < N; a++) bestd[a] = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100199 for (c = from; c < upto; c++) {
Marc Kupietzdc22b982015-10-09 09:19:34 +0200200 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100201// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100202// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
203// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200204 dist = 0;
205 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100206 if(dist > worstbest) {
207 for (a = 0; a < N; a++) {
208 if (dist > bestd[a]) {
209 for (d = N - 1; d > a; d--) {
210 bestd[d] = bestd[d - 1];
211 besti[d] = besti[d - 1];
212 }
213 bestd[a] = dist;
214 besti[a] = c;
215 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200216 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200217 }
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100218 worstbest = bestd[N-1];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200219 }
220 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100221
Marc Kupietz000ad862016-02-26 14:59:12 +0100222 nbs = malloc(sizeof(knn));
223 nbs->index = besti;
224 nbs->dist = bestd;
225 nbs->length = N;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100226end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100227 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200228}
229
Marc Kupietz000ad862016-02-26 14:59:12 +0100230SV *get_neighbours(char *st1, int N) {
231 float bestd[MAX_NEIGHBOURS], vec[max_size];
232 long long besti[MAX_NEIGHBOURS], a, b, c, d, slice;
233 char *bestw[MAX_NEIGHBOURS];
234 knn *nbs[MAX_THREADS];
235 knnpars pars[MAX_THREADS];
236 pthread_t *pt = (pthread_t *)malloc(num_threads * sizeof(pthread_t));
237 AV* array = newAV();
238
239 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
240
241 slice = words / num_threads;
242
243 for(a=0; a < num_threads; a++) {
244 pars[a].token = st1;
245 pars[a].N = N;
246 pars[a].from = a*slice;
247 pars[a].upto = ((a+1)*slice > words? words:(a+1)*slice);
248 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
249 }
250 for (a = 0; a < num_threads; a++) pthread_join(pt[a], &nbs[a]);
251
252 if(!nbs[0])
253 goto end;
254
255 for(b=0; b < N; b++) {
256 besti[b] = nbs[0]->index[b];
257 bestd[b] = nbs[0]->dist[b];
258 }
259
260 for(a=1; a < num_threads; a++) {
261 for(b=0; b < N; b++) {
262 for(c=0; c < N; c++) {
263 if(nbs[a]->dist[b] > bestd[c]) {
264 for(d=N-1; d>c; d--) {
265 bestd[d] = bestd[d-1];
266 besti[d] = besti[d-1];
267 }
268 besti[c] = nbs[a]->index[b];
269 bestd[c] = nbs[a]->dist[b];
270 break;
271 }
272 }
273 }
274 }
275
276 if(nbs) {
277 for (a = 0; a < N; a++) {
278 bestw[a] = (char *)malloc(max_size * sizeof(char));
279 }
280 for (a = 0; a < N; a++) {
281 strcpy(bestw[a], &vocab[besti[a] * max_w]);
282 HV* hash = newHV();
283 hv_store(hash, "word", strlen("word"), newSVpvf(bestw[a], 0), 0);
284 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
285 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
286 AV *vector = newAV();
287 for (b = 0; b < size; b++) {
288 av_push(vector, newSVnv(M[b + besti[a] * size]));
289 }
290 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
291 av_push(array, newRV_noinc((SV*)hash));
292 }
293 }
294end:
295 return newRV_noinc((SV*)array);
296}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100297
Marc Kupietzdc22b982015-10-09 09:19:34 +0200298__DATA__
299
300@@ index.html.ep
301<!DOCTYPE html>
302<html>
Marc Kupietzc4893362016-02-25 08:04:46 +0100303<head>
304 <title>DeReKo-Word-Vector-Distances</title>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100305 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
Marc Kupietzc4893362016-02-25 08:04:46 +0100306 <script src="http://code.jquery.com/jquery-latest.min.js"></script>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100307 <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
308 <script>
309 $(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100310 $( document ).tooltip({
311 content: function() {
312 return $(this).attr('title');
313 }}
314 )
315 })
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100316 </script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100317 <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
318 <script src="http://klinux10/word2vec/tsne.js"></script>
Marc Kupietzd7aea722016-03-02 11:59:12 +0100319 <script src="http://klinux10/word2vec/som.js"></script>
Marc Kupietzc5990da2016-02-26 08:47:12 +0100320 <script src="http://klinux10/word2vec/labeler.js"></script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100321<style>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100322body, input {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100323 font-family: Arial, sans-serif;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100324 font-size: 11pt;
325}
326
327.ui-tooltip-content {
328 font-size: 9pt;
329 colour: #222222;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100330}
Marc Kupietz5f780672016-02-25 17:15:54 +0100331
332svg > .ui-tooltip-content {
Marc Kupietzb1029362016-02-27 21:38:55 +0100333 font-size: 8pt;
Marc Kupietz5f780672016-02-25 17:15:54 +0100334 colour: #222222;
335}
336
Marc Kupietzc4893362016-02-25 08:04:46 +0100337#wrapper {
338 width: 100%;
339// border: 1px solid red;
340 overflow: hidden; /* will contain if #first is longer than #second */
341}
342#first {
Marc Kupietzb1029362016-02-27 21:38:55 +0100343 margin-right: 20px;
344 float: left;
345 // border: 1px solid green;
Marc Kupietzc4893362016-02-25 08:04:46 +0100346}
347#second {
348 border: 1px solid #333;
349 overflow: hidden; /* if you don't want #second to wrap below #first */
350}
Marc Kupietzd7aea722016-03-02 11:59:12 +0100351#som2 svg {
352 border: 1px solid #333;
353}
354
Marc Kupietz4aa62172016-02-25 10:39:27 +0100355#cost {
Marc Kupietzb1029362016-02-27 21:38:55 +0100356 font-size: 8pt;
357 color: #222222;
358 margin-top: 4px;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100359 margin-bottom: 12px;
Marc Kupietz4aa62172016-02-25 10:39:27 +0100360}
Marc Kupietzd7aea722016-03-02 11:59:12 +0100361
362#sominfo {
363 font-size: 8pt;
364 color: #222222;
365 margin-top: 0px;
366}
367
368#third {
369 border: 1px solid #333;
370}
371
Marc Kupietzc4893362016-02-25 08:04:46 +0100372</style>
373<script>
374
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100375var opt = {epsilon: <%= $epsilon %>, perplexity: <%= $perplexity %>},
Marc Kupietz9fca1732016-02-29 09:07:04 +0100376 mapWidth = 800, // width map
377 mapHeight = 800,
378 jitterRadius = 7;
379
Marc Kupietzc4893362016-02-25 08:04:46 +0100380var T = new tsnejs.tSNE(opt); // create a tSNE instance
381
382var Y;
383
384var data;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100385var labeler;
Marc Kupietzc4893362016-02-25 08:04:46 +0100386
Marc Kupietzc5990da2016-02-26 08:47:12 +0100387
388function applyJitter() {
Marc Kupietzd7aea722016-03-02 11:59:12 +0100389 svg.selectAll('.tsnet')
Marc Kupietzc5990da2016-02-26 08:47:12 +0100390 .data(labels)
391 .transition()
392 .duration(50)
393 .attr("transform", function(d, i) {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100394 T.Y[i][0] = (d.x - mapWidth/2 - tx)/ss/20;
395 T.Y[i][1] = (d.y - mapHeight/2 - ty)/ss/20;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100396 return "translate(" +
397 (d.x) + "," +
398 (d.y) + ")";
399 });
400}
401
Marc Kupietzc4893362016-02-25 08:04:46 +0100402function updateEmbedding() {
403 var Y = T.getSolution();
Marc Kupietzd7aea722016-03-02 11:59:12 +0100404 svg.selectAll('.tsnet')
Marc Kupietz9fca1732016-02-29 09:07:04 +0100405 .data(data.words)
406 .attr("transform", function(d, i) {
407 return "translate(" +
408 ((Y[i][0]*20*ss + tx) + mapWidth/2) + "," +
409 ((Y[i][1]*20*ss + ty) + mapHeight/2) + ")"; });
Marc Kupietzc4893362016-02-25 08:04:46 +0100410}
411
412var svg;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100413var labels = [];
414var anchor_array = [];
415var text;
416
Marc Kupietzc4893362016-02-25 08:04:46 +0100417function drawEmbedding() {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100418 $("#embed").empty();
419 var div = d3.select("#embed");
420
421 // get min and max in each column of Y
422 var Y = T.Y;
423
424 svg = div.append("svg") // svg is global
425 .attr("width", mapWidth)
426 .attr("height", mapHeight);
427
428 var g = svg.selectAll(".b")
429 .data(data.words)
430 .enter().append("g")
Marc Kupietzd7aea722016-03-02 11:59:12 +0100431 .attr("class", "tsnet");
Marc Kupietz9fca1732016-02-29 09:07:04 +0100432
433 g.append("a")
434 .attr("xlink:href", function(word) {return "/?word="+word;})
435 .attr("title", function(d, i) {
Marc Kupietze50c8162016-03-01 10:24:43 +0100436 return "rank: "+i +" "+"freq. rank: "+data.ranks[i].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
Marc Kupietz9fca1732016-02-29 09:07:04 +0100437 })
Marc Kupietza350bce2016-02-25 09:34:25 +0100438 .append("text")
Marc Kupietz9fca1732016-02-29 09:07:04 +0100439 .attr("text-anchor", "top")
440 .attr("font-size", 12)
441 .attr("fill", function(d) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100442 if(data.target.indexOf(" "+d+" ") >= 0) {
Marc Kupietza350bce2016-02-25 09:34:25 +0100443 return "red";
444 } else {
445 return "#333"
Marc Kupietzc4893362016-02-25 08:04:46 +0100446 }
Marc Kupietz9fca1732016-02-29 09:07:04 +0100447 })
448 .text(function(d) { return d; });
449
450 var zoomListener = d3.behavior.zoom()
451 .scaleExtent([0.1, 10])
452 .center([0,0])
453 .on("zoom", zoomHandler);
454 zoomListener(svg);
455}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100456
Marc Kupietz9fca1732016-02-29 09:07:04 +0100457var tx=0, ty=0;
458var ss=1;
459var iter_id=-1;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100460
Marc Kupietz9fca1732016-02-29 09:07:04 +0100461function zoomHandler() {
462 tx = d3.event.translate[0];
463 ty = d3.event.translate[1];
464 ss = d3.event.scale;
465 updateEmbedding();
466}
467
468var stepnum = 0;
469
470function stopStep() {
471 clearInterval(iter_id);
472 text = svg.selectAll("text");
473
474 // jitter function needs different data and co-ordinate representation
475 labels = d3.range(data.words.length).map(function(i) {
476 var x = (T.Y[i][0]*20*ss + tx) + mapWidth/2;
477 var y = (T.Y[i][1]*20*ss + ty) + mapHeight/2;
478 anchor_array.push({x: x, y: y, r: jitterRadius});
479 return {
480 x: x,
481 y: y,
482 name: data.words[i]
483 };
484 });
485
486 // get the actual label bounding boxes for the jitter function
487 var index = 0;
488 text.each(function() {
489 labels[index].width = this.getBBox().width;
490 labels[index].height = this.getBBox().height;
491 index += 1;
492 });
Marc Kupietzc5990da2016-02-26 08:47:12 +0100493
494
495// setTimeout(updateEmbedding, 1);
496// setTimeout(
Marc Kupietz9fca1732016-02-29 09:07:04 +0100497 labeler = d3.labeler()
498 .label(labels)
499 .anchor(anchor_array)
500 .width(mapWidth)
501 .height(mapHeight)
502 .update(applyJitter);
503 // .start(1000);
Marc Kupietzc5990da2016-02-26 08:47:12 +0100504
Marc Kupietz9fca1732016-02-29 09:07:04 +0100505 iter_id = setInterval(jitterStep, 1);
506}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100507
Marc Kupietz9fca1732016-02-29 09:07:04 +0100508var jitter_i=0;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100509
510function jitterStep() {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100511 if(jitter_i++ > 100) {
512 clearInterval(iter_id);
513 } else {
514 labeler.start2(10);
515 applyJitter();
516 }
Marc Kupietzc5990da2016-02-26 08:47:12 +0100517}
Marc Kupietzb1029362016-02-27 21:38:55 +0100518
519var last_cost=1000;
520
Marc Kupietz9fca1732016-02-29 09:07:04 +0100521function step() {
522 var i = T.iter;
523
524 if(i > <%= $no_iterations %>) {
525 stopStep();
526 } else {
527 var cost = Math.round(T.step() * 100000) / 100000; // do a few steps
528 $("#cost").html("tsne iteration " + i + ", cost: " + cost.toFixed(5));
529 if(i % 250 == 0 && cost >= last_cost) {
530 stopStep();
531 } else {
532 last_cost = cost;
533 updateEmbedding();
534 }
535 }
536}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100537
Marc Kupietz9fca1732016-02-29 09:07:04 +0100538function showMap(j) {
539 data=j;
540 T.iter=0;
541 T.initDataRaw(data.vecs); // init embedding
542 drawEmbedding(); // draw initial embedding
543
544 if(iter_id >= 0) {
545 clearInterval(iter_id);
546 }
547 //T.debugGrad();
548 iter_id = setInterval(step, 1);
Marc Kupietzd7aea722016-03-02 11:59:12 +0100549 if(<%= $show_som %>) {
550 makeSOM(j, <%= $no_iterations %>);
551 }
Marc Kupietz9fca1732016-02-29 09:07:04 +0100552}
Marc Kupietza350bce2016-02-25 09:34:25 +0100553
Marc Kupietzc4893362016-02-25 08:04:46 +0100554</script>
555</head>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200556<body>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100557 <form action="<%=url_for('/')->to_abs%>" method="GET">
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100558 word(s):
559 <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.">
560 max. neighbours: <input type="text" size="8" name="n" value="<%= $no_nbs %>">
Marc Kupietzd7aea722016-03-02 11:59:12 +0100561 max. iterations: <input type="text" name="N" size="8" value="<%= $no_iterations %>">
562 SOM <input type="checkbox" name="som" value="1" <%= ($show_som ? "checked" : "") %>>
563 <span> </span><input type="submit" value="Show">
Marc Kupietz4aa62172016-02-25 10:39:27 +0100564 </form>
565 <br>
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100566 % if($lists) {
Marc Kupietz4aa62172016-02-25 10:39:27 +0100567 <div id="wrapper">
568 <table id="first">
569 <tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100570 <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 +0100571 </tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100572 % my $j=0; my @words; my @vecs; my @ranks; for my $list (@$lists) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100573 % my $i=1; for my $item (@$list) {
574 % if(!grep{$_ eq $item->{word}} @words) {
575 % push @vecs, $item->{vector};
576 % push @words, $item->{word};
Marc Kupietz5f780672016-02-25 17:15:54 +0100577 % push @ranks, $item->{rank};
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100578 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100579 <tr>
580 <td align="right">
581 <%= $i++ %>.
582 </td>
583 <td>
584 <a href="/?word=<%= $item->{word} %>">
585 <%= $item->{word} %>
586 </a>
587 </td>
588 <td align="right">
589 <%= sprintf("%.3f", $item->{dist}) %>
590 </td>
Marc Kupietz5f780672016-02-25 17:15:54 +0100591 <td align="right">
592 <%= $item->{rank} %>
593 </td>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100594 </tr>
595 % }
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100596 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100597 </table>
598 <script>
599 % use Mojo::ByteStream 'b';
600 $(window).load(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100601 showMap(<%= b(Mojo::JSON::to_json({target => " $word ", words => \@words, vecs => \@vecs, ranks => \@ranks})); %>);
Marc Kupietz4aa62172016-02-25 10:39:27 +0100602 });
603 </script>
604 % }
605 <div id="second" style="width:800px; height:800px; font-family: arial;">
606 <div id="embed">
607 </div>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100608 </div>
Marc Kupietzb1029362016-02-27 21:38:55 +0100609 <div id="cost"></div>
Marc Kupietzd7aea722016-03-02 11:59:12 +0100610 % if($show_som) {
611 <div id="som2">
612 </div>
613 <div id="sominfo">SOM iteration <span id="iterations">0</span></div>
614 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100615 </div>
616 <p>
617 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 +0200618 <pre>
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100619-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 +0100620 </pre>
621 </p>
622</body>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200623</html>
624