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