Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 1 | #!/usr/local/bin/perl |
| 2 | use Inline C; |
| 3 | use Mojolicious::Lite; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 4 | use Mojo::JSON qw(decode_json encode_json to_json); |
Marc Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 5 | use Encode qw(decode encode); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 6 | use Getopt::Std; |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 7 | use Mojo::Server::Daemon; |
Marc Kupietz | d422739 | 2016-03-01 16:45:12 +0100 | [diff] [blame] | 8 | plugin 'Log::Access'; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 9 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 10 | our $opt_i = 0; # latin1-input? |
| 11 | our $opt_l = undef; |
| 12 | our $opt_p = 5676; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 13 | our $opt_n = undef; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 14 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 15 | getopt('il:p:n:'); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 16 | |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 17 | # -cbow 1 -size 200 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 40 -binary 1 -iter 15 |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 18 | if(!$ARGV[0]) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 19 | init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0)); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 20 | } else { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 21 | init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0)); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 22 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 23 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 24 | my $daemon = Mojo::Server::Daemon->new( |
| 25 | app => app, |
| 26 | listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"] |
| 27 | ); |
| 28 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 29 | get '/' => sub { |
| 30 | my $c = shift; |
| 31 | my $word=$c->param('word'); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 32 | my $no_nbs=$c->param('n') || 100; |
| 33 | my $no_iterations=$c->param('N') || 2000; |
Marc Kupietz | d422739 | 2016-03-01 16:45:12 +0100 | [diff] [blame] | 34 | my $perplexity=$c->param('perplexity') || 20; |
Marc Kupietz | c4d62f8 | 2016-03-01 11:04:24 +0100 | [diff] [blame] | 35 | my $epsilon=$c->param('epsilon') || 5; |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 36 | my $som=$c->param('som') || 0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 37 | my $res; |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 38 | my @lists; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 39 | my @collocations; |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 40 | if(defined($word) && $word !~ /^\s*$/) { |
| 41 | $c->inactivity_timeout(300); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 42 | $word =~ s/\s+/ /g; |
| 43 | for my $w (split(' *\| *', $word)) { |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 44 | $c->app->log->debug('Looking for neighbours of '.$w); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 45 | if($opt_i) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 46 | $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 47 | } else { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 48 | $res = get_neighbours($w, $no_nbs); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 49 | } |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 50 | push(@lists, $res->{paradigmatic}); |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 51 | } |
Marc Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 52 | } |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 53 | $word =~ s/ *\| */ | /g; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 54 | $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 Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 57 | $daemon->run; # app->start; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 58 | |
| 59 | exit; |
| 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 Kupietz | f080976 | 2016-02-26 10:13:47 +0100 | [diff] [blame] | 69 | #include <sys/mman.h> |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 70 | #include <pthread.h> |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 71 | |
| 72 | #define max_size 2000 |
| 73 | #define max_w 50 |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 74 | #define MAX_NEIGHBOURS 1000 |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 75 | #define MAX_WORDS -1 |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 76 | #define MAX_THREADS 100 |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 77 | #define MAX_CC 50 |
| 78 | #define EXP_TABLE_SIZE 1000 |
| 79 | #define MAX_EXP 6 |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 80 | |
| 81 | //the thread function |
| 82 | void *connection_handler(void *); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 83 | |
| 84 | typedef struct { |
| 85 | long long *index; |
| 86 | float *dist; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 87 | long long *pos; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 88 | unsigned int length; |
| 89 | } knn; |
| 90 | |
| 91 | |
| 92 | typedef struct { |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame^] | 93 | long long wordi[MAX_NEIGHBOURS]; |
| 94 | char sep[MAX_NEIGHBOURS]; |
| 95 | int length; |
| 96 | } wordlist; |
| 97 | |
| 98 | typedef struct { |
| 99 | wordlist *wl; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 100 | char *token; |
| 101 | int N; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 102 | long from; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 103 | unsigned long upto; |
| 104 | } knnpars; |
| 105 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 106 | float *M, *syn1neg_window, *expTable; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 107 | char *vocab; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 108 | |
Marc Kupietz | 82b0267 | 2016-02-26 12:32:25 +0100 | [diff] [blame] | 109 | long long words, size; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 110 | int num_threads=20; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 111 | int latin_enc=0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 112 | int window; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 113 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 114 | int init_net(char *file_name, char *net_name, int latin) { |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 115 | FILE *f, *binvecs, *binwords; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 116 | int binwords_fd, binvecs_fd, net_fd, i; |
Marc Kupietz | 82b0267 | 2016-02-26 12:32:25 +0100 | [diff] [blame] | 117 | long long a, b, c, d, cn; |
| 118 | float len; |
| 119 | |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 120 | char binvecs_fname[256], binwords_fname[256]; |
| 121 | strcpy(binwords_fname, file_name); |
| 122 | strcat(binwords_fname, ".words"); |
| 123 | strcpy(binvecs_fname, file_name); |
| 124 | strcat(binvecs_fname, ".vecs"); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 125 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 126 | latin_enc = latin; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 127 | f = fopen(file_name, "rb"); |
| 128 | if (f == NULL) { |
| 129 | printf("Input file %s not found\n", file_name); |
| 130 | return -1; |
| 131 | } |
| 132 | fscanf(f, "%lld", &words); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 133 | if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 134 | fscanf(f, "%lld", &size); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 135 | if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) { |
| 136 | printf("Converting %s to memory mappable structures\n", file_name); |
Marc Kupietz | f080976 | 2016-02-26 10:13:47 +0100 | [diff] [blame] | 137 | vocab = (char *)malloc((long long)words * max_w * sizeof(char)); |
| 138 | M = (float *)malloc((long long)words * (long long)size * sizeof(float)); |
| 139 | if (M == NULL) { |
| 140 | printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size); |
| 141 | return -1; |
| 142 | } |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 143 | for (b = 0; b < words; b++) { |
| 144 | a = 0; |
| 145 | while (1) { |
| 146 | vocab[b * max_w + a] = fgetc(f); |
| 147 | if (feof(f) || (vocab[b * max_w + a] == ' ')) break; |
| 148 | if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++; |
| 149 | } |
| 150 | vocab[b * max_w + a] = 0; |
| 151 | fread(&M[b * size], sizeof(float), size, f); |
| 152 | len = 0; |
| 153 | for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size]; |
| 154 | len = sqrt(len); |
| 155 | for (a = 0; a < size; a++) M[a + b * size] /= len; |
| 156 | } |
| 157 | if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) { |
| 158 | fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs); |
| 159 | fclose(binvecs); |
| 160 | fwrite(vocab, sizeof(char), (long long)words * max_w, binwords); |
| 161 | fclose(binwords); |
| 162 | } |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 163 | } |
| 164 | if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) { |
| 165 | M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0); |
| 166 | vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0); |
| 167 | if (M == MAP_FAILED || vocab == MAP_FAILED) { |
| 168 | close(binvecs_fd); |
| 169 | close(binwords_fd); |
| 170 | fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname); |
| 171 | exit(-1); |
| 172 | } |
| 173 | } else { |
| 174 | fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname); |
| 175 | exit(-1); |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 176 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 177 | fclose(f); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 178 | |
| 179 | if(net_name) { |
| 180 | if( (net_fd = open(net_name, O_RDONLY)) >= 0) { |
| 181 | window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2; |
| 182 | // lseek(net_fd, sizeof(float) * words * size, SEEK_SET); |
| 183 | munmap(M, sizeof(float) * words * size); |
| 184 | M = mmap(0, sizeof(float) * words * size + sizeof(float) * 2 * window * size * words, PROT_READ, MAP_SHARED, net_fd, 0); |
| 185 | if (M == MAP_FAILED) { |
| 186 | close(net_fd); |
| 187 | fprintf(stderr, "Cannot mmap %s\n", net_name); |
| 188 | exit(-1); |
| 189 | } |
| 190 | syn1neg_window = M + words * size; |
| 191 | } else { |
| 192 | fprintf(stderr, "Cannot open %s\n", net_name); |
| 193 | exit(-1); |
| 194 | } |
| 195 | fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window); |
| 196 | } |
| 197 | |
| 198 | expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float)); |
| 199 | for (i = 0; i < EXP_TABLE_SIZE; i++) { |
| 200 | expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table |
| 201 | expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1) |
| 202 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 203 | return 0; |
| 204 | } |
| 205 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 206 | knn *getCollocators(int cc) { |
| 207 | knn *nbs = NULL; |
| 208 | long window_layer_size = size * window * 2; |
| 209 | long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target; |
| 210 | float f, max_f, maxmax_f; |
| 211 | float *target_sums, *bestf, worstbest; |
| 212 | long long *besti, *bestp; |
| 213 | int N = 10; |
| 214 | a = posix_memalign((void **) &target_sums, 128, words * sizeof(float)); |
| 215 | besti = malloc(N * sizeof(long long)); |
| 216 | bestp = malloc(N * sizeof(long long)); |
| 217 | bestf = malloc(N * sizeof(float)); |
| 218 | for (b = 0; b < words; b++) |
| 219 | target_sums[b]=0; |
| 220 | for (b = 0; b < N; b++) |
| 221 | bestf[b]=-1; |
| 222 | worstbest = -1; |
| 223 | d = cc; |
| 224 | maxmax_f = -1; |
| 225 | maxmax_target = 0; |
| 226 | |
| 227 | |
| 228 | for (a = window * 2 + 1; a >=0; a--) { |
| 229 | printf("window pos: %ld\n", a); |
| 230 | if (a != window) { |
| 231 | max_f = -1; |
| 232 | window_offset = a * size; |
| 233 | if (a > window) |
| 234 | window_offset -= size; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame^] | 235 | for(target = 0; target < words / 2; target ++) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 236 | if(target == d) |
| 237 | continue; |
| 238 | f = 0; |
| 239 | for (c = 0; c < size; c++) |
| 240 | f += M[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c]; |
| 241 | if (f < -MAX_EXP) |
| 242 | continue; |
| 243 | else if (f > MAX_EXP) |
| 244 | continue; |
| 245 | else |
| 246 | f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))]; |
| 247 | if(f > max_f) { |
| 248 | max_f = f; |
| 249 | max_target = target; |
| 250 | } |
| 251 | target_sums[target] += (1-target_sums[target]) * f; |
| 252 | if(f > worstbest) { |
| 253 | for (b = 0; b < N; b++) { |
| 254 | if (f > bestf[b]) { |
| 255 | for (e = N - 1; e > b; e--) { |
| 256 | bestf[e] = bestf[e - 1]; |
| 257 | besti[e] = besti[e - 1]; |
| 258 | bestp[e] = bestp[e - 1]; |
| 259 | } |
| 260 | bestf[b] = f; |
| 261 | besti[b] = target; |
| 262 | bestp[b] = window-a; |
| 263 | break; |
| 264 | } |
| 265 | } |
| 266 | worstbest = bestf[N-1]; |
| 267 | } |
| 268 | } |
| 269 | printf("%d %.2f\n", max_target, max_f); |
| 270 | printf("%s (%.2f) ", &vocab[max_target * max_w], max_f); |
| 271 | if(max_f > maxmax_f) { |
| 272 | maxmax_f = max_f; |
| 273 | maxmax_target = max_target; |
| 274 | } |
| 275 | } else { |
| 276 | printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]); |
| 277 | } |
| 278 | } |
| 279 | max_f = -1; |
| 280 | for (b = 0; b < words; b++) { |
| 281 | if(target_sums[b] > max_f) { |
| 282 | max_f = target_sums[b]; |
| 283 | max_target = b; |
| 284 | } |
| 285 | } |
| 286 | printf(" -- max sum: %s (%.2f), max resp.: \x1b[1m%s\x1b[0m (%.2f)\n", |
| 287 | &vocab[max_target * max_w], max_f, |
| 288 | &vocab[maxmax_target * max_w], maxmax_f); |
| 289 | for(b=0; b<N && bestf[b]>-1; b++) |
| 290 | printf("%-32s %.2f %d\n", &vocab[besti[b]*max_w], bestf[b], bestp[b]); |
| 291 | printf("\n"); |
| 292 | free(target_sums); |
| 293 | nbs = malloc(sizeof(knn)); |
| 294 | nbs->index = besti; |
| 295 | nbs->dist = bestf; |
| 296 | nbs->pos = bestp; |
| 297 | nbs->length = N; |
| 298 | return(nbs); |
| 299 | } |
| 300 | |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame^] | 301 | wordlist *getTargetWords(char *st1) { |
| 302 | wordlist *wl = malloc(sizeof(wordlist)); |
| 303 | char st[100][max_size], sep[100]; |
| 304 | long a, b=0, c=0, cn=0; |
| 305 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 306 | while (1) { |
| 307 | st[cn][b] = st1[c]; |
| 308 | b++; |
| 309 | c++; |
| 310 | st[cn][b] = 0; |
| 311 | if (st1[c] == 0) break; |
Marc Kupietz | 95aa1c0 | 2016-03-15 09:40:43 +0100 | [diff] [blame] | 312 | if (st1[c] == ' ' || st1[c] == '-') { |
| 313 | sep[cn++] = st1[c]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 314 | b = 0; |
| 315 | c++; |
| 316 | } |
| 317 | } |
| 318 | cn++; |
| 319 | for (a = 0; a < cn; a++) { |
Marc Kupietz | 34a3ee9 | 2016-02-27 22:43:16 +0100 | [diff] [blame] | 320 | for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break; |
| 321 | if (b == words) b = -1; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame^] | 322 | wl->wordi[a] = b; |
| 323 | fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], wl->wordi[a]); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 324 | if (b == -1) { |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 325 | fprintf(stderr, "Out of dictionary word!\n"); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 326 | cn--; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 327 | break; |
| 328 | } |
| 329 | } |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame^] | 330 | wl->length=cn; |
| 331 | return(wl); |
| 332 | } |
| 333 | |
| 334 | void *_get_neighbours(knnpars *pars) { |
| 335 | char *st1 = pars->token; |
| 336 | int N = pars->N; |
| 337 | long from = pars -> from; |
| 338 | unsigned long upto = pars -> upto; |
| 339 | char file_name[max_size], st[100][max_size], *sep; |
| 340 | float dist, len, *bestd, vec[max_size]; |
| 341 | long long a, b, c, d, cn, *bi, *besti; |
| 342 | char ch; |
| 343 | knn *nbs = NULL; |
| 344 | wordlist *wl = pars->wl; |
| 345 | |
| 346 | besti = malloc(N * sizeof(long long)); |
| 347 | bestd = malloc(N * sizeof(float)); |
| 348 | |
| 349 | float worstbest=-1; |
| 350 | |
| 351 | for (a = 0; a < N; a++) bestd[a] = 0; |
| 352 | a = 0; |
| 353 | bi = wl->wordi; |
| 354 | cn = wl->length; |
| 355 | sep = wl->sep; |
| 356 | b = bi[0]; |
| 357 | c = 0; |
| 358 | |
| 359 | if(from < 0) { |
| 360 | nbs = getCollocators(b); |
| 361 | pthread_exit(nbs); |
| 362 | } |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 363 | if (b == -1) { |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 364 | N = 0; |
| 365 | goto end; |
| 366 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 367 | for (a = 0; a < size; a++) vec[a] = 0; |
| 368 | for (b = 0; b < cn; b++) { |
| 369 | if (bi[b] == -1) continue; |
Marc Kupietz | 95aa1c0 | 2016-03-15 09:40:43 +0100 | [diff] [blame] | 370 | if(b>0 && sep[b-1] == '-') |
| 371 | for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size]; |
| 372 | else |
| 373 | for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 374 | } |
| 375 | len = 0; |
| 376 | for (a = 0; a < size; a++) len += vec[a] * vec[a]; |
| 377 | len = sqrt(len); |
| 378 | for (a = 0; a < size; a++) vec[a] /= len; |
| 379 | for (a = 0; a < N; a++) bestd[a] = -1; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 380 | for (c = from; c < upto; c++) { |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 381 | a = 0; |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 382 | // do not skip taget word |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 383 | // for (b = 0; b < cn; b++) if (bi[b] == c) a = 1; |
| 384 | // if (a == 1) continue; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 385 | dist = 0; |
| 386 | for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size]; |
Marc Kupietz | be1b9fc | 2016-02-26 10:34:30 +0100 | [diff] [blame] | 387 | if(dist > worstbest) { |
| 388 | for (a = 0; a < N; a++) { |
| 389 | if (dist > bestd[a]) { |
| 390 | for (d = N - 1; d > a; d--) { |
| 391 | bestd[d] = bestd[d - 1]; |
| 392 | besti[d] = besti[d - 1]; |
| 393 | } |
| 394 | bestd[a] = dist; |
| 395 | besti[a] = c; |
| 396 | break; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 397 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 398 | } |
Marc Kupietz | be1b9fc | 2016-02-26 10:34:30 +0100 | [diff] [blame] | 399 | worstbest = bestd[N-1]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 400 | } |
| 401 | } |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 402 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 403 | nbs = malloc(sizeof(knn)); |
| 404 | nbs->index = besti; |
| 405 | nbs->dist = bestd; |
| 406 | nbs->length = N; |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 407 | end: |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 408 | pthread_exit(nbs); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 409 | } |
| 410 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 411 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 412 | SV *get_neighbours(char *st1, int N) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 413 | HV *result = newHV(); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 414 | float bestd[MAX_NEIGHBOURS], vec[max_size]; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 415 | long long besti[MAX_NEIGHBOURS], bestp[MAX_NEIGHBOURS], a, b, c, d, slice; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 416 | char *bestw[MAX_NEIGHBOURS]; |
| 417 | knn *nbs[MAX_THREADS]; |
| 418 | knnpars pars[MAX_THREADS]; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 419 | pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t)); |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame^] | 420 | wordlist *wl; |
| 421 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 422 | if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS; |
| 423 | |
| 424 | slice = words / num_threads; |
| 425 | |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame^] | 426 | wl = getTargetWords(st1); |
| 427 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 428 | a = num_threads; |
| 429 | pars[a].token = st1; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame^] | 430 | pars[a].wl = wl; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 431 | pars[a].N = N; |
| 432 | pars[a].from = -1; |
| 433 | pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]); |
| 434 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 435 | for(a=0; a < num_threads; a++) { |
| 436 | pars[a].token = st1; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame^] | 437 | pars[a].wl = wl; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 438 | pars[a].N = N; |
| 439 | pars[a].from = a*slice; |
| 440 | pars[a].upto = ((a+1)*slice > words? words:(a+1)*slice); |
| 441 | pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]); |
| 442 | } |
| 443 | for (a = 0; a < num_threads; a++) pthread_join(pt[a], &nbs[a]); |
| 444 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 445 | pthread_join(pt[a], &nbs[a]); |
| 446 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 447 | if(!nbs[0]) |
| 448 | goto end; |
| 449 | |
| 450 | for(b=0; b < N; b++) { |
| 451 | besti[b] = nbs[0]->index[b]; |
| 452 | bestd[b] = nbs[0]->dist[b]; |
| 453 | } |
| 454 | |
| 455 | for(a=1; a < num_threads; a++) { |
| 456 | for(b=0; b < N; b++) { |
| 457 | for(c=0; c < N; c++) { |
| 458 | if(nbs[a]->dist[b] > bestd[c]) { |
| 459 | for(d=N-1; d>c; d--) { |
| 460 | bestd[d] = bestd[d-1]; |
| 461 | besti[d] = besti[d-1]; |
| 462 | } |
| 463 | besti[c] = nbs[a]->index[b]; |
| 464 | bestd[c] = nbs[a]->dist[b]; |
| 465 | break; |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 471 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 472 | if(nbs) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 473 | AV* array = newAV(); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 474 | for (a = 0; a < N; a++) { |
| 475 | bestw[a] = (char *)malloc(max_size * sizeof(char)); |
| 476 | } |
| 477 | for (a = 0; a < N; a++) { |
| 478 | strcpy(bestw[a], &vocab[besti[a] * max_w]); |
| 479 | HV* hash = newHV(); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 480 | SV* word = newSVpvf(bestw[a], 0); |
| 481 | if(latin_enc == 0) SvUTF8_on(word); |
| 482 | hv_store(hash, "word", strlen("word"), word , 0); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 483 | hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0); |
| 484 | hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0); |
| 485 | AV *vector = newAV(); |
| 486 | for (b = 0; b < size; b++) { |
| 487 | av_push(vector, newSVnv(M[b + besti[a] * size])); |
| 488 | } |
| 489 | hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0); |
| 490 | av_push(array, newRV_noinc((SV*)hash)); |
| 491 | } |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 492 | hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0); |
| 493 | |
| 494 | for(b=0; b < 10; b++) { |
| 495 | besti[b] = nbs[num_threads]->index[b]; |
| 496 | bestd[b] = nbs[num_threads]->dist[b]; |
| 497 | bestp[b] = nbs[num_threads]->pos[b]; |
| 498 | } |
| 499 | array = newAV(); |
| 500 | for (a = 0; a < 10; a++) { |
| 501 | strcpy(bestw[a], &vocab[besti[a] * max_w]); |
| 502 | HV* hash = newHV(); |
| 503 | SV* word = newSVpvf(bestw[a], 0); |
| 504 | if(latin_enc == 0) SvUTF8_on(word); |
| 505 | hv_store(hash, "word", strlen("word"), word , 0); |
| 506 | hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0); |
| 507 | hv_store(hash, "pos", strlen("pos"), newSVnv(bestp[a]), 0); |
| 508 | av_push(array, newRV_noinc((SV*)hash)); |
| 509 | } |
| 510 | hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 511 | } |
| 512 | end: |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 513 | return newRV_noinc((SV*)result); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 514 | } |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 515 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 516 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 517 | __DATA__ |
| 518 | |
| 519 | @@ index.html.ep |
| 520 | <!DOCTYPE html> |
| 521 | <html> |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 522 | <head> |
| 523 | <title>DeReKo-Word-Vector-Distances</title> |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 524 | <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 525 | <script src="http://code.jquery.com/jquery-latest.min.js"></script> |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 526 | <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> |
| 527 | <script> |
| 528 | $(function() { |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 529 | $( document ).tooltip({ |
| 530 | content: function() { |
| 531 | return $(this).attr('title'); |
| 532 | }} |
| 533 | ) |
| 534 | }) |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 535 | </script> |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 536 | <script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script> |
| 537 | <script src="http://klinux10/word2vec/tsne.js"></script> |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 538 | <script src="http://klinux10/word2vec/som.js"></script> |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 539 | <script src="http://klinux10/word2vec/labeler.js"></script> |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 540 | <style> |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 541 | body, input { |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 542 | font-family: Arial, sans-serif; |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 543 | font-size: 11pt; |
| 544 | } |
| 545 | |
| 546 | .ui-tooltip-content { |
| 547 | font-size: 9pt; |
| 548 | colour: #222222; |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 549 | } |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 550 | |
| 551 | svg > .ui-tooltip-content { |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 552 | font-size: 8pt; |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 553 | colour: #222222; |
| 554 | } |
| 555 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 556 | #collocators { |
| 557 | margin-bottom: 15px; |
| 558 | } |
| 559 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 560 | #wrapper { |
| 561 | width: 100%; |
| 562 | // border: 1px solid red; |
| 563 | overflow: hidden; /* will contain if #first is longer than #second */ |
| 564 | } |
| 565 | #first { |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 566 | margin-right: 20px; |
| 567 | float: left; |
| 568 | // border: 1px solid green; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 569 | } |
| 570 | #second { |
| 571 | border: 1px solid #333; |
| 572 | overflow: hidden; /* if you don't want #second to wrap below #first */ |
| 573 | } |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 574 | #som2 svg { |
| 575 | border: 1px solid #333; |
| 576 | } |
| 577 | |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 578 | #cost { |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 579 | font-size: 8pt; |
| 580 | color: #222222; |
| 581 | margin-top: 4px; |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 582 | margin-bottom: 12px; |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 583 | } |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 584 | |
Marc Kupietz | 6c1ca44 | 2016-03-03 09:35:18 +0100 | [diff] [blame] | 585 | #sominfo1, #sominfo { |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 586 | font-size: 8pt; |
| 587 | color: #222222; |
| 588 | margin-top: 0px; |
| 589 | } |
| 590 | |
Marc Kupietz | 6c1ca44 | 2016-03-03 09:35:18 +0100 | [diff] [blame] | 591 | #somcolor1, #somcolor2, #somcolor3 { |
| 592 | display: inline-block; |
| 593 | height: 10px; |
| 594 | width: 10px; |
| 595 | } |
| 596 | |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 597 | #third { |
| 598 | border: 1px solid #333; |
| 599 | } |
| 600 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 601 | </style> |
| 602 | <script> |
| 603 | |
Marc Kupietz | c4d62f8 | 2016-03-01 11:04:24 +0100 | [diff] [blame] | 604 | var opt = {epsilon: <%= $epsilon %>, perplexity: <%= $perplexity %>}, |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 605 | mapWidth = 800, // width map |
| 606 | mapHeight = 800, |
| 607 | jitterRadius = 7; |
| 608 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 609 | var T = new tsnejs.tSNE(opt); // create a tSNE instance |
| 610 | |
| 611 | var Y; |
| 612 | |
| 613 | var data; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 614 | var labeler; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 615 | |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 616 | |
| 617 | function applyJitter() { |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 618 | svg.selectAll('.tsnet') |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 619 | .data(labels) |
| 620 | .transition() |
| 621 | .duration(50) |
| 622 | .attr("transform", function(d, i) { |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 623 | T.Y[i][0] = (d.x - mapWidth/2 - tx)/ss/20; |
| 624 | T.Y[i][1] = (d.y - mapHeight/2 - ty)/ss/20; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 625 | return "translate(" + |
| 626 | (d.x) + "," + |
| 627 | (d.y) + ")"; |
| 628 | }); |
| 629 | } |
| 630 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 631 | function updateEmbedding() { |
| 632 | var Y = T.getSolution(); |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 633 | svg.selectAll('.tsnet') |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 634 | .data(data.words) |
| 635 | .attr("transform", function(d, i) { |
| 636 | return "translate(" + |
| 637 | ((Y[i][0]*20*ss + tx) + mapWidth/2) + "," + |
| 638 | ((Y[i][1]*20*ss + ty) + mapHeight/2) + ")"; }); |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | var svg; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 642 | var labels = []; |
| 643 | var anchor_array = []; |
| 644 | var text; |
| 645 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 646 | function drawEmbedding() { |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 647 | $("#embed").empty(); |
| 648 | var div = d3.select("#embed"); |
| 649 | |
| 650 | // get min and max in each column of Y |
| 651 | var Y = T.Y; |
| 652 | |
| 653 | svg = div.append("svg") // svg is global |
| 654 | .attr("width", mapWidth) |
| 655 | .attr("height", mapHeight); |
| 656 | |
| 657 | var g = svg.selectAll(".b") |
| 658 | .data(data.words) |
| 659 | .enter().append("g") |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 660 | .attr("class", "tsnet"); |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 661 | |
| 662 | g.append("a") |
| 663 | .attr("xlink:href", function(word) {return "/?word="+word;}) |
| 664 | .attr("title", function(d, i) { |
Marc Kupietz | e50c816 | 2016-03-01 10:24:43 +0100 | [diff] [blame] | 665 | return "rank: "+i +" "+"freq. rank: "+data.ranks[i].toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 666 | }) |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 667 | .append("text") |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 668 | .attr("text-anchor", "top") |
| 669 | .attr("font-size", 12) |
| 670 | .attr("fill", function(d) { |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 671 | if(data.target.indexOf(" "+d+" ") >= 0) { |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 672 | return "red"; |
| 673 | } else { |
| 674 | return "#333" |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 675 | } |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 676 | }) |
| 677 | .text(function(d) { return d; }); |
| 678 | |
| 679 | var zoomListener = d3.behavior.zoom() |
| 680 | .scaleExtent([0.1, 10]) |
| 681 | .center([0,0]) |
| 682 | .on("zoom", zoomHandler); |
| 683 | zoomListener(svg); |
| 684 | } |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 685 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 686 | var tx=0, ty=0; |
| 687 | var ss=1; |
| 688 | var iter_id=-1; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 689 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 690 | function zoomHandler() { |
| 691 | tx = d3.event.translate[0]; |
| 692 | ty = d3.event.translate[1]; |
| 693 | ss = d3.event.scale; |
| 694 | updateEmbedding(); |
| 695 | } |
| 696 | |
| 697 | var stepnum = 0; |
| 698 | |
| 699 | function stopStep() { |
| 700 | clearInterval(iter_id); |
| 701 | text = svg.selectAll("text"); |
| 702 | |
| 703 | // jitter function needs different data and co-ordinate representation |
| 704 | labels = d3.range(data.words.length).map(function(i) { |
| 705 | var x = (T.Y[i][0]*20*ss + tx) + mapWidth/2; |
| 706 | var y = (T.Y[i][1]*20*ss + ty) + mapHeight/2; |
| 707 | anchor_array.push({x: x, y: y, r: jitterRadius}); |
| 708 | return { |
| 709 | x: x, |
| 710 | y: y, |
| 711 | name: data.words[i] |
| 712 | }; |
| 713 | }); |
| 714 | |
| 715 | // get the actual label bounding boxes for the jitter function |
| 716 | var index = 0; |
| 717 | text.each(function() { |
| 718 | labels[index].width = this.getBBox().width; |
| 719 | labels[index].height = this.getBBox().height; |
| 720 | index += 1; |
| 721 | }); |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 722 | |
| 723 | |
| 724 | // setTimeout(updateEmbedding, 1); |
| 725 | // setTimeout( |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 726 | labeler = d3.labeler() |
| 727 | .label(labels) |
| 728 | .anchor(anchor_array) |
| 729 | .width(mapWidth) |
| 730 | .height(mapHeight) |
| 731 | .update(applyJitter); |
| 732 | // .start(1000); |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 733 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 734 | iter_id = setInterval(jitterStep, 1); |
| 735 | } |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 736 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 737 | var jitter_i=0; |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 738 | |
| 739 | function jitterStep() { |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 740 | if(jitter_i++ > 100) { |
| 741 | clearInterval(iter_id); |
| 742 | } else { |
| 743 | labeler.start2(10); |
| 744 | applyJitter(); |
| 745 | } |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 746 | } |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 747 | |
| 748 | var last_cost=1000; |
| 749 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 750 | function step() { |
| 751 | var i = T.iter; |
| 752 | |
| 753 | if(i > <%= $no_iterations %>) { |
| 754 | stopStep(); |
| 755 | } else { |
| 756 | var cost = Math.round(T.step() * 100000) / 100000; // do a few steps |
| 757 | $("#cost").html("tsne iteration " + i + ", cost: " + cost.toFixed(5)); |
| 758 | if(i % 250 == 0 && cost >= last_cost) { |
| 759 | stopStep(); |
| 760 | } else { |
| 761 | last_cost = cost; |
| 762 | updateEmbedding(); |
| 763 | } |
| 764 | } |
| 765 | } |
Marc Kupietz | c5990da | 2016-02-26 08:47:12 +0100 | [diff] [blame] | 766 | |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 767 | function showMap(j) { |
| 768 | data=j; |
| 769 | T.iter=0; |
| 770 | T.initDataRaw(data.vecs); // init embedding |
| 771 | drawEmbedding(); // draw initial embedding |
| 772 | |
| 773 | if(iter_id >= 0) { |
| 774 | clearInterval(iter_id); |
| 775 | } |
| 776 | //T.debugGrad(); |
| 777 | iter_id = setInterval(step, 1); |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 778 | if(<%= $show_som %>) { |
| 779 | makeSOM(j, <%= $no_iterations %>); |
| 780 | } |
Marc Kupietz | 9fca173 | 2016-02-29 09:07:04 +0100 | [diff] [blame] | 781 | } |
Marc Kupietz | a350bce | 2016-02-25 09:34:25 +0100 | [diff] [blame] | 782 | |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 783 | </script> |
| 784 | </head> |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 785 | <body> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 786 | <form action="<%=url_for('/')->to_abs%>" method="GET"> |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 787 | word(s): |
| 788 | <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."> |
| 789 | max. neighbours: <input type="text" size="8" name="n" value="<%= $no_nbs %>"> |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 790 | max. iterations: <input type="text" name="N" size="8" value="<%= $no_iterations %>"> |
| 791 | SOM <input type="checkbox" name="som" value="1" <%= ($show_som ? "checked" : "") %>> |
| 792 | <span> </span><input type="submit" value="Show"> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 793 | </form> |
| 794 | <br> |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 795 | % if($collocators) { |
| 796 | <div id="collocators"> |
| 797 | % for my $item (@$collocators) { |
| 798 | <i><%= $item->{word} %></i> (<%= $item->{pos} %>: <%= sprintf("%.2f", $item->{dist}) %>) |
| 799 | % } |
| 800 | </div> |
| 801 | % } |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 802 | % if($lists) { |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 803 | <div id="wrapper"> |
| 804 | <table id="first"> |
| 805 | <tr> |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 806 | <th align="right">Pos.</th><th align="left">Word</th><th align="right">Cosine dist.</th><th>Freq. rank</th> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 807 | </tr> |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 808 | % my $j=0; my @words; my @vecs; my @ranks; for my $list (@$lists) { |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 809 | % my $i=1; for my $item (@$list) { |
| 810 | % if(!grep{$_ eq $item->{word}} @words) { |
| 811 | % push @vecs, $item->{vector}; |
| 812 | % push @words, $item->{word}; |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 813 | % push @ranks, $item->{rank}; |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 814 | % } |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 815 | <tr> |
| 816 | <td align="right"> |
| 817 | <%= $i++ %>. |
| 818 | </td> |
| 819 | <td> |
| 820 | <a href="/?word=<%= $item->{word} %>"> |
| 821 | <%= $item->{word} %> |
| 822 | </a> |
| 823 | </td> |
| 824 | <td align="right"> |
| 825 | <%= sprintf("%.3f", $item->{dist}) %> |
| 826 | </td> |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 827 | <td align="right"> |
| 828 | <%= $item->{rank} %> |
| 829 | </td> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 830 | </tr> |
| 831 | % } |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 832 | % } |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 833 | </table> |
| 834 | <script> |
| 835 | % use Mojo::ByteStream 'b'; |
| 836 | $(window).load(function() { |
Marc Kupietz | 5f78067 | 2016-02-25 17:15:54 +0100 | [diff] [blame] | 837 | showMap(<%= b(Mojo::JSON::to_json({target => " $word ", words => \@words, vecs => \@vecs, ranks => \@ranks})); %>); |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 838 | }); |
| 839 | </script> |
| 840 | % } |
| 841 | <div id="second" style="width:800px; height:800px; font-family: arial;"> |
| 842 | <div id="embed"> |
| 843 | </div> |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 844 | </div> |
Marc Kupietz | b102936 | 2016-02-27 21:38:55 +0100 | [diff] [blame] | 845 | <div id="cost"></div> |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 846 | % if($show_som) { |
| 847 | <div id="som2"> |
| 848 | </div> |
Marc Kupietz | 6c1ca44 | 2016-03-03 09:35:18 +0100 | [diff] [blame] | 849 | <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 Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 850 | <div id="sominfo">SOM iteration <span id="iterations">0</span></div> |
| 851 | % } |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 852 | </div> |
| 853 | <p> |
| 854 | Word vector model based on DeReKo-2015-II. Trained with <a href="https://code.google.com/p/word2vec/">word2vec</a> using the following parameters:</p> |
Marc Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 855 | <pre> |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 856 | -cbow 1 -size 300 -window 7 -negative 5 -hs 0 -sample 1e-5 -threads 44 -binary 1 -iter 5 |
Marc Kupietz | 4aa6217 | 2016-02-25 10:39:27 +0100 | [diff] [blame] | 857 | </pre> |
| 858 | </p> |
| 859 | </body> |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 860 | </html> |
| 861 | |