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