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