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