blob: e7ad7f5973ce97d1afdb36f569b58935e6b44b81 [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 Kupietz6b2975c2016-03-18 21:59:33 +010015getopt('il:p:n:');
Marc Kupietza5b90152016-03-15 17:39:19 +010016
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010017# -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 +010018if(!$ARGV[0]) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010019 init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0));
Marc Kupietz2cb667e2016-03-10 09:44:12 +010020} else {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010021 init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0));
Marc Kupietz2cb667e2016-03-10 09:44:12 +010022}
Marc Kupietzdc22b982015-10-09 09:19:34 +020023
Marc Kupietza5b90152016-03-15 17:39:19 +010024my $daemon = Mojo::Server::Daemon->new(
25 app => app,
26 listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"]
27);
28
Marc Kupietzdc22b982015-10-09 09:19:34 +020029get '/' => sub {
30 my $c = shift;
31 my $word=$c->param('word');
Marc Kupietz44bee3c2016-02-25 16:26:29 +010032 my $no_nbs=$c->param('n') || 100;
33 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +010034 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +010035 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +010036 my $som=$c->param('som') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +010037 my $sort=$c->param('sort') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +010038 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010039 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +010040 my @collocations;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010041 if(defined($word) && $word !~ /^\s*$/) {
42 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +010043 $word =~ s/\s+/ /g;
44 for my $w (split(' *\| *', $word)) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010045 $c->app->log->debug('Looking for neighbours of '.$w);
Marc Kupietza5b90152016-03-15 17:39:19 +010046 if($opt_i) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +010047 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort);
Marc Kupietza5b90152016-03-15 17:39:19 +010048 } else {
Marc Kupietz6d9a6782016-03-23 17:25:25 +010049 $res = get_neighbours($w, $no_nbs, $sort);
Marc Kupietza5b90152016-03-15 17:39:19 +010050 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +010051 push(@lists, $res->{paradigmatic});
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010052 }
Marc Kupietz247500f2015-10-09 11:29:01 +020053 }
Marc Kupietz000ad862016-02-26 14:59:12 +010054 $word =~ s/ *\| */ | /g;
Marc Kupietz6d9a6782016-03-23 17:25:25 +010055 $c->render(template=>"index", word=>$word, no_nbs=>$no_nbs, no_iterations => $no_iterations, epsilon=> $epsilon, perplexity=> $perplexity, show_som=>$som, sort=>$sort, lists=> \@lists, collocators=> $res->{syntagmatic});
Marc Kupietzdc22b982015-10-09 09:19:34 +020056};
57
Marc Kupietza5b90152016-03-15 17:39:19 +010058$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +020059
60exit;
61
62__END__
63
64__C__
65#include <stdio.h>
66#include <string.h>
67#include <math.h>
68#include <malloc.h>
69#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +010070#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +010071#include <pthread.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +020072
73#define max_size 2000
74#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010075#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +010076#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +010077#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +010078#define MAX_CC 50
79#define EXP_TABLE_SIZE 1000
80#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +010081#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +020082
83//the thread function
84void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +010085
86typedef struct {
87 long long *index;
88 float *dist;
Marc Kupietzb864ccf2016-03-21 22:40:03 +010089 float *norm;
Marc Kupietz6b2975c2016-03-18 21:59:33 +010090 long long *pos;
Marc Kupietz000ad862016-02-26 14:59:12 +010091 unsigned int length;
92} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +010093
Marc Kupietz000ad862016-02-26 14:59:12 +010094typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +010095 long long wordi[MAX_NEIGHBOURS];
96 char sep[MAX_NEIGHBOURS];
97 int length;
98} wordlist;
99
100typedef struct {
101 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100102 char *token;
103 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100104 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100105 unsigned long upto;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100106 float *target_sums;
Marc Kupietz000ad862016-02-26 14:59:12 +0100107} knnpars;
108
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100109float *M, *M2, *syn1neg_window, *expTable;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200110char *vocab;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100111
Marc Kupietz82b02672016-02-26 12:32:25 +0100112long long words, size;
Marc Kupietz000ad862016-02-26 14:59:12 +0100113int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100114int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100115int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200116
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100117int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100118 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100119 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100120 long long a, b, c, d, cn;
121 float len;
122
Marc Kupietz67c20282016-02-26 09:42:00 +0100123 char binvecs_fname[256], binwords_fname[256];
124 strcpy(binwords_fname, file_name);
125 strcat(binwords_fname, ".words");
126 strcpy(binvecs_fname, file_name);
127 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200128
Marc Kupietza5b90152016-03-15 17:39:19 +0100129 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200130 f = fopen(file_name, "rb");
131 if (f == NULL) {
132 printf("Input file %s not found\n", file_name);
133 return -1;
134 }
135 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100136 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200137 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100138 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
139 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100140 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
141 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
142 if (M == NULL) {
143 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
144 return -1;
145 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100146 for (b = 0; b < words; b++) {
147 a = 0;
148 while (1) {
149 vocab[b * max_w + a] = fgetc(f);
150 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
151 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
152 }
153 vocab[b * max_w + a] = 0;
154 fread(&M[b * size], sizeof(float), size, f);
155 len = 0;
156 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
157 len = sqrt(len);
158 for (a = 0; a < size; a++) M[a + b * size] /= len;
159 }
160 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
161 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
162 fclose(binvecs);
163 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
164 fclose(binwords);
165 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100166 }
167 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
168 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
169 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
170 if (M == MAP_FAILED || vocab == MAP_FAILED) {
171 close(binvecs_fd);
172 close(binwords_fd);
173 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
174 exit(-1);
175 }
176 } else {
177 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
178 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100179 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200180 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100181
182 if(net_name) {
183 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
184 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
185 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100186 // munmap(M, sizeof(float) * words * size);
187 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 +0100188 if (M == MAP_FAILED) {
189 close(net_fd);
190 fprintf(stderr, "Cannot mmap %s\n", net_name);
191 exit(-1);
192 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100193 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100194 } else {
195 fprintf(stderr, "Cannot open %s\n", net_name);
196 exit(-1);
197 }
198 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
199 }
200
201 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
202 for (i = 0; i < EXP_TABLE_SIZE; i++) {
203 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
204 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
205 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200206 return 0;
207}
208
Marc Kupietz271e2a42016-03-22 11:37:43 +0100209void *getCollocators(knnpars *pars) {
210 int N = pars->N;
211 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100212 knn *nbs = NULL;
213 long window_layer_size = size * window * 2;
214 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
215 float f, max_f, maxmax_f;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100216 float *target_sums, *bestf, *bestn, worstbest, wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100217 long long *besti, *bestp;
Marc Kupietzd5642582016-03-19 22:23:13 +0100218
219 if(cc == -1)
220 return NULL;
221
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100222 a = posix_memalign((void **) &target_sums, 128, words * sizeof(float));
223 besti = malloc(N * sizeof(long long));
224 bestp = malloc(N * sizeof(long long));
225 bestf = malloc(N * sizeof(float));
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100226 bestn = malloc(N * sizeof(float));
227
Marc Kupietz271e2a42016-03-22 11:37:43 +0100228 worstbest = MIN_RESP;
229
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100230 for (b = 0; b < words; b++)
231 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100232 for (b = 0; b < N; b++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100233 besti[b] = -1;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100234 bestn[b] = 1;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100235 bestf[b] = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100236 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100237
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100238 d = cc;
239 maxmax_f = -1;
240 maxmax_target = 0;
241
Marc Kupietz271e2a42016-03-22 11:37:43 +0100242 for (a = pars->from; a < pars->upto; a++) {
243 if(a >= window)
244 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100245 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100246 printf("window pos: %ld\n", a);
247 if (a != window) {
248 max_f = -1;
249 window_offset = a * size;
250 if (a > window)
251 window_offset -= size;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100252 for(target = 0; target < words; target ++) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100253 if(target == d)
254 continue;
255 f = 0;
256 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100257 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100258 if (f < -MAX_EXP)
259 continue;
260 else if (f > MAX_EXP)
261 continue;
262 else
263 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100264 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100265
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100266 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100267 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100268 for (b = 0; b < N; b++) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100269 if (f > bestf[b]) {
Marc Kupietz33679a32016-03-22 08:49:39 +0100270 memmove(bestf + b + 1, bestf + b, (N - b -1) * sizeof(float));
271 memmove(besti + b + 1, besti + b, (N - b -1) * sizeof(long long));
272 memmove(bestp + b + 1, bestp + b, (N - b -1) * sizeof(long long));
273 bestf[b] = f;
274 besti[b] = target;
275 bestp[b] = window-a;
276 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100277 }
278 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100279 if(b == N - 1)
280 worstbest = bestf[N-1];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100281 }
282 }
283 printf("%d %.2f\n", max_target, max_f);
284 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
285 if(max_f > maxmax_f) {
286 maxmax_f = max_f;
287 maxmax_target = max_target;
288 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100289 for (b = 0; b < N; b++)
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100290 if(bestp[b] == window-a)
291 bestn[b] = bestf[b] / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100292 } else {
293 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
294 }
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100295
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100296 }
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100297 for (b = 0; b < words; b++)
298 pars->target_sums[b] += (target_sums[b] / wpos_sum ) / (window * 2);
299 free(target_sums);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100300 for(b=0; b<N && besti[b] >= 0; b++) // THIS LOOP IS NEEDED (b...)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100301 printf("%s %.2f %d * ", &vocab[besti[b]*max_w], bestf[b], bestp[b]);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100302 printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100303 nbs = malloc(sizeof(knn));
304 nbs->index = besti;
305 nbs->dist = bestf;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100306 nbs->norm = bestn;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100307 nbs->pos = bestp;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100308 nbs->length = b-1;
309 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100310}
311
Marc Kupietz48c29682016-03-19 11:30:43 +0100312wordlist *getTargetWords(char *st1) {
313 wordlist *wl = malloc(sizeof(wordlist));
314 char st[100][max_size], sep[100];
315 long a, b=0, c=0, cn=0;
316
Marc Kupietzdc22b982015-10-09 09:19:34 +0200317 while (1) {
318 st[cn][b] = st1[c];
319 b++;
320 c++;
321 st[cn][b] = 0;
322 if (st1[c] == 0) break;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100323 if (st1[c] == ' ' || st1[c] == '-') {
324 sep[cn++] = st1[c];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200325 b = 0;
326 c++;
327 }
328 }
329 cn++;
330 for (a = 0; a < cn; a++) {
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100331 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
332 if (b == words) b = -1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100333 wl->wordi[a] = b;
334 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], wl->wordi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200335 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100336 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100337 cn--;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200338 break;
339 }
340 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100341 wl->length=cn;
342 return(wl);
343}
344
345void *_get_neighbours(knnpars *pars) {
346 char *st1 = pars->token;
347 int N = pars->N;
348 long from = pars -> from;
349 unsigned long upto = pars -> upto;
350 char file_name[max_size], st[100][max_size], *sep;
351 float dist, len, *bestd, vec[max_size];
352 long long a, b, c, d, cn, *bi, *besti;
353 char ch;
354 knn *nbs = NULL;
355 wordlist *wl = pars->wl;
356
357 besti = malloc(N * sizeof(long long));
358 bestd = malloc(N * sizeof(float));
359
360 float worstbest=-1;
361
362 for (a = 0; a < N; a++) bestd[a] = 0;
363 a = 0;
364 bi = wl->wordi;
365 cn = wl->length;
366 sep = wl->sep;
367 b = bi[0];
368 c = 0;
369
Marc Kupietz000ad862016-02-26 14:59:12 +0100370 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100371 N = 0;
372 goto end;
373 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200374 for (a = 0; a < size; a++) vec[a] = 0;
375 for (b = 0; b < cn; b++) {
376 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100377 if(b>0 && sep[b-1] == '-')
378 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
379 else
380 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200381 }
382 len = 0;
383 for (a = 0; a < size; a++) len += vec[a] * vec[a];
384 len = sqrt(len);
385 for (a = 0; a < size; a++) vec[a] /= len;
386 for (a = 0; a < N; a++) bestd[a] = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100387 for (c = from; c < upto; c++) {
Marc Kupietzdc22b982015-10-09 09:19:34 +0200388 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100389// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100390// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
391// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200392 dist = 0;
393 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100394 if(dist > worstbest) {
395 for (a = 0; a < N; a++) {
396 if (dist > bestd[a]) {
Marc Kupietz33679a32016-03-22 08:49:39 +0100397 memmove(bestd + a + 1, bestd + a, (N - a -1) * sizeof(float));
398 memmove(besti + a + 1, besti + a, (N - a -1) * sizeof(long long));
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100399 bestd[a] = dist;
400 besti[a] = c;
401 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200402 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200403 }
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100404 worstbest = bestd[N-1];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200405 }
406 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100407
Marc Kupietz000ad862016-02-26 14:59:12 +0100408 nbs = malloc(sizeof(knn));
409 nbs->index = besti;
410 nbs->dist = bestd;
411 nbs->length = N;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100412end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100413 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200414}
415
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100416
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100417SV *get_neighbours(char *st1, int N, int sort_by) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100418 HV *result = newHV();
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100419 float *target_sums, bestd[MAX_NEIGHBOURS], bestn[MAX_NEIGHBOURS], bests[MAX_NEIGHBOURS], vec[max_size];
Marc Kupietz50485ba2016-03-23 09:13:14 +0100420 long besti[MAX_NEIGHBOURS], bestp[MAX_NEIGHBOURS], a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100421 knn *para_nbs[MAX_THREADS];
422 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100423 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100424 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100425 wordlist *wl;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100426 int para_threads = num_threads - window * 2;
427 int syn_threads = window * 2;
428 num_threads = para_threads+syn_threads;
Marc Kupietz48c29682016-03-19 11:30:43 +0100429
Marc Kupietz000ad862016-02-26 14:59:12 +0100430 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
431
Marc Kupietz271e2a42016-03-22 11:37:43 +0100432 slice = words / syn_threads;
Marc Kupietz000ad862016-02-26 14:59:12 +0100433
Marc Kupietz48c29682016-03-19 11:30:43 +0100434 wl = getTargetWords(st1);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100435 if(wl->length < 1)
436 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100437
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100438 a = posix_memalign((void **) &target_sums, 128, words * sizeof(float));
439 for(a = 0; a < words; a++)
440 target_sums[a] = 0;
441
Marc Kupietz271e2a42016-03-22 11:37:43 +0100442 for(a=0; a < para_threads; a++) {
Marc Kupietz000ad862016-02-26 14:59:12 +0100443 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100444 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100445 pars[a].N = N;
446 pars[a].from = a*slice;
447 pars[a].upto = ((a+1)*slice > words? words:(a+1)*slice);
448 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
449 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100450 for(a=0; a < syn_threads; a++) {
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100451 pars[a + para_threads].target_sums = target_sums;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100452 pars[a + para_threads].wl = wl;
453 pars[a + para_threads].N = N;
454 pars[a + para_threads].from = a;
455 pars[a + para_threads].upto = a+1;
456 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
457 }
458 printf("Waiting for para threads to join\n");
459 fflush(stdout);
460 for (a = 0; a < para_threads; a++) pthread_join(pt[a], &para_nbs[a]);
461 printf("Para threads joint\n");
462 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +0100463
Marc Kupietz271e2a42016-03-22 11:37:43 +0100464 if(!syn_nbs[0])
Marc Kupietz000ad862016-02-26 14:59:12 +0100465 goto end;
466
467 for(b=0; b < N; b++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100468 besti[b] = para_nbs[0]->index[b];
469 bestd[b] = para_nbs[0]->dist[b];
Marc Kupietz000ad862016-02-26 14:59:12 +0100470 }
471
Marc Kupietz271e2a42016-03-22 11:37:43 +0100472 for(a=1; a < para_threads; a++) {
473 for(b=0; b < para_nbs[a]->length && para_nbs[a]->index[b] >= 0; b++) {
Marc Kupietz000ad862016-02-26 14:59:12 +0100474 for(c=0; c < N; c++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100475 if(para_nbs[a]->dist[b] > bestd[c]) {
Marc Kupietz000ad862016-02-26 14:59:12 +0100476 for(d=N-1; d>c; d--) {
477 bestd[d] = bestd[d-1];
478 besti[d] = besti[d-1];
479 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100480 besti[c] = para_nbs[a]->index[b];
481 bestd[c] = para_nbs[a]->dist[b];
Marc Kupietz000ad862016-02-26 14:59:12 +0100482 break;
483 }
484 }
485 }
486 }
487
Marc Kupietz271e2a42016-03-22 11:37:43 +0100488 AV* array = newAV();
489 for (a = 0; a < N; a++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100490 HV* hash = newHV();
Marc Kupietz50485ba2016-03-23 09:13:14 +0100491 SV* word = newSVpvf(&vocab[besti[a] * max_w], 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100492 if(latin_enc == 0) SvUTF8_on(word);
493 hv_store(hash, "word", strlen("word"), word , 0);
494 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
495 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
496 AV *vector = newAV();
497 for (b = 0; b < size; b++) {
498 av_push(vector, newSVnv(M[b + besti[a] * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100499 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100500 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
501 av_push(array, newRV_noinc((SV*)hash));
502 }
503 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
504
Marc Kupietz50485ba2016-03-23 09:13:14 +0100505 for(b=0; b < MAX_NEIGHBOURS; b++) {
506 besti[b] = -1L;
507 bestd[b] = 0;
508 bestn[b] = 0;
509 bestp[b] = 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100510 bests[b] = 0;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100511 }
512
Marc Kupietz271e2a42016-03-22 11:37:43 +0100513 printf("Waiting for syn threads to join\n");
514 fflush(stdout);
515 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], &syn_nbs[a]);
516 printf("syn threads joint\n");
517 fflush(stdout);
518
Marc Kupietz50485ba2016-03-23 09:13:14 +0100519
520 for(b=0; b < syn_nbs[0]->length; b++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100521 besti[b] = syn_nbs[0]->index[b];
522 bestd[b] = syn_nbs[0]->dist[b];
523 bestn[b] = syn_nbs[0]->norm[b];
524 bestp[b] = syn_nbs[0]->pos[b];
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100525 bests[b] = target_sums[syn_nbs[0]->index[b]];
Marc Kupietz271e2a42016-03-22 11:37:43 +0100526 }
527
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100528 if(sort_by != 1) { // sort by responsiveness
529 for(a=1; a < syn_threads; a++) {
530 for(b=0; b < syn_nbs[a]->length; b++) {
531 for(c=0; c < MAX_NEIGHBOURS; c++) {
532 if(syn_nbs[a]->dist[b] > bestd[c]) {
533 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
534 bestd[d] = bestd[d-1];
535 besti[d] = besti[d-1];
536 bestn[d] = bestn[d-1];
537 bestp[d] = bestp[d-1];
538 }
539 besti[c] = syn_nbs[a]->index[b];
540 bestd[c] = syn_nbs[a]->dist[b];
541 bestn[c] = syn_nbs[a]->norm[b];
542 bestp[c] = syn_nbs[a]->pos[b];
543 break;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100544 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100545 }
546 }
547 }
548 } else { // sort by mean p
549 for(a=1; a < syn_threads; a++) {
550 for(b=0; b < syn_nbs[a]->length; b++) {
551 for(c=0; c < MAX_NEIGHBOURS; c++) {
552 if(target_sums[syn_nbs[a]->index[b]] > bests[c]) {
553 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
554 bestd[d] = bestd[d-1];
555 besti[d] = besti[d-1];
556 bestn[d] = bestn[d-1];
557 bestp[d] = bestp[d-1];
558 bests[d] = bests[d-1];
559 }
560 besti[c] = syn_nbs[a]->index[b];
561 bestd[c] = syn_nbs[a]->dist[b];
562 bestn[c] = syn_nbs[a]->norm[b];
563 bestp[c] = syn_nbs[a]->pos[b];
564 bests[c] = target_sums[syn_nbs[a]->index[b]];
565 break;
566 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100567 }
568 }
569 }
570 }
571 array = newAV();
Marc Kupietz50485ba2016-03-23 09:13:14 +0100572 for (a = 0; a < MAX_NEIGHBOURS && besti[a] >= 0; a++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100573 HV* hash = newHV();
Marc Kupietz50485ba2016-03-23 09:13:14 +0100574 SV* word = newSVpvf(&vocab[besti[a] * max_w], 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100575 if(latin_enc == 0) SvUTF8_on(word);
576 hv_store(hash, "word", strlen("word"), word , 0);
577 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
578 hv_store(hash, "norm", strlen("norm"), newSVnv(bestn[a]), 0);
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100579 hv_store(hash, "sum", strlen("sum"), newSVnv(target_sums[besti[a]]), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100580 hv_store(hash, "pos", strlen("pos"), newSVnv(bestp[a]), 0);
581 av_push(array, newRV_noinc((SV*)hash));
582 }
583 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz000ad862016-02-26 14:59:12 +0100584end:
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100585 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +0100586}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100587
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100588
Marc Kupietzdc22b982015-10-09 09:19:34 +0200589__DATA__
590
591@@ index.html.ep
592<!DOCTYPE html>
593<html>
Marc Kupietzc4893362016-02-25 08:04:46 +0100594<head>
595 <title>DeReKo-Word-Vector-Distances</title>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100596 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
Marc Kupietzc4893362016-02-25 08:04:46 +0100597 <script src="http://code.jquery.com/jquery-latest.min.js"></script>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100598 <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
599 <script>
600 $(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100601 $( document ).tooltip({
602 content: function() {
603 return $(this).attr('title');
604 }}
605 )
606 })
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100607 </script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100608 <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
609 <script src="http://klinux10/word2vec/tsne.js"></script>
Marc Kupietzd7aea722016-03-02 11:59:12 +0100610 <script src="http://klinux10/word2vec/som.js"></script>
Marc Kupietzc5990da2016-02-26 08:47:12 +0100611 <script src="http://klinux10/word2vec/labeler.js"></script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100612<style>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100613body, input {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100614 font-family: Arial, sans-serif;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100615 font-size: 11pt;
616}
617
618.ui-tooltip-content {
619 font-size: 9pt;
620 colour: #222222;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100621}
Marc Kupietz5f780672016-02-25 17:15:54 +0100622
623svg > .ui-tooltip-content {
Marc Kupietzb1029362016-02-27 21:38:55 +0100624 font-size: 8pt;
Marc Kupietz5f780672016-02-25 17:15:54 +0100625 colour: #222222;
626}
627
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100628#collocators {
629 margin-bottom: 15px;
630}
631
Marc Kupietzc4893362016-02-25 08:04:46 +0100632#wrapper {
633 width: 100%;
634// border: 1px solid red;
635 overflow: hidden; /* will contain if #first is longer than #second */
636}
637#first {
Marc Kupietzb1029362016-02-27 21:38:55 +0100638 margin-right: 20px;
639 float: left;
640 // border: 1px solid green;
Marc Kupietzc4893362016-02-25 08:04:46 +0100641}
642#second {
643 border: 1px solid #333;
644 overflow: hidden; /* if you don't want #second to wrap below #first */
645}
Marc Kupietzd7aea722016-03-02 11:59:12 +0100646#som2 svg {
647 border: 1px solid #333;
648}
649
Marc Kupietz4aa62172016-02-25 10:39:27 +0100650#cost {
Marc Kupietzb1029362016-02-27 21:38:55 +0100651 font-size: 8pt;
652 color: #222222;
653 margin-top: 4px;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100654 margin-bottom: 12px;
Marc Kupietz4aa62172016-02-25 10:39:27 +0100655}
Marc Kupietzd7aea722016-03-02 11:59:12 +0100656
Marc Kupietz6c1ca442016-03-03 09:35:18 +0100657#sominfo1, #sominfo {
Marc Kupietzd7aea722016-03-02 11:59:12 +0100658 font-size: 8pt;
659 color: #222222;
660 margin-top: 0px;
661}
662
Marc Kupietz6c1ca442016-03-03 09:35:18 +0100663#somcolor1, #somcolor2, #somcolor3 {
664 display: inline-block;
665 height: 10px;
666 width: 10px;
667}
668
Marc Kupietzd7aea722016-03-02 11:59:12 +0100669#third {
670 border: 1px solid #333;
671}
672
Marc Kupietzc4893362016-02-25 08:04:46 +0100673</style>
674<script>
675
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100676var opt = {epsilon: <%= $epsilon %>, perplexity: <%= $perplexity %>},
Marc Kupietz9fca1732016-02-29 09:07:04 +0100677 mapWidth = 800, // width map
678 mapHeight = 800,
679 jitterRadius = 7;
680
Marc Kupietzc4893362016-02-25 08:04:46 +0100681var T = new tsnejs.tSNE(opt); // create a tSNE instance
682
683var Y;
684
685var data;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100686var labeler;
Marc Kupietzc4893362016-02-25 08:04:46 +0100687
Marc Kupietzc5990da2016-02-26 08:47:12 +0100688
689function applyJitter() {
Marc Kupietzd7aea722016-03-02 11:59:12 +0100690 svg.selectAll('.tsnet')
Marc Kupietzc5990da2016-02-26 08:47:12 +0100691 .data(labels)
692 .transition()
693 .duration(50)
694 .attr("transform", function(d, i) {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100695 T.Y[i][0] = (d.x - mapWidth/2 - tx)/ss/20;
696 T.Y[i][1] = (d.y - mapHeight/2 - ty)/ss/20;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100697 return "translate(" +
698 (d.x) + "," +
699 (d.y) + ")";
700 });
701}
702
Marc Kupietzc4893362016-02-25 08:04:46 +0100703function updateEmbedding() {
704 var Y = T.getSolution();
Marc Kupietzd7aea722016-03-02 11:59:12 +0100705 svg.selectAll('.tsnet')
Marc Kupietz9fca1732016-02-29 09:07:04 +0100706 .data(data.words)
707 .attr("transform", function(d, i) {
708 return "translate(" +
709 ((Y[i][0]*20*ss + tx) + mapWidth/2) + "," +
710 ((Y[i][1]*20*ss + ty) + mapHeight/2) + ")"; });
Marc Kupietzc4893362016-02-25 08:04:46 +0100711}
712
713var svg;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100714var labels = [];
715var anchor_array = [];
716var text;
717
Marc Kupietzc4893362016-02-25 08:04:46 +0100718function drawEmbedding() {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100719 $("#embed").empty();
720 var div = d3.select("#embed");
721
722 // get min and max in each column of Y
723 var Y = T.Y;
724
725 svg = div.append("svg") // svg is global
726 .attr("width", mapWidth)
727 .attr("height", mapHeight);
728
729 var g = svg.selectAll(".b")
730 .data(data.words)
731 .enter().append("g")
Marc Kupietzd7aea722016-03-02 11:59:12 +0100732 .attr("class", "tsnet");
Marc Kupietz9fca1732016-02-29 09:07:04 +0100733
734 g.append("a")
735 .attr("xlink:href", function(word) {return "/?word="+word;})
736 .attr("title", function(d, i) {
Marc Kupietze50c8162016-03-01 10:24:43 +0100737 return "rank: "+i +" "+"freq. rank: "+data.ranks[i].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
Marc Kupietz9fca1732016-02-29 09:07:04 +0100738 })
Marc Kupietza350bce2016-02-25 09:34:25 +0100739 .append("text")
Marc Kupietz9fca1732016-02-29 09:07:04 +0100740 .attr("text-anchor", "top")
741 .attr("font-size", 12)
742 .attr("fill", function(d) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100743 if(data.target.indexOf(" "+d+" ") >= 0) {
Marc Kupietza350bce2016-02-25 09:34:25 +0100744 return "red";
745 } else {
746 return "#333"
Marc Kupietzc4893362016-02-25 08:04:46 +0100747 }
Marc Kupietz9fca1732016-02-29 09:07:04 +0100748 })
749 .text(function(d) { return d; });
750
751 var zoomListener = d3.behavior.zoom()
752 .scaleExtent([0.1, 10])
753 .center([0,0])
754 .on("zoom", zoomHandler);
755 zoomListener(svg);
756}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100757
Marc Kupietz9fca1732016-02-29 09:07:04 +0100758var tx=0, ty=0;
759var ss=1;
760var iter_id=-1;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100761
Marc Kupietz9fca1732016-02-29 09:07:04 +0100762function zoomHandler() {
763 tx = d3.event.translate[0];
764 ty = d3.event.translate[1];
765 ss = d3.event.scale;
766 updateEmbedding();
767}
768
769var stepnum = 0;
770
771function stopStep() {
772 clearInterval(iter_id);
773 text = svg.selectAll("text");
774
775 // jitter function needs different data and co-ordinate representation
776 labels = d3.range(data.words.length).map(function(i) {
777 var x = (T.Y[i][0]*20*ss + tx) + mapWidth/2;
778 var y = (T.Y[i][1]*20*ss + ty) + mapHeight/2;
779 anchor_array.push({x: x, y: y, r: jitterRadius});
780 return {
781 x: x,
782 y: y,
783 name: data.words[i]
784 };
785 });
786
787 // get the actual label bounding boxes for the jitter function
788 var index = 0;
789 text.each(function() {
790 labels[index].width = this.getBBox().width;
791 labels[index].height = this.getBBox().height;
792 index += 1;
793 });
Marc Kupietzc5990da2016-02-26 08:47:12 +0100794
795
796// setTimeout(updateEmbedding, 1);
797// setTimeout(
Marc Kupietz9fca1732016-02-29 09:07:04 +0100798 labeler = d3.labeler()
799 .label(labels)
800 .anchor(anchor_array)
801 .width(mapWidth)
802 .height(mapHeight)
803 .update(applyJitter);
804 // .start(1000);
Marc Kupietzc5990da2016-02-26 08:47:12 +0100805
Marc Kupietz9fca1732016-02-29 09:07:04 +0100806 iter_id = setInterval(jitterStep, 1);
807}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100808
Marc Kupietz9fca1732016-02-29 09:07:04 +0100809var jitter_i=0;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100810
811function jitterStep() {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100812 if(jitter_i++ > 100) {
813 clearInterval(iter_id);
814 } else {
815 labeler.start2(10);
816 applyJitter();
817 }
Marc Kupietzc5990da2016-02-26 08:47:12 +0100818}
Marc Kupietzb1029362016-02-27 21:38:55 +0100819
820var last_cost=1000;
821
Marc Kupietz9fca1732016-02-29 09:07:04 +0100822function step() {
823 var i = T.iter;
824
825 if(i > <%= $no_iterations %>) {
826 stopStep();
827 } else {
828 var cost = Math.round(T.step() * 100000) / 100000; // do a few steps
829 $("#cost").html("tsne iteration " + i + ", cost: " + cost.toFixed(5));
830 if(i % 250 == 0 && cost >= last_cost) {
831 stopStep();
832 } else {
833 last_cost = cost;
834 updateEmbedding();
835 }
836 }
837}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100838
Marc Kupietz9fca1732016-02-29 09:07:04 +0100839function showMap(j) {
840 data=j;
841 T.iter=0;
842 T.initDataRaw(data.vecs); // init embedding
843 drawEmbedding(); // draw initial embedding
844
845 if(iter_id >= 0) {
846 clearInterval(iter_id);
847 }
848 //T.debugGrad();
849 iter_id = setInterval(step, 1);
Marc Kupietzd7aea722016-03-02 11:59:12 +0100850 if(<%= $show_som %>) {
851 makeSOM(j, <%= $no_iterations %>);
852 }
Marc Kupietz9fca1732016-02-29 09:07:04 +0100853}
Marc Kupietza350bce2016-02-25 09:34:25 +0100854
Marc Kupietzc4893362016-02-25 08:04:46 +0100855</script>
856</head>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200857<body>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100858 <form action="<%=url_for('/')->to_abs%>" method="GET">
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100859 word(s):
860 <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.">
861 max. neighbours: <input type="text" size="8" name="n" value="<%= $no_nbs %>">
Marc Kupietzd7aea722016-03-02 11:59:12 +0100862 max. iterations: <input type="text" name="N" size="8" value="<%= $no_iterations %>">
863 SOM <input type="checkbox" name="som" value="1" <%= ($show_som ? "checked" : "") %>>
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100864 <span> </span>sort collocators by
865 <select name="sort">
866 <option value="0" <%= ($sort!=1? "selected":"") %>>responsiveness</option>
867 <option value="1" <%= ($sort==1? "selected":"") %>>mean p</option>
868 </select>
Marc Kupietzd7aea722016-03-02 11:59:12 +0100869 <span> </span><input type="submit" value="Show">
Marc Kupietz4aa62172016-02-25 10:39:27 +0100870 </form>
871 <br>
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100872 % if($lists) {
Marc Kupietz4aa62172016-02-25 10:39:27 +0100873 <div id="wrapper">
874 <table id="first">
875 <tr>
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100876 <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 +0100877 </tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100878 % my $j=0; my @words; my @vecs; my @ranks; for my $list (@$lists) {
Marc Kupietz50485ba2016-03-23 09:13:14 +0100879 % my $i=0; while(1) {
880 % my $item = (@$list)[$i];
881 % my $c = (@$collocators)[$i];
882 % last if(!$c && !$item);
Marc Kupietz4aa62172016-02-25 10:39:27 +0100883 <tr>
884 <td align="right">
Marc Kupietzd5642582016-03-19 22:23:13 +0100885 <%= ++$i %>.
Marc Kupietz4aa62172016-02-25 10:39:27 +0100886 </td>
Marc Kupietz50485ba2016-03-23 09:13:14 +0100887 % if($item) {
888 % if(!grep{$_ eq $item->{word}} @words) {
889 % push @vecs, $item->{vector};
890 % push @words, $item->{word};
891 % push @ranks, $item->{rank};
892 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100893 <td align="right">
894 <%= sprintf("%.3f", $item->{dist}) %>
895 </td>
Marc Kupietzd5642582016-03-19 22:23:13 +0100896 <td>
897 <a title="freq. rank: <%= $item->{rank} %>" href="/?word=<%= $item->{word} %>">
898 <%= $item->{word} %>
899 </a>
900 </td>
Marc Kupietz50485ba2016-03-23 09:13:14 +0100901 % } else {
902 <td colspan="2"/>
903 % }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100904 % if($c) {
Marc Kupietz5f780672016-02-25 17:15:54 +0100905 <td align="right">
Marc Kupietzd5642582016-03-19 22:23:13 +0100906 <%= $c->{pos} %>:
907 </td>
908 <td align="right">
909 <%= sprintf("%.3f", $c->{dist}) %>
910 </td>
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100911 <td align="right">
912 <%= sprintf("%.3e", $c->{norm}) %>
913 </td>
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100914 <td align="right">
915 <%= sprintf("%.3e", $c->{sum}) %>
916 </td>
Marc Kupietzd5642582016-03-19 22:23:13 +0100917 <td align="left">
918 <a href="/?word=<%= $c->{word} %>">
919 <%= $c->{word} %>
Marc Kupietz5f780672016-02-25 17:15:54 +0100920 </td>
Marc Kupietz271e2a42016-03-22 11:37:43 +0100921 % } else {
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100922 <td colspan="5"/>
Marc Kupietz271e2a42016-03-22 11:37:43 +0100923 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100924 </tr>
925 % }
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100926 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100927 </table>
928 <script>
929 % use Mojo::ByteStream 'b';
930 $(window).load(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100931 showMap(<%= b(Mojo::JSON::to_json({target => " $word ", words => \@words, vecs => \@vecs, ranks => \@ranks})); %>);
Marc Kupietz4aa62172016-02-25 10:39:27 +0100932 });
933 </script>
934 % }
935 <div id="second" style="width:800px; height:800px; font-family: arial;">
936 <div id="embed">
937 </div>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100938 </div>
Marc Kupietzb1029362016-02-27 21:38:55 +0100939 <div id="cost"></div>
Marc Kupietzd7aea722016-03-02 11:59:12 +0100940 % if($show_som) {
941 <div id="som2">
942 </div>
Marc Kupietz6c1ca442016-03-03 09:35:18 +0100943 <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 +0100944 <div id="sominfo">SOM iteration <span id="iterations">0</span></div>
945 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100946 </div>
947 <p>
948 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 +0200949 <pre>
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100950-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 +0100951 </pre>
952 </p>
953</body>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200954</html>
955