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