Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 1 | #!/usr/local/bin/perl |
| 2 | use Inline C; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 3 | use Inline C => Config => CLEAN_AFTER_BUILD => 0, ccflags => $Config{ccflags}." -O4"; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 4 | #use Inline C => Config => BUILD_NOISY => 1, CFLAGS => $Config{cflags}." -O4 -mtune k9"; |
| 5 | #use Inline C => Config => CLEAN_AFTER_BUILD => 0, ccflags => $Config{ccflags}." -Ofast -march k8 -mtune k8 "; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 6 | use Mojolicious::Lite; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 7 | use Mojo::JSON qw(decode_json encode_json to_json); |
Marc Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 8 | use Encode qw(decode encode); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 9 | use Getopt::Std; |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 10 | use Mojo::Server::Daemon; |
Marc Kupietz | d422739 | 2016-03-01 16:45:12 +0100 | [diff] [blame] | 11 | plugin 'Log::Access'; |
Marc Kupietz | b3422c1 | 2017-07-04 14:12:11 +0200 | [diff] [blame] | 12 | plugin "RequestBase"; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 13 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 14 | our $opt_i = 0; # latin1-input? |
| 15 | our $opt_l = undef; |
| 16 | our $opt_p = 5676; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 17 | our $opt_m; |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 18 | our $opt_M; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 19 | our $opt_n = ''; |
| 20 | our $opt_d; |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 21 | our $opt_G; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 22 | |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 23 | my %marked; |
Marc Kupietz | 793413b | 2016-04-02 21:48:57 +0200 | [diff] [blame] | 24 | my $training_args=""; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 25 | my $mergedEnd=0; |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame^] | 26 | my %cache; |
Marc Kupietz | 793413b | 2016-04-02 21:48:57 +0200 | [diff] [blame] | 27 | |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 28 | getopts('d:Gil:p:m:n:M:'); |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 29 | |
| 30 | if($opt_M) { |
Marc Kupietz | ed93021 | 2016-04-27 15:42:38 +0200 | [diff] [blame] | 31 | open my $handle, '<:encoding(UTF-8)', $opt_M |
| 32 | or die "Can't open '$opt_M' for reading: $!"; |
| 33 | while(<$handle>) { |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 34 | foreach my $mw (split /\s+/) { |
| 35 | $marked{$mw}=1 |
| 36 | } |
| 37 | } |
Marc Kupietz | ed93021 | 2016-04-27 15:42:38 +0200 | [diff] [blame] | 38 | close($handle); |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 39 | } |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 40 | |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 41 | # -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] | 42 | if(!$ARGV[0]) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 43 | init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0)); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 44 | } else { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 45 | init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0)); |
Marc Kupietz | 793413b | 2016-04-02 21:48:57 +0200 | [diff] [blame] | 46 | if(open(FILE, "$ARGV[0].args")) { |
| 47 | $training_args = <FILE>; |
| 48 | } |
| 49 | close(FILE); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 50 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 51 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 52 | if($opt_m) { |
| 53 | $mergedEnd = mergeVectors($opt_m); |
| 54 | } |
| 55 | |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 56 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 57 | if($opt_d) { # -d: dump vecs and exit |
| 58 | dump_vecs($opt_d); |
| 59 | exit; |
| 60 | } |
| 61 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 62 | my $daemon = Mojo::Server::Daemon->new( |
| 63 | app => app, |
| 64 | listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"] |
| 65 | ); |
| 66 | |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 67 | if($opt_G) { |
| 68 | print "Filtering garbage\n"; |
| 69 | filter_garbage(); |
| 70 | } |
| 71 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 72 | get '/' => sub { |
| 73 | my $c = shift; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 74 | $c->app->log->info("get: ".$c->req->url->to_abs); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 75 | my $word=$c->param('word'); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 76 | my $no_nbs=$c->param('n') || 100; |
| 77 | my $no_iterations=$c->param('N') || 2000; |
Marc Kupietz | d422739 | 2016-03-01 16:45:12 +0100 | [diff] [blame] | 78 | my $perplexity=$c->param('perplexity') || 20; |
Marc Kupietz | c4d62f8 | 2016-03-01 11:04:24 +0100 | [diff] [blame] | 79 | my $epsilon=$c->param('epsilon') || 5; |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 80 | my $som=$c->param('som') || 0; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 81 | my $searchBaseVocabFirst=$c->param('sbf') || 0; |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 82 | my $sort=$c->param('sort') || 0; |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 83 | my $json=$c->param('json') || 0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 84 | my $res; |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 85 | my @lists; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 86 | my @collocations; |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 87 | if(defined($word) && $word !~ /^\s*$/) { |
| 88 | $c->inactivity_timeout(300); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 89 | $word =~ s/\s+/ /g; |
| 90 | for my $w (split(' *\| *', $word)) { |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame^] | 91 | if ($cache{$w}) { |
| 92 | $c->app->log->info("Getting $w results from cache"); |
| 93 | $res = $cache{$w} |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 94 | } else { |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame^] | 95 | $c->app->log->info('Looking for neighbours of '.$w); |
| 96 | if($opt_i) { |
| 97 | $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst); |
| 98 | } else { |
| 99 | $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst); |
| 100 | } |
| 101 | $cache{$w} = $res; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 102 | } |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 103 | push(@lists, $res->{paradigmatic}); |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame^] | 104 | } |
| 105 | } |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 106 | $word =~ s/ *\| */ | /g; |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 107 | if($json) { |
| 108 | return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}}); |
| 109 | } else { |
| 110 | $c->render(template=>"index", word=>$word, no_nbs=>$no_nbs, no_iterations => $no_iterations, epsilon=> $epsilon, perplexity=> $perplexity, show_som=>$som, searchBaseVocabFirst=>$searchBaseVocabFirst, sort=>$sort, training_args=>$training_args, mergedEnd=> $mergedEnd, marked=>\%marked, lists=> \@lists, collocators=> $res->{syntagmatic}); |
| 111 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 112 | }; |
| 113 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 114 | $daemon->run; # app->start; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 115 | |
| 116 | exit; |
| 117 | |
| 118 | __END__ |
| 119 | |
| 120 | __C__ |
| 121 | #include <stdio.h> |
| 122 | #include <string.h> |
| 123 | #include <math.h> |
| 124 | #include <malloc.h> |
| 125 | #include <stdlib.h> //strlen |
Marc Kupietz | f080976 | 2016-02-26 10:13:47 +0100 | [diff] [blame] | 126 | #include <sys/mman.h> |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 127 | #include <pthread.h> |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 128 | |
| 129 | #define max_size 2000 |
| 130 | #define max_w 50 |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 131 | #define MAX_NEIGHBOURS 1000 |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 132 | #define MAX_WORDS -1 |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 133 | #define MAX_THREADS 100 |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 134 | #define MAX_CC 50 |
| 135 | #define EXP_TABLE_SIZE 1000 |
| 136 | #define MAX_EXP 6 |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 137 | #define MIN_RESP 0.50 |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 138 | |
| 139 | //the thread function |
| 140 | void *connection_handler(void *); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 141 | |
| 142 | typedef struct { |
| 143 | long long *index; |
| 144 | float *dist; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 145 | float *norm; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 146 | long long *pos; |
Marc Kupietz | 80abb44 | 2016-03-23 21:04:08 +0100 | [diff] [blame] | 147 | int length; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 148 | } knn; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 149 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 150 | typedef struct { |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 151 | long long wordi[MAX_NEIGHBOURS]; |
| 152 | char sep[MAX_NEIGHBOURS]; |
| 153 | int length; |
| 154 | } wordlist; |
| 155 | |
| 156 | typedef struct { |
| 157 | wordlist *wl; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 158 | char *token; |
| 159 | int N; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 160 | long from; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 161 | unsigned long upto; |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 162 | float *target_sums; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 163 | } knnpars; |
| 164 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 165 | float *M, *M2=0L, *syn1neg_window, *expTable; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 166 | char *vocab; |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 167 | char *garbage = NULL; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 168 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 169 | long long words, size, merged_end; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 170 | long long merge_words = 0; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 171 | int num_threads=20; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 172 | int latin_enc=0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 173 | int window; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 174 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 175 | int init_net(char *file_name, char *net_name, int latin) { |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 176 | FILE *f, *binvecs, *binwords; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 177 | int binwords_fd, binvecs_fd, net_fd, i; |
Marc Kupietz | 82b0267 | 2016-02-26 12:32:25 +0100 | [diff] [blame] | 178 | long long a, b, c, d, cn; |
| 179 | float len; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 180 | double val; |
Marc Kupietz | 82b0267 | 2016-02-26 12:32:25 +0100 | [diff] [blame] | 181 | |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 182 | char binvecs_fname[256], binwords_fname[256]; |
| 183 | strcpy(binwords_fname, file_name); |
| 184 | strcat(binwords_fname, ".words"); |
| 185 | strcpy(binvecs_fname, file_name); |
| 186 | strcat(binvecs_fname, ".vecs"); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 187 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 188 | latin_enc = latin; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 189 | f = fopen(file_name, "rb"); |
| 190 | if (f == NULL) { |
| 191 | printf("Input file %s not found\n", file_name); |
| 192 | return -1; |
| 193 | } |
| 194 | fscanf(f, "%lld", &words); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 195 | if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 196 | fscanf(f, "%lld", &size); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 197 | if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) { |
| 198 | printf("Converting %s to memory mappable structures\n", file_name); |
Marc Kupietz | f080976 | 2016-02-26 10:13:47 +0100 | [diff] [blame] | 199 | vocab = (char *)malloc((long long)words * max_w * sizeof(char)); |
| 200 | M = (float *)malloc((long long)words * (long long)size * sizeof(float)); |
| 201 | if (M == NULL) { |
| 202 | printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size); |
| 203 | return -1; |
| 204 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 205 | if(strstr(file_name, ".txt")) { |
| 206 | for (b = 0; b < words; b++) { |
| 207 | a = 0; |
| 208 | while (1) { |
| 209 | vocab[b * max_w + a] = fgetc(f); |
| 210 | if (feof(f) || (vocab[b * max_w + a] == ' ')) break; |
| 211 | if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++; |
| 212 | } |
| 213 | vocab[b * max_w + a] = 0; |
| 214 | len = 0; |
| 215 | for (a = 0; a < size; a++) { |
| 216 | fscanf(f, "%lf", &val); |
| 217 | M[a + b * size] = val; |
| 218 | len += val * val; |
| 219 | } |
| 220 | len = sqrt(len); |
| 221 | for (a = 0; a < size; a++) M[a + b * size] /= len; |
| 222 | } |
| 223 | } else { |
| 224 | for (b = 0; b < words; b++) { |
| 225 | a = 0; |
| 226 | while (1) { |
| 227 | vocab[b * max_w + a] = fgetc(f); |
| 228 | if (feof(f) || (vocab[b * max_w + a] == ' ')) break; |
| 229 | if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++; |
| 230 | } |
| 231 | vocab[b * max_w + a] = 0; |
| 232 | fread(&M[b * size], sizeof(float), size, f); |
| 233 | len = 0; |
| 234 | for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size]; |
| 235 | len = sqrt(len); |
| 236 | for (a = 0; a < size; a++) M[a + b * size] /= len; |
| 237 | } |
| 238 | } |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 239 | if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) { |
| 240 | fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs); |
| 241 | fclose(binvecs); |
| 242 | fwrite(vocab, sizeof(char), (long long)words * max_w, binwords); |
| 243 | fclose(binwords); |
| 244 | } |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 245 | } |
| 246 | if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) { |
| 247 | M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0); |
| 248 | vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0); |
| 249 | if (M == MAP_FAILED || vocab == MAP_FAILED) { |
| 250 | close(binvecs_fd); |
| 251 | close(binwords_fd); |
| 252 | fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname); |
| 253 | exit(-1); |
| 254 | } |
| 255 | } else { |
| 256 | fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname); |
| 257 | exit(-1); |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 258 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 259 | fclose(f); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 260 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 261 | if(net_name && strlen(net_name) > 0) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 262 | if( (net_fd = open(net_name, O_RDONLY)) >= 0) { |
| 263 | window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2; |
| 264 | // lseek(net_fd, sizeof(float) * words * size, SEEK_SET); |
Marc Kupietz | 10bec2b | 2016-03-23 09:41:31 +0100 | [diff] [blame] | 265 | // munmap(M, sizeof(float) * words * size); |
| 266 | M2 = mmap(0, sizeof(float) * words * size + sizeof(float) * 2 * window * size * words, PROT_READ, MAP_SHARED, net_fd, 0); |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 267 | if (M2 == MAP_FAILED) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 268 | close(net_fd); |
| 269 | fprintf(stderr, "Cannot mmap %s\n", net_name); |
| 270 | exit(-1); |
| 271 | } |
Marc Kupietz | 10bec2b | 2016-03-23 09:41:31 +0100 | [diff] [blame] | 272 | syn1neg_window = M2 + words * size; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 273 | } else { |
| 274 | fprintf(stderr, "Cannot open %s\n", net_name); |
| 275 | exit(-1); |
| 276 | } |
| 277 | fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window); |
| 278 | } |
| 279 | |
| 280 | expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float)); |
| 281 | for (i = 0; i < EXP_TABLE_SIZE; i++) { |
| 282 | expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table |
| 283 | expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1) |
| 284 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 285 | return 0; |
| 286 | } |
| 287 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 288 | long mergeVectors(char *file_name){ |
| 289 | FILE *f, *binvecs, *binwords; |
| 290 | int binwords_fd, binvecs_fd, net_fd, i; |
| 291 | long long a, b, c, d, cn; |
| 292 | float len; |
| 293 | float *merge_vecs; |
| 294 | char *merge_vocab; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 295 | /* long long merge_words, merge_size; */ |
| 296 | long long merge_size; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 297 | |
| 298 | char binvecs_fname[256], binwords_fname[256]; |
| 299 | strcpy(binwords_fname, file_name); |
| 300 | strcat(binwords_fname, ".words"); |
| 301 | strcpy(binvecs_fname, file_name); |
| 302 | strcat(binvecs_fname, ".vecs"); |
| 303 | |
| 304 | f = fopen(file_name, "rb"); |
| 305 | if (f == NULL) { |
| 306 | printf("Input file %s not found\n", file_name); |
| 307 | exit -1; |
| 308 | } |
| 309 | fscanf(f, "%lld", &merge_words); |
| 310 | fscanf(f, "%lld", &merge_size); |
| 311 | if(merge_size != size){ |
| 312 | fprintf(stderr, "vectors must have the same length\n"); |
| 313 | exit(-1); |
| 314 | } |
| 315 | if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) { |
| 316 | merge_vecs = malloc(sizeof(float) * (words + merge_words) * size); |
| 317 | merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w); |
| 318 | if (merge_vecs == NULL || merge_vocab == NULL) { |
| 319 | close(binvecs_fd); |
| 320 | close(binwords_fd); |
| 321 | fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname); |
| 322 | exit(-1); |
| 323 | } |
| 324 | read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float)); |
| 325 | read(binwords_fd, merge_vocab, merge_words * max_w); |
| 326 | } else { |
| 327 | fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname); |
| 328 | exit(-1); |
| 329 | } |
| 330 | printf("Successfully reallocated memory\nMerging...\n"); |
| 331 | fflush(stdout); |
| 332 | memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float)); |
| 333 | memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w); |
| 334 | munmap(M, words * size * sizeof(float)); |
| 335 | munmap(vocab, words * max_w); |
| 336 | M = merge_vecs; |
| 337 | vocab = merge_vocab; |
| 338 | merged_end = merge_words; |
| 339 | words += merge_words; |
| 340 | fclose(f); |
| 341 | printf("merged_end: %lld, words: %lld\n", merged_end, words); |
| 342 | return((long) merged_end); |
| 343 | } |
| 344 | |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 345 | void filter_garbage() { |
| 346 | long i; |
Marc Kupietz | ab591a8 | 2016-04-28 14:08:49 +0200 | [diff] [blame] | 347 | unsigned char *w, previous, c; |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 348 | garbage = malloc(words); |
| 349 | memset(garbage, 0, words); |
| 350 | for (i = 0; i < words; i++) { |
| 351 | w = vocab + i * max_w; |
| 352 | previous = 0; |
Marc Kupietz | ab591a8 | 2016-04-28 14:08:49 +0200 | [diff] [blame] | 353 | while((c = *w++) && !garbage[i]) { |
| 354 | if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) || |
| 355 | (previous == '-' && (c & 32)) || |
| 356 | (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) |
| 357 | ) { |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 358 | garbage[i]=1; |
| 359 | continue; |
| 360 | } |
| 361 | previous = c; |
| 362 | } |
| 363 | } |
| 364 | return; |
| 365 | } |
| 366 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 367 | void *getCollocators(knnpars *pars) { |
| 368 | int N = pars->N; |
| 369 | int cc = pars->wl->wordi[0]; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 370 | knn *nbs = NULL; |
| 371 | long window_layer_size = size * window * 2; |
| 372 | long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target; |
| 373 | float f, max_f, maxmax_f; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 374 | float *target_sums, *bestf, *bestn, worstbest, wpos_sum; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 375 | long long *besti, *bestp; |
Marc Kupietz | d564258 | 2016-03-19 22:23:13 +0100 | [diff] [blame] | 376 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 377 | if(M2 == NULL || cc == -1) |
Marc Kupietz | d564258 | 2016-03-19 22:23:13 +0100 | [diff] [blame] | 378 | return NULL; |
| 379 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 380 | a = posix_memalign((void **) &target_sums, 128, words * sizeof(float)); |
| 381 | besti = malloc(N * sizeof(long long)); |
| 382 | bestp = malloc(N * sizeof(long long)); |
| 383 | bestf = malloc(N * sizeof(float)); |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 384 | bestn = malloc(N * sizeof(float)); |
| 385 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 386 | worstbest = MIN_RESP; |
| 387 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 388 | for (b = 0; b < words; b++) |
| 389 | target_sums[b]=0; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 390 | for (b = 0; b < N; b++) { |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 391 | besti[b] = -1; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 392 | bestn[b] = 1; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 393 | bestf[b] = worstbest; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 394 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 395 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 396 | d = cc; |
| 397 | maxmax_f = -1; |
| 398 | maxmax_target = 0; |
| 399 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 400 | for (a = pars->from; a < pars->upto; a++) { |
| 401 | if(a >= window) |
| 402 | a++; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 403 | wpos_sum = 0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 404 | printf("window pos: %ld\n", a); |
| 405 | if (a != window) { |
| 406 | max_f = -1; |
| 407 | window_offset = a * size; |
| 408 | if (a > window) |
| 409 | window_offset -= size; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 410 | for(target = 0; target < words; target ++) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 411 | if(target == d) |
| 412 | continue; |
| 413 | f = 0; |
| 414 | for (c = 0; c < size; c++) |
Marc Kupietz | 10bec2b | 2016-03-23 09:41:31 +0100 | [diff] [blame] | 415 | 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] | 416 | if (f < -MAX_EXP) |
| 417 | continue; |
| 418 | else if (f > MAX_EXP) |
| 419 | continue; |
| 420 | else |
| 421 | f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))]; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 422 | wpos_sum += f; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 423 | |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 424 | target_sums[target] += f; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 425 | if(f > worstbest) { |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 426 | for (b = 0; b < N; b++) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 427 | if (f > bestf[b]) { |
Marc Kupietz | 33679a3 | 2016-03-22 08:49:39 +0100 | [diff] [blame] | 428 | memmove(bestf + b + 1, bestf + b, (N - b -1) * sizeof(float)); |
| 429 | memmove(besti + b + 1, besti + b, (N - b -1) * sizeof(long long)); |
| 430 | memmove(bestp + b + 1, bestp + b, (N - b -1) * sizeof(long long)); |
| 431 | bestf[b] = f; |
| 432 | besti[b] = target; |
| 433 | bestp[b] = window-a; |
| 434 | break; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 435 | } |
| 436 | } |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 437 | if(b == N - 1) |
| 438 | worstbest = bestf[N-1]; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | printf("%d %.2f\n", max_target, max_f); |
| 442 | printf("%s (%.2f) ", &vocab[max_target * max_w], max_f); |
| 443 | if(max_f > maxmax_f) { |
| 444 | maxmax_f = max_f; |
| 445 | maxmax_target = max_target; |
| 446 | } |
Marc Kupietz | 33679a3 | 2016-03-22 08:49:39 +0100 | [diff] [blame] | 447 | for (b = 0; b < N; b++) |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 448 | if(bestp[b] == window-a) |
| 449 | bestn[b] = bestf[b] / wpos_sum; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 450 | } else { |
| 451 | printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]); |
| 452 | } |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 453 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 454 | } |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 455 | for (b = 0; b < words; b++) |
| 456 | pars->target_sums[b] += (target_sums[b] / wpos_sum ) / (window * 2); |
| 457 | free(target_sums); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 458 | 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] | 459 | 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] | 460 | printf("\n"); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 461 | nbs = malloc(sizeof(knn)); |
| 462 | nbs->index = besti; |
| 463 | nbs->dist = bestf; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 464 | nbs->norm = bestn; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 465 | nbs->pos = bestp; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 466 | nbs->length = b-1; |
| 467 | pthread_exit(nbs); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 468 | } |
| 469 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 470 | wordlist *getTargetWords(char *st1, int search_backw) { |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 471 | wordlist *wl = malloc(sizeof(wordlist)); |
| 472 | char st[100][max_size], sep[100]; |
| 473 | long a, b=0, c=0, cn=0; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 474 | int unmerged; |
| 475 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 476 | while (1) { |
| 477 | st[cn][b] = st1[c]; |
| 478 | b++; |
| 479 | c++; |
| 480 | st[cn][b] = 0; |
| 481 | if (st1[c] == 0) break; |
Marc Kupietz | 95aa1c0 | 2016-03-15 09:40:43 +0100 | [diff] [blame] | 482 | if (st1[c] == ' ' || st1[c] == '-') { |
| 483 | sep[cn++] = st1[c]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 484 | b = 0; |
| 485 | c++; |
| 486 | } |
| 487 | } |
| 488 | cn++; |
| 489 | for (a = 0; a < cn; a++) { |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 490 | if(search_backw) { |
| 491 | for (b = words - 1; b >= 0; b--) if (!strcmp(&vocab[b * max_w], st[a])) break; |
| 492 | } else { |
| 493 | for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break; |
| 494 | } |
Marc Kupietz | 34a3ee9 | 2016-02-27 22:43:16 +0100 | [diff] [blame] | 495 | if (b == words) b = -1; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 496 | wl->wordi[a] = b; |
| 497 | 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] | 498 | if (b == -1) { |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 499 | fprintf(stderr, "Out of dictionary word!\n"); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 500 | cn--; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 501 | break; |
| 502 | } |
| 503 | } |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 504 | wl->length=cn; |
| 505 | return(wl); |
| 506 | } |
| 507 | |
| 508 | void *_get_neighbours(knnpars *pars) { |
| 509 | char *st1 = pars->token; |
| 510 | int N = pars->N; |
| 511 | long from = pars -> from; |
| 512 | unsigned long upto = pars -> upto; |
| 513 | char file_name[max_size], st[100][max_size], *sep; |
| 514 | float dist, len, *bestd, vec[max_size]; |
| 515 | long long a, b, c, d, cn, *bi, *besti; |
| 516 | char ch; |
| 517 | knn *nbs = NULL; |
| 518 | wordlist *wl = pars->wl; |
| 519 | |
| 520 | besti = malloc(N * sizeof(long long)); |
| 521 | bestd = malloc(N * sizeof(float)); |
| 522 | |
| 523 | float worstbest=-1; |
| 524 | |
| 525 | for (a = 0; a < N; a++) bestd[a] = 0; |
| 526 | a = 0; |
| 527 | bi = wl->wordi; |
| 528 | cn = wl->length; |
| 529 | sep = wl->sep; |
| 530 | b = bi[0]; |
| 531 | c = 0; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 532 | if (b == -1) { |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 533 | N = 0; |
| 534 | goto end; |
| 535 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 536 | for (a = 0; a < size; a++) vec[a] = 0; |
| 537 | for (b = 0; b < cn; b++) { |
| 538 | if (bi[b] == -1) continue; |
Marc Kupietz | 95aa1c0 | 2016-03-15 09:40:43 +0100 | [diff] [blame] | 539 | if(b>0 && sep[b-1] == '-') |
| 540 | for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size]; |
| 541 | else |
| 542 | 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] | 543 | } |
| 544 | len = 0; |
| 545 | for (a = 0; a < size; a++) len += vec[a] * vec[a]; |
| 546 | len = sqrt(len); |
| 547 | for (a = 0; a < size; a++) vec[a] /= len; |
| 548 | for (a = 0; a < N; a++) bestd[a] = -1; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 549 | for (c = from; c < upto; c++) { |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 550 | if(garbage && garbage[c]) continue; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 551 | a = 0; |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 552 | // do not skip taget word |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 553 | // for (b = 0; b < cn; b++) if (bi[b] == c) a = 1; |
| 554 | // if (a == 1) continue; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 555 | dist = 0; |
| 556 | 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] | 557 | if(dist > worstbest) { |
| 558 | for (a = 0; a < N; a++) { |
| 559 | if (dist > bestd[a]) { |
Marc Kupietz | 33679a3 | 2016-03-22 08:49:39 +0100 | [diff] [blame] | 560 | memmove(bestd + a + 1, bestd + a, (N - a -1) * sizeof(float)); |
| 561 | memmove(besti + a + 1, besti + a, (N - a -1) * sizeof(long long)); |
Marc Kupietz | be1b9fc | 2016-02-26 10:34:30 +0100 | [diff] [blame] | 562 | bestd[a] = dist; |
| 563 | besti[a] = c; |
| 564 | break; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 565 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 566 | } |
Marc Kupietz | be1b9fc | 2016-02-26 10:34:30 +0100 | [diff] [blame] | 567 | worstbest = bestd[N-1]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 568 | } |
| 569 | } |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 570 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 571 | nbs = malloc(sizeof(knn)); |
| 572 | nbs->index = besti; |
| 573 | nbs->dist = bestd; |
| 574 | nbs->length = N; |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 575 | end: |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 576 | pthread_exit(nbs); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 577 | } |
| 578 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 579 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 580 | SV *get_neighbours(char *st1, int N, int sort_by, int search_backw) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 581 | HV *result = newHV(); |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 582 | float *target_sums, bestd[MAX_NEIGHBOURS], bestn[MAX_NEIGHBOURS], bests[MAX_NEIGHBOURS], vec[max_size]; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 583 | long long old_words; |
Marc Kupietz | 50485ba | 2016-03-23 09:13:14 +0100 | [diff] [blame] | 584 | long besti[MAX_NEIGHBOURS], bestp[MAX_NEIGHBOURS], a, b, c, d, slice; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 585 | knn *para_nbs[MAX_THREADS]; |
| 586 | knn *syn_nbs[MAX_THREADS]; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 587 | knnpars pars[MAX_THREADS]; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 588 | pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t)); |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 589 | wordlist *wl; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 590 | int syn_threads = (M2? window * 2 : 0); |
| 591 | int para_threads = num_threads - syn_threads; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 592 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 593 | if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS; |
| 594 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 595 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 596 | wl = getTargetWords(st1, search_backw); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 597 | if(wl->length < 1) |
| 598 | goto end; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 599 | |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 600 | old_words = words; |
| 601 | if(merge_words > 0) |
| 602 | words = merge_words * 1.25; /* HACK */ |
| 603 | slice = words / para_threads; |
| 604 | |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 605 | a = posix_memalign((void **) &target_sums, 128, words * sizeof(float)); |
| 606 | for(a = 0; a < words; a++) |
| 607 | target_sums[a] = 0; |
| 608 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 609 | printf("Starting %d threads\n", para_threads); |
| 610 | fflush(stdout); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 611 | for(a=0; a < para_threads; a++) { |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 612 | pars[a].token = st1; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 613 | pars[a].wl = wl; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 614 | pars[a].N = N; |
| 615 | pars[a].from = a*slice; |
| 616 | pars[a].upto = ((a+1)*slice > words? words:(a+1)*slice); |
| 617 | pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]); |
| 618 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 619 | if(M2) { |
| 620 | for(a=0; a < syn_threads; a++) { |
| 621 | pars[a + para_threads].target_sums = target_sums; |
| 622 | pars[a + para_threads].wl = wl; |
| 623 | pars[a + para_threads].N = N; |
| 624 | pars[a + para_threads].from = a; |
| 625 | pars[a + para_threads].upto = a+1; |
| 626 | pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]); |
| 627 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 628 | } |
| 629 | printf("Waiting for para threads to join\n"); |
| 630 | fflush(stdout); |
| 631 | for (a = 0; a < para_threads; a++) pthread_join(pt[a], ¶_nbs[a]); |
| 632 | printf("Para threads joint\n"); |
| 633 | fflush(stdout); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 634 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 635 | /* if(!syn_nbs[0]) */ |
| 636 | /* goto end; */ |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 637 | |
| 638 | for(b=0; b < N; b++) { |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 639 | besti[b] = para_nbs[0]->index[b]; |
| 640 | bestd[b] = para_nbs[0]->dist[b]; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 641 | } |
| 642 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 643 | for(a=1; a < para_threads; a++) { |
| 644 | for(b=0; b < para_nbs[a]->length && para_nbs[a]->index[b] >= 0; b++) { |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 645 | for(c=0; c < N * para_threads; c++) { |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 646 | if(para_nbs[a]->dist[b] > bestd[c]) { |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 647 | for(d=N-1; d>c; d--) { |
| 648 | bestd[d] = bestd[d-1]; |
| 649 | besti[d] = besti[d-1]; |
| 650 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 651 | besti[c] = para_nbs[a]->index[b]; |
| 652 | bestd[c] = para_nbs[a]->dist[b]; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 653 | break; |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 659 | AV* array = newAV(); |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 660 | int i; |
| 661 | int l1_words=0, l2_words=0; |
| 662 | for (a = 0, i = 0; i < N && a < 600; a++) { |
| 663 | long long c = besti[a]; |
| 664 | if(merge_words > 0) { |
| 665 | if(c >= merge_words) { |
| 666 | if(l1_words > N / 2) |
| 667 | continue; |
| 668 | else |
| 669 | l1_words++; |
| 670 | } else { |
| 671 | if(l2_words > N / 2) |
| 672 | continue; |
| 673 | else |
| 674 | l2_words++; |
| 675 | } |
| 676 | } |
| 677 | fflush(stdout); |
| 678 | printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a); |
| 679 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 680 | HV* hash = newHV(); |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 681 | SV* word = newSVpvf(&vocab[c * max_w], 0); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 682 | if(latin_enc == 0) SvUTF8_on(word); |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 683 | fflush(stdout); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 684 | hv_store(hash, "word", strlen("word"), word , 0); |
| 685 | hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0); |
| 686 | hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0); |
| 687 | AV *vector = newAV(); |
| 688 | for (b = 0; b < size; b++) { |
| 689 | av_push(vector, newSVnv(M[b + besti[a] * size])); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 690 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 691 | hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0); |
| 692 | av_push(array, newRV_noinc((SV*)hash)); |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 693 | i++; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 694 | } |
| 695 | hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0); |
| 696 | |
Marc Kupietz | 50485ba | 2016-03-23 09:13:14 +0100 | [diff] [blame] | 697 | for(b=0; b < MAX_NEIGHBOURS; b++) { |
| 698 | besti[b] = -1L; |
| 699 | bestd[b] = 0; |
| 700 | bestn[b] = 0; |
| 701 | bestp[b] = 0; |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 702 | bests[b] = 0; |
Marc Kupietz | 50485ba | 2016-03-23 09:13:14 +0100 | [diff] [blame] | 703 | } |
| 704 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 705 | if (M2) { |
| 706 | printf("Waiting for syn threads to join\n"); |
| 707 | fflush(stdout); |
| 708 | for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], &syn_nbs[a]); |
| 709 | printf("syn threads joint\n"); |
| 710 | fflush(stdout); |
Marc Kupietz | 50485ba | 2016-03-23 09:13:14 +0100 | [diff] [blame] | 711 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 712 | for(b=0; b < syn_nbs[0]->length; b++) { |
| 713 | besti[b] = syn_nbs[0]->index[b]; |
| 714 | bestd[b] = syn_nbs[0]->dist[b]; |
| 715 | bestn[b] = syn_nbs[0]->norm[b]; |
| 716 | bestp[b] = syn_nbs[0]->pos[b]; |
| 717 | bests[b] = target_sums[syn_nbs[0]->index[b]]; |
| 718 | } |
| 719 | |
| 720 | if(sort_by != 1) { // sort by responsiveness |
| 721 | for(a=1; a < syn_threads; a++) { |
| 722 | for(b=0; b < syn_nbs[a]->length; b++) { |
| 723 | for(c=0; c < MAX_NEIGHBOURS; c++) { |
| 724 | if(syn_nbs[a]->dist[b] > bestd[c]) { |
| 725 | for(d=MAX_NEIGHBOURS-1; d>c; d--) { |
| 726 | bestd[d] = bestd[d-1]; |
| 727 | besti[d] = besti[d-1]; |
| 728 | bestn[d] = bestn[d-1]; |
| 729 | bestp[d] = bestp[d-1]; |
| 730 | } |
| 731 | besti[c] = syn_nbs[a]->index[b]; |
| 732 | bestd[c] = syn_nbs[a]->dist[b]; |
| 733 | bestn[c] = syn_nbs[a]->norm[b]; |
| 734 | bestp[c] = syn_nbs[a]->pos[b]; |
| 735 | break; |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 736 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 737 | } |
| 738 | } |
| 739 | } |
| 740 | } else { // sort by mean p |
| 741 | for(a=1; a < syn_threads; a++) { |
| 742 | for(b=0; b < syn_nbs[a]->length; b++) { |
| 743 | for(c=0; c < MAX_NEIGHBOURS; c++) { |
| 744 | if(target_sums[syn_nbs[a]->index[b]] > bests[c]) { |
| 745 | for(d=MAX_NEIGHBOURS-1; d>c; d--) { |
| 746 | bestd[d] = bestd[d-1]; |
| 747 | besti[d] = besti[d-1]; |
| 748 | bestn[d] = bestn[d-1]; |
| 749 | bestp[d] = bestp[d-1]; |
| 750 | bests[d] = bests[d-1]; |
| 751 | } |
| 752 | besti[c] = syn_nbs[a]->index[b]; |
| 753 | bestd[c] = syn_nbs[a]->dist[b]; |
| 754 | bestn[c] = syn_nbs[a]->norm[b]; |
| 755 | bestp[c] = syn_nbs[a]->pos[b]; |
| 756 | bests[c] = target_sums[syn_nbs[a]->index[b]]; |
| 757 | break; |
| 758 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 759 | } |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 763 | array = newAV(); |
| 764 | for (a = 0; a < MAX_NEIGHBOURS && besti[a] >= 0; a++) { |
| 765 | HV* hash = newHV(); |
| 766 | SV* word = newSVpvf(&vocab[besti[a] * max_w], 0); |
| 767 | if(latin_enc == 0) SvUTF8_on(word); |
| 768 | hv_store(hash, "word", strlen("word"), word , 0); |
| 769 | hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0); |
| 770 | hv_store(hash, "norm", strlen("norm"), newSVnv(bestn[a]), 0); |
| 771 | hv_store(hash, "sum", strlen("sum"), newSVnv(target_sums[besti[a]]), 0); |
| 772 | hv_store(hash, "pos", strlen("pos"), newSVnv(bestp[a]), 0); |
| 773 | av_push(array, newRV_noinc((SV*)hash)); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 774 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 775 | hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 776 | } |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 777 | end: |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 778 | words = old_words; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 779 | return newRV_noinc((SV*)result); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 780 | } |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 781 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 782 | int dump_vecs(char *fname) { |
| 783 | long i, j; |
| 784 | FILE *f; |
| 785 | /* if(words>200000) */ |
| 786 | /* words=200000; */ |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 787 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 788 | if((f=fopen(fname, "w")) == NULL) { |
| 789 | fprintf(stderr, "cannot open %s for writing\n", fname); |
| 790 | return(-1); |
| 791 | } |
| 792 | fprintf(f, "%lld %lld\n", words, size); |
| 793 | for (i=0; i < words; i++) { |
| 794 | fprintf(f, "%s ", &vocab[i * max_w]); |
| 795 | for(j=0; j < size - 1; j++) |
| 796 | fprintf(f, "%f ", M[i*size + j]); |
| 797 | fprintf(f, "%f\n", M[i*size + j]); |
| 798 | } |
| 799 | fclose(f); |
| 800 | return(0); |
| 801 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 802 | |