Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 1 | #!/usr/local/bin/perl |
| 2 | use Inline C; |
| 3 | use Mojolicious::Lite; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 4 | use Mojo::JSON qw(decode_json encode_json to_json); |
Marc Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 5 | use Encode qw(decode encode); |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 6 | use Mojo::Server::Daemon; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 7 | |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 8 | # -cbow 1 -size 200 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 40 -binary 1 -iter 15 |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 9 | init_net("vectors14.bin"); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 10 | |
| 11 | get '/' => sub { |
| 12 | my $c = shift; |
| 13 | my $word=$c->param('word'); |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame^] | 14 | my $no_nbs=$c->param('n') || 100; |
Marc Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 15 | my $list; |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 16 | if(defined($word) && $word !~ /^\s*$/) { |
| 17 | $c->inactivity_timeout(300); |
| 18 | $c->app->log->debug('Looking for neighbours of '.$word); |
| 19 | $list = get_neighbours(encode("iso-8859-1", $word), $no_nbs); |
Marc Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 20 | } |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 21 | $c->render(template=>"index", word=>$word, no_nbs=>$no_nbs, list=> $list); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | app->start; |
| 25 | |
| 26 | exit; |
| 27 | |
| 28 | __END__ |
| 29 | |
| 30 | __C__ |
| 31 | #include <stdio.h> |
| 32 | #include <string.h> |
| 33 | #include <math.h> |
| 34 | #include <malloc.h> |
| 35 | #include <stdlib.h> //strlen |
| 36 | |
| 37 | #define max_size 2000 |
| 38 | #define max_w 50 |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 39 | #define MAX_NEIGHBOURS 1000 |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 40 | |
| 41 | //the thread function |
| 42 | void *connection_handler(void *); |
| 43 | |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 44 | char *bestw[MAX_NEIGHBOURS]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 45 | char file_name[max_size], st[100][max_size]; |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 46 | float dist, len, bestd[MAX_NEIGHBOURS], vec[max_size]; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 47 | long long words, size, a, b, c, d, cn, bi[100], besti[MAX_NEIGHBOURS]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 48 | char ch; |
| 49 | float *M; |
| 50 | char *vocab; |
| 51 | char *stringBuffer; |
| 52 | |
| 53 | int init_net(char *file_name) { |
| 54 | FILE *f; |
| 55 | |
| 56 | stringBuffer = malloc(64000); |
| 57 | f = fopen(file_name, "rb"); |
| 58 | if (f == NULL) { |
| 59 | printf("Input file %s not found\n", file_name); |
| 60 | return -1; |
| 61 | } |
| 62 | fscanf(f, "%lld", &words); |
| 63 | fscanf(f, "%lld", &size); |
| 64 | vocab = (char *)malloc((long long)words * max_w * sizeof(char)); |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 65 | for (a = 0; a < MAX_NEIGHBOURS; a++) bestw[a] = (char *)malloc(max_size * sizeof(char)); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 66 | M = (float *)malloc((long long)words * (long long)size * sizeof(float)); |
| 67 | if (M == NULL) { |
| 68 | printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size); |
| 69 | return -1; |
| 70 | } |
| 71 | for (b = 0; b < words; b++) { |
| 72 | a = 0; |
| 73 | while (1) { |
| 74 | vocab[b * max_w + a] = fgetc(f); |
| 75 | if (feof(f) || (vocab[b * max_w + a] == ' ')) break; |
| 76 | if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++; |
| 77 | } |
| 78 | vocab[b * max_w + a] = 0; |
Marc Kupietz | 30a4321 | 2016-02-25 08:12:00 +0100 | [diff] [blame] | 79 | fread(&M[b * size], sizeof(float), size, f); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 80 | len = 0; |
| 81 | for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size]; |
| 82 | len = sqrt(len); |
| 83 | for (a = 0; a < size; a++) M[a + b * size] /= len; |
| 84 | } |
| 85 | fclose(f); |
| 86 | return 0; |
| 87 | } |
| 88 | |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 89 | SV *get_neighbours(char *st1, int N) { |
| 90 | if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS; |
| 91 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 92 | FILE *out=stdout; |
| 93 | *stringBuffer=0; |
| 94 | |
| 95 | for (a = 0; a < N; a++) bestd[a] = 0; |
| 96 | for (a = 0; a < N; a++) bestw[a][0] = 0; |
| 97 | a = 0; |
| 98 | cn = 0; |
| 99 | b = 0; |
| 100 | c = 0; |
| 101 | while (1) { |
| 102 | st[cn][b] = st1[c]; |
| 103 | b++; |
| 104 | c++; |
| 105 | st[cn][b] = 0; |
| 106 | if (st1[c] == 0) break; |
| 107 | if (st1[c] == ' ') { |
| 108 | cn++; |
| 109 | b = 0; |
| 110 | c++; |
| 111 | } |
| 112 | } |
| 113 | cn++; |
| 114 | for (a = 0; a < cn; a++) { |
| 115 | for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break; |
| 116 | if (b == words) b = -1; |
| 117 | bi[a] = b; |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 118 | fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], bi[a]); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 119 | if (b == -1) { |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 120 | fprintf(stderr, "Out of dictionary word!\n"); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 121 | break; |
| 122 | } |
| 123 | } |
Marc Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 124 | if (b == -1) goto end; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 125 | for (a = 0; a < size; a++) vec[a] = 0; |
| 126 | for (b = 0; b < cn; b++) { |
| 127 | if (bi[b] == -1) continue; |
| 128 | for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size]; |
| 129 | } |
| 130 | len = 0; |
| 131 | for (a = 0; a < size; a++) len += vec[a] * vec[a]; |
| 132 | len = sqrt(len); |
| 133 | for (a = 0; a < size; a++) vec[a] /= len; |
| 134 | for (a = 0; a < N; a++) bestd[a] = -1; |
| 135 | for (a = 0; a < N; a++) bestw[a][0] = 0; |
| 136 | for (c = 0; c < words; c++) { |
| 137 | a = 0; |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 138 | // do not skip taget word |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 139 | // for (b = 0; b < cn; b++) if (bi[b] == c) a = 1; |
| 140 | // if (a == 1) continue; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 141 | dist = 0; |
| 142 | for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size]; |
| 143 | for (a = 0; a < N; a++) { |
| 144 | if (dist > bestd[a]) { |
| 145 | for (d = N - 1; d > a; d--) { |
| 146 | bestd[d] = bestd[d - 1]; |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 147 | besti[d] = besti[d - 1]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 148 | } |
| 149 | bestd[a] = dist; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 150 | besti[a] = c; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 151 | break; |
| 152 | } |
| 153 | } |
| 154 | } |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 155 | |
Marc Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 156 | AV* array = newAV(); |
| 157 | for (a = 0; a < N; a++) { |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 158 | strcpy(bestw[a], &vocab[besti[a] * max_w]); |
Marc Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 159 | HV* hash = newHV(); |
| 160 | hv_store(hash, "word", strlen("word"), newSVpvf(bestw[a], 0), 0); |
| 161 | hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0); |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 162 | AV *vector = newAV(); |
| 163 | for (b = 0; b < size; b++) { |
| 164 | av_push(vector, newSVnv(M[b + besti[a] * size])); |
| 165 | } |
| 166 | hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0); |
Marc Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 167 | av_push(array, newRV_noinc((SV*)hash)); |
| 168 | } |
| 169 | end: |
| 170 | return newRV_noinc((SV*)array); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 171 | } |
| 172 | |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 173 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 174 | __DATA__ |
| 175 | |
| 176 | @@ index.html.ep |
| 177 | <!DOCTYPE html> |
| 178 | <html> |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 179 | <head> |
| 180 | <title>DeReKo-Word-Vector-Distances</title> |
| 181 | <script src="http://code.jquery.com/jquery-latest.min.js"></script> |
| 182 | <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script> |
| 183 | <script src="http://klinux10/word2vec/tsne.js"></script> |
| 184 | <style> |
| 185 | svg { |
| 186 | // border: 1px solid #333; |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame^] | 187 | margin-right: 10px; |
| 188 | margin-bottom:10px; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 189 | } |
| 190 | #wrapper { |
| 191 | width: 100%; |
| 192 | // border: 1px solid red; |
| 193 | overflow: hidden; /* will contain if #first is longer than #second */ |
| 194 | } |
| 195 | #first { |
| 196 | width: 300px; |
| 197 | margin-right: 20px; |
| 198 | float:left; /* add this */ |
| 199 | // border: 1px solid green; |
| 200 | } |
| 201 | #second { |
| 202 | border: 1px solid #333; |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame^] | 203 | height: 850px; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 204 | overflow: hidden; /* if you don't want #second to wrap below #first */ |
| 205 | } |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame^] | 206 | #cost { |
| 207 | z-index: 1; |
| 208 | position: fixed; |
| 209 | font-size: 10px; |
| 210 | color: #222222; |
| 211 | margin-bottom: 10px; |
| 212 | } |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 213 | </style> |
| 214 | <script> |
| 215 | |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame^] | 216 | var opt = {epsilon: 1, perplexity: 20}; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 217 | var T = new tsnejs.tSNE(opt); // create a tSNE instance |
| 218 | |
| 219 | var Y; |
| 220 | |
| 221 | var data; |
| 222 | |
| 223 | function updateEmbedding() { |
| 224 | var Y = T.getSolution(); |
| 225 | svg.selectAll('.u') |
| 226 | .data(data.words) |
| 227 | .attr("transform", function(d, i) { return "translate(" + |
| 228 | ((Y[i][0]*20*ss + tx) + 400) + "," + |
| 229 | ((Y[i][1]*20*ss + ty) + 400) + ")"; }); |
| 230 | } |
| 231 | |
| 232 | var svg; |
| 233 | function drawEmbedding() { |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 234 | $("#embed").empty(); |
| 235 | var div = d3.select("#embed"); |
| 236 | |
| 237 | // get min and max in each column of Y |
| 238 | var Y = T.Y; |
| 239 | |
| 240 | svg = div.append("svg") // svg is global |
| 241 | .attr("width", 800) |
| 242 | .attr("height", 800); |
| 243 | |
| 244 | var g = svg.selectAll(".b") |
| 245 | .data(data.words) |
| 246 | .enter().append("g") |
| 247 | .attr("class", "u"); |
| 248 | |
| 249 | g.append("a") |
| 250 | .attr("xlink:href", function(word) {return "/?word="+word;}) |
| 251 | .append("text") |
| 252 | .attr("text-anchor", "top") |
| 253 | .attr("font-size", 12) |
| 254 | .attr("fill", function(d) { |
| 255 | if(d == data.target) { |
| 256 | return "red"; |
| 257 | } else { |
| 258 | return "#333" |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 259 | } |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 260 | }) |
| 261 | .text(function(d) { return d; }); |
| 262 | |
| 263 | var zoomListener = d3.behavior.zoom() |
| 264 | .scaleExtent([0.1, 10]) |
| 265 | .center([0,0]) |
| 266 | .on("zoom", zoomHandler); |
| 267 | zoomListener(svg); |
| 268 | } |
| 269 | |
| 270 | var tx=0, ty=0; |
| 271 | var ss=1; |
| 272 | var iter_id=-1; |
| 273 | |
| 274 | function zoomHandler() { |
| 275 | tx = d3.event.translate[0]; |
| 276 | ty = d3.event.translate[1]; |
| 277 | ss = d3.event.scale; |
| 278 | updateEmbedding(); |
| 279 | } |
| 280 | |
| 281 | var stepnum = 0; |
| 282 | |
| 283 | function stopStep() { |
| 284 | clearInterval(iter_id); |
| 285 | } |
| 286 | |
| 287 | function step() { |
| 288 | var i = T.iter; |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame^] | 289 | if(i > 2000) { |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 290 | stopStep(); |
| 291 | } else { |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame^] | 292 | var cost = Math.round(T.step() *1000) / 1000; // do a few steps |
| 293 | $("#cost").html("iteration " + i + ", cost: " + cost.toFixed(3)); |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 294 | updateEmbedding(); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | function showMap(j) { |
| 299 | data=j; |
| 300 | T.iter=0; |
| 301 | T.initDataRaw(data.vecs); // init embedding |
| 302 | drawEmbedding(); // draw initial embedding |
| 303 | |
| 304 | if(iter_id >= 0) { |
| 305 | clearInterval(iter_id); |
| 306 | } |
| 307 | //T.debugGrad(); |
| 308 | iter_id = setInterval(step, 1); |
| 309 | //step(); |
| 310 | } |
| 311 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 312 | </script> |
| 313 | </head> |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 314 | <body> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame^] | 315 | <form action="<%=url_for('/')->to_abs%>" method="GET"> |
| 316 | Word: <input type="text" name="word" value="<%= $word %>"> |
| 317 | Neighbours: <input type="text" name="n" value="<%= $no_nbs %>"> |
| 318 | <input type="submit" value="Show neighbours"> |
| 319 | </form> |
| 320 | <br> |
| 321 | % if($list) { |
| 322 | <div id="wrapper"> |
| 323 | <table id="first"> |
| 324 | <tr> |
| 325 | <th align="right">Pos.</th><th align="left">Word</th><th align="right">Cosine dist.</th> |
| 326 | </tr> |
| 327 | % my $i=1; my @words; my @vecs; for my $item (@$list) { |
| 328 | % push @vecs, $item->{vector}; |
| 329 | % push @words, $item->{word}; |
| 330 | <tr> |
| 331 | <td align="right"> |
| 332 | <%= $i++ %>. |
| 333 | </td> |
| 334 | <td> |
| 335 | <a href="/?word=<%= $item->{word} %>"> |
| 336 | <%= $item->{word} %> |
| 337 | </a> |
| 338 | </td> |
| 339 | <td align="right"> |
| 340 | <%= sprintf("%.3f", $item->{dist}) %> |
| 341 | </td> |
| 342 | </tr> |
| 343 | % } |
| 344 | </table> |
| 345 | <script> |
| 346 | % use Mojo::ByteStream 'b'; |
| 347 | $(window).load(function() { |
| 348 | showMap(<%= b(Mojo::JSON::to_json({target => $word, words => \@words, vecs => \@vecs})); %>); |
| 349 | }); |
| 350 | </script> |
| 351 | % } |
| 352 | <div id="second" style="width:800px; height:800px; font-family: arial;"> |
| 353 | <div id="embed"> |
| 354 | </div> |
| 355 | <div id="cost"></div> |
| 356 | </div> |
| 357 | </div> |
| 358 | <p> |
| 359 | 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 Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 360 | <pre> |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 361 | -cbow 1 -size 300 -window 7 -negative 5 -hs 0 -sample 1e-5 -threads 44 -binary 1 -iter 5 |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame^] | 362 | </pre> |
| 363 | </p> |
| 364 | </body> |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 365 | </html> |
| 366 | |