blob: 8c7d048dac4491f9ed753385dbdba4ab454e9dbc [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 Kupietzdc22b982015-10-09 09:19:34 +020051float *M;
52char *vocab;
Marc Kupietz82b02672016-02-26 12:32:25 +010053long long words, size;
Marc Kupietzdc22b982015-10-09 09:19:34 +020054
55int init_net(char *file_name) {
Marc Kupietz67c20282016-02-26 09:42:00 +010056 FILE *f, *binvecs, *binwords;
Marc Kupietzf0809762016-02-26 10:13:47 +010057 int binwords_fd, binvecs_fd;
Marc Kupietz82b02672016-02-26 12:32:25 +010058 long long a, b, c, d, cn;
59 float len;
60
Marc Kupietz67c20282016-02-26 09:42:00 +010061 char binvecs_fname[256], binwords_fname[256];
62 strcpy(binwords_fname, file_name);
63 strcat(binwords_fname, ".words");
64 strcpy(binvecs_fname, file_name);
65 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +020066
Marc Kupietzdc22b982015-10-09 09:19:34 +020067 f = fopen(file_name, "rb");
68 if (f == NULL) {
69 printf("Input file %s not found\n", file_name);
70 return -1;
71 }
72 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +010073 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +020074 fscanf(f, "%lld", &size);
Marc Kupietzf0809762016-02-26 10:13:47 +010075 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
76 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_PRIVATE, binvecs_fd, 0);
77 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_PRIVATE, binwords_fd, 0);
78 if (M == MAP_FAILED || vocab == MAP_FAILED) {
79 close(binvecs_fd);
80 close(binwords_fd);
81 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
82 exit(-1);
83 }
Marc Kupietz67c20282016-02-26 09:42:00 +010084 } else {
Marc Kupietzf0809762016-02-26 10:13:47 +010085 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
86 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
87 if (M == NULL) {
88 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
89 return -1;
90 }
Marc Kupietz67c20282016-02-26 09:42:00 +010091 for (b = 0; b < words; b++) {
92 a = 0;
93 while (1) {
94 vocab[b * max_w + a] = fgetc(f);
95 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
96 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
97 }
98 vocab[b * max_w + a] = 0;
99 fread(&M[b * size], sizeof(float), size, f);
100 len = 0;
101 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
102 len = sqrt(len);
103 for (a = 0; a < size; a++) M[a + b * size] /= len;
104 }
105 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
106 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
107 fclose(binvecs);
108 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
109 fclose(binwords);
110 }
111 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200112 fclose(f);
113 return 0;
114}
115
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100116SV *get_neighbours(char *st1, int N) {
Marc Kupietz82b02672016-02-26 12:32:25 +0100117 char *bestw[MAX_NEIGHBOURS];
118 char file_name[max_size], st[100][max_size];
119 float dist, len, bestd[MAX_NEIGHBOURS], vec[max_size];
120 long long a, b, c, d, cn, bi[100], besti[MAX_NEIGHBOURS];
121 char ch;
122
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100123 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
124
Marc Kupietz82b02672016-02-26 12:32:25 +0100125 for (a = 0; a < MAX_NEIGHBOURS; a++) bestw[a] = (char *)malloc(max_size * sizeof(char));
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100126 float worstbest=0;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200127
128 for (a = 0; a < N; a++) bestd[a] = 0;
129 for (a = 0; a < N; a++) bestw[a][0] = 0;
130 a = 0;
131 cn = 0;
132 b = 0;
133 c = 0;
134 while (1) {
135 st[cn][b] = st1[c];
136 b++;
137 c++;
138 st[cn][b] = 0;
139 if (st1[c] == 0) break;
140 if (st1[c] == ' ') {
141 cn++;
142 b = 0;
143 c++;
144 }
145 }
146 cn++;
147 for (a = 0; a < cn; a++) {
148 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
149 if (b == words) b = -1;
150 bi[a] = b;
Marc Kupietze8da3062016-02-25 08:37:53 +0100151 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], bi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200152 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100153 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100154 cn--;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200155 break;
156 }
157 }
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100158 if (b == -1 && cn <= 0) {
159 N = 0;
160 goto end;
161 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200162 for (a = 0; a < size; a++) vec[a] = 0;
163 for (b = 0; b < cn; b++) {
164 if (bi[b] == -1) continue;
165 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
166 }
167 len = 0;
168 for (a = 0; a < size; a++) len += vec[a] * vec[a];
169 len = sqrt(len);
170 for (a = 0; a < size; a++) vec[a] /= len;
171 for (a = 0; a < N; a++) bestd[a] = -1;
172 for (a = 0; a < N; a++) bestw[a][0] = 0;
173 for (c = 0; c < words; c++) {
174 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100175// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100176// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
177// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200178 dist = 0;
179 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100180 if(dist > worstbest) {
181 for (a = 0; a < N; a++) {
182 if (dist > bestd[a]) {
183 for (d = N - 1; d > a; d--) {
184 bestd[d] = bestd[d - 1];
185 besti[d] = besti[d - 1];
186 }
187 bestd[a] = dist;
188 besti[a] = c;
189 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200190 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200191 }
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100192 worstbest = bestd[N-1];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200193 }
194 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100195
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100196end:
197 a=0;
Marc Kupietz247500f2015-10-09 11:29:01 +0200198 AV* array = newAV();
199 for (a = 0; a < N; a++) {
Marc Kupietz34020dc2016-02-25 08:44:19 +0100200 strcpy(bestw[a], &vocab[besti[a] * max_w]);
Marc Kupietz247500f2015-10-09 11:29:01 +0200201 HV* hash = newHV();
202 hv_store(hash, "word", strlen("word"), newSVpvf(bestw[a], 0), 0);
203 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
Marc Kupietz5f780672016-02-25 17:15:54 +0100204 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
Marc Kupietzc4893362016-02-25 08:04:46 +0100205 AV *vector = newAV();
206 for (b = 0; b < size; b++) {
207 av_push(vector, newSVnv(M[b + besti[a] * size]));
208 }
209 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
Marc Kupietz247500f2015-10-09 11:29:01 +0200210 av_push(array, newRV_noinc((SV*)hash));
211 }
Marc Kupietz247500f2015-10-09 11:29:01 +0200212 return newRV_noinc((SV*)array);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200213}
214
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100215
Marc Kupietzdc22b982015-10-09 09:19:34 +0200216__DATA__
217
218@@ index.html.ep
219<!DOCTYPE html>
220<html>
Marc Kupietzc4893362016-02-25 08:04:46 +0100221<head>
222 <title>DeReKo-Word-Vector-Distances</title>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100223 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
Marc Kupietzc4893362016-02-25 08:04:46 +0100224 <script src="http://code.jquery.com/jquery-latest.min.js"></script>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100225 <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
226 <script>
227 $(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100228 $( document ).tooltip({
229 content: function() {
230 return $(this).attr('title');
231 }}
232 )
233 })
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100234 </script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100235 <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
236 <script src="http://klinux10/word2vec/tsne.js"></script>
Marc Kupietzc5990da2016-02-26 08:47:12 +0100237 <script src="http://klinux10/word2vec/labeler.js"></script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100238<style>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100239body, input {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100240 font-family: Arial, sans-serif;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100241 font-size: 11pt;
242}
243
244.ui-tooltip-content {
245 font-size: 9pt;
246 colour: #222222;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100247}
Marc Kupietz5f780672016-02-25 17:15:54 +0100248
249svg > .ui-tooltip-content {
250 font-size: 7pt;
251 colour: #222222;
252}
253
Marc Kupietzc4893362016-02-25 08:04:46 +0100254svg {
255// border: 1px solid #333;
Marc Kupietz4aa62172016-02-25 10:39:27 +0100256 margin-right: 10px;
257 margin-bottom:10px;
Marc Kupietzc4893362016-02-25 08:04:46 +0100258}
259#wrapper {
260 width: 100%;
261// border: 1px solid red;
262 overflow: hidden; /* will contain if #first is longer than #second */
263}
264#first {
Marc Kupietzc4893362016-02-25 08:04:46 +0100265 margin-right: 20px;
266 float:left; /* add this */
267// border: 1px solid green;
268}
269#second {
270 border: 1px solid #333;
Marc Kupietz4aa62172016-02-25 10:39:27 +0100271 height: 850px;
Marc Kupietzc4893362016-02-25 08:04:46 +0100272 overflow: hidden; /* if you don't want #second to wrap below #first */
273}
Marc Kupietz4aa62172016-02-25 10:39:27 +0100274#cost {
275 z-index: 1;
276 position: fixed;
277 font-size: 10px;
278 color: #222222;
279 margin-bottom: 10px;
280}
Marc Kupietzc4893362016-02-25 08:04:46 +0100281</style>
282<script>
283
Marc Kupietz4aa62172016-02-25 10:39:27 +0100284var opt = {epsilon: 1, perplexity: 20};
Marc Kupietzc4893362016-02-25 08:04:46 +0100285var T = new tsnejs.tSNE(opt); // create a tSNE instance
286
287var Y;
288
289var data;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100290var labeler;
Marc Kupietzc4893362016-02-25 08:04:46 +0100291
Marc Kupietzc5990da2016-02-26 08:47:12 +0100292
293function applyJitter() {
294 svg.selectAll('.u')
295 .data(labels)
296 .transition()
297 .duration(50)
298 .attr("transform", function(d, i) {
299 T.Y[i][0] = (d.x - 400 - tx)/ss/20;
300 T.Y[i][1] = (d.y - 400 - ty)/ss/20;
301 return "translate(" +
302 (d.x) + "," +
303 (d.y) + ")";
304 });
305}
306
Marc Kupietzc4893362016-02-25 08:04:46 +0100307function updateEmbedding() {
308 var Y = T.getSolution();
309 svg.selectAll('.u')
310 .data(data.words)
Marc Kupietzc5990da2016-02-26 08:47:12 +0100311 .attr("transform", function(d, i) {
312 return "translate(" +
313 ((Y[i][0]*20*ss + tx) + 400) + "," +
314 ((Y[i][1]*20*ss + ty) + 400) + ")"; });
Marc Kupietzc4893362016-02-25 08:04:46 +0100315}
316
317var svg;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100318var labels = [];
319var anchor_array = [];
320var text;
321
Marc Kupietzc4893362016-02-25 08:04:46 +0100322function drawEmbedding() {
Marc Kupietza350bce2016-02-25 09:34:25 +0100323 $("#embed").empty();
324 var div = d3.select("#embed");
325
326 // get min and max in each column of Y
327 var Y = T.Y;
328
329 svg = div.append("svg") // svg is global
330 .attr("width", 800)
331 .attr("height", 800);
332
333 var g = svg.selectAll(".b")
334 .data(data.words)
335 .enter().append("g")
336 .attr("class", "u");
337
338 g.append("a")
339 .attr("xlink:href", function(word) {return "/?word="+word;})
Marc Kupietz5f780672016-02-25 17:15:54 +0100340 .attr("title", function(d, i) {
341 return "rank: "+i +" "+"freq. rank: "+data.ranks[i];
342 })
Marc Kupietza350bce2016-02-25 09:34:25 +0100343 .append("text")
344 .attr("text-anchor", "top")
345 .attr("font-size", 12)
346 .attr("fill", function(d) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100347 if(data.target.indexOf(" "+d+" ") >= 0) {
Marc Kupietza350bce2016-02-25 09:34:25 +0100348 return "red";
349 } else {
350 return "#333"
Marc Kupietzc4893362016-02-25 08:04:46 +0100351 }
Marc Kupietza350bce2016-02-25 09:34:25 +0100352 })
353 .text(function(d) { return d; });
354
355 var zoomListener = d3.behavior.zoom()
356 .scaleExtent([0.1, 10])
357 .center([0,0])
358 .on("zoom", zoomHandler);
359 zoomListener(svg);
360 }
361
362 var tx=0, ty=0;
363 var ss=1;
364 var iter_id=-1;
365
366 function zoomHandler() {
367 tx = d3.event.translate[0];
368 ty = d3.event.translate[1];
369 ss = d3.event.scale;
370 updateEmbedding();
371 }
372
373 var stepnum = 0;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100374
Marc Kupietza350bce2016-02-25 09:34:25 +0100375 function stopStep() {
376 clearInterval(iter_id);
Marc Kupietzc5990da2016-02-26 08:47:12 +0100377 text = svg.selectAll("text");
378
379 labels = d3.range(data.words.length).map(function(i) {
380 var x = (T.Y[i][0]*20*ss + tx) + 400;
381 var y = (T.Y[i][1]*20*ss + ty) + 400;
382 anchor_array.push({x: x, y: y, r: 5});
383 return {
384 x: x,
385 y: y,
386 name: data.words[i]
387 };
388 });
389
390 var index = 0;
391 text.each(function() {
392 labels[index].width = this.getBBox().width;
393 labels[index].height = this.getBBox().height;
394 index += 1;
395 });
396
397
398// setTimeout(updateEmbedding, 1);
399// setTimeout(
400 labeler = d3.labeler()
401 .label(labels)
402 .anchor(anchor_array)
403 .width(800)
404 .height(800)
405 .update(applyJitter);
406// .start(1000);
407
408 iter_id = setInterval(jitterStep, 1);
409
Marc Kupietza350bce2016-02-25 09:34:25 +0100410 }
Marc Kupietzc5990da2016-02-26 08:47:12 +0100411
412var jitter_i=0;;
413
414function jitterStep() {
415 if(jitter_i++ > 100) {
416 clearInterval(iter_id);
417 } else {
418 labeler.start2(10);
419 applyJitter();
420 }
421}
Marc Kupietza350bce2016-02-25 09:34:25 +0100422
423 function step() {
424 var i = T.iter;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100425 if(i > <%= $no_iterations %>) {
Marc Kupietza350bce2016-02-25 09:34:25 +0100426 stopStep();
427 } else {
Marc Kupietz4aa62172016-02-25 10:39:27 +0100428 var cost = Math.round(T.step() *1000) / 1000; // do a few steps
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100429 $("#cost").html("tsne iteration " + i + ", cost: " + cost.toFixed(3));
Marc Kupietza350bce2016-02-25 09:34:25 +0100430 updateEmbedding();
431 }
432 }
Marc Kupietzc5990da2016-02-26 08:47:12 +0100433
Marc Kupietza350bce2016-02-25 09:34:25 +0100434 function showMap(j) {
435 data=j;
436 T.iter=0;
437 T.initDataRaw(data.vecs); // init embedding
438 drawEmbedding(); // draw initial embedding
439
440 if(iter_id >= 0) {
441 clearInterval(iter_id);
442 }
443 //T.debugGrad();
444 iter_id = setInterval(step, 1);
445 //step();
446 }
447
Marc Kupietzc5990da2016-02-26 08:47:12 +0100448 function update() {
449
450 text.transition().duration(800)
451 .attr("transform", transform);
452 }
453
454
Marc Kupietzc4893362016-02-25 08:04:46 +0100455</script>
456</head>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200457<body>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100458 <form action="<%=url_for('/')->to_abs%>" method="GET">
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100459 word(s):
460 <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.">
461 max. neighbours: <input type="text" size="8" name="n" value="<%= $no_nbs %>">
462 iterations: <input type="text" name="N" size="8" value="<%= $no_iterations %>">
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100463 <input type="submit" value="Show">
Marc Kupietz4aa62172016-02-25 10:39:27 +0100464 </form>
465 <br>
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100466 % if($lists) {
Marc Kupietz4aa62172016-02-25 10:39:27 +0100467 <div id="wrapper">
468 <table id="first">
469 <tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100470 <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 +0100471 </tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100472 % my $j=0; my @words; my @vecs; my @ranks; for my $list (@$lists) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100473 % my $i=1; for my $item (@$list) {
474 % if(!grep{$_ eq $item->{word}} @words) {
475 % push @vecs, $item->{vector};
476 % push @words, $item->{word};
Marc Kupietz5f780672016-02-25 17:15:54 +0100477 % push @ranks, $item->{rank};
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100478 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100479 <tr>
480 <td align="right">
481 <%= $i++ %>.
482 </td>
483 <td>
484 <a href="/?word=<%= $item->{word} %>">
485 <%= $item->{word} %>
486 </a>
487 </td>
488 <td align="right">
489 <%= sprintf("%.3f", $item->{dist}) %>
490 </td>
Marc Kupietz5f780672016-02-25 17:15:54 +0100491 <td align="right">
492 <%= $item->{rank} %>
493 </td>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100494 </tr>
495 % }
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100496 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100497 </table>
498 <script>
499 % use Mojo::ByteStream 'b';
500 $(window).load(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100501 showMap(<%= b(Mojo::JSON::to_json({target => " $word ", words => \@words, vecs => \@vecs, ranks => \@ranks})); %>);
Marc Kupietz4aa62172016-02-25 10:39:27 +0100502 });
503 </script>
504 % }
505 <div id="second" style="width:800px; height:800px; font-family: arial;">
506 <div id="embed">
507 </div>
508 <div id="cost"></div>
509 </div>
510 </div>
511 <p>
512 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 +0200513 <pre>
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100514-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 +0100515 </pre>
516 </p>
517</body>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200518</html>
519