blob: a44df71c282ca5afbb630c28dd6081f9df50c068 [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 Kupietza5b90152016-03-15 17:39:19 +01006use Getopt::Std;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +01007use Mojo::Server::Daemon;
Marc Kupietzd4227392016-03-01 16:45:12 +01008plugin 'Log::Access';
Marc Kupietzdc22b982015-10-09 09:19:34 +02009
Marc Kupietza5b90152016-03-15 17:39:19 +010010our $opt_i = 0; # latin1-input?
11our $opt_l = undef;
12our $opt_p = 5676;
Marc Kupietz6b2975c2016-03-18 21:59:33 +010013our $opt_n = undef;
Marc Kupietza5b90152016-03-15 17:39:19 +010014
Marc Kupietz793413b2016-04-02 21:48:57 +020015my $training_args="";
16
Marc Kupietz6b2975c2016-03-18 21:59:33 +010017getopt('il:p:n:');
Marc Kupietza5b90152016-03-15 17:39:19 +010018
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010019# -cbow 1 -size 200 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 40 -binary 1 -iter 15
Marc Kupietza5b90152016-03-15 17:39:19 +010020if(!$ARGV[0]) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010021 init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0));
Marc Kupietz2cb667e2016-03-10 09:44:12 +010022} else {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010023 init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0));
Marc Kupietz793413b2016-04-02 21:48:57 +020024 if(open(FILE, "$ARGV[0].args")) {
25 $training_args = <FILE>;
26 }
27 close(FILE);
Marc Kupietz2cb667e2016-03-10 09:44:12 +010028}
Marc Kupietzdc22b982015-10-09 09:19:34 +020029
Marc Kupietza5b90152016-03-15 17:39:19 +010030my $daemon = Mojo::Server::Daemon->new(
31 app => app,
32 listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"]
33);
34
Marc Kupietzdc22b982015-10-09 09:19:34 +020035get '/' => sub {
36 my $c = shift;
37 my $word=$c->param('word');
Marc Kupietz44bee3c2016-02-25 16:26:29 +010038 my $no_nbs=$c->param('n') || 100;
39 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +010040 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +010041 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +010042 my $som=$c->param('som') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +010043 my $sort=$c->param('sort') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +010044 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010045 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +010046 my @collocations;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010047 if(defined($word) && $word !~ /^\s*$/) {
48 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +010049 $word =~ s/\s+/ /g;
50 for my $w (split(' *\| *', $word)) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010051 $c->app->log->debug('Looking for neighbours of '.$w);
Marc Kupietza5b90152016-03-15 17:39:19 +010052 if($opt_i) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +010053 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort);
Marc Kupietza5b90152016-03-15 17:39:19 +010054 } else {
Marc Kupietz6d9a6782016-03-23 17:25:25 +010055 $res = get_neighbours($w, $no_nbs, $sort);
Marc Kupietza5b90152016-03-15 17:39:19 +010056 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +010057 push(@lists, $res->{paradigmatic});
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010058 }
Marc Kupietz247500f2015-10-09 11:29:01 +020059 }
Marc Kupietz000ad862016-02-26 14:59:12 +010060 $word =~ s/ *\| */ | /g;
Marc Kupietzc47b3902016-04-22 10:29:44 +020061 $c->render(template=>"index", word=>$word, no_nbs=>$no_nbs, no_iterations => $no_iterations, epsilon=> $epsilon, perplexity=> $perplexity, show_som=>$som, sort=>$sort, training_args=>$training_args, lists=> \@lists, collocators=> $res->{syntagmatic});
Marc Kupietzdc22b982015-10-09 09:19:34 +020062};
63
Marc Kupietza5b90152016-03-15 17:39:19 +010064$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +020065
66exit;
67
68__END__
69
70__C__
71#include <stdio.h>
72#include <string.h>
73#include <math.h>
74#include <malloc.h>
75#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +010076#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +010077#include <pthread.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +020078
79#define max_size 2000
80#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010081#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +010082#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +010083#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +010084#define MAX_CC 50
85#define EXP_TABLE_SIZE 1000
86#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +010087#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +020088
89//the thread function
90void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +010091
92typedef struct {
93 long long *index;
94 float *dist;
Marc Kupietzb864ccf2016-03-21 22:40:03 +010095 float *norm;
Marc Kupietz6b2975c2016-03-18 21:59:33 +010096 long long *pos;
Marc Kupietz80abb442016-03-23 21:04:08 +010097 int length;
Marc Kupietz000ad862016-02-26 14:59:12 +010098} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +010099
Marc Kupietz000ad862016-02-26 14:59:12 +0100100typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +0100101 long long wordi[MAX_NEIGHBOURS];
102 char sep[MAX_NEIGHBOURS];
103 int length;
104} wordlist;
105
106typedef struct {
107 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100108 char *token;
109 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100110 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100111 unsigned long upto;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100112 float *target_sums;
Marc Kupietz000ad862016-02-26 14:59:12 +0100113} knnpars;
114
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100115float *M, *M2, *syn1neg_window, *expTable;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200116char *vocab;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100117
Marc Kupietz82b02672016-02-26 12:32:25 +0100118long long words, size;
Marc Kupietz000ad862016-02-26 14:59:12 +0100119int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100120int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100121int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200122
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100123int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100124 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100125 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100126 long long a, b, c, d, cn;
127 float len;
128
Marc Kupietz67c20282016-02-26 09:42:00 +0100129 char binvecs_fname[256], binwords_fname[256];
130 strcpy(binwords_fname, file_name);
131 strcat(binwords_fname, ".words");
132 strcpy(binvecs_fname, file_name);
133 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200134
Marc Kupietza5b90152016-03-15 17:39:19 +0100135 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200136 f = fopen(file_name, "rb");
137 if (f == NULL) {
138 printf("Input file %s not found\n", file_name);
139 return -1;
140 }
141 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100142 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200143 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100144 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
145 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100146 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
147 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
148 if (M == NULL) {
149 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
150 return -1;
151 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100152 for (b = 0; b < words; b++) {
153 a = 0;
154 while (1) {
155 vocab[b * max_w + a] = fgetc(f);
156 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
157 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
158 }
159 vocab[b * max_w + a] = 0;
160 fread(&M[b * size], sizeof(float), size, f);
161 len = 0;
162 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
163 len = sqrt(len);
164 for (a = 0; a < size; a++) M[a + b * size] /= len;
165 }
166 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
167 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
168 fclose(binvecs);
169 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
170 fclose(binwords);
171 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100172 }
173 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
174 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
175 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
176 if (M == MAP_FAILED || vocab == MAP_FAILED) {
177 close(binvecs_fd);
178 close(binwords_fd);
179 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
180 exit(-1);
181 }
182 } else {
183 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
184 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100185 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200186 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100187
188 if(net_name) {
189 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
190 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
191 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100192 // munmap(M, sizeof(float) * words * size);
193 M2 = mmap(0, sizeof(float) * words * size + sizeof(float) * 2 * window * size * words, PROT_READ, MAP_SHARED, net_fd, 0);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100194 if (M == MAP_FAILED) {
195 close(net_fd);
196 fprintf(stderr, "Cannot mmap %s\n", net_name);
197 exit(-1);
198 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100199 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100200 } else {
201 fprintf(stderr, "Cannot open %s\n", net_name);
202 exit(-1);
203 }
204 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
205 }
206
207 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
208 for (i = 0; i < EXP_TABLE_SIZE; i++) {
209 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
210 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
211 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200212 return 0;
213}
214
Marc Kupietz271e2a42016-03-22 11:37:43 +0100215void *getCollocators(knnpars *pars) {
216 int N = pars->N;
217 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100218 knn *nbs = NULL;
219 long window_layer_size = size * window * 2;
220 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
221 float f, max_f, maxmax_f;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100222 float *target_sums, *bestf, *bestn, worstbest, wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100223 long long *besti, *bestp;
Marc Kupietzd5642582016-03-19 22:23:13 +0100224
225 if(cc == -1)
226 return NULL;
227
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100228 a = posix_memalign((void **) &target_sums, 128, words * sizeof(float));
229 besti = malloc(N * sizeof(long long));
230 bestp = malloc(N * sizeof(long long));
231 bestf = malloc(N * sizeof(float));
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100232 bestn = malloc(N * sizeof(float));
233
Marc Kupietz271e2a42016-03-22 11:37:43 +0100234 worstbest = MIN_RESP;
235
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100236 for (b = 0; b < words; b++)
237 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100238 for (b = 0; b < N; b++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100239 besti[b] = -1;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100240 bestn[b] = 1;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100241 bestf[b] = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100242 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100243
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100244 d = cc;
245 maxmax_f = -1;
246 maxmax_target = 0;
247
Marc Kupietz271e2a42016-03-22 11:37:43 +0100248 for (a = pars->from; a < pars->upto; a++) {
249 if(a >= window)
250 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100251 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100252 printf("window pos: %ld\n", a);
253 if (a != window) {
254 max_f = -1;
255 window_offset = a * size;
256 if (a > window)
257 window_offset -= size;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100258 for(target = 0; target < words; target ++) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100259 if(target == d)
260 continue;
261 f = 0;
262 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100263 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100264 if (f < -MAX_EXP)
265 continue;
266 else if (f > MAX_EXP)
267 continue;
268 else
269 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100270 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100271
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100272 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100273 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100274 for (b = 0; b < N; b++) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100275 if (f > bestf[b]) {
Marc Kupietz33679a32016-03-22 08:49:39 +0100276 memmove(bestf + b + 1, bestf + b, (N - b -1) * sizeof(float));
277 memmove(besti + b + 1, besti + b, (N - b -1) * sizeof(long long));
278 memmove(bestp + b + 1, bestp + b, (N - b -1) * sizeof(long long));
279 bestf[b] = f;
280 besti[b] = target;
281 bestp[b] = window-a;
282 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100283 }
284 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100285 if(b == N - 1)
286 worstbest = bestf[N-1];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100287 }
288 }
289 printf("%d %.2f\n", max_target, max_f);
290 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
291 if(max_f > maxmax_f) {
292 maxmax_f = max_f;
293 maxmax_target = max_target;
294 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100295 for (b = 0; b < N; b++)
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100296 if(bestp[b] == window-a)
297 bestn[b] = bestf[b] / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100298 } else {
299 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
300 }
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100301
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100302 }
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100303 for (b = 0; b < words; b++)
304 pars->target_sums[b] += (target_sums[b] / wpos_sum ) / (window * 2);
305 free(target_sums);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100306 for(b=0; b<N && besti[b] >= 0; b++) // THIS LOOP IS NEEDED (b...)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100307 printf("%s %.2f %d * ", &vocab[besti[b]*max_w], bestf[b], bestp[b]);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100308 printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100309 nbs = malloc(sizeof(knn));
310 nbs->index = besti;
311 nbs->dist = bestf;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100312 nbs->norm = bestn;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100313 nbs->pos = bestp;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100314 nbs->length = b-1;
315 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100316}
317
Marc Kupietz48c29682016-03-19 11:30:43 +0100318wordlist *getTargetWords(char *st1) {
319 wordlist *wl = malloc(sizeof(wordlist));
320 char st[100][max_size], sep[100];
321 long a, b=0, c=0, cn=0;
322
Marc Kupietzdc22b982015-10-09 09:19:34 +0200323 while (1) {
324 st[cn][b] = st1[c];
325 b++;
326 c++;
327 st[cn][b] = 0;
328 if (st1[c] == 0) break;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100329 if (st1[c] == ' ' || st1[c] == '-') {
330 sep[cn++] = st1[c];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200331 b = 0;
332 c++;
333 }
334 }
335 cn++;
336 for (a = 0; a < cn; a++) {
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100337 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
338 if (b == words) b = -1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100339 wl->wordi[a] = b;
340 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], wl->wordi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200341 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100342 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100343 cn--;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200344 break;
345 }
346 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100347 wl->length=cn;
348 return(wl);
349}
350
351void *_get_neighbours(knnpars *pars) {
352 char *st1 = pars->token;
353 int N = pars->N;
354 long from = pars -> from;
355 unsigned long upto = pars -> upto;
356 char file_name[max_size], st[100][max_size], *sep;
357 float dist, len, *bestd, vec[max_size];
358 long long a, b, c, d, cn, *bi, *besti;
359 char ch;
360 knn *nbs = NULL;
361 wordlist *wl = pars->wl;
362
363 besti = malloc(N * sizeof(long long));
364 bestd = malloc(N * sizeof(float));
365
366 float worstbest=-1;
367
368 for (a = 0; a < N; a++) bestd[a] = 0;
369 a = 0;
370 bi = wl->wordi;
371 cn = wl->length;
372 sep = wl->sep;
373 b = bi[0];
374 c = 0;
375
Marc Kupietz000ad862016-02-26 14:59:12 +0100376 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100377 N = 0;
378 goto end;
379 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200380 for (a = 0; a < size; a++) vec[a] = 0;
381 for (b = 0; b < cn; b++) {
382 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100383 if(b>0 && sep[b-1] == '-')
384 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
385 else
386 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200387 }
388 len = 0;
389 for (a = 0; a < size; a++) len += vec[a] * vec[a];
390 len = sqrt(len);
391 for (a = 0; a < size; a++) vec[a] /= len;
392 for (a = 0; a < N; a++) bestd[a] = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100393 for (c = from; c < upto; c++) {
Marc Kupietzdc22b982015-10-09 09:19:34 +0200394 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100395// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100396// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
397// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200398 dist = 0;
399 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100400 if(dist > worstbest) {
401 for (a = 0; a < N; a++) {
402 if (dist > bestd[a]) {
Marc Kupietz33679a32016-03-22 08:49:39 +0100403 memmove(bestd + a + 1, bestd + a, (N - a -1) * sizeof(float));
404 memmove(besti + a + 1, besti + a, (N - a -1) * sizeof(long long));
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100405 bestd[a] = dist;
406 besti[a] = c;
407 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200408 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200409 }
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100410 worstbest = bestd[N-1];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200411 }
412 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100413
Marc Kupietz000ad862016-02-26 14:59:12 +0100414 nbs = malloc(sizeof(knn));
415 nbs->index = besti;
416 nbs->dist = bestd;
417 nbs->length = N;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100418end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100419 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200420}
421
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100422
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100423SV *get_neighbours(char *st1, int N, int sort_by) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100424 HV *result = newHV();
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100425 float *target_sums, bestd[MAX_NEIGHBOURS], bestn[MAX_NEIGHBOURS], bests[MAX_NEIGHBOURS], vec[max_size];
Marc Kupietz50485ba2016-03-23 09:13:14 +0100426 long besti[MAX_NEIGHBOURS], bestp[MAX_NEIGHBOURS], a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100427 knn *para_nbs[MAX_THREADS];
428 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100429 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100430 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100431 wordlist *wl;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100432 int para_threads = num_threads - window * 2;
433 int syn_threads = window * 2;
434 num_threads = para_threads+syn_threads;
Marc Kupietz48c29682016-03-19 11:30:43 +0100435
Marc Kupietz000ad862016-02-26 14:59:12 +0100436 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
437
Marc Kupietz271e2a42016-03-22 11:37:43 +0100438 slice = words / syn_threads;
Marc Kupietz000ad862016-02-26 14:59:12 +0100439
Marc Kupietz48c29682016-03-19 11:30:43 +0100440 wl = getTargetWords(st1);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100441 if(wl->length < 1)
442 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100443
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100444 a = posix_memalign((void **) &target_sums, 128, words * sizeof(float));
445 for(a = 0; a < words; a++)
446 target_sums[a] = 0;
447
Marc Kupietz271e2a42016-03-22 11:37:43 +0100448 for(a=0; a < para_threads; a++) {
Marc Kupietz000ad862016-02-26 14:59:12 +0100449 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100450 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100451 pars[a].N = N;
452 pars[a].from = a*slice;
453 pars[a].upto = ((a+1)*slice > words? words:(a+1)*slice);
454 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
455 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100456 for(a=0; a < syn_threads; a++) {
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100457 pars[a + para_threads].target_sums = target_sums;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100458 pars[a + para_threads].wl = wl;
459 pars[a + para_threads].N = N;
460 pars[a + para_threads].from = a;
461 pars[a + para_threads].upto = a+1;
462 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
463 }
464 printf("Waiting for para threads to join\n");
465 fflush(stdout);
466 for (a = 0; a < para_threads; a++) pthread_join(pt[a], &para_nbs[a]);
467 printf("Para threads joint\n");
468 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +0100469
Marc Kupietz271e2a42016-03-22 11:37:43 +0100470 if(!syn_nbs[0])
Marc Kupietz000ad862016-02-26 14:59:12 +0100471 goto end;
472
473 for(b=0; b < N; b++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100474 besti[b] = para_nbs[0]->index[b];
475 bestd[b] = para_nbs[0]->dist[b];
Marc Kupietz000ad862016-02-26 14:59:12 +0100476 }
477
Marc Kupietz271e2a42016-03-22 11:37:43 +0100478 for(a=1; a < para_threads; a++) {
479 for(b=0; b < para_nbs[a]->length && para_nbs[a]->index[b] >= 0; b++) {
Marc Kupietz000ad862016-02-26 14:59:12 +0100480 for(c=0; c < N; c++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100481 if(para_nbs[a]->dist[b] > bestd[c]) {
Marc Kupietz000ad862016-02-26 14:59:12 +0100482 for(d=N-1; d>c; d--) {
483 bestd[d] = bestd[d-1];
484 besti[d] = besti[d-1];
485 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100486 besti[c] = para_nbs[a]->index[b];
487 bestd[c] = para_nbs[a]->dist[b];
Marc Kupietz000ad862016-02-26 14:59:12 +0100488 break;
489 }
490 }
491 }
492 }
493
Marc Kupietz271e2a42016-03-22 11:37:43 +0100494 AV* array = newAV();
495 for (a = 0; a < N; a++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100496 HV* hash = newHV();
Marc Kupietz50485ba2016-03-23 09:13:14 +0100497 SV* word = newSVpvf(&vocab[besti[a] * max_w], 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100498 if(latin_enc == 0) SvUTF8_on(word);
499 hv_store(hash, "word", strlen("word"), word , 0);
500 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
501 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
502 AV *vector = newAV();
503 for (b = 0; b < size; b++) {
504 av_push(vector, newSVnv(M[b + besti[a] * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100505 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100506 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
507 av_push(array, newRV_noinc((SV*)hash));
508 }
509 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
510
Marc Kupietz50485ba2016-03-23 09:13:14 +0100511 for(b=0; b < MAX_NEIGHBOURS; b++) {
512 besti[b] = -1L;
513 bestd[b] = 0;
514 bestn[b] = 0;
515 bestp[b] = 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100516 bests[b] = 0;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100517 }
518
Marc Kupietz271e2a42016-03-22 11:37:43 +0100519 printf("Waiting for syn threads to join\n");
520 fflush(stdout);
521 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], &syn_nbs[a]);
522 printf("syn threads joint\n");
523 fflush(stdout);
524
Marc Kupietz50485ba2016-03-23 09:13:14 +0100525
526 for(b=0; b < syn_nbs[0]->length; b++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100527 besti[b] = syn_nbs[0]->index[b];
528 bestd[b] = syn_nbs[0]->dist[b];
529 bestn[b] = syn_nbs[0]->norm[b];
530 bestp[b] = syn_nbs[0]->pos[b];
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100531 bests[b] = target_sums[syn_nbs[0]->index[b]];
Marc Kupietz271e2a42016-03-22 11:37:43 +0100532 }
533
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100534 if(sort_by != 1) { // sort by responsiveness
535 for(a=1; a < syn_threads; a++) {
536 for(b=0; b < syn_nbs[a]->length; b++) {
537 for(c=0; c < MAX_NEIGHBOURS; c++) {
538 if(syn_nbs[a]->dist[b] > bestd[c]) {
539 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
540 bestd[d] = bestd[d-1];
541 besti[d] = besti[d-1];
542 bestn[d] = bestn[d-1];
543 bestp[d] = bestp[d-1];
544 }
545 besti[c] = syn_nbs[a]->index[b];
546 bestd[c] = syn_nbs[a]->dist[b];
547 bestn[c] = syn_nbs[a]->norm[b];
548 bestp[c] = syn_nbs[a]->pos[b];
549 break;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100550 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100551 }
552 }
553 }
554 } else { // sort by mean p
555 for(a=1; a < syn_threads; a++) {
556 for(b=0; b < syn_nbs[a]->length; b++) {
557 for(c=0; c < MAX_NEIGHBOURS; c++) {
558 if(target_sums[syn_nbs[a]->index[b]] > bests[c]) {
559 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
560 bestd[d] = bestd[d-1];
561 besti[d] = besti[d-1];
562 bestn[d] = bestn[d-1];
563 bestp[d] = bestp[d-1];
564 bests[d] = bests[d-1];
565 }
566 besti[c] = syn_nbs[a]->index[b];
567 bestd[c] = syn_nbs[a]->dist[b];
568 bestn[c] = syn_nbs[a]->norm[b];
569 bestp[c] = syn_nbs[a]->pos[b];
570 bests[c] = target_sums[syn_nbs[a]->index[b]];
571 break;
572 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100573 }
574 }
575 }
576 }
577 array = newAV();
Marc Kupietz50485ba2016-03-23 09:13:14 +0100578 for (a = 0; a < MAX_NEIGHBOURS && besti[a] >= 0; a++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100579 HV* hash = newHV();
Marc Kupietz50485ba2016-03-23 09:13:14 +0100580 SV* word = newSVpvf(&vocab[besti[a] * max_w], 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100581 if(latin_enc == 0) SvUTF8_on(word);
582 hv_store(hash, "word", strlen("word"), word , 0);
583 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
584 hv_store(hash, "norm", strlen("norm"), newSVnv(bestn[a]), 0);
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100585 hv_store(hash, "sum", strlen("sum"), newSVnv(target_sums[besti[a]]), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100586 hv_store(hash, "pos", strlen("pos"), newSVnv(bestp[a]), 0);
587 av_push(array, newRV_noinc((SV*)hash));
588 }
589 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz000ad862016-02-26 14:59:12 +0100590end:
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100591 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +0100592}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100593
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100594
Marc Kupietzdc22b982015-10-09 09:19:34 +0200595__DATA__
596
597@@ index.html.ep
598<!DOCTYPE html>
599<html>
Marc Kupietzc4893362016-02-25 08:04:46 +0100600<head>
601 <title>DeReKo-Word-Vector-Distances</title>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100602 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
Marc Kupietzc4893362016-02-25 08:04:46 +0100603 <script src="http://code.jquery.com/jquery-latest.min.js"></script>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100604 <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
605 <script>
606 $(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100607 $( document ).tooltip({
608 content: function() {
609 return $(this).attr('title');
610 }}
611 )
612 })
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100613 </script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100614 <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
615 <script src="http://klinux10/word2vec/tsne.js"></script>
Marc Kupietzd7aea722016-03-02 11:59:12 +0100616 <script src="http://klinux10/word2vec/som.js"></script>
Marc Kupietzc5990da2016-02-26 08:47:12 +0100617 <script src="http://klinux10/word2vec/labeler.js"></script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100618<style>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100619body, input {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100620 font-family: Arial, sans-serif;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100621 font-size: 11pt;
622}
623
624.ui-tooltip-content {
625 font-size: 9pt;
626 colour: #222222;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100627}
Marc Kupietz5f780672016-02-25 17:15:54 +0100628
629svg > .ui-tooltip-content {
Marc Kupietzb1029362016-02-27 21:38:55 +0100630 font-size: 8pt;
Marc Kupietz5f780672016-02-25 17:15:54 +0100631 colour: #222222;
632}
633
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100634#collocators {
635 margin-bottom: 15px;
636}
637
Marc Kupietzc4893362016-02-25 08:04:46 +0100638#wrapper {
639 width: 100%;
640// border: 1px solid red;
641 overflow: hidden; /* will contain if #first is longer than #second */
642}
643#first {
Marc Kupietzb1029362016-02-27 21:38:55 +0100644 margin-right: 20px;
645 float: left;
646 // border: 1px solid green;
Marc Kupietzc4893362016-02-25 08:04:46 +0100647}
648#second {
649 border: 1px solid #333;
650 overflow: hidden; /* if you don't want #second to wrap below #first */
651}
Marc Kupietzd7aea722016-03-02 11:59:12 +0100652#som2 svg {
653 border: 1px solid #333;
654}
655
Marc Kupietz4aa62172016-02-25 10:39:27 +0100656#cost {
Marc Kupietzb1029362016-02-27 21:38:55 +0100657 font-size: 8pt;
658 color: #222222;
659 margin-top: 4px;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100660 margin-bottom: 12px;
Marc Kupietz4aa62172016-02-25 10:39:27 +0100661}
Marc Kupietzd7aea722016-03-02 11:59:12 +0100662
Marc Kupietz6c1ca442016-03-03 09:35:18 +0100663#sominfo1, #sominfo {
Marc Kupietzd7aea722016-03-02 11:59:12 +0100664 font-size: 8pt;
665 color: #222222;
666 margin-top: 0px;
667}
668
Marc Kupietz6c1ca442016-03-03 09:35:18 +0100669#somcolor1, #somcolor2, #somcolor3 {
670 display: inline-block;
671 height: 10px;
672 width: 10px;
673}
674
Marc Kupietzd7aea722016-03-02 11:59:12 +0100675#third {
676 border: 1px solid #333;
677}
678
Marc Kupietzc4893362016-02-25 08:04:46 +0100679</style>
680<script>
681
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100682var opt = {epsilon: <%= $epsilon %>, perplexity: <%= $perplexity %>},
Marc Kupietz9fca1732016-02-29 09:07:04 +0100683 mapWidth = 800, // width map
684 mapHeight = 800,
685 jitterRadius = 7;
686
Marc Kupietzc4893362016-02-25 08:04:46 +0100687var T = new tsnejs.tSNE(opt); // create a tSNE instance
688
689var Y;
690
691var data;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100692var labeler;
Marc Kupietzc4893362016-02-25 08:04:46 +0100693
Marc Kupietzc5990da2016-02-26 08:47:12 +0100694
695function applyJitter() {
Marc Kupietzd7aea722016-03-02 11:59:12 +0100696 svg.selectAll('.tsnet')
Marc Kupietzc5990da2016-02-26 08:47:12 +0100697 .data(labels)
698 .transition()
699 .duration(50)
700 .attr("transform", function(d, i) {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100701 T.Y[i][0] = (d.x - mapWidth/2 - tx)/ss/20;
702 T.Y[i][1] = (d.y - mapHeight/2 - ty)/ss/20;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100703 return "translate(" +
704 (d.x) + "," +
705 (d.y) + ")";
706 });
707}
708
Marc Kupietzc4893362016-02-25 08:04:46 +0100709function updateEmbedding() {
710 var Y = T.getSolution();
Marc Kupietzd7aea722016-03-02 11:59:12 +0100711 svg.selectAll('.tsnet')
Marc Kupietz9fca1732016-02-29 09:07:04 +0100712 .data(data.words)
713 .attr("transform", function(d, i) {
714 return "translate(" +
715 ((Y[i][0]*20*ss + tx) + mapWidth/2) + "," +
716 ((Y[i][1]*20*ss + ty) + mapHeight/2) + ")"; });
Marc Kupietzc4893362016-02-25 08:04:46 +0100717}
718
719var svg;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100720var labels = [];
721var anchor_array = [];
722var text;
723
Marc Kupietzc4893362016-02-25 08:04:46 +0100724function drawEmbedding() {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100725 $("#embed").empty();
726 var div = d3.select("#embed");
727
728 // get min and max in each column of Y
729 var Y = T.Y;
730
731 svg = div.append("svg") // svg is global
732 .attr("width", mapWidth)
733 .attr("height", mapHeight);
734
735 var g = svg.selectAll(".b")
736 .data(data.words)
737 .enter().append("g")
Marc Kupietzd7aea722016-03-02 11:59:12 +0100738 .attr("class", "tsnet");
Marc Kupietz9fca1732016-02-29 09:07:04 +0100739
740 g.append("a")
741 .attr("xlink:href", function(word) {return "/?word="+word;})
742 .attr("title", function(d, i) {
Marc Kupietze50c8162016-03-01 10:24:43 +0100743 return "rank: "+i +" "+"freq. rank: "+data.ranks[i].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
Marc Kupietz9fca1732016-02-29 09:07:04 +0100744 })
Marc Kupietza350bce2016-02-25 09:34:25 +0100745 .append("text")
Marc Kupietz9fca1732016-02-29 09:07:04 +0100746 .attr("text-anchor", "top")
747 .attr("font-size", 12)
748 .attr("fill", function(d) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100749 if(data.target.indexOf(" "+d+" ") >= 0) {
Marc Kupietza350bce2016-02-25 09:34:25 +0100750 return "red";
751 } else {
752 return "#333"
Marc Kupietzc4893362016-02-25 08:04:46 +0100753 }
Marc Kupietz9fca1732016-02-29 09:07:04 +0100754 })
755 .text(function(d) { return d; });
756
757 var zoomListener = d3.behavior.zoom()
758 .scaleExtent([0.1, 10])
759 .center([0,0])
760 .on("zoom", zoomHandler);
761 zoomListener(svg);
762}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100763
Marc Kupietz9fca1732016-02-29 09:07:04 +0100764var tx=0, ty=0;
765var ss=1;
766var iter_id=-1;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100767
Marc Kupietz9fca1732016-02-29 09:07:04 +0100768function zoomHandler() {
769 tx = d3.event.translate[0];
770 ty = d3.event.translate[1];
771 ss = d3.event.scale;
772 updateEmbedding();
773}
774
775var stepnum = 0;
776
777function stopStep() {
778 clearInterval(iter_id);
779 text = svg.selectAll("text");
780
781 // jitter function needs different data and co-ordinate representation
782 labels = d3.range(data.words.length).map(function(i) {
783 var x = (T.Y[i][0]*20*ss + tx) + mapWidth/2;
784 var y = (T.Y[i][1]*20*ss + ty) + mapHeight/2;
785 anchor_array.push({x: x, y: y, r: jitterRadius});
786 return {
787 x: x,
788 y: y,
789 name: data.words[i]
790 };
791 });
792
793 // get the actual label bounding boxes for the jitter function
794 var index = 0;
795 text.each(function() {
796 labels[index].width = this.getBBox().width;
797 labels[index].height = this.getBBox().height;
798 index += 1;
799 });
Marc Kupietzc5990da2016-02-26 08:47:12 +0100800
801
802// setTimeout(updateEmbedding, 1);
803// setTimeout(
Marc Kupietz9fca1732016-02-29 09:07:04 +0100804 labeler = d3.labeler()
805 .label(labels)
806 .anchor(anchor_array)
807 .width(mapWidth)
808 .height(mapHeight)
809 .update(applyJitter);
810 // .start(1000);
Marc Kupietzc5990da2016-02-26 08:47:12 +0100811
Marc Kupietz9fca1732016-02-29 09:07:04 +0100812 iter_id = setInterval(jitterStep, 1);
813}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100814
Marc Kupietz9fca1732016-02-29 09:07:04 +0100815var jitter_i=0;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100816
817function jitterStep() {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100818 if(jitter_i++ > 100) {
819 clearInterval(iter_id);
820 } else {
821 labeler.start2(10);
822 applyJitter();
823 }
Marc Kupietzc5990da2016-02-26 08:47:12 +0100824}
Marc Kupietzb1029362016-02-27 21:38:55 +0100825
826var last_cost=1000;
827
Marc Kupietz9fca1732016-02-29 09:07:04 +0100828function step() {
829 var i = T.iter;
830
831 if(i > <%= $no_iterations %>) {
832 stopStep();
833 } else {
834 var cost = Math.round(T.step() * 100000) / 100000; // do a few steps
835 $("#cost").html("tsne iteration " + i + ", cost: " + cost.toFixed(5));
836 if(i % 250 == 0 && cost >= last_cost) {
837 stopStep();
838 } else {
839 last_cost = cost;
840 updateEmbedding();
841 }
842 }
843}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100844
Marc Kupietz9fca1732016-02-29 09:07:04 +0100845function showMap(j) {
846 data=j;
847 T.iter=0;
848 T.initDataRaw(data.vecs); // init embedding
849 drawEmbedding(); // draw initial embedding
850
851 if(iter_id >= 0) {
852 clearInterval(iter_id);
853 }
854 //T.debugGrad();
855 iter_id = setInterval(step, 1);
Marc Kupietzd7aea722016-03-02 11:59:12 +0100856 if(<%= $show_som %>) {
857 makeSOM(j, <%= $no_iterations %>);
858 }
Marc Kupietz9fca1732016-02-29 09:07:04 +0100859}
Marc Kupietza350bce2016-02-25 09:34:25 +0100860
Marc Kupietzc4893362016-02-25 08:04:46 +0100861</script>
862</head>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200863<body>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100864 <form action="<%=url_for('/')->to_abs%>" method="GET">
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100865 word(s):
866 <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.">
867 max. neighbours: <input type="text" size="8" name="n" value="<%= $no_nbs %>">
Marc Kupietzd7aea722016-03-02 11:59:12 +0100868 max. iterations: <input type="text" name="N" size="8" value="<%= $no_iterations %>">
869 SOM <input type="checkbox" name="som" value="1" <%= ($show_som ? "checked" : "") %>>
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100870 <span> </span>sort collocators by
871 <select name="sort">
872 <option value="0" <%= ($sort!=1? "selected":"") %>>responsiveness</option>
873 <option value="1" <%= ($sort==1? "selected":"") %>>mean p</option>
874 </select>
Marc Kupietzd7aea722016-03-02 11:59:12 +0100875 <span> </span><input type="submit" value="Show">
Marc Kupietz4aa62172016-02-25 10:39:27 +0100876 </form>
877 <br>
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100878 % if($lists) {
Marc Kupietz4aa62172016-02-25 10:39:27 +0100879 <div id="wrapper">
880 <table id="first">
881 <tr>
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100882 <th align="right">#</th><th align="right">cos</th><th align="left">paradigmatic</th><th title="Position in winodw around target word. Absolute value can be too low because of sub-sampling frequent words.">@</th><th align="right" title="&#34;Responsivenes&#34; of the collocator at the relative position @. Approximation of the probability that the combination of the target word and the collocator at the relative position @ come from the corpus.">resp.</th><th title="Probability of the collocator at window location @."align="right">p(c<sub><small>@</small></sub>)</th><th align="right"p(c<sub><small>@</small></sub>)/|w|</th><th align="left">syntagmatic</th>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100883 </tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100884 % my $j=0; my @words; my @vecs; my @ranks; for my $list (@$lists) {
Marc Kupietzc47b3902016-04-22 10:29:44 +0200885 % my $i=0; while($list) {
Marc Kupietz50485ba2016-03-23 09:13:14 +0100886 % my $item = (@$list)[$i];
887 % my $c = (@$collocators)[$i];
888 % last if(!$c && !$item);
Marc Kupietz4aa62172016-02-25 10:39:27 +0100889 <tr>
890 <td align="right">
Marc Kupietzd5642582016-03-19 22:23:13 +0100891 <%= ++$i %>.
Marc Kupietz4aa62172016-02-25 10:39:27 +0100892 </td>
Marc Kupietz50485ba2016-03-23 09:13:14 +0100893 % if($item) {
894 % if(!grep{$_ eq $item->{word}} @words) {
895 % push @vecs, $item->{vector};
896 % push @words, $item->{word};
897 % push @ranks, $item->{rank};
898 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100899 <td align="right">
900 <%= sprintf("%.3f", $item->{dist}) %>
901 </td>
Marc Kupietzd5642582016-03-19 22:23:13 +0100902 <td>
903 <a title="freq. rank: <%= $item->{rank} %>" href="/?word=<%= $item->{word} %>">
904 <%= $item->{word} %>
905 </a>
906 </td>
Marc Kupietz50485ba2016-03-23 09:13:14 +0100907 % } else {
908 <td colspan="2"/>
909 % }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100910 % if($c) {
Marc Kupietz5f780672016-02-25 17:15:54 +0100911 <td align="right">
Marc Kupietzd5642582016-03-19 22:23:13 +0100912 <%= $c->{pos} %>:
913 </td>
914 <td align="right">
915 <%= sprintf("%.3f", $c->{dist}) %>
916 </td>
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100917 <td align="right">
918 <%= sprintf("%.3e", $c->{norm}) %>
919 </td>
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100920 <td align="right">
921 <%= sprintf("%.3e", $c->{sum}) %>
922 </td>
Marc Kupietzd5642582016-03-19 22:23:13 +0100923 <td align="left">
924 <a href="/?word=<%= $c->{word} %>">
925 <%= $c->{word} %>
Marc Kupietz5f780672016-02-25 17:15:54 +0100926 </td>
Marc Kupietz271e2a42016-03-22 11:37:43 +0100927 % } else {
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100928 <td colspan="5"/>
Marc Kupietz271e2a42016-03-22 11:37:43 +0100929 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100930 </tr>
931 % }
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100932 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100933 </table>
934 <script>
935 % use Mojo::ByteStream 'b';
936 $(window).load(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100937 showMap(<%= b(Mojo::JSON::to_json({target => " $word ", words => \@words, vecs => \@vecs, ranks => \@ranks})); %>);
Marc Kupietz4aa62172016-02-25 10:39:27 +0100938 });
939 </script>
940 % }
941 <div id="second" style="width:800px; height:800px; font-family: arial;">
942 <div id="embed">
943 </div>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100944 </div>
Marc Kupietzb1029362016-02-27 21:38:55 +0100945 <div id="cost"></div>
Marc Kupietzd7aea722016-03-02 11:59:12 +0100946 % if($show_som) {
947 <div id="som2">
948 </div>
Marc Kupietz6c1ca442016-03-03 09:35:18 +0100949 <div id="sominfo1"><span id="somcolor1"> </span> <span id="somword1"> </span> <span id="somcolor2"> </span> <span id="somword2"> </span> <span id="somcolor3"> </span></div>
Marc Kupietzd7aea722016-03-02 11:59:12 +0100950 <div id="sominfo">SOM iteration <span id="iterations">0</span></div>
951 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100952 </div>
Marc Kupietz793413b2016-04-02 21:48:57 +0200953 % if($training_args) {
954 <p>
955 Word vector model trained with <a href="https://code.google.com/p/word2vec/">word2vec</a> using the following parameters: <pre><%= $training_args %></pre>
956 </p>
957 % }
958 </body>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200959</html>
960