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