blob: 7702e0a1be33a26b91918df19802ece4ed0a7266 [file] [log] [blame]
Marc Kupietzdc22b982015-10-09 09:19:34 +02001#!/usr/local/bin/perl
2use Inline C;
Marc Kupietz00eecb12019-01-24 16:08:44 +01003use Inline C => Config => CLEAN_AFTER_BUILD => 0, BUILD_NOISY => 1, ccflags => $Config{ccflags}." -I/vol/work/kupietz/Work2/kl/trunk/CollocatorDB -I, -L. -Wall -O4", libs => "-shared -lpthread -lcollocatordb -lrt -lsnappy -lz -lbz2 -llz4 -lzstd -lrocksdb -lgomp";
Marc Kupietza5f60042017-05-04 10:38:12 +02004#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 Kupietzdc22b982015-10-09 09:19:34 +02006use Mojolicious::Lite;
Marc Kupietzc4893362016-02-25 08:04:46 +01007use Mojo::JSON qw(decode_json encode_json to_json);
Marc Kupietz30ca4342017-11-22 21:21:20 +01008use base 'Mojolicious::Plugin';
9
Marc Kupietz247500f2015-10-09 11:29:01 +020010use Encode qw(decode encode);
Marc Kupietza5b90152016-03-15 17:39:19 +010011use Getopt::Std;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010012use Mojo::Server::Daemon;
Marc Kupietzffef9302017-11-07 15:58:01 +010013use Cwd;
Marc Kupietz66bfd952017-12-11 09:59:45 +010014
Marc Kupietzffef9302017-11-07 15:58:01 +010015app->static->paths->[0] = getcwd;
16
Marc Kupietzd4227392016-03-01 16:45:12 +010017plugin 'Log::Access';
Marc Kupietzb3422c12017-07-04 14:12:11 +020018plugin "RequestBase";
Marc Kupietzdc22b982015-10-09 09:19:34 +020019
Marc Kupietza5b90152016-03-15 17:39:19 +010020our $opt_i = 0; # latin1-input?
21our $opt_l = undef;
22our $opt_p = 5676;
Marc Kupietza2e64502016-04-27 09:53:51 +020023our $opt_m;
Marc Kupietz6ed81872016-04-27 14:04:04 +020024our $opt_M;
Marc Kupietz43ee87e2016-04-25 10:50:08 +020025our $opt_n = '';
26our $opt_d;
Marc Kupietzfa194262018-06-05 09:39:32 +020027our $opt_D;
Marc Kupietz5c3887d2016-04-28 08:53:35 +020028our $opt_G;
Marc Kupietza5b90152016-03-15 17:39:19 +010029
Marc Kupietz6ed81872016-04-27 14:04:04 +020030my %marked;
Marc Kupietzc053d972019-01-10 10:41:51 +010031my $title="";
Marc Kupietz793413b2016-04-02 21:48:57 +020032my $training_args="";
Marc Kupietza2e64502016-04-27 09:53:51 +020033my $mergedEnd=0;
Marc Kupietz15987412017-11-07 15:56:58 +010034my %cache;
Marc Kupietz19c68242018-03-12 09:42:21 +010035my %cccache; # classic collocator cache
Marc Kupietza51dcfa2018-03-19 16:22:05 +010036my %spcache; # similar profile cache
Marc Kupietz793413b2016-04-02 21:48:57 +020037
Marc Kupietzfa194262018-06-05 09:39:32 +020038getopts('d:D:Gil:p:m:n:M:');
Marc Kupietz6ed81872016-04-27 14:04:04 +020039
40if($opt_M) {
Marc Kupietzed930212016-04-27 15:42:38 +020041 open my $handle, '<:encoding(UTF-8)', $opt_M
42 or die "Can't open '$opt_M' for reading: $!";
43 while(<$handle>) {
Marc Kupietz6ed81872016-04-27 14:04:04 +020044 foreach my $mw (split /\s+/) {
45 $marked{$mw}=1
46 }
47 }
Marc Kupietzed930212016-04-27 15:42:38 +020048 close($handle);
Marc Kupietz6ed81872016-04-27 14:04:04 +020049}
Marc Kupietza5b90152016-03-15 17:39:19 +010050
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010051# -cbow 1 -size 200 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 40 -binary 1 -iter 15
Marc Kupietza5b90152016-03-15 17:39:19 +010052if(!$ARGV[0]) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010053 init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0));
Marc Kupietz2cb667e2016-03-10 09:44:12 +010054} else {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010055 init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0));
Marc Kupietz793413b2016-04-02 21:48:57 +020056 if(open(FILE, "$ARGV[0].args")) {
57 $training_args = <FILE>;
58 }
59 close(FILE);
Marc Kupietzc053d972019-01-10 10:41:51 +010060 $title = fname2corpusname($ARGV[0]);
Marc Kupietz2cb667e2016-03-10 09:44:12 +010061}
Marc Kupietzdc22b982015-10-09 09:19:34 +020062
Marc Kupietza51dcfa2018-03-19 16:22:05 +010063my $have_sprofiles = load_sprofiles($ARGV[0]);
64
Marc Kupietza2e64502016-04-27 09:53:51 +020065if($opt_m) {
66 $mergedEnd = mergeVectors($opt_m);
Marc Kupietzc053d972019-01-10 10:41:51 +010067 $title = "<span class=\"merged\">" . $title . "</span> vs. " . fname2corpusname($opt_m);
Marc Kupietza2e64502016-04-27 09:53:51 +020068}
69
Marc Kupietze5568a02018-12-20 11:42:02 +010070
Marc Kupietz43ee87e2016-04-25 10:50:08 +020071if($opt_d) { # -d: dump vecs and exit
72 dump_vecs($opt_d);
73 exit;
74}
75
Marc Kupietzfa194262018-06-05 09:39:32 +020076if($opt_D) { # -D: dump vecs for numpy and exit
77 dump_for_numpy($opt_D);
78 exit;
79}
80
Marc Kupietza5b90152016-03-15 17:39:19 +010081my $daemon = Mojo::Server::Daemon->new(
82 app => app,
83 listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"]
84);
85
Marc Kupietz5c3887d2016-04-28 08:53:35 +020086if($opt_G) {
87 print "Filtering garbage\n";
88 filter_garbage();
89}
90
Marc Kupietz554aff52017-11-09 14:42:09 +010091get '*/js/*' => sub {
Marc Kupietzffef9302017-11-07 15:58:01 +010092 my $c = shift;
93 my $url = $c->req->url;
94 $url =~ s@/derekovecs@@g;
95 $c->app->log->info("GET: " . $url);
96 $c->reply->static($url);
97};
98
Marc Kupietza9270572018-03-17 15:17:07 +010099get '*/css/*' => sub {
100 my $c = shift;
101 my $url = $c->req->url;
102 $url =~ s@/derekovecs/@/@g;
103 $c->app->log->info("GET: " . $url);
104 $c->reply->static($url);
105};
106
Marc Kupietzc053d972019-01-10 10:41:51 +0100107sub fname2corpusname {
108 ($_) = @_;
109 s@.*/@@;
110 s@\..*@@;
111 return $_;
112}
113
Marc Kupietz19c68242018-03-12 09:42:21 +0100114sub getClassicCollocatorsCached {
115 my ($c, $word) = @_;
Marc Kupietz9ff3c992019-02-04 12:32:54 +0100116 if($word > $mergedEnd) {
117 $word-=$mergedEnd;
118 }
Marc Kupietz19c68242018-03-12 09:42:21 +0100119 if(!$cccache{$word}) {
120 $cccache{$word} = getClassicCollocators($word);
121 } else {
122 $c->app->log->info("Getting classic collocators for $word from cache.");
123 }
124 return $cccache{$word};
125}
126
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100127sub getSimilarProfilesCached {
128 my ($c, $word) = @_;
129 if(!$spcache{$word}) {
130 $spcache{$word} = getSimilarProfiles($word);
131 } else {
132 $c->app->log->info("Getting similar profiles for $word from cache:");
133 print $spcache{$word};
134 }
135 return $spcache{$word};
136}
137
Marc Kupietz66bfd952017-12-11 09:59:45 +0100138post '/derekovecs/getVecsByRanks' => sub {
139 my $self = shift;
140 my $vec = getVecs($self->req->json);
141 $self->render(json => $vec);
142};
143
Marc Kupietze13a3552018-01-25 08:48:34 +0100144any '*/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100145 my $self = shift;
Marc Kupietz19c68242018-03-12 09:42:21 +0100146 $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100147};
148
Marc Kupietze13a3552018-01-25 08:48:34 +0100149any '/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100150 my $self = shift;
Marc Kupietz19c68242018-03-12 09:42:21 +0100151 $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100152};
153
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100154any '*/getSimilarProfiles' => sub {
155 my $self = shift;
156 $self->render(data => getSimilarProfilesCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
157};
158
Marc Kupietzc987fa82018-03-21 12:14:25 +0100159any '/getSimilarProfiles' => sub {
160 my $self = shift;
161 $self->render(data => getSimilarProfilesCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
162};
163
Marc Kupietzdf3d4b52017-11-29 16:57:27 +0100164get '*/img/*' => sub {
165 my $c = shift;
166 my $url = $c->req->url;
167 $url =~ s@/derekovecs@@g;
168 $c->app->log->info("GET: " . $url);
169 $c->reply->static($url);
170};
171
Marc Kupietzdc22b982015-10-09 09:19:34 +0200172get '/' => sub {
173 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +0200174 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200175 my $word=$c->param('word');
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100176 my $no_nbs=$c->param('n') || 100;
177 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +0100178 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100179 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100180 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200181 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100182 my $sort=$c->param('sort') || 0;
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100183 my $csv=$c->param('csv') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +0200184 my $json=$c->param('json') || 0;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100185 my $cutoff=$c->param('cutoff') || 500000;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100186 my $dedupe=$c->param('dedupe') || 0;
Marc Kupietzac707b32018-12-20 11:36:38 +0100187 my $nosp=$c->param('nosp') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100188 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100189 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100190 my @collocations;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100191 if(defined($word) && $word !~ /^\s*$/) {
192 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100193 $word =~ s/\s+/ /g;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100194 if($opt_m && $word !~ /\|/) {
195 $word .= "|$word";
196 }
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100197 for my $w (split(' *\| *', $word)) {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100198 if($opt_m) {
199 if($searchBaseVocabFirst) {
200 $searchBaseVocabFirst=0;
201 } else {
202 $searchBaseVocabFirst=1;
203 }
204 }
205 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe,$searchBaseVocabFirst}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100206 $c->app->log->info("Getting $w results from cache");
Marc Kupietz3082fd02019-01-09 14:54:06 +0100207 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe.$searchBaseVocabFirst}
Marc Kupietza5b90152016-03-15 17:39:19 +0100208 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100209 $c->app->log->info('Looking for neighbours of '.$w);
210 if($opt_i) {
Marc Kupietzac707b32018-12-20 11:36:38 +0100211 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100212 } else {
Marc Kupietzac707b32018-12-20 11:36:38 +0100213 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100214 }
Marc Kupietz2dd2dd72017-12-01 22:08:14 +0100215 $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100216 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100217 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100218 }
219 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100220 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200221 if($json) {
222 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100223 } elsif($csv) {
224 my $csv_data="";
225 for (my $i=0; $i <= $no_nbs; $i++) {
226 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
227 }
228 for (my $i=0; $i < $no_nbs; $i++) {
229 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
230 }
231 chop $csv_data;
232 chop $csv_data;
233 $csv_data .= "\n";
234 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200235 } else {
Marc Kupietzc053d972019-01-10 10:41:51 +0100236 $c->render(template=>"index", title=>$title, word=>$word, cutoff=>$cutoff, no_nbs=>$no_nbs, no_iterations => $no_iterations, epsilon=> $epsilon, perplexity=> $perplexity, show_som=>$som, searchBaseVocabFirst=>$searchBaseVocabFirst, sort=>$sort, training_args=>$training_args, mergedEnd=> $mergedEnd, haveSProfiles=> $have_sprofiles, dedupe=> $dedupe, marked=>\%marked, lists=> \@lists, collocators=> $res->{syntagmatic});
Marc Kupietzb613b052016-04-28 14:11:59 +0200237 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200238};
239
Marc Kupietz30ca4342017-11-22 21:21:20 +0100240helper(bitvec2window => sub {
241 my ($self, $n) = @_;
242 my $str = unpack("B32", pack("N", $n));
243 $str =~ s/^\d{22}//;
244 $str =~ s/^(\d{5})/$1x/;
245 $str =~ s/0/·/g;
246 $str =~ s/1/+/g;
247 return $str;
248 });
249
Marc Kupietza5b90152016-03-15 17:39:19 +0100250$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200251
252exit;
253
254__END__
255
256__C__
257#include <stdio.h>
258#include <string.h>
259#include <math.h>
260#include <malloc.h>
261#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +0100262#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +0100263#include <pthread.h>
Marc Kupietze243efd2018-01-11 22:19:24 +0100264#include <collocatordb.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200265
266#define max_size 2000
267#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100268#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100269#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +0100270#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100271#define MAX_CC 50
272#define EXP_TABLE_SIZE 1000
273#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +0100274#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +0200275
276//the thread function
277void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +0100278
279typedef struct {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100280 long long wordi;
281 long position;
282 float activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100283 float average;
Marc Kupietza77acce2017-11-30 16:59:07 +0100284 float cprobability; // column wise probability
Marc Kupietz4116b432017-12-06 14:15:32 +0100285 float cprobability_sum;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100286 float probability;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100287 float activation_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100288 float max_activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100289 float heat[16];
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100290} collocator;
291
292typedef struct {
293 collocator *best;
Marc Kupietz80abb442016-03-23 21:04:08 +0100294 int length;
Marc Kupietz000ad862016-02-26 14:59:12 +0100295} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100296
Marc Kupietz000ad862016-02-26 14:59:12 +0100297typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +0100298 long long wordi[MAX_NEIGHBOURS];
299 char sep[MAX_NEIGHBOURS];
300 int length;
301} wordlist;
302
303typedef struct {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100304 long cutoff;
Marc Kupietz48c29682016-03-19 11:30:43 +0100305 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100306 char *token;
307 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100308 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100309 unsigned long upto;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100310 collocator *best;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100311 float *target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100312 float *window_sums;
Marc Kupietz000ad862016-02-26 14:59:12 +0100313} knnpars;
314
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100315typedef struct {
316 uint32_t index;
317 float value;
318} sparse_t;
319
320typedef struct {
321 uint32_t len;
322 sparse_t nbr[100];
323} profile_t;
324
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200325float *M, *M2=0L, *syn1neg_window, *expTable;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100326float *window_sums;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200327char *vocab;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200328char *garbage = NULL;
Marc Kupietze243efd2018-01-11 22:19:24 +0100329COLLOCATORDB *cdb = NULL;
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100330profile_t *sprofiles = NULL;
331size_t sprofiles_qty = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100332
Marc Kupietza2e64502016-04-27 09:53:51 +0200333long long words, size, merged_end;
Marc Kupietza5f60042017-05-04 10:38:12 +0200334long long merge_words = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100335int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100336int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100337int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200338
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100339/* load collocation profiles if file exists */
340int load_sprofiles(char *vecsname) {
341 char *basename = strdup(vecsname);
342 char *pos = strstr(basename, ".vecs");
343 if(pos)
344 *pos=0;
345
346 char binsprofiles_fname[256];
347 strcpy(binsprofiles_fname, basename);
348 strcat(binsprofiles_fname, ".sprofiles.bin");
349 FILE *fp = fopen(binsprofiles_fname, "rb");
350 if (fp == NULL) {
351 printf("Collocation profiles %s not found. No problem.\n", binsprofiles_fname);
352 return 0;
353 }
354 fseek(fp, 0L, SEEK_END);
355 size_t sz = ftell(fp);
356 fclose(fp);
357
358 int fd = open(binsprofiles_fname, O_RDONLY);
359 sprofiles = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0);
360 if (sprofiles == MAP_FAILED) {
361 close(fd);
362 fprintf(stderr, "Cannot mmap %s\n", binsprofiles_fname);
363 sprofiles = NULL;
364 return 0;
365 } else {
366 sprofiles_qty = sz / sizeof(profile_t);
367 fprintf(stderr, "Successfully mmaped %s containing similar profiles for %ld word forms.\n", binsprofiles_fname, sprofiles_qty);
368 }
369 return 1;
370}
371
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100372int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100373 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100374 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100375 long long a, b, c, d, cn;
376 float len;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200377 double val;
Marc Kupietz82b02672016-02-26 12:32:25 +0100378
Marc Kupietz67c20282016-02-26 09:42:00 +0100379 char binvecs_fname[256], binwords_fname[256];
380 strcpy(binwords_fname, file_name);
381 strcat(binwords_fname, ".words");
382 strcpy(binvecs_fname, file_name);
383 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200384
Marc Kupietza5b90152016-03-15 17:39:19 +0100385 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200386 f = fopen(file_name, "rb");
387 if (f == NULL) {
388 printf("Input file %s not found\n", file_name);
389 return -1;
390 }
391 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100392 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200393 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100394 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
395 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100396 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
397 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
398 if (M == NULL) {
399 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
400 return -1;
401 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200402 if(strstr(file_name, ".txt")) {
403 for (b = 0; b < words; b++) {
404 a = 0;
405 while (1) {
406 vocab[b * max_w + a] = fgetc(f);
407 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
408 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
409 }
410 vocab[b * max_w + a] = 0;
411 len = 0;
412 for (a = 0; a < size; a++) {
413 fscanf(f, "%lf", &val);
414 M[a + b * size] = val;
415 len += val * val;
416 }
417 len = sqrt(len);
418 for (a = 0; a < size; a++) M[a + b * size] /= len;
419 }
420 } else {
421 for (b = 0; b < words; b++) {
422 a = 0;
423 while (1) {
424 vocab[b * max_w + a] = fgetc(f);
425 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
426 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
427 }
428 vocab[b * max_w + a] = 0;
429 fread(&M[b * size], sizeof(float), size, f);
430 len = 0;
431 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
432 len = sqrt(len);
433 for (a = 0; a < size; a++) M[a + b * size] /= len;
434 }
435 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100436 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
437 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
438 fclose(binvecs);
439 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
440 fclose(binwords);
441 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100442 }
443 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
444 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
445 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
446 if (M == MAP_FAILED || vocab == MAP_FAILED) {
447 close(binvecs_fd);
448 close(binwords_fd);
449 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
450 exit(-1);
451 }
452 } else {
453 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
454 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100455 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200456 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100457
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200458 if(net_name && strlen(net_name) > 0) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100459 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
460 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
461 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100462 // munmap(M, sizeof(float) * words * size);
463 M2 = mmap(0, sizeof(float) * words * size + sizeof(float) * 2 * window * size * words, PROT_READ, MAP_SHARED, net_fd, 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200464 if (M2 == MAP_FAILED) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100465 close(net_fd);
466 fprintf(stderr, "Cannot mmap %s\n", net_name);
467 exit(-1);
468 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100469 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100470 } else {
471 fprintf(stderr, "Cannot open %s\n", net_name);
472 exit(-1);
473 }
474 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
Marc Kupietze243efd2018-01-11 22:19:24 +0100475
476 char collocatordb_name[2048];
477 strcpy(collocatordb_name, net_name);
478 char *ext = rindex(collocatordb_name, '.');
479 if(ext) {
480 strcpy(ext, ".rocksdb");
481 if(access(collocatordb_name, R_OK) == 0) {
482 *ext = 0;
483 fprintf(stderr, "Opening collocator DB %s\n", collocatordb_name);
484 cdb = open_collocatordb(collocatordb_name);
485 }
486 }
487 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100488
489 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
490 for (i = 0; i < EXP_TABLE_SIZE; i++) {
491 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
492 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
493 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100494 window_sums = malloc(sizeof(float) * (window+1) * 2);
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100495
496 return 0;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200497}
498
Marc Kupietza2e64502016-04-27 09:53:51 +0200499long mergeVectors(char *file_name){
500 FILE *f, *binvecs, *binwords;
501 int binwords_fd, binvecs_fd, net_fd, i;
502 long long a, b, c, d, cn;
503 float len;
504 float *merge_vecs;
505 char *merge_vocab;
Marc Kupietza5f60042017-05-04 10:38:12 +0200506 /* long long merge_words, merge_size; */
507 long long merge_size;
Marc Kupietza2e64502016-04-27 09:53:51 +0200508
509 char binvecs_fname[256], binwords_fname[256];
510 strcpy(binwords_fname, file_name);
511 strcat(binwords_fname, ".words");
512 strcpy(binvecs_fname, file_name);
513 strcat(binvecs_fname, ".vecs");
514
515 f = fopen(file_name, "rb");
516 if (f == NULL) {
517 printf("Input file %s not found\n", file_name);
518 exit -1;
519 }
520 fscanf(f, "%lld", &merge_words);
521 fscanf(f, "%lld", &merge_size);
522 if(merge_size != size){
523 fprintf(stderr, "vectors must have the same length\n");
524 exit(-1);
525 }
526 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
527 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
528 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
529 if (merge_vecs == NULL || merge_vocab == NULL) {
530 close(binvecs_fd);
531 close(binwords_fd);
532 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
533 exit(-1);
534 }
535 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
536 read(binwords_fd, merge_vocab, merge_words * max_w);
537 } else {
538 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
539 exit(-1);
540 }
541 printf("Successfully reallocated memory\nMerging...\n");
542 fflush(stdout);
543 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
544 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
545 munmap(M, words * size * sizeof(float));
546 munmap(vocab, words * max_w);
547 M = merge_vecs;
548 vocab = merge_vocab;
549 merged_end = merge_words;
550 words += merge_words;
551 fclose(f);
552 printf("merged_end: %lld, words: %lld\n", merged_end, words);
Marc Kupietzebc131e2019-01-03 16:24:27 +0100553 printBiggestMergedDifferences();
Marc Kupietza2e64502016-04-27 09:53:51 +0200554 return((long) merged_end);
555}
556
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200557void filter_garbage() {
558 long i;
Marc Kupietzab591a82016-04-28 14:08:49 +0200559 unsigned char *w, previous, c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200560 garbage = malloc(words);
561 memset(garbage, 0, words);
562 for (i = 0; i < words; i++) {
563 w = vocab + i * max_w;
564 previous = 0;
Marc Kupietz969adfa2018-03-12 09:43:53 +0100565 if(strncmp("quot", w, 4) == 0) {
566 garbage[i]=1;
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100567// printf("Gargabe: %s\n", vocab + i * max_w);
Marc Kupietz969adfa2018-03-12 09:43:53 +0100568 } else {
569 while((c = *w++) && !garbage[i]) {
570 if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
571 (previous == '-' && (c & 32)) ||
572 (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) ||
573 (previous == 'q' && c == 'u' && *(w) == 'o' && *(w+1) == 't') || /* quot */
574 c == '<'
575 ) {
576 garbage[i]=1;
577 continue;
578 }
579 previous = c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200580 }
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200581 }
582 }
583 return;
584}
585
Marc Kupietzac707b32018-12-20 11:36:38 +0100586
587knn *simpleGetCollocators(int word, int number, long cutoff, int *result) {
588 knnpars *pars = calloc(sizeof(knnpars), 1);
589 float *target_sums;
590 float *window_sums = malloc(sizeof(float) * (window+1) * 2);
591 pars->cutoff = (cutoff? cutoff : 300000);
592 long a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
593 for(a = 0; a < cutoff; a++)
594 target_sums[a] = 0;
595 pars->target_sums = target_sums;
596 pars->window_sums = window_sums;
597 pars->N = (number? number : 20);
598 pars->from = 0;
599 pars->upto = window * 2 -1;
Marc Kupietz7631e022019-01-25 12:04:42 +0100600 knn *syn_nbs = NULL; // = (knn*) getCollocators(pars);
Marc Kupietzac707b32018-12-20 11:36:38 +0100601 free(pars);
602 free(window_sums);
603 free(target_sums);
604 return syn_nbs;
605}
606
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100607void *getCollocators(void *args) {
608 knnpars *pars = args;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100609 int N = pars->N;
Marc Kupietz52702832019-01-15 17:55:52 +0100610
Marc Kupietz271e2a42016-03-22 11:37:43 +0100611 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100612 knn *nbs = NULL;
613 long window_layer_size = size * window * 2;
614 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
615 float f, max_f, maxmax_f;
Marc Kupietz7631e022019-01-25 12:04:42 +0100616 float *target_sums=NULL, worstbest, wpos_sum;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100617 collocator *best;
Marc Kupietzd5642582016-03-19 22:23:13 +0100618
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200619 if(M2 == NULL || cc == -1)
Marc Kupietzd5642582016-03-19 22:23:13 +0100620 return NULL;
621
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100622 a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietz7631e022019-01-25 12:04:42 +0100623 memset(target_sums, 0, pars->cutoff * sizeof(float));
624 best = malloc((N>200?N:200) * sizeof(collocator));
625 memset(best, 0, (N>200?N:200) * sizeof(collocator));
Marc Kupietz271e2a42016-03-22 11:37:43 +0100626 worstbest = MIN_RESP;
627
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100628 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100629 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100630 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100631 best[b].wordi = -1;
632 best[b].probability = 1;
633 best[b].activation = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100634 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100635
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100636 d = cc;
637 maxmax_f = -1;
638 maxmax_target = 0;
639
Marc Kupietz271e2a42016-03-22 11:37:43 +0100640 for (a = pars->from; a < pars->upto; a++) {
641 if(a >= window)
642 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100643 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100644 printf("window pos: %ld\n", a);
645 if (a != window) {
646 max_f = -1;
647 window_offset = a * size;
648 if (a > window)
649 window_offset -= size;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100650 for(target = 0; target < pars->cutoff; target ++) {
651 if(garbage && garbage[target]) continue;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100652 if(target == d)
653 continue;
654 f = 0;
655 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100656 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100657 if (f < -MAX_EXP)
658 continue;
659 else if (f > MAX_EXP)
660 continue;
661 else
662 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100663 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100664
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100665 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100666 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100667 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100668 if (f > best[b].activation) {
669 memmove(best + b + 1, best + b, (N - b -1) * sizeof(collocator));
670 best[b].activation = f;
671 best[b].wordi = target;
672 best[b].position = window-a;
Marc Kupietz33679a32016-03-22 08:49:39 +0100673 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100674 }
675 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100676 if(b == N - 1)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100677 worstbest = best[N-1].activation;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100678 }
679 }
680 printf("%d %.2f\n", max_target, max_f);
681 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
682 if(max_f > maxmax_f) {
683 maxmax_f = max_f;
684 maxmax_target = max_target;
685 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100686 for (b = 0; b < N; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100687 if(best[b].position == window-a)
Marc Kupietza77acce2017-11-30 16:59:07 +0100688 best[b].cprobability = best[b].activation / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100689 } else {
690 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
691 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100692 pars->window_sums[a] = wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100693 }
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100694 for (b = 0; b < pars->cutoff; b++)
Marc Kupietza77acce2017-11-30 16:59:07 +0100695 pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2);
696 printf("Target-Summe von 0: %f\n", pars->target_sums[150298]);
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100697 free(target_sums);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100698 for(b=0; b<N && best[b].wordi >= 0; b++);; // THIS LOOP IS NEEDED (b...)
699// printf("%d: best syn: %s %.2f %.5f\n", b, &vocab[best[b].wordi*max_w], best[b].activation, best[b].probability);
Marc Kupietzbd41da32017-11-24 10:26:43 +0100700// printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100701 nbs = malloc(sizeof(knn));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100702 nbs->best = best;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100703 nbs->length = b-1;
704 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100705}
706
Marc Kupietz30ca4342017-11-22 21:21:20 +0100707
Marc Kupietz66bfd952017-12-11 09:59:45 +0100708AV *getVecs(AV *array) {
709 int i, b;
710 AV *result = newAV();
711 for (i=0; i<=av_len(array); i++) {
712 SV** elem = av_fetch(array, i, 0);
713 if (elem != NULL) {
714 long j = (long) SvNV(*elem);
715 AV *vector = newAV();
716 for (b = 0; b < size; b++) {
717 av_push(vector, newSVnv(M[b + j * size]));
718 }
719 av_push(result, newRV_noinc(vector));
720 }
721 }
722 return result;
723}
724
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100725char *getSimilarProfiles(long node) {
726 int i;
727 char buffer[120000];
728 char pair_buffer[2048];
729 buffer[0]='[';
730 buffer[1]=0;
731 if(node >= sprofiles_qty) {
732 printf("Not available in precomputed profile\n");
733 return(strdup("[{\"w\":\"not available\", \"v\":0}]\n"));
734 }
735
736 printf("******* %s ******\n", &vocab[max_w * node]);
737
738 for(i=0; i < 100 && i < sprofiles[node].len; i++) {
739 sprintf(pair_buffer, "{\"w\":\"%s\", \"v\":%f},", &vocab[max_w * (sprofiles[node].nbr[i].index)], sprofiles[node].nbr[i].value);
740 strcat(buffer, pair_buffer);
741 }
742 buffer[strlen(buffer)-1]=']';
743 strcat(buffer, "\n");
744 printf(buffer);
745 return(strdup(buffer));
746}
747
Marc Kupietze243efd2018-01-11 22:19:24 +0100748char *getClassicCollocators(long node) {
749 char *res = (cdb? strdup(get_collocators_as_json(cdb, node)) : "[]");
750 return res;
751}
752
Marc Kupietz0957d532019-01-03 17:54:00 +0100753wordlist *getTargetWords(char *st1, int search_backw) {
Marc Kupietz48c29682016-03-19 11:30:43 +0100754 wordlist *wl = malloc(sizeof(wordlist));
755 char st[100][max_size], sep[100];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100756 long a, b=0, c=0, cn=0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200757 int unmerged;
758
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100759 while (1) {
760 st[cn][b] = st1[c];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100761 b++;
762 c++;
763 st[cn][b] = 0;
764 if (st1[c] == 0) break;
765 if (st1[c] == ' ' || st1[c] == '-') {
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100766 sep[cn++] = st1[c];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100767 b = 0;
768 c++;
769 }
770 }
771 cn++;
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100772 for (a = 0; a < cn; a++) {
773 if (search_backw) {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100774 for (b = words - 1; b >= (merge_words? merge_words : 0) && strcmp(&vocab[b * max_w], st[a]) !=0; b--);
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100775 } else {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100776 for (b = 0; b < (merge_words? merge_words : words) && strcmp(&vocab[b * max_w], st[a]) != 0; b++);
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100777 }
778 if (b == words) b = -1;
779 wl->wordi[a] = b;
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100780 if (b == -1) {
781 fprintf(stderr, "Out of dictionary word!\n");
782 cn--;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100783 } else {
784 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", &vocab[wl->wordi[a]*max_w], wl->wordi[a]);
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100785 }
786 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100787 wl->length=cn;
788 return(wl);
789}
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100790
Marc Kupietzebc131e2019-01-03 16:24:27 +0100791void printBiggestMergedDifferences() {
792 float dist, len, vec[max_size];
793 long long a, b, c, d, cn, *bi;
794 char ch;
795 knn *nbs = NULL;
796 int N = 100;
797
798 printf("Looking for biggest distances between main and merged vectors ...\n");
799 collocator *best;
800 best = malloc(N * sizeof(collocator));
801 memset(best, 0, N * sizeof(collocator));
802
803 float worstbest=1000000;
804
805 for (a = 0; a < N; a++) best[a].activation = worstbest;
806
807 for (c = 0; c < 10000; c++) {
808 if(garbage && garbage[c]) continue;
809 a = 0;
810 dist = 0;
811 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + (c+merged_end) * size];
812 if(dist < worstbest) {
813 for (a = 0; a < N; a++) {
814 if (dist < best[a].activation) {
815 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
816 best[a].activation = dist;
817 best[a].wordi = c;
818 break;
819 }
820 }
821 worstbest = best[N-1].activation;
822 }
823 }
824
825 printf("Most distant vectors for:\n ");
826 for (a = 0; a < N; a++) {
827 printf("%s ", &vocab[best[a].wordi * max_w]);
828 }
829 printf("\n");
830}
831
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100832void *_get_neighbours(void *arg) {
833 knnpars *pars = arg;
Marc Kupietz48c29682016-03-19 11:30:43 +0100834 char *st1 = pars->token;
835 int N = pars->N;
836 long from = pars -> from;
837 unsigned long upto = pars -> upto;
838 char file_name[max_size], st[100][max_size], *sep;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100839 float dist, len, vec[max_size];
840 long long a, b, c, d, cn, *bi;
Marc Kupietz48c29682016-03-19 11:30:43 +0100841 char ch;
842 knn *nbs = NULL;
843 wordlist *wl = pars->wl;
844
Marc Kupietzc55c8872017-12-01 23:27:10 +0100845 collocator *best = pars->best;
Marc Kupietz48c29682016-03-19 11:30:43 +0100846
847 float worstbest=-1;
848
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100849 for (a = 0; a < N; a++) best[a].activation = 0;
Marc Kupietz48c29682016-03-19 11:30:43 +0100850 a = 0;
851 bi = wl->wordi;
852 cn = wl->length;
853 sep = wl->sep;
854 b = bi[0];
855 c = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100856 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100857 N = 0;
858 goto end;
859 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200860 for (a = 0; a < size; a++) vec[a] = 0;
861 for (b = 0; b < cn; b++) {
862 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100863 if(b>0 && sep[b-1] == '-')
864 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
865 else
866 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200867 }
868 len = 0;
869 for (a = 0; a < size; a++) len += vec[a] * vec[a];
870 len = sqrt(len);
871 for (a = 0; a < size; a++) vec[a] /= len;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100872 for (a = 0; a < N; a++) best[a].activation = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100873 for (c = from; c < upto; c++) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200874 if(garbage && garbage[c]) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200875 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100876// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100877// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
878// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200879 dist = 0;
880 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100881 if(dist > worstbest) {
882 for (a = 0; a < N; a++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100883 if (dist > best[a].activation) {
884 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
885 best[a].activation = dist;
886 best[a].wordi = c;
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100887 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200888 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200889 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100890 worstbest = best[N-1].activation;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200891 }
892 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100893
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100894end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100895 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200896}
897
Marc Kupietze0e42132017-11-24 16:44:34 +0100898int cmp_activation (const void * a, const void * b) {
899 float fb = ((collocator *)a)->activation;
900 float fa = ((collocator *)b)->activation;
901 return (fa > fb) - (fa < fb);
902}
903
904int cmp_probability (const void * a, const void * b) {
905 float fb = ((collocator *)a)->probability;
906 float fa = ((collocator *)b)->probability;
907 return (fa > fb) - (fa < fb);
908}
909
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100910
Marc Kupietzac707b32018-12-20 11:36:38 +0100911SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe, int no_similar_profiles) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100912 HV *result = newHV();
Marc Kupietz7631e022019-01-25 12:04:42 +0100913 float *target_sums=NULL, vec[max_size];
Marc Kupietza5f60042017-05-04 10:38:12 +0200914 long long old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100915 long a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100916 knn *para_nbs[MAX_THREADS];
917 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100918 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100919 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100920 wordlist *wl;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200921 int syn_threads = (M2? window * 2 : 0);
Marc Kupietzac707b32018-12-20 11:36:38 +0100922 int para_threads = (no_similar_profiles? 0 : num_threads - syn_threads);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100923
Marc Kupietz7631e022019-01-25 12:04:42 +0100924 collocator *best = NULL;
925 posix_memalign((void **) &best, 128, 10 * (N>=200? N : 200) * sizeof(collocator));
926 memset(best, 0, (N>=200? N : 200) * sizeof(collocator));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100927
Marc Kupietz000ad862016-02-26 14:59:12 +0100928 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
929
Marc Kupietzd5d05732018-01-30 11:57:35 +0100930 if(cutoff < 1 || cutoff > words)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100931 cutoff=words;
932
Marc Kupietza2e64502016-04-27 09:53:51 +0200933 wl = getTargetWords(st1, search_backw);
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100934 if(wl == NULL || wl->length < 1)
Marc Kupietz271e2a42016-03-22 11:37:43 +0100935 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100936
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100937 old_words = cutoff;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100938 slice = cutoff / para_threads;
Marc Kupietza5f60042017-05-04 10:38:12 +0200939
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100940 a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float));
Marc Kupietz7631e022019-01-25 12:04:42 +0100941 memset(target_sums, 0, cutoff * sizeof(float));
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100942
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200943 printf("Starting %d threads\n", para_threads);
944 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100945 for(a=0; a < para_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100946 pars[a].cutoff = cutoff;
Marc Kupietz000ad862016-02-26 14:59:12 +0100947 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100948 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100949 pars[a].N = N;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100950 pars[a].best = &best[N*a];
Marc Kupietz3082fd02019-01-09 14:54:06 +0100951 if(merge_words == 0 || search_backw == 0) {
Marc Kupietz1f0f7802019-01-03 16:23:29 +0100952 pars[a].from = a*slice;
953 pars[a].upto = ((a+1)*slice > cutoff? cutoff : (a+1) * slice);
954 } else {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100955 pars[a].from = merge_words + a * slice;
956 pars[a].upto = merge_words + ((a+1)*slice > cutoff? cutoff : (a+1) * slice);
Marc Kupietz1f0f7802019-01-03 16:23:29 +0100957 }
958 printf("From: %ld, Upto: %ld\n", pars[a].from, pars[a].upto);
Marc Kupietz000ad862016-02-26 14:59:12 +0100959 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
960 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200961 if(M2) {
962 for(a=0; a < syn_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100963 pars[a + para_threads].cutoff = cutoff;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200964 pars[a + para_threads].target_sums = target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100965 pars[a + para_threads].window_sums = window_sums;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200966 pars[a + para_threads].wl = wl;
967 pars[a + para_threads].N = N;
968 pars[a + para_threads].from = a;
969 pars[a + para_threads].upto = a+1;
970 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
971 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100972 }
973 printf("Waiting for para threads to join\n");
974 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100975 for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *) &para_nbs[a]);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100976 printf("Para threads joint\n");
977 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +0100978
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200979 /* if(!syn_nbs[0]) */
980 /* goto end; */
Marc Kupietz000ad862016-02-26 14:59:12 +0100981
Marc Kupietzc55c8872017-12-01 23:27:10 +0100982 qsort(best, N*para_threads, sizeof(collocator), cmp_activation);
Marc Kupietz000ad862016-02-26 14:59:12 +0100983
Marc Kupietz000ad862016-02-26 14:59:12 +0100984
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100985 long long chosen[MAX_NEIGHBOURS];
986 printf("N: %ld\n", N);
987
Marc Kupietz271e2a42016-03-22 11:37:43 +0100988 AV* array = newAV();
Marc Kupietzd91212f2017-11-13 10:05:09 +0100989 int i, j;
Marc Kupietza5f60042017-05-04 10:38:12 +0200990 int l1_words=0, l2_words=0;
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100991
992 for (a = 0, i = 0; i < N && a < N*para_threads; a++) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100993 int filtered=0;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100994 long long c = best[a].wordi;
Marc Kupietze8b9dd22019-01-03 17:55:59 +0100995 if ((merge_words && dedupe && i > 1) || (!merge_words && dedupe && i > 0)) {
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100996 for (j=0; j<i && !filtered; j++)
997 if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) ||
998 strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) {
999 printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]);
Marc Kupietzd91212f2017-11-13 10:05:09 +01001000 filtered = 1;
1001 }
1002 if(filtered)
1003 continue;
1004 }
Marc Kupietz3082fd02019-01-09 14:54:06 +01001005
1006
1007 if(0 && merge_words > 0) {
Marc Kupietza5f60042017-05-04 10:38:12 +02001008 if(c >= merge_words) {
1009 if(l1_words > N / 2)
1010 continue;
1011 else
1012 l1_words++;
1013 } else {
1014 if(l2_words > N / 2)
1015 continue;
1016 else
1017 l2_words++;
1018 }
1019 }
Marc Kupietz3082fd02019-01-09 14:54:06 +01001020
1021// printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
1022// fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001023 HV* hash = newHV();
Marc Kupietza5f60042017-05-04 10:38:12 +02001024 SV* word = newSVpvf(&vocab[c * max_w], 0);
Marc Kupietzd0d3e912017-12-01 22:09:43 +01001025 chosen[i] = c;
Marc Kupietz271e2a42016-03-22 11:37:43 +01001026 if(latin_enc == 0) SvUTF8_on(word);
Marc Kupietza5f60042017-05-04 10:38:12 +02001027 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001028 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001029 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
1030 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001031 AV *vector = newAV();
1032 for (b = 0; b < size; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001033 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001034 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001035 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
1036 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietza5f60042017-05-04 10:38:12 +02001037 i++;
Marc Kupietz271e2a42016-03-22 11:37:43 +01001038 }
1039 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz7631e022019-01-25 12:04:42 +01001040
Marc Kupietz50485ba2016-03-23 09:13:14 +01001041 for(b=0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001042 best[b].wordi = -1L;
1043 best[b].activation = 0;
1044 best[b].probability = 0;
1045 best[b].position = 0;
1046 best[b].activation_sum = 0;
Marc Kupietz7631e022019-01-25 12:04:42 +01001047 memset(best[b].heat, 0, sizeof(float)*16);
Marc Kupietz50485ba2016-03-23 09:13:14 +01001048 }
1049
Marc Kupietza77acce2017-11-30 16:59:07 +01001050 float total_activation = 0;
1051
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001052 if (M2) {
1053 printf("Waiting for syn threads to join\n");
1054 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001055 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], (void *) &syn_nbs[a]);
Marc Kupietza77acce2017-11-30 16:59:07 +01001056 for (a = 0; a <= syn_threads; a++) {
Marc Kupietz63872182018-03-12 09:44:49 +01001057 if(a == window) continue;
Marc Kupietza77acce2017-11-30 16:59:07 +01001058 total_activation += window_sums[a];
1059 printf("window pos: %d, sum: %f\n", a, window_sums[a]);
1060 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001061 printf("syn threads joint\n");
1062 fflush(stdout);
Marc Kupietz50485ba2016-03-23 09:13:14 +01001063
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001064 for(b=0; b < syn_nbs[0]->length; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001065 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
1066 best[b].position = -1; // syn_nbs[0]->pos[b];
1067 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001068 best[b].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +01001069 best[b].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001070 best[b].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +01001071 best[b].cprobability = syn_nbs[0]->best[b].cprobability;
Marc Kupietz7631e022019-01-25 12:04:42 +01001072 memset(best[b].heat, 0, sizeof(float)*16);
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001073 }
Marc Kupietz30ca4342017-11-22 21:21:20 +01001074
1075 float best_window_sum[MAX_NEIGHBOURS];
1076 int found_index=0, i=0, j, w;
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001077 for(a=0; a < syn_threads; a++) {
1078 for(b=0; b < syn_nbs[a]->length; b++) {
1079 for(i=0; i < found_index; i++)
1080 if(best[i].wordi == syn_nbs[a]->best[b].wordi)
1081 break;
1082 if(i >= found_index) {
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001083 best[found_index].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +01001084 best[found_index].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001085 best[found_index].probability = 0.0;
Marc Kupietz7631e022019-01-25 12:04:42 +01001086 memset(best[found_index].heat, 0, sizeof(float)*16);
Marc Kupietza77acce2017-11-30 16:59:07 +01001087 best[found_index].cprobability = syn_nbs[a]->best[b].cprobability;
1088 best[found_index].activation_sum = target_sums[syn_nbs[a]->best[b].wordi]; // syn_nbs[a]->best[b].activation_sum;
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001089 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
1090 // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
Marc Kupietz30ca4342017-11-22 21:21:20 +01001091 }
1092 }
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001093 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001094 sort_by =0; // ALWAYS AUTO-FOCUS
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001095 if(sort_by != 1 && sort_by != 2) { // sort by auto focus mean
Marc Kupietz30ca4342017-11-22 21:21:20 +01001096 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) -1);
Marc Kupietzbd41da32017-11-24 10:26:43 +01001097 int wpos;
Marc Kupietz4116b432017-12-06 14:15:32 +01001098 int bits_set = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001099 for(i=0; i < found_index; i++) {
Marc Kupietz4116b432017-12-06 14:15:32 +01001100 best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001101 for(w=1; w < (1 << syn_threads); w++) { // loop through all possible windows
Marc Kupietz4116b432017-12-06 14:15:32 +01001102 float word_window_sum = 0, word_window_average=0, word_cprobability_sum=0, word_activation_sum = 0, total_window_sum = 0;
1103 bits_set = 0;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001104 for(a=0; a < syn_threads; a++) {
Marc Kupietz30ca4342017-11-22 21:21:20 +01001105 if((1 << a) & w) {
Marc Kupietzbd41da32017-11-24 10:26:43 +01001106 wpos = (a >= window? a+1 : a);
1107 total_window_sum += window_sums[wpos];
Marc Kupietz30ca4342017-11-22 21:21:20 +01001108 }
1109 }
Marc Kupietzbd41da32017-11-24 10:26:43 +01001110// printf("%d window-sum %f\n", w, total_window_sum);
1111 for(a=0; a < syn_threads; a++) {
1112 if((1 << a) & w) {
1113 wpos = (a >= window? a+1 : a);
1114 bits_set++;
1115 for(b=0; b < syn_nbs[a]->length; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001116 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001117// float acti = syn_nbs[a]->best[b].activation / total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001118// word_window_sum += syn_nbs[a]->dist[b] * syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
Marc Kupietzbd41da32017-11-24 10:26:43 +01001119// word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1120// 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 Kupietza77acce2017-11-30 16:59:07 +01001121
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001122 word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
Marc Kupietza77acce2017-11-30 16:59:07 +01001123// word_window_sum += acti - (word_window_sum * acti); syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1124
Marc Kupietz4116b432017-12-06 14:15:32 +01001125 word_window_average += syn_nbs[a]->best[b].activation; // - word_window_average * syn_nbs[a]->best[b].activation; // conormalied activation sum
1126 word_cprobability_sum += syn_nbs[a]->best[b].cprobability - word_cprobability_sum * syn_nbs[a]->best[b].cprobability; // conormalied column probability sum
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001127 word_activation_sum += syn_nbs[a]->best[b].activation;
1128 if(syn_nbs[a]->best[b].activation > best[i].max_activation)
1129 best[i].max_activation = syn_nbs[a]->best[b].activation;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +01001130 if(syn_nbs[a]->best[b].activation > best[i].heat[wpos] )
Marc Kupietz728b8ed2017-12-02 11:13:49 +01001131 best[i].heat[wpos] = syn_nbs[a]->best[b].activation;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001132 }
1133 }
1134 }
Marc Kupietz4116b432017-12-06 14:15:32 +01001135 if(bits_set) {
1136 word_window_average /= bits_set;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001137// word_activation_sum /= bits_set;
1138// word_window_sum /= bits_set;
Marc Kupietz4116b432017-12-06 14:15:32 +01001139 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001140
1141 word_window_sum /= total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001142
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001143 if(word_window_sum > best[i].probability) {
Marc Kupietz4116b432017-12-06 14:15:32 +01001144// best[i].position = w;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001145 best[i].probability = word_window_sum;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001146 }
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001147
Marc Kupietz4116b432017-12-06 14:15:32 +01001148 if(word_cprobability_sum > best[i].cprobability_sum) {
1149 best[i].position = w;
1150 best[i].cprobability_sum = word_cprobability_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001151 }
Marc Kupietz4116b432017-12-06 14:15:32 +01001152
1153 best[i].average = word_window_average;
1154// best[i].activation = word_activation_sum;
1155 }
Marc Kupietz30ca4342017-11-22 21:21:20 +01001156 }
Marc Kupietze0e42132017-11-24 16:44:34 +01001157 qsort(best, found_index, sizeof(collocator), cmp_probability);
Marc Kupietz30ca4342017-11-22 21:21:20 +01001158// for(i=0; i < found_index; i++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001159// printf("found: %s - sum: %f - window: %d\n", &vocab[best[i].wordi * max_w], best[i].activation, best[i].position);
Marc Kupietz30ca4342017-11-22 21:21:20 +01001160// }
1161
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001162 } else if(sort_by == 1) { // responsiveness any window position
1163 int wpos;
1164 for(i=0; i < found_index; i++) {
1165 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
1166 for(a=0; a < syn_threads; a++) {
1167 wpos = (a >= window? a+1 : a);
1168 for(b=0; b < syn_nbs[a]->length; b++)
1169 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
1170 best[i].probability += syn_nbs[a]->best[b].probability;
1171 if(syn_nbs[a]->best[b].activation > 0.25)
1172 best[i].position |= 1 << wpos;
1173 if(syn_nbs[a]->best[b].activation > best[i].activation) {
1174 best[i].activation = syn_nbs[a]->best[b].activation;
1175 }
1176 }
1177 }
1178 }
1179 qsort(best, found_index, sizeof(collocator), cmp_activation);
1180 } else if(sort_by == 2) { // single window position
Marc Kupietz30ca4342017-11-22 21:21:20 +01001181 for(a=1; a < syn_threads; a++) {
1182 for(b=0; b < syn_nbs[a]->length; b++) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001183 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001184 if(syn_nbs[a]->best[b].activation > best[c].activation) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001185 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001186 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001187 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001188 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
1189 best[c].position = 1 << (-syn_nbs[a]->best[b].position+window - (syn_nbs[a]->best[b].position < 0 ? 1:0));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001190 break;
Marc Kupietz6d9a6782016-03-23 17:25:25 +01001191 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001192 }
1193 }
1194 }
1195 } else { // sort by mean p
1196 for(a=1; a < syn_threads; a++) {
1197 for(b=0; b < syn_nbs[a]->length; b++) {
1198 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001199 if(target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001200 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001201 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001202 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001203 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
1204 best[c].position = (1 << 2*window) - 1; // syn_nbs[a]->pos[b];
1205 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001206 break;
1207 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001208 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +01001209 }
1210 }
1211 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001212 array = newAV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001213 for (a = 0, i=0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
1214 long long c = best[a].wordi;
Marc Kupietza77acce2017-11-30 16:59:07 +01001215/*
Marc Kupietzd91212f2017-11-13 10:05:09 +01001216 if (dedupe) {
1217 int filtered=0;
1218 for (j=0; j<i; j++)
1219 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
1220 strcasestr(chosen[j], &vocab[c * max_w])) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001221 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
1222 filtered = 1;
1223 }
Marc Kupietzd91212f2017-11-13 10:05:09 +01001224 if(filtered)
1225 continue;
1226 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001227*/
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001228 chosen[i++]=c;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001229 HV* hash = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001230 SV* word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001231 AV* heat = newAV();
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001232 if(latin_enc == 0) SvUTF8_on(word);
1233 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001234 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001235 hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001236 hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001237 hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001238 hv_store(hash, "max", strlen("max"), newSVnv(best[a].max_activation), 0); // newSVnv(target_sums[best[a].wordi]), 0);
Marc Kupietza77acce2017-11-30 16:59:07 +01001239 hv_store(hash, "overall", strlen("overall"), newSVnv(best[a].activation_sum/total_activation), 0); // newSVnv(target_sums[best[a].wordi]), 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001240 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001241 best[a].heat[5]=0;
1242 for(i=10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i]));
1243 hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV*)heat), 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001244 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietz271e2a42016-03-22 11:37:43 +01001245 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001246 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001247 }
Marc Kupietz000ad862016-02-26 14:59:12 +01001248end:
Marc Kupietz257a93d2019-01-03 17:55:22 +01001249 // words = old_words; // why was this here?
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001250 free(best);
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001251 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +01001252}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +01001253
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001254int dump_vecs(char *fname) {
1255 long i, j;
1256 FILE *f;
Marc Kupietz0a68a792018-12-20 11:41:45 +01001257 if(words>100000)
1258 words=100000;
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001259
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001260 if((f=fopen(fname, "w")) == NULL) {
1261 fprintf(stderr, "cannot open %s for writing\n", fname);
1262 return(-1);
1263 }
1264 fprintf(f, "%lld %lld\n", words, size);
1265 for (i=0; i < words; i++) {
1266 fprintf(f, "%s ", &vocab[i * max_w]);
1267 for(j=0; j < size - 1; j++)
1268 fprintf(f, "%f ", M[i*size + j]);
1269 fprintf(f, "%f\n", M[i*size + j]);
1270 }
1271 fclose(f);
1272 return(0);
1273}
Marc Kupietzdc22b982015-10-09 09:19:34 +02001274
Marc Kupietzfa194262018-06-05 09:39:32 +02001275int dump_for_numpy(char *fname) {
1276 long i, j;
1277 FILE *f;
1278 /* if(words>200000) */
1279 /* words=200000; */
1280
1281 if((f=fopen(fname, "w")) == NULL) {
1282 fprintf(stderr, "cannot open %s for writing\n", fname);
1283 return(-1);
1284 }
1285 for (i=0; i < words; i++) {
1286 for(j=0; j < size - 1; j++)
1287 fprintf(f, "%f\t", M[i*size + j]);
1288 fprintf(f, "%f\n", M[i*size + j]);
1289 }
1290 fclose(f);
Marc Kupietz7631e022019-01-25 12:04:42 +01001291 return(0);
Marc Kupietzfa194262018-06-05 09:39:32 +02001292}