blob: 01b49a7578e649dc6af9891cf6e8abde027a19da [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 Kupietz793413b2016-04-02 21:48:57 +020031my $training_args="";
Marc Kupietza2e64502016-04-27 09:53:51 +020032my $mergedEnd=0;
Marc Kupietz15987412017-11-07 15:56:58 +010033my %cache;
Marc Kupietz19c68242018-03-12 09:42:21 +010034my %cccache; # classic collocator cache
Marc Kupietza51dcfa2018-03-19 16:22:05 +010035my %spcache; # similar profile cache
Marc Kupietz793413b2016-04-02 21:48:57 +020036
Marc Kupietzfa194262018-06-05 09:39:32 +020037getopts('d:D:Gil:p:m:n:M:');
Marc Kupietz6ed81872016-04-27 14:04:04 +020038
39if($opt_M) {
Marc Kupietzed930212016-04-27 15:42:38 +020040 open my $handle, '<:encoding(UTF-8)', $opt_M
41 or die "Can't open '$opt_M' for reading: $!";
42 while(<$handle>) {
Marc Kupietz6ed81872016-04-27 14:04:04 +020043 foreach my $mw (split /\s+/) {
44 $marked{$mw}=1
45 }
46 }
Marc Kupietzed930212016-04-27 15:42:38 +020047 close($handle);
Marc Kupietz6ed81872016-04-27 14:04:04 +020048}
Marc Kupietza5b90152016-03-15 17:39:19 +010049
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010050# -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 +010051if(!$ARGV[0]) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010052 init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0));
Marc Kupietz2cb667e2016-03-10 09:44:12 +010053} else {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010054 init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0));
Marc Kupietz793413b2016-04-02 21:48:57 +020055 if(open(FILE, "$ARGV[0].args")) {
56 $training_args = <FILE>;
57 }
58 close(FILE);
Marc Kupietz2cb667e2016-03-10 09:44:12 +010059}
Marc Kupietzdc22b982015-10-09 09:19:34 +020060
Marc Kupietza51dcfa2018-03-19 16:22:05 +010061my $have_sprofiles = load_sprofiles($ARGV[0]);
62
Marc Kupietza2e64502016-04-27 09:53:51 +020063if($opt_m) {
64 $mergedEnd = mergeVectors($opt_m);
65}
66
Marc Kupietze5568a02018-12-20 11:42:02 +010067
Marc Kupietz43ee87e2016-04-25 10:50:08 +020068if($opt_d) { # -d: dump vecs and exit
69 dump_vecs($opt_d);
70 exit;
71}
72
Marc Kupietzfa194262018-06-05 09:39:32 +020073if($opt_D) { # -D: dump vecs for numpy and exit
74 dump_for_numpy($opt_D);
75 exit;
76}
77
Marc Kupietza5b90152016-03-15 17:39:19 +010078my $daemon = Mojo::Server::Daemon->new(
79 app => app,
80 listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"]
81);
82
Marc Kupietz5c3887d2016-04-28 08:53:35 +020083if($opt_G) {
84 print "Filtering garbage\n";
85 filter_garbage();
86}
87
Marc Kupietz554aff52017-11-09 14:42:09 +010088get '*/js/*' => sub {
Marc Kupietzffef9302017-11-07 15:58:01 +010089 my $c = shift;
90 my $url = $c->req->url;
91 $url =~ s@/derekovecs@@g;
92 $c->app->log->info("GET: " . $url);
93 $c->reply->static($url);
94};
95
Marc Kupietza9270572018-03-17 15:17:07 +010096get '*/css/*' => sub {
97 my $c = shift;
98 my $url = $c->req->url;
99 $url =~ s@/derekovecs/@/@g;
100 $c->app->log->info("GET: " . $url);
101 $c->reply->static($url);
102};
103
Marc Kupietz19c68242018-03-12 09:42:21 +0100104sub getClassicCollocatorsCached {
105 my ($c, $word) = @_;
106 if(!$cccache{$word}) {
107 $cccache{$word} = getClassicCollocators($word);
108 } else {
109 $c->app->log->info("Getting classic collocators for $word from cache.");
110 }
111 return $cccache{$word};
112}
113
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100114sub getSimilarProfilesCached {
115 my ($c, $word) = @_;
116 if(!$spcache{$word}) {
117 $spcache{$word} = getSimilarProfiles($word);
118 } else {
119 $c->app->log->info("Getting similar profiles for $word from cache:");
120 print $spcache{$word};
121 }
122 return $spcache{$word};
123}
124
Marc Kupietz66bfd952017-12-11 09:59:45 +0100125post '/derekovecs/getVecsByRanks' => sub {
126 my $self = shift;
127 my $vec = getVecs($self->req->json);
128 $self->render(json => $vec);
129};
130
Marc Kupietze13a3552018-01-25 08:48:34 +0100131any '*/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100132 my $self = shift;
Marc Kupietz19c68242018-03-12 09:42:21 +0100133 $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100134};
135
Marc Kupietze13a3552018-01-25 08:48:34 +0100136any '/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100137 my $self = shift;
Marc Kupietz19c68242018-03-12 09:42:21 +0100138 $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100139};
140
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100141any '*/getSimilarProfiles' => sub {
142 my $self = shift;
143 $self->render(data => getSimilarProfilesCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
144};
145
Marc Kupietzc987fa82018-03-21 12:14:25 +0100146any '/getSimilarProfiles' => sub {
147 my $self = shift;
148 $self->render(data => getSimilarProfilesCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
149};
150
Marc Kupietzdf3d4b52017-11-29 16:57:27 +0100151get '*/img/*' => sub {
152 my $c = shift;
153 my $url = $c->req->url;
154 $url =~ s@/derekovecs@@g;
155 $c->app->log->info("GET: " . $url);
156 $c->reply->static($url);
157};
158
Marc Kupietzdc22b982015-10-09 09:19:34 +0200159get '/' => sub {
160 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +0200161 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200162 my $word=$c->param('word');
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100163 my $no_nbs=$c->param('n') || 100;
164 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +0100165 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100166 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100167 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200168 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100169 my $sort=$c->param('sort') || 0;
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100170 my $csv=$c->param('csv') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +0200171 my $json=$c->param('json') || 0;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100172 my $cutoff=$c->param('cutoff') || 500000;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100173 my $dedupe=$c->param('dedupe') || 0;
Marc Kupietzac707b32018-12-20 11:36:38 +0100174 my $nosp=$c->param('nosp') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100175 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100176 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100177 my @collocations;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100178 if(defined($word) && $word !~ /^\s*$/) {
179 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100180 $word =~ s/\s+/ /g;
181 for my $w (split(' *\| *', $word)) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100182 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100183 $c->app->log->info("Getting $w results from cache");
Marc Kupietzd91212f2017-11-13 10:05:09 +0100184 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe}
Marc Kupietza5b90152016-03-15 17:39:19 +0100185 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100186 $c->app->log->info('Looking for neighbours of '.$w);
187 if($opt_i) {
Marc Kupietzac707b32018-12-20 11:36:38 +0100188 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100189 } else {
Marc Kupietzac707b32018-12-20 11:36:38 +0100190 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe, $nosp);
Marc Kupietz15987412017-11-07 15:56:58 +0100191 }
Marc Kupietz2dd2dd72017-12-01 22:08:14 +0100192 $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100193 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100194 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100195 }
196 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100197 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200198 if($json) {
199 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100200 } elsif($csv) {
201 my $csv_data="";
202 for (my $i=0; $i <= $no_nbs; $i++) {
203 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
204 }
205 for (my $i=0; $i < $no_nbs; $i++) {
206 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
207 }
208 chop $csv_data;
209 chop $csv_data;
210 $csv_data .= "\n";
211 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200212 } else {
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100213 $c->render(template=>"index", word=>$word, cutoff=>$cutoff, no_nbs=>$no_nbs, no_iterations => $no_iterations, epsilon=> $epsilon, perplexity=> $perplexity, show_som=>$som, searchBaseVocabFirst=>$searchBaseVocabFirst, sort=>$sort, training_args=>$training_args, mergedEnd=> $mergedEnd, haveSProfiles=> $have_sprofiles, dedupe=> $dedupe, marked=>\%marked, lists=> \@lists, collocators=> $res->{syntagmatic});
Marc Kupietzb613b052016-04-28 14:11:59 +0200214 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200215};
216
Marc Kupietz30ca4342017-11-22 21:21:20 +0100217helper(bitvec2window => sub {
218 my ($self, $n) = @_;
219 my $str = unpack("B32", pack("N", $n));
220 $str =~ s/^\d{22}//;
221 $str =~ s/^(\d{5})/$1x/;
222 $str =~ s/0/·/g;
223 $str =~ s/1/+/g;
224 return $str;
225 });
226
Marc Kupietza5b90152016-03-15 17:39:19 +0100227$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200228
229exit;
230
231__END__
232
233__C__
234#include <stdio.h>
235#include <string.h>
236#include <math.h>
237#include <malloc.h>
238#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +0100239#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +0100240#include <pthread.h>
Marc Kupietze243efd2018-01-11 22:19:24 +0100241#include <collocatordb.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200242
243#define max_size 2000
244#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100245#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100246#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +0100247#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100248#define MAX_CC 50
249#define EXP_TABLE_SIZE 1000
250#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +0100251#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +0200252
253//the thread function
254void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +0100255
256typedef struct {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100257 long long wordi;
258 long position;
259 float activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100260 float average;
Marc Kupietza77acce2017-11-30 16:59:07 +0100261 float cprobability; // column wise probability
Marc Kupietz4116b432017-12-06 14:15:32 +0100262 float cprobability_sum;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100263 float probability;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100264 float activation_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100265 float max_activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100266 float heat[16];
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100267} collocator;
268
269typedef struct {
270 collocator *best;
Marc Kupietz80abb442016-03-23 21:04:08 +0100271 int length;
Marc Kupietz000ad862016-02-26 14:59:12 +0100272} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100273
Marc Kupietz000ad862016-02-26 14:59:12 +0100274typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +0100275 long long wordi[MAX_NEIGHBOURS];
276 char sep[MAX_NEIGHBOURS];
277 int length;
278} wordlist;
279
280typedef struct {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100281 long cutoff;
Marc Kupietz48c29682016-03-19 11:30:43 +0100282 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100283 char *token;
284 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100285 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100286 unsigned long upto;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100287 collocator *best;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100288 float *target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100289 float *window_sums;
Marc Kupietz000ad862016-02-26 14:59:12 +0100290} knnpars;
291
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100292typedef struct {
293 uint32_t index;
294 float value;
295} sparse_t;
296
297typedef struct {
298 uint32_t len;
299 sparse_t nbr[100];
300} profile_t;
301
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200302float *M, *M2=0L, *syn1neg_window, *expTable;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100303float *window_sums;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200304char *vocab;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200305char *garbage = NULL;
Marc Kupietze243efd2018-01-11 22:19:24 +0100306COLLOCATORDB *cdb = NULL;
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100307profile_t *sprofiles = NULL;
308size_t sprofiles_qty = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100309
Marc Kupietza2e64502016-04-27 09:53:51 +0200310long long words, size, merged_end;
Marc Kupietza5f60042017-05-04 10:38:12 +0200311long long merge_words = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100312int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100313int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100314int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200315
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100316/* load collocation profiles if file exists */
317int load_sprofiles(char *vecsname) {
318 char *basename = strdup(vecsname);
319 char *pos = strstr(basename, ".vecs");
320 if(pos)
321 *pos=0;
322
323 char binsprofiles_fname[256];
324 strcpy(binsprofiles_fname, basename);
325 strcat(binsprofiles_fname, ".sprofiles.bin");
326 FILE *fp = fopen(binsprofiles_fname, "rb");
327 if (fp == NULL) {
328 printf("Collocation profiles %s not found. No problem.\n", binsprofiles_fname);
329 return 0;
330 }
331 fseek(fp, 0L, SEEK_END);
332 size_t sz = ftell(fp);
333 fclose(fp);
334
335 int fd = open(binsprofiles_fname, O_RDONLY);
336 sprofiles = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0);
337 if (sprofiles == MAP_FAILED) {
338 close(fd);
339 fprintf(stderr, "Cannot mmap %s\n", binsprofiles_fname);
340 sprofiles = NULL;
341 return 0;
342 } else {
343 sprofiles_qty = sz / sizeof(profile_t);
344 fprintf(stderr, "Successfully mmaped %s containing similar profiles for %ld word forms.\n", binsprofiles_fname, sprofiles_qty);
345 }
346 return 1;
347}
348
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100349int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100350 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100351 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100352 long long a, b, c, d, cn;
353 float len;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200354 double val;
Marc Kupietz82b02672016-02-26 12:32:25 +0100355
Marc Kupietz67c20282016-02-26 09:42:00 +0100356 char binvecs_fname[256], binwords_fname[256];
357 strcpy(binwords_fname, file_name);
358 strcat(binwords_fname, ".words");
359 strcpy(binvecs_fname, file_name);
360 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200361
Marc Kupietza5b90152016-03-15 17:39:19 +0100362 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200363 f = fopen(file_name, "rb");
364 if (f == NULL) {
365 printf("Input file %s not found\n", file_name);
366 return -1;
367 }
368 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100369 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200370 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100371 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
372 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100373 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
374 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
375 if (M == NULL) {
376 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
377 return -1;
378 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200379 if(strstr(file_name, ".txt")) {
380 for (b = 0; b < words; b++) {
381 a = 0;
382 while (1) {
383 vocab[b * max_w + a] = fgetc(f);
384 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
385 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
386 }
387 vocab[b * max_w + a] = 0;
388 len = 0;
389 for (a = 0; a < size; a++) {
390 fscanf(f, "%lf", &val);
391 M[a + b * size] = val;
392 len += val * val;
393 }
394 len = sqrt(len);
395 for (a = 0; a < size; a++) M[a + b * size] /= len;
396 }
397 } else {
398 for (b = 0; b < words; b++) {
399 a = 0;
400 while (1) {
401 vocab[b * max_w + a] = fgetc(f);
402 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
403 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
404 }
405 vocab[b * max_w + a] = 0;
406 fread(&M[b * size], sizeof(float), size, f);
407 len = 0;
408 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
409 len = sqrt(len);
410 for (a = 0; a < size; a++) M[a + b * size] /= len;
411 }
412 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100413 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
414 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
415 fclose(binvecs);
416 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
417 fclose(binwords);
418 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100419 }
420 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
421 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
422 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
423 if (M == MAP_FAILED || vocab == MAP_FAILED) {
424 close(binvecs_fd);
425 close(binwords_fd);
426 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
427 exit(-1);
428 }
429 } else {
430 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
431 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100432 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200433 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100434
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200435 if(net_name && strlen(net_name) > 0) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100436 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
437 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
438 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100439 // munmap(M, sizeof(float) * words * size);
440 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 +0200441 if (M2 == MAP_FAILED) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100442 close(net_fd);
443 fprintf(stderr, "Cannot mmap %s\n", net_name);
444 exit(-1);
445 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100446 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100447 } else {
448 fprintf(stderr, "Cannot open %s\n", net_name);
449 exit(-1);
450 }
451 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
Marc Kupietze243efd2018-01-11 22:19:24 +0100452
453 char collocatordb_name[2048];
454 strcpy(collocatordb_name, net_name);
455 char *ext = rindex(collocatordb_name, '.');
456 if(ext) {
457 strcpy(ext, ".rocksdb");
458 if(access(collocatordb_name, R_OK) == 0) {
459 *ext = 0;
460 fprintf(stderr, "Opening collocator DB %s\n", collocatordb_name);
461 cdb = open_collocatordb(collocatordb_name);
462 }
463 }
464 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100465
466 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
467 for (i = 0; i < EXP_TABLE_SIZE; i++) {
468 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
469 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
470 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100471 window_sums = malloc(sizeof(float) * (window+1) * 2);
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100472
473 return 0;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200474}
475
Marc Kupietza2e64502016-04-27 09:53:51 +0200476long mergeVectors(char *file_name){
477 FILE *f, *binvecs, *binwords;
478 int binwords_fd, binvecs_fd, net_fd, i;
479 long long a, b, c, d, cn;
480 float len;
481 float *merge_vecs;
482 char *merge_vocab;
Marc Kupietza5f60042017-05-04 10:38:12 +0200483 /* long long merge_words, merge_size; */
484 long long merge_size;
Marc Kupietza2e64502016-04-27 09:53:51 +0200485
486 char binvecs_fname[256], binwords_fname[256];
487 strcpy(binwords_fname, file_name);
488 strcat(binwords_fname, ".words");
489 strcpy(binvecs_fname, file_name);
490 strcat(binvecs_fname, ".vecs");
491
492 f = fopen(file_name, "rb");
493 if (f == NULL) {
494 printf("Input file %s not found\n", file_name);
495 exit -1;
496 }
497 fscanf(f, "%lld", &merge_words);
498 fscanf(f, "%lld", &merge_size);
499 if(merge_size != size){
500 fprintf(stderr, "vectors must have the same length\n");
501 exit(-1);
502 }
503 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
504 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
505 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
506 if (merge_vecs == NULL || merge_vocab == NULL) {
507 close(binvecs_fd);
508 close(binwords_fd);
509 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
510 exit(-1);
511 }
512 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
513 read(binwords_fd, merge_vocab, merge_words * max_w);
514 } else {
515 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
516 exit(-1);
517 }
518 printf("Successfully reallocated memory\nMerging...\n");
519 fflush(stdout);
520 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
521 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
522 munmap(M, words * size * sizeof(float));
523 munmap(vocab, words * max_w);
524 M = merge_vecs;
525 vocab = merge_vocab;
526 merged_end = merge_words;
527 words += merge_words;
528 fclose(f);
529 printf("merged_end: %lld, words: %lld\n", merged_end, words);
Marc Kupietzebc131e2019-01-03 16:24:27 +0100530 printBiggestMergedDifferences();
Marc Kupietza2e64502016-04-27 09:53:51 +0200531 return((long) merged_end);
532}
533
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200534void filter_garbage() {
535 long i;
Marc Kupietzab591a82016-04-28 14:08:49 +0200536 unsigned char *w, previous, c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200537 garbage = malloc(words);
538 memset(garbage, 0, words);
539 for (i = 0; i < words; i++) {
540 w = vocab + i * max_w;
541 previous = 0;
Marc Kupietz969adfa2018-03-12 09:43:53 +0100542 if(strncmp("quot", w, 4) == 0) {
543 garbage[i]=1;
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100544// printf("Gargabe: %s\n", vocab + i * max_w);
Marc Kupietz969adfa2018-03-12 09:43:53 +0100545 } else {
546 while((c = *w++) && !garbage[i]) {
547 if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
548 (previous == '-' && (c & 32)) ||
549 (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) ||
550 (previous == 'q' && c == 'u' && *(w) == 'o' && *(w+1) == 't') || /* quot */
551 c == '<'
552 ) {
553 garbage[i]=1;
554 continue;
555 }
556 previous = c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200557 }
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200558 }
559 }
560 return;
561}
562
Marc Kupietzac707b32018-12-20 11:36:38 +0100563
564knn *simpleGetCollocators(int word, int number, long cutoff, int *result) {
565 knnpars *pars = calloc(sizeof(knnpars), 1);
566 float *target_sums;
567 float *window_sums = malloc(sizeof(float) * (window+1) * 2);
568 pars->cutoff = (cutoff? cutoff : 300000);
569 long a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
570 for(a = 0; a < cutoff; a++)
571 target_sums[a] = 0;
572 pars->target_sums = target_sums;
573 pars->window_sums = window_sums;
574 pars->N = (number? number : 20);
575 pars->from = 0;
576 pars->upto = window * 2 -1;
577 knn *syn_nbs; // = (knn*) getCollocators(pars);
578 free(pars);
579 free(window_sums);
580 free(target_sums);
581 return syn_nbs;
582}
583
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100584void *getCollocators(void *args) {
585 knnpars *pars = args;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100586 int N = pars->N;
587 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100588 knn *nbs = NULL;
589 long window_layer_size = size * window * 2;
590 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
591 float f, max_f, maxmax_f;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100592 float *target_sums, worstbest, wpos_sum;
593 collocator *best;
Marc Kupietzd5642582016-03-19 22:23:13 +0100594
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200595 if(M2 == NULL || cc == -1)
Marc Kupietzd5642582016-03-19 22:23:13 +0100596 return NULL;
597
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100598 a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100599 best = malloc(N * sizeof(collocator));
Marc Kupietz271e2a42016-03-22 11:37:43 +0100600 worstbest = MIN_RESP;
601
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100602 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100603 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100604 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100605 best[b].wordi = -1;
606 best[b].probability = 1;
607 best[b].activation = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100608 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100609
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100610 d = cc;
611 maxmax_f = -1;
612 maxmax_target = 0;
613
Marc Kupietz271e2a42016-03-22 11:37:43 +0100614 for (a = pars->from; a < pars->upto; a++) {
615 if(a >= window)
616 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100617 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100618 printf("window pos: %ld\n", a);
619 if (a != window) {
620 max_f = -1;
621 window_offset = a * size;
622 if (a > window)
623 window_offset -= size;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100624 for(target = 0; target < pars->cutoff; target ++) {
625 if(garbage && garbage[target]) continue;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100626 if(target == d)
627 continue;
628 f = 0;
629 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100630 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100631 if (f < -MAX_EXP)
632 continue;
633 else if (f > MAX_EXP)
634 continue;
635 else
636 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100637 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100638
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100639 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100640 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100641 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100642 if (f > best[b].activation) {
643 memmove(best + b + 1, best + b, (N - b -1) * sizeof(collocator));
644 best[b].activation = f;
645 best[b].wordi = target;
646 best[b].position = window-a;
Marc Kupietz33679a32016-03-22 08:49:39 +0100647 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100648 }
649 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100650 if(b == N - 1)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100651 worstbest = best[N-1].activation;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100652 }
653 }
654 printf("%d %.2f\n", max_target, max_f);
655 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
656 if(max_f > maxmax_f) {
657 maxmax_f = max_f;
658 maxmax_target = max_target;
659 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100660 for (b = 0; b < N; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100661 if(best[b].position == window-a)
Marc Kupietza77acce2017-11-30 16:59:07 +0100662 best[b].cprobability = best[b].activation / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100663 } else {
664 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
665 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100666 pars->window_sums[a] = wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100667 }
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100668 for (b = 0; b < pars->cutoff; b++)
Marc Kupietza77acce2017-11-30 16:59:07 +0100669 pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2);
670 printf("Target-Summe von 0: %f\n", pars->target_sums[150298]);
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100671 free(target_sums);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100672 for(b=0; b<N && best[b].wordi >= 0; b++);; // THIS LOOP IS NEEDED (b...)
673// 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 +0100674// printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100675 nbs = malloc(sizeof(knn));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100676 nbs->best = best;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100677 nbs->length = b-1;
678 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100679}
680
Marc Kupietz30ca4342017-11-22 21:21:20 +0100681
Marc Kupietz66bfd952017-12-11 09:59:45 +0100682AV *getVecs(AV *array) {
683 int i, b;
684 AV *result = newAV();
685 for (i=0; i<=av_len(array); i++) {
686 SV** elem = av_fetch(array, i, 0);
687 if (elem != NULL) {
688 long j = (long) SvNV(*elem);
689 AV *vector = newAV();
690 for (b = 0; b < size; b++) {
691 av_push(vector, newSVnv(M[b + j * size]));
692 }
693 av_push(result, newRV_noinc(vector));
694 }
695 }
696 return result;
697}
698
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100699char *getSimilarProfiles(long node) {
700 int i;
701 char buffer[120000];
702 char pair_buffer[2048];
703 buffer[0]='[';
704 buffer[1]=0;
705 if(node >= sprofiles_qty) {
706 printf("Not available in precomputed profile\n");
707 return(strdup("[{\"w\":\"not available\", \"v\":0}]\n"));
708 }
709
710 printf("******* %s ******\n", &vocab[max_w * node]);
711
712 for(i=0; i < 100 && i < sprofiles[node].len; i++) {
713 sprintf(pair_buffer, "{\"w\":\"%s\", \"v\":%f},", &vocab[max_w * (sprofiles[node].nbr[i].index)], sprofiles[node].nbr[i].value);
714 strcat(buffer, pair_buffer);
715 }
716 buffer[strlen(buffer)-1]=']';
717 strcat(buffer, "\n");
718 printf(buffer);
719 return(strdup(buffer));
720}
721
Marc Kupietze243efd2018-01-11 22:19:24 +0100722char *getClassicCollocators(long node) {
723 char *res = (cdb? strdup(get_collocators_as_json(cdb, node)) : "[]");
724 return res;
725}
726
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100727wordlist *getTargetWords(char *st1, int sbw) {
Marc Kupietz48c29682016-03-19 11:30:43 +0100728 wordlist *wl = malloc(sizeof(wordlist));
729 char st[100][max_size], sep[100];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100730 long a, b=0, c=0, cn=0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200731 int unmerged;
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100732 int search_backw = 0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200733
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100734 while (1) {
735 st[cn][b] = st1[c];
736 if (merge_words > 0)
Marc Kupietz2e2e4db2018-12-21 15:07:37 +0100737 st[cn+1][b] = st1[c];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100738 b++;
739 c++;
740 st[cn][b] = 0;
741 if (st1[c] == 0) break;
742 if (st1[c] == ' ' || st1[c] == '-') {
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100743 sep[cn++] = st1[c];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100744 if (merge_words > 0)
Marc Kupietz2e2e4db2018-12-21 15:07:37 +0100745 sep[cn++] = st1[c];
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100746 b = 0;
747 c++;
748 }
749 }
750 cn++;
751 if (merge_words > 0)
Marc Kupietz2e2e4db2018-12-21 15:07:37 +0100752 cn++;
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100753 for (a = 0; a < cn; a++) {
754 if (search_backw) {
755 for (b = words - 1; b >= 0; b--) if (!strcmp(&vocab[b * max_w], st[a])) break;
756 } else {
757 if (merge_words > 0 && a % 2 == 1) {
758 for (b = merge_words; b < words; b++)
759 if (!strcmp(&vocab[b * max_w], st[a])) break;
760 } else {
761 for (b = 0; b < words; b++)
762 if (!strcmp(&vocab[b * max_w], st[a])) break;
763 }
764 }
765 if (b == words) b = -1;
766 wl->wordi[a] = b;
767 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], wl->wordi[a]);
768 if (b == -1) {
769 fprintf(stderr, "Out of dictionary word!\n");
770 cn--;
771 }
772 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100773 wl->length=cn;
774 return(wl);
775}
Marc Kupietz1f36dfa2019-01-03 16:18:12 +0100776
Marc Kupietzebc131e2019-01-03 16:24:27 +0100777void printBiggestMergedDifferences() {
778 float dist, len, vec[max_size];
779 long long a, b, c, d, cn, *bi;
780 char ch;
781 knn *nbs = NULL;
782 int N = 100;
783
784 printf("Looking for biggest distances between main and merged vectors ...\n");
785 collocator *best;
786 best = malloc(N * sizeof(collocator));
787 memset(best, 0, N * sizeof(collocator));
788
789 float worstbest=1000000;
790
791 for (a = 0; a < N; a++) best[a].activation = worstbest;
792
793 for (c = 0; c < 10000; c++) {
794 if(garbage && garbage[c]) continue;
795 a = 0;
796 dist = 0;
797 for (a = 0; a < size; a++) dist += M[a + c * size] * M[a + (c+merged_end) * size];
798 if(dist < worstbest) {
799 for (a = 0; a < N; a++) {
800 if (dist < best[a].activation) {
801 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
802 best[a].activation = dist;
803 best[a].wordi = c;
804 break;
805 }
806 }
807 worstbest = best[N-1].activation;
808 }
809 }
810
811 printf("Most distant vectors for:\n ");
812 for (a = 0; a < N; a++) {
813 printf("%s ", &vocab[best[a].wordi * max_w]);
814 }
815 printf("\n");
816}
817
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100818void *_get_neighbours(void *arg) {
819 knnpars *pars = arg;
Marc Kupietz48c29682016-03-19 11:30:43 +0100820 char *st1 = pars->token;
821 int N = pars->N;
822 long from = pars -> from;
823 unsigned long upto = pars -> upto;
824 char file_name[max_size], st[100][max_size], *sep;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100825 float dist, len, vec[max_size];
826 long long a, b, c, d, cn, *bi;
Marc Kupietz48c29682016-03-19 11:30:43 +0100827 char ch;
828 knn *nbs = NULL;
829 wordlist *wl = pars->wl;
830
Marc Kupietzc55c8872017-12-01 23:27:10 +0100831 collocator *best = pars->best;
Marc Kupietz48c29682016-03-19 11:30:43 +0100832
833 float worstbest=-1;
834
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100835 for (a = 0; a < N; a++) best[a].activation = 0;
Marc Kupietz48c29682016-03-19 11:30:43 +0100836 a = 0;
837 bi = wl->wordi;
838 cn = wl->length;
839 sep = wl->sep;
840 b = bi[0];
841 c = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100842 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100843 N = 0;
844 goto end;
845 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200846 for (a = 0; a < size; a++) vec[a] = 0;
847 for (b = 0; b < cn; b++) {
848 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100849 if(b>0 && sep[b-1] == '-')
850 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
851 else
852 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200853 }
854 len = 0;
855 for (a = 0; a < size; a++) len += vec[a] * vec[a];
856 len = sqrt(len);
857 for (a = 0; a < size; a++) vec[a] /= len;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100858 for (a = 0; a < N; a++) best[a].activation = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100859 for (c = from; c < upto; c++) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200860 if(garbage && garbage[c]) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200861 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100862// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100863// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
864// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200865 dist = 0;
866 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100867 if(dist > worstbest) {
868 for (a = 0; a < N; a++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100869 if (dist > best[a].activation) {
870 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
871 best[a].activation = dist;
872 best[a].wordi = c;
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100873 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200874 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200875 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100876 worstbest = best[N-1].activation;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200877 }
878 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100879
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100880end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100881 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200882}
883
Marc Kupietze0e42132017-11-24 16:44:34 +0100884int cmp_activation (const void * a, const void * b) {
885 float fb = ((collocator *)a)->activation;
886 float fa = ((collocator *)b)->activation;
887 return (fa > fb) - (fa < fb);
888}
889
890int cmp_probability (const void * a, const void * b) {
891 float fb = ((collocator *)a)->probability;
892 float fa = ((collocator *)b)->probability;
893 return (fa > fb) - (fa < fb);
894}
895
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100896
Marc Kupietzac707b32018-12-20 11:36:38 +0100897SV *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 +0100898 HV *result = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100899 float *target_sums, vec[max_size];
Marc Kupietza5f60042017-05-04 10:38:12 +0200900 long long old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100901 long a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100902 knn *para_nbs[MAX_THREADS];
903 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100904 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100905 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100906 wordlist *wl;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200907 int syn_threads = (M2? window * 2 : 0);
Marc Kupietzac707b32018-12-20 11:36:38 +0100908 int para_threads = (no_similar_profiles? 0 : num_threads - syn_threads);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100909
Marc Kupietzc55c8872017-12-01 23:27:10 +0100910 collocator *best;
911 posix_memalign((void **) &best, 128, 10 * N * sizeof(collocator));
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100912 memset(best, 0, 10 * N * sizeof(collocator));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100913
Marc Kupietz000ad862016-02-26 14:59:12 +0100914 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
915
Marc Kupietzd5d05732018-01-30 11:57:35 +0100916 if(cutoff < 1 || cutoff > words)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100917 cutoff=words;
918
Marc Kupietza2e64502016-04-27 09:53:51 +0200919 wl = getTargetWords(st1, search_backw);
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100920 if(wl == NULL || wl->length < 1)
Marc Kupietz271e2a42016-03-22 11:37:43 +0100921 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100922
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100923 old_words = cutoff;
Marc Kupietz1f0f7802019-01-03 16:23:29 +0100924 slice = (para_threads? cutoff / para_threads * (merge_words > 0? 2 : 1) : 0);
Marc Kupietza5f60042017-05-04 10:38:12 +0200925
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100926 a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float));
927 for(a = 0; a < cutoff; a++)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100928 target_sums[a] = 0;
929
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200930 printf("Starting %d threads\n", para_threads);
931 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100932 for(a=0; a < para_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100933 pars[a].cutoff = cutoff;
Marc Kupietz000ad862016-02-26 14:59:12 +0100934 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100935 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100936 pars[a].N = N;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100937 pars[a].best = &best[N*a];
Marc Kupietz1f0f7802019-01-03 16:23:29 +0100938 if(merge_words == 0 || a < para_threads / 2) {
939 pars[a].from = a*slice;
940 pars[a].upto = ((a+1)*slice > cutoff? cutoff : (a+1) * slice);
941 } else {
942 pars[a].from = merge_words + (a - para_threads / 2) * slice;
943 pars[a].upto = merge_words + ((a - para_threads / 2 + 1)*slice > cutoff? cutoff : (a - para_threads / 2 + 1) *slice);
944 }
945 printf("From: %ld, Upto: %ld\n", pars[a].from, pars[a].upto);
Marc Kupietz000ad862016-02-26 14:59:12 +0100946 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
947 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200948 if(M2) {
949 for(a=0; a < syn_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100950 pars[a + para_threads].cutoff = cutoff;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200951 pars[a + para_threads].target_sums = target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100952 pars[a + para_threads].window_sums = window_sums;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200953 pars[a + para_threads].wl = wl;
954 pars[a + para_threads].N = N;
955 pars[a + para_threads].from = a;
956 pars[a + para_threads].upto = a+1;
957 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
958 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100959 }
960 printf("Waiting for para threads to join\n");
961 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100962 for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *) &para_nbs[a]);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100963 printf("Para threads joint\n");
964 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +0100965
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200966 /* if(!syn_nbs[0]) */
967 /* goto end; */
Marc Kupietz000ad862016-02-26 14:59:12 +0100968
Marc Kupietzc55c8872017-12-01 23:27:10 +0100969 qsort(best, N*para_threads, sizeof(collocator), cmp_activation);
Marc Kupietz000ad862016-02-26 14:59:12 +0100970
Marc Kupietz000ad862016-02-26 14:59:12 +0100971
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100972 long long chosen[MAX_NEIGHBOURS];
973 printf("N: %ld\n", N);
974
Marc Kupietz271e2a42016-03-22 11:37:43 +0100975 AV* array = newAV();
Marc Kupietzd91212f2017-11-13 10:05:09 +0100976 int i, j;
Marc Kupietza5f60042017-05-04 10:38:12 +0200977 int l1_words=0, l2_words=0;
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100978
979 for (a = 0, i = 0; i < N && a < N*para_threads; a++) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100980 int filtered=0;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100981 long long c = best[a].wordi;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100982 if (dedupe && i > 0) {
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100983 for (j=0; j<i && !filtered; j++)
984 if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) ||
985 strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) {
986 printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]);
Marc Kupietzd91212f2017-11-13 10:05:09 +0100987 filtered = 1;
988 }
989 if(filtered)
990 continue;
991 }
Marc Kupietz1f0f7802019-01-03 16:23:29 +0100992/*
Marc Kupietza5f60042017-05-04 10:38:12 +0200993 if(merge_words > 0) {
994 if(c >= merge_words) {
995 if(l1_words > N / 2)
996 continue;
997 else
998 l1_words++;
999 } else {
1000 if(l2_words > N / 2)
1001 continue;
1002 else
1003 l2_words++;
1004 }
1005 }
Marc Kupietz1f0f7802019-01-03 16:23:29 +01001006*/
Marc Kupietza5f60042017-05-04 10:38:12 +02001007 printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a);
Marc Kupietzd0d3e912017-12-01 22:09:43 +01001008 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001009 HV* hash = newHV();
Marc Kupietza5f60042017-05-04 10:38:12 +02001010 SV* word = newSVpvf(&vocab[c * max_w], 0);
Marc Kupietzd0d3e912017-12-01 22:09:43 +01001011 chosen[i] = c;
Marc Kupietz271e2a42016-03-22 11:37:43 +01001012 if(latin_enc == 0) SvUTF8_on(word);
Marc Kupietza5f60042017-05-04 10:38:12 +02001013 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001014 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001015 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
1016 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001017 AV *vector = newAV();
1018 for (b = 0; b < size; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001019 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001020 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001021 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
1022 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietza5f60042017-05-04 10:38:12 +02001023 i++;
Marc Kupietz271e2a42016-03-22 11:37:43 +01001024 }
1025 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
1026
Marc Kupietz50485ba2016-03-23 09:13:14 +01001027 for(b=0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001028 best[b].wordi = -1L;
1029 best[b].activation = 0;
1030 best[b].probability = 0;
1031 best[b].position = 0;
1032 best[b].activation_sum = 0;
Marc Kupietz50485ba2016-03-23 09:13:14 +01001033 }
1034
Marc Kupietza77acce2017-11-30 16:59:07 +01001035 float total_activation = 0;
1036
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001037 if (M2) {
1038 printf("Waiting for syn threads to join\n");
1039 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001040 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], (void *) &syn_nbs[a]);
Marc Kupietza77acce2017-11-30 16:59:07 +01001041 for (a = 0; a <= syn_threads; a++) {
Marc Kupietz63872182018-03-12 09:44:49 +01001042 if(a == window) continue;
Marc Kupietza77acce2017-11-30 16:59:07 +01001043 total_activation += window_sums[a];
1044 printf("window pos: %d, sum: %f\n", a, window_sums[a]);
1045 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001046 printf("syn threads joint\n");
1047 fflush(stdout);
Marc Kupietz50485ba2016-03-23 09:13:14 +01001048
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001049 for(b=0; b < syn_nbs[0]->length; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001050 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
1051 best[b].position = -1; // syn_nbs[0]->pos[b];
1052 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001053 best[b].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +01001054 best[b].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001055 best[b].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +01001056 best[b].cprobability = syn_nbs[0]->best[b].cprobability;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001057 }
Marc Kupietz30ca4342017-11-22 21:21:20 +01001058
1059 float best_window_sum[MAX_NEIGHBOURS];
1060 int found_index=0, i=0, j, w;
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001061 for(a=0; a < syn_threads; a++) {
1062 for(b=0; b < syn_nbs[a]->length; b++) {
1063 for(i=0; i < found_index; i++)
1064 if(best[i].wordi == syn_nbs[a]->best[b].wordi)
1065 break;
1066 if(i >= found_index) {
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001067 best[found_index].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +01001068 best[found_index].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001069 best[found_index].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +01001070 best[found_index].cprobability = syn_nbs[a]->best[b].cprobability;
1071 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 +01001072 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
1073 // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
Marc Kupietz30ca4342017-11-22 21:21:20 +01001074 }
1075 }
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001076 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001077 sort_by =0; // ALWAYS AUTO-FOCUS
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001078 if(sort_by != 1 && sort_by != 2) { // sort by auto focus mean
Marc Kupietz30ca4342017-11-22 21:21:20 +01001079 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) -1);
Marc Kupietzbd41da32017-11-24 10:26:43 +01001080 int wpos;
Marc Kupietz4116b432017-12-06 14:15:32 +01001081 int bits_set = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001082 for(i=0; i < found_index; i++) {
Marc Kupietz4116b432017-12-06 14:15:32 +01001083 best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001084 for(w=1; w < (1 << syn_threads); w++) { // loop through all possible windows
Marc Kupietz4116b432017-12-06 14:15:32 +01001085 float word_window_sum = 0, word_window_average=0, word_cprobability_sum=0, word_activation_sum = 0, total_window_sum = 0;
1086 bits_set = 0;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001087 for(a=0; a < syn_threads; a++) {
Marc Kupietz30ca4342017-11-22 21:21:20 +01001088 if((1 << a) & w) {
Marc Kupietzbd41da32017-11-24 10:26:43 +01001089 wpos = (a >= window? a+1 : a);
1090 total_window_sum += window_sums[wpos];
Marc Kupietz30ca4342017-11-22 21:21:20 +01001091 }
1092 }
Marc Kupietzbd41da32017-11-24 10:26:43 +01001093// printf("%d window-sum %f\n", w, total_window_sum);
1094 for(a=0; a < syn_threads; a++) {
1095 if((1 << a) & w) {
1096 wpos = (a >= window? a+1 : a);
1097 bits_set++;
1098 for(b=0; b < syn_nbs[a]->length; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001099 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001100// float acti = syn_nbs[a]->best[b].activation / total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001101// 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 +01001102// word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1103// 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 +01001104
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001105 word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
Marc Kupietza77acce2017-11-30 16:59:07 +01001106// word_window_sum += acti - (word_window_sum * acti); syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1107
Marc Kupietz4116b432017-12-06 14:15:32 +01001108 word_window_average += syn_nbs[a]->best[b].activation; // - word_window_average * syn_nbs[a]->best[b].activation; // conormalied activation sum
1109 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 +01001110 word_activation_sum += syn_nbs[a]->best[b].activation;
1111 if(syn_nbs[a]->best[b].activation > best[i].max_activation)
1112 best[i].max_activation = syn_nbs[a]->best[b].activation;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +01001113 if(syn_nbs[a]->best[b].activation > best[i].heat[wpos] )
Marc Kupietz728b8ed2017-12-02 11:13:49 +01001114 best[i].heat[wpos] = syn_nbs[a]->best[b].activation;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001115 }
1116 }
1117 }
Marc Kupietz4116b432017-12-06 14:15:32 +01001118 if(bits_set) {
1119 word_window_average /= bits_set;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001120// word_activation_sum /= bits_set;
1121// word_window_sum /= bits_set;
Marc Kupietz4116b432017-12-06 14:15:32 +01001122 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001123
1124 word_window_sum /= total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001125
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001126 if(word_window_sum > best[i].probability) {
Marc Kupietz4116b432017-12-06 14:15:32 +01001127// best[i].position = w;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001128 best[i].probability = word_window_sum;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001129 }
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001130
Marc Kupietz4116b432017-12-06 14:15:32 +01001131 if(word_cprobability_sum > best[i].cprobability_sum) {
1132 best[i].position = w;
1133 best[i].cprobability_sum = word_cprobability_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001134 }
Marc Kupietz4116b432017-12-06 14:15:32 +01001135
1136 best[i].average = word_window_average;
1137// best[i].activation = word_activation_sum;
1138 }
Marc Kupietz30ca4342017-11-22 21:21:20 +01001139 }
Marc Kupietze0e42132017-11-24 16:44:34 +01001140 qsort(best, found_index, sizeof(collocator), cmp_probability);
Marc Kupietz30ca4342017-11-22 21:21:20 +01001141// for(i=0; i < found_index; i++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001142// 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 +01001143// }
1144
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001145 } else if(sort_by == 1) { // responsiveness any window position
1146 int wpos;
1147 for(i=0; i < found_index; i++) {
1148 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
1149 for(a=0; a < syn_threads; a++) {
1150 wpos = (a >= window? a+1 : a);
1151 for(b=0; b < syn_nbs[a]->length; b++)
1152 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
1153 best[i].probability += syn_nbs[a]->best[b].probability;
1154 if(syn_nbs[a]->best[b].activation > 0.25)
1155 best[i].position |= 1 << wpos;
1156 if(syn_nbs[a]->best[b].activation > best[i].activation) {
1157 best[i].activation = syn_nbs[a]->best[b].activation;
1158 }
1159 }
1160 }
1161 }
1162 qsort(best, found_index, sizeof(collocator), cmp_activation);
1163 } else if(sort_by == 2) { // single window position
Marc Kupietz30ca4342017-11-22 21:21:20 +01001164 for(a=1; a < syn_threads; a++) {
1165 for(b=0; b < syn_nbs[a]->length; b++) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001166 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001167 if(syn_nbs[a]->best[b].activation > best[c].activation) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001168 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001169 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001170 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001171 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
1172 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 +02001173 break;
Marc Kupietz6d9a6782016-03-23 17:25:25 +01001174 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001175 }
1176 }
1177 }
1178 } else { // sort by mean p
1179 for(a=1; a < syn_threads; a++) {
1180 for(b=0; b < syn_nbs[a]->length; b++) {
1181 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001182 if(target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001183 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001184 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001185 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001186 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
1187 best[c].position = (1 << 2*window) - 1; // syn_nbs[a]->pos[b];
1188 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001189 break;
1190 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001191 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +01001192 }
1193 }
1194 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001195 array = newAV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001196 for (a = 0, i=0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
1197 long long c = best[a].wordi;
Marc Kupietza77acce2017-11-30 16:59:07 +01001198/*
Marc Kupietzd91212f2017-11-13 10:05:09 +01001199 if (dedupe) {
1200 int filtered=0;
1201 for (j=0; j<i; j++)
1202 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
1203 strcasestr(chosen[j], &vocab[c * max_w])) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001204 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
1205 filtered = 1;
1206 }
Marc Kupietzd91212f2017-11-13 10:05:09 +01001207 if(filtered)
1208 continue;
1209 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001210*/
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001211 chosen[i++]=c;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001212 HV* hash = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001213 SV* word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001214 AV* heat = newAV();
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001215 if(latin_enc == 0) SvUTF8_on(word);
1216 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001217 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001218 hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001219 hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001220 hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001221 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 +01001222 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 +01001223 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001224 best[a].heat[5]=0;
1225 for(i=10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i]));
1226 hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV*)heat), 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001227 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietz271e2a42016-03-22 11:37:43 +01001228 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001229 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001230 }
Marc Kupietz000ad862016-02-26 14:59:12 +01001231end:
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001232 words = old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001233 free(best);
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001234 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +01001235}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +01001236
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001237int dump_vecs(char *fname) {
1238 long i, j;
1239 FILE *f;
Marc Kupietz0a68a792018-12-20 11:41:45 +01001240 if(words>100000)
1241 words=100000;
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001242
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001243 if((f=fopen(fname, "w")) == NULL) {
1244 fprintf(stderr, "cannot open %s for writing\n", fname);
1245 return(-1);
1246 }
1247 fprintf(f, "%lld %lld\n", words, size);
1248 for (i=0; i < words; i++) {
1249 fprintf(f, "%s ", &vocab[i * max_w]);
1250 for(j=0; j < size - 1; j++)
1251 fprintf(f, "%f ", M[i*size + j]);
1252 fprintf(f, "%f\n", M[i*size + j]);
1253 }
1254 fclose(f);
1255 return(0);
1256}
Marc Kupietzdc22b982015-10-09 09:19:34 +02001257
Marc Kupietzfa194262018-06-05 09:39:32 +02001258int dump_for_numpy(char *fname) {
1259 long i, j;
1260 FILE *f;
1261 /* if(words>200000) */
1262 /* words=200000; */
1263
1264 if((f=fopen(fname, "w")) == NULL) {
1265 fprintf(stderr, "cannot open %s for writing\n", fname);
1266 return(-1);
1267 }
1268 for (i=0; i < words; i++) {
1269 for(j=0; j < size - 1; j++)
1270 fprintf(f, "%f\t", M[i*size + j]);
1271 fprintf(f, "%f\n", M[i*size + j]);
1272 }
1273 fclose(f);
1274 return(0);
1275}