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