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