blob: b1e542f8c9fba8f82487a44afddc806d0c8294e1 [file] [log] [blame]
Marc Kupietzdc22b982015-10-09 09:19:34 +02001#!/usr/local/bin/perl
2use Inline C;
Marc Kupietze243efd2018-01-11 22:19:24 +01003use Inline C => Config => CLEAN_AFTER_BUILD => 0, BUILD_NOISY => 1, ccflags => $Config{ccflags}." -I/vol/work/kupietz/Work2/kl/trunk/CollocatorDB -L/vol/work/kupietz/Work2/kl/trunk/CollocatorDB -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 Kupietz5c3887d2016-04-28 08:53:35 +020027our $opt_G;
Marc Kupietza5b90152016-03-15 17:39:19 +010028
Marc Kupietz6ed81872016-04-27 14:04:04 +020029my %marked;
Marc Kupietz793413b2016-04-02 21:48:57 +020030my $training_args="";
Marc Kupietza2e64502016-04-27 09:53:51 +020031my $mergedEnd=0;
Marc Kupietz15987412017-11-07 15:56:58 +010032my %cache;
Marc Kupietz19c68242018-03-12 09:42:21 +010033my %cccache; # classic collocator cache
Marc Kupietza51dcfa2018-03-19 16:22:05 +010034my %spcache; # similar profile cache
Marc Kupietz793413b2016-04-02 21:48:57 +020035
Marc Kupietza5f60042017-05-04 10:38:12 +020036getopts('d:Gil:p:m:n:M:');
Marc Kupietz6ed81872016-04-27 14:04:04 +020037
38if($opt_M) {
Marc Kupietzed930212016-04-27 15:42:38 +020039 open my $handle, '<:encoding(UTF-8)', $opt_M
40 or die "Can't open '$opt_M' for reading: $!";
41 while(<$handle>) {
Marc Kupietz6ed81872016-04-27 14:04:04 +020042 foreach my $mw (split /\s+/) {
43 $marked{$mw}=1
44 }
45 }
Marc Kupietzed930212016-04-27 15:42:38 +020046 close($handle);
Marc Kupietz6ed81872016-04-27 14:04:04 +020047}
Marc Kupietza5b90152016-03-15 17:39:19 +010048
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010049# -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 +010050if(!$ARGV[0]) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010051 init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0));
Marc Kupietz2cb667e2016-03-10 09:44:12 +010052} else {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010053 init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0));
Marc Kupietz793413b2016-04-02 21:48:57 +020054 if(open(FILE, "$ARGV[0].args")) {
55 $training_args = <FILE>;
56 }
57 close(FILE);
Marc Kupietz2cb667e2016-03-10 09:44:12 +010058}
Marc Kupietzdc22b982015-10-09 09:19:34 +020059
Marc Kupietza51dcfa2018-03-19 16:22:05 +010060my $have_sprofiles = load_sprofiles($ARGV[0]);
61
Marc Kupietza2e64502016-04-27 09:53:51 +020062if($opt_m) {
63 $mergedEnd = mergeVectors($opt_m);
64}
65
Marc Kupietz6ed81872016-04-27 14:04:04 +020066
Marc Kupietz43ee87e2016-04-25 10:50:08 +020067if($opt_d) { # -d: dump vecs and exit
68 dump_vecs($opt_d);
69 exit;
70}
71
Marc Kupietza5b90152016-03-15 17:39:19 +010072my $daemon = Mojo::Server::Daemon->new(
73 app => app,
74 listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"]
75);
76
Marc Kupietz5c3887d2016-04-28 08:53:35 +020077if($opt_G) {
78 print "Filtering garbage\n";
79 filter_garbage();
80}
81
Marc Kupietz554aff52017-11-09 14:42:09 +010082get '*/js/*' => sub {
Marc Kupietzffef9302017-11-07 15:58:01 +010083 my $c = shift;
84 my $url = $c->req->url;
85 $url =~ s@/derekovecs@@g;
86 $c->app->log->info("GET: " . $url);
87 $c->reply->static($url);
88};
89
Marc Kupietza9270572018-03-17 15:17:07 +010090get '*/css/*' => sub {
91 my $c = shift;
92 my $url = $c->req->url;
93 $url =~ s@/derekovecs/@/@g;
94 $c->app->log->info("GET: " . $url);
95 $c->reply->static($url);
96};
97
Marc Kupietz19c68242018-03-12 09:42:21 +010098sub getClassicCollocatorsCached {
99 my ($c, $word) = @_;
100 if(!$cccache{$word}) {
101 $cccache{$word} = getClassicCollocators($word);
102 } else {
103 $c->app->log->info("Getting classic collocators for $word from cache.");
104 }
105 return $cccache{$word};
106}
107
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100108sub getSimilarProfilesCached {
109 my ($c, $word) = @_;
110 if(!$spcache{$word}) {
111 $spcache{$word} = getSimilarProfiles($word);
112 } else {
113 $c->app->log->info("Getting similar profiles for $word from cache:");
114 print $spcache{$word};
115 }
116 return $spcache{$word};
117}
118
Marc Kupietz66bfd952017-12-11 09:59:45 +0100119post '/derekovecs/getVecsByRanks' => sub {
120 my $self = shift;
121 my $vec = getVecs($self->req->json);
122 $self->render(json => $vec);
123};
124
Marc Kupietze13a3552018-01-25 08:48:34 +0100125any '*/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100126 my $self = shift;
Marc Kupietz19c68242018-03-12 09:42:21 +0100127 $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100128};
129
Marc Kupietze13a3552018-01-25 08:48:34 +0100130any '/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100131 my $self = shift;
Marc Kupietz19c68242018-03-12 09:42:21 +0100132 $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100133};
134
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100135any '*/getSimilarProfiles' => sub {
136 my $self = shift;
137 $self->render(data => getSimilarProfilesCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
138};
139
Marc Kupietzdf3d4b52017-11-29 16:57:27 +0100140get '*/img/*' => sub {
141 my $c = shift;
142 my $url = $c->req->url;
143 $url =~ s@/derekovecs@@g;
144 $c->app->log->info("GET: " . $url);
145 $c->reply->static($url);
146};
147
Marc Kupietzdc22b982015-10-09 09:19:34 +0200148get '/' => sub {
149 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +0200150 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200151 my $word=$c->param('word');
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100152 my $no_nbs=$c->param('n') || 100;
153 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +0100154 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100155 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100156 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200157 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100158 my $sort=$c->param('sort') || 0;
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100159 my $csv=$c->param('csv') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +0200160 my $json=$c->param('json') || 0;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100161 my $cutoff=$c->param('cutoff') || 500000;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100162 my $dedupe=$c->param('dedupe') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100163 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100164 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100165 my @collocations;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100166 if(defined($word) && $word !~ /^\s*$/) {
167 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100168 $word =~ s/\s+/ /g;
169 for my $w (split(' *\| *', $word)) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100170 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100171 $c->app->log->info("Getting $w results from cache");
Marc Kupietzd91212f2017-11-13 10:05:09 +0100172 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe}
Marc Kupietza5b90152016-03-15 17:39:19 +0100173 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100174 $c->app->log->info('Looking for neighbours of '.$w);
175 if($opt_i) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100176 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe);
Marc Kupietz15987412017-11-07 15:56:58 +0100177 } else {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100178 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe);
Marc Kupietz15987412017-11-07 15:56:58 +0100179 }
Marc Kupietz2dd2dd72017-12-01 22:08:14 +0100180 $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100181 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100182 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100183 }
184 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100185 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200186 if($json) {
187 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100188 } elsif($csv) {
189 my $csv_data="";
190 for (my $i=0; $i <= $no_nbs; $i++) {
191 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
192 }
193 for (my $i=0; $i < $no_nbs; $i++) {
194 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
195 }
196 chop $csv_data;
197 chop $csv_data;
198 $csv_data .= "\n";
199 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200200 } else {
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100201 $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 +0200202 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200203};
204
Marc Kupietz30ca4342017-11-22 21:21:20 +0100205helper(bitvec2window => sub {
206 my ($self, $n) = @_;
207 my $str = unpack("B32", pack("N", $n));
208 $str =~ s/^\d{22}//;
209 $str =~ s/^(\d{5})/$1x/;
210 $str =~ s/0/·/g;
211 $str =~ s/1/+/g;
212 return $str;
213 });
214
Marc Kupietza5b90152016-03-15 17:39:19 +0100215$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200216
217exit;
218
219__END__
220
221__C__
222#include <stdio.h>
223#include <string.h>
224#include <math.h>
225#include <malloc.h>
226#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +0100227#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +0100228#include <pthread.h>
Marc Kupietze243efd2018-01-11 22:19:24 +0100229#include <collocatordb.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200230
231#define max_size 2000
232#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100233#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100234#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +0100235#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100236#define MAX_CC 50
237#define EXP_TABLE_SIZE 1000
238#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +0100239#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +0200240
241//the thread function
242void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +0100243
244typedef struct {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100245 long long wordi;
246 long position;
247 float activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100248 float average;
Marc Kupietza77acce2017-11-30 16:59:07 +0100249 float cprobability; // column wise probability
Marc Kupietz4116b432017-12-06 14:15:32 +0100250 float cprobability_sum;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100251 float probability;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100252 float activation_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100253 float max_activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100254 float heat[16];
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100255} collocator;
256
257typedef struct {
258 collocator *best;
Marc Kupietz80abb442016-03-23 21:04:08 +0100259 int length;
Marc Kupietz000ad862016-02-26 14:59:12 +0100260} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100261
Marc Kupietz000ad862016-02-26 14:59:12 +0100262typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +0100263 long long wordi[MAX_NEIGHBOURS];
264 char sep[MAX_NEIGHBOURS];
265 int length;
266} wordlist;
267
268typedef struct {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100269 long cutoff;
Marc Kupietz48c29682016-03-19 11:30:43 +0100270 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100271 char *token;
272 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100273 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100274 unsigned long upto;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100275 collocator *best;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100276 float *target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100277 float *window_sums;
Marc Kupietz000ad862016-02-26 14:59:12 +0100278} knnpars;
279
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100280typedef struct {
281 uint32_t index;
282 float value;
283} sparse_t;
284
285typedef struct {
286 uint32_t len;
287 sparse_t nbr[100];
288} profile_t;
289
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200290float *M, *M2=0L, *syn1neg_window, *expTable;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100291float *window_sums;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200292char *vocab;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200293char *garbage = NULL;
Marc Kupietze243efd2018-01-11 22:19:24 +0100294COLLOCATORDB *cdb = NULL;
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100295profile_t *sprofiles = NULL;
296size_t sprofiles_qty = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100297
Marc Kupietza2e64502016-04-27 09:53:51 +0200298long long words, size, merged_end;
Marc Kupietza5f60042017-05-04 10:38:12 +0200299long long merge_words = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100300int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100301int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100302int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200303
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100304/* load collocation profiles if file exists */
305int load_sprofiles(char *vecsname) {
306 char *basename = strdup(vecsname);
307 char *pos = strstr(basename, ".vecs");
308 if(pos)
309 *pos=0;
310
311 char binsprofiles_fname[256];
312 strcpy(binsprofiles_fname, basename);
313 strcat(binsprofiles_fname, ".sprofiles.bin");
314 FILE *fp = fopen(binsprofiles_fname, "rb");
315 if (fp == NULL) {
316 printf("Collocation profiles %s not found. No problem.\n", binsprofiles_fname);
317 return 0;
318 }
319 fseek(fp, 0L, SEEK_END);
320 size_t sz = ftell(fp);
321 fclose(fp);
322
323 int fd = open(binsprofiles_fname, O_RDONLY);
324 sprofiles = mmap(0, sz, PROT_READ, MAP_SHARED, fd, 0);
325 if (sprofiles == MAP_FAILED) {
326 close(fd);
327 fprintf(stderr, "Cannot mmap %s\n", binsprofiles_fname);
328 sprofiles = NULL;
329 return 0;
330 } else {
331 sprofiles_qty = sz / sizeof(profile_t);
332 fprintf(stderr, "Successfully mmaped %s containing similar profiles for %ld word forms.\n", binsprofiles_fname, sprofiles_qty);
333 }
334 return 1;
335}
336
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100337int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100338 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100339 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100340 long long a, b, c, d, cn;
341 float len;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200342 double val;
Marc Kupietz82b02672016-02-26 12:32:25 +0100343
Marc Kupietz67c20282016-02-26 09:42:00 +0100344 char binvecs_fname[256], binwords_fname[256];
345 strcpy(binwords_fname, file_name);
346 strcat(binwords_fname, ".words");
347 strcpy(binvecs_fname, file_name);
348 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200349
Marc Kupietza5b90152016-03-15 17:39:19 +0100350 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200351 f = fopen(file_name, "rb");
352 if (f == NULL) {
353 printf("Input file %s not found\n", file_name);
354 return -1;
355 }
356 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100357 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200358 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100359 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
360 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100361 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
362 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
363 if (M == NULL) {
364 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
365 return -1;
366 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200367 if(strstr(file_name, ".txt")) {
368 for (b = 0; b < words; b++) {
369 a = 0;
370 while (1) {
371 vocab[b * max_w + a] = fgetc(f);
372 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
373 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
374 }
375 vocab[b * max_w + a] = 0;
376 len = 0;
377 for (a = 0; a < size; a++) {
378 fscanf(f, "%lf", &val);
379 M[a + b * size] = val;
380 len += val * val;
381 }
382 len = sqrt(len);
383 for (a = 0; a < size; a++) M[a + b * size] /= len;
384 }
385 } else {
386 for (b = 0; b < words; b++) {
387 a = 0;
388 while (1) {
389 vocab[b * max_w + a] = fgetc(f);
390 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
391 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
392 }
393 vocab[b * max_w + a] = 0;
394 fread(&M[b * size], sizeof(float), size, f);
395 len = 0;
396 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
397 len = sqrt(len);
398 for (a = 0; a < size; a++) M[a + b * size] /= len;
399 }
400 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100401 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
402 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
403 fclose(binvecs);
404 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
405 fclose(binwords);
406 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100407 }
408 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
409 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
410 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
411 if (M == MAP_FAILED || vocab == MAP_FAILED) {
412 close(binvecs_fd);
413 close(binwords_fd);
414 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
415 exit(-1);
416 }
417 } else {
418 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
419 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100420 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200421 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100422
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200423 if(net_name && strlen(net_name) > 0) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100424 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
425 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
426 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100427 // munmap(M, sizeof(float) * words * size);
428 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 +0200429 if (M2 == MAP_FAILED) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100430 close(net_fd);
431 fprintf(stderr, "Cannot mmap %s\n", net_name);
432 exit(-1);
433 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100434 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100435 } else {
436 fprintf(stderr, "Cannot open %s\n", net_name);
437 exit(-1);
438 }
439 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
Marc Kupietze243efd2018-01-11 22:19:24 +0100440
441 char collocatordb_name[2048];
442 strcpy(collocatordb_name, net_name);
443 char *ext = rindex(collocatordb_name, '.');
444 if(ext) {
445 strcpy(ext, ".rocksdb");
446 if(access(collocatordb_name, R_OK) == 0) {
447 *ext = 0;
448 fprintf(stderr, "Opening collocator DB %s\n", collocatordb_name);
449 cdb = open_collocatordb(collocatordb_name);
450 }
451 }
452 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100453
454 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
455 for (i = 0; i < EXP_TABLE_SIZE; i++) {
456 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
457 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
458 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100459 window_sums = malloc(sizeof(float) * (window+1) * 2);
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100460
461 return 0;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200462}
463
Marc Kupietza2e64502016-04-27 09:53:51 +0200464long mergeVectors(char *file_name){
465 FILE *f, *binvecs, *binwords;
466 int binwords_fd, binvecs_fd, net_fd, i;
467 long long a, b, c, d, cn;
468 float len;
469 float *merge_vecs;
470 char *merge_vocab;
Marc Kupietza5f60042017-05-04 10:38:12 +0200471 /* long long merge_words, merge_size; */
472 long long merge_size;
Marc Kupietza2e64502016-04-27 09:53:51 +0200473
474 char binvecs_fname[256], binwords_fname[256];
475 strcpy(binwords_fname, file_name);
476 strcat(binwords_fname, ".words");
477 strcpy(binvecs_fname, file_name);
478 strcat(binvecs_fname, ".vecs");
479
480 f = fopen(file_name, "rb");
481 if (f == NULL) {
482 printf("Input file %s not found\n", file_name);
483 exit -1;
484 }
485 fscanf(f, "%lld", &merge_words);
486 fscanf(f, "%lld", &merge_size);
487 if(merge_size != size){
488 fprintf(stderr, "vectors must have the same length\n");
489 exit(-1);
490 }
491 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
492 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
493 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
494 if (merge_vecs == NULL || merge_vocab == NULL) {
495 close(binvecs_fd);
496 close(binwords_fd);
497 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
498 exit(-1);
499 }
500 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
501 read(binwords_fd, merge_vocab, merge_words * max_w);
502 } else {
503 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
504 exit(-1);
505 }
506 printf("Successfully reallocated memory\nMerging...\n");
507 fflush(stdout);
508 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
509 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
510 munmap(M, words * size * sizeof(float));
511 munmap(vocab, words * max_w);
512 M = merge_vecs;
513 vocab = merge_vocab;
514 merged_end = merge_words;
515 words += merge_words;
516 fclose(f);
517 printf("merged_end: %lld, words: %lld\n", merged_end, words);
518 return((long) merged_end);
519}
520
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200521void filter_garbage() {
522 long i;
Marc Kupietzab591a82016-04-28 14:08:49 +0200523 unsigned char *w, previous, c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200524 garbage = malloc(words);
525 memset(garbage, 0, words);
526 for (i = 0; i < words; i++) {
527 w = vocab + i * max_w;
528 previous = 0;
Marc Kupietz969adfa2018-03-12 09:43:53 +0100529 if(strncmp("quot", w, 4) == 0) {
530 garbage[i]=1;
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100531// printf("Gargabe: %s\n", vocab + i * max_w);
Marc Kupietz969adfa2018-03-12 09:43:53 +0100532 } else {
533 while((c = *w++) && !garbage[i]) {
534 if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
535 (previous == '-' && (c & 32)) ||
536 (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) ||
537 (previous == 'q' && c == 'u' && *(w) == 'o' && *(w+1) == 't') || /* quot */
538 c == '<'
539 ) {
540 garbage[i]=1;
541 continue;
542 }
543 previous = c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200544 }
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200545 }
546 }
547 return;
548}
549
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100550void *getCollocators(void *args) {
551 knnpars *pars = args;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100552 int N = pars->N;
553 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100554 knn *nbs = NULL;
555 long window_layer_size = size * window * 2;
556 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
557 float f, max_f, maxmax_f;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100558 float *target_sums, worstbest, wpos_sum;
559 collocator *best;
Marc Kupietzd5642582016-03-19 22:23:13 +0100560
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200561 if(M2 == NULL || cc == -1)
Marc Kupietzd5642582016-03-19 22:23:13 +0100562 return NULL;
563
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100564 a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100565 best = malloc(N * sizeof(collocator));
Marc Kupietz271e2a42016-03-22 11:37:43 +0100566 worstbest = MIN_RESP;
567
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100568 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100569 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100570 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100571 best[b].wordi = -1;
572 best[b].probability = 1;
573 best[b].activation = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100574 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100575
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100576 d = cc;
577 maxmax_f = -1;
578 maxmax_target = 0;
579
Marc Kupietz271e2a42016-03-22 11:37:43 +0100580 for (a = pars->from; a < pars->upto; a++) {
581 if(a >= window)
582 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100583 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100584 printf("window pos: %ld\n", a);
585 if (a != window) {
586 max_f = -1;
587 window_offset = a * size;
588 if (a > window)
589 window_offset -= size;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100590 for(target = 0; target < pars->cutoff; target ++) {
591 if(garbage && garbage[target]) continue;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100592 if(target == d)
593 continue;
594 f = 0;
595 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100596 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100597 if (f < -MAX_EXP)
598 continue;
599 else if (f > MAX_EXP)
600 continue;
601 else
602 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100603 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100604
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100605 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100606 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100607 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100608 if (f > best[b].activation) {
609 memmove(best + b + 1, best + b, (N - b -1) * sizeof(collocator));
610 best[b].activation = f;
611 best[b].wordi = target;
612 best[b].position = window-a;
Marc Kupietz33679a32016-03-22 08:49:39 +0100613 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100614 }
615 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100616 if(b == N - 1)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100617 worstbest = best[N-1].activation;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100618 }
619 }
620 printf("%d %.2f\n", max_target, max_f);
621 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
622 if(max_f > maxmax_f) {
623 maxmax_f = max_f;
624 maxmax_target = max_target;
625 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100626 for (b = 0; b < N; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100627 if(best[b].position == window-a)
Marc Kupietza77acce2017-11-30 16:59:07 +0100628 best[b].cprobability = best[b].activation / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100629 } else {
630 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
631 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100632 pars->window_sums[a] = wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100633 }
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100634 for (b = 0; b < pars->cutoff; b++)
Marc Kupietza77acce2017-11-30 16:59:07 +0100635 pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2);
636 printf("Target-Summe von 0: %f\n", pars->target_sums[150298]);
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100637 free(target_sums);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100638 for(b=0; b<N && best[b].wordi >= 0; b++);; // THIS LOOP IS NEEDED (b...)
639// 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 +0100640// printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100641 nbs = malloc(sizeof(knn));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100642 nbs->best = best;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100643 nbs->length = b-1;
644 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100645}
646
Marc Kupietz30ca4342017-11-22 21:21:20 +0100647
Marc Kupietz66bfd952017-12-11 09:59:45 +0100648AV *getVecs(AV *array) {
649 int i, b;
650 AV *result = newAV();
651 for (i=0; i<=av_len(array); i++) {
652 SV** elem = av_fetch(array, i, 0);
653 if (elem != NULL) {
654 long j = (long) SvNV(*elem);
655 AV *vector = newAV();
656 for (b = 0; b < size; b++) {
657 av_push(vector, newSVnv(M[b + j * size]));
658 }
659 av_push(result, newRV_noinc(vector));
660 }
661 }
662 return result;
663}
664
Marc Kupietza51dcfa2018-03-19 16:22:05 +0100665char *getSimilarProfiles(long node) {
666 int i;
667 char buffer[120000];
668 char pair_buffer[2048];
669 buffer[0]='[';
670 buffer[1]=0;
671 if(node >= sprofiles_qty) {
672 printf("Not available in precomputed profile\n");
673 return(strdup("[{\"w\":\"not available\", \"v\":0}]\n"));
674 }
675
676 printf("******* %s ******\n", &vocab[max_w * node]);
677
678 for(i=0; i < 100 && i < sprofiles[node].len; i++) {
679 sprintf(pair_buffer, "{\"w\":\"%s\", \"v\":%f},", &vocab[max_w * (sprofiles[node].nbr[i].index)], sprofiles[node].nbr[i].value);
680 strcat(buffer, pair_buffer);
681 }
682 buffer[strlen(buffer)-1]=']';
683 strcat(buffer, "\n");
684 printf(buffer);
685 return(strdup(buffer));
686}
687
Marc Kupietze243efd2018-01-11 22:19:24 +0100688char *getClassicCollocators(long node) {
689 char *res = (cdb? strdup(get_collocators_as_json(cdb, node)) : "[]");
690 return res;
691}
692
Marc Kupietza2e64502016-04-27 09:53:51 +0200693wordlist *getTargetWords(char *st1, int search_backw) {
Marc Kupietz48c29682016-03-19 11:30:43 +0100694 wordlist *wl = malloc(sizeof(wordlist));
695 char st[100][max_size], sep[100];
696 long a, b=0, c=0, cn=0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200697 int unmerged;
698
Marc Kupietzdc22b982015-10-09 09:19:34 +0200699 while (1) {
700 st[cn][b] = st1[c];
701 b++;
702 c++;
703 st[cn][b] = 0;
704 if (st1[c] == 0) break;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100705 if (st1[c] == ' ' || st1[c] == '-') {
706 sep[cn++] = st1[c];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200707 b = 0;
708 c++;
709 }
710 }
711 cn++;
712 for (a = 0; a < cn; a++) {
Marc Kupietza2e64502016-04-27 09:53:51 +0200713 if(search_backw) {
714 for (b = words - 1; b >= 0; b--) if (!strcmp(&vocab[b * max_w], st[a])) break;
715 } else {
716 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
717 }
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100718 if (b == words) b = -1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100719 wl->wordi[a] = b;
720 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], wl->wordi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200721 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100722 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100723 cn--;
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100724 free(wl);
725 return NULL;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200726 }
727 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100728 wl->length=cn;
729 return(wl);
730}
731
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100732void *_get_neighbours(void *arg) {
733 knnpars *pars = arg;
Marc Kupietz48c29682016-03-19 11:30:43 +0100734 char *st1 = pars->token;
735 int N = pars->N;
736 long from = pars -> from;
737 unsigned long upto = pars -> upto;
738 char file_name[max_size], st[100][max_size], *sep;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100739 float dist, len, vec[max_size];
740 long long a, b, c, d, cn, *bi;
Marc Kupietz48c29682016-03-19 11:30:43 +0100741 char ch;
742 knn *nbs = NULL;
743 wordlist *wl = pars->wl;
744
Marc Kupietzc55c8872017-12-01 23:27:10 +0100745 collocator *best = pars->best;
Marc Kupietz48c29682016-03-19 11:30:43 +0100746
747 float worstbest=-1;
748
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100749 for (a = 0; a < N; a++) best[a].activation = 0;
Marc Kupietz48c29682016-03-19 11:30:43 +0100750 a = 0;
751 bi = wl->wordi;
752 cn = wl->length;
753 sep = wl->sep;
754 b = bi[0];
755 c = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100756 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100757 N = 0;
758 goto end;
759 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200760 for (a = 0; a < size; a++) vec[a] = 0;
761 for (b = 0; b < cn; b++) {
762 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100763 if(b>0 && sep[b-1] == '-')
764 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
765 else
766 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200767 }
768 len = 0;
769 for (a = 0; a < size; a++) len += vec[a] * vec[a];
770 len = sqrt(len);
771 for (a = 0; a < size; a++) vec[a] /= len;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100772 for (a = 0; a < N; a++) best[a].activation = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100773 for (c = from; c < upto; c++) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200774 if(garbage && garbage[c]) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200775 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100776// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100777// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
778// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200779 dist = 0;
780 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100781 if(dist > worstbest) {
782 for (a = 0; a < N; a++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100783 if (dist > best[a].activation) {
784 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
785 best[a].activation = dist;
786 best[a].wordi = c;
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100787 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200788 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200789 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100790 worstbest = best[N-1].activation;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200791 }
792 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100793
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100794end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100795 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200796}
797
Marc Kupietze0e42132017-11-24 16:44:34 +0100798int cmp_activation (const void * a, const void * b) {
799 float fb = ((collocator *)a)->activation;
800 float fa = ((collocator *)b)->activation;
801 return (fa > fb) - (fa < fb);
802}
803
804int cmp_probability (const void * a, const void * b) {
805 float fb = ((collocator *)a)->probability;
806 float fa = ((collocator *)b)->probability;
807 return (fa > fb) - (fa < fb);
808}
809
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100810
Marc Kupietzd91212f2017-11-13 10:05:09 +0100811SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100812 HV *result = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100813 float *target_sums, vec[max_size];
Marc Kupietza5f60042017-05-04 10:38:12 +0200814 long long old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100815 long a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100816 knn *para_nbs[MAX_THREADS];
817 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100818 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100819 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100820 wordlist *wl;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200821 int syn_threads = (M2? window * 2 : 0);
822 int para_threads = num_threads - syn_threads;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100823
Marc Kupietzc55c8872017-12-01 23:27:10 +0100824 collocator *best;
825 posix_memalign((void **) &best, 128, 10 * N * sizeof(collocator));
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100826 memset(best, 0, 10 * N * sizeof(collocator));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100827
Marc Kupietz000ad862016-02-26 14:59:12 +0100828 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
829
Marc Kupietzd5d05732018-01-30 11:57:35 +0100830 if(cutoff < 1 || cutoff > words)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100831 cutoff=words;
832
Marc Kupietza2e64502016-04-27 09:53:51 +0200833 wl = getTargetWords(st1, search_backw);
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100834 if(wl == NULL || wl->length < 1)
Marc Kupietz271e2a42016-03-22 11:37:43 +0100835 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100836
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100837 old_words = cutoff;
Marc Kupietza5f60042017-05-04 10:38:12 +0200838 if(merge_words > 0)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100839 cutoff = merge_words * 1.25; /* HACK */
840 slice = cutoff / para_threads;
Marc Kupietza5f60042017-05-04 10:38:12 +0200841
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100842 a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float));
843 for(a = 0; a < cutoff; a++)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100844 target_sums[a] = 0;
845
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200846 printf("Starting %d threads\n", para_threads);
847 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100848 for(a=0; a < para_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100849 pars[a].cutoff = cutoff;
Marc Kupietz000ad862016-02-26 14:59:12 +0100850 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100851 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100852 pars[a].N = N;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100853 pars[a].best = &best[N*a];
Marc Kupietz000ad862016-02-26 14:59:12 +0100854 pars[a].from = a*slice;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100855 pars[a].upto = ((a+1)*slice > cutoff? cutoff:(a+1)*slice);
Marc Kupietz000ad862016-02-26 14:59:12 +0100856 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
857 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200858 if(M2) {
859 for(a=0; a < syn_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100860 pars[a + para_threads].cutoff = cutoff;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200861 pars[a + para_threads].target_sums = target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100862 pars[a + para_threads].window_sums = window_sums;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200863 pars[a + para_threads].wl = wl;
864 pars[a + para_threads].N = N;
865 pars[a + para_threads].from = a;
866 pars[a + para_threads].upto = a+1;
867 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
868 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100869 }
870 printf("Waiting for para threads to join\n");
871 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100872 for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *) &para_nbs[a]);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100873 printf("Para threads joint\n");
874 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +0100875
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200876 /* if(!syn_nbs[0]) */
877 /* goto end; */
Marc Kupietz000ad862016-02-26 14:59:12 +0100878
Marc Kupietzc55c8872017-12-01 23:27:10 +0100879 qsort(best, N*para_threads, sizeof(collocator), cmp_activation);
Marc Kupietz000ad862016-02-26 14:59:12 +0100880
Marc Kupietz000ad862016-02-26 14:59:12 +0100881
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100882 long long chosen[MAX_NEIGHBOURS];
883 printf("N: %ld\n", N);
884
Marc Kupietz271e2a42016-03-22 11:37:43 +0100885 AV* array = newAV();
Marc Kupietzd91212f2017-11-13 10:05:09 +0100886 int i, j;
Marc Kupietza5f60042017-05-04 10:38:12 +0200887 int l1_words=0, l2_words=0;
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100888
889 for (a = 0, i = 0; i < N && a < N*para_threads; a++) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100890 int filtered=0;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100891 long long c = best[a].wordi;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100892 if (dedupe && i > 0) {
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100893 for (j=0; j<i && !filtered; j++)
894 if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) ||
895 strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) {
896 printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]);
Marc Kupietzd91212f2017-11-13 10:05:09 +0100897 filtered = 1;
898 }
899 if(filtered)
900 continue;
901 }
Marc Kupietza5f60042017-05-04 10:38:12 +0200902 if(merge_words > 0) {
903 if(c >= merge_words) {
904 if(l1_words > N / 2)
905 continue;
906 else
907 l1_words++;
908 } else {
909 if(l2_words > N / 2)
910 continue;
911 else
912 l2_words++;
913 }
914 }
Marc Kupietza5f60042017-05-04 10:38:12 +0200915 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 +0100916 fflush(stdout);
Marc Kupietza5f60042017-05-04 10:38:12 +0200917
Marc Kupietz271e2a42016-03-22 11:37:43 +0100918 HV* hash = newHV();
Marc Kupietza5f60042017-05-04 10:38:12 +0200919 SV* word = newSVpvf(&vocab[c * max_w], 0);
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100920 chosen[i] = c;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100921 if(latin_enc == 0) SvUTF8_on(word);
Marc Kupietza5f60042017-05-04 10:38:12 +0200922 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100923 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100924 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
925 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100926 AV *vector = newAV();
927 for (b = 0; b < size; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100928 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100929 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100930 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
931 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietza5f60042017-05-04 10:38:12 +0200932 i++;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100933 }
934 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
935
Marc Kupietz50485ba2016-03-23 09:13:14 +0100936 for(b=0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100937 best[b].wordi = -1L;
938 best[b].activation = 0;
939 best[b].probability = 0;
940 best[b].position = 0;
941 best[b].activation_sum = 0;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100942 }
943
Marc Kupietza77acce2017-11-30 16:59:07 +0100944 float total_activation = 0;
945
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200946 if (M2) {
947 printf("Waiting for syn threads to join\n");
948 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100949 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], (void *) &syn_nbs[a]);
Marc Kupietza77acce2017-11-30 16:59:07 +0100950 for (a = 0; a <= syn_threads; a++) {
Marc Kupietz63872182018-03-12 09:44:49 +0100951 if(a == window) continue;
Marc Kupietza77acce2017-11-30 16:59:07 +0100952 total_activation += window_sums[a];
953 printf("window pos: %d, sum: %f\n", a, window_sums[a]);
954 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200955 printf("syn threads joint\n");
956 fflush(stdout);
Marc Kupietz50485ba2016-03-23 09:13:14 +0100957
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200958 for(b=0; b < syn_nbs[0]->length; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100959 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
960 best[b].position = -1; // syn_nbs[0]->pos[b];
961 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100962 best[b].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +0100963 best[b].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100964 best[b].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +0100965 best[b].cprobability = syn_nbs[0]->best[b].cprobability;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200966 }
Marc Kupietz30ca4342017-11-22 21:21:20 +0100967
968 float best_window_sum[MAX_NEIGHBOURS];
969 int found_index=0, i=0, j, w;
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100970 for(a=0; a < syn_threads; a++) {
971 for(b=0; b < syn_nbs[a]->length; b++) {
972 for(i=0; i < found_index; i++)
973 if(best[i].wordi == syn_nbs[a]->best[b].wordi)
974 break;
975 if(i >= found_index) {
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100976 best[found_index].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +0100977 best[found_index].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100978 best[found_index].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +0100979 best[found_index].cprobability = syn_nbs[a]->best[b].cprobability;
980 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 +0100981 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
982 // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
Marc Kupietz30ca4342017-11-22 21:21:20 +0100983 }
984 }
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100985 }
Marc Kupietza77acce2017-11-30 16:59:07 +0100986 sort_by =0; // ALWAYS AUTO-FOCUS
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100987 if(sort_by != 1 && sort_by != 2) { // sort by auto focus mean
Marc Kupietz30ca4342017-11-22 21:21:20 +0100988 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) -1);
Marc Kupietzbd41da32017-11-24 10:26:43 +0100989 int wpos;
Marc Kupietz4116b432017-12-06 14:15:32 +0100990 int bits_set = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100991 for(i=0; i < found_index; i++) {
Marc Kupietz4116b432017-12-06 14:15:32 +0100992 best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100993 for(w=1; w < (1 << syn_threads); w++) { // loop through all possible windows
Marc Kupietz4116b432017-12-06 14:15:32 +0100994 float word_window_sum = 0, word_window_average=0, word_cprobability_sum=0, word_activation_sum = 0, total_window_sum = 0;
995 bits_set = 0;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100996 for(a=0; a < syn_threads; a++) {
Marc Kupietz30ca4342017-11-22 21:21:20 +0100997 if((1 << a) & w) {
Marc Kupietzbd41da32017-11-24 10:26:43 +0100998 wpos = (a >= window? a+1 : a);
999 total_window_sum += window_sums[wpos];
Marc Kupietz30ca4342017-11-22 21:21:20 +01001000 }
1001 }
Marc Kupietzbd41da32017-11-24 10:26:43 +01001002// printf("%d window-sum %f\n", w, total_window_sum);
1003 for(a=0; a < syn_threads; a++) {
1004 if((1 << a) & w) {
1005 wpos = (a >= window? a+1 : a);
1006 bits_set++;
1007 for(b=0; b < syn_nbs[a]->length; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001008 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001009// float acti = syn_nbs[a]->best[b].activation / total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001010// 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 +01001011// word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1012// 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 +01001013
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001014 word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
Marc Kupietza77acce2017-11-30 16:59:07 +01001015// word_window_sum += acti - (word_window_sum * acti); syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
1016
Marc Kupietz4116b432017-12-06 14:15:32 +01001017 word_window_average += syn_nbs[a]->best[b].activation; // - word_window_average * syn_nbs[a]->best[b].activation; // conormalied activation sum
1018 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 +01001019 word_activation_sum += syn_nbs[a]->best[b].activation;
1020 if(syn_nbs[a]->best[b].activation > best[i].max_activation)
1021 best[i].max_activation = syn_nbs[a]->best[b].activation;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +01001022 if(syn_nbs[a]->best[b].activation > best[i].heat[wpos] )
Marc Kupietz728b8ed2017-12-02 11:13:49 +01001023 best[i].heat[wpos] = syn_nbs[a]->best[b].activation;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001024 }
1025 }
1026 }
Marc Kupietz4116b432017-12-06 14:15:32 +01001027 if(bits_set) {
1028 word_window_average /= bits_set;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001029// word_activation_sum /= bits_set;
1030// word_window_sum /= bits_set;
Marc Kupietz4116b432017-12-06 14:15:32 +01001031 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001032
1033 word_window_sum /= total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +01001034
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001035 if(word_window_sum > best[i].probability) {
Marc Kupietz4116b432017-12-06 14:15:32 +01001036// best[i].position = w;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001037 best[i].probability = word_window_sum;
Marc Kupietz30ca4342017-11-22 21:21:20 +01001038 }
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001039
Marc Kupietz4116b432017-12-06 14:15:32 +01001040 if(word_cprobability_sum > best[i].cprobability_sum) {
1041 best[i].position = w;
1042 best[i].cprobability_sum = word_cprobability_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001043 }
Marc Kupietz4116b432017-12-06 14:15:32 +01001044
1045 best[i].average = word_window_average;
1046// best[i].activation = word_activation_sum;
1047 }
Marc Kupietz30ca4342017-11-22 21:21:20 +01001048 }
Marc Kupietze0e42132017-11-24 16:44:34 +01001049 qsort(best, found_index, sizeof(collocator), cmp_probability);
Marc Kupietz30ca4342017-11-22 21:21:20 +01001050// for(i=0; i < found_index; i++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001051// 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 +01001052// }
1053
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001054 } else if(sort_by == 1) { // responsiveness any window position
1055 int wpos;
1056 for(i=0; i < found_index; i++) {
1057 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
1058 for(a=0; a < syn_threads; a++) {
1059 wpos = (a >= window? a+1 : a);
1060 for(b=0; b < syn_nbs[a]->length; b++)
1061 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
1062 best[i].probability += syn_nbs[a]->best[b].probability;
1063 if(syn_nbs[a]->best[b].activation > 0.25)
1064 best[i].position |= 1 << wpos;
1065 if(syn_nbs[a]->best[b].activation > best[i].activation) {
1066 best[i].activation = syn_nbs[a]->best[b].activation;
1067 }
1068 }
1069 }
1070 }
1071 qsort(best, found_index, sizeof(collocator), cmp_activation);
1072 } else if(sort_by == 2) { // single window position
Marc Kupietz30ca4342017-11-22 21:21:20 +01001073 for(a=1; a < syn_threads; a++) {
1074 for(b=0; b < syn_nbs[a]->length; b++) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001075 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001076 if(syn_nbs[a]->best[b].activation > best[c].activation) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001077 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001078 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001079 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001080 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
1081 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 +02001082 break;
Marc Kupietz6d9a6782016-03-23 17:25:25 +01001083 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001084 }
1085 }
1086 }
1087 } else { // sort by mean p
1088 for(a=1; a < syn_threads; a++) {
1089 for(b=0; b < syn_nbs[a]->length; b++) {
1090 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001091 if(target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001092 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001093 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001094 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001095 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
1096 best[c].position = (1 << 2*window) - 1; // syn_nbs[a]->pos[b];
1097 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001098 break;
1099 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001100 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +01001101 }
1102 }
1103 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001104 array = newAV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001105 for (a = 0, i=0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
1106 long long c = best[a].wordi;
Marc Kupietza77acce2017-11-30 16:59:07 +01001107/*
Marc Kupietzd91212f2017-11-13 10:05:09 +01001108 if (dedupe) {
1109 int filtered=0;
1110 for (j=0; j<i; j++)
1111 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
1112 strcasestr(chosen[j], &vocab[c * max_w])) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001113 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
1114 filtered = 1;
1115 }
Marc Kupietzd91212f2017-11-13 10:05:09 +01001116 if(filtered)
1117 continue;
1118 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001119*/
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001120 chosen[i++]=c;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001121 HV* hash = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001122 SV* word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001123 AV* heat = newAV();
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001124 if(latin_enc == 0) SvUTF8_on(word);
1125 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001126 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001127 hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001128 hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001129 hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001130 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 +01001131 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 +01001132 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001133 best[a].heat[5]=0;
1134 for(i=10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i]));
1135 hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV*)heat), 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001136 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietz271e2a42016-03-22 11:37:43 +01001137 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001138 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001139 }
Marc Kupietz000ad862016-02-26 14:59:12 +01001140end:
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001141 words = old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001142 free(best);
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001143 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +01001144}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +01001145
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001146int dump_vecs(char *fname) {
1147 long i, j;
1148 FILE *f;
1149 /* if(words>200000) */
1150 /* words=200000; */
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001151
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001152 if((f=fopen(fname, "w")) == NULL) {
1153 fprintf(stderr, "cannot open %s for writing\n", fname);
1154 return(-1);
1155 }
1156 fprintf(f, "%lld %lld\n", words, size);
1157 for (i=0; i < words; i++) {
1158 fprintf(f, "%s ", &vocab[i * max_w]);
1159 for(j=0; j < size - 1; j++)
1160 fprintf(f, "%f ", M[i*size + j]);
1161 fprintf(f, "%f\n", M[i*size + j]);
1162 }
1163 fclose(f);
1164 return(0);
1165}
Marc Kupietzdc22b982015-10-09 09:19:34 +02001166