blob: 149dfbfcd91682ec55150bee65aedfc6a542ae30 [file] [log] [blame]
Marc Kupietzdc22b982015-10-09 09:19:34 +02001#!/usr/local/bin/perl
2use Inline C;
3use Mojolicious::Lite;
Marc Kupietzc4893362016-02-25 08:04:46 +01004use Mojo::JSON qw(decode_json encode_json to_json);
Marc Kupietz247500f2015-10-09 11:29:01 +02005use Encode qw(decode encode);
Marc Kupietz7bc85fd2016-02-24 11:42:41 +01006use Mojo::Server::Daemon;
Marc Kupietzdc22b982015-10-09 09:19:34 +02007
Marc Kupietzc5990da2016-02-26 08:47:12 +01008print STDERR $ARGV[0];
Marc Kupietz7bc85fd2016-02-24 11:42:41 +01009# -cbow 1 -size 200 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 40 -binary 1 -iter 15
Marc Kupietz44bee3c2016-02-25 16:26:29 +010010init_net("vectors15.bin");
Marc Kupietzdc22b982015-10-09 09:19:34 +020011
12get '/' => sub {
13 my $c = shift;
14 my $word=$c->param('word');
Marc Kupietz44bee3c2016-02-25 16:26:29 +010015 my $no_nbs=$c->param('n') || 100;
16 my $no_iterations=$c->param('N') || 2000;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010017 my @lists;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010018 if(defined($word) && $word !~ /^\s*$/) {
19 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +010020 $word =~ s/\s+/ /g;
21 for my $w (split(' *\| *', $word)) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010022 $c->app->log->debug('Looking for neighbours of '.$w);
23 push(@lists, get_neighbours(encode("iso-8859-1", $w), $no_nbs));
24 }
Marc Kupietz247500f2015-10-09 11:29:01 +020025 }
Marc Kupietz44bee3c2016-02-25 16:26:29 +010026 $c->render(template=>"index", word=>$word, no_nbs=>$no_nbs, no_iterations => $no_iterations, lists=> \@lists);
Marc Kupietzdc22b982015-10-09 09:19:34 +020027};
28
29app->start;
30
31exit;
32
33__END__
34
35__C__
36#include <stdio.h>
37#include <string.h>
38#include <math.h>
39#include <malloc.h>
40#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +010041#include <sys/mman.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +020042
43#define max_size 2000
44#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010045#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +010046#define MAX_WORDS -1
Marc Kupietzdc22b982015-10-09 09:19:34 +020047
48//the thread function
49void *connection_handler(void *);
50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010051char *bestw[MAX_NEIGHBOURS];
Marc Kupietzdc22b982015-10-09 09:19:34 +020052char file_name[max_size], st[100][max_size];
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010053float dist, len, bestd[MAX_NEIGHBOURS], vec[max_size];
Marc Kupietzc4893362016-02-25 08:04:46 +010054long long words, size, a, b, c, d, cn, bi[100], besti[MAX_NEIGHBOURS];
Marc Kupietzdc22b982015-10-09 09:19:34 +020055char ch;
56float *M;
57char *vocab;
58char *stringBuffer;
59
60int init_net(char *file_name) {
Marc Kupietz67c20282016-02-26 09:42:00 +010061 FILE *f, *binvecs, *binwords;
Marc Kupietzf0809762016-02-26 10:13:47 +010062 int binwords_fd, binvecs_fd;
Marc Kupietz67c20282016-02-26 09:42:00 +010063 char binvecs_fname[256], binwords_fname[256];
64 strcpy(binwords_fname, file_name);
65 strcat(binwords_fname, ".words");
66 strcpy(binvecs_fname, file_name);
67 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +020068
69 stringBuffer = malloc(64000);
70 f = fopen(file_name, "rb");
71 if (f == NULL) {
72 printf("Input file %s not found\n", file_name);
73 return -1;
74 }
75 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +010076 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +020077 fscanf(f, "%lld", &size);
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010078 for (a = 0; a < MAX_NEIGHBOURS; a++) bestw[a] = (char *)malloc(max_size * sizeof(char));
Marc Kupietzf0809762016-02-26 10:13:47 +010079 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
80 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_PRIVATE, binvecs_fd, 0);
81 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_PRIVATE, binwords_fd, 0);
82 if (M == MAP_FAILED || vocab == MAP_FAILED) {
83 close(binvecs_fd);
84 close(binwords_fd);
85 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
86 exit(-1);
87 }
Marc Kupietz67c20282016-02-26 09:42:00 +010088 } else {
Marc Kupietzf0809762016-02-26 10:13:47 +010089 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
90 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
91 if (M == NULL) {
92 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
93 return -1;
94 }
Marc Kupietz67c20282016-02-26 09:42:00 +010095 for (b = 0; b < words; b++) {
96 a = 0;
97 while (1) {
98 vocab[b * max_w + a] = fgetc(f);
99 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
100 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
101 }
102 vocab[b * max_w + a] = 0;
103 fread(&M[b * size], sizeof(float), size, f);
104 len = 0;
105 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
106 len = sqrt(len);
107 for (a = 0; a < size; a++) M[a + b * size] /= len;
108 }
109 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
110 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
111 fclose(binvecs);
112 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
113 fclose(binwords);
114 }
115 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200116 fclose(f);
117 return 0;
118}
119
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100120SV *get_neighbours(char *st1, int N) {
121 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
122
Marc Kupietzdc22b982015-10-09 09:19:34 +0200123 FILE *out=stdout;
124 *stringBuffer=0;
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100125 float worstbest=0;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200126
127 for (a = 0; a < N; a++) bestd[a] = 0;
128 for (a = 0; a < N; a++) bestw[a][0] = 0;
129 a = 0;
130 cn = 0;
131 b = 0;
132 c = 0;
133 while (1) {
134 st[cn][b] = st1[c];
135 b++;
136 c++;
137 st[cn][b] = 0;
138 if (st1[c] == 0) break;
139 if (st1[c] == ' ') {
140 cn++;
141 b = 0;
142 c++;
143 }
144 }
145 cn++;
146 for (a = 0; a < cn; a++) {
147 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
148 if (b == words) b = -1;
149 bi[a] = b;
Marc Kupietze8da3062016-02-25 08:37:53 +0100150 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], bi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200151 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100152 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100153 cn--;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200154 break;
155 }
156 }
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100157 if (b == -1 && cn <= 0) {
158 N = 0;
159 goto end;
160 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200161 for (a = 0; a < size; a++) vec[a] = 0;
162 for (b = 0; b < cn; b++) {
163 if (bi[b] == -1) continue;
164 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
165 }
166 len = 0;
167 for (a = 0; a < size; a++) len += vec[a] * vec[a];
168 len = sqrt(len);
169 for (a = 0; a < size; a++) vec[a] /= len;
170 for (a = 0; a < N; a++) bestd[a] = -1;
171 for (a = 0; a < N; a++) bestw[a][0] = 0;
172 for (c = 0; c < words; c++) {
173 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100174// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100175// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
176// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200177 dist = 0;
178 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100179 if(dist > worstbest) {
180 for (a = 0; a < N; a++) {
181 if (dist > bestd[a]) {
182 for (d = N - 1; d > a; d--) {
183 bestd[d] = bestd[d - 1];
184 besti[d] = besti[d - 1];
185 }
186 bestd[a] = dist;
187 besti[a] = c;
188 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200189 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200190 }
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100191 worstbest = bestd[N-1];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200192 }
193 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100194
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100195end:
196 a=0;
Marc Kupietz247500f2015-10-09 11:29:01 +0200197 AV* array = newAV();
198 for (a = 0; a < N; a++) {
Marc Kupietz34020dc2016-02-25 08:44:19 +0100199 strcpy(bestw[a], &vocab[besti[a] * max_w]);
Marc Kupietz247500f2015-10-09 11:29:01 +0200200 HV* hash = newHV();
201 hv_store(hash, "word", strlen("word"), newSVpvf(bestw[a], 0), 0);
202 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
Marc Kupietz5f780672016-02-25 17:15:54 +0100203 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
Marc Kupietzc4893362016-02-25 08:04:46 +0100204 AV *vector = newAV();
205 for (b = 0; b < size; b++) {
206 av_push(vector, newSVnv(M[b + besti[a] * size]));
207 }
208 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
Marc Kupietz247500f2015-10-09 11:29:01 +0200209 av_push(array, newRV_noinc((SV*)hash));
210 }
Marc Kupietz247500f2015-10-09 11:29:01 +0200211 return newRV_noinc((SV*)array);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200212}
213
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100214
Marc Kupietzdc22b982015-10-09 09:19:34 +0200215__DATA__
216
217@@ index.html.ep
218<!DOCTYPE html>
219<html>
Marc Kupietzc4893362016-02-25 08:04:46 +0100220<head>
221 <title>DeReKo-Word-Vector-Distances</title>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100222 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
Marc Kupietzc4893362016-02-25 08:04:46 +0100223 <script src="http://code.jquery.com/jquery-latest.min.js"></script>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100224 <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
225 <script>
226 $(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100227 $( document ).tooltip({
228 content: function() {
229 return $(this).attr('title');
230 }}
231 )
232 })
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100233 </script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100234 <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
235 <script src="http://klinux10/word2vec/tsne.js"></script>
Marc Kupietzc5990da2016-02-26 08:47:12 +0100236 <script src="http://klinux10/word2vec/labeler.js"></script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100237<style>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100238body, input {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100239 font-family: Arial, sans-serif;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100240 font-size: 11pt;
241}
242
243.ui-tooltip-content {
244 font-size: 9pt;
245 colour: #222222;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100246}
Marc Kupietz5f780672016-02-25 17:15:54 +0100247
248svg > .ui-tooltip-content {
249 font-size: 7pt;
250 colour: #222222;
251}
252
Marc Kupietzc4893362016-02-25 08:04:46 +0100253svg {
254// border: 1px solid #333;
Marc Kupietz4aa62172016-02-25 10:39:27 +0100255 margin-right: 10px;
256 margin-bottom:10px;
Marc Kupietzc4893362016-02-25 08:04:46 +0100257}
258#wrapper {
259 width: 100%;
260// border: 1px solid red;
261 overflow: hidden; /* will contain if #first is longer than #second */
262}
263#first {
Marc Kupietzc4893362016-02-25 08:04:46 +0100264 margin-right: 20px;
265 float:left; /* add this */
266// border: 1px solid green;
267}
268#second {
269 border: 1px solid #333;
Marc Kupietz4aa62172016-02-25 10:39:27 +0100270 height: 850px;
Marc Kupietzc4893362016-02-25 08:04:46 +0100271 overflow: hidden; /* if you don't want #second to wrap below #first */
272}
Marc Kupietz4aa62172016-02-25 10:39:27 +0100273#cost {
274 z-index: 1;
275 position: fixed;
276 font-size: 10px;
277 color: #222222;
278 margin-bottom: 10px;
279}
Marc Kupietzc4893362016-02-25 08:04:46 +0100280</style>
281<script>
282
Marc Kupietz4aa62172016-02-25 10:39:27 +0100283var opt = {epsilon: 1, perplexity: 20};
Marc Kupietzc4893362016-02-25 08:04:46 +0100284var T = new tsnejs.tSNE(opt); // create a tSNE instance
285
286var Y;
287
288var data;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100289var labeler;
Marc Kupietzc4893362016-02-25 08:04:46 +0100290
Marc Kupietzc5990da2016-02-26 08:47:12 +0100291
292function applyJitter() {
293 svg.selectAll('.u')
294 .data(labels)
295 .transition()
296 .duration(50)
297 .attr("transform", function(d, i) {
298 T.Y[i][0] = (d.x - 400 - tx)/ss/20;
299 T.Y[i][1] = (d.y - 400 - ty)/ss/20;
300 return "translate(" +
301 (d.x) + "," +
302 (d.y) + ")";
303 });
304}
305
Marc Kupietzc4893362016-02-25 08:04:46 +0100306function updateEmbedding() {
307 var Y = T.getSolution();
308 svg.selectAll('.u')
309 .data(data.words)
Marc Kupietzc5990da2016-02-26 08:47:12 +0100310 .attr("transform", function(d, i) {
311 return "translate(" +
312 ((Y[i][0]*20*ss + tx) + 400) + "," +
313 ((Y[i][1]*20*ss + ty) + 400) + ")"; });
Marc Kupietzc4893362016-02-25 08:04:46 +0100314}
315
316var svg;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100317var labels = [];
318var anchor_array = [];
319var text;
320
Marc Kupietzc4893362016-02-25 08:04:46 +0100321function drawEmbedding() {
Marc Kupietza350bce2016-02-25 09:34:25 +0100322 $("#embed").empty();
323 var div = d3.select("#embed");
324
325 // get min and max in each column of Y
326 var Y = T.Y;
327
328 svg = div.append("svg") // svg is global
329 .attr("width", 800)
330 .attr("height", 800);
331
332 var g = svg.selectAll(".b")
333 .data(data.words)
334 .enter().append("g")
335 .attr("class", "u");
336
337 g.append("a")
338 .attr("xlink:href", function(word) {return "/?word="+word;})
Marc Kupietz5f780672016-02-25 17:15:54 +0100339 .attr("title", function(d, i) {
340 return "rank: "+i +" "+"freq. rank: "+data.ranks[i];
341 })
Marc Kupietza350bce2016-02-25 09:34:25 +0100342 .append("text")
343 .attr("text-anchor", "top")
344 .attr("font-size", 12)
345 .attr("fill", function(d) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100346 if(data.target.indexOf(" "+d+" ") >= 0) {
Marc Kupietza350bce2016-02-25 09:34:25 +0100347 return "red";
348 } else {
349 return "#333"
Marc Kupietzc4893362016-02-25 08:04:46 +0100350 }
Marc Kupietza350bce2016-02-25 09:34:25 +0100351 })
352 .text(function(d) { return d; });
353
354 var zoomListener = d3.behavior.zoom()
355 .scaleExtent([0.1, 10])
356 .center([0,0])
357 .on("zoom", zoomHandler);
358 zoomListener(svg);
359 }
360
361 var tx=0, ty=0;
362 var ss=1;
363 var iter_id=-1;
364
365 function zoomHandler() {
366 tx = d3.event.translate[0];
367 ty = d3.event.translate[1];
368 ss = d3.event.scale;
369 updateEmbedding();
370 }
371
372 var stepnum = 0;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100373
Marc Kupietza350bce2016-02-25 09:34:25 +0100374 function stopStep() {
375 clearInterval(iter_id);
Marc Kupietzc5990da2016-02-26 08:47:12 +0100376 text = svg.selectAll("text");
377
378 labels = d3.range(data.words.length).map(function(i) {
379 var x = (T.Y[i][0]*20*ss + tx) + 400;
380 var y = (T.Y[i][1]*20*ss + ty) + 400;
381 anchor_array.push({x: x, y: y, r: 5});
382 return {
383 x: x,
384 y: y,
385 name: data.words[i]
386 };
387 });
388
389 var index = 0;
390 text.each(function() {
391 labels[index].width = this.getBBox().width;
392 labels[index].height = this.getBBox().height;
393 index += 1;
394 });
395
396
397// setTimeout(updateEmbedding, 1);
398// setTimeout(
399 labeler = d3.labeler()
400 .label(labels)
401 .anchor(anchor_array)
402 .width(800)
403 .height(800)
404 .update(applyJitter);
405// .start(1000);
406
407 iter_id = setInterval(jitterStep, 1);
408
Marc Kupietza350bce2016-02-25 09:34:25 +0100409 }
Marc Kupietzc5990da2016-02-26 08:47:12 +0100410
411var jitter_i=0;;
412
413function jitterStep() {
414 if(jitter_i++ > 100) {
415 clearInterval(iter_id);
416 } else {
417 labeler.start2(10);
418 applyJitter();
419 }
420}
Marc Kupietza350bce2016-02-25 09:34:25 +0100421
422 function step() {
423 var i = T.iter;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100424 if(i > <%= $no_iterations %>) {
Marc Kupietza350bce2016-02-25 09:34:25 +0100425 stopStep();
426 } else {
Marc Kupietz4aa62172016-02-25 10:39:27 +0100427 var cost = Math.round(T.step() *1000) / 1000; // do a few steps
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100428 $("#cost").html("tsne iteration " + i + ", cost: " + cost.toFixed(3));
Marc Kupietza350bce2016-02-25 09:34:25 +0100429 updateEmbedding();
430 }
431 }
Marc Kupietzc5990da2016-02-26 08:47:12 +0100432
Marc Kupietza350bce2016-02-25 09:34:25 +0100433 function showMap(j) {
434 data=j;
435 T.iter=0;
436 T.initDataRaw(data.vecs); // init embedding
437 drawEmbedding(); // draw initial embedding
438
439 if(iter_id >= 0) {
440 clearInterval(iter_id);
441 }
442 //T.debugGrad();
443 iter_id = setInterval(step, 1);
444 //step();
445 }
446
Marc Kupietzc5990da2016-02-26 08:47:12 +0100447 function update() {
448
449 text.transition().duration(800)
450 .attr("transform", transform);
451 }
452
453
Marc Kupietzc4893362016-02-25 08:04:46 +0100454</script>
455</head>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200456<body>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100457 <form action="<%=url_for('/')->to_abs%>" method="GET">
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100458 word(s):
459 <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.">
460 max. neighbours: <input type="text" size="8" name="n" value="<%= $no_nbs %>">
461 iterations: <input type="text" name="N" size="8" value="<%= $no_iterations %>">
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100462 <input type="submit" value="Show">
Marc Kupietz4aa62172016-02-25 10:39:27 +0100463 </form>
464 <br>
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100465 % if($lists) {
Marc Kupietz4aa62172016-02-25 10:39:27 +0100466 <div id="wrapper">
467 <table id="first">
468 <tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100469 <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 +0100470 </tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100471 % my $j=0; my @words; my @vecs; my @ranks; for my $list (@$lists) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100472 % my $i=1; for my $item (@$list) {
473 % if(!grep{$_ eq $item->{word}} @words) {
474 % push @vecs, $item->{vector};
475 % push @words, $item->{word};
Marc Kupietz5f780672016-02-25 17:15:54 +0100476 % push @ranks, $item->{rank};
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100477 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100478 <tr>
479 <td align="right">
480 <%= $i++ %>.
481 </td>
482 <td>
483 <a href="/?word=<%= $item->{word} %>">
484 <%= $item->{word} %>
485 </a>
486 </td>
487 <td align="right">
488 <%= sprintf("%.3f", $item->{dist}) %>
489 </td>
Marc Kupietz5f780672016-02-25 17:15:54 +0100490 <td align="right">
491 <%= $item->{rank} %>
492 </td>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100493 </tr>
494 % }
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100495 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100496 </table>
497 <script>
498 % use Mojo::ByteStream 'b';
499 $(window).load(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100500 showMap(<%= b(Mojo::JSON::to_json({target => " $word ", words => \@words, vecs => \@vecs, ranks => \@ranks})); %>);
Marc Kupietz4aa62172016-02-25 10:39:27 +0100501 });
502 </script>
503 % }
504 <div id="second" style="width:800px; height:800px; font-family: arial;">
505 <div id="embed">
506 </div>
507 <div id="cost"></div>
508 </div>
509 </div>
510 <p>
511 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 +0200512 <pre>
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100513-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 +0100514 </pre>
515 </p>
516</body>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200517</html>
518