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