blob: 0d6bff33df0ae25c4469566577175a64915bb997 [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 Kupietz82b02672016-02-26 12:32:25 +0100154 char file_name[max_size], st[100][max_size];
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;
176 if (st1[c] == ' ') {
177 cn++;
178 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;
201 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
202 }
203 len = 0;
204 for (a = 0; a < size; a++) len += vec[a] * vec[a];
205 len = sqrt(len);
206 for (a = 0; a < size; a++) vec[a] /= len;
207 for (a = 0; a < N; a++) bestd[a] = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100208 for (c = from; c < upto; c++) {
Marc Kupietzdc22b982015-10-09 09:19:34 +0200209 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100210// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100211// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
212// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200213 dist = 0;
214 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100215 if(dist > worstbest) {
216 for (a = 0; a < N; a++) {
217 if (dist > bestd[a]) {
218 for (d = N - 1; d > a; d--) {
219 bestd[d] = bestd[d - 1];
220 besti[d] = besti[d - 1];
221 }
222 bestd[a] = dist;
223 besti[a] = c;
224 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200225 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200226 }
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100227 worstbest = bestd[N-1];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200228 }
229 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100230
Marc Kupietz000ad862016-02-26 14:59:12 +0100231 nbs = malloc(sizeof(knn));
232 nbs->index = besti;
233 nbs->dist = bestd;
234 nbs->length = N;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100235end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100236 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200237}
238
Marc Kupietz000ad862016-02-26 14:59:12 +0100239SV *get_neighbours(char *st1, int N) {
240 float bestd[MAX_NEIGHBOURS], vec[max_size];
241 long long besti[MAX_NEIGHBOURS], a, b, c, d, slice;
242 char *bestw[MAX_NEIGHBOURS];
243 knn *nbs[MAX_THREADS];
244 knnpars pars[MAX_THREADS];
245 pthread_t *pt = (pthread_t *)malloc(num_threads * sizeof(pthread_t));
246 AV* array = newAV();
247
248 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
249
250 slice = words / num_threads;
251
252 for(a=0; a < num_threads; a++) {
253 pars[a].token = st1;
254 pars[a].N = N;
255 pars[a].from = a*slice;
256 pars[a].upto = ((a+1)*slice > words? words:(a+1)*slice);
257 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
258 }
259 for (a = 0; a < num_threads; a++) pthread_join(pt[a], &nbs[a]);
260
261 if(!nbs[0])
262 goto end;
263
264 for(b=0; b < N; b++) {
265 besti[b] = nbs[0]->index[b];
266 bestd[b] = nbs[0]->dist[b];
267 }
268
269 for(a=1; a < num_threads; a++) {
270 for(b=0; b < N; b++) {
271 for(c=0; c < N; c++) {
272 if(nbs[a]->dist[b] > bestd[c]) {
273 for(d=N-1; d>c; d--) {
274 bestd[d] = bestd[d-1];
275 besti[d] = besti[d-1];
276 }
277 besti[c] = nbs[a]->index[b];
278 bestd[c] = nbs[a]->dist[b];
279 break;
280 }
281 }
282 }
283 }
284
285 if(nbs) {
286 for (a = 0; a < N; a++) {
287 bestw[a] = (char *)malloc(max_size * sizeof(char));
288 }
289 for (a = 0; a < N; a++) {
290 strcpy(bestw[a], &vocab[besti[a] * max_w]);
291 HV* hash = newHV();
292 hv_store(hash, "word", strlen("word"), newSVpvf(bestw[a], 0), 0);
293 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
294 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
295 AV *vector = newAV();
296 for (b = 0; b < size; b++) {
297 av_push(vector, newSVnv(M[b + besti[a] * size]));
298 }
299 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
300 av_push(array, newRV_noinc((SV*)hash));
301 }
302 }
303end:
304 return newRV_noinc((SV*)array);
305}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100306
Marc Kupietzdc22b982015-10-09 09:19:34 +0200307__DATA__
308
309@@ index.html.ep
310<!DOCTYPE html>
311<html>
Marc Kupietzc4893362016-02-25 08:04:46 +0100312<head>
313 <title>DeReKo-Word-Vector-Distances</title>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100314 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
Marc Kupietzc4893362016-02-25 08:04:46 +0100315 <script src="http://code.jquery.com/jquery-latest.min.js"></script>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100316 <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
317 <script>
318 $(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100319 $( document ).tooltip({
320 content: function() {
321 return $(this).attr('title');
322 }}
323 )
324 })
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100325 </script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100326 <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
327 <script src="http://klinux10/word2vec/tsne.js"></script>
Marc Kupietzd7aea722016-03-02 11:59:12 +0100328 <script src="http://klinux10/word2vec/som.js"></script>
Marc Kupietzc5990da2016-02-26 08:47:12 +0100329 <script src="http://klinux10/word2vec/labeler.js"></script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100330<style>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100331body, input {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100332 font-family: Arial, sans-serif;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100333 font-size: 11pt;
334}
335
336.ui-tooltip-content {
337 font-size: 9pt;
338 colour: #222222;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100339}
Marc Kupietz5f780672016-02-25 17:15:54 +0100340
341svg > .ui-tooltip-content {
Marc Kupietzb1029362016-02-27 21:38:55 +0100342 font-size: 8pt;
Marc Kupietz5f780672016-02-25 17:15:54 +0100343 colour: #222222;
344}
345
Marc Kupietzc4893362016-02-25 08:04:46 +0100346#wrapper {
347 width: 100%;
348// border: 1px solid red;
349 overflow: hidden; /* will contain if #first is longer than #second */
350}
351#first {
Marc Kupietzb1029362016-02-27 21:38:55 +0100352 margin-right: 20px;
353 float: left;
354 // border: 1px solid green;
Marc Kupietzc4893362016-02-25 08:04:46 +0100355}
356#second {
357 border: 1px solid #333;
358 overflow: hidden; /* if you don't want #second to wrap below #first */
359}
Marc Kupietzd7aea722016-03-02 11:59:12 +0100360#som2 svg {
361 border: 1px solid #333;
362}
363
Marc Kupietz4aa62172016-02-25 10:39:27 +0100364#cost {
Marc Kupietzb1029362016-02-27 21:38:55 +0100365 font-size: 8pt;
366 color: #222222;
367 margin-top: 4px;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100368 margin-bottom: 12px;
Marc Kupietz4aa62172016-02-25 10:39:27 +0100369}
Marc Kupietzd7aea722016-03-02 11:59:12 +0100370
Marc Kupietz6c1ca442016-03-03 09:35:18 +0100371#sominfo1, #sominfo {
Marc Kupietzd7aea722016-03-02 11:59:12 +0100372 font-size: 8pt;
373 color: #222222;
374 margin-top: 0px;
375}
376
Marc Kupietz6c1ca442016-03-03 09:35:18 +0100377#somcolor1, #somcolor2, #somcolor3 {
378 display: inline-block;
379 height: 10px;
380 width: 10px;
381}
382
Marc Kupietzd7aea722016-03-02 11:59:12 +0100383#third {
384 border: 1px solid #333;
385}
386
Marc Kupietzc4893362016-02-25 08:04:46 +0100387</style>
388<script>
389
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100390var opt = {epsilon: <%= $epsilon %>, perplexity: <%= $perplexity %>},
Marc Kupietz9fca1732016-02-29 09:07:04 +0100391 mapWidth = 800, // width map
392 mapHeight = 800,
393 jitterRadius = 7;
394
Marc Kupietzc4893362016-02-25 08:04:46 +0100395var T = new tsnejs.tSNE(opt); // create a tSNE instance
396
397var Y;
398
399var data;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100400var labeler;
Marc Kupietzc4893362016-02-25 08:04:46 +0100401
Marc Kupietzc5990da2016-02-26 08:47:12 +0100402
403function applyJitter() {
Marc Kupietzd7aea722016-03-02 11:59:12 +0100404 svg.selectAll('.tsnet')
Marc Kupietzc5990da2016-02-26 08:47:12 +0100405 .data(labels)
406 .transition()
407 .duration(50)
408 .attr("transform", function(d, i) {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100409 T.Y[i][0] = (d.x - mapWidth/2 - tx)/ss/20;
410 T.Y[i][1] = (d.y - mapHeight/2 - ty)/ss/20;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100411 return "translate(" +
412 (d.x) + "," +
413 (d.y) + ")";
414 });
415}
416
Marc Kupietzc4893362016-02-25 08:04:46 +0100417function updateEmbedding() {
418 var Y = T.getSolution();
Marc Kupietzd7aea722016-03-02 11:59:12 +0100419 svg.selectAll('.tsnet')
Marc Kupietz9fca1732016-02-29 09:07:04 +0100420 .data(data.words)
421 .attr("transform", function(d, i) {
422 return "translate(" +
423 ((Y[i][0]*20*ss + tx) + mapWidth/2) + "," +
424 ((Y[i][1]*20*ss + ty) + mapHeight/2) + ")"; });
Marc Kupietzc4893362016-02-25 08:04:46 +0100425}
426
427var svg;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100428var labels = [];
429var anchor_array = [];
430var text;
431
Marc Kupietzc4893362016-02-25 08:04:46 +0100432function drawEmbedding() {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100433 $("#embed").empty();
434 var div = d3.select("#embed");
435
436 // get min and max in each column of Y
437 var Y = T.Y;
438
439 svg = div.append("svg") // svg is global
440 .attr("width", mapWidth)
441 .attr("height", mapHeight);
442
443 var g = svg.selectAll(".b")
444 .data(data.words)
445 .enter().append("g")
Marc Kupietzd7aea722016-03-02 11:59:12 +0100446 .attr("class", "tsnet");
Marc Kupietz9fca1732016-02-29 09:07:04 +0100447
448 g.append("a")
449 .attr("xlink:href", function(word) {return "/?word="+word;})
450 .attr("title", function(d, i) {
Marc Kupietze50c8162016-03-01 10:24:43 +0100451 return "rank: "+i +" "+"freq. rank: "+data.ranks[i].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
Marc Kupietz9fca1732016-02-29 09:07:04 +0100452 })
Marc Kupietza350bce2016-02-25 09:34:25 +0100453 .append("text")
Marc Kupietz9fca1732016-02-29 09:07:04 +0100454 .attr("text-anchor", "top")
455 .attr("font-size", 12)
456 .attr("fill", function(d) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100457 if(data.target.indexOf(" "+d+" ") >= 0) {
Marc Kupietza350bce2016-02-25 09:34:25 +0100458 return "red";
459 } else {
460 return "#333"
Marc Kupietzc4893362016-02-25 08:04:46 +0100461 }
Marc Kupietz9fca1732016-02-29 09:07:04 +0100462 })
463 .text(function(d) { return d; });
464
465 var zoomListener = d3.behavior.zoom()
466 .scaleExtent([0.1, 10])
467 .center([0,0])
468 .on("zoom", zoomHandler);
469 zoomListener(svg);
470}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100471
Marc Kupietz9fca1732016-02-29 09:07:04 +0100472var tx=0, ty=0;
473var ss=1;
474var iter_id=-1;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100475
Marc Kupietz9fca1732016-02-29 09:07:04 +0100476function zoomHandler() {
477 tx = d3.event.translate[0];
478 ty = d3.event.translate[1];
479 ss = d3.event.scale;
480 updateEmbedding();
481}
482
483var stepnum = 0;
484
485function stopStep() {
486 clearInterval(iter_id);
487 text = svg.selectAll("text");
488
489 // jitter function needs different data and co-ordinate representation
490 labels = d3.range(data.words.length).map(function(i) {
491 var x = (T.Y[i][0]*20*ss + tx) + mapWidth/2;
492 var y = (T.Y[i][1]*20*ss + ty) + mapHeight/2;
493 anchor_array.push({x: x, y: y, r: jitterRadius});
494 return {
495 x: x,
496 y: y,
497 name: data.words[i]
498 };
499 });
500
501 // get the actual label bounding boxes for the jitter function
502 var index = 0;
503 text.each(function() {
504 labels[index].width = this.getBBox().width;
505 labels[index].height = this.getBBox().height;
506 index += 1;
507 });
Marc Kupietzc5990da2016-02-26 08:47:12 +0100508
509
510// setTimeout(updateEmbedding, 1);
511// setTimeout(
Marc Kupietz9fca1732016-02-29 09:07:04 +0100512 labeler = d3.labeler()
513 .label(labels)
514 .anchor(anchor_array)
515 .width(mapWidth)
516 .height(mapHeight)
517 .update(applyJitter);
518 // .start(1000);
Marc Kupietzc5990da2016-02-26 08:47:12 +0100519
Marc Kupietz9fca1732016-02-29 09:07:04 +0100520 iter_id = setInterval(jitterStep, 1);
521}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100522
Marc Kupietz9fca1732016-02-29 09:07:04 +0100523var jitter_i=0;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100524
525function jitterStep() {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100526 if(jitter_i++ > 100) {
527 clearInterval(iter_id);
528 } else {
529 labeler.start2(10);
530 applyJitter();
531 }
Marc Kupietzc5990da2016-02-26 08:47:12 +0100532}
Marc Kupietzb1029362016-02-27 21:38:55 +0100533
534var last_cost=1000;
535
Marc Kupietz9fca1732016-02-29 09:07:04 +0100536function step() {
537 var i = T.iter;
538
539 if(i > <%= $no_iterations %>) {
540 stopStep();
541 } else {
542 var cost = Math.round(T.step() * 100000) / 100000; // do a few steps
543 $("#cost").html("tsne iteration " + i + ", cost: " + cost.toFixed(5));
544 if(i % 250 == 0 && cost >= last_cost) {
545 stopStep();
546 } else {
547 last_cost = cost;
548 updateEmbedding();
549 }
550 }
551}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100552
Marc Kupietz9fca1732016-02-29 09:07:04 +0100553function showMap(j) {
554 data=j;
555 T.iter=0;
556 T.initDataRaw(data.vecs); // init embedding
557 drawEmbedding(); // draw initial embedding
558
559 if(iter_id >= 0) {
560 clearInterval(iter_id);
561 }
562 //T.debugGrad();
563 iter_id = setInterval(step, 1);
Marc Kupietzd7aea722016-03-02 11:59:12 +0100564 if(<%= $show_som %>) {
565 makeSOM(j, <%= $no_iterations %>);
566 }
Marc Kupietz9fca1732016-02-29 09:07:04 +0100567}
Marc Kupietza350bce2016-02-25 09:34:25 +0100568
Marc Kupietzc4893362016-02-25 08:04:46 +0100569</script>
570</head>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200571<body>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100572 <form action="<%=url_for('/')->to_abs%>" method="GET">
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100573 word(s):
574 <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.">
575 max. neighbours: <input type="text" size="8" name="n" value="<%= $no_nbs %>">
Marc Kupietzd7aea722016-03-02 11:59:12 +0100576 max. iterations: <input type="text" name="N" size="8" value="<%= $no_iterations %>">
577 SOM <input type="checkbox" name="som" value="1" <%= ($show_som ? "checked" : "") %>>
578 <span> </span><input type="submit" value="Show">
Marc Kupietz4aa62172016-02-25 10:39:27 +0100579 </form>
580 <br>
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100581 % if($lists) {
Marc Kupietz4aa62172016-02-25 10:39:27 +0100582 <div id="wrapper">
583 <table id="first">
584 <tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100585 <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 +0100586 </tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100587 % my $j=0; my @words; my @vecs; my @ranks; for my $list (@$lists) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100588 % my $i=1; for my $item (@$list) {
589 % if(!grep{$_ eq $item->{word}} @words) {
590 % push @vecs, $item->{vector};
591 % push @words, $item->{word};
Marc Kupietz5f780672016-02-25 17:15:54 +0100592 % push @ranks, $item->{rank};
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100593 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100594 <tr>
595 <td align="right">
596 <%= $i++ %>.
597 </td>
598 <td>
599 <a href="/?word=<%= $item->{word} %>">
600 <%= $item->{word} %>
601 </a>
602 </td>
603 <td align="right">
604 <%= sprintf("%.3f", $item->{dist}) %>
605 </td>
Marc Kupietz5f780672016-02-25 17:15:54 +0100606 <td align="right">
607 <%= $item->{rank} %>
608 </td>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100609 </tr>
610 % }
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100611 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100612 </table>
613 <script>
614 % use Mojo::ByteStream 'b';
615 $(window).load(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100616 showMap(<%= b(Mojo::JSON::to_json({target => " $word ", words => \@words, vecs => \@vecs, ranks => \@ranks})); %>);
Marc Kupietz4aa62172016-02-25 10:39:27 +0100617 });
618 </script>
619 % }
620 <div id="second" style="width:800px; height:800px; font-family: arial;">
621 <div id="embed">
622 </div>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100623 </div>
Marc Kupietzb1029362016-02-27 21:38:55 +0100624 <div id="cost"></div>
Marc Kupietzd7aea722016-03-02 11:59:12 +0100625 % if($show_som) {
626 <div id="som2">
627 </div>
Marc Kupietz6c1ca442016-03-03 09:35:18 +0100628 <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 +0100629 <div id="sominfo">SOM iteration <span id="iterations">0</span></div>
630 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100631 </div>
632 <p>
633 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 +0200634 <pre>
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100635-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 +0100636 </pre>
637 </p>
638</body>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200639</html>
640