blob: 3e3a683e1709a871925ee286c8451097d5162c98 [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 Kupietz793413b2016-04-02 21:48:57 +020034
Marc Kupietza5f60042017-05-04 10:38:12 +020035getopts('d:Gil:p:m:n:M:');
Marc Kupietz6ed81872016-04-27 14:04:04 +020036
37if($opt_M) {
Marc Kupietzed930212016-04-27 15:42:38 +020038 open my $handle, '<:encoding(UTF-8)', $opt_M
39 or die "Can't open '$opt_M' for reading: $!";
40 while(<$handle>) {
Marc Kupietz6ed81872016-04-27 14:04:04 +020041 foreach my $mw (split /\s+/) {
42 $marked{$mw}=1
43 }
44 }
Marc Kupietzed930212016-04-27 15:42:38 +020045 close($handle);
Marc Kupietz6ed81872016-04-27 14:04:04 +020046}
Marc Kupietza5b90152016-03-15 17:39:19 +010047
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010048# -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 +010049if(!$ARGV[0]) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010050 init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0));
Marc Kupietz2cb667e2016-03-10 09:44:12 +010051} else {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010052 init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0));
Marc Kupietz793413b2016-04-02 21:48:57 +020053 if(open(FILE, "$ARGV[0].args")) {
54 $training_args = <FILE>;
55 }
56 close(FILE);
Marc Kupietz2cb667e2016-03-10 09:44:12 +010057}
Marc Kupietzdc22b982015-10-09 09:19:34 +020058
Marc Kupietza2e64502016-04-27 09:53:51 +020059if($opt_m) {
60 $mergedEnd = mergeVectors($opt_m);
61}
62
Marc Kupietz6ed81872016-04-27 14:04:04 +020063
Marc Kupietz43ee87e2016-04-25 10:50:08 +020064if($opt_d) { # -d: dump vecs and exit
65 dump_vecs($opt_d);
66 exit;
67}
68
Marc Kupietza5b90152016-03-15 17:39:19 +010069my $daemon = Mojo::Server::Daemon->new(
70 app => app,
71 listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"]
72);
73
Marc Kupietz5c3887d2016-04-28 08:53:35 +020074if($opt_G) {
75 print "Filtering garbage\n";
76 filter_garbage();
77}
78
Marc Kupietz554aff52017-11-09 14:42:09 +010079get '*/js/*' => sub {
Marc Kupietzffef9302017-11-07 15:58:01 +010080 my $c = shift;
81 my $url = $c->req->url;
82 $url =~ s@/derekovecs@@g;
83 $c->app->log->info("GET: " . $url);
84 $c->reply->static($url);
85};
86
Marc Kupietz19c68242018-03-12 09:42:21 +010087sub getClassicCollocatorsCached {
88 my ($c, $word) = @_;
89 if(!$cccache{$word}) {
90 $cccache{$word} = getClassicCollocators($word);
91 } else {
92 $c->app->log->info("Getting classic collocators for $word from cache.");
93 }
94 return $cccache{$word};
95}
96
Marc Kupietz66bfd952017-12-11 09:59:45 +010097post '/derekovecs/getVecsByRanks' => sub {
98 my $self = shift;
99 my $vec = getVecs($self->req->json);
100 $self->render(json => $vec);
101};
102
Marc Kupietze13a3552018-01-25 08:48:34 +0100103any '*/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100104 my $self = shift;
Marc Kupietz19c68242018-03-12 09:42:21 +0100105 $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100106};
107
Marc Kupietze13a3552018-01-25 08:48:34 +0100108any '/getClassicCollocators' => sub {
Marc Kupietze243efd2018-01-11 22:19:24 +0100109 my $self = shift;
Marc Kupietz19c68242018-03-12 09:42:21 +0100110 $self->render(data => getClassicCollocatorsCached($self, $self->param("w") ? $self->param("w") : $self->req->json), format=>'json');
Marc Kupietze243efd2018-01-11 22:19:24 +0100111};
112
Marc Kupietzdf3d4b52017-11-29 16:57:27 +0100113get '*/img/*' => sub {
114 my $c = shift;
115 my $url = $c->req->url;
116 $url =~ s@/derekovecs@@g;
117 $c->app->log->info("GET: " . $url);
118 $c->reply->static($url);
119};
120
Marc Kupietzdc22b982015-10-09 09:19:34 +0200121get '/' => sub {
122 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +0200123 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200124 my $word=$c->param('word');
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100125 my $no_nbs=$c->param('n') || 100;
126 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +0100127 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100128 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100129 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200130 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100131 my $sort=$c->param('sort') || 0;
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100132 my $csv=$c->param('csv') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +0200133 my $json=$c->param('json') || 0;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100134 my $cutoff=$c->param('cutoff') || 500000;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100135 my $dedupe=$c->param('dedupe') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100136 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100137 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100138 my @collocations;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100139 if(defined($word) && $word !~ /^\s*$/) {
140 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100141 $word =~ s/\s+/ /g;
142 for my $w (split(' *\| *', $word)) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100143 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100144 $c->app->log->info("Getting $w results from cache");
Marc Kupietzd91212f2017-11-13 10:05:09 +0100145 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe}
Marc Kupietza5b90152016-03-15 17:39:19 +0100146 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100147 $c->app->log->info('Looking for neighbours of '.$w);
148 if($opt_i) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100149 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe);
Marc Kupietz15987412017-11-07 15:56:58 +0100150 } else {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100151 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe);
Marc Kupietz15987412017-11-07 15:56:58 +0100152 }
Marc Kupietz2dd2dd72017-12-01 22:08:14 +0100153 $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100154 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100155 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100156 }
157 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100158 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200159 if($json) {
160 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100161 } elsif($csv) {
162 my $csv_data="";
163 for (my $i=0; $i <= $no_nbs; $i++) {
164 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
165 }
166 for (my $i=0; $i < $no_nbs; $i++) {
167 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
168 }
169 chop $csv_data;
170 chop $csv_data;
171 $csv_data .= "\n";
172 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200173 } else {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100174 $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, dedupe=> $dedupe, marked=>\%marked, lists=> \@lists, collocators=> $res->{syntagmatic});
Marc Kupietzb613b052016-04-28 14:11:59 +0200175 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200176};
177
Marc Kupietz30ca4342017-11-22 21:21:20 +0100178helper(bitvec2window => sub {
179 my ($self, $n) = @_;
180 my $str = unpack("B32", pack("N", $n));
181 $str =~ s/^\d{22}//;
182 $str =~ s/^(\d{5})/$1x/;
183 $str =~ s/0/·/g;
184 $str =~ s/1/+/g;
185 return $str;
186 });
187
Marc Kupietza5b90152016-03-15 17:39:19 +0100188$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200189
190exit;
191
192__END__
193
194__C__
195#include <stdio.h>
196#include <string.h>
197#include <math.h>
198#include <malloc.h>
199#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +0100200#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +0100201#include <pthread.h>
Marc Kupietze243efd2018-01-11 22:19:24 +0100202#include <collocatordb.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200203
204#define max_size 2000
205#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100206#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100207#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +0100208#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100209#define MAX_CC 50
210#define EXP_TABLE_SIZE 1000
211#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +0100212#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +0200213
214//the thread function
215void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +0100216
217typedef struct {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100218 long long wordi;
219 long position;
220 float activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100221 float average;
Marc Kupietza77acce2017-11-30 16:59:07 +0100222 float cprobability; // column wise probability
Marc Kupietz4116b432017-12-06 14:15:32 +0100223 float cprobability_sum;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100224 float probability;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100225 float activation_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100226 float max_activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100227 float heat[16];
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100228} collocator;
229
230typedef struct {
231 collocator *best;
Marc Kupietz80abb442016-03-23 21:04:08 +0100232 int length;
Marc Kupietz000ad862016-02-26 14:59:12 +0100233} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100234
Marc Kupietz000ad862016-02-26 14:59:12 +0100235typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +0100236 long long wordi[MAX_NEIGHBOURS];
237 char sep[MAX_NEIGHBOURS];
238 int length;
239} wordlist;
240
241typedef struct {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100242 long cutoff;
Marc Kupietz48c29682016-03-19 11:30:43 +0100243 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100244 char *token;
245 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100246 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100247 unsigned long upto;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100248 collocator *best;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100249 float *target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100250 float *window_sums;
Marc Kupietz000ad862016-02-26 14:59:12 +0100251} knnpars;
252
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200253float *M, *M2=0L, *syn1neg_window, *expTable;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100254float *window_sums;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200255char *vocab;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200256char *garbage = NULL;
Marc Kupietze243efd2018-01-11 22:19:24 +0100257COLLOCATORDB *cdb = NULL;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100258
Marc Kupietza2e64502016-04-27 09:53:51 +0200259long long words, size, merged_end;
Marc Kupietza5f60042017-05-04 10:38:12 +0200260long long merge_words = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100261int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100262int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100263int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200264
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100265int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100266 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100267 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100268 long long a, b, c, d, cn;
269 float len;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200270 double val;
Marc Kupietz82b02672016-02-26 12:32:25 +0100271
Marc Kupietz67c20282016-02-26 09:42:00 +0100272 char binvecs_fname[256], binwords_fname[256];
273 strcpy(binwords_fname, file_name);
274 strcat(binwords_fname, ".words");
275 strcpy(binvecs_fname, file_name);
276 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200277
Marc Kupietza5b90152016-03-15 17:39:19 +0100278 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200279 f = fopen(file_name, "rb");
280 if (f == NULL) {
281 printf("Input file %s not found\n", file_name);
282 return -1;
283 }
284 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100285 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200286 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100287 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
288 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100289 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
290 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
291 if (M == NULL) {
292 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
293 return -1;
294 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200295 if(strstr(file_name, ".txt")) {
296 for (b = 0; b < words; b++) {
297 a = 0;
298 while (1) {
299 vocab[b * max_w + a] = fgetc(f);
300 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
301 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
302 }
303 vocab[b * max_w + a] = 0;
304 len = 0;
305 for (a = 0; a < size; a++) {
306 fscanf(f, "%lf", &val);
307 M[a + b * size] = val;
308 len += val * val;
309 }
310 len = sqrt(len);
311 for (a = 0; a < size; a++) M[a + b * size] /= len;
312 }
313 } else {
314 for (b = 0; b < words; b++) {
315 a = 0;
316 while (1) {
317 vocab[b * max_w + a] = fgetc(f);
318 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
319 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
320 }
321 vocab[b * max_w + a] = 0;
322 fread(&M[b * size], sizeof(float), size, f);
323 len = 0;
324 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
325 len = sqrt(len);
326 for (a = 0; a < size; a++) M[a + b * size] /= len;
327 }
328 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100329 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
330 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
331 fclose(binvecs);
332 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
333 fclose(binwords);
334 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100335 }
336 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
337 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
338 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
339 if (M == MAP_FAILED || vocab == MAP_FAILED) {
340 close(binvecs_fd);
341 close(binwords_fd);
342 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
343 exit(-1);
344 }
345 } else {
346 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
347 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100348 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200349 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100350
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200351 if(net_name && strlen(net_name) > 0) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100352 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
353 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
354 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100355 // munmap(M, sizeof(float) * words * size);
356 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 +0200357 if (M2 == MAP_FAILED) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100358 close(net_fd);
359 fprintf(stderr, "Cannot mmap %s\n", net_name);
360 exit(-1);
361 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100362 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100363 } else {
364 fprintf(stderr, "Cannot open %s\n", net_name);
365 exit(-1);
366 }
367 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
Marc Kupietze243efd2018-01-11 22:19:24 +0100368
369 char collocatordb_name[2048];
370 strcpy(collocatordb_name, net_name);
371 char *ext = rindex(collocatordb_name, '.');
372 if(ext) {
373 strcpy(ext, ".rocksdb");
374 if(access(collocatordb_name, R_OK) == 0) {
375 *ext = 0;
376 fprintf(stderr, "Opening collocator DB %s\n", collocatordb_name);
377 cdb = open_collocatordb(collocatordb_name);
378 }
379 }
380 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100381
382 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
383 for (i = 0; i < EXP_TABLE_SIZE; i++) {
384 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
385 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
386 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100387 window_sums = malloc(sizeof(float) * (window+1) * 2);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200388 return 0;
389}
390
Marc Kupietza2e64502016-04-27 09:53:51 +0200391long mergeVectors(char *file_name){
392 FILE *f, *binvecs, *binwords;
393 int binwords_fd, binvecs_fd, net_fd, i;
394 long long a, b, c, d, cn;
395 float len;
396 float *merge_vecs;
397 char *merge_vocab;
Marc Kupietza5f60042017-05-04 10:38:12 +0200398 /* long long merge_words, merge_size; */
399 long long merge_size;
Marc Kupietza2e64502016-04-27 09:53:51 +0200400
401 char binvecs_fname[256], binwords_fname[256];
402 strcpy(binwords_fname, file_name);
403 strcat(binwords_fname, ".words");
404 strcpy(binvecs_fname, file_name);
405 strcat(binvecs_fname, ".vecs");
406
407 f = fopen(file_name, "rb");
408 if (f == NULL) {
409 printf("Input file %s not found\n", file_name);
410 exit -1;
411 }
412 fscanf(f, "%lld", &merge_words);
413 fscanf(f, "%lld", &merge_size);
414 if(merge_size != size){
415 fprintf(stderr, "vectors must have the same length\n");
416 exit(-1);
417 }
418 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
419 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
420 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
421 if (merge_vecs == NULL || merge_vocab == NULL) {
422 close(binvecs_fd);
423 close(binwords_fd);
424 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
425 exit(-1);
426 }
427 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
428 read(binwords_fd, merge_vocab, merge_words * max_w);
429 } else {
430 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
431 exit(-1);
432 }
433 printf("Successfully reallocated memory\nMerging...\n");
434 fflush(stdout);
435 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
436 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
437 munmap(M, words * size * sizeof(float));
438 munmap(vocab, words * max_w);
439 M = merge_vecs;
440 vocab = merge_vocab;
441 merged_end = merge_words;
442 words += merge_words;
443 fclose(f);
444 printf("merged_end: %lld, words: %lld\n", merged_end, words);
445 return((long) merged_end);
446}
447
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200448void filter_garbage() {
449 long i;
Marc Kupietzab591a82016-04-28 14:08:49 +0200450 unsigned char *w, previous, c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200451 garbage = malloc(words);
452 memset(garbage, 0, words);
453 for (i = 0; i < words; i++) {
454 w = vocab + i * max_w;
455 previous = 0;
Marc Kupietz969adfa2018-03-12 09:43:53 +0100456 if(strncmp("quot", w, 4) == 0) {
457 garbage[i]=1;
458 printf("Gargabe: %s\n", vocab + i * max_w);
459 } else {
460 while((c = *w++) && !garbage[i]) {
461 if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
462 (previous == '-' && (c & 32)) ||
463 (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) ||
464 (previous == 'q' && c == 'u' && *(w) == 'o' && *(w+1) == 't') || /* quot */
465 c == '<'
466 ) {
467 garbage[i]=1;
468 continue;
469 }
470 previous = c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200471 }
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200472 }
473 }
474 return;
475}
476
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100477void *getCollocators(void *args) {
478 knnpars *pars = args;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100479 int N = pars->N;
480 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100481 knn *nbs = NULL;
482 long window_layer_size = size * window * 2;
483 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
484 float f, max_f, maxmax_f;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100485 float *target_sums, worstbest, wpos_sum;
486 collocator *best;
Marc Kupietzd5642582016-03-19 22:23:13 +0100487
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200488 if(M2 == NULL || cc == -1)
Marc Kupietzd5642582016-03-19 22:23:13 +0100489 return NULL;
490
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100491 a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100492 best = malloc(N * sizeof(collocator));
Marc Kupietz271e2a42016-03-22 11:37:43 +0100493 worstbest = MIN_RESP;
494
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100495 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100496 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100497 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100498 best[b].wordi = -1;
499 best[b].probability = 1;
500 best[b].activation = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100501 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100502
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100503 d = cc;
504 maxmax_f = -1;
505 maxmax_target = 0;
506
Marc Kupietz271e2a42016-03-22 11:37:43 +0100507 for (a = pars->from; a < pars->upto; a++) {
508 if(a >= window)
509 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100510 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100511 printf("window pos: %ld\n", a);
512 if (a != window) {
513 max_f = -1;
514 window_offset = a * size;
515 if (a > window)
516 window_offset -= size;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100517 for(target = 0; target < pars->cutoff; target ++) {
518 if(garbage && garbage[target]) continue;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100519 if(target == d)
520 continue;
521 f = 0;
522 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100523 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100524 if (f < -MAX_EXP)
525 continue;
526 else if (f > MAX_EXP)
527 continue;
528 else
529 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100530 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100531
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100532 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100533 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100534 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100535 if (f > best[b].activation) {
536 memmove(best + b + 1, best + b, (N - b -1) * sizeof(collocator));
537 best[b].activation = f;
538 best[b].wordi = target;
539 best[b].position = window-a;
Marc Kupietz33679a32016-03-22 08:49:39 +0100540 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100541 }
542 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100543 if(b == N - 1)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100544 worstbest = best[N-1].activation;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100545 }
546 }
547 printf("%d %.2f\n", max_target, max_f);
548 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
549 if(max_f > maxmax_f) {
550 maxmax_f = max_f;
551 maxmax_target = max_target;
552 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100553 for (b = 0; b < N; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100554 if(best[b].position == window-a)
Marc Kupietza77acce2017-11-30 16:59:07 +0100555 best[b].cprobability = best[b].activation / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100556 } else {
557 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
558 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100559 pars->window_sums[a] = wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100560 }
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100561 for (b = 0; b < pars->cutoff; b++)
Marc Kupietza77acce2017-11-30 16:59:07 +0100562 pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2);
563 printf("Target-Summe von 0: %f\n", pars->target_sums[150298]);
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100564 free(target_sums);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100565 for(b=0; b<N && best[b].wordi >= 0; b++);; // THIS LOOP IS NEEDED (b...)
566// 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 +0100567// printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100568 nbs = malloc(sizeof(knn));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100569 nbs->best = best;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100570 nbs->length = b-1;
571 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100572}
573
Marc Kupietz30ca4342017-11-22 21:21:20 +0100574
Marc Kupietz66bfd952017-12-11 09:59:45 +0100575AV *getVecs(AV *array) {
576 int i, b;
577 AV *result = newAV();
578 for (i=0; i<=av_len(array); i++) {
579 SV** elem = av_fetch(array, i, 0);
580 if (elem != NULL) {
581 long j = (long) SvNV(*elem);
582 AV *vector = newAV();
583 for (b = 0; b < size; b++) {
584 av_push(vector, newSVnv(M[b + j * size]));
585 }
586 av_push(result, newRV_noinc(vector));
587 }
588 }
589 return result;
590}
591
Marc Kupietze243efd2018-01-11 22:19:24 +0100592char *getClassicCollocators(long node) {
593 char *res = (cdb? strdup(get_collocators_as_json(cdb, node)) : "[]");
594 return res;
595}
596
Marc Kupietza2e64502016-04-27 09:53:51 +0200597wordlist *getTargetWords(char *st1, int search_backw) {
Marc Kupietz48c29682016-03-19 11:30:43 +0100598 wordlist *wl = malloc(sizeof(wordlist));
599 char st[100][max_size], sep[100];
600 long a, b=0, c=0, cn=0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200601 int unmerged;
602
Marc Kupietzdc22b982015-10-09 09:19:34 +0200603 while (1) {
604 st[cn][b] = st1[c];
605 b++;
606 c++;
607 st[cn][b] = 0;
608 if (st1[c] == 0) break;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100609 if (st1[c] == ' ' || st1[c] == '-') {
610 sep[cn++] = st1[c];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200611 b = 0;
612 c++;
613 }
614 }
615 cn++;
616 for (a = 0; a < cn; a++) {
Marc Kupietza2e64502016-04-27 09:53:51 +0200617 if(search_backw) {
618 for (b = words - 1; b >= 0; b--) if (!strcmp(&vocab[b * max_w], st[a])) break;
619 } else {
620 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
621 }
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100622 if (b == words) b = -1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100623 wl->wordi[a] = b;
624 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], wl->wordi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200625 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100626 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100627 cn--;
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100628 free(wl);
629 return NULL;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200630 }
631 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100632 wl->length=cn;
633 return(wl);
634}
635
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100636void *_get_neighbours(void *arg) {
637 knnpars *pars = arg;
Marc Kupietz48c29682016-03-19 11:30:43 +0100638 char *st1 = pars->token;
639 int N = pars->N;
640 long from = pars -> from;
641 unsigned long upto = pars -> upto;
642 char file_name[max_size], st[100][max_size], *sep;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100643 float dist, len, vec[max_size];
644 long long a, b, c, d, cn, *bi;
Marc Kupietz48c29682016-03-19 11:30:43 +0100645 char ch;
646 knn *nbs = NULL;
647 wordlist *wl = pars->wl;
648
Marc Kupietzc55c8872017-12-01 23:27:10 +0100649 collocator *best = pars->best;
Marc Kupietz48c29682016-03-19 11:30:43 +0100650
651 float worstbest=-1;
652
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100653 for (a = 0; a < N; a++) best[a].activation = 0;
Marc Kupietz48c29682016-03-19 11:30:43 +0100654 a = 0;
655 bi = wl->wordi;
656 cn = wl->length;
657 sep = wl->sep;
658 b = bi[0];
659 c = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100660 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100661 N = 0;
662 goto end;
663 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200664 for (a = 0; a < size; a++) vec[a] = 0;
665 for (b = 0; b < cn; b++) {
666 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100667 if(b>0 && sep[b-1] == '-')
668 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
669 else
670 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200671 }
672 len = 0;
673 for (a = 0; a < size; a++) len += vec[a] * vec[a];
674 len = sqrt(len);
675 for (a = 0; a < size; a++) vec[a] /= len;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100676 for (a = 0; a < N; a++) best[a].activation = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100677 for (c = from; c < upto; c++) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200678 if(garbage && garbage[c]) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200679 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100680// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100681// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
682// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200683 dist = 0;
684 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100685 if(dist > worstbest) {
686 for (a = 0; a < N; a++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100687 if (dist > best[a].activation) {
688 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
689 best[a].activation = dist;
690 best[a].wordi = c;
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100691 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200692 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200693 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100694 worstbest = best[N-1].activation;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200695 }
696 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100697
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100698end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100699 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200700}
701
Marc Kupietze0e42132017-11-24 16:44:34 +0100702int cmp_activation (const void * a, const void * b) {
703 float fb = ((collocator *)a)->activation;
704 float fa = ((collocator *)b)->activation;
705 return (fa > fb) - (fa < fb);
706}
707
708int cmp_probability (const void * a, const void * b) {
709 float fb = ((collocator *)a)->probability;
710 float fa = ((collocator *)b)->probability;
711 return (fa > fb) - (fa < fb);
712}
713
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100714
Marc Kupietzd91212f2017-11-13 10:05:09 +0100715SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100716 HV *result = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100717 float *target_sums, vec[max_size];
Marc Kupietza5f60042017-05-04 10:38:12 +0200718 long long old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100719 long a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100720 knn *para_nbs[MAX_THREADS];
721 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100722 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100723 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100724 wordlist *wl;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200725 int syn_threads = (M2? window * 2 : 0);
726 int para_threads = num_threads - syn_threads;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100727
Marc Kupietzc55c8872017-12-01 23:27:10 +0100728 collocator *best;
729 posix_memalign((void **) &best, 128, 10 * N * sizeof(collocator));
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100730 memset(best, 0, 10 * N * sizeof(collocator));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100731
Marc Kupietz000ad862016-02-26 14:59:12 +0100732 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
733
Marc Kupietzd5d05732018-01-30 11:57:35 +0100734 if(cutoff < 1 || cutoff > words)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100735 cutoff=words;
736
Marc Kupietza2e64502016-04-27 09:53:51 +0200737 wl = getTargetWords(st1, search_backw);
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100738 if(wl == NULL || wl->length < 1)
Marc Kupietz271e2a42016-03-22 11:37:43 +0100739 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100740
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100741 old_words = cutoff;
Marc Kupietza5f60042017-05-04 10:38:12 +0200742 if(merge_words > 0)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100743 cutoff = merge_words * 1.25; /* HACK */
744 slice = cutoff / para_threads;
Marc Kupietza5f60042017-05-04 10:38:12 +0200745
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100746 a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float));
747 for(a = 0; a < cutoff; a++)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100748 target_sums[a] = 0;
749
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200750 printf("Starting %d threads\n", para_threads);
751 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100752 for(a=0; a < para_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100753 pars[a].cutoff = cutoff;
Marc Kupietz000ad862016-02-26 14:59:12 +0100754 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100755 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100756 pars[a].N = N;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100757 pars[a].best = &best[N*a];
Marc Kupietz000ad862016-02-26 14:59:12 +0100758 pars[a].from = a*slice;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100759 pars[a].upto = ((a+1)*slice > cutoff? cutoff:(a+1)*slice);
Marc Kupietz000ad862016-02-26 14:59:12 +0100760 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
761 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200762 if(M2) {
763 for(a=0; a < syn_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100764 pars[a + para_threads].cutoff = cutoff;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200765 pars[a + para_threads].target_sums = target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100766 pars[a + para_threads].window_sums = window_sums;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200767 pars[a + para_threads].wl = wl;
768 pars[a + para_threads].N = N;
769 pars[a + para_threads].from = a;
770 pars[a + para_threads].upto = a+1;
771 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
772 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100773 }
774 printf("Waiting for para threads to join\n");
775 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100776 for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *) &para_nbs[a]);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100777 printf("Para threads joint\n");
778 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +0100779
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200780 /* if(!syn_nbs[0]) */
781 /* goto end; */
Marc Kupietz000ad862016-02-26 14:59:12 +0100782
Marc Kupietzc55c8872017-12-01 23:27:10 +0100783 qsort(best, N*para_threads, sizeof(collocator), cmp_activation);
Marc Kupietz000ad862016-02-26 14:59:12 +0100784
Marc Kupietz000ad862016-02-26 14:59:12 +0100785
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100786 long long chosen[MAX_NEIGHBOURS];
787 printf("N: %ld\n", N);
788
Marc Kupietz271e2a42016-03-22 11:37:43 +0100789 AV* array = newAV();
Marc Kupietzd91212f2017-11-13 10:05:09 +0100790 int i, j;
Marc Kupietza5f60042017-05-04 10:38:12 +0200791 int l1_words=0, l2_words=0;
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100792
793 for (a = 0, i = 0; i < N && a < N*para_threads; a++) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100794 int filtered=0;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100795 long long c = best[a].wordi;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100796 if (dedupe && i > 0) {
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100797 for (j=0; j<i && !filtered; j++)
798 if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) ||
799 strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) {
800 printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]);
Marc Kupietzd91212f2017-11-13 10:05:09 +0100801 filtered = 1;
802 }
803 if(filtered)
804 continue;
805 }
Marc Kupietza5f60042017-05-04 10:38:12 +0200806 if(merge_words > 0) {
807 if(c >= merge_words) {
808 if(l1_words > N / 2)
809 continue;
810 else
811 l1_words++;
812 } else {
813 if(l2_words > N / 2)
814 continue;
815 else
816 l2_words++;
817 }
818 }
Marc Kupietza5f60042017-05-04 10:38:12 +0200819 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 +0100820 fflush(stdout);
Marc Kupietza5f60042017-05-04 10:38:12 +0200821
Marc Kupietz271e2a42016-03-22 11:37:43 +0100822 HV* hash = newHV();
Marc Kupietza5f60042017-05-04 10:38:12 +0200823 SV* word = newSVpvf(&vocab[c * max_w], 0);
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100824 chosen[i] = c;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100825 if(latin_enc == 0) SvUTF8_on(word);
Marc Kupietza5f60042017-05-04 10:38:12 +0200826 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100827 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100828 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
829 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100830 AV *vector = newAV();
831 for (b = 0; b < size; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100832 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100833 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100834 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
835 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietza5f60042017-05-04 10:38:12 +0200836 i++;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100837 }
838 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
839
Marc Kupietz50485ba2016-03-23 09:13:14 +0100840 for(b=0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100841 best[b].wordi = -1L;
842 best[b].activation = 0;
843 best[b].probability = 0;
844 best[b].position = 0;
845 best[b].activation_sum = 0;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100846 }
847
Marc Kupietza77acce2017-11-30 16:59:07 +0100848 float total_activation = 0;
849
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200850 if (M2) {
851 printf("Waiting for syn threads to join\n");
852 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100853 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], (void *) &syn_nbs[a]);
Marc Kupietza77acce2017-11-30 16:59:07 +0100854 for (a = 0; a <= syn_threads; a++) {
855 total_activation += window_sums[a];
856 printf("window pos: %d, sum: %f\n", a, window_sums[a]);
857 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200858 printf("syn threads joint\n");
859 fflush(stdout);
Marc Kupietz50485ba2016-03-23 09:13:14 +0100860
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200861 for(b=0; b < syn_nbs[0]->length; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100862 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
863 best[b].position = -1; // syn_nbs[0]->pos[b];
864 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100865 best[b].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +0100866 best[b].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100867 best[b].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +0100868 best[b].cprobability = syn_nbs[0]->best[b].cprobability;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200869 }
Marc Kupietz30ca4342017-11-22 21:21:20 +0100870
871 float best_window_sum[MAX_NEIGHBOURS];
872 int found_index=0, i=0, j, w;
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100873 for(a=0; a < syn_threads; a++) {
874 for(b=0; b < syn_nbs[a]->length; b++) {
875 for(i=0; i < found_index; i++)
876 if(best[i].wordi == syn_nbs[a]->best[b].wordi)
877 break;
878 if(i >= found_index) {
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100879 best[found_index].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +0100880 best[found_index].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100881 best[found_index].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +0100882 best[found_index].cprobability = syn_nbs[a]->best[b].cprobability;
883 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 +0100884 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
885 // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
Marc Kupietz30ca4342017-11-22 21:21:20 +0100886 }
887 }
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100888 }
Marc Kupietza77acce2017-11-30 16:59:07 +0100889 sort_by =0; // ALWAYS AUTO-FOCUS
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100890 if(sort_by != 1 && sort_by != 2) { // sort by auto focus mean
Marc Kupietz30ca4342017-11-22 21:21:20 +0100891 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) -1);
Marc Kupietzbd41da32017-11-24 10:26:43 +0100892 int wpos;
Marc Kupietz4116b432017-12-06 14:15:32 +0100893 int bits_set = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100894 for(i=0; i < found_index; i++) {
Marc Kupietz4116b432017-12-06 14:15:32 +0100895 best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100896 for(w=1; w < (1 << syn_threads); w++) { // loop through all possible windows
Marc Kupietz4116b432017-12-06 14:15:32 +0100897 float word_window_sum = 0, word_window_average=0, word_cprobability_sum=0, word_activation_sum = 0, total_window_sum = 0;
898 bits_set = 0;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100899 for(a=0; a < syn_threads; a++) {
Marc Kupietz30ca4342017-11-22 21:21:20 +0100900 if((1 << a) & w) {
Marc Kupietzbd41da32017-11-24 10:26:43 +0100901 wpos = (a >= window? a+1 : a);
902 total_window_sum += window_sums[wpos];
Marc Kupietz30ca4342017-11-22 21:21:20 +0100903 }
904 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100905// printf("%d window-sum %f\n", w, total_window_sum);
906 for(a=0; a < syn_threads; a++) {
907 if((1 << a) & w) {
908 wpos = (a >= window? a+1 : a);
909 bits_set++;
910 for(b=0; b < syn_nbs[a]->length; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100911 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
Marc Kupietza77acce2017-11-30 16:59:07 +0100912// float acti = syn_nbs[a]->best[b].activation / total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100913// 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 +0100914// word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
915// 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 +0100916
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100917 word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
Marc Kupietza77acce2017-11-30 16:59:07 +0100918// word_window_sum += acti - (word_window_sum * acti); syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
919
Marc Kupietz4116b432017-12-06 14:15:32 +0100920 word_window_average += syn_nbs[a]->best[b].activation; // - word_window_average * syn_nbs[a]->best[b].activation; // conormalied activation sum
921 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 +0100922 word_activation_sum += syn_nbs[a]->best[b].activation;
923 if(syn_nbs[a]->best[b].activation > best[i].max_activation)
924 best[i].max_activation = syn_nbs[a]->best[b].activation;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100925 if(syn_nbs[a]->best[b].activation > best[i].heat[wpos] )
Marc Kupietz728b8ed2017-12-02 11:13:49 +0100926 best[i].heat[wpos] = syn_nbs[a]->best[b].activation;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100927 }
928 }
929 }
Marc Kupietz4116b432017-12-06 14:15:32 +0100930 if(bits_set) {
931 word_window_average /= bits_set;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100932// word_activation_sum /= bits_set;
933// word_window_sum /= bits_set;
Marc Kupietz4116b432017-12-06 14:15:32 +0100934 }
Marc Kupietza77acce2017-11-30 16:59:07 +0100935
936 word_window_sum /= total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100937
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100938 if(word_window_sum > best[i].probability) {
Marc Kupietz4116b432017-12-06 14:15:32 +0100939// best[i].position = w;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100940 best[i].probability = word_window_sum;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100941 }
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100942
Marc Kupietz4116b432017-12-06 14:15:32 +0100943 if(word_cprobability_sum > best[i].cprobability_sum) {
944 best[i].position = w;
945 best[i].cprobability_sum = word_cprobability_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100946 }
Marc Kupietz4116b432017-12-06 14:15:32 +0100947
948 best[i].average = word_window_average;
949// best[i].activation = word_activation_sum;
950 }
Marc Kupietz30ca4342017-11-22 21:21:20 +0100951 }
Marc Kupietze0e42132017-11-24 16:44:34 +0100952 qsort(best, found_index, sizeof(collocator), cmp_probability);
Marc Kupietz30ca4342017-11-22 21:21:20 +0100953// for(i=0; i < found_index; i++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100954// 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 +0100955// }
956
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100957 } else if(sort_by == 1) { // responsiveness any window position
958 int wpos;
959 for(i=0; i < found_index; i++) {
960 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
961 for(a=0; a < syn_threads; a++) {
962 wpos = (a >= window? a+1 : a);
963 for(b=0; b < syn_nbs[a]->length; b++)
964 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
965 best[i].probability += syn_nbs[a]->best[b].probability;
966 if(syn_nbs[a]->best[b].activation > 0.25)
967 best[i].position |= 1 << wpos;
968 if(syn_nbs[a]->best[b].activation > best[i].activation) {
969 best[i].activation = syn_nbs[a]->best[b].activation;
970 }
971 }
972 }
973 }
974 qsort(best, found_index, sizeof(collocator), cmp_activation);
975 } else if(sort_by == 2) { // single window position
Marc Kupietz30ca4342017-11-22 21:21:20 +0100976 for(a=1; a < syn_threads; a++) {
977 for(b=0; b < syn_nbs[a]->length; b++) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200978 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100979 if(syn_nbs[a]->best[b].activation > best[c].activation) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200980 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100981 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200982 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100983 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
984 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 +0200985 break;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100986 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200987 }
988 }
989 }
990 } else { // sort by mean p
991 for(a=1; a < syn_threads; a++) {
992 for(b=0; b < syn_nbs[a]->length; b++) {
993 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100994 if(target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200995 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100996 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200997 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100998 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
999 best[c].position = (1 << 2*window) - 1; // syn_nbs[a]->pos[b];
1000 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001001 break;
1002 }
Marc Kupietz271e2a42016-03-22 11:37:43 +01001003 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +01001004 }
1005 }
1006 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001007 array = newAV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001008 for (a = 0, i=0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
1009 long long c = best[a].wordi;
Marc Kupietza77acce2017-11-30 16:59:07 +01001010/*
Marc Kupietzd91212f2017-11-13 10:05:09 +01001011 if (dedupe) {
1012 int filtered=0;
1013 for (j=0; j<i; j++)
1014 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
1015 strcasestr(chosen[j], &vocab[c * max_w])) {
Marc Kupietza77acce2017-11-30 16:59:07 +01001016 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
1017 filtered = 1;
1018 }
Marc Kupietzd91212f2017-11-13 10:05:09 +01001019 if(filtered)
1020 continue;
1021 }
Marc Kupietza77acce2017-11-30 16:59:07 +01001022*/
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001023 chosen[i++]=c;
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001024 HV* hash = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001025 SV* word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001026 AV* heat = newAV();
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001027 if(latin_enc == 0) SvUTF8_on(word);
1028 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001029 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001030 hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001031 hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +01001032 hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +01001033 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 +01001034 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 +01001035 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +01001036 best[a].heat[5]=0;
1037 for(i=10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i]));
1038 hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV*)heat), 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001039 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietz271e2a42016-03-22 11:37:43 +01001040 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001041 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +01001042 }
Marc Kupietz000ad862016-02-26 14:59:12 +01001043end:
Marc Kupietz580ebdf2017-11-29 21:18:38 +01001044 words = old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +01001045 free(best);
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001046 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +01001047}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +01001048
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001049int dump_vecs(char *fname) {
1050 long i, j;
1051 FILE *f;
1052 /* if(words>200000) */
1053 /* words=200000; */
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001054
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001055 if((f=fopen(fname, "w")) == NULL) {
1056 fprintf(stderr, "cannot open %s for writing\n", fname);
1057 return(-1);
1058 }
1059 fprintf(f, "%lld %lld\n", words, size);
1060 for (i=0; i < words; i++) {
1061 fprintf(f, "%s ", &vocab[i * max_w]);
1062 for(j=0; j < size - 1; j++)
1063 fprintf(f, "%f ", M[i*size + j]);
1064 fprintf(f, "%f\n", M[i*size + j]);
1065 }
1066 fclose(f);
1067 return(0);
1068}
Marc Kupietzdc22b982015-10-09 09:19:34 +02001069