| #!/usr/local/bin/perl |
| use Inline C; |
| use Mojolicious::Lite; |
| use Mojo::JSON qw(decode_json encode_json to_json); |
| use Encode qw(decode encode); |
| use Mojo::Server::Daemon; |
| |
| print STDERR $ARGV[0]; |
| # -cbow 1 -size 200 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 40 -binary 1 -iter 15 |
| init_net("vectors15.bin"); |
| |
| get '/' => sub { |
| my $c = shift; |
| my $word=$c->param('word'); |
| my $no_nbs=$c->param('n') || 100; |
| my $no_iterations=$c->param('N') || 2000; |
| my @lists; |
| if(defined($word) && $word !~ /^\s*$/) { |
| $c->inactivity_timeout(300); |
| $word =~ s/\s+/ /g; |
| for my $w (split(' *\| *', $word)) { |
| $c->app->log->debug('Looking for neighbours of '.$w); |
| push(@lists, get_neighbours(encode("iso-8859-1", $w), $no_nbs)); |
| } |
| } |
| $c->render(template=>"index", word=>$word, no_nbs=>$no_nbs, no_iterations => $no_iterations, lists=> \@lists); |
| }; |
| |
| app->start; |
| |
| exit; |
| |
| __END__ |
| |
| __C__ |
| #include <stdio.h> |
| #include <string.h> |
| #include <math.h> |
| #include <malloc.h> |
| #include <stdlib.h> //strlen |
| |
| #define max_size 2000 |
| #define max_w 50 |
| #define MAX_NEIGHBOURS 1000 |
| #define MAX_WORDS -1 |
| |
| //the thread function |
| void *connection_handler(void *); |
| |
| char *bestw[MAX_NEIGHBOURS]; |
| char file_name[max_size], st[100][max_size]; |
| float dist, len, bestd[MAX_NEIGHBOURS], vec[max_size]; |
| long long words, size, a, b, c, d, cn, bi[100], besti[MAX_NEIGHBOURS]; |
| char ch; |
| float *M; |
| char *vocab; |
| char *stringBuffer; |
| |
| int init_net(char *file_name) { |
| FILE *f; |
| |
| stringBuffer = malloc(64000); |
| f = fopen(file_name, "rb"); |
| if (f == NULL) { |
| printf("Input file %s not found\n", file_name); |
| return -1; |
| } |
| fscanf(f, "%lld", &words); |
| if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS; |
| fscanf(f, "%lld", &size); |
| vocab = (char *)malloc((long long)words * max_w * sizeof(char)); |
| for (a = 0; a < MAX_NEIGHBOURS; a++) bestw[a] = (char *)malloc(max_size * sizeof(char)); |
| M = (float *)malloc((long long)words * (long long)size * sizeof(float)); |
| if (M == NULL) { |
| printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size); |
| return -1; |
| } |
| for (b = 0; b < words; b++) { |
| a = 0; |
| while (1) { |
| vocab[b * max_w + a] = fgetc(f); |
| if (feof(f) || (vocab[b * max_w + a] == ' ')) break; |
| if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++; |
| } |
| vocab[b * max_w + a] = 0; |
| fread(&M[b * size], sizeof(float), size, f); |
| len = 0; |
| for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size]; |
| len = sqrt(len); |
| for (a = 0; a < size; a++) M[a + b * size] /= len; |
| } |
| fclose(f); |
| return 0; |
| } |
| |
| SV *get_neighbours(char *st1, int N) { |
| if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS; |
| |
| FILE *out=stdout; |
| *stringBuffer=0; |
| |
| for (a = 0; a < N; a++) bestd[a] = 0; |
| for (a = 0; a < N; a++) bestw[a][0] = 0; |
| a = 0; |
| cn = 0; |
| b = 0; |
| c = 0; |
| while (1) { |
| st[cn][b] = st1[c]; |
| b++; |
| c++; |
| st[cn][b] = 0; |
| if (st1[c] == 0) break; |
| if (st1[c] == ' ') { |
| cn++; |
| b = 0; |
| c++; |
| } |
| } |
| cn++; |
| for (a = 0; a < cn; a++) { |
| for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break; |
| if (b == words) b = -1; |
| bi[a] = b; |
| fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], bi[a]); |
| if (b == -1) { |
| fprintf(stderr, "Out of dictionary word!\n"); |
| cn--; |
| break; |
| } |
| } |
| if (b == -1 && cn <= 0) { |
| N = 0; |
| goto end; |
| } |
| for (a = 0; a < size; a++) vec[a] = 0; |
| for (b = 0; b < cn; b++) { |
| if (bi[b] == -1) continue; |
| for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size]; |
| } |
| len = 0; |
| for (a = 0; a < size; a++) len += vec[a] * vec[a]; |
| len = sqrt(len); |
| for (a = 0; a < size; a++) vec[a] /= len; |
| for (a = 0; a < N; a++) bestd[a] = -1; |
| for (a = 0; a < N; a++) bestw[a][0] = 0; |
| for (c = 0; c < words; c++) { |
| a = 0; |
| // do not skip taget word |
| // for (b = 0; b < cn; b++) if (bi[b] == c) a = 1; |
| // if (a == 1) continue; |
| dist = 0; |
| for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size]; |
| for (a = 0; a < N; a++) { |
| if (dist > bestd[a]) { |
| for (d = N - 1; d > a; d--) { |
| bestd[d] = bestd[d - 1]; |
| besti[d] = besti[d - 1]; |
| } |
| bestd[a] = dist; |
| besti[a] = c; |
| break; |
| } |
| } |
| } |
| |
| end: |
| a=0; |
| AV* array = newAV(); |
| for (a = 0; a < N; a++) { |
| strcpy(bestw[a], &vocab[besti[a] * max_w]); |
| HV* hash = newHV(); |
| hv_store(hash, "word", strlen("word"), newSVpvf(bestw[a], 0), 0); |
| hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0); |
| hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0); |
| AV *vector = newAV(); |
| for (b = 0; b < size; b++) { |
| av_push(vector, newSVnv(M[b + besti[a] * size])); |
| } |
| hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0); |
| av_push(array, newRV_noinc((SV*)hash)); |
| } |
| return newRV_noinc((SV*)array); |
| } |
| |
| |
| __DATA__ |
| |
| @@ index.html.ep |
| <!DOCTYPE html> |
| <html> |
| <head> |
| <title>DeReKo-Word-Vector-Distances</title> |
| <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> |
| <script src="http://code.jquery.com/jquery-latest.min.js"></script> |
| <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> |
| <script> |
| $(function() { |
| $( document ).tooltip({ |
| content: function() { |
| return $(this).attr('title'); |
| }} |
| ) |
| }) |
| </script> |
| <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script> |
| <script src="http://klinux10/word2vec/tsne.js"></script> |
| <script src="http://klinux10/word2vec/labeler.js"></script> |
| <style> |
| body, input { |
| font-family: Arial, sans-serif; |
| font-size: 11pt; |
| } |
| |
| .ui-tooltip-content { |
| font-size: 9pt; |
| colour: #222222; |
| } |
| |
| svg > .ui-tooltip-content { |
| font-size: 7pt; |
| colour: #222222; |
| } |
| |
| svg { |
| // border: 1px solid #333; |
| margin-right: 10px; |
| margin-bottom:10px; |
| } |
| #wrapper { |
| width: 100%; |
| // border: 1px solid red; |
| overflow: hidden; /* will contain if #first is longer than #second */ |
| } |
| #first { |
| margin-right: 20px; |
| float:left; /* add this */ |
| // border: 1px solid green; |
| } |
| #second { |
| border: 1px solid #333; |
| height: 850px; |
| overflow: hidden; /* if you don't want #second to wrap below #first */ |
| } |
| #cost { |
| z-index: 1; |
| position: fixed; |
| font-size: 10px; |
| color: #222222; |
| margin-bottom: 10px; |
| } |
| </style> |
| <script> |
| |
| var opt = {epsilon: 1, perplexity: 20}; |
| var T = new tsnejs.tSNE(opt); // create a tSNE instance |
| |
| var Y; |
| |
| var data; |
| var labeler; |
| |
| |
| function applyJitter() { |
| svg.selectAll('.u') |
| .data(labels) |
| .transition() |
| .duration(50) |
| .attr("transform", function(d, i) { |
| T.Y[i][0] = (d.x - 400 - tx)/ss/20; |
| T.Y[i][1] = (d.y - 400 - ty)/ss/20; |
| return "translate(" + |
| (d.x) + "," + |
| (d.y) + ")"; |
| }); |
| } |
| |
| function updateEmbedding() { |
| var Y = T.getSolution(); |
| svg.selectAll('.u') |
| .data(data.words) |
| .attr("transform", function(d, i) { |
| return "translate(" + |
| ((Y[i][0]*20*ss + tx) + 400) + "," + |
| ((Y[i][1]*20*ss + ty) + 400) + ")"; }); |
| } |
| |
| var svg; |
| var labels = []; |
| var anchor_array = []; |
| var text; |
| |
| function drawEmbedding() { |
| $("#embed").empty(); |
| var div = d3.select("#embed"); |
| |
| // get min and max in each column of Y |
| var Y = T.Y; |
| |
| svg = div.append("svg") // svg is global |
| .attr("width", 800) |
| .attr("height", 800); |
| |
| var g = svg.selectAll(".b") |
| .data(data.words) |
| .enter().append("g") |
| .attr("class", "u"); |
| |
| g.append("a") |
| .attr("xlink:href", function(word) {return "/?word="+word;}) |
| .attr("title", function(d, i) { |
| return "rank: "+i +" "+"freq. rank: "+data.ranks[i]; |
| }) |
| .append("text") |
| .attr("text-anchor", "top") |
| .attr("font-size", 12) |
| .attr("fill", function(d) { |
| if(data.target.indexOf(" "+d+" ") >= 0) { |
| return "red"; |
| } else { |
| return "#333" |
| } |
| }) |
| .text(function(d) { return d; }); |
| |
| var zoomListener = d3.behavior.zoom() |
| .scaleExtent([0.1, 10]) |
| .center([0,0]) |
| .on("zoom", zoomHandler); |
| zoomListener(svg); |
| } |
| |
| var tx=0, ty=0; |
| var ss=1; |
| var iter_id=-1; |
| |
| function zoomHandler() { |
| tx = d3.event.translate[0]; |
| ty = d3.event.translate[1]; |
| ss = d3.event.scale; |
| updateEmbedding(); |
| } |
| |
| var stepnum = 0; |
| |
| function stopStep() { |
| clearInterval(iter_id); |
| text = svg.selectAll("text"); |
| |
| labels = d3.range(data.words.length).map(function(i) { |
| var x = (T.Y[i][0]*20*ss + tx) + 400; |
| var y = (T.Y[i][1]*20*ss + ty) + 400; |
| anchor_array.push({x: x, y: y, r: 5}); |
| return { |
| x: x, |
| y: y, |
| name: data.words[i] |
| }; |
| }); |
| |
| var index = 0; |
| text.each(function() { |
| labels[index].width = this.getBBox().width; |
| labels[index].height = this.getBBox().height; |
| index += 1; |
| }); |
| |
| |
| // setTimeout(updateEmbedding, 1); |
| // setTimeout( |
| labeler = d3.labeler() |
| .label(labels) |
| .anchor(anchor_array) |
| .width(800) |
| .height(800) |
| .update(applyJitter); |
| // .start(1000); |
| |
| iter_id = setInterval(jitterStep, 1); |
| |
| } |
| |
| var jitter_i=0;; |
| |
| function jitterStep() { |
| if(jitter_i++ > 100) { |
| clearInterval(iter_id); |
| } else { |
| labeler.start2(10); |
| applyJitter(); |
| } |
| } |
| |
| function step() { |
| var i = T.iter; |
| if(i > <%= $no_iterations %>) { |
| stopStep(); |
| } else { |
| var cost = Math.round(T.step() *1000) / 1000; // do a few steps |
| $("#cost").html("tsne iteration " + i + ", cost: " + cost.toFixed(3)); |
| updateEmbedding(); |
| } |
| } |
| |
| function showMap(j) { |
| data=j; |
| T.iter=0; |
| T.initDataRaw(data.vecs); // init embedding |
| drawEmbedding(); // draw initial embedding |
| |
| if(iter_id >= 0) { |
| clearInterval(iter_id); |
| } |
| //T.debugGrad(); |
| iter_id = setInterval(step, 1); |
| //step(); |
| } |
| |
| function update() { |
| |
| text.transition().duration(800) |
| .attr("transform", transform); |
| } |
| |
| |
| </script> |
| </head> |
| <body> |
| <form action="<%=url_for('/')->to_abs%>" method="GET"> |
| word(s): |
| <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."> |
| max. neighbours: <input type="text" size="8" name="n" value="<%= $no_nbs %>"> |
| iterations: <input type="text" name="N" size="8" value="<%= $no_iterations %>"> |
| <input type="submit" value="Show"> |
| </form> |
| <br> |
| % if($lists) { |
| <div id="wrapper"> |
| <table id="first"> |
| <tr> |
| <th align="right">Pos.</th><th align="left">Word</th><th align="right">Cosine dist.</th><th>Freq. rank</th> |
| </tr> |
| % my $j=0; my @words; my @vecs; my @ranks; for my $list (@$lists) { |
| % my $i=1; for my $item (@$list) { |
| % if(!grep{$_ eq $item->{word}} @words) { |
| % push @vecs, $item->{vector}; |
| % push @words, $item->{word}; |
| % push @ranks, $item->{rank}; |
| % } |
| <tr> |
| <td align="right"> |
| <%= $i++ %>. |
| </td> |
| <td> |
| <a href="/?word=<%= $item->{word} %>"> |
| <%= $item->{word} %> |
| </a> |
| </td> |
| <td align="right"> |
| <%= sprintf("%.3f", $item->{dist}) %> |
| </td> |
| <td align="right"> |
| <%= $item->{rank} %> |
| </td> |
| </tr> |
| % } |
| % } |
| </table> |
| <script> |
| % use Mojo::ByteStream 'b'; |
| $(window).load(function() { |
| showMap(<%= b(Mojo::JSON::to_json({target => " $word ", words => \@words, vecs => \@vecs, ranks => \@ranks})); %>); |
| }); |
| </script> |
| % } |
| <div id="second" style="width:800px; height:800px; font-family: arial;"> |
| <div id="embed"> |
| </div> |
| <div id="cost"></div> |
| </div> |
| </div> |
| <p> |
| 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> |
| <pre> |
| -cbow 1 -size 300 -window 7 -negative 5 -hs 0 -sample 1e-5 -threads 44 -binary 1 -iter 5 |
| </pre> |
| </p> |
| </body> |
| </html> |
| |