Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 1 | #!/usr/local/bin/perl |
| 2 | use Inline C; |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 3 | use Inline C => Config => CLEAN_AFTER_BUILD => 0, BUILD_NOISY => 1, ccflags => $Config{ccflags}." -I/vol/work/kupietz/Work2/kl/trunk/CollocatorDB -L/vol/work/kupietz/Work2/kl/trunk/CollocatorDB -Wall -O4", libs => "-shared -lpthread -lcollocatordb -lrt -lsnappy -lz -lbz2 -llz4 -lzstd -lrocksdb"; |
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; |
Marc Kupietz | 66bfd95 | 2017-12-11 09:59:45 +0100 | [diff] [blame] | 14 | |
Marc Kupietz | ffef930 | 2017-11-07 15:58:01 +0100 | [diff] [blame] | 15 | app->static->paths->[0] = getcwd; |
| 16 | |
Marc Kupietz | d422739 | 2016-03-01 16:45:12 +0100 | [diff] [blame] | 17 | plugin 'Log::Access'; |
Marc Kupietz | b3422c1 | 2017-07-04 14:12:11 +0200 | [diff] [blame] | 18 | plugin "RequestBase"; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 19 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 20 | our $opt_i = 0; # latin1-input? |
| 21 | our $opt_l = undef; |
| 22 | our $opt_p = 5676; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 23 | our $opt_m; |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 24 | our $opt_M; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 25 | our $opt_n = ''; |
| 26 | our $opt_d; |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 27 | our $opt_G; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 28 | |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 29 | my %marked; |
Marc Kupietz | 793413b | 2016-04-02 21:48:57 +0200 | [diff] [blame] | 30 | my $training_args=""; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 31 | my $mergedEnd=0; |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 32 | my %cache; |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 33 | my %cccache; # classic collocator cache |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 34 | my %spcache; # similar profile cache |
Marc Kupietz | 793413b | 2016-04-02 21:48:57 +0200 | [diff] [blame] | 35 | |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 36 | getopts('d:Gil:p:m:n:M:'); |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 37 | |
| 38 | if($opt_M) { |
Marc Kupietz | ed93021 | 2016-04-27 15:42:38 +0200 | [diff] [blame] | 39 | open my $handle, '<:encoding(UTF-8)', $opt_M |
| 40 | or die "Can't open '$opt_M' for reading: $!"; |
| 41 | while(<$handle>) { |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 42 | foreach my $mw (split /\s+/) { |
| 43 | $marked{$mw}=1 |
| 44 | } |
| 45 | } |
Marc Kupietz | ed93021 | 2016-04-27 15:42:38 +0200 | [diff] [blame] | 46 | close($handle); |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 47 | } |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 48 | |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 49 | # -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] | 50 | if(!$ARGV[0]) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 51 | init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0)); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 52 | } else { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 53 | init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0)); |
Marc Kupietz | 793413b | 2016-04-02 21:48:57 +0200 | [diff] [blame] | 54 | if(open(FILE, "$ARGV[0].args")) { |
| 55 | $training_args = <FILE>; |
| 56 | } |
| 57 | close(FILE); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 58 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 59 | |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 60 | my $have_sprofiles = load_sprofiles($ARGV[0]); |
| 61 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 62 | if($opt_m) { |
| 63 | $mergedEnd = mergeVectors($opt_m); |
| 64 | } |
| 65 | |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 66 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 67 | if($opt_d) { # -d: dump vecs and exit |
| 68 | dump_vecs($opt_d); |
| 69 | exit; |
| 70 | } |
| 71 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 72 | my $daemon = Mojo::Server::Daemon->new( |
| 73 | app => app, |
| 74 | listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"] |
| 75 | ); |
| 76 | |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 77 | if($opt_G) { |
| 78 | print "Filtering garbage\n"; |
| 79 | filter_garbage(); |
| 80 | } |
| 81 | |
Marc Kupietz | 554aff5 | 2017-11-09 14:42:09 +0100 | [diff] [blame] | 82 | get '*/js/*' => sub { |
Marc Kupietz | ffef930 | 2017-11-07 15:58:01 +0100 | [diff] [blame] | 83 | my $c = shift; |
| 84 | my $url = $c->req->url; |
| 85 | $url =~ s@/derekovecs@@g; |
| 86 | $c->app->log->info("GET: " . $url); |
| 87 | $c->reply->static($url); |
| 88 | }; |
| 89 | |
Marc Kupietz | a927057 | 2018-03-17 15:17:07 +0100 | [diff] [blame] | 90 | get '*/css/*' => sub { |
| 91 | my $c = shift; |
| 92 | my $url = $c->req->url; |
| 93 | $url =~ s@/derekovecs/@/@g; |
| 94 | $c->app->log->info("GET: " . $url); |
| 95 | $c->reply->static($url); |
| 96 | }; |
| 97 | |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 98 | sub getClassicCollocatorsCached { |
| 99 | my ($c, $word) = @_; |
| 100 | if(!$cccache{$word}) { |
| 101 | $cccache{$word} = getClassicCollocators($word); |
| 102 | } else { |
| 103 | $c->app->log->info("Getting classic collocators for $word from cache."); |
| 104 | } |
| 105 | return $cccache{$word}; |
| 106 | } |
| 107 | |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 108 | sub getSimilarProfilesCached { |
| 109 | my ($c, $word) = @_; |
| 110 | if(!$spcache{$word}) { |
| 111 | $spcache{$word} = getSimilarProfiles($word); |
| 112 | } else { |
| 113 | $c->app->log->info("Getting similar profiles for $word from cache:"); |
| 114 | print $spcache{$word}; |
| 115 | } |
| 116 | return $spcache{$word}; |
| 117 | } |
| 118 | |
Marc Kupietz | 66bfd95 | 2017-12-11 09:59:45 +0100 | [diff] [blame] | 119 | post '/derekovecs/getVecsByRanks' => sub { |
| 120 | my $self = shift; |
| 121 | my $vec = getVecs($self->req->json); |
| 122 | $self->render(json => $vec); |
| 123 | }; |
| 124 | |
Marc Kupietz | e13a355 | 2018-01-25 08:48:34 +0100 | [diff] [blame] | 125 | any '*/getClassicCollocators' => sub { |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 126 | my $self = shift; |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 127 | $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json'); |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 128 | }; |
| 129 | |
Marc Kupietz | e13a355 | 2018-01-25 08:48:34 +0100 | [diff] [blame] | 130 | any '/getClassicCollocators' => sub { |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 131 | my $self = shift; |
Marc Kupietz | 19c6824 | 2018-03-12 09:42:21 +0100 | [diff] [blame] | 132 | $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json'); |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 133 | }; |
| 134 | |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 135 | any '*/getSimilarProfiles' => sub { |
| 136 | my $self = shift; |
| 137 | $self->render(data => getSimilarProfilesCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json'); |
| 138 | }; |
| 139 | |
Marc Kupietz | c987fa8 | 2018-03-21 12:14:25 +0100 | [diff] [blame^] | 140 | any '/getSimilarProfiles' => sub { |
| 141 | my $self = shift; |
| 142 | $self->render(data => getSimilarProfilesCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json'); |
| 143 | }; |
| 144 | |
Marc Kupietz | df3d4b5 | 2017-11-29 16:57:27 +0100 | [diff] [blame] | 145 | get '*/img/*' => sub { |
| 146 | my $c = shift; |
| 147 | my $url = $c->req->url; |
| 148 | $url =~ s@/derekovecs@@g; |
| 149 | $c->app->log->info("GET: " . $url); |
| 150 | $c->reply->static($url); |
| 151 | }; |
| 152 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 153 | get '/' => sub { |
| 154 | my $c = shift; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 155 | $c->app->log->info("get: ".$c->req->url->to_abs); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 156 | my $word=$c->param('word'); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 157 | my $no_nbs=$c->param('n') || 100; |
| 158 | my $no_iterations=$c->param('N') || 2000; |
Marc Kupietz | d422739 | 2016-03-01 16:45:12 +0100 | [diff] [blame] | 159 | my $perplexity=$c->param('perplexity') || 20; |
Marc Kupietz | c4d62f8 | 2016-03-01 11:04:24 +0100 | [diff] [blame] | 160 | my $epsilon=$c->param('epsilon') || 5; |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 161 | my $som=$c->param('som') || 0; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 162 | my $searchBaseVocabFirst=$c->param('sbf') || 0; |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 163 | my $sort=$c->param('sort') || 0; |
Marc Kupietz | c469f3b | 2017-11-13 14:07:36 +0100 | [diff] [blame] | 164 | my $csv=$c->param('csv') || 0; |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 165 | my $json=$c->param('json') || 0; |
Marc Kupietz | db2dc7e | 2017-12-02 12:04:03 +0100 | [diff] [blame] | 166 | my $cutoff=$c->param('cutoff') || 500000; |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 167 | my $dedupe=$c->param('dedupe') || 0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 168 | my $res; |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 169 | my @lists; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 170 | my @collocations; |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 171 | if(defined($word) && $word !~ /^\s*$/) { |
| 172 | $c->inactivity_timeout(300); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 173 | $word =~ s/\s+/ /g; |
| 174 | for my $w (split(' *\| *', $word)) { |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 175 | if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe}) { |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 176 | $c->app->log->info("Getting $w results from cache"); |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 177 | $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 178 | } else { |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 179 | $c->app->log->info('Looking for neighbours of '.$w); |
| 180 | if($opt_i) { |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 181 | $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] | 182 | } else { |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 183 | $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe); |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 184 | } |
Marc Kupietz | 2dd2dd7 | 2017-12-01 22:08:14 +0100 | [diff] [blame] | 185 | $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 186 | } |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 187 | push(@lists, $res->{paradigmatic}); |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 188 | } |
| 189 | } |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 190 | $word =~ s/ *\| */ | /g; |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 191 | if($json) { |
| 192 | return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}}); |
Marc Kupietz | c469f3b | 2017-11-13 14:07:36 +0100 | [diff] [blame] | 193 | } elsif($csv) { |
| 194 | my $csv_data=""; |
| 195 | for (my $i=0; $i <= $no_nbs; $i++) { |
| 196 | $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", "; |
| 197 | } |
| 198 | for (my $i=0; $i < $no_nbs; $i++) { |
| 199 | $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", "; |
| 200 | } |
| 201 | chop $csv_data; |
| 202 | chop $csv_data; |
| 203 | $csv_data .= "\n"; |
| 204 | return $c->render(text=>$csv_data); |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 205 | } else { |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 206 | $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, haveSProfiles=> $have_sprofiles, dedupe=> $dedupe, marked=>\%marked, lists=> \@lists, collocators=> $res->{syntagmatic}); |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 207 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 208 | }; |
| 209 | |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 210 | helper(bitvec2window => sub { |
| 211 | my ($self, $n) = @_; |
| 212 | my $str = unpack("B32", pack("N", $n)); |
| 213 | $str =~ s/^\d{22}//; |
| 214 | $str =~ s/^(\d{5})/$1x/; |
| 215 | $str =~ s/0/·/g; |
| 216 | $str =~ s/1/+/g; |
| 217 | return $str; |
| 218 | }); |
| 219 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 220 | $daemon->run; # app->start; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 221 | |
| 222 | exit; |
| 223 | |
| 224 | __END__ |
| 225 | |
| 226 | __C__ |
| 227 | #include <stdio.h> |
| 228 | #include <string.h> |
| 229 | #include <math.h> |
| 230 | #include <malloc.h> |
| 231 | #include <stdlib.h> //strlen |
Marc Kupietz | f080976 | 2016-02-26 10:13:47 +0100 | [diff] [blame] | 232 | #include <sys/mman.h> |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 233 | #include <pthread.h> |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 234 | #include <collocatordb.h> |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 235 | |
| 236 | #define max_size 2000 |
| 237 | #define max_w 50 |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 238 | #define MAX_NEIGHBOURS 1000 |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 239 | #define MAX_WORDS -1 |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 240 | #define MAX_THREADS 100 |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 241 | #define MAX_CC 50 |
| 242 | #define EXP_TABLE_SIZE 1000 |
| 243 | #define MAX_EXP 6 |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 244 | #define MIN_RESP 0.50 |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 245 | |
| 246 | //the thread function |
| 247 | void *connection_handler(void *); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 248 | |
| 249 | typedef struct { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 250 | long long wordi; |
| 251 | long position; |
| 252 | float activation; |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 253 | float average; |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 254 | float cprobability; // column wise probability |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 255 | float cprobability_sum; |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 256 | float probability; |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 257 | float activation_sum; |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 258 | float max_activation; |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 259 | float heat[16]; |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 260 | } collocator; |
| 261 | |
| 262 | typedef struct { |
| 263 | collocator *best; |
Marc Kupietz | 80abb44 | 2016-03-23 21:04:08 +0100 | [diff] [blame] | 264 | int length; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 265 | } knn; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 266 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 267 | typedef struct { |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 268 | long long wordi[MAX_NEIGHBOURS]; |
| 269 | char sep[MAX_NEIGHBOURS]; |
| 270 | int length; |
| 271 | } wordlist; |
| 272 | |
| 273 | typedef struct { |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 274 | long cutoff; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 275 | wordlist *wl; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 276 | char *token; |
| 277 | int N; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 278 | long from; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 279 | unsigned long upto; |
Marc Kupietz | c55c887 | 2017-12-01 23:27:10 +0100 | [diff] [blame] | 280 | collocator *best; |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 281 | float *target_sums; |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 282 | float *window_sums; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 283 | } knnpars; |
| 284 | |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 285 | typedef struct { |
| 286 | uint32_t index; |
| 287 | float value; |
| 288 | } sparse_t; |
| 289 | |
| 290 | typedef struct { |
| 291 | uint32_t len; |
| 292 | sparse_t nbr[100]; |
| 293 | } profile_t; |
| 294 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 295 | float *M, *M2=0L, *syn1neg_window, *expTable; |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 296 | float *window_sums; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 297 | char *vocab; |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 298 | char *garbage = NULL; |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 299 | COLLOCATORDB *cdb = NULL; |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 300 | profile_t *sprofiles = NULL; |
| 301 | size_t sprofiles_qty = 0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 302 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 303 | long long words, size, merged_end; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 304 | long long merge_words = 0; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 305 | int num_threads=20; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 306 | int latin_enc=0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 307 | int window; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 308 | |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 309 | /* load collocation profiles if file exists */ |
| 310 | int load_sprofiles(char *vecsname) { |
| 311 | char *basename = strdup(vecsname); |
| 312 | char *pos = strstr(basename, ".vecs"); |
| 313 | if(pos) |
| 314 | *pos=0; |
| 315 | |
| 316 | char binsprofiles_fname[256]; |
| 317 | strcpy(binsprofiles_fname, basename); |
| 318 | strcat(binsprofiles_fname, ".sprofiles.bin"); |
| 319 | FILE *fp = fopen(binsprofiles_fname, "rb"); |
| 320 | if (fp == NULL) { |
| 321 | printf("Collocation profiles %s not found. No problem.\n", binsprofiles_fname); |
| 322 | return 0; |
| 323 | } |
| 324 | fseek(fp, 0L, SEEK_END); |
| 325 | size_t sz = ftell(fp); |
| 326 | fclose(fp); |
| 327 | |
| 328 | int fd = open(binsprofiles_fname, O_RDONLY); |
| 329 | sprofiles = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0); |
| 330 | if (sprofiles == MAP_FAILED) { |
| 331 | close(fd); |
| 332 | fprintf(stderr, "Cannot mmap %s\n", binsprofiles_fname); |
| 333 | sprofiles = NULL; |
| 334 | return 0; |
| 335 | } else { |
| 336 | sprofiles_qty = sz / sizeof(profile_t); |
| 337 | fprintf(stderr, "Successfully mmaped %s containing similar profiles for %ld word forms.\n", binsprofiles_fname, sprofiles_qty); |
| 338 | } |
| 339 | return 1; |
| 340 | } |
| 341 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 342 | int init_net(char *file_name, char *net_name, int latin) { |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 343 | FILE *f, *binvecs, *binwords; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 344 | int binwords_fd, binvecs_fd, net_fd, i; |
Marc Kupietz | 82b0267 | 2016-02-26 12:32:25 +0100 | [diff] [blame] | 345 | long long a, b, c, d, cn; |
| 346 | float len; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 347 | double val; |
Marc Kupietz | 82b0267 | 2016-02-26 12:32:25 +0100 | [diff] [blame] | 348 | |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 349 | char binvecs_fname[256], binwords_fname[256]; |
| 350 | strcpy(binwords_fname, file_name); |
| 351 | strcat(binwords_fname, ".words"); |
| 352 | strcpy(binvecs_fname, file_name); |
| 353 | strcat(binvecs_fname, ".vecs"); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 354 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 355 | latin_enc = latin; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 356 | f = fopen(file_name, "rb"); |
| 357 | if (f == NULL) { |
| 358 | printf("Input file %s not found\n", file_name); |
| 359 | return -1; |
| 360 | } |
| 361 | fscanf(f, "%lld", &words); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 362 | if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 363 | fscanf(f, "%lld", &size); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 364 | if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) { |
| 365 | printf("Converting %s to memory mappable structures\n", file_name); |
Marc Kupietz | f080976 | 2016-02-26 10:13:47 +0100 | [diff] [blame] | 366 | vocab = (char *)malloc((long long)words * max_w * sizeof(char)); |
| 367 | M = (float *)malloc((long long)words * (long long)size * sizeof(float)); |
| 368 | if (M == NULL) { |
| 369 | printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size); |
| 370 | return -1; |
| 371 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 372 | if(strstr(file_name, ".txt")) { |
| 373 | for (b = 0; b < words; b++) { |
| 374 | a = 0; |
| 375 | while (1) { |
| 376 | vocab[b * max_w + a] = fgetc(f); |
| 377 | if (feof(f) || (vocab[b * max_w + a] == ' ')) break; |
| 378 | if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++; |
| 379 | } |
| 380 | vocab[b * max_w + a] = 0; |
| 381 | len = 0; |
| 382 | for (a = 0; a < size; a++) { |
| 383 | fscanf(f, "%lf", &val); |
| 384 | M[a + b * size] = val; |
| 385 | len += val * val; |
| 386 | } |
| 387 | len = sqrt(len); |
| 388 | for (a = 0; a < size; a++) M[a + b * size] /= len; |
| 389 | } |
| 390 | } else { |
| 391 | for (b = 0; b < words; b++) { |
| 392 | a = 0; |
| 393 | while (1) { |
| 394 | vocab[b * max_w + a] = fgetc(f); |
| 395 | if (feof(f) || (vocab[b * max_w + a] == ' ')) break; |
| 396 | if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++; |
| 397 | } |
| 398 | vocab[b * max_w + a] = 0; |
| 399 | fread(&M[b * size], sizeof(float), size, f); |
| 400 | len = 0; |
| 401 | for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size]; |
| 402 | len = sqrt(len); |
| 403 | for (a = 0; a < size; a++) M[a + b * size] /= len; |
| 404 | } |
| 405 | } |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 406 | if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) { |
| 407 | fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs); |
| 408 | fclose(binvecs); |
| 409 | fwrite(vocab, sizeof(char), (long long)words * max_w, binwords); |
| 410 | fclose(binwords); |
| 411 | } |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 412 | } |
| 413 | if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) { |
| 414 | M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0); |
| 415 | vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0); |
| 416 | if (M == MAP_FAILED || vocab == MAP_FAILED) { |
| 417 | close(binvecs_fd); |
| 418 | close(binwords_fd); |
| 419 | fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname); |
| 420 | exit(-1); |
| 421 | } |
| 422 | } else { |
| 423 | fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname); |
| 424 | exit(-1); |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 425 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 426 | fclose(f); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 427 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 428 | if(net_name && strlen(net_name) > 0) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 429 | if( (net_fd = open(net_name, O_RDONLY)) >= 0) { |
| 430 | window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2; |
| 431 | // lseek(net_fd, sizeof(float) * words * size, SEEK_SET); |
Marc Kupietz | 10bec2b | 2016-03-23 09:41:31 +0100 | [diff] [blame] | 432 | // munmap(M, sizeof(float) * words * size); |
| 433 | 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] | 434 | if (M2 == MAP_FAILED) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 435 | close(net_fd); |
| 436 | fprintf(stderr, "Cannot mmap %s\n", net_name); |
| 437 | exit(-1); |
| 438 | } |
Marc Kupietz | 10bec2b | 2016-03-23 09:41:31 +0100 | [diff] [blame] | 439 | syn1neg_window = M2 + words * size; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 440 | } else { |
| 441 | fprintf(stderr, "Cannot open %s\n", net_name); |
| 442 | exit(-1); |
| 443 | } |
| 444 | fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window); |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 445 | |
| 446 | char collocatordb_name[2048]; |
| 447 | strcpy(collocatordb_name, net_name); |
| 448 | char *ext = rindex(collocatordb_name, '.'); |
| 449 | if(ext) { |
| 450 | strcpy(ext, ".rocksdb"); |
| 451 | if(access(collocatordb_name, R_OK) == 0) { |
| 452 | *ext = 0; |
| 453 | fprintf(stderr, "Opening collocator DB %s\n", collocatordb_name); |
| 454 | cdb = open_collocatordb(collocatordb_name); |
| 455 | } |
| 456 | } |
| 457 | } |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 458 | |
| 459 | expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float)); |
| 460 | for (i = 0; i < EXP_TABLE_SIZE; i++) { |
| 461 | expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table |
| 462 | expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1) |
| 463 | } |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 464 | window_sums = malloc(sizeof(float) * (window+1) * 2); |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 465 | |
| 466 | return 0; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 467 | } |
| 468 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 469 | long mergeVectors(char *file_name){ |
| 470 | FILE *f, *binvecs, *binwords; |
| 471 | int binwords_fd, binvecs_fd, net_fd, i; |
| 472 | long long a, b, c, d, cn; |
| 473 | float len; |
| 474 | float *merge_vecs; |
| 475 | char *merge_vocab; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 476 | /* long long merge_words, merge_size; */ |
| 477 | long long merge_size; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 478 | |
| 479 | char binvecs_fname[256], binwords_fname[256]; |
| 480 | strcpy(binwords_fname, file_name); |
| 481 | strcat(binwords_fname, ".words"); |
| 482 | strcpy(binvecs_fname, file_name); |
| 483 | strcat(binvecs_fname, ".vecs"); |
| 484 | |
| 485 | f = fopen(file_name, "rb"); |
| 486 | if (f == NULL) { |
| 487 | printf("Input file %s not found\n", file_name); |
| 488 | exit -1; |
| 489 | } |
| 490 | fscanf(f, "%lld", &merge_words); |
| 491 | fscanf(f, "%lld", &merge_size); |
| 492 | if(merge_size != size){ |
| 493 | fprintf(stderr, "vectors must have the same length\n"); |
| 494 | exit(-1); |
| 495 | } |
| 496 | if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) { |
| 497 | merge_vecs = malloc(sizeof(float) * (words + merge_words) * size); |
| 498 | merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w); |
| 499 | if (merge_vecs == NULL || merge_vocab == NULL) { |
| 500 | close(binvecs_fd); |
| 501 | close(binwords_fd); |
| 502 | fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname); |
| 503 | exit(-1); |
| 504 | } |
| 505 | read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float)); |
| 506 | read(binwords_fd, merge_vocab, merge_words * max_w); |
| 507 | } else { |
| 508 | fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname); |
| 509 | exit(-1); |
| 510 | } |
| 511 | printf("Successfully reallocated memory\nMerging...\n"); |
| 512 | fflush(stdout); |
| 513 | memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float)); |
| 514 | memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w); |
| 515 | munmap(M, words * size * sizeof(float)); |
| 516 | munmap(vocab, words * max_w); |
| 517 | M = merge_vecs; |
| 518 | vocab = merge_vocab; |
| 519 | merged_end = merge_words; |
| 520 | words += merge_words; |
| 521 | fclose(f); |
| 522 | printf("merged_end: %lld, words: %lld\n", merged_end, words); |
| 523 | return((long) merged_end); |
| 524 | } |
| 525 | |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 526 | void filter_garbage() { |
| 527 | long i; |
Marc Kupietz | ab591a8 | 2016-04-28 14:08:49 +0200 | [diff] [blame] | 528 | unsigned char *w, previous, c; |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 529 | garbage = malloc(words); |
| 530 | memset(garbage, 0, words); |
| 531 | for (i = 0; i < words; i++) { |
| 532 | w = vocab + i * max_w; |
| 533 | previous = 0; |
Marc Kupietz | 969adfa | 2018-03-12 09:43:53 +0100 | [diff] [blame] | 534 | if(strncmp("quot", w, 4) == 0) { |
| 535 | garbage[i]=1; |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 536 | // printf("Gargabe: %s\n", vocab + i * max_w); |
Marc Kupietz | 969adfa | 2018-03-12 09:43:53 +0100 | [diff] [blame] | 537 | } else { |
| 538 | while((c = *w++) && !garbage[i]) { |
| 539 | if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) || |
| 540 | (previous == '-' && (c & 32)) || |
| 541 | (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) || |
| 542 | (previous == 'q' && c == 'u' && *(w) == 'o' && *(w+1) == 't') || /* quot */ |
| 543 | c == '<' |
| 544 | ) { |
| 545 | garbage[i]=1; |
| 546 | continue; |
| 547 | } |
| 548 | previous = c; |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 549 | } |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | return; |
| 553 | } |
| 554 | |
Marc Kupietz | 580ebdf | 2017-11-29 21:18:38 +0100 | [diff] [blame] | 555 | void *getCollocators(void *args) { |
| 556 | knnpars *pars = args; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 557 | int N = pars->N; |
| 558 | int cc = pars->wl->wordi[0]; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 559 | knn *nbs = NULL; |
| 560 | long window_layer_size = size * window * 2; |
| 561 | long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target; |
| 562 | float f, max_f, maxmax_f; |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 563 | float *target_sums, worstbest, wpos_sum; |
| 564 | collocator *best; |
Marc Kupietz | d564258 | 2016-03-19 22:23:13 +0100 | [diff] [blame] | 565 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 566 | if(M2 == NULL || cc == -1) |
Marc Kupietz | d564258 | 2016-03-19 22:23:13 +0100 | [diff] [blame] | 567 | return NULL; |
| 568 | |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 569 | a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float)); |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 570 | best = malloc(N * sizeof(collocator)); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 571 | worstbest = MIN_RESP; |
| 572 | |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 573 | for (b = 0; b < pars->cutoff; b++) |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 574 | target_sums[b]=0; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 575 | for (b = 0; b < N; b++) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 576 | best[b].wordi = -1; |
| 577 | best[b].probability = 1; |
| 578 | best[b].activation = worstbest; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 579 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 580 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 581 | d = cc; |
| 582 | maxmax_f = -1; |
| 583 | maxmax_target = 0; |
| 584 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 585 | for (a = pars->from; a < pars->upto; a++) { |
| 586 | if(a >= window) |
| 587 | a++; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 588 | wpos_sum = 0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 589 | printf("window pos: %ld\n", a); |
| 590 | if (a != window) { |
| 591 | max_f = -1; |
| 592 | window_offset = a * size; |
| 593 | if (a > window) |
| 594 | window_offset -= size; |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 595 | for(target = 0; target < pars->cutoff; target ++) { |
| 596 | if(garbage && garbage[target]) continue; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 597 | if(target == d) |
| 598 | continue; |
| 599 | f = 0; |
| 600 | for (c = 0; c < size; c++) |
Marc Kupietz | 10bec2b | 2016-03-23 09:41:31 +0100 | [diff] [blame] | 601 | 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] | 602 | if (f < -MAX_EXP) |
| 603 | continue; |
| 604 | else if (f > MAX_EXP) |
| 605 | continue; |
| 606 | else |
| 607 | f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))]; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 608 | wpos_sum += f; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 609 | |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 610 | target_sums[target] += f; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 611 | if(f > worstbest) { |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 612 | for (b = 0; b < N; b++) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 613 | if (f > best[b].activation) { |
| 614 | memmove(best + b + 1, best + b, (N - b -1) * sizeof(collocator)); |
| 615 | best[b].activation = f; |
| 616 | best[b].wordi = target; |
| 617 | best[b].position = window-a; |
Marc Kupietz | 33679a3 | 2016-03-22 08:49:39 +0100 | [diff] [blame] | 618 | break; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 619 | } |
| 620 | } |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 621 | if(b == N - 1) |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 622 | worstbest = best[N-1].activation; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | printf("%d %.2f\n", max_target, max_f); |
| 626 | printf("%s (%.2f) ", &vocab[max_target * max_w], max_f); |
| 627 | if(max_f > maxmax_f) { |
| 628 | maxmax_f = max_f; |
| 629 | maxmax_target = max_target; |
| 630 | } |
Marc Kupietz | 33679a3 | 2016-03-22 08:49:39 +0100 | [diff] [blame] | 631 | for (b = 0; b < N; b++) |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 632 | if(best[b].position == window-a) |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 633 | best[b].cprobability = best[b].activation / wpos_sum; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 634 | } else { |
| 635 | printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]); |
| 636 | } |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 637 | pars->window_sums[a] = wpos_sum; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 638 | } |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 639 | for (b = 0; b < pars->cutoff; b++) |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 640 | pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2); |
| 641 | printf("Target-Summe von 0: %f\n", pars->target_sums[150298]); |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 642 | free(target_sums); |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 643 | for(b=0; b<N && best[b].wordi >= 0; b++);; // THIS LOOP IS NEEDED (b...) |
| 644 | // 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] | 645 | // printf("\n"); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 646 | nbs = malloc(sizeof(knn)); |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 647 | nbs->best = best; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 648 | nbs->length = b-1; |
| 649 | pthread_exit(nbs); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 650 | } |
| 651 | |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 652 | |
Marc Kupietz | 66bfd95 | 2017-12-11 09:59:45 +0100 | [diff] [blame] | 653 | AV *getVecs(AV *array) { |
| 654 | int i, b; |
| 655 | AV *result = newAV(); |
| 656 | for (i=0; i<=av_len(array); i++) { |
| 657 | SV** elem = av_fetch(array, i, 0); |
| 658 | if (elem != NULL) { |
| 659 | long j = (long) SvNV(*elem); |
| 660 | AV *vector = newAV(); |
| 661 | for (b = 0; b < size; b++) { |
| 662 | av_push(vector, newSVnv(M[b + j * size])); |
| 663 | } |
| 664 | av_push(result, newRV_noinc(vector)); |
| 665 | } |
| 666 | } |
| 667 | return result; |
| 668 | } |
| 669 | |
Marc Kupietz | a51dcfa | 2018-03-19 16:22:05 +0100 | [diff] [blame] | 670 | char *getSimilarProfiles(long node) { |
| 671 | int i; |
| 672 | char buffer[120000]; |
| 673 | char pair_buffer[2048]; |
| 674 | buffer[0]='['; |
| 675 | buffer[1]=0; |
| 676 | if(node >= sprofiles_qty) { |
| 677 | printf("Not available in precomputed profile\n"); |
| 678 | return(strdup("[{\"w\":\"not available\", \"v\":0}]\n")); |
| 679 | } |
| 680 | |
| 681 | printf("******* %s ******\n", &vocab[max_w * node]); |
| 682 | |
| 683 | for(i=0; i < 100 && i < sprofiles[node].len; i++) { |
| 684 | sprintf(pair_buffer, "{\"w\":\"%s\", \"v\":%f},", &vocab[max_w * (sprofiles[node].nbr[i].index)], sprofiles[node].nbr[i].value); |
| 685 | strcat(buffer, pair_buffer); |
| 686 | } |
| 687 | buffer[strlen(buffer)-1]=']'; |
| 688 | strcat(buffer, "\n"); |
| 689 | printf(buffer); |
| 690 | return(strdup(buffer)); |
| 691 | } |
| 692 | |
Marc Kupietz | e243efd | 2018-01-11 22:19:24 +0100 | [diff] [blame] | 693 | char *getClassicCollocators(long node) { |
| 694 | char *res = (cdb? strdup(get_collocators_as_json(cdb, node)) : "[]"); |
| 695 | return res; |
| 696 | } |
| 697 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 698 | wordlist *getTargetWords(char *st1, int search_backw) { |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 699 | wordlist *wl = malloc(sizeof(wordlist)); |
| 700 | char st[100][max_size], sep[100]; |
| 701 | long a, b=0, c=0, cn=0; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 702 | int unmerged; |
| 703 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 704 | while (1) { |
| 705 | st[cn][b] = st1[c]; |
| 706 | b++; |
| 707 | c++; |
| 708 | st[cn][b] = 0; |
| 709 | if (st1[c] == 0) break; |
Marc Kupietz | 95aa1c0 | 2016-03-15 09:40:43 +0100 | [diff] [blame] | 710 | if (st1[c] == ' ' || st1[c] == '-') { |
| 711 | sep[cn++] = st1[c]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 712 | b = 0; |
| 713 | c++; |
| 714 | } |
| 715 | } |
| 716 | cn++; |
| 717 | for (a = 0; a < cn; a++) { |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 718 | if(search_backw) { |
| 719 | for (b = words - 1; b >= 0; b--) if (!strcmp(&vocab[b * max_w], st[a])) break; |
| 720 | } else { |
| 721 | for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break; |
| 722 | } |
Marc Kupietz | 34a3ee9 | 2016-02-27 22:43:16 +0100 | [diff] [blame] | 723 | if (b == words) b = -1; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 724 | wl->wordi[a] = b; |
| 725 | 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] | 726 | if (b == -1) { |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 727 | fprintf(stderr, "Out of dictionary word!\n"); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 728 | cn--; |
Marc Kupietz | f9ac54e | 2017-11-21 09:22:29 +0100 | [diff] [blame] | 729 | free(wl); |
| 730 | return NULL; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 731 | } |
| 732 | } |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 733 | wl->length=cn; |
| 734 | return(wl); |
| 735 | } |
| 736 | |
Marc Kupietz | 580ebdf | 2017-11-29 21:18:38 +0100 | [diff] [blame] | 737 | void *_get_neighbours(void *arg) { |
| 738 | knnpars *pars = arg; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 739 | char *st1 = pars->token; |
| 740 | int N = pars->N; |
| 741 | long from = pars -> from; |
| 742 | unsigned long upto = pars -> upto; |
| 743 | char file_name[max_size], st[100][max_size], *sep; |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 744 | float dist, len, vec[max_size]; |
| 745 | long long a, b, c, d, cn, *bi; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 746 | char ch; |
| 747 | knn *nbs = NULL; |
| 748 | wordlist *wl = pars->wl; |
| 749 | |
Marc Kupietz | c55c887 | 2017-12-01 23:27:10 +0100 | [diff] [blame] | 750 | collocator *best = pars->best; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 751 | |
| 752 | float worstbest=-1; |
| 753 | |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 754 | for (a = 0; a < N; a++) best[a].activation = 0; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 755 | a = 0; |
| 756 | bi = wl->wordi; |
| 757 | cn = wl->length; |
| 758 | sep = wl->sep; |
| 759 | b = bi[0]; |
| 760 | c = 0; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 761 | if (b == -1) { |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 762 | N = 0; |
| 763 | goto end; |
| 764 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 765 | for (a = 0; a < size; a++) vec[a] = 0; |
| 766 | for (b = 0; b < cn; b++) { |
| 767 | if (bi[b] == -1) continue; |
Marc Kupietz | 95aa1c0 | 2016-03-15 09:40:43 +0100 | [diff] [blame] | 768 | if(b>0 && sep[b-1] == '-') |
| 769 | for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size]; |
| 770 | else |
| 771 | 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] | 772 | } |
| 773 | len = 0; |
| 774 | for (a = 0; a < size; a++) len += vec[a] * vec[a]; |
| 775 | len = sqrt(len); |
| 776 | for (a = 0; a < size; a++) vec[a] /= len; |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 777 | for (a = 0; a < N; a++) best[a].activation = -1; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 778 | for (c = from; c < upto; c++) { |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 779 | if(garbage && garbage[c]) continue; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 780 | a = 0; |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 781 | // do not skip taget word |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 782 | // for (b = 0; b < cn; b++) if (bi[b] == c) a = 1; |
| 783 | // if (a == 1) continue; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 784 | dist = 0; |
| 785 | 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] | 786 | if(dist > worstbest) { |
| 787 | for (a = 0; a < N; a++) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 788 | if (dist > best[a].activation) { |
| 789 | memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator)); |
| 790 | best[a].activation = dist; |
| 791 | best[a].wordi = c; |
Marc Kupietz | be1b9fc | 2016-02-26 10:34:30 +0100 | [diff] [blame] | 792 | break; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 793 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 794 | } |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 795 | worstbest = best[N-1].activation; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 796 | } |
| 797 | } |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 798 | |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 799 | end: |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 800 | pthread_exit(nbs); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 801 | } |
| 802 | |
Marc Kupietz | e0e4213 | 2017-11-24 16:44:34 +0100 | [diff] [blame] | 803 | int cmp_activation (const void * a, const void * b) { |
| 804 | float fb = ((collocator *)a)->activation; |
| 805 | float fa = ((collocator *)b)->activation; |
| 806 | return (fa > fb) - (fa < fb); |
| 807 | } |
| 808 | |
| 809 | int cmp_probability (const void * a, const void * b) { |
| 810 | float fb = ((collocator *)a)->probability; |
| 811 | float fa = ((collocator *)b)->probability; |
| 812 | return (fa > fb) - (fa < fb); |
| 813 | } |
| 814 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 815 | |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 816 | 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] | 817 | HV *result = newHV(); |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 818 | float *target_sums, vec[max_size]; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 819 | long long old_words; |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 820 | long a, b, c, d, slice; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 821 | knn *para_nbs[MAX_THREADS]; |
| 822 | knn *syn_nbs[MAX_THREADS]; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 823 | knnpars pars[MAX_THREADS]; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 824 | pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t)); |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 825 | wordlist *wl; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 826 | int syn_threads = (M2? window * 2 : 0); |
| 827 | int para_threads = num_threads - syn_threads; |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 828 | |
Marc Kupietz | c55c887 | 2017-12-01 23:27:10 +0100 | [diff] [blame] | 829 | collocator *best; |
| 830 | posix_memalign((void **) &best, 128, 10 * N * sizeof(collocator)); |
Marc Kupietz | db2dc7e | 2017-12-02 12:04:03 +0100 | [diff] [blame] | 831 | memset(best, 0, 10 * N * sizeof(collocator)); |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 832 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 833 | if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS; |
| 834 | |
Marc Kupietz | d5d0573 | 2018-01-30 11:57:35 +0100 | [diff] [blame] | 835 | if(cutoff < 1 || cutoff > words) |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 836 | cutoff=words; |
| 837 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 838 | wl = getTargetWords(st1, search_backw); |
Marc Kupietz | f9ac54e | 2017-11-21 09:22:29 +0100 | [diff] [blame] | 839 | if(wl == NULL || wl->length < 1) |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 840 | goto end; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 841 | |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 842 | old_words = cutoff; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 843 | if(merge_words > 0) |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 844 | cutoff = merge_words * 1.25; /* HACK */ |
| 845 | slice = cutoff / para_threads; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 846 | |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 847 | a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float)); |
| 848 | for(a = 0; a < cutoff; a++) |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 849 | target_sums[a] = 0; |
| 850 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 851 | printf("Starting %d threads\n", para_threads); |
| 852 | fflush(stdout); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 853 | for(a=0; a < para_threads; a++) { |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 854 | pars[a].cutoff = cutoff; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 855 | pars[a].token = st1; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 856 | pars[a].wl = wl; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 857 | pars[a].N = N; |
Marc Kupietz | c55c887 | 2017-12-01 23:27:10 +0100 | [diff] [blame] | 858 | pars[a].best = &best[N*a]; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 859 | pars[a].from = a*slice; |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 860 | pars[a].upto = ((a+1)*slice > cutoff? cutoff:(a+1)*slice); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 861 | pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]); |
| 862 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 863 | if(M2) { |
| 864 | for(a=0; a < syn_threads; a++) { |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 865 | pars[a + para_threads].cutoff = cutoff; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 866 | pars[a + para_threads].target_sums = target_sums; |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 867 | pars[a + para_threads].window_sums = window_sums; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 868 | pars[a + para_threads].wl = wl; |
| 869 | pars[a + para_threads].N = N; |
| 870 | pars[a + para_threads].from = a; |
| 871 | pars[a + para_threads].upto = a+1; |
| 872 | pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]); |
| 873 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 874 | } |
| 875 | printf("Waiting for para threads to join\n"); |
| 876 | fflush(stdout); |
Marc Kupietz | 580ebdf | 2017-11-29 21:18:38 +0100 | [diff] [blame] | 877 | for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *) ¶_nbs[a]); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 878 | printf("Para threads joint\n"); |
| 879 | fflush(stdout); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 880 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 881 | /* if(!syn_nbs[0]) */ |
| 882 | /* goto end; */ |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 883 | |
Marc Kupietz | c55c887 | 2017-12-01 23:27:10 +0100 | [diff] [blame] | 884 | qsort(best, N*para_threads, sizeof(collocator), cmp_activation); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 885 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 886 | |
Marc Kupietz | d0d3e91 | 2017-12-01 22:09:43 +0100 | [diff] [blame] | 887 | long long chosen[MAX_NEIGHBOURS]; |
| 888 | printf("N: %ld\n", N); |
| 889 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 890 | AV* array = newAV(); |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 891 | int i, j; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 892 | int l1_words=0, l2_words=0; |
Marc Kupietz | d0d3e91 | 2017-12-01 22:09:43 +0100 | [diff] [blame] | 893 | |
| 894 | for (a = 0, i = 0; i < N && a < N*para_threads; a++) { |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 895 | int filtered=0; |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 896 | long long c = best[a].wordi; |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 897 | if (dedupe && i > 0) { |
Marc Kupietz | d0d3e91 | 2017-12-01 22:09:43 +0100 | [diff] [blame] | 898 | for (j=0; j<i && !filtered; j++) |
| 899 | if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) || |
| 900 | strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) { |
| 901 | printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]); |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 902 | filtered = 1; |
| 903 | } |
| 904 | if(filtered) |
| 905 | continue; |
| 906 | } |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 907 | if(merge_words > 0) { |
| 908 | if(c >= merge_words) { |
| 909 | if(l1_words > N / 2) |
| 910 | continue; |
| 911 | else |
| 912 | l1_words++; |
| 913 | } else { |
| 914 | if(l2_words > N / 2) |
| 915 | continue; |
| 916 | else |
| 917 | l2_words++; |
| 918 | } |
| 919 | } |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 920 | printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a); |
Marc Kupietz | d0d3e91 | 2017-12-01 22:09:43 +0100 | [diff] [blame] | 921 | fflush(stdout); |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 922 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 923 | HV* hash = newHV(); |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 924 | SV* word = newSVpvf(&vocab[c * max_w], 0); |
Marc Kupietz | d0d3e91 | 2017-12-01 22:09:43 +0100 | [diff] [blame] | 925 | chosen[i] = c; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 926 | if(latin_enc == 0) SvUTF8_on(word); |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 927 | fflush(stdout); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 928 | hv_store(hash, "word", strlen("word"), word , 0); |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 929 | hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0); |
| 930 | hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 931 | AV *vector = newAV(); |
| 932 | for (b = 0; b < size; b++) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 933 | av_push(vector, newSVnv(M[b + best[a].wordi * size])); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 934 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 935 | hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0); |
| 936 | av_push(array, newRV_noinc((SV*)hash)); |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 937 | i++; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 938 | } |
| 939 | hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0); |
| 940 | |
Marc Kupietz | 50485ba | 2016-03-23 09:13:14 +0100 | [diff] [blame] | 941 | for(b=0; b < MAX_NEIGHBOURS; b++) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 942 | best[b].wordi = -1L; |
| 943 | best[b].activation = 0; |
| 944 | best[b].probability = 0; |
| 945 | best[b].position = 0; |
| 946 | best[b].activation_sum = 0; |
Marc Kupietz | 50485ba | 2016-03-23 09:13:14 +0100 | [diff] [blame] | 947 | } |
| 948 | |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 949 | float total_activation = 0; |
| 950 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 951 | if (M2) { |
| 952 | printf("Waiting for syn threads to join\n"); |
| 953 | fflush(stdout); |
Marc Kupietz | 580ebdf | 2017-11-29 21:18:38 +0100 | [diff] [blame] | 954 | for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], (void *) &syn_nbs[a]); |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 955 | for (a = 0; a <= syn_threads; a++) { |
Marc Kupietz | 6387218 | 2018-03-12 09:44:49 +0100 | [diff] [blame] | 956 | if(a == window) continue; |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 957 | total_activation += window_sums[a]; |
| 958 | printf("window pos: %d, sum: %f\n", a, window_sums[a]); |
| 959 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 960 | printf("syn threads joint\n"); |
| 961 | fflush(stdout); |
Marc Kupietz | 50485ba | 2016-03-23 09:13:14 +0100 | [diff] [blame] | 962 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 963 | for(b=0; b < syn_nbs[0]->length; b++) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 964 | memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator)); |
| 965 | best[b].position = -1; // syn_nbs[0]->pos[b]; |
| 966 | best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi]; |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 967 | best[b].max_activation = 0.0; |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 968 | best[b].average = 0.0; |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 969 | best[b].probability = 0.0; |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 970 | best[b].cprobability = syn_nbs[0]->best[b].cprobability; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 971 | } |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 972 | |
| 973 | float best_window_sum[MAX_NEIGHBOURS]; |
| 974 | int found_index=0, i=0, j, w; |
Marc Kupietz | 580ebdf | 2017-11-29 21:18:38 +0100 | [diff] [blame] | 975 | for(a=0; a < syn_threads; a++) { |
| 976 | for(b=0; b < syn_nbs[a]->length; b++) { |
| 977 | for(i=0; i < found_index; i++) |
| 978 | if(best[i].wordi == syn_nbs[a]->best[b].wordi) |
| 979 | break; |
| 980 | if(i >= found_index) { |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 981 | best[found_index].max_activation = 0.0; |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 982 | best[found_index].average = 0.0; |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 983 | best[found_index].probability = 0.0; |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 984 | best[found_index].cprobability = syn_nbs[a]->best[b].cprobability; |
| 985 | best[found_index].activation_sum = target_sums[syn_nbs[a]->best[b].wordi]; // syn_nbs[a]->best[b].activation_sum; |
Marc Kupietz | 580ebdf | 2017-11-29 21:18:38 +0100 | [diff] [blame] | 986 | best[found_index++].wordi = syn_nbs[a]->best[b].wordi; |
| 987 | // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]); |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 988 | } |
| 989 | } |
Marc Kupietz | 580ebdf | 2017-11-29 21:18:38 +0100 | [diff] [blame] | 990 | } |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 991 | sort_by =0; // ALWAYS AUTO-FOCUS |
Marc Kupietz | 580ebdf | 2017-11-29 21:18:38 +0100 | [diff] [blame] | 992 | if(sort_by != 1 && sort_by != 2) { // sort by auto focus mean |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 993 | 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] | 994 | int wpos; |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 995 | int bits_set = 0; |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 996 | for(i=0; i < found_index; i++) { |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 997 | best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0; |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 998 | for(w=1; w < (1 << syn_threads); w++) { // loop through all possible windows |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 999 | float word_window_sum = 0, word_window_average=0, word_cprobability_sum=0, word_activation_sum = 0, total_window_sum = 0; |
| 1000 | bits_set = 0; |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 1001 | for(a=0; a < syn_threads; a++) { |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 1002 | if((1 << a) & w) { |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 1003 | wpos = (a >= window? a+1 : a); |
| 1004 | total_window_sum += window_sums[wpos]; |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 1005 | } |
| 1006 | } |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 1007 | // printf("%d window-sum %f\n", w, total_window_sum); |
| 1008 | for(a=0; a < syn_threads; a++) { |
| 1009 | if((1 << a) & w) { |
| 1010 | wpos = (a >= window? a+1 : a); |
| 1011 | bits_set++; |
| 1012 | for(b=0; b < syn_nbs[a]->length; b++) |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1013 | if(best[i].wordi == syn_nbs[a]->best[b].wordi) { |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1014 | // float acti = syn_nbs[a]->best[b].activation / total_window_sum; |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 1015 | // word_window_sum += syn_nbs[a]->dist[b] * syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b]; |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 1016 | // word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b]; |
| 1017 | // 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 | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1018 | |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 1019 | word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b]; |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1020 | // word_window_sum += acti - (word_window_sum * acti); syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b]; |
| 1021 | |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1022 | word_window_average += syn_nbs[a]->best[b].activation; // - word_window_average * syn_nbs[a]->best[b].activation; // conormalied activation sum |
| 1023 | word_cprobability_sum += syn_nbs[a]->best[b].cprobability - word_cprobability_sum * syn_nbs[a]->best[b].cprobability; // conormalied column probability sum |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 1024 | word_activation_sum += syn_nbs[a]->best[b].activation; |
| 1025 | if(syn_nbs[a]->best[b].activation > best[i].max_activation) |
| 1026 | best[i].max_activation = syn_nbs[a]->best[b].activation; |
Marc Kupietz | db2dc7e | 2017-12-02 12:04:03 +0100 | [diff] [blame] | 1027 | if(syn_nbs[a]->best[b].activation > best[i].heat[wpos] ) |
Marc Kupietz | 728b8ed | 2017-12-02 11:13:49 +0100 | [diff] [blame] | 1028 | best[i].heat[wpos] = syn_nbs[a]->best[b].activation; |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | } |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1032 | if(bits_set) { |
| 1033 | word_window_average /= bits_set; |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 1034 | // word_activation_sum /= bits_set; |
| 1035 | // word_window_sum /= bits_set; |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1036 | } |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1037 | |
| 1038 | word_window_sum /= total_window_sum; |
Marc Kupietz | bd41da3 | 2017-11-24 10:26:43 +0100 | [diff] [blame] | 1039 | |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1040 | if(word_window_sum > best[i].probability) { |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1041 | // best[i].position = w; |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1042 | best[i].probability = word_window_sum; |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 1043 | } |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 1044 | |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1045 | if(word_cprobability_sum > best[i].cprobability_sum) { |
| 1046 | best[i].position = w; |
| 1047 | best[i].cprobability_sum = word_cprobability_sum; |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 1048 | } |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1049 | |
| 1050 | best[i].average = word_window_average; |
| 1051 | // best[i].activation = word_activation_sum; |
| 1052 | } |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 1053 | } |
Marc Kupietz | e0e4213 | 2017-11-24 16:44:34 +0100 | [diff] [blame] | 1054 | qsort(best, found_index, sizeof(collocator), cmp_probability); |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 1055 | // for(i=0; i < found_index; i++) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1056 | // 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] | 1057 | // } |
| 1058 | |
Marc Kupietz | 580ebdf | 2017-11-29 21:18:38 +0100 | [diff] [blame] | 1059 | } else if(sort_by == 1) { // responsiveness any window position |
| 1060 | int wpos; |
| 1061 | for(i=0; i < found_index; i++) { |
| 1062 | float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0; |
| 1063 | for(a=0; a < syn_threads; a++) { |
| 1064 | wpos = (a >= window? a+1 : a); |
| 1065 | for(b=0; b < syn_nbs[a]->length; b++) |
| 1066 | if(best[i].wordi == syn_nbs[a]->best[b].wordi) { |
| 1067 | best[i].probability += syn_nbs[a]->best[b].probability; |
| 1068 | if(syn_nbs[a]->best[b].activation > 0.25) |
| 1069 | best[i].position |= 1 << wpos; |
| 1070 | if(syn_nbs[a]->best[b].activation > best[i].activation) { |
| 1071 | best[i].activation = syn_nbs[a]->best[b].activation; |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | } |
| 1076 | qsort(best, found_index, sizeof(collocator), cmp_activation); |
| 1077 | } else if(sort_by == 2) { // single window position |
Marc Kupietz | 30ca434 | 2017-11-22 21:21:20 +0100 | [diff] [blame] | 1078 | for(a=1; a < syn_threads; a++) { |
| 1079 | for(b=0; b < syn_nbs[a]->length; b++) { |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1080 | for(c=0; c < MAX_NEIGHBOURS; c++) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1081 | if(syn_nbs[a]->best[b].activation > best[c].activation) { |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1082 | for(d=MAX_NEIGHBOURS-1; d>c; d--) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1083 | memmove(best + d, best + d - 1, sizeof(collocator)); |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1084 | } |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1085 | memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator)); |
| 1086 | 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] | 1087 | break; |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 1088 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1089 | } |
| 1090 | } |
| 1091 | } |
| 1092 | } else { // sort by mean p |
| 1093 | for(a=1; a < syn_threads; a++) { |
| 1094 | for(b=0; b < syn_nbs[a]->length; b++) { |
| 1095 | for(c=0; c < MAX_NEIGHBOURS; c++) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1096 | 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] | 1097 | for(d=MAX_NEIGHBOURS-1; d>c; d--) { |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1098 | memmove(best + d, best + d - 1, sizeof(collocator)); |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1099 | } |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1100 | memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator)); |
| 1101 | best[c].position = (1 << 2*window) - 1; // syn_nbs[a]->pos[b]; |
| 1102 | best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi]; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1103 | break; |
| 1104 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 1105 | } |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1109 | array = newAV(); |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1110 | for (a = 0, i=0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) { |
| 1111 | long long c = best[a].wordi; |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1112 | /* |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 1113 | if (dedupe) { |
| 1114 | int filtered=0; |
| 1115 | for (j=0; j<i; j++) |
| 1116 | if (strcasestr(&vocab[c * max_w], chosen[j]) || |
| 1117 | strcasestr(chosen[j], &vocab[c * max_w])) { |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1118 | printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]); |
| 1119 | filtered = 1; |
| 1120 | } |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame] | 1121 | if(filtered) |
| 1122 | continue; |
| 1123 | } |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1124 | */ |
Marc Kupietz | b6c615d | 2017-12-02 10:38:20 +0100 | [diff] [blame] | 1125 | chosen[i++]=c; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1126 | HV* hash = newHV(); |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1127 | SV* word = newSVpvf(&vocab[best[a].wordi * max_w], 0); |
Marc Kupietz | b6c615d | 2017-12-02 10:38:20 +0100 | [diff] [blame] | 1128 | AV* heat = newAV(); |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1129 | if(latin_enc == 0) SvUTF8_on(word); |
| 1130 | hv_store(hash, "word", strlen("word"), word , 0); |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1131 | hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0); |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1132 | hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0); |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 1133 | hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0); |
Marc Kupietz | 4116b43 | 2017-12-06 14:15:32 +0100 | [diff] [blame] | 1134 | hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0); |
Marc Kupietz | d64f3f2 | 2017-11-30 12:07:42 +0100 | [diff] [blame] | 1135 | hv_store(hash, "max", strlen("max"), newSVnv(best[a].max_activation), 0); // newSVnv(target_sums[best[a].wordi]), 0); |
Marc Kupietz | a77acce | 2017-11-30 16:59:07 +0100 | [diff] [blame] | 1136 | hv_store(hash, "overall", strlen("overall"), newSVnv(best[a].activation_sum/total_activation), 0); // newSVnv(target_sums[best[a].wordi]), 0); |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1137 | hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0); |
Marc Kupietz | b6c615d | 2017-12-02 10:38:20 +0100 | [diff] [blame] | 1138 | best[a].heat[5]=0; |
| 1139 | for(i=10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i])); |
| 1140 | hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV*)heat), 0); |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1141 | av_push(array, newRV_noinc((SV*)hash)); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 1142 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1143 | hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 1144 | } |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 1145 | end: |
Marc Kupietz | 580ebdf | 2017-11-29 21:18:38 +0100 | [diff] [blame] | 1146 | words = old_words; |
Marc Kupietz | c8fdf3c | 2017-11-24 15:32:19 +0100 | [diff] [blame] | 1147 | free(best); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 1148 | return newRV_noinc((SV*)result); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 1149 | } |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 1150 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1151 | int dump_vecs(char *fname) { |
| 1152 | long i, j; |
| 1153 | FILE *f; |
| 1154 | /* if(words>200000) */ |
| 1155 | /* words=200000; */ |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 1156 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 1157 | if((f=fopen(fname, "w")) == NULL) { |
| 1158 | fprintf(stderr, "cannot open %s for writing\n", fname); |
| 1159 | return(-1); |
| 1160 | } |
| 1161 | fprintf(f, "%lld %lld\n", words, size); |
| 1162 | for (i=0; i < words; i++) { |
| 1163 | fprintf(f, "%s ", &vocab[i * max_w]); |
| 1164 | for(j=0; j < size - 1; j++) |
| 1165 | fprintf(f, "%f ", M[i*size + j]); |
| 1166 | fprintf(f, "%f\n", M[i*size + j]); |
| 1167 | } |
| 1168 | fclose(f); |
| 1169 | return(0); |
| 1170 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 1171 | |