blob: ec33052698e40204da124950a85583c1fb38877e [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 Kupietz6b2975c2016-03-18 21:59:33 +010037 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010038 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +010039 my @collocations;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010040 if(defined($word) && $word !~ /^\s*$/) {
41 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +010042 $word =~ s/\s+/ /g;
43 for my $w (split(' *\| *', $word)) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010044 $c->app->log->debug('Looking for neighbours of '.$w);
Marc Kupietza5b90152016-03-15 17:39:19 +010045 if($opt_i) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010046 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs);
Marc Kupietza5b90152016-03-15 17:39:19 +010047 } else {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010048 $res = get_neighbours($w, $no_nbs);
Marc Kupietza5b90152016-03-15 17:39:19 +010049 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +010050 push(@lists, $res->{paradigmatic});
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +010051 }
Marc Kupietz247500f2015-10-09 11:29:01 +020052 }
Marc Kupietz000ad862016-02-26 14:59:12 +010053 $word =~ s/ *\| */ | /g;
Marc Kupietz6b2975c2016-03-18 21:59:33 +010054 $c->render(template=>"index", word=>$word, no_nbs=>$no_nbs, no_iterations => $no_iterations, epsilon=> $epsilon, perplexity=> $perplexity, show_som=>$som, lists=> \@lists, collocators=> $res->{syntagmatic});
Marc Kupietzdc22b982015-10-09 09:19:34 +020055};
56
Marc Kupietza5b90152016-03-15 17:39:19 +010057$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +020058
59exit;
60
61__END__
62
63__C__
64#include <stdio.h>
65#include <string.h>
66#include <math.h>
67#include <malloc.h>
68#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +010069#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +010070#include <pthread.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +020071
72#define max_size 2000
73#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010074#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +010075#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +010076#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +010077#define MAX_CC 50
78#define EXP_TABLE_SIZE 1000
79#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +010080#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +020081
82//the thread function
83void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +010084
85typedef struct {
86 long long *index;
87 float *dist;
Marc Kupietzb864ccf2016-03-21 22:40:03 +010088 float *norm;
Marc Kupietz6b2975c2016-03-18 21:59:33 +010089 long long *pos;
Marc Kupietz000ad862016-02-26 14:59:12 +010090 unsigned int length;
91} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +010092
Marc Kupietz000ad862016-02-26 14:59:12 +010093typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +010094 long long wordi[MAX_NEIGHBOURS];
95 char sep[MAX_NEIGHBOURS];
96 int length;
97} wordlist;
98
99typedef struct {
100 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100101 char *token;
102 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100103 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100104 unsigned long upto;
105} knnpars;
106
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100107float *M, *syn1neg_window, *expTable;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200108char *vocab;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100109
Marc Kupietz82b02672016-02-26 12:32:25 +0100110long long words, size;
Marc Kupietz000ad862016-02-26 14:59:12 +0100111int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100112int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100113int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200114
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100115int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100116 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100117 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100118 long long a, b, c, d, cn;
119 float len;
120
Marc Kupietz67c20282016-02-26 09:42:00 +0100121 char binvecs_fname[256], binwords_fname[256];
122 strcpy(binwords_fname, file_name);
123 strcat(binwords_fname, ".words");
124 strcpy(binvecs_fname, file_name);
125 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200126
Marc Kupietza5b90152016-03-15 17:39:19 +0100127 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200128 f = fopen(file_name, "rb");
129 if (f == NULL) {
130 printf("Input file %s not found\n", file_name);
131 return -1;
132 }
133 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100134 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200135 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100136 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
137 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100138 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
139 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
140 if (M == NULL) {
141 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
142 return -1;
143 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100144 for (b = 0; b < words; b++) {
145 a = 0;
146 while (1) {
147 vocab[b * max_w + a] = fgetc(f);
148 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
149 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
150 }
151 vocab[b * max_w + a] = 0;
152 fread(&M[b * size], sizeof(float), size, f);
153 len = 0;
154 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
155 len = sqrt(len);
156 for (a = 0; a < size; a++) M[a + b * size] /= len;
157 }
158 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
159 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
160 fclose(binvecs);
161 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
162 fclose(binwords);
163 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100164 }
165 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
166 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
167 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
168 if (M == MAP_FAILED || vocab == MAP_FAILED) {
169 close(binvecs_fd);
170 close(binwords_fd);
171 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
172 exit(-1);
173 }
174 } else {
175 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
176 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100177 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200178 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100179
180 if(net_name) {
181 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
182 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
183 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
184 munmap(M, sizeof(float) * words * size);
185 M = mmap(0, sizeof(float) * words * size + sizeof(float) * 2 * window * size * words, PROT_READ, MAP_SHARED, net_fd, 0);
186 if (M == MAP_FAILED) {
187 close(net_fd);
188 fprintf(stderr, "Cannot mmap %s\n", net_name);
189 exit(-1);
190 }
191 syn1neg_window = M + words * size;
192 } else {
193 fprintf(stderr, "Cannot open %s\n", net_name);
194 exit(-1);
195 }
196 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
197 }
198
199 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
200 for (i = 0; i < EXP_TABLE_SIZE; i++) {
201 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
202 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
203 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200204 return 0;
205}
206
Marc Kupietz271e2a42016-03-22 11:37:43 +0100207void *getCollocators(knnpars *pars) {
208 int N = pars->N;
209 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100210 knn *nbs = NULL;
211 long window_layer_size = size * window * 2;
212 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
213 float f, max_f, maxmax_f;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100214 float *target_sums, *bestf, *bestn, worstbest, wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100215 long long *besti, *bestp;
Marc Kupietzd5642582016-03-19 22:23:13 +0100216
217 if(cc == -1)
218 return NULL;
219
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100220 a = posix_memalign((void **) &target_sums, 128, words * sizeof(float));
221 besti = malloc(N * sizeof(long long));
222 bestp = malloc(N * sizeof(long long));
223 bestf = malloc(N * sizeof(float));
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100224 bestn = malloc(N * sizeof(float));
225
Marc Kupietz271e2a42016-03-22 11:37:43 +0100226 worstbest = MIN_RESP;
227
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100228 for (b = 0; b < words; b++)
229 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100230 for (b = 0; b < N; b++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100231 besti[b] = -1;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100232 bestn[b] = 1;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100233 bestf[b] = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100234 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100235
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100236 d = cc;
237 maxmax_f = -1;
238 maxmax_target = 0;
239
Marc Kupietz271e2a42016-03-22 11:37:43 +0100240 for (a = pars->from; a < pars->upto; a++) {
241 if(a >= window)
242 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100243 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100244 printf("window pos: %ld\n", a);
245 if (a != window) {
246 max_f = -1;
247 window_offset = a * size;
248 if (a > window)
249 window_offset -= size;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100250 for(target = 0; target < words; target ++) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100251 if(target == d)
252 continue;
253 f = 0;
254 for (c = 0; c < size; c++)
255 f += M[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
256 if (f < -MAX_EXP)
257 continue;
258 else if (f > MAX_EXP)
259 continue;
260 else
261 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100262 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100263
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100264 target_sums[target] += (1-target_sums[target]) * f;
265 if(f > worstbest) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100266 for (b = 0; b < N/2; b++) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100267 if (f > bestf[b]) {
Marc Kupietz33679a32016-03-22 08:49:39 +0100268 memmove(bestf + b + 1, bestf + b, (N - b -1) * sizeof(float));
269 memmove(besti + b + 1, besti + b, (N - b -1) * sizeof(long long));
270 memmove(bestp + b + 1, bestp + b, (N - b -1) * sizeof(long long));
271 bestf[b] = f;
272 besti[b] = target;
273 bestp[b] = window-a;
274 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100275 }
276 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100277 if(b == N/2 - 1)
278 worstbest = bestf[N/2-1];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100279 }
280 }
281 printf("%d %.2f\n", max_target, max_f);
282 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
283 if(max_f > maxmax_f) {
284 maxmax_f = max_f;
285 maxmax_target = max_target;
286 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100287 for (b = 0; b < N; b++)
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100288 if(bestp[b] == window-a)
289 bestn[b] = bestf[b] / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100290 } else {
291 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
292 }
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100293
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100294 }
295 max_f = -1;
296 for (b = 0; b < words; b++) {
297 if(target_sums[b] > max_f) {
298 max_f = target_sums[b];
299 max_target = b;
300 }
301 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100302 for(b=0; b<N && besti[b] >= 0; b++) // THIS LOOP IS NEEDED (b...)
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100303 printf("%-32s %.2f %d\n", &vocab[besti[b]*max_w], bestf[b], bestp[b]);
304 printf("\n");
305 free(target_sums);
306 nbs = malloc(sizeof(knn));
307 nbs->index = besti;
308 nbs->dist = bestf;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100309 nbs->norm = bestn;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100310 nbs->pos = bestp;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100311 nbs->length = b-1;
312 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100313}
314
Marc Kupietz48c29682016-03-19 11:30:43 +0100315wordlist *getTargetWords(char *st1) {
316 wordlist *wl = malloc(sizeof(wordlist));
317 char st[100][max_size], sep[100];
318 long a, b=0, c=0, cn=0;
319
Marc Kupietzdc22b982015-10-09 09:19:34 +0200320 while (1) {
321 st[cn][b] = st1[c];
322 b++;
323 c++;
324 st[cn][b] = 0;
325 if (st1[c] == 0) break;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100326 if (st1[c] == ' ' || st1[c] == '-') {
327 sep[cn++] = st1[c];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200328 b = 0;
329 c++;
330 }
331 }
332 cn++;
333 for (a = 0; a < cn; a++) {
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100334 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
335 if (b == words) b = -1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100336 wl->wordi[a] = b;
337 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], wl->wordi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200338 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100339 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100340 cn--;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200341 break;
342 }
343 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100344 wl->length=cn;
345 return(wl);
346}
347
348void *_get_neighbours(knnpars *pars) {
349 char *st1 = pars->token;
350 int N = pars->N;
351 long from = pars -> from;
352 unsigned long upto = pars -> upto;
353 char file_name[max_size], st[100][max_size], *sep;
354 float dist, len, *bestd, vec[max_size];
355 long long a, b, c, d, cn, *bi, *besti;
356 char ch;
357 knn *nbs = NULL;
358 wordlist *wl = pars->wl;
359
360 besti = malloc(N * sizeof(long long));
361 bestd = malloc(N * sizeof(float));
362
363 float worstbest=-1;
364
365 for (a = 0; a < N; a++) bestd[a] = 0;
366 a = 0;
367 bi = wl->wordi;
368 cn = wl->length;
369 sep = wl->sep;
370 b = bi[0];
371 c = 0;
372
Marc Kupietz000ad862016-02-26 14:59:12 +0100373 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100374 N = 0;
375 goto end;
376 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200377 for (a = 0; a < size; a++) vec[a] = 0;
378 for (b = 0; b < cn; b++) {
379 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100380 if(b>0 && sep[b-1] == '-')
381 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
382 else
383 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200384 }
385 len = 0;
386 for (a = 0; a < size; a++) len += vec[a] * vec[a];
387 len = sqrt(len);
388 for (a = 0; a < size; a++) vec[a] /= len;
389 for (a = 0; a < N; a++) bestd[a] = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100390 for (c = from; c < upto; c++) {
Marc Kupietzdc22b982015-10-09 09:19:34 +0200391 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100392// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100393// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
394// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200395 dist = 0;
396 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100397 if(dist > worstbest) {
398 for (a = 0; a < N; a++) {
399 if (dist > bestd[a]) {
Marc Kupietz33679a32016-03-22 08:49:39 +0100400 memmove(bestd + a + 1, bestd + a, (N - a -1) * sizeof(float));
401 memmove(besti + a + 1, besti + a, (N - a -1) * sizeof(long long));
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100402 bestd[a] = dist;
403 besti[a] = c;
404 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200405 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200406 }
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100407 worstbest = bestd[N-1];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200408 }
409 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100410
Marc Kupietz000ad862016-02-26 14:59:12 +0100411 nbs = malloc(sizeof(knn));
412 nbs->index = besti;
413 nbs->dist = bestd;
414 nbs->length = N;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100415end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100416 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200417}
418
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100419
Marc Kupietz000ad862016-02-26 14:59:12 +0100420SV *get_neighbours(char *st1, int N) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100421 HV *result = newHV();
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100422 float bestd[MAX_NEIGHBOURS], bestn[MAX_NEIGHBOURS], vec[max_size];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100423 long long besti[MAX_NEIGHBOURS], bestp[MAX_NEIGHBOURS], a, b, c, d, slice;
Marc Kupietz000ad862016-02-26 14:59:12 +0100424 char *bestw[MAX_NEIGHBOURS];
Marc Kupietz271e2a42016-03-22 11:37:43 +0100425 knn *para_nbs[MAX_THREADS];
426 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100427 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100428 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100429 wordlist *wl;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100430 int para_threads = num_threads - window * 2;
431 int syn_threads = window * 2;
432 num_threads = para_threads+syn_threads;
Marc Kupietz48c29682016-03-19 11:30:43 +0100433
Marc Kupietz000ad862016-02-26 14:59:12 +0100434 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
435
Marc Kupietz271e2a42016-03-22 11:37:43 +0100436 slice = words / syn_threads;
Marc Kupietz000ad862016-02-26 14:59:12 +0100437
Marc Kupietz48c29682016-03-19 11:30:43 +0100438 wl = getTargetWords(st1);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100439 if(wl->length < 1)
440 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100441
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++) {
451 pars[a + para_threads].wl = wl;
452 pars[a + para_threads].N = N;
453 pars[a + para_threads].from = a;
454 pars[a + para_threads].upto = a+1;
455 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
456 }
457 printf("Waiting for para threads to join\n");
458 fflush(stdout);
459 for (a = 0; a < para_threads; a++) pthread_join(pt[a], &para_nbs[a]);
460 printf("Para threads joint\n");
461 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +0100462
Marc Kupietz271e2a42016-03-22 11:37:43 +0100463 if(!syn_nbs[0])
Marc Kupietz000ad862016-02-26 14:59:12 +0100464 goto end;
465
466 for(b=0; b < N; b++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100467 besti[b] = para_nbs[0]->index[b];
468 bestd[b] = para_nbs[0]->dist[b];
Marc Kupietz000ad862016-02-26 14:59:12 +0100469 }
470
Marc Kupietz271e2a42016-03-22 11:37:43 +0100471 for(a=1; a < para_threads; a++) {
472 for(b=0; b < para_nbs[a]->length && para_nbs[a]->index[b] >= 0; b++) {
Marc Kupietz000ad862016-02-26 14:59:12 +0100473 for(c=0; c < N; c++) {
Marc Kupietz271e2a42016-03-22 11:37:43 +0100474 if(para_nbs[a]->dist[b] > bestd[c]) {
Marc Kupietz000ad862016-02-26 14:59:12 +0100475 for(d=N-1; d>c; d--) {
476 bestd[d] = bestd[d-1];
477 besti[d] = besti[d-1];
478 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100479 besti[c] = para_nbs[a]->index[b];
480 bestd[c] = para_nbs[a]->dist[b];
Marc Kupietz000ad862016-02-26 14:59:12 +0100481 break;
482 }
483 }
484 }
485 }
486
Marc Kupietz271e2a42016-03-22 11:37:43 +0100487 AV* array = newAV();
488 for (a = 0; a < N; a++) {
489 bestw[a] = (char *)malloc(max_size * sizeof(char));
490 }
491 for (a = 0; a < N; a++) {
492 strcpy(bestw[a], &vocab[besti[a] * max_w]);
493 HV* hash = newHV();
494 SV* word = newSVpvf(bestw[a], 0);
495 if(latin_enc == 0) SvUTF8_on(word);
496 hv_store(hash, "word", strlen("word"), word , 0);
497 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
498 hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0);
499 AV *vector = newAV();
500 for (b = 0; b < size; b++) {
501 av_push(vector, newSVnv(M[b + besti[a] * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100502 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100503 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
504 av_push(array, newRV_noinc((SV*)hash));
505 }
506 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
507
508 printf("Waiting for syn threads to join\n");
509 fflush(stdout);
510 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], &syn_nbs[a]);
511 printf("syn threads joint\n");
512 fflush(stdout);
513
514 for(b=0; b < N; b++) {
515 besti[b] = syn_nbs[0]->index[b];
516 bestd[b] = syn_nbs[0]->dist[b];
517 bestn[b] = syn_nbs[0]->norm[b];
518 bestp[b] = syn_nbs[0]->pos[b];
519 }
520
521
522 for(a=1; a < syn_threads; a++) {
523 for(b=0; b < N; b++) {
524 for(c=0; c < N; c++) {
525 if(syn_nbs[a]->dist[b] > bestd[c]) {
526 for(d=N-1; d>c; d--) {
527 bestd[d] = bestd[d-1];
528 besti[d] = besti[d-1];
529 bestn[d] = bestn[d-1];
530 bestp[d] = bestp[d-1];
531 }
532 besti[c] = syn_nbs[a]->index[b];
533 bestd[c] = syn_nbs[a]->dist[b];
534 bestn[c] = syn_nbs[a]->norm[b];
535 bestp[c] = syn_nbs[a]->pos[b];
536 break;
537 }
538 }
539 }
540 }
541 array = newAV();
542 for (a = 0; a < N && besti[a] >= 0; a++) {
543 strcpy(bestw[a], &vocab[besti[a] * max_w]);
544 HV* hash = newHV();
545 SV* word = newSVpvf(bestw[a], 0);
546 if(latin_enc == 0) SvUTF8_on(word);
547 hv_store(hash, "word", strlen("word"), word , 0);
548 hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0);
549 hv_store(hash, "norm", strlen("norm"), newSVnv(bestn[a]), 0);
550 hv_store(hash, "pos", strlen("pos"), newSVnv(bestp[a]), 0);
551 av_push(array, newRV_noinc((SV*)hash));
552 }
553 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz000ad862016-02-26 14:59:12 +0100554end:
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100555 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +0100556}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100557
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100558
Marc Kupietzdc22b982015-10-09 09:19:34 +0200559__DATA__
560
561@@ index.html.ep
562<!DOCTYPE html>
563<html>
Marc Kupietzc4893362016-02-25 08:04:46 +0100564<head>
565 <title>DeReKo-Word-Vector-Distances</title>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100566 <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
Marc Kupietzc4893362016-02-25 08:04:46 +0100567 <script src="http://code.jquery.com/jquery-latest.min.js"></script>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100568 <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
569 <script>
570 $(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100571 $( document ).tooltip({
572 content: function() {
573 return $(this).attr('title');
574 }}
575 )
576 })
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100577 </script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100578 <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
579 <script src="http://klinux10/word2vec/tsne.js"></script>
Marc Kupietzd7aea722016-03-02 11:59:12 +0100580 <script src="http://klinux10/word2vec/som.js"></script>
Marc Kupietzc5990da2016-02-26 08:47:12 +0100581 <script src="http://klinux10/word2vec/labeler.js"></script>
Marc Kupietzc4893362016-02-25 08:04:46 +0100582<style>
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100583body, input {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100584 font-family: Arial, sans-serif;
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100585 font-size: 11pt;
586}
587
588.ui-tooltip-content {
589 font-size: 9pt;
590 colour: #222222;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100591}
Marc Kupietz5f780672016-02-25 17:15:54 +0100592
593svg > .ui-tooltip-content {
Marc Kupietzb1029362016-02-27 21:38:55 +0100594 font-size: 8pt;
Marc Kupietz5f780672016-02-25 17:15:54 +0100595 colour: #222222;
596}
597
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100598#collocators {
599 margin-bottom: 15px;
600}
601
Marc Kupietzc4893362016-02-25 08:04:46 +0100602#wrapper {
603 width: 100%;
604// border: 1px solid red;
605 overflow: hidden; /* will contain if #first is longer than #second */
606}
607#first {
Marc Kupietzb1029362016-02-27 21:38:55 +0100608 margin-right: 20px;
609 float: left;
610 // border: 1px solid green;
Marc Kupietzc4893362016-02-25 08:04:46 +0100611}
612#second {
613 border: 1px solid #333;
614 overflow: hidden; /* if you don't want #second to wrap below #first */
615}
Marc Kupietzd7aea722016-03-02 11:59:12 +0100616#som2 svg {
617 border: 1px solid #333;
618}
619
Marc Kupietz4aa62172016-02-25 10:39:27 +0100620#cost {
Marc Kupietzb1029362016-02-27 21:38:55 +0100621 font-size: 8pt;
622 color: #222222;
623 margin-top: 4px;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100624 margin-bottom: 12px;
Marc Kupietz4aa62172016-02-25 10:39:27 +0100625}
Marc Kupietzd7aea722016-03-02 11:59:12 +0100626
Marc Kupietz6c1ca442016-03-03 09:35:18 +0100627#sominfo1, #sominfo {
Marc Kupietzd7aea722016-03-02 11:59:12 +0100628 font-size: 8pt;
629 color: #222222;
630 margin-top: 0px;
631}
632
Marc Kupietz6c1ca442016-03-03 09:35:18 +0100633#somcolor1, #somcolor2, #somcolor3 {
634 display: inline-block;
635 height: 10px;
636 width: 10px;
637}
638
Marc Kupietzd7aea722016-03-02 11:59:12 +0100639#third {
640 border: 1px solid #333;
641}
642
Marc Kupietzc4893362016-02-25 08:04:46 +0100643</style>
644<script>
645
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100646var opt = {epsilon: <%= $epsilon %>, perplexity: <%= $perplexity %>},
Marc Kupietz9fca1732016-02-29 09:07:04 +0100647 mapWidth = 800, // width map
648 mapHeight = 800,
649 jitterRadius = 7;
650
Marc Kupietzc4893362016-02-25 08:04:46 +0100651var T = new tsnejs.tSNE(opt); // create a tSNE instance
652
653var Y;
654
655var data;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100656var labeler;
Marc Kupietzc4893362016-02-25 08:04:46 +0100657
Marc Kupietzc5990da2016-02-26 08:47:12 +0100658
659function applyJitter() {
Marc Kupietzd7aea722016-03-02 11:59:12 +0100660 svg.selectAll('.tsnet')
Marc Kupietzc5990da2016-02-26 08:47:12 +0100661 .data(labels)
662 .transition()
663 .duration(50)
664 .attr("transform", function(d, i) {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100665 T.Y[i][0] = (d.x - mapWidth/2 - tx)/ss/20;
666 T.Y[i][1] = (d.y - mapHeight/2 - ty)/ss/20;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100667 return "translate(" +
668 (d.x) + "," +
669 (d.y) + ")";
670 });
671}
672
Marc Kupietzc4893362016-02-25 08:04:46 +0100673function updateEmbedding() {
674 var Y = T.getSolution();
Marc Kupietzd7aea722016-03-02 11:59:12 +0100675 svg.selectAll('.tsnet')
Marc Kupietz9fca1732016-02-29 09:07:04 +0100676 .data(data.words)
677 .attr("transform", function(d, i) {
678 return "translate(" +
679 ((Y[i][0]*20*ss + tx) + mapWidth/2) + "," +
680 ((Y[i][1]*20*ss + ty) + mapHeight/2) + ")"; });
Marc Kupietzc4893362016-02-25 08:04:46 +0100681}
682
683var svg;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100684var labels = [];
685var anchor_array = [];
686var text;
687
Marc Kupietzc4893362016-02-25 08:04:46 +0100688function drawEmbedding() {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100689 $("#embed").empty();
690 var div = d3.select("#embed");
691
692 // get min and max in each column of Y
693 var Y = T.Y;
694
695 svg = div.append("svg") // svg is global
696 .attr("width", mapWidth)
697 .attr("height", mapHeight);
698
699 var g = svg.selectAll(".b")
700 .data(data.words)
701 .enter().append("g")
Marc Kupietzd7aea722016-03-02 11:59:12 +0100702 .attr("class", "tsnet");
Marc Kupietz9fca1732016-02-29 09:07:04 +0100703
704 g.append("a")
705 .attr("xlink:href", function(word) {return "/?word="+word;})
706 .attr("title", function(d, i) {
Marc Kupietze50c8162016-03-01 10:24:43 +0100707 return "rank: "+i +" "+"freq. rank: "+data.ranks[i].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
Marc Kupietz9fca1732016-02-29 09:07:04 +0100708 })
Marc Kupietza350bce2016-02-25 09:34:25 +0100709 .append("text")
Marc Kupietz9fca1732016-02-29 09:07:04 +0100710 .attr("text-anchor", "top")
711 .attr("font-size", 12)
712 .attr("fill", function(d) {
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100713 if(data.target.indexOf(" "+d+" ") >= 0) {
Marc Kupietza350bce2016-02-25 09:34:25 +0100714 return "red";
715 } else {
716 return "#333"
Marc Kupietzc4893362016-02-25 08:04:46 +0100717 }
Marc Kupietz9fca1732016-02-29 09:07:04 +0100718 })
719 .text(function(d) { return d; });
720
721 var zoomListener = d3.behavior.zoom()
722 .scaleExtent([0.1, 10])
723 .center([0,0])
724 .on("zoom", zoomHandler);
725 zoomListener(svg);
726}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100727
Marc Kupietz9fca1732016-02-29 09:07:04 +0100728var tx=0, ty=0;
729var ss=1;
730var iter_id=-1;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100731
Marc Kupietz9fca1732016-02-29 09:07:04 +0100732function zoomHandler() {
733 tx = d3.event.translate[0];
734 ty = d3.event.translate[1];
735 ss = d3.event.scale;
736 updateEmbedding();
737}
738
739var stepnum = 0;
740
741function stopStep() {
742 clearInterval(iter_id);
743 text = svg.selectAll("text");
744
745 // jitter function needs different data and co-ordinate representation
746 labels = d3.range(data.words.length).map(function(i) {
747 var x = (T.Y[i][0]*20*ss + tx) + mapWidth/2;
748 var y = (T.Y[i][1]*20*ss + ty) + mapHeight/2;
749 anchor_array.push({x: x, y: y, r: jitterRadius});
750 return {
751 x: x,
752 y: y,
753 name: data.words[i]
754 };
755 });
756
757 // get the actual label bounding boxes for the jitter function
758 var index = 0;
759 text.each(function() {
760 labels[index].width = this.getBBox().width;
761 labels[index].height = this.getBBox().height;
762 index += 1;
763 });
Marc Kupietzc5990da2016-02-26 08:47:12 +0100764
765
766// setTimeout(updateEmbedding, 1);
767// setTimeout(
Marc Kupietz9fca1732016-02-29 09:07:04 +0100768 labeler = d3.labeler()
769 .label(labels)
770 .anchor(anchor_array)
771 .width(mapWidth)
772 .height(mapHeight)
773 .update(applyJitter);
774 // .start(1000);
Marc Kupietzc5990da2016-02-26 08:47:12 +0100775
Marc Kupietz9fca1732016-02-29 09:07:04 +0100776 iter_id = setInterval(jitterStep, 1);
777}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100778
Marc Kupietz9fca1732016-02-29 09:07:04 +0100779var jitter_i=0;
Marc Kupietzc5990da2016-02-26 08:47:12 +0100780
781function jitterStep() {
Marc Kupietz9fca1732016-02-29 09:07:04 +0100782 if(jitter_i++ > 100) {
783 clearInterval(iter_id);
784 } else {
785 labeler.start2(10);
786 applyJitter();
787 }
Marc Kupietzc5990da2016-02-26 08:47:12 +0100788}
Marc Kupietzb1029362016-02-27 21:38:55 +0100789
790var last_cost=1000;
791
Marc Kupietz9fca1732016-02-29 09:07:04 +0100792function step() {
793 var i = T.iter;
794
795 if(i > <%= $no_iterations %>) {
796 stopStep();
797 } else {
798 var cost = Math.round(T.step() * 100000) / 100000; // do a few steps
799 $("#cost").html("tsne iteration " + i + ", cost: " + cost.toFixed(5));
800 if(i % 250 == 0 && cost >= last_cost) {
801 stopStep();
802 } else {
803 last_cost = cost;
804 updateEmbedding();
805 }
806 }
807}
Marc Kupietzc5990da2016-02-26 08:47:12 +0100808
Marc Kupietz9fca1732016-02-29 09:07:04 +0100809function showMap(j) {
810 data=j;
811 T.iter=0;
812 T.initDataRaw(data.vecs); // init embedding
813 drawEmbedding(); // draw initial embedding
814
815 if(iter_id >= 0) {
816 clearInterval(iter_id);
817 }
818 //T.debugGrad();
819 iter_id = setInterval(step, 1);
Marc Kupietzd7aea722016-03-02 11:59:12 +0100820 if(<%= $show_som %>) {
821 makeSOM(j, <%= $no_iterations %>);
822 }
Marc Kupietz9fca1732016-02-29 09:07:04 +0100823}
Marc Kupietza350bce2016-02-25 09:34:25 +0100824
Marc Kupietzc4893362016-02-25 08:04:46 +0100825</script>
826</head>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200827<body>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100828 <form action="<%=url_for('/')->to_abs%>" method="GET">
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100829 word(s):
830 <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.">
831 max. neighbours: <input type="text" size="8" name="n" value="<%= $no_nbs %>">
Marc Kupietzd7aea722016-03-02 11:59:12 +0100832 max. iterations: <input type="text" name="N" size="8" value="<%= $no_iterations %>">
833 SOM <input type="checkbox" name="som" value="1" <%= ($show_som ? "checked" : "") %>>
834 <span> </span><input type="submit" value="Show">
Marc Kupietz4aa62172016-02-25 10:39:27 +0100835 </form>
836 <br>
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100837 % if($lists) {
Marc Kupietz4aa62172016-02-25 10:39:27 +0100838 <div id="wrapper">
839 <table id="first">
840 <tr>
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100841 <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="left">syntagmatic</th>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100842 </tr>
Marc Kupietz5f780672016-02-25 17:15:54 +0100843 % my $j=0; my @words; my @vecs; my @ranks; for my $list (@$lists) {
Marc Kupietzd5642582016-03-19 22:23:13 +0100844 % my $i=0; for my $item (@$list) {
845 % my $c = (@$collocators)[$i];
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100846 % if(!grep{$_ eq $item->{word}} @words) {
847 % push @vecs, $item->{vector};
848 % push @words, $item->{word};
Marc Kupietz5f780672016-02-25 17:15:54 +0100849 % push @ranks, $item->{rank};
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100850 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100851 <tr>
852 <td align="right">
Marc Kupietzd5642582016-03-19 22:23:13 +0100853 <%= ++$i %>.
Marc Kupietz4aa62172016-02-25 10:39:27 +0100854 </td>
855 <td align="right">
856 <%= sprintf("%.3f", $item->{dist}) %>
857 </td>
Marc Kupietzd5642582016-03-19 22:23:13 +0100858 <td>
859 <a title="freq. rank: <%= $item->{rank} %>" href="/?word=<%= $item->{word} %>">
860 <%= $item->{word} %>
861 </a>
862 </td>
Marc Kupietz271e2a42016-03-22 11:37:43 +0100863 % if($c) {
Marc Kupietz5f780672016-02-25 17:15:54 +0100864 <td align="right">
Marc Kupietzd5642582016-03-19 22:23:13 +0100865 <%= $c->{pos} %>:
866 </td>
867 <td align="right">
868 <%= sprintf("%.3f", $c->{dist}) %>
869 </td>
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100870 <td align="right">
871 <%= sprintf("%.3e", $c->{norm}) %>
872 </td>
Marc Kupietzd5642582016-03-19 22:23:13 +0100873 <td align="left">
874 <a href="/?word=<%= $c->{word} %>">
875 <%= $c->{word} %>
Marc Kupietz5f780672016-02-25 17:15:54 +0100876 </td>
Marc Kupietz271e2a42016-03-22 11:37:43 +0100877 % } else {
878 <td colspan="4"/>
879 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100880 </tr>
881 % }
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100882 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100883 </table>
884 <script>
885 % use Mojo::ByteStream 'b';
886 $(window).load(function() {
Marc Kupietz5f780672016-02-25 17:15:54 +0100887 showMap(<%= b(Mojo::JSON::to_json({target => " $word ", words => \@words, vecs => \@vecs, ranks => \@ranks})); %>);
Marc Kupietz4aa62172016-02-25 10:39:27 +0100888 });
889 </script>
890 % }
891 <div id="second" style="width:800px; height:800px; font-family: arial;">
892 <div id="embed">
893 </div>
Marc Kupietz4aa62172016-02-25 10:39:27 +0100894 </div>
Marc Kupietzb1029362016-02-27 21:38:55 +0100895 <div id="cost"></div>
Marc Kupietzd7aea722016-03-02 11:59:12 +0100896 % if($show_som) {
897 <div id="som2">
898 </div>
Marc Kupietz6c1ca442016-03-03 09:35:18 +0100899 <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 +0100900 <div id="sominfo">SOM iteration <span id="iterations">0</span></div>
901 % }
Marc Kupietz4aa62172016-02-25 10:39:27 +0100902 </div>
903 <p>
904 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 +0200905 <pre>
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100906-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 +0100907 </pre>
908 </p>
909</body>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200910</html>
911