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