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