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