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