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