blob: 2d1b874123eee3a8ad17f796fcfc70d2c78a14ed [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 Kupietz2cb667e2016-03-10 09:44:12 +01009print STDERR $ARGV[1];
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 Kupietz2cb667e2016-03-10 09:44:12 +010011if(!$ARGV[1]) {
12 init_net("vectors15.bin");
13} else {
14 init_net($ARGV[1]);
15}
Marc Kupietzdc22b982015-10-09 09:19:34 +020016
17get '/' => sub {
18 my $c = shift;
19 my $word=$c->param('word');
Marc Kupietz44bee3c2016-02-25 16:26:29 +010020 my $no_nbs=$c->param('n') || 100;
21 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +010022 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +010023 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +010024 my $som=$c->param('som') || 0;
Marc Kupietzd4227392016-03-01 16:45:12 +010025
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010026 my @lists;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010027 if(defined($word) && $word !~ /^\s*$/) {
28 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +010029 $word =~ s/\s+/ /g;
30 for my $w (split(' *\| *', $word)) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010031 $c->app->log->debug('Looking for neighbours of '.$w);
32 push(@lists, get_neighbours(encode("iso-8859-1", $w), $no_nbs));
33 }
Marc Kupietz247500f2015-10-09 11:29:01 +020034 }
Marc Kupietz000ad862016-02-26 14:59:12 +010035 $word =~ s/ *\| */ | /g;
Marc Kupietzd7aea722016-03-02 11:59:12 +010036 $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 +020037};
38
39app->start;
40
41exit;
42
43__END__
44
45__C__
46#include <stdio.h>
47#include <string.h>
48#include <math.h>
49#include <malloc.h>
50#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +010051#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +010052#include <pthread.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +020053
54#define max_size 2000
55#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010056#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +010057#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +010058#define MAX_THREADS 100
Marc Kupietzdc22b982015-10-09 09:19:34 +020059
60//the thread function
61void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +010062
63typedef struct {
64 long long *index;
65 float *dist;
66 unsigned int length;
67} knn;
68
69
70typedef struct {
71 char *token;
72 int N;
73 unsigned long from;
74 unsigned long upto;
75} knnpars;
76
Marc Kupietzdc22b982015-10-09 09:19:34 +020077float *M;
78char *vocab;
Marc Kupietz82b02672016-02-26 12:32:25 +010079long long words, size;
Marc Kupietz000ad862016-02-26 14:59:12 +010080int num_threads=20;
Marc Kupietzdc22b982015-10-09 09:19:34 +020081
82int init_net(char *file_name) {
Marc Kupietz67c20282016-02-26 09:42:00 +010083 FILE *f, *binvecs, *binwords;
Marc Kupietzf0809762016-02-26 10:13:47 +010084 int binwords_fd, binvecs_fd;
Marc Kupietz82b02672016-02-26 12:32:25 +010085 long long a, b, c, d, cn;
86 float len;
87
Marc Kupietz67c20282016-02-26 09:42:00 +010088 char binvecs_fname[256], binwords_fname[256];
89 strcpy(binwords_fname, file_name);
90 strcat(binwords_fname, ".words");
91 strcpy(binvecs_fname, file_name);
92 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +020093
Marc Kupietzdc22b982015-10-09 09:19:34 +020094 f = fopen(file_name, "rb");
95 if (f == NULL) {
96 printf("Input file %s not found\n", file_name);
97 return -1;
98 }
99 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100100 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200101 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100102 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
103 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100104 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
105 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
106 if (M == NULL) {
107 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
108 return -1;
109 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100110 for (b = 0; b < words; b++) {
111 a = 0;
112 while (1) {
113 vocab[b * max_w + a] = fgetc(f);
114 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
115 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
116 }
117 vocab[b * max_w + a] = 0;
118 fread(&M[b * size], sizeof(float), size, f);
119 len = 0;
120 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
121 len = sqrt(len);
122 for (a = 0; a < size; a++) M[a + b * size] /= len;
123 }
124 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
125 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
126 fclose(binvecs);
127 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
128 fclose(binwords);
129 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100130 }
131 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
132 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
133 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
134 if (M == MAP_FAILED || vocab == MAP_FAILED) {
135 close(binvecs_fd);
136 close(binwords_fd);
137 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
138 exit(-1);
139 }
140 } else {
141 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
142 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100143 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200144 fclose(f);
145 return 0;
146}
147
Marc Kupietz000ad862016-02-26 14:59:12 +0100148
149void *_get_neighbours(knnpars *pars) {
150 char *st1 = pars->token;
151 int N = pars->N;
152 unsigned long from = pars -> from;
153 unsigned long upto = pars -> upto;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100154 char file_name[max_size], st[100][max_size], sep[100];
Marc Kupietz000ad862016-02-26 14:59:12 +0100155 float dist, len, *bestd, vec[max_size];
156 long long a, b, c, d, cn, bi[100], *besti;
Marc Kupietz82b02672016-02-26 12:32:25 +0100157 char ch;
Marc Kupietz000ad862016-02-26 14:59:12 +0100158 knn *nbs = NULL;
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100159
Marc Kupietz000ad862016-02-26 14:59:12 +0100160 besti = malloc(N * sizeof(long long));
161 bestd = malloc(N * sizeof(float));
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100162
Marc Kupietz000ad862016-02-26 14:59:12 +0100163 float worstbest=-1;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200164
165 for (a = 0; a < N; a++) bestd[a] = 0;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200166 a = 0;
167 cn = 0;
168 b = 0;
169 c = 0;
170 while (1) {
171 st[cn][b] = st1[c];
172 b++;
173 c++;
174 st[cn][b] = 0;
175 if (st1[c] == 0) break;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100176 if (st1[c] == ' ' || st1[c] == '-') {
177 sep[cn++] = st1[c];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200178 b = 0;
179 c++;
180 }
181 }
182 cn++;
183 for (a = 0; a < cn; a++) {
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100184 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
185 if (b == words) b = -1;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200186 bi[a] = b;
Marc Kupietze8da3062016-02-25 08:37:53 +0100187 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], bi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200188 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100189 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100190 cn--;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200191 break;
192 }
193 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100194 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100195 N = 0;
196 goto end;
197 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200198 for (a = 0; a < size; a++) vec[a] = 0;
199 for (b = 0; b < cn; b++) {
200 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100201 if(b>0 && sep[b-1] == '-')
202 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
203 else
204 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200205 }
206 len = 0;
207 for (a = 0; a < size; a++) len += vec[a] * vec[a];
208 len = sqrt(len);
209 for (a = 0; a < size; a++) vec[a] /= len;
210 for (a = 0; a < N; a++) bestd[a] = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100211 for (c = from; c < upto; c++) {
Marc Kupietzdc22b982015-10-09 09:19:34 +0200212 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100213// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100214// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
215// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200216 dist = 0;
217 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100218 if(dist > worstbest) {
219 for (a = 0; a < N; a++) {
220 if (dist > bestd[a]) {
221 for (d = N - 1; d > a; d--) {
222 bestd[d] = bestd[d - 1];
223 besti[d] = besti[d - 1];
224 }
225 bestd[a] = dist;
226 besti[a] = c;
227 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200228 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200229 }
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100230 worstbest = bestd[N-1];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200231 }
232 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100233
Marc Kupietz000ad862016-02-26 14:59:12 +0100234 nbs = malloc(sizeof(knn));
235 nbs->index = besti;
236 nbs->dist = bestd;
237 nbs->length = N;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100238end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100239 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200240}
241
Marc Kupietz000ad862016-02-26 14:59:12 +0100242SV *get_neighbours(char *st1, int N) {
243 float bestd[MAX_NEIGHBOURS], vec[max_size];
244 long long besti[MAX_NEIGHBOURS], a, b, c, d, slice;
245 char *bestw[MAX_NEIGHBOURS];
246 knn *nbs[MAX_THREADS];
247 knnpars pars[MAX_THREADS];
248 pthread_t *pt = (pthread_t *)malloc(num_threads * sizeof(pthread_t));
249 AV* array = newAV();
250
251 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
252
253 slice = words / num_threads;
254
255 for(a=0; a < num_threads; a++) {
256 pars[a].token = st1;
257 pars[a].N = N;
258 pars[a].from = a*slice;
259 pars[a].upto = ((a+1)*slice > words? words:(a+1)*slice);
260 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
261 }
262 for (a = 0; a < num_threads; a++) pthread_join(pt[a], &nbs[a]);
263
264 if(!nbs[0])
265 goto end;
266
267 for(b=0; b < N; b++) {
268 besti[b] = nbs[0]->index[b];
269 bestd[b] = nbs[0]->dist[b];
270 }
271
272 for(a=1; a < num_threads; a++) {
273 for(b=0; b < N; b++) {
274 for(c=0; c < N; c++) {
275 if(nbs[a]->dist[b] > bestd[c]) {
276 for(d=N-1; d>c; d--) {
277 bestd[d] = bestd[d-1];
278 besti[d] = besti[d-1];
279 }
280 besti[c] = nbs[a]->index[b];
281 bestd[c] = nbs[a]->dist[b];
282 break;
283 }
284 }
285 }
286 }
287
288 if(nbs) {
289 for (a = 0; a < N; a++) {
290 bestw[a] = (char *)malloc(max_size * sizeof(char));
291 }
292 for (a = 0; a < N; a++) {
293 strcpy(bestw[a], &vocab[besti[a] * max_w]);
294 HV* hash = newHV();
295 hv_store(hash, "word", strlen("word"), newSVpvf(bestw[a], 0), 0);
296 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
297 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
298 AV *vector = newAV();
299 for (b = 0; b < size; b++) {
300 av_push(vector, newSVnv(M[b + besti[a] * size]));
301 }
302 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
303 av_push(array, newRV_noinc((SV*)hash));
304 }
305 }
306end:
307 return newRV_noinc((SV*)array);
308}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100309
Marc Kupietzdc22b982015-10-09 09:19:34 +0200310__DATA__
311
312@@ index.html.ep
313<!DOCTYPE html>
314<html>
Marc Kupietzc4893362016-02-25 08:04:46 +0100315<head>
316 <title>DeReKo-Word-Vector-Distances</title>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100317 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
Marc Kupietzc4893362016-02-25 08:04:46 +0100318 <script src="http://code.jquery.com/jquery-latest.min.js"></script>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100319 <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
320 <script>
321 $(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100322 $( document ).tooltip({
323 content: function() {
324 return $(this).attr('title');
325 }}
326 )
327 })
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100328 </script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100329 <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
330 <script src="http://klinux10/word2vec/tsne.js"></script>
Marc Kupietzd7aea722016-03-02 11:59:12 +0100331 <script src="http://klinux10/word2vec/som.js"></script>
Marc Kupietzc5990da2016-02-26 08:47:12 +0100332 <script src="http://klinux10/word2vec/labeler.js"></script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100333<style>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100334body, input {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100335 font-family: Arial, sans-serif;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100336 font-size: 11pt;
337}
338
339.ui-tooltip-content {
340 font-size: 9pt;
341 colour: #222222;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100342}
Marc Kupietz5f780672016-02-25 17:15:54 +0100343
344svg > .ui-tooltip-content {
Marc Kupietzb1029362016-02-27 21:38:55 +0100345 font-size: 8pt;
Marc Kupietz5f780672016-02-25 17:15:54 +0100346 colour: #222222;
347}
348
Marc Kupietzc4893362016-02-25 08:04:46 +0100349#wrapper {
350 width: 100%;
351// border: 1px solid red;
352 overflow: hidden; /* will contain if #first is longer than #second */
353}
354#first {
Marc Kupietzb1029362016-02-27 21:38:55 +0100355 margin-right: 20px;
356 float: left;
357 // border: 1px solid green;
Marc Kupietzc4893362016-02-25 08:04:46 +0100358}
359#second {
360 border: 1px solid #333;
361 overflow: hidden; /* if you don't want #second to wrap below #first */
362}
Marc Kupietzd7aea722016-03-02 11:59:12 +0100363#som2 svg {
364 border: 1px solid #333;
365}
366
Marc Kupietz4aa62172016-02-25 10:39:27 +0100367#cost {
Marc Kupietzb1029362016-02-27 21:38:55 +0100368 font-size: 8pt;
369 color: #222222;
370 margin-top: 4px;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100371 margin-bottom: 12px;
Marc Kupietz4aa62172016-02-25 10:39:27 +0100372}
Marc Kupietzd7aea722016-03-02 11:59:12 +0100373
Marc Kupietz6c1ca442016-03-03 09:35:18 +0100374#sominfo1, #sominfo {
Marc Kupietzd7aea722016-03-02 11:59:12 +0100375 font-size: 8pt;
376 color: #222222;
377 margin-top: 0px;
378}
379
Marc Kupietz6c1ca442016-03-03 09:35:18 +0100380#somcolor1, #somcolor2, #somcolor3 {
381 display: inline-block;
382 height: 10px;
383 width: 10px;
384}
385
Marc Kupietzd7aea722016-03-02 11:59:12 +0100386#third {
387 border: 1px solid #333;
388}
389
Marc Kupietzc4893362016-02-25 08:04:46 +0100390</style>
391<script>
392
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100393var opt = {epsilon: <%= $epsilon %>, perplexity: <%= $perplexity %>},
Marc Kupietz9fca1732016-02-29 09:07:04 +0100394 mapWidth = 800, // width map
395 mapHeight = 800,
396 jitterRadius = 7;
397
Marc Kupietzc4893362016-02-25 08:04:46 +0100398var T = new tsnejs.tSNE(opt); // create a tSNE instance
399
400var Y;
401
402var data;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100403var labeler;
Marc Kupietzc4893362016-02-25 08:04:46 +0100404
Marc Kupietzc5990da2016-02-26 08:47:12 +0100405
406function applyJitter() {
Marc Kupietzd7aea722016-03-02 11:59:12 +0100407 svg.selectAll('.tsnet')
Marc Kupietzc5990da2016-02-26 08:47:12 +0100408 .data(labels)
409 .transition()
410 .duration(50)
411 .attr("transform", function(d, i) {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100412 T.Y[i][0] = (d.x - mapWidth/2 - tx)/ss/20;
413 T.Y[i][1] = (d.y - mapHeight/2 - ty)/ss/20;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100414 return "translate(" +
415 (d.x) + "," +
416 (d.y) + ")";
417 });
418}
419
Marc Kupietzc4893362016-02-25 08:04:46 +0100420function updateEmbedding() {
421 var Y = T.getSolution();
Marc Kupietzd7aea722016-03-02 11:59:12 +0100422 svg.selectAll('.tsnet')
Marc Kupietz9fca1732016-02-29 09:07:04 +0100423 .data(data.words)
424 .attr("transform", function(d, i) {
425 return "translate(" +
426 ((Y[i][0]*20*ss + tx) + mapWidth/2) + "," +
427 ((Y[i][1]*20*ss + ty) + mapHeight/2) + ")"; });
Marc Kupietzc4893362016-02-25 08:04:46 +0100428}
429
430var svg;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100431var labels = [];
432var anchor_array = [];
433var text;
434
Marc Kupietzc4893362016-02-25 08:04:46 +0100435function drawEmbedding() {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100436 $("#embed").empty();
437 var div = d3.select("#embed");
438
439 // get min and max in each column of Y
440 var Y = T.Y;
441
442 svg = div.append("svg") // svg is global
443 .attr("width", mapWidth)
444 .attr("height", mapHeight);
445
446 var g = svg.selectAll(".b")
447 .data(data.words)
448 .enter().append("g")
Marc Kupietzd7aea722016-03-02 11:59:12 +0100449 .attr("class", "tsnet");
Marc Kupietz9fca1732016-02-29 09:07:04 +0100450
451 g.append("a")
452 .attr("xlink:href", function(word) {return "/?word="+word;})
453 .attr("title", function(d, i) {
Marc Kupietze50c8162016-03-01 10:24:43 +0100454 return "rank: "+i +" "+"freq. rank: "+data.ranks[i].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
Marc Kupietz9fca1732016-02-29 09:07:04 +0100455 })
Marc Kupietza350bce2016-02-25 09:34:25 +0100456 .append("text")
Marc Kupietz9fca1732016-02-29 09:07:04 +0100457 .attr("text-anchor", "top")
458 .attr("font-size", 12)
459 .attr("fill", function(d) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100460 if(data.target.indexOf(" "+d+" ") >= 0) {
Marc Kupietza350bce2016-02-25 09:34:25 +0100461 return "red";
462 } else {
463 return "#333"
Marc Kupietzc4893362016-02-25 08:04:46 +0100464 }
Marc Kupietz9fca1732016-02-29 09:07:04 +0100465 })
466 .text(function(d) { return d; });
467
468 var zoomListener = d3.behavior.zoom()
469 .scaleExtent([0.1, 10])
470 .center([0,0])
471 .on("zoom", zoomHandler);
472 zoomListener(svg);
473}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100474
Marc Kupietz9fca1732016-02-29 09:07:04 +0100475var tx=0, ty=0;
476var ss=1;
477var iter_id=-1;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100478
Marc Kupietz9fca1732016-02-29 09:07:04 +0100479function zoomHandler() {
480 tx = d3.event.translate[0];
481 ty = d3.event.translate[1];
482 ss = d3.event.scale;
483 updateEmbedding();
484}
485
486var stepnum = 0;
487
488function stopStep() {
489 clearInterval(iter_id);
490 text = svg.selectAll("text");
491
492 // jitter function needs different data and co-ordinate representation
493 labels = d3.range(data.words.length).map(function(i) {
494 var x = (T.Y[i][0]*20*ss + tx) + mapWidth/2;
495 var y = (T.Y[i][1]*20*ss + ty) + mapHeight/2;
496 anchor_array.push({x: x, y: y, r: jitterRadius});
497 return {
498 x: x,
499 y: y,
500 name: data.words[i]
501 };
502 });
503
504 // get the actual label bounding boxes for the jitter function
505 var index = 0;
506 text.each(function() {
507 labels[index].width = this.getBBox().width;
508 labels[index].height = this.getBBox().height;
509 index += 1;
510 });
Marc Kupietzc5990da2016-02-26 08:47:12 +0100511
512
513// setTimeout(updateEmbedding, 1);
514// setTimeout(
Marc Kupietz9fca1732016-02-29 09:07:04 +0100515 labeler = d3.labeler()
516 .label(labels)
517 .anchor(anchor_array)
518 .width(mapWidth)
519 .height(mapHeight)
520 .update(applyJitter);
521 // .start(1000);
Marc Kupietzc5990da2016-02-26 08:47:12 +0100522
Marc Kupietz9fca1732016-02-29 09:07:04 +0100523 iter_id = setInterval(jitterStep, 1);
524}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100525
Marc Kupietz9fca1732016-02-29 09:07:04 +0100526var jitter_i=0;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100527
528function jitterStep() {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100529 if(jitter_i++ > 100) {
530 clearInterval(iter_id);
531 } else {
532 labeler.start2(10);
533 applyJitter();
534 }
Marc Kupietzc5990da2016-02-26 08:47:12 +0100535}
Marc Kupietzb1029362016-02-27 21:38:55 +0100536
537var last_cost=1000;
538
Marc Kupietz9fca1732016-02-29 09:07:04 +0100539function step() {
540 var i = T.iter;
541
542 if(i > <%= $no_iterations %>) {
543 stopStep();
544 } else {
545 var cost = Math.round(T.step() * 100000) / 100000; // do a few steps
546 $("#cost").html("tsne iteration " + i + ", cost: " + cost.toFixed(5));
547 if(i % 250 == 0 && cost >= last_cost) {
548 stopStep();
549 } else {
550 last_cost = cost;
551 updateEmbedding();
552 }
553 }
554}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100555
Marc Kupietz9fca1732016-02-29 09:07:04 +0100556function showMap(j) {
557 data=j;
558 T.iter=0;
559 T.initDataRaw(data.vecs); // init embedding
560 drawEmbedding(); // draw initial embedding
561
562 if(iter_id >= 0) {
563 clearInterval(iter_id);
564 }
565 //T.debugGrad();
566 iter_id = setInterval(step, 1);
Marc Kupietzd7aea722016-03-02 11:59:12 +0100567 if(<%= $show_som %>) {
568 makeSOM(j, <%= $no_iterations %>);
569 }
Marc Kupietz9fca1732016-02-29 09:07:04 +0100570}
Marc Kupietza350bce2016-02-25 09:34:25 +0100571
Marc Kupietzc4893362016-02-25 08:04:46 +0100572</script>
573</head>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200574<body>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100575 <form action="<%=url_for('/')->to_abs%>" method="GET">
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100576 word(s):
577 <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.">
578 max. neighbours: <input type="text" size="8" name="n" value="<%= $no_nbs %>">
Marc Kupietzd7aea722016-03-02 11:59:12 +0100579 max. iterations: <input type="text" name="N" size="8" value="<%= $no_iterations %>">
580 SOM <input type="checkbox" name="som" value="1" <%= ($show_som ? "checked" : "") %>>
581 <span> </span><input type="submit" value="Show">
Marc Kupietz4aa62172016-02-25 10:39:27 +0100582 </form>
583 <br>
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100584 % if($lists) {
Marc Kupietz4aa62172016-02-25 10:39:27 +0100585 <div id="wrapper">
586 <table id="first">
587 <tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100588 <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 +0100589 </tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100590 % my $j=0; my @words; my @vecs; my @ranks; for my $list (@$lists) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100591 % my $i=1; for my $item (@$list) {
592 % if(!grep{$_ eq $item->{word}} @words) {
593 % push @vecs, $item->{vector};
594 % push @words, $item->{word};
Marc Kupietz5f780672016-02-25 17:15:54 +0100595 % push @ranks, $item->{rank};
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100596 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100597 <tr>
598 <td align="right">
599 <%= $i++ %>.
600 </td>
601 <td>
602 <a href="/?word=<%= $item->{word} %>">
603 <%= $item->{word} %>
604 </a>
605 </td>
606 <td align="right">
607 <%= sprintf("%.3f", $item->{dist}) %>
608 </td>
Marc Kupietz5f780672016-02-25 17:15:54 +0100609 <td align="right">
610 <%= $item->{rank} %>
611 </td>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100612 </tr>
613 % }
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100614 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100615 </table>
616 <script>
617 % use Mojo::ByteStream 'b';
618 $(window).load(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100619 showMap(<%= b(Mojo::JSON::to_json({target => " $word ", words => \@words, vecs => \@vecs, ranks => \@ranks})); %>);
Marc Kupietz4aa62172016-02-25 10:39:27 +0100620 });
621 </script>
622 % }
623 <div id="second" style="width:800px; height:800px; font-family: arial;">
624 <div id="embed">
625 </div>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100626 </div>
Marc Kupietzb1029362016-02-27 21:38:55 +0100627 <div id="cost"></div>
Marc Kupietzd7aea722016-03-02 11:59:12 +0100628 % if($show_som) {
629 <div id="som2">
630 </div>
Marc Kupietz6c1ca442016-03-03 09:35:18 +0100631 <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 +0100632 <div id="sominfo">SOM iteration <span id="iterations">0</span></div>
633 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100634 </div>
635 <p>
636 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 +0200637 <pre>
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100638-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 +0100639 </pre>
640 </p>
641</body>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200642</html>
643