blob: 2c575542eb5d039f94913a4c3935de24f97de728 [file] [log] [blame]
Marc Kupietzdc22b982015-10-09 09:19:34 +02001#!/usr/local/bin/perl
2use Inline C;
Marc Kupietz3c487bb2018-12-20 11:31:45 +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";
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) = @_;
116 if(!$cccache{$word}) {
117 $cccache{$word} = getClassicCollocators($word);
118 } else {
119 $c->app->log->info("Getting classic collocators for $word from cache.");
120 }
121 return $cccache{$word};
122}
123
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100124sub getSimilarProfilesCached {
125 my ($c, $word) = @_;
126 if(!$spcache{$word}) {
127 $spcache{$word} = getSimilarProfiles($word);
128 } else {
129 $c->app->log->info("Getting similar profiles for $word from cache:");
130 print $spcache{$word};
131 }
132 return $spcache{$word};
133}
134
Marc Kupietz66bfd952017-12-11 09:59:45 +0100135post '/derekovecs/getVecsByRanks' => sub {
136 my $self = shift;
137 my $vec = getVecs($self->req->json);
138 $self->render(json => $vec);
139};
140
Marc Kupietze13a3552018-01-25 08:48:34 +0100141any '*/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100142 my $self = shift;
Marc Kupietz19c68242018-03-12 09:42:21 +0100143 $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100144};
145
Marc Kupietze13a3552018-01-25 08:48:34 +0100146any '/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100147 my $self = shift;
Marc Kupietz19c68242018-03-12 09:42:21 +0100148 $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100149};
150
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100151any '*/getSimilarProfiles' => sub {
152 my $self = shift;
153 $self->render(data => getSimilarProfilesCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
154};
155
Marc Kupietzc987fa82018-03-21 12:14:25 +0100156any '/getSimilarProfiles' => sub {
157 my $self = shift;
158 $self->render(data => getSimilarProfilesCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
159};
160
Marc Kupietzdf3d4b52017-11-29 16:57:27 +0100161get '*/img/*' => sub {
162 my $c = shift;
163 my $url = $c->req->url;
164 $url =~ s@/derekovecs@@g;
165 $c->app->log->info("GET: " . $url);
166 $c->reply->static($url);
167};
168
Marc Kupietzdc22b982015-10-09 09:19:34 +0200169get '/' => sub {
170 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +0200171 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200172 my $word=$c->param('word');
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100173 my $no_nbs=$c->param('n') || 100;
174 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +0100175 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100176 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100177 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200178 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100179 my $sort=$c->param('sort') || 0;
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100180 my $csv=$c->param('csv') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +0200181 my $json=$c->param('json') || 0;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100182 my $cutoff=$c->param('cutoff') || 500000;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100183 my $dedupe=$c->param('dedupe') || 0;
Marc Kupietzac707b32018-12-20 11:36:38 +0100184 my $nosp=$c->param('nosp') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100185 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100186 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100187 my @collocations;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100188 if(defined($word) && $word !~ /^\s*$/) {
189 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100190 $word =~ s/\s+/ /g;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100191 if($opt_m && $word !~ /\|/) {
192 $word .= "|$word";
193 }
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100194 for my $w (split(' *\| *', $word)) {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100195 if($opt_m) {
196 if($searchBaseVocabFirst) {
197 $searchBaseVocabFirst=0;
198 } else {
199 $searchBaseVocabFirst=1;
200 }
201 }
202 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe,$searchBaseVocabFirst}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100203 $c->app->log->info("Getting $w results from cache");
Marc Kupietz3082fd02019-01-09 14:54:06 +0100204 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe.$searchBaseVocabFirst}
Marc Kupietza5b90152016-03-15 17:39:19 +0100205 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100206 $c->app->log->info('Looking for neighbours of '.$w);
207 if($opt_i) {
Marc Kupietzac707b32018-12-20 11:36:38 +0100208 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100209 } else {
Marc Kupietzac707b32018-12-20 11:36:38 +0100210 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100211 }
Marc Kupietz2dd2dd72017-12-01 22:08:14 +0100212 $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100213 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100214 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100215 }
216 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100217 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200218 if($json) {
219 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100220 } elsif($csv) {
221 my $csv_data="";
222 for (my $i=0; $i <= $no_nbs; $i++) {
223 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
224 }
225 for (my $i=0; $i < $no_nbs; $i++) {
226 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
227 }
228 chop $csv_data;
229 chop $csv_data;
230 $csv_data .= "\n";
231 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200232 } else {
Marc Kupietzc053d972019-01-10 10:41:51 +0100233 $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 +0200234 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200235};
236
Marc Kupietz30ca4342017-11-22 21:21:20 +0100237helper(bitvec2window => sub {
238 my ($self, $n) = @_;
239 my $str = unpack("B32", pack("N", $n));
240 $str =~ s/^\d{22}//;
241 $str =~ s/^(\d{5})/$1x/;
242 $str =~ s/0/·/g;
243 $str =~ s/1/+/g;
244 return $str;
245 });
246
Marc Kupietza5b90152016-03-15 17:39:19 +0100247$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200248
249exit;
250
251__END__
252
253__C__
254#include <stdio.h>
255#include <string.h>
256#include <math.h>
257#include <malloc.h>
258#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +0100259#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +0100260#include <pthread.h>
Marc Kupietze243efd2018-01-11 22:19:24 +0100261#include <collocatordb.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200262
263#define max_size 2000
264#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100265#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100266#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +0100267#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100268#define MAX_CC 50
269#define EXP_TABLE_SIZE 1000
270#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +0100271#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +0200272
273//the thread function
274void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +0100275
276typedef struct {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100277 long long wordi;
278 long position;
279 float activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100280 float average;
Marc Kupietza77acce2017-11-30 16:59:07 +0100281 float cprobability; // column wise probability
Marc Kupietz4116b432017-12-06 14:15:32 +0100282 float cprobability_sum;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100283 float probability;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100284 float activation_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100285 float max_activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100286 float heat[16];
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100287} collocator;
288
289typedef struct {
290 collocator *best;
Marc Kupietz80abb442016-03-23 21:04:08 +0100291 int length;
Marc Kupietz000ad862016-02-26 14:59:12 +0100292} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100293
Marc Kupietz000ad862016-02-26 14:59:12 +0100294typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +0100295 long long wordi[MAX_NEIGHBOURS];
296 char sep[MAX_NEIGHBOURS];
297 int length;
298} wordlist;
299
300typedef struct {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100301 long cutoff;
Marc Kupietz48c29682016-03-19 11:30:43 +0100302 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100303 char *token;
304 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100305 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100306 unsigned long upto;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100307 collocator *best;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100308 float *target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100309 float *window_sums;
Marc Kupietz000ad862016-02-26 14:59:12 +0100310} knnpars;
311
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100312typedef struct {
313 uint32_t index;
314 float value;
315} sparse_t;
316
317typedef struct {
318 uint32_t len;
319 sparse_t nbr[100];
320} profile_t;
321
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200322float *M, *M2=0L, *syn1neg_window, *expTable;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100323float *window_sums;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200324char *vocab;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200325char *garbage = NULL;
Marc Kupietze243efd2018-01-11 22:19:24 +0100326COLLOCATORDB *cdb = NULL;
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100327profile_t *sprofiles = NULL;
328size_t sprofiles_qty = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100329
Marc Kupietza2e64502016-04-27 09:53:51 +0200330long long words, size, merged_end;
Marc Kupietza5f60042017-05-04 10:38:12 +0200331long long merge_words = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100332int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100333int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100334int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200335
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100336/* load collocation profiles if file exists */
337int load_sprofiles(char *vecsname) {
338 char *basename = strdup(vecsname);
339 char *pos = strstr(basename, ".vecs");
340 if(pos)
341 *pos=0;
342
343 char binsprofiles_fname[256];
344 strcpy(binsprofiles_fname, basename);
345 strcat(binsprofiles_fname, ".sprofiles.bin");
346 FILE *fp = fopen(binsprofiles_fname, "rb");
347 if (fp == NULL) {
348 printf("Collocation profiles %s not found. No problem.\n", binsprofiles_fname);
349 return 0;
350 }
351 fseek(fp, 0L, SEEK_END);
352 size_t sz = ftell(fp);
353 fclose(fp);
354
355 int fd = open(binsprofiles_fname, O_RDONLY);
356 sprofiles = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0);
357 if (sprofiles == MAP_FAILED) {
358 close(fd);
359 fprintf(stderr, "Cannot mmap %s\n", binsprofiles_fname);
360 sprofiles = NULL;
361 return 0;
362 } else {
363 sprofiles_qty = sz / sizeof(profile_t);
364 fprintf(stderr, "Successfully mmaped %s containing similar profiles for %ld word forms.\n", binsprofiles_fname, sprofiles_qty);
365 }
366 return 1;
367}
368
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100369int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100370 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100371 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100372 long long a, b, c, d, cn;
373 float len;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200374 double val;
Marc Kupietz82b02672016-02-26 12:32:25 +0100375
Marc Kupietz67c20282016-02-26 09:42:00 +0100376 char binvecs_fname[256], binwords_fname[256];
377 strcpy(binwords_fname, file_name);
378 strcat(binwords_fname, ".words");
379 strcpy(binvecs_fname, file_name);
380 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200381
Marc Kupietza5b90152016-03-15 17:39:19 +0100382 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200383 f = fopen(file_name, "rb");
384 if (f == NULL) {
385 printf("Input file %s not found\n", file_name);
386 return -1;
387 }
388 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100389 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200390 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100391 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
392 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100393 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
394 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
395 if (M == NULL) {
396 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
397 return -1;
398 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200399 if(strstr(file_name, ".txt")) {
400 for (b = 0; b < words; b++) {
401 a = 0;
402 while (1) {
403 vocab[b * max_w + a] = fgetc(f);
404 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
405 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
406 }
407 vocab[b * max_w + a] = 0;
408 len = 0;
409 for (a = 0; a < size; a++) {
410 fscanf(f, "%lf", &val);
411 M[a + b * size] = val;
412 len += val * val;
413 }
414 len = sqrt(len);
415 for (a = 0; a < size; a++) M[a + b * size] /= len;
416 }
417 } else {
418 for (b = 0; b < words; b++) {
419 a = 0;
420 while (1) {
421 vocab[b * max_w + a] = fgetc(f);
422 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
423 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
424 }
425 vocab[b * max_w + a] = 0;
426 fread(&M[b * size], sizeof(float), size, f);
427 len = 0;
428 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
429 len = sqrt(len);
430 for (a = 0; a < size; a++) M[a + b * size] /= len;
431 }
432 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100433 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
434 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
435 fclose(binvecs);
436 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
437 fclose(binwords);
438 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100439 }
440 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
441 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
442 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
443 if (M == MAP_FAILED || vocab == MAP_FAILED) {
444 close(binvecs_fd);
445 close(binwords_fd);
446 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
447 exit(-1);
448 }
449 } else {
450 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
451 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100452 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200453 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100454
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200455 if(net_name && strlen(net_name) > 0) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100456 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
457 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
458 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100459 // munmap(M, sizeof(float) * words * size);
460 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 +0200461 if (M2 == MAP_FAILED) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100462 close(net_fd);
463 fprintf(stderr, "Cannot mmap %s\n", net_name);
464 exit(-1);
465 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100466 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100467 } else {
468 fprintf(stderr, "Cannot open %s\n", net_name);
469 exit(-1);
470 }
471 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
Marc Kupietze243efd2018-01-11 22:19:24 +0100472
473 char collocatordb_name[2048];
474 strcpy(collocatordb_name, net_name);
475 char *ext = rindex(collocatordb_name, '.');
476 if(ext) {
477 strcpy(ext, ".rocksdb");
478 if(access(collocatordb_name, R_OK) == 0) {
479 *ext = 0;
480 fprintf(stderr, "Opening collocator DB %s\n", collocatordb_name);
481 cdb = open_collocatordb(collocatordb_name);
482 }
483 }
484 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100485
486 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
487 for (i = 0; i < EXP_TABLE_SIZE; i++) {
488 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
489 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
490 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100491 window_sums = malloc(sizeof(float) * (window+1) * 2);
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100492
493 return 0;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200494}
495
Marc Kupietza2e64502016-04-27 09:53:51 +0200496long mergeVectors(char *file_name){
497 FILE *f, *binvecs, *binwords;
498 int binwords_fd, binvecs_fd, net_fd, i;
499 long long a, b, c, d, cn;
500 float len;
501 float *merge_vecs;
502 char *merge_vocab;
Marc Kupietza5f60042017-05-04 10:38:12 +0200503 /* long long merge_words, merge_size; */
504 long long merge_size;
Marc Kupietza2e64502016-04-27 09:53:51 +0200505
506 char binvecs_fname[256], binwords_fname[256];
507 strcpy(binwords_fname, file_name);
508 strcat(binwords_fname, ".words");
509 strcpy(binvecs_fname, file_name);
510 strcat(binvecs_fname, ".vecs");
511
512 f = fopen(file_name, "rb");
513 if (f == NULL) {
514 printf("Input file %s not found\n", file_name);
515 exit -1;
516 }
517 fscanf(f, "%lld", &merge_words);
518 fscanf(f, "%lld", &merge_size);
519 if(merge_size != size){
520 fprintf(stderr, "vectors must have the same length\n");
521 exit(-1);
522 }
523 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
524 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
525 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
526 if (merge_vecs == NULL || merge_vocab == NULL) {
527 close(binvecs_fd);
528 close(binwords_fd);
529 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
530 exit(-1);
531 }
532 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
533 read(binwords_fd, merge_vocab, merge_words * max_w);
534 } else {
535 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
536 exit(-1);
537 }
538 printf("Successfully reallocated memory\nMerging...\n");
539 fflush(stdout);
540 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
541 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
542 munmap(M, words * size * sizeof(float));
543 munmap(vocab, words * max_w);
544 M = merge_vecs;
545 vocab = merge_vocab;
546 merged_end = merge_words;
547 words += merge_words;
548 fclose(f);
549 printf("merged_end: %lld, words: %lld\n", merged_end, words);
Marc Kupietzebc131e2019-01-03 16:24:27 +0100550 printBiggestMergedDifferences();
Marc Kupietza2e64502016-04-27 09:53:51 +0200551 return((long) merged_end);
552}
553
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200554void filter_garbage() {
555 long i;
Marc Kupietzab591a82016-04-28 14:08:49 +0200556 unsigned char *w, previous, c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200557 garbage = malloc(words);
558 memset(garbage, 0, words);
559 for (i = 0; i < words; i++) {
560 w = vocab + i * max_w;
561 previous = 0;
Marc Kupietz969adfa2018-03-12 09:43:53 +0100562 if(strncmp("quot", w, 4) == 0) {
563 garbage[i]=1;
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100564// printf("Gargabe: %s\n", vocab + i * max_w);
Marc Kupietz969adfa2018-03-12 09:43:53 +0100565 } else {
566 while((c = *w++) && !garbage[i]) {
567 if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
568 (previous == '-' && (c & 32)) ||
569 (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) ||
570 (previous == 'q' && c == 'u' && *(w) == 'o' && *(w+1) == 't') || /* quot */
571 c == '<'
572 ) {
573 garbage[i]=1;
574 continue;
575 }
576 previous = c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200577 }
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200578 }
579 }
580 return;
581}
582
Marc Kupietzac707b32018-12-20 11:36:38 +0100583
584knn *simpleGetCollocators(int word, int number, long cutoff, int *result) {
585 knnpars *pars = calloc(sizeof(knnpars), 1);
586 float *target_sums;
587 float *window_sums = malloc(sizeof(float) * (window+1) * 2);
588 pars->cutoff = (cutoff? cutoff : 300000);
589 long a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
590 for(a = 0; a < cutoff; a++)
591 target_sums[a] = 0;
592 pars->target_sums = target_sums;
593 pars->window_sums = window_sums;
594 pars->N = (number? number : 20);
595 pars->from = 0;
596 pars->upto = window * 2 -1;
597 knn *syn_nbs; // = (knn*) getCollocators(pars);
598 free(pars);
599 free(window_sums);
600 free(target_sums);
601 return syn_nbs;
602}
603
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100604void *getCollocators(void *args) {
605 knnpars *pars = args;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100606 int N = pars->N;
607 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100608 knn *nbs = NULL;
609 long window_layer_size = size * window * 2;
610 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
611 float f, max_f, maxmax_f;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100612 float *target_sums, worstbest, wpos_sum;
613 collocator *best;
Marc Kupietzd5642582016-03-19 22:23:13 +0100614
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200615 if(M2 == NULL || cc == -1)
Marc Kupietzd5642582016-03-19 22:23:13 +0100616 return NULL;
617
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100618 a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100619 best = malloc(N * sizeof(collocator));
Marc Kupietz271e2a42016-03-22 11:37:43 +0100620 worstbest = MIN_RESP;
621
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100622 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100623 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100624 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100625 best[b].wordi = -1;
626 best[b].probability = 1;
627 best[b].activation = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100628 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100629
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100630 d = cc;
631 maxmax_f = -1;
632 maxmax_target = 0;
633
Marc Kupietz271e2a42016-03-22 11:37:43 +0100634 for (a = pars->from; a < pars->upto; a++) {
635 if(a >= window)
636 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100637 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100638 printf("window pos: %ld\n", a);
639 if (a != window) {
640 max_f = -1;
641 window_offset = a * size;
642 if (a > window)
643 window_offset -= size;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100644 for(target = 0; target < pars->cutoff; target ++) {
645 if(garbage && garbage[target]) continue;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100646 if(target == d)
647 continue;
648 f = 0;
649 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100650 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100651 if (f < -MAX_EXP)
652 continue;
653 else if (f > MAX_EXP)
654 continue;
655 else
656 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100657 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100658
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100659 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100660 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100661 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100662 if (f > best[b].activation) {
663 memmove(best + b + 1, best + b, (N - b -1) * sizeof(collocator));
664 best[b].activation = f;
665 best[b].wordi = target;
666 best[b].position = window-a;
Marc Kupietz33679a32016-03-22 08:49:39 +0100667 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100668 }
669 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100670 if(b == N - 1)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100671 worstbest = best[N-1].activation;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100672 }
673 }
674 printf("%d %.2f\n", max_target, max_f);
675 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
676 if(max_f > maxmax_f) {
677 maxmax_f = max_f;
678 maxmax_target = max_target;
679 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100680 for (b = 0; b < N; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100681 if(best[b].position == window-a)
Marc Kupietza77acce2017-11-30 16:59:07 +0100682 best[b].cprobability = best[b].activation / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100683 } else {
684 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
685 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100686 pars->window_sums[a] = wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100687 }
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100688 for (b = 0; b < pars->cutoff; b++)
Marc Kupietza77acce2017-11-30 16:59:07 +0100689 pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2);
690 printf("Target-Summe von 0: %f\n", pars->target_sums[150298]);
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100691 free(target_sums);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100692 for(b=0; b<N && best[b].wordi >= 0; b++);; // THIS LOOP IS NEEDED (b...)
693// 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 +0100694// printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100695 nbs = malloc(sizeof(knn));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100696 nbs->best = best;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100697 nbs->length = b-1;
698 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100699}
700
Marc Kupietz30ca4342017-11-22 21:21:20 +0100701
Marc Kupietz66bfd952017-12-11 09:59:45 +0100702AV *getVecs(AV *array) {
703 int i, b;
704 AV *result = newAV();
705 for (i=0; i<=av_len(array); i++) {
706 SV** elem = av_fetch(array, i, 0);
707 if (elem != NULL) {
708 long j = (long) SvNV(*elem);
709 AV *vector = newAV();
710 for (b = 0; b < size; b++) {
711 av_push(vector, newSVnv(M[b + j * size]));
712 }
713 av_push(result, newRV_noinc(vector));
714 }
715 }
716 return result;
717}
718
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100719char *getSimilarProfiles(long node) {
720 int i;
721 char buffer[120000];
722 char pair_buffer[2048];
723 buffer[0]='[';
724 buffer[1]=0;
725 if(node >= sprofiles_qty) {
726 printf("Not available in precomputed profile\n");
727 return(strdup("[{\"w\":\"not available\", \"v\":0}]\n"));
728 }
729
730 printf("******* %s ******\n", &vocab[max_w * node]);
731
732 for(i=0; i < 100 && i < sprofiles[node].len; i++) {
733 sprintf(pair_buffer, "{\"w\":\"%s\", \"v\":%f},", &vocab[max_w * (sprofiles[node].nbr[i].index)], sprofiles[node].nbr[i].value);
734 strcat(buffer, pair_buffer);
735 }
736 buffer[strlen(buffer)-1]=']';
737 strcat(buffer, "\n");
738 printf(buffer);
739 return(strdup(buffer));
740}
741
Marc Kupietze243efd2018-01-11 22:19:24 +0100742char *getClassicCollocators(long node) {
743 char *res = (cdb? strdup(get_collocators_as_json(cdb, node)) : "[]");
744 return res;
745}
746
Marc Kupietz0957d532019-01-03 17:54:00 +0100747wordlist *getTargetWords(char *st1, int search_backw) {
Marc Kupietz48c29682016-03-19 11:30:43 +0100748 wordlist *wl = malloc(sizeof(wordlist));
749 char st[100][max_size], sep[100];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100750 long a, b=0, c=0, cn=0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200751 int unmerged;
752
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100753 while (1) {
754 st[cn][b] = st1[c];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100755 b++;
756 c++;
757 st[cn][b] = 0;
758 if (st1[c] == 0) break;
759 if (st1[c] == ' ' || st1[c] == '-') {
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100760 sep[cn++] = st1[c];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100761 b = 0;
762 c++;
763 }
764 }
765 cn++;
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100766 for (a = 0; a < cn; a++) {
767 if (search_backw) {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100768 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 +0100769 } else {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100770 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 +0100771 }
772 if (b == words) b = -1;
773 wl->wordi[a] = b;
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100774 if (b == -1) {
775 fprintf(stderr, "Out of dictionary word!\n");
776 cn--;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100777 } else {
778 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 +0100779 }
780 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100781 wl->length=cn;
782 return(wl);
783}
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100784
Marc Kupietzebc131e2019-01-03 16:24:27 +0100785void printBiggestMergedDifferences() {
786 float dist, len, vec[max_size];
787 long long a, b, c, d, cn, *bi;
788 char ch;
789 knn *nbs = NULL;
790 int N = 100;
791
792 printf("Looking for biggest distances between main and merged vectors ...\n");
793 collocator *best;
794 best = malloc(N * sizeof(collocator));
795 memset(best, 0, N * sizeof(collocator));
796
797 float worstbest=1000000;
798
799 for (a = 0; a < N; a++) best[a].activation = worstbest;
800
801 for (c = 0; c < 10000; c++) {
802 if(garbage && garbage[c]) continue;
803 a = 0;
804 dist = 0;
805 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + (c+merged_end) * size];
806 if(dist < worstbest) {
807 for (a = 0; a < N; a++) {
808 if (dist < best[a].activation) {
809 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
810 best[a].activation = dist;
811 best[a].wordi = c;
812 break;
813 }
814 }
815 worstbest = best[N-1].activation;
816 }
817 }
818
819 printf("Most distant vectors for:\n ");
820 for (a = 0; a < N; a++) {
821 printf("%s ", &vocab[best[a].wordi * max_w]);
822 }
823 printf("\n");
824}
825
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100826void *_get_neighbours(void *arg) {
827 knnpars *pars = arg;
Marc Kupietz48c29682016-03-19 11:30:43 +0100828 char *st1 = pars->token;
829 int N = pars->N;
830 long from = pars -> from;
831 unsigned long upto = pars -> upto;
832 char file_name[max_size], st[100][max_size], *sep;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100833 float dist, len, vec[max_size];
834 long long a, b, c, d, cn, *bi;
Marc Kupietz48c29682016-03-19 11:30:43 +0100835 char ch;
836 knn *nbs = NULL;
837 wordlist *wl = pars->wl;
838
Marc Kupietzc55c8872017-12-01 23:27:10 +0100839 collocator *best = pars->best;
Marc Kupietz48c29682016-03-19 11:30:43 +0100840
841 float worstbest=-1;
842
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100843 for (a = 0; a < N; a++) best[a].activation = 0;
Marc Kupietz48c29682016-03-19 11:30:43 +0100844 a = 0;
845 bi = wl->wordi;
846 cn = wl->length;
847 sep = wl->sep;
848 b = bi[0];
849 c = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100850 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100851 N = 0;
852 goto end;
853 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200854 for (a = 0; a < size; a++) vec[a] = 0;
855 for (b = 0; b < cn; b++) {
856 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100857 if(b>0 && sep[b-1] == '-')
858 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
859 else
860 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200861 }
862 len = 0;
863 for (a = 0; a < size; a++) len += vec[a] * vec[a];
864 len = sqrt(len);
865 for (a = 0; a < size; a++) vec[a] /= len;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100866 for (a = 0; a < N; a++) best[a].activation = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100867 for (c = from; c < upto; c++) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200868 if(garbage && garbage[c]) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200869 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100870// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100871// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
872// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200873 dist = 0;
874 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100875 if(dist > worstbest) {
876 for (a = 0; a < N; a++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100877 if (dist > best[a].activation) {
878 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
879 best[a].activation = dist;
880 best[a].wordi = c;
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100881 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200882 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200883 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100884 worstbest = best[N-1].activation;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200885 }
886 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100887
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100888end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100889 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200890}
891
Marc Kupietze0e42132017-11-24 16:44:34 +0100892int cmp_activation (const void * a, const void * b) {
893 float fb = ((collocator *)a)->activation;
894 float fa = ((collocator *)b)->activation;
895 return (fa > fb) - (fa < fb);
896}
897
898int cmp_probability (const void * a, const void * b) {
899 float fb = ((collocator *)a)->probability;
900 float fa = ((collocator *)b)->probability;
901 return (fa > fb) - (fa < fb);
902}
903
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100904
Marc Kupietzac707b32018-12-20 11:36:38 +0100905SV *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 +0100906 HV *result = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100907 float *target_sums, vec[max_size];
Marc Kupietza5f60042017-05-04 10:38:12 +0200908 long long old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100909 long a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100910 knn *para_nbs[MAX_THREADS];
911 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100912 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100913 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100914 wordlist *wl;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200915 int syn_threads = (M2? window * 2 : 0);
Marc Kupietzac707b32018-12-20 11:36:38 +0100916 int para_threads = (no_similar_profiles? 0 : num_threads - syn_threads);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100917
Marc Kupietzc55c8872017-12-01 23:27:10 +0100918 collocator *best;
919 posix_memalign((void **) &best, 128, 10 * N * sizeof(collocator));
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100920 memset(best, 0, 10 * N * sizeof(collocator));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100921
Marc Kupietz000ad862016-02-26 14:59:12 +0100922 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
923
Marc Kupietzd5d05732018-01-30 11:57:35 +0100924 if(cutoff < 1 || cutoff > words)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100925 cutoff=words;
926
Marc Kupietza2e64502016-04-27 09:53:51 +0200927 wl = getTargetWords(st1, search_backw);
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100928 if(wl == NULL || wl->length < 1)
Marc Kupietz271e2a42016-03-22 11:37:43 +0100929 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100930
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100931 old_words = cutoff;
Marc Kupietz3082fd02019-01-09 14:54:06 +0100932 slice = cutoff / para_threads;
Marc Kupietza5f60042017-05-04 10:38:12 +0200933
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100934 a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float));
935 for(a = 0; a < cutoff; a++)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100936 target_sums[a] = 0;
937
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200938 printf("Starting %d threads\n", para_threads);
939 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100940 for(a=0; a < para_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100941 pars[a].cutoff = cutoff;
Marc Kupietz000ad862016-02-26 14:59:12 +0100942 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100943 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100944 pars[a].N = N;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100945 pars[a].best = &best[N*a];
Marc Kupietz3082fd02019-01-09 14:54:06 +0100946 if(merge_words == 0 || search_backw == 0) {
Marc Kupietz1f0f7802019-01-03 16:23:29 +0100947 pars[a].from = a*slice;
948 pars[a].upto = ((a+1)*slice > cutoff? cutoff : (a+1) * slice);
949 } else {
Marc Kupietz3082fd02019-01-09 14:54:06 +0100950 pars[a].from = merge_words + a * slice;
951 pars[a].upto = merge_words + ((a+1)*slice > cutoff? cutoff : (a+1) * slice);
Marc Kupietz1f0f7802019-01-03 16:23:29 +0100952 }
953 printf("From: %ld, Upto: %ld\n", pars[a].from, pars[a].upto);
Marc Kupietz000ad862016-02-26 14:59:12 +0100954 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
955 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200956 if(M2) {
957 for(a=0; a < syn_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100958 pars[a + para_threads].cutoff = cutoff;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200959 pars[a + para_threads].target_sums = target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100960 pars[a + para_threads].window_sums = window_sums;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200961 pars[a + para_threads].wl = wl;
962 pars[a + para_threads].N = N;
963 pars[a + para_threads].from = a;
964 pars[a + para_threads].upto = a+1;
965 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
966 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100967 }
968 printf("Waiting for para threads to join\n");
969 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100970 for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *) &para_nbs[a]);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100971 printf("Para threads joint\n");
972 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +0100973
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200974 /* if(!syn_nbs[0]) */
975 /* goto end; */
Marc Kupietz000ad862016-02-26 14:59:12 +0100976
Marc Kupietzc55c8872017-12-01 23:27:10 +0100977 qsort(best, N*para_threads, sizeof(collocator), cmp_activation);
Marc Kupietz000ad862016-02-26 14:59:12 +0100978
Marc Kupietz000ad862016-02-26 14:59:12 +0100979
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100980 long long chosen[MAX_NEIGHBOURS];
981 printf("N: %ld\n", N);
982
Marc Kupietz271e2a42016-03-22 11:37:43 +0100983 AV* array = newAV();
Marc Kupietzd91212f2017-11-13 10:05:09 +0100984 int i, j;
Marc Kupietza5f60042017-05-04 10:38:12 +0200985 int l1_words=0, l2_words=0;
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100986
987 for (a = 0, i = 0; i < N && a < N*para_threads; a++) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100988 int filtered=0;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100989 long long c = best[a].wordi;
Marc Kupietze8b9dd22019-01-03 17:55:59 +0100990 if ((merge_words && dedupe && i > 1) || (!merge_words && dedupe && i > 0)) {
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100991 for (j=0; j<i && !filtered; j++)
992 if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) ||
993 strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) {
994 printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]);
Marc Kupietzd91212f2017-11-13 10:05:09 +0100995 filtered = 1;
996 }
997 if(filtered)
998 continue;
999 }
Marc Kupietz3082fd02019-01-09 14:54:06 +01001000
1001
1002 if(0 && merge_words > 0) {
Marc Kupietza5f60042017-05-04 10:38:12 +02001003 if(c >= merge_words) {
1004 if(l1_words > N / 2)
1005 continue;
1006 else
1007 l1_words++;
1008 } else {
1009 if(l2_words > N / 2)
1010 continue;
1011 else
1012 l2_words++;
1013 }
1014 }
Marc Kupietz3082fd02019-01-09 14:54:06 +01001015
1016// printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
1017// fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001018 HV* hash = newHV();
Marc Kupietza5f60042017-05-04 10:38:12 +02001019 SV* word = newSVpvf(&vocab[c * max_w], 0);
Marc Kupietzd0d3e912017-12-01 22:09:43 +01001020 chosen[i] = c;
Marc Kupietz271e2a42016-03-22 11:37:43 +01001021 if(latin_enc == 0) SvUTF8_on(word);
Marc Kupietza5f60042017-05-04 10:38:12 +02001022 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001023 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001024 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
1025 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001026 AV *vector = newAV();
1027 for (b = 0; b < size; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001028 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001029 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001030 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
1031 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietza5f60042017-05-04 10:38:12 +02001032 i++;
Marc Kupietz271e2a42016-03-22 11:37:43 +01001033 }
1034 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
1035
Marc Kupietz50485ba2016-03-23 09:13:14 +01001036 for(b=0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001037 best[b].wordi = -1L;
1038 best[b].activation = 0;
1039 best[b].probability = 0;
1040 best[b].position = 0;
1041 best[b].activation_sum = 0;
Marc Kupietz50485ba2016-03-23 09:13:14 +01001042 }
1043
Marc Kupietza77acce2017-11-30 16:59:07 +01001044 float total_activation = 0;
1045
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001046 if (M2) {
1047 printf("Waiting for syn threads to join\n");
1048 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001049 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], (void *) &syn_nbs[a]);
Marc Kupietza77acce2017-11-30 16:59:07 +01001050 for (a = 0; a <= syn_threads; a++) {
Marc Kupietz63872182018-03-12 09:44:49 +01001051 if(a == window) continue;
Marc Kupietza77acce2017-11-30 16:59:07 +01001052 total_activation += window_sums[a];
1053 printf("window pos: %d, sum: %f\n", a, window_sums[a]);
1054 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001055 printf("syn threads joint\n");
1056 fflush(stdout);
Marc Kupietz50485ba2016-03-23 09:13:14 +01001057
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001058 for(b=0; b < syn_nbs[0]->length; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001059 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
1060 best[b].position = -1; // syn_nbs[0]->pos[b];
1061 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001062 best[b].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +01001063 best[b].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001064 best[b].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +01001065 best[b].cprobability = syn_nbs[0]->best[b].cprobability;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001066 }
Marc Kupietz30ca4342017-11-22 21:21:20 +01001067
1068 float best_window_sum[MAX_NEIGHBOURS];
1069 int found_index=0, i=0, j, w;
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001070 for(a=0; a < syn_threads; a++) {
1071 for(b=0; b < syn_nbs[a]->length; b++) {
1072 for(i=0; i < found_index; i++)
1073 if(best[i].wordi == syn_nbs[a]->best[b].wordi)
1074 break;
1075 if(i >= found_index) {
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001076 best[found_index].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +01001077 best[found_index].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001078 best[found_index].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +01001079 best[found_index].cprobability = syn_nbs[a]->best[b].cprobability;
1080 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 +01001081 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
1082 // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
Marc Kupietz30ca4342017-11-22 21:21:20 +01001083 }
1084 }
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001085 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001086 sort_by =0; // ALWAYS AUTO-FOCUS
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001087 if(sort_by != 1 && sort_by != 2) { // sort by auto focus mean
Marc Kupietz30ca4342017-11-22 21:21:20 +01001088 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) -1);
Marc Kupietzbd41da32017-11-24 10:26:43 +01001089 int wpos;
Marc Kupietz4116b432017-12-06 14:15:32 +01001090 int bits_set = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001091 for(i=0; i < found_index; i++) {
Marc Kupietz4116b432017-12-06 14:15:32 +01001092 best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001093 for(w=1; w < (1 << syn_threads); w++) { // loop through all possible windows
Marc Kupietz4116b432017-12-06 14:15:32 +01001094 float word_window_sum = 0, word_window_average=0, word_cprobability_sum=0, word_activation_sum = 0, total_window_sum = 0;
1095 bits_set = 0;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001096 for(a=0; a < syn_threads; a++) {
Marc Kupietz30ca4342017-11-22 21:21:20 +01001097 if((1 << a) & w) {
Marc Kupietzbd41da32017-11-24 10:26:43 +01001098 wpos = (a >= window? a+1 : a);
1099 total_window_sum += window_sums[wpos];
Marc Kupietz30ca4342017-11-22 21:21:20 +01001100 }
1101 }
Marc Kupietzbd41da32017-11-24 10:26:43 +01001102// printf("%d window-sum %f\n", w, total_window_sum);
1103 for(a=0; a < syn_threads; a++) {
1104 if((1 << a) & w) {
1105 wpos = (a >= window? a+1 : a);
1106 bits_set++;
1107 for(b=0; b < syn_nbs[a]->length; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001108 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001109// float acti = syn_nbs[a]->best[b].activation / total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001110// 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 +01001111// word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1112// 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 +01001113
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001114 word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
Marc Kupietza77acce2017-11-30 16:59:07 +01001115// word_window_sum += acti - (word_window_sum * acti); syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1116
Marc Kupietz4116b432017-12-06 14:15:32 +01001117 word_window_average += syn_nbs[a]->best[b].activation; // - word_window_average * syn_nbs[a]->best[b].activation; // conormalied activation sum
1118 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 +01001119 word_activation_sum += syn_nbs[a]->best[b].activation;
1120 if(syn_nbs[a]->best[b].activation > best[i].max_activation)
1121 best[i].max_activation = syn_nbs[a]->best[b].activation;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +01001122 if(syn_nbs[a]->best[b].activation > best[i].heat[wpos] )
Marc Kupietz728b8ed2017-12-02 11:13:49 +01001123 best[i].heat[wpos] = syn_nbs[a]->best[b].activation;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001124 }
1125 }
1126 }
Marc Kupietz4116b432017-12-06 14:15:32 +01001127 if(bits_set) {
1128 word_window_average /= bits_set;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001129// word_activation_sum /= bits_set;
1130// word_window_sum /= bits_set;
Marc Kupietz4116b432017-12-06 14:15:32 +01001131 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001132
1133 word_window_sum /= total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001134
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001135 if(word_window_sum > best[i].probability) {
Marc Kupietz4116b432017-12-06 14:15:32 +01001136// best[i].position = w;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001137 best[i].probability = word_window_sum;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001138 }
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001139
Marc Kupietz4116b432017-12-06 14:15:32 +01001140 if(word_cprobability_sum > best[i].cprobability_sum) {
1141 best[i].position = w;
1142 best[i].cprobability_sum = word_cprobability_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001143 }
Marc Kupietz4116b432017-12-06 14:15:32 +01001144
1145 best[i].average = word_window_average;
1146// best[i].activation = word_activation_sum;
1147 }
Marc Kupietz30ca4342017-11-22 21:21:20 +01001148 }
Marc Kupietze0e42132017-11-24 16:44:34 +01001149 qsort(best, found_index, sizeof(collocator), cmp_probability);
Marc Kupietz30ca4342017-11-22 21:21:20 +01001150// for(i=0; i < found_index; i++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001151// 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 +01001152// }
1153
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001154 } else if(sort_by == 1) { // responsiveness any window position
1155 int wpos;
1156 for(i=0; i < found_index; i++) {
1157 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
1158 for(a=0; a < syn_threads; a++) {
1159 wpos = (a >= window? a+1 : a);
1160 for(b=0; b < syn_nbs[a]->length; b++)
1161 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
1162 best[i].probability += syn_nbs[a]->best[b].probability;
1163 if(syn_nbs[a]->best[b].activation > 0.25)
1164 best[i].position |= 1 << wpos;
1165 if(syn_nbs[a]->best[b].activation > best[i].activation) {
1166 best[i].activation = syn_nbs[a]->best[b].activation;
1167 }
1168 }
1169 }
1170 }
1171 qsort(best, found_index, sizeof(collocator), cmp_activation);
1172 } else if(sort_by == 2) { // single window position
Marc Kupietz30ca4342017-11-22 21:21:20 +01001173 for(a=1; a < syn_threads; a++) {
1174 for(b=0; b < syn_nbs[a]->length; b++) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001175 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001176 if(syn_nbs[a]->best[b].activation > best[c].activation) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001177 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001178 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001179 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001180 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
1181 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 +02001182 break;
Marc Kupietz6d9a6782016-03-23 17:25:25 +01001183 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001184 }
1185 }
1186 }
1187 } else { // sort by mean p
1188 for(a=1; a < syn_threads; a++) {
1189 for(b=0; b < syn_nbs[a]->length; b++) {
1190 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001191 if(target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001192 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001193 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001194 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001195 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
1196 best[c].position = (1 << 2*window) - 1; // syn_nbs[a]->pos[b];
1197 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001198 break;
1199 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001200 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +01001201 }
1202 }
1203 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001204 array = newAV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001205 for (a = 0, i=0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
1206 long long c = best[a].wordi;
Marc Kupietza77acce2017-11-30 16:59:07 +01001207/*
Marc Kupietzd91212f2017-11-13 10:05:09 +01001208 if (dedupe) {
1209 int filtered=0;
1210 for (j=0; j<i; j++)
1211 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
1212 strcasestr(chosen[j], &vocab[c * max_w])) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001213 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
1214 filtered = 1;
1215 }
Marc Kupietzd91212f2017-11-13 10:05:09 +01001216 if(filtered)
1217 continue;
1218 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001219*/
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001220 chosen[i++]=c;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001221 HV* hash = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001222 SV* word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001223 AV* heat = newAV();
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001224 if(latin_enc == 0) SvUTF8_on(word);
1225 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001226 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001227 hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001228 hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001229 hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001230 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 +01001231 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 +01001232 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001233 best[a].heat[5]=0;
1234 for(i=10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i]));
1235 hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV*)heat), 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001236 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietz271e2a42016-03-22 11:37:43 +01001237 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001238 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001239 }
Marc Kupietz000ad862016-02-26 14:59:12 +01001240end:
Marc Kupietz257a93d2019-01-03 17:55:22 +01001241 // words = old_words; // why was this here?
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001242 free(best);
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001243 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +01001244}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +01001245
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001246int dump_vecs(char *fname) {
1247 long i, j;
1248 FILE *f;
Marc Kupietz0a68a792018-12-20 11:41:45 +01001249 if(words>100000)
1250 words=100000;
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001251
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001252 if((f=fopen(fname, "w")) == NULL) {
1253 fprintf(stderr, "cannot open %s for writing\n", fname);
1254 return(-1);
1255 }
1256 fprintf(f, "%lld %lld\n", words, size);
1257 for (i=0; i < words; i++) {
1258 fprintf(f, "%s ", &vocab[i * max_w]);
1259 for(j=0; j < size - 1; j++)
1260 fprintf(f, "%f ", M[i*size + j]);
1261 fprintf(f, "%f\n", M[i*size + j]);
1262 }
1263 fclose(f);
1264 return(0);
1265}
Marc Kupietzdc22b982015-10-09 09:19:34 +02001266
Marc Kupietzfa194262018-06-05 09:39:32 +02001267int dump_for_numpy(char *fname) {
1268 long i, j;
1269 FILE *f;
1270 /* if(words>200000) */
1271 /* words=200000; */
1272
1273 if((f=fopen(fname, "w")) == NULL) {
1274 fprintf(stderr, "cannot open %s for writing\n", fname);
1275 return(-1);
1276 }
1277 for (i=0; i < words; i++) {
1278 for(j=0; j < size - 1; j++)
1279 fprintf(f, "%f\t", M[i*size + j]);
1280 fprintf(f, "%f\n", M[i*size + j]);
1281 }
1282 fclose(f);
1283 return(0);
1284}