blob: 92c223f662a4d4a3d0fb6d7e6c6a404194ec1d79 [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 Kupietz6ed81872016-04-27 14:04:04 +020067
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 Kupietz6b2975c2016-03-18 21:59:33 +0100174 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100175 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100176 my @collocations;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100177 if(defined($word) && $word !~ /^\s*$/) {
178 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100179 $word =~ s/\s+/ /g;
180 for my $w (split(' *\| *', $word)) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100181 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100182 $c->app->log->info("Getting $w results from cache");
Marc Kupietzd91212f2017-11-13 10:05:09 +0100183 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe}
Marc Kupietza5b90152016-03-15 17:39:19 +0100184 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100185 $c->app->log->info('Looking for neighbours of '.$w);
186 if($opt_i) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100187 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe);
Marc Kupietz15987412017-11-07 15:56:58 +0100188 } else {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100189 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe);
Marc Kupietz15987412017-11-07 15:56:58 +0100190 }
Marc Kupietz2dd2dd72017-12-01 22:08:14 +0100191 $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100192 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100193 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100194 }
195 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100196 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200197 if($json) {
198 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100199 } elsif($csv) {
200 my $csv_data="";
201 for (my $i=0; $i <= $no_nbs; $i++) {
202 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
203 }
204 for (my $i=0; $i < $no_nbs; $i++) {
205 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
206 }
207 chop $csv_data;
208 chop $csv_data;
209 $csv_data .= "\n";
210 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200211 } else {
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100212 $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 +0200213 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200214};
215
Marc Kupietz30ca4342017-11-22 21:21:20 +0100216helper(bitvec2window => sub {
217 my ($self, $n) = @_;
218 my $str = unpack("B32", pack("N", $n));
219 $str =~ s/^\d{22}//;
220 $str =~ s/^(\d{5})/$1x/;
221 $str =~ s/0/·/g;
222 $str =~ s/1/+/g;
223 return $str;
224 });
225
Marc Kupietza5b90152016-03-15 17:39:19 +0100226$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200227
228exit;
229
230__END__
231
232__C__
233#include <stdio.h>
234#include <string.h>
235#include <math.h>
236#include <malloc.h>
237#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +0100238#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +0100239#include <pthread.h>
Marc Kupietze243efd2018-01-11 22:19:24 +0100240#include <collocatordb.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200241
242#define max_size 2000
243#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100244#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100245#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +0100246#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100247#define MAX_CC 50
248#define EXP_TABLE_SIZE 1000
249#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +0100250#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +0200251
252//the thread function
253void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +0100254
255typedef struct {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100256 long long wordi;
257 long position;
258 float activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100259 float average;
Marc Kupietza77acce2017-11-30 16:59:07 +0100260 float cprobability; // column wise probability
Marc Kupietz4116b432017-12-06 14:15:32 +0100261 float cprobability_sum;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100262 float probability;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100263 float activation_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100264 float max_activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100265 float heat[16];
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100266} collocator;
267
268typedef struct {
269 collocator *best;
Marc Kupietz80abb442016-03-23 21:04:08 +0100270 int length;
Marc Kupietz000ad862016-02-26 14:59:12 +0100271} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100272
Marc Kupietz000ad862016-02-26 14:59:12 +0100273typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +0100274 long long wordi[MAX_NEIGHBOURS];
275 char sep[MAX_NEIGHBOURS];
276 int length;
277} wordlist;
278
279typedef struct {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100280 long cutoff;
Marc Kupietz48c29682016-03-19 11:30:43 +0100281 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100282 char *token;
283 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100284 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100285 unsigned long upto;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100286 collocator *best;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100287 float *target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100288 float *window_sums;
Marc Kupietz000ad862016-02-26 14:59:12 +0100289} knnpars;
290
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100291typedef struct {
292 uint32_t index;
293 float value;
294} sparse_t;
295
296typedef struct {
297 uint32_t len;
298 sparse_t nbr[100];
299} profile_t;
300
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200301float *M, *M2=0L, *syn1neg_window, *expTable;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100302float *window_sums;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200303char *vocab;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200304char *garbage = NULL;
Marc Kupietze243efd2018-01-11 22:19:24 +0100305COLLOCATORDB *cdb = NULL;
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100306profile_t *sprofiles = NULL;
307size_t sprofiles_qty = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100308
Marc Kupietza2e64502016-04-27 09:53:51 +0200309long long words, size, merged_end;
Marc Kupietza5f60042017-05-04 10:38:12 +0200310long long merge_words = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100311int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100312int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100313int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200314
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100315/* load collocation profiles if file exists */
316int load_sprofiles(char *vecsname) {
317 char *basename = strdup(vecsname);
318 char *pos = strstr(basename, ".vecs");
319 if(pos)
320 *pos=0;
321
322 char binsprofiles_fname[256];
323 strcpy(binsprofiles_fname, basename);
324 strcat(binsprofiles_fname, ".sprofiles.bin");
325 FILE *fp = fopen(binsprofiles_fname, "rb");
326 if (fp == NULL) {
327 printf("Collocation profiles %s not found. No problem.\n", binsprofiles_fname);
328 return 0;
329 }
330 fseek(fp, 0L, SEEK_END);
331 size_t sz = ftell(fp);
332 fclose(fp);
333
334 int fd = open(binsprofiles_fname, O_RDONLY);
335 sprofiles = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0);
336 if (sprofiles == MAP_FAILED) {
337 close(fd);
338 fprintf(stderr, "Cannot mmap %s\n", binsprofiles_fname);
339 sprofiles = NULL;
340 return 0;
341 } else {
342 sprofiles_qty = sz / sizeof(profile_t);
343 fprintf(stderr, "Successfully mmaped %s containing similar profiles for %ld word forms.\n", binsprofiles_fname, sprofiles_qty);
344 }
345 return 1;
346}
347
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100348int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100349 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100350 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100351 long long a, b, c, d, cn;
352 float len;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200353 double val;
Marc Kupietz82b02672016-02-26 12:32:25 +0100354
Marc Kupietz67c20282016-02-26 09:42:00 +0100355 char binvecs_fname[256], binwords_fname[256];
356 strcpy(binwords_fname, file_name);
357 strcat(binwords_fname, ".words");
358 strcpy(binvecs_fname, file_name);
359 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200360
Marc Kupietza5b90152016-03-15 17:39:19 +0100361 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200362 f = fopen(file_name, "rb");
363 if (f == NULL) {
364 printf("Input file %s not found\n", file_name);
365 return -1;
366 }
367 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100368 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200369 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100370 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
371 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100372 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
373 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
374 if (M == NULL) {
375 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
376 return -1;
377 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200378 if(strstr(file_name, ".txt")) {
379 for (b = 0; b < words; b++) {
380 a = 0;
381 while (1) {
382 vocab[b * max_w + a] = fgetc(f);
383 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
384 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
385 }
386 vocab[b * max_w + a] = 0;
387 len = 0;
388 for (a = 0; a < size; a++) {
389 fscanf(f, "%lf", &val);
390 M[a + b * size] = val;
391 len += val * val;
392 }
393 len = sqrt(len);
394 for (a = 0; a < size; a++) M[a + b * size] /= len;
395 }
396 } else {
397 for (b = 0; b < words; b++) {
398 a = 0;
399 while (1) {
400 vocab[b * max_w + a] = fgetc(f);
401 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
402 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
403 }
404 vocab[b * max_w + a] = 0;
405 fread(&M[b * size], sizeof(float), size, f);
406 len = 0;
407 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
408 len = sqrt(len);
409 for (a = 0; a < size; a++) M[a + b * size] /= len;
410 }
411 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100412 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
413 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
414 fclose(binvecs);
415 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
416 fclose(binwords);
417 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100418 }
419 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
420 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
421 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
422 if (M == MAP_FAILED || vocab == MAP_FAILED) {
423 close(binvecs_fd);
424 close(binwords_fd);
425 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
426 exit(-1);
427 }
428 } else {
429 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
430 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100431 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200432 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100433
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200434 if(net_name && strlen(net_name) > 0) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100435 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
436 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
437 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100438 // munmap(M, sizeof(float) * words * size);
439 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 +0200440 if (M2 == MAP_FAILED) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100441 close(net_fd);
442 fprintf(stderr, "Cannot mmap %s\n", net_name);
443 exit(-1);
444 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100445 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100446 } else {
447 fprintf(stderr, "Cannot open %s\n", net_name);
448 exit(-1);
449 }
450 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
Marc Kupietze243efd2018-01-11 22:19:24 +0100451
452 char collocatordb_name[2048];
453 strcpy(collocatordb_name, net_name);
454 char *ext = rindex(collocatordb_name, '.');
455 if(ext) {
456 strcpy(ext, ".rocksdb");
457 if(access(collocatordb_name, R_OK) == 0) {
458 *ext = 0;
459 fprintf(stderr, "Opening collocator DB %s\n", collocatordb_name);
460 cdb = open_collocatordb(collocatordb_name);
461 }
462 }
463 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100464
465 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
466 for (i = 0; i < EXP_TABLE_SIZE; i++) {
467 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
468 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
469 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100470 window_sums = malloc(sizeof(float) * (window+1) * 2);
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100471
472 return 0;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200473}
474
Marc Kupietza2e64502016-04-27 09:53:51 +0200475long mergeVectors(char *file_name){
476 FILE *f, *binvecs, *binwords;
477 int binwords_fd, binvecs_fd, net_fd, i;
478 long long a, b, c, d, cn;
479 float len;
480 float *merge_vecs;
481 char *merge_vocab;
Marc Kupietza5f60042017-05-04 10:38:12 +0200482 /* long long merge_words, merge_size; */
483 long long merge_size;
Marc Kupietza2e64502016-04-27 09:53:51 +0200484
485 char binvecs_fname[256], binwords_fname[256];
486 strcpy(binwords_fname, file_name);
487 strcat(binwords_fname, ".words");
488 strcpy(binvecs_fname, file_name);
489 strcat(binvecs_fname, ".vecs");
490
491 f = fopen(file_name, "rb");
492 if (f == NULL) {
493 printf("Input file %s not found\n", file_name);
494 exit -1;
495 }
496 fscanf(f, "%lld", &merge_words);
497 fscanf(f, "%lld", &merge_size);
498 if(merge_size != size){
499 fprintf(stderr, "vectors must have the same length\n");
500 exit(-1);
501 }
502 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
503 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
504 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
505 if (merge_vecs == NULL || merge_vocab == NULL) {
506 close(binvecs_fd);
507 close(binwords_fd);
508 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
509 exit(-1);
510 }
511 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
512 read(binwords_fd, merge_vocab, merge_words * max_w);
513 } else {
514 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
515 exit(-1);
516 }
517 printf("Successfully reallocated memory\nMerging...\n");
518 fflush(stdout);
519 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
520 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
521 munmap(M, words * size * sizeof(float));
522 munmap(vocab, words * max_w);
523 M = merge_vecs;
524 vocab = merge_vocab;
525 merged_end = merge_words;
526 words += merge_words;
527 fclose(f);
528 printf("merged_end: %lld, words: %lld\n", merged_end, words);
529 return((long) merged_end);
530}
531
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200532void filter_garbage() {
533 long i;
Marc Kupietzab591a82016-04-28 14:08:49 +0200534 unsigned char *w, previous, c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200535 garbage = malloc(words);
536 memset(garbage, 0, words);
537 for (i = 0; i < words; i++) {
538 w = vocab + i * max_w;
539 previous = 0;
Marc Kupietz969adfa2018-03-12 09:43:53 +0100540 if(strncmp("quot", w, 4) == 0) {
541 garbage[i]=1;
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100542// printf("Gargabe: %s\n", vocab + i * max_w);
Marc Kupietz969adfa2018-03-12 09:43:53 +0100543 } else {
544 while((c = *w++) && !garbage[i]) {
545 if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
546 (previous == '-' && (c & 32)) ||
547 (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) ||
548 (previous == 'q' && c == 'u' && *(w) == 'o' && *(w+1) == 't') || /* quot */
549 c == '<'
550 ) {
551 garbage[i]=1;
552 continue;
553 }
554 previous = c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200555 }
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200556 }
557 }
558 return;
559}
560
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100561void *getCollocators(void *args) {
562 knnpars *pars = args;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100563 int N = pars->N;
564 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100565 knn *nbs = NULL;
566 long window_layer_size = size * window * 2;
567 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
568 float f, max_f, maxmax_f;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100569 float *target_sums, worstbest, wpos_sum;
570 collocator *best;
Marc Kupietzd5642582016-03-19 22:23:13 +0100571
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200572 if(M2 == NULL || cc == -1)
Marc Kupietzd5642582016-03-19 22:23:13 +0100573 return NULL;
574
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100575 a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100576 best = malloc(N * sizeof(collocator));
Marc Kupietz271e2a42016-03-22 11:37:43 +0100577 worstbest = MIN_RESP;
578
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100579 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100580 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100581 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100582 best[b].wordi = -1;
583 best[b].probability = 1;
584 best[b].activation = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100585 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100586
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100587 d = cc;
588 maxmax_f = -1;
589 maxmax_target = 0;
590
Marc Kupietz271e2a42016-03-22 11:37:43 +0100591 for (a = pars->from; a < pars->upto; a++) {
592 if(a >= window)
593 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100594 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100595 printf("window pos: %ld\n", a);
596 if (a != window) {
597 max_f = -1;
598 window_offset = a * size;
599 if (a > window)
600 window_offset -= size;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100601 for(target = 0; target < pars->cutoff; target ++) {
602 if(garbage && garbage[target]) continue;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100603 if(target == d)
604 continue;
605 f = 0;
606 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100607 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100608 if (f < -MAX_EXP)
609 continue;
610 else if (f > MAX_EXP)
611 continue;
612 else
613 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100614 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100615
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100616 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100617 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100618 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100619 if (f > best[b].activation) {
620 memmove(best + b + 1, best + b, (N - b -1) * sizeof(collocator));
621 best[b].activation = f;
622 best[b].wordi = target;
623 best[b].position = window-a;
Marc Kupietz33679a32016-03-22 08:49:39 +0100624 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100625 }
626 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100627 if(b == N - 1)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100628 worstbest = best[N-1].activation;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100629 }
630 }
631 printf("%d %.2f\n", max_target, max_f);
632 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
633 if(max_f > maxmax_f) {
634 maxmax_f = max_f;
635 maxmax_target = max_target;
636 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100637 for (b = 0; b < N; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100638 if(best[b].position == window-a)
Marc Kupietza77acce2017-11-30 16:59:07 +0100639 best[b].cprobability = best[b].activation / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100640 } else {
641 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
642 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100643 pars->window_sums[a] = wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100644 }
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100645 for (b = 0; b < pars->cutoff; b++)
Marc Kupietza77acce2017-11-30 16:59:07 +0100646 pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2);
647 printf("Target-Summe von 0: %f\n", pars->target_sums[150298]);
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100648 free(target_sums);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100649 for(b=0; b<N && best[b].wordi >= 0; b++);; // THIS LOOP IS NEEDED (b...)
650// 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 +0100651// printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100652 nbs = malloc(sizeof(knn));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100653 nbs->best = best;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100654 nbs->length = b-1;
655 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100656}
657
Marc Kupietz30ca4342017-11-22 21:21:20 +0100658
Marc Kupietz66bfd952017-12-11 09:59:45 +0100659AV *getVecs(AV *array) {
660 int i, b;
661 AV *result = newAV();
662 for (i=0; i<=av_len(array); i++) {
663 SV** elem = av_fetch(array, i, 0);
664 if (elem != NULL) {
665 long j = (long) SvNV(*elem);
666 AV *vector = newAV();
667 for (b = 0; b < size; b++) {
668 av_push(vector, newSVnv(M[b + j * size]));
669 }
670 av_push(result, newRV_noinc(vector));
671 }
672 }
673 return result;
674}
675
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100676char *getSimilarProfiles(long node) {
677 int i;
678 char buffer[120000];
679 char pair_buffer[2048];
680 buffer[0]='[';
681 buffer[1]=0;
682 if(node >= sprofiles_qty) {
683 printf("Not available in precomputed profile\n");
684 return(strdup("[{\"w\":\"not available\", \"v\":0}]\n"));
685 }
686
687 printf("******* %s ******\n", &vocab[max_w * node]);
688
689 for(i=0; i < 100 && i < sprofiles[node].len; i++) {
690 sprintf(pair_buffer, "{\"w\":\"%s\", \"v\":%f},", &vocab[max_w * (sprofiles[node].nbr[i].index)], sprofiles[node].nbr[i].value);
691 strcat(buffer, pair_buffer);
692 }
693 buffer[strlen(buffer)-1]=']';
694 strcat(buffer, "\n");
695 printf(buffer);
696 return(strdup(buffer));
697}
698
Marc Kupietze243efd2018-01-11 22:19:24 +0100699char *getClassicCollocators(long node) {
700 char *res = (cdb? strdup(get_collocators_as_json(cdb, node)) : "[]");
701 return res;
702}
703
Marc Kupietza2e64502016-04-27 09:53:51 +0200704wordlist *getTargetWords(char *st1, int search_backw) {
Marc Kupietz48c29682016-03-19 11:30:43 +0100705 wordlist *wl = malloc(sizeof(wordlist));
706 char st[100][max_size], sep[100];
707 long a, b=0, c=0, cn=0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200708 int unmerged;
709
Marc Kupietzdc22b982015-10-09 09:19:34 +0200710 while (1) {
711 st[cn][b] = st1[c];
712 b++;
713 c++;
714 st[cn][b] = 0;
715 if (st1[c] == 0) break;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100716 if (st1[c] == ' ' || st1[c] == '-') {
717 sep[cn++] = st1[c];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200718 b = 0;
719 c++;
720 }
721 }
722 cn++;
723 for (a = 0; a < cn; a++) {
Marc Kupietza2e64502016-04-27 09:53:51 +0200724 if(search_backw) {
725 for (b = words - 1; b >= 0; b--) if (!strcmp(&vocab[b * max_w], st[a])) break;
726 } else {
727 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
728 }
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100729 if (b == words) b = -1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100730 wl->wordi[a] = b;
731 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], wl->wordi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200732 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100733 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100734 cn--;
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100735 free(wl);
736 return NULL;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200737 }
738 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100739 wl->length=cn;
740 return(wl);
741}
742
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100743void *_get_neighbours(void *arg) {
744 knnpars *pars = arg;
Marc Kupietz48c29682016-03-19 11:30:43 +0100745 char *st1 = pars->token;
746 int N = pars->N;
747 long from = pars -> from;
748 unsigned long upto = pars -> upto;
749 char file_name[max_size], st[100][max_size], *sep;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100750 float dist, len, vec[max_size];
751 long long a, b, c, d, cn, *bi;
Marc Kupietz48c29682016-03-19 11:30:43 +0100752 char ch;
753 knn *nbs = NULL;
754 wordlist *wl = pars->wl;
755
Marc Kupietzc55c8872017-12-01 23:27:10 +0100756 collocator *best = pars->best;
Marc Kupietz48c29682016-03-19 11:30:43 +0100757
758 float worstbest=-1;
759
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100760 for (a = 0; a < N; a++) best[a].activation = 0;
Marc Kupietz48c29682016-03-19 11:30:43 +0100761 a = 0;
762 bi = wl->wordi;
763 cn = wl->length;
764 sep = wl->sep;
765 b = bi[0];
766 c = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100767 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100768 N = 0;
769 goto end;
770 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200771 for (a = 0; a < size; a++) vec[a] = 0;
772 for (b = 0; b < cn; b++) {
773 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100774 if(b>0 && sep[b-1] == '-')
775 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
776 else
777 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200778 }
779 len = 0;
780 for (a = 0; a < size; a++) len += vec[a] * vec[a];
781 len = sqrt(len);
782 for (a = 0; a < size; a++) vec[a] /= len;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100783 for (a = 0; a < N; a++) best[a].activation = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100784 for (c = from; c < upto; c++) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200785 if(garbage && garbage[c]) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200786 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100787// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100788// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
789// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200790 dist = 0;
791 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100792 if(dist > worstbest) {
793 for (a = 0; a < N; a++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100794 if (dist > best[a].activation) {
795 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
796 best[a].activation = dist;
797 best[a].wordi = c;
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100798 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200799 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200800 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100801 worstbest = best[N-1].activation;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200802 }
803 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100804
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100805end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100806 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200807}
808
Marc Kupietze0e42132017-11-24 16:44:34 +0100809int cmp_activation (const void * a, const void * b) {
810 float fb = ((collocator *)a)->activation;
811 float fa = ((collocator *)b)->activation;
812 return (fa > fb) - (fa < fb);
813}
814
815int cmp_probability (const void * a, const void * b) {
816 float fb = ((collocator *)a)->probability;
817 float fa = ((collocator *)b)->probability;
818 return (fa > fb) - (fa < fb);
819}
820
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100821
Marc Kupietzd91212f2017-11-13 10:05:09 +0100822SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100823 HV *result = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100824 float *target_sums, vec[max_size];
Marc Kupietza5f60042017-05-04 10:38:12 +0200825 long long old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100826 long a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100827 knn *para_nbs[MAX_THREADS];
828 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100829 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100830 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100831 wordlist *wl;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200832 int syn_threads = (M2? window * 2 : 0);
833 int para_threads = num_threads - syn_threads;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100834
Marc Kupietzc55c8872017-12-01 23:27:10 +0100835 collocator *best;
836 posix_memalign((void **) &best, 128, 10 * N * sizeof(collocator));
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100837 memset(best, 0, 10 * N * sizeof(collocator));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100838
Marc Kupietz000ad862016-02-26 14:59:12 +0100839 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
840
Marc Kupietzd5d05732018-01-30 11:57:35 +0100841 if(cutoff < 1 || cutoff > words)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100842 cutoff=words;
843
Marc Kupietza2e64502016-04-27 09:53:51 +0200844 wl = getTargetWords(st1, search_backw);
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100845 if(wl == NULL || wl->length < 1)
Marc Kupietz271e2a42016-03-22 11:37:43 +0100846 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100847
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100848 old_words = cutoff;
Marc Kupietza5f60042017-05-04 10:38:12 +0200849 if(merge_words > 0)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100850 cutoff = merge_words * 1.25; /* HACK */
851 slice = cutoff / para_threads;
Marc Kupietza5f60042017-05-04 10:38:12 +0200852
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100853 a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float));
854 for(a = 0; a < cutoff; a++)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100855 target_sums[a] = 0;
856
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200857 printf("Starting %d threads\n", para_threads);
858 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100859 for(a=0; a < para_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100860 pars[a].cutoff = cutoff;
Marc Kupietz000ad862016-02-26 14:59:12 +0100861 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100862 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100863 pars[a].N = N;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100864 pars[a].best = &best[N*a];
Marc Kupietz000ad862016-02-26 14:59:12 +0100865 pars[a].from = a*slice;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100866 pars[a].upto = ((a+1)*slice > cutoff? cutoff:(a+1)*slice);
Marc Kupietz000ad862016-02-26 14:59:12 +0100867 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
868 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200869 if(M2) {
870 for(a=0; a < syn_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100871 pars[a + para_threads].cutoff = cutoff;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200872 pars[a + para_threads].target_sums = target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100873 pars[a + para_threads].window_sums = window_sums;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200874 pars[a + para_threads].wl = wl;
875 pars[a + para_threads].N = N;
876 pars[a + para_threads].from = a;
877 pars[a + para_threads].upto = a+1;
878 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
879 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100880 }
881 printf("Waiting for para threads to join\n");
882 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100883 for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *) &para_nbs[a]);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100884 printf("Para threads joint\n");
885 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +0100886
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200887 /* if(!syn_nbs[0]) */
888 /* goto end; */
Marc Kupietz000ad862016-02-26 14:59:12 +0100889
Marc Kupietzc55c8872017-12-01 23:27:10 +0100890 qsort(best, N*para_threads, sizeof(collocator), cmp_activation);
Marc Kupietz000ad862016-02-26 14:59:12 +0100891
Marc Kupietz000ad862016-02-26 14:59:12 +0100892
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100893 long long chosen[MAX_NEIGHBOURS];
894 printf("N: %ld\n", N);
895
Marc Kupietz271e2a42016-03-22 11:37:43 +0100896 AV* array = newAV();
Marc Kupietzd91212f2017-11-13 10:05:09 +0100897 int i, j;
Marc Kupietza5f60042017-05-04 10:38:12 +0200898 int l1_words=0, l2_words=0;
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100899
900 for (a = 0, i = 0; i < N && a < N*para_threads; a++) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100901 int filtered=0;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100902 long long c = best[a].wordi;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100903 if (dedupe && i > 0) {
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100904 for (j=0; j<i && !filtered; j++)
905 if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) ||
906 strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) {
907 printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]);
Marc Kupietzd91212f2017-11-13 10:05:09 +0100908 filtered = 1;
909 }
910 if(filtered)
911 continue;
912 }
Marc Kupietza5f60042017-05-04 10:38:12 +0200913 if(merge_words > 0) {
914 if(c >= merge_words) {
915 if(l1_words > N / 2)
916 continue;
917 else
918 l1_words++;
919 } else {
920 if(l2_words > N / 2)
921 continue;
922 else
923 l2_words++;
924 }
925 }
Marc Kupietza5f60042017-05-04 10:38:12 +0200926 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 +0100927 fflush(stdout);
Marc Kupietza5f60042017-05-04 10:38:12 +0200928
Marc Kupietz271e2a42016-03-22 11:37:43 +0100929 HV* hash = newHV();
Marc Kupietza5f60042017-05-04 10:38:12 +0200930 SV* word = newSVpvf(&vocab[c * max_w], 0);
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100931 chosen[i] = c;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100932 if(latin_enc == 0) SvUTF8_on(word);
Marc Kupietza5f60042017-05-04 10:38:12 +0200933 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100934 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100935 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
936 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100937 AV *vector = newAV();
938 for (b = 0; b < size; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100939 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100940 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100941 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
942 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietza5f60042017-05-04 10:38:12 +0200943 i++;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100944 }
945 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
946
Marc Kupietz50485ba2016-03-23 09:13:14 +0100947 for(b=0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100948 best[b].wordi = -1L;
949 best[b].activation = 0;
950 best[b].probability = 0;
951 best[b].position = 0;
952 best[b].activation_sum = 0;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100953 }
954
Marc Kupietza77acce2017-11-30 16:59:07 +0100955 float total_activation = 0;
956
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200957 if (M2) {
958 printf("Waiting for syn threads to join\n");
959 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100960 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], (void *) &syn_nbs[a]);
Marc Kupietza77acce2017-11-30 16:59:07 +0100961 for (a = 0; a <= syn_threads; a++) {
Marc Kupietz63872182018-03-12 09:44:49 +0100962 if(a == window) continue;
Marc Kupietza77acce2017-11-30 16:59:07 +0100963 total_activation += window_sums[a];
964 printf("window pos: %d, sum: %f\n", a, window_sums[a]);
965 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200966 printf("syn threads joint\n");
967 fflush(stdout);
Marc Kupietz50485ba2016-03-23 09:13:14 +0100968
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200969 for(b=0; b < syn_nbs[0]->length; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100970 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
971 best[b].position = -1; // syn_nbs[0]->pos[b];
972 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100973 best[b].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +0100974 best[b].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100975 best[b].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +0100976 best[b].cprobability = syn_nbs[0]->best[b].cprobability;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200977 }
Marc Kupietz30ca4342017-11-22 21:21:20 +0100978
979 float best_window_sum[MAX_NEIGHBOURS];
980 int found_index=0, i=0, j, w;
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100981 for(a=0; a < syn_threads; a++) {
982 for(b=0; b < syn_nbs[a]->length; b++) {
983 for(i=0; i < found_index; i++)
984 if(best[i].wordi == syn_nbs[a]->best[b].wordi)
985 break;
986 if(i >= found_index) {
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100987 best[found_index].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +0100988 best[found_index].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100989 best[found_index].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +0100990 best[found_index].cprobability = syn_nbs[a]->best[b].cprobability;
991 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 +0100992 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
993 // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
Marc Kupietz30ca4342017-11-22 21:21:20 +0100994 }
995 }
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100996 }
Marc Kupietza77acce2017-11-30 16:59:07 +0100997 sort_by =0; // ALWAYS AUTO-FOCUS
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100998 if(sort_by != 1 && sort_by != 2) { // sort by auto focus mean
Marc Kupietz30ca4342017-11-22 21:21:20 +0100999 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) -1);
Marc Kupietzbd41da32017-11-24 10:26:43 +01001000 int wpos;
Marc Kupietz4116b432017-12-06 14:15:32 +01001001 int bits_set = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001002 for(i=0; i < found_index; i++) {
Marc Kupietz4116b432017-12-06 14:15:32 +01001003 best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001004 for(w=1; w < (1 << syn_threads); w++) { // loop through all possible windows
Marc Kupietz4116b432017-12-06 14:15:32 +01001005 float word_window_sum = 0, word_window_average=0, word_cprobability_sum=0, word_activation_sum = 0, total_window_sum = 0;
1006 bits_set = 0;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001007 for(a=0; a < syn_threads; a++) {
Marc Kupietz30ca4342017-11-22 21:21:20 +01001008 if((1 << a) & w) {
Marc Kupietzbd41da32017-11-24 10:26:43 +01001009 wpos = (a >= window? a+1 : a);
1010 total_window_sum += window_sums[wpos];
Marc Kupietz30ca4342017-11-22 21:21:20 +01001011 }
1012 }
Marc Kupietzbd41da32017-11-24 10:26:43 +01001013// printf("%d window-sum %f\n", w, total_window_sum);
1014 for(a=0; a < syn_threads; a++) {
1015 if((1 << a) & w) {
1016 wpos = (a >= window? a+1 : a);
1017 bits_set++;
1018 for(b=0; b < syn_nbs[a]->length; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001019 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001020// float acti = syn_nbs[a]->best[b].activation / total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001021// 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 +01001022// word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1023// 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 +01001024
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001025 word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
Marc Kupietza77acce2017-11-30 16:59:07 +01001026// word_window_sum += acti - (word_window_sum * acti); syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1027
Marc Kupietz4116b432017-12-06 14:15:32 +01001028 word_window_average += syn_nbs[a]->best[b].activation; // - word_window_average * syn_nbs[a]->best[b].activation; // conormalied activation sum
1029 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 +01001030 word_activation_sum += syn_nbs[a]->best[b].activation;
1031 if(syn_nbs[a]->best[b].activation > best[i].max_activation)
1032 best[i].max_activation = syn_nbs[a]->best[b].activation;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +01001033 if(syn_nbs[a]->best[b].activation > best[i].heat[wpos] )
Marc Kupietz728b8ed2017-12-02 11:13:49 +01001034 best[i].heat[wpos] = syn_nbs[a]->best[b].activation;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001035 }
1036 }
1037 }
Marc Kupietz4116b432017-12-06 14:15:32 +01001038 if(bits_set) {
1039 word_window_average /= bits_set;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001040// word_activation_sum /= bits_set;
1041// word_window_sum /= bits_set;
Marc Kupietz4116b432017-12-06 14:15:32 +01001042 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001043
1044 word_window_sum /= total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001045
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001046 if(word_window_sum > best[i].probability) {
Marc Kupietz4116b432017-12-06 14:15:32 +01001047// best[i].position = w;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001048 best[i].probability = word_window_sum;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001049 }
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001050
Marc Kupietz4116b432017-12-06 14:15:32 +01001051 if(word_cprobability_sum > best[i].cprobability_sum) {
1052 best[i].position = w;
1053 best[i].cprobability_sum = word_cprobability_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001054 }
Marc Kupietz4116b432017-12-06 14:15:32 +01001055
1056 best[i].average = word_window_average;
1057// best[i].activation = word_activation_sum;
1058 }
Marc Kupietz30ca4342017-11-22 21:21:20 +01001059 }
Marc Kupietze0e42132017-11-24 16:44:34 +01001060 qsort(best, found_index, sizeof(collocator), cmp_probability);
Marc Kupietz30ca4342017-11-22 21:21:20 +01001061// for(i=0; i < found_index; i++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001062// 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 +01001063// }
1064
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001065 } else if(sort_by == 1) { // responsiveness any window position
1066 int wpos;
1067 for(i=0; i < found_index; i++) {
1068 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
1069 for(a=0; a < syn_threads; a++) {
1070 wpos = (a >= window? a+1 : a);
1071 for(b=0; b < syn_nbs[a]->length; b++)
1072 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
1073 best[i].probability += syn_nbs[a]->best[b].probability;
1074 if(syn_nbs[a]->best[b].activation > 0.25)
1075 best[i].position |= 1 << wpos;
1076 if(syn_nbs[a]->best[b].activation > best[i].activation) {
1077 best[i].activation = syn_nbs[a]->best[b].activation;
1078 }
1079 }
1080 }
1081 }
1082 qsort(best, found_index, sizeof(collocator), cmp_activation);
1083 } else if(sort_by == 2) { // single window position
Marc Kupietz30ca4342017-11-22 21:21:20 +01001084 for(a=1; a < syn_threads; a++) {
1085 for(b=0; b < syn_nbs[a]->length; b++) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001086 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001087 if(syn_nbs[a]->best[b].activation > best[c].activation) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001088 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001089 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001090 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001091 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
1092 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 +02001093 break;
Marc Kupietz6d9a6782016-03-23 17:25:25 +01001094 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001095 }
1096 }
1097 }
1098 } else { // sort by mean p
1099 for(a=1; a < syn_threads; a++) {
1100 for(b=0; b < syn_nbs[a]->length; b++) {
1101 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001102 if(target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001103 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001104 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001105 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001106 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
1107 best[c].position = (1 << 2*window) - 1; // syn_nbs[a]->pos[b];
1108 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001109 break;
1110 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001111 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +01001112 }
1113 }
1114 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001115 array = newAV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001116 for (a = 0, i=0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
1117 long long c = best[a].wordi;
Marc Kupietza77acce2017-11-30 16:59:07 +01001118/*
Marc Kupietzd91212f2017-11-13 10:05:09 +01001119 if (dedupe) {
1120 int filtered=0;
1121 for (j=0; j<i; j++)
1122 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
1123 strcasestr(chosen[j], &vocab[c * max_w])) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001124 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
1125 filtered = 1;
1126 }
Marc Kupietzd91212f2017-11-13 10:05:09 +01001127 if(filtered)
1128 continue;
1129 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001130*/
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001131 chosen[i++]=c;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001132 HV* hash = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001133 SV* word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001134 AV* heat = newAV();
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001135 if(latin_enc == 0) SvUTF8_on(word);
1136 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001137 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001138 hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001139 hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001140 hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001141 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 +01001142 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 +01001143 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001144 best[a].heat[5]=0;
1145 for(i=10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i]));
1146 hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV*)heat), 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001147 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietz271e2a42016-03-22 11:37:43 +01001148 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001149 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001150 }
Marc Kupietz000ad862016-02-26 14:59:12 +01001151end:
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001152 words = old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001153 free(best);
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001154 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +01001155}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +01001156
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001157int dump_vecs(char *fname) {
1158 long i, j;
1159 FILE *f;
1160 /* if(words>200000) */
1161 /* words=200000; */
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001162
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001163 if((f=fopen(fname, "w")) == NULL) {
1164 fprintf(stderr, "cannot open %s for writing\n", fname);
1165 return(-1);
1166 }
1167 fprintf(f, "%lld %lld\n", words, size);
1168 for (i=0; i < words; i++) {
1169 fprintf(f, "%s ", &vocab[i * max_w]);
1170 for(j=0; j < size - 1; j++)
1171 fprintf(f, "%f ", M[i*size + j]);
1172 fprintf(f, "%f\n", M[i*size + j]);
1173 }
1174 fclose(f);
1175 return(0);
1176}
Marc Kupietzdc22b982015-10-09 09:19:34 +02001177
Marc Kupietzfa194262018-06-05 09:39:32 +02001178int dump_for_numpy(char *fname) {
1179 long i, j;
1180 FILE *f;
1181 /* if(words>200000) */
1182 /* words=200000; */
1183
1184 if((f=fopen(fname, "w")) == NULL) {
1185 fprintf(stderr, "cannot open %s for writing\n", fname);
1186 return(-1);
1187 }
1188 for (i=0; i < words; i++) {
1189 for(j=0; j < size - 1; j++)
1190 fprintf(f, "%f\t", M[i*size + j]);
1191 fprintf(f, "%f\n", M[i*size + j]);
1192 }
1193 fclose(f);
1194 return(0);
1195}