word2vec-server: use mojo-template to print neighbours
diff --git a/w2v-server.pl b/w2v-server.pl
index 2767360..82e7b17 100644
--- a/w2v-server.pl
+++ b/w2v-server.pl
@@ -1,15 +1,18 @@
#!/usr/local/bin/perl
use Inline C;
use Mojolicious::Lite;
+use Encode qw(decode encode);
-init_net($ARGV[1]);
-
-helper print_neighbours => sub { shift; print_neighbours(@_) };
+init_net("vectors14.bin");
get '/' => sub {
my $c = shift;
my $word=$c->param('word');
- $c->render(template=>"index", word=>$word);
+ my $list;
+ if($word !~ /^\s*$/) {
+ $list = print_neighbours(encode("iso-8859-1", $word));
+ }
+ $c->render(template=>"index", word=>$word, list=> $list);
};
app->start;
@@ -77,7 +80,7 @@
return 0;
}
-char *print_neighbours(char *st1) {
+SV *print_neighbours(char *st1) {
FILE *out=stdout;
*stringBuffer=0;
@@ -110,8 +113,7 @@
break;
}
}
- if (b == -1) return stringBuffer;
- sprintf(stringBuffer+strlen(stringBuffer), "\n<table><tr><th>Word</th><th>Cosine distance</th></tr>\n");
+ if (b == -1) goto end;
for (a = 0; a < size; a++) vec[a] = 0;
for (b = 0; b < cn; b++) {
if (bi[b] == -1) continue;
@@ -141,9 +143,15 @@
}
}
}
- for (a = 0; a < N; a++) sprintf(stringBuffer+strlen(stringBuffer), "<tr><td>%s</td><td align=\"right\">%f</td></tr>\n", bestw[a], bestd[a]);
- sprintf(stringBuffer+strlen(stringBuffer), "</table>\n");
- return stringBuffer;
+ AV* array = newAV();
+ for (a = 0; a < N; a++) {
+ HV* hash = newHV();
+ hv_store(hash, "word", strlen("word"), newSVpvf(bestw[a], 0), 0);
+ hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
+ av_push(array, newRV_noinc((SV*)hash));
+ }
+ end:
+ return newRV_noinc((SV*)array);
}
__DATA__
@@ -151,14 +159,42 @@
@@ index.html.ep
<!DOCTYPE html>
<html>
-<head><title>word2vec</title></head>
+<head><title>DeReKo-Word-Vector-Distances</title></head>
<body>
+ <p>Word vector model based on a 1.9 billion word sample of DeReKo-2015-II (mainly wikipedia including discussions, current newspapapers and fiction). Trained with <a href="https://code.google.com/p/word2vec/">word2vec</a> using the following parameters:</p>
+ <pre>
+-cbow 1 -size 200 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 40 -binary 1 -iter 15
+</pre>
+</p>
<form action="<%=url_for('/')->to_abs%>" method="GET">
Word: <input type="text" name="word">
<input type="submit" value="Show neighbours">
</form>
<br>
- <%== print_neighbours($word) %>
+ % if($list) {
+ <p>Target word: <b><%= $word%></b></p>
+ <h3>Nearest neighbours</h3>
+ <table>
+ <tr>
+ <th align="right">Pos.</th><th align="left">Word</th><th align="right">Cosine dist.</th>
+ </tr>
+ % my $i=1; for my $item (@$list) {
+ <tr>
+ <td align="right">
+ <%= $i++ %>.
+ </td>
+ <td>
+ <a href="/?word=<%= $item->{word} %>">
+ <%= $item->{word} %>
+ </a>
+ </td>
+ <td align="right">
+ <%= sprintf("%.3f", $item->{dist}) %>
+ </td>
+ </tr>
+ % }
+ </table>
+ % }
</body>
</html>