blob: 56e0180442b0630e257ad35152fcc8ff2ae25914 [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
Marc Kupietz6c1ca442016-03-03 09:35:18 +0100362#sominfo1, #sominfo {
Marc Kupietzd7aea722016-03-02 11:59:12 +0100363 font-size: 8pt;
364 color: #222222;
365 margin-top: 0px;
366}
367
Marc Kupietz6c1ca442016-03-03 09:35:18 +0100368#somcolor1, #somcolor2, #somcolor3 {
369 display: inline-block;
370 height: 10px;
371 width: 10px;
372}
373
Marc Kupietzd7aea722016-03-02 11:59:12 +0100374#third {
375 border: 1px solid #333;
376}
377
Marc Kupietzc4893362016-02-25 08:04:46 +0100378</style>
379<script>
380
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100381var opt = {epsilon: <%= $epsilon %>, perplexity: <%= $perplexity %>},
Marc Kupietz9fca1732016-02-29 09:07:04 +0100382 mapWidth = 800, // width map
383 mapHeight = 800,
384 jitterRadius = 7;
385
Marc Kupietzc4893362016-02-25 08:04:46 +0100386var T = new tsnejs.tSNE(opt); // create a tSNE instance
387
388var Y;
389
390var data;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100391var labeler;
Marc Kupietzc4893362016-02-25 08:04:46 +0100392
Marc Kupietzc5990da2016-02-26 08:47:12 +0100393
394function applyJitter() {
Marc Kupietzd7aea722016-03-02 11:59:12 +0100395 svg.selectAll('.tsnet')
Marc Kupietzc5990da2016-02-26 08:47:12 +0100396 .data(labels)
397 .transition()
398 .duration(50)
399 .attr("transform", function(d, i) {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100400 T.Y[i][0] = (d.x - mapWidth/2 - tx)/ss/20;
401 T.Y[i][1] = (d.y - mapHeight/2 - ty)/ss/20;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100402 return "translate(" +
403 (d.x) + "," +
404 (d.y) + ")";
405 });
406}
407
Marc Kupietzc4893362016-02-25 08:04:46 +0100408function updateEmbedding() {
409 var Y = T.getSolution();
Marc Kupietzd7aea722016-03-02 11:59:12 +0100410 svg.selectAll('.tsnet')
Marc Kupietz9fca1732016-02-29 09:07:04 +0100411 .data(data.words)
412 .attr("transform", function(d, i) {
413 return "translate(" +
414 ((Y[i][0]*20*ss + tx) + mapWidth/2) + "," +
415 ((Y[i][1]*20*ss + ty) + mapHeight/2) + ")"; });
Marc Kupietzc4893362016-02-25 08:04:46 +0100416}
417
418var svg;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100419var labels = [];
420var anchor_array = [];
421var text;
422
Marc Kupietzc4893362016-02-25 08:04:46 +0100423function drawEmbedding() {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100424 $("#embed").empty();
425 var div = d3.select("#embed");
426
427 // get min and max in each column of Y
428 var Y = T.Y;
429
430 svg = div.append("svg") // svg is global
431 .attr("width", mapWidth)
432 .attr("height", mapHeight);
433
434 var g = svg.selectAll(".b")
435 .data(data.words)
436 .enter().append("g")
Marc Kupietzd7aea722016-03-02 11:59:12 +0100437 .attr("class", "tsnet");
Marc Kupietz9fca1732016-02-29 09:07:04 +0100438
439 g.append("a")
440 .attr("xlink:href", function(word) {return "/?word="+word;})
441 .attr("title", function(d, i) {
Marc Kupietze50c8162016-03-01 10:24:43 +0100442 return "rank: "+i +" "+"freq. rank: "+data.ranks[i].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
Marc Kupietz9fca1732016-02-29 09:07:04 +0100443 })
Marc Kupietza350bce2016-02-25 09:34:25 +0100444 .append("text")
Marc Kupietz9fca1732016-02-29 09:07:04 +0100445 .attr("text-anchor", "top")
446 .attr("font-size", 12)
447 .attr("fill", function(d) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100448 if(data.target.indexOf(" "+d+" ") >= 0) {
Marc Kupietza350bce2016-02-25 09:34:25 +0100449 return "red";
450 } else {
451 return "#333"
Marc Kupietzc4893362016-02-25 08:04:46 +0100452 }
Marc Kupietz9fca1732016-02-29 09:07:04 +0100453 })
454 .text(function(d) { return d; });
455
456 var zoomListener = d3.behavior.zoom()
457 .scaleExtent([0.1, 10])
458 .center([0,0])
459 .on("zoom", zoomHandler);
460 zoomListener(svg);
461}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100462
Marc Kupietz9fca1732016-02-29 09:07:04 +0100463var tx=0, ty=0;
464var ss=1;
465var iter_id=-1;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100466
Marc Kupietz9fca1732016-02-29 09:07:04 +0100467function zoomHandler() {
468 tx = d3.event.translate[0];
469 ty = d3.event.translate[1];
470 ss = d3.event.scale;
471 updateEmbedding();
472}
473
474var stepnum = 0;
475
476function stopStep() {
477 clearInterval(iter_id);
478 text = svg.selectAll("text");
479
480 // jitter function needs different data and co-ordinate representation
481 labels = d3.range(data.words.length).map(function(i) {
482 var x = (T.Y[i][0]*20*ss + tx) + mapWidth/2;
483 var y = (T.Y[i][1]*20*ss + ty) + mapHeight/2;
484 anchor_array.push({x: x, y: y, r: jitterRadius});
485 return {
486 x: x,
487 y: y,
488 name: data.words[i]
489 };
490 });
491
492 // get the actual label bounding boxes for the jitter function
493 var index = 0;
494 text.each(function() {
495 labels[index].width = this.getBBox().width;
496 labels[index].height = this.getBBox().height;
497 index += 1;
498 });
Marc Kupietzc5990da2016-02-26 08:47:12 +0100499
500
501// setTimeout(updateEmbedding, 1);
502// setTimeout(
Marc Kupietz9fca1732016-02-29 09:07:04 +0100503 labeler = d3.labeler()
504 .label(labels)
505 .anchor(anchor_array)
506 .width(mapWidth)
507 .height(mapHeight)
508 .update(applyJitter);
509 // .start(1000);
Marc Kupietzc5990da2016-02-26 08:47:12 +0100510
Marc Kupietz9fca1732016-02-29 09:07:04 +0100511 iter_id = setInterval(jitterStep, 1);
512}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100513
Marc Kupietz9fca1732016-02-29 09:07:04 +0100514var jitter_i=0;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100515
516function jitterStep() {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100517 if(jitter_i++ > 100) {
518 clearInterval(iter_id);
519 } else {
520 labeler.start2(10);
521 applyJitter();
522 }
Marc Kupietzc5990da2016-02-26 08:47:12 +0100523}
Marc Kupietzb1029362016-02-27 21:38:55 +0100524
525var last_cost=1000;
526
Marc Kupietz9fca1732016-02-29 09:07:04 +0100527function step() {
528 var i = T.iter;
529
530 if(i > <%= $no_iterations %>) {
531 stopStep();
532 } else {
533 var cost = Math.round(T.step() * 100000) / 100000; // do a few steps
534 $("#cost").html("tsne iteration " + i + ", cost: " + cost.toFixed(5));
535 if(i % 250 == 0 && cost >= last_cost) {
536 stopStep();
537 } else {
538 last_cost = cost;
539 updateEmbedding();
540 }
541 }
542}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100543
Marc Kupietz9fca1732016-02-29 09:07:04 +0100544function showMap(j) {
545 data=j;
546 T.iter=0;
547 T.initDataRaw(data.vecs); // init embedding
548 drawEmbedding(); // draw initial embedding
549
550 if(iter_id >= 0) {
551 clearInterval(iter_id);
552 }
553 //T.debugGrad();
554 iter_id = setInterval(step, 1);
Marc Kupietzd7aea722016-03-02 11:59:12 +0100555 if(<%= $show_som %>) {
556 makeSOM(j, <%= $no_iterations %>);
557 }
Marc Kupietz9fca1732016-02-29 09:07:04 +0100558}
Marc Kupietza350bce2016-02-25 09:34:25 +0100559
Marc Kupietzc4893362016-02-25 08:04:46 +0100560</script>
561</head>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200562<body>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100563 <form action="<%=url_for('/')->to_abs%>" method="GET">
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100564 word(s):
565 <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.">
566 max. neighbours: <input type="text" size="8" name="n" value="<%= $no_nbs %>">
Marc Kupietzd7aea722016-03-02 11:59:12 +0100567 max. iterations: <input type="text" name="N" size="8" value="<%= $no_iterations %>">
568 SOM <input type="checkbox" name="som" value="1" <%= ($show_som ? "checked" : "") %>>
569 <span> </span><input type="submit" value="Show">
Marc Kupietz4aa62172016-02-25 10:39:27 +0100570 </form>
571 <br>
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100572 % if($lists) {
Marc Kupietz4aa62172016-02-25 10:39:27 +0100573 <div id="wrapper">
574 <table id="first">
575 <tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100576 <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 +0100577 </tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100578 % my $j=0; my @words; my @vecs; my @ranks; for my $list (@$lists) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100579 % my $i=1; for my $item (@$list) {
580 % if(!grep{$_ eq $item->{word}} @words) {
581 % push @vecs, $item->{vector};
582 % push @words, $item->{word};
Marc Kupietz5f780672016-02-25 17:15:54 +0100583 % push @ranks, $item->{rank};
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100584 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100585 <tr>
586 <td align="right">
587 <%= $i++ %>.
588 </td>
589 <td>
590 <a href="/?word=<%= $item->{word} %>">
591 <%= $item->{word} %>
592 </a>
593 </td>
594 <td align="right">
595 <%= sprintf("%.3f", $item->{dist}) %>
596 </td>
Marc Kupietz5f780672016-02-25 17:15:54 +0100597 <td align="right">
598 <%= $item->{rank} %>
599 </td>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100600 </tr>
601 % }
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100602 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100603 </table>
604 <script>
605 % use Mojo::ByteStream 'b';
606 $(window).load(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100607 showMap(<%= b(Mojo::JSON::to_json({target => " $word ", words => \@words, vecs => \@vecs, ranks => \@ranks})); %>);
Marc Kupietz4aa62172016-02-25 10:39:27 +0100608 });
609 </script>
610 % }
611 <div id="second" style="width:800px; height:800px; font-family: arial;">
612 <div id="embed">
613 </div>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100614 </div>
Marc Kupietzb1029362016-02-27 21:38:55 +0100615 <div id="cost"></div>
Marc Kupietzd7aea722016-03-02 11:59:12 +0100616 % if($show_som) {
617 <div id="som2">
618 </div>
Marc Kupietz6c1ca442016-03-03 09:35:18 +0100619 <div id="sominfo1"><span id="somcolor1"> </span> <span id="somword1"> </span> <span id="somcolor2"> </span> <span id="somword2"> </span> <span id="somcolor3"> </span></div>
Marc Kupietzd7aea722016-03-02 11:59:12 +0100620 <div id="sominfo">SOM iteration <span id="iterations">0</span></div>
621 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100622 </div>
623 <p>
624 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 +0200625 <pre>
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100626-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 +0100627 </pre>
628 </p>
629</body>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200630</html>
631