blob: 22dbb3a5a44e21cf1c4a582d367b1cce60aa11fd [file] [log] [blame]
Marc Kupietzdc22b982015-10-09 09:19:34 +02001#!/usr/local/bin/perl
2use Inline C;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +01003use Inline C => Config => CLEAN_AFTER_BUILD => 0, ccflags => $Config{ccflags}." -Wall -O4";
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 Kupietz793413b2016-04-02 21:48:57 +020033
Marc Kupietza5f60042017-05-04 10:38:12 +020034getopts('d:Gil:p:m:n:M:');
Marc Kupietz6ed81872016-04-27 14:04:04 +020035
36if($opt_M) {
Marc Kupietzed930212016-04-27 15:42:38 +020037 open my $handle, '<:encoding(UTF-8)', $opt_M
38 or die "Can't open '$opt_M' for reading: $!";
39 while(<$handle>) {
Marc Kupietz6ed81872016-04-27 14:04:04 +020040 foreach my $mw (split /\s+/) {
41 $marked{$mw}=1
42 }
43 }
Marc Kupietzed930212016-04-27 15:42:38 +020044 close($handle);
Marc Kupietz6ed81872016-04-27 14:04:04 +020045}
Marc Kupietza5b90152016-03-15 17:39:19 +010046
Marc Kupietz7bc85fd2016-02-24 11:42:41 +010047# -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 +010048if(!$ARGV[0]) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010049 init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0));
Marc Kupietz2cb667e2016-03-10 09:44:12 +010050} else {
Marc Kupietz6b2975c2016-03-18 21:59:33 +010051 init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0));
Marc Kupietz793413b2016-04-02 21:48:57 +020052 if(open(FILE, "$ARGV[0].args")) {
53 $training_args = <FILE>;
54 }
55 close(FILE);
Marc Kupietz2cb667e2016-03-10 09:44:12 +010056}
Marc Kupietzdc22b982015-10-09 09:19:34 +020057
Marc Kupietza2e64502016-04-27 09:53:51 +020058if($opt_m) {
59 $mergedEnd = mergeVectors($opt_m);
60}
61
Marc Kupietz6ed81872016-04-27 14:04:04 +020062
Marc Kupietz43ee87e2016-04-25 10:50:08 +020063if($opt_d) { # -d: dump vecs and exit
64 dump_vecs($opt_d);
65 exit;
66}
67
Marc Kupietza5b90152016-03-15 17:39:19 +010068my $daemon = Mojo::Server::Daemon->new(
69 app => app,
70 listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"]
71);
72
Marc Kupietz5c3887d2016-04-28 08:53:35 +020073if($opt_G) {
74 print "Filtering garbage\n";
75 filter_garbage();
76}
77
Marc Kupietz554aff52017-11-09 14:42:09 +010078get '*/js/*' => sub {
Marc Kupietzffef9302017-11-07 15:58:01 +010079 my $c = shift;
80 my $url = $c->req->url;
81 $url =~ s@/derekovecs@@g;
82 $c->app->log->info("GET: " . $url);
83 $c->reply->static($url);
84};
85
Marc Kupietz66bfd952017-12-11 09:59:45 +010086post '/derekovecs/getVecsByRanks' => sub {
87 my $self = shift;
88 my $vec = getVecs($self->req->json);
89 $self->render(json => $vec);
90};
91
Marc Kupietzdf3d4b52017-11-29 16:57:27 +010092get '*/img/*' => sub {
93 my $c = shift;
94 my $url = $c->req->url;
95 $url =~ s@/derekovecs@@g;
96 $c->app->log->info("GET: " . $url);
97 $c->reply->static($url);
98};
99
Marc Kupietzdc22b982015-10-09 09:19:34 +0200100get '/' => sub {
101 my $c = shift;
Marc Kupietza5f60042017-05-04 10:38:12 +0200102 $c->app->log->info("get: ".$c->req->url->to_abs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200103 my $word=$c->param('word');
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100104 my $no_nbs=$c->param('n') || 100;
105 my $no_iterations=$c->param('N') || 2000;
Marc Kupietzd4227392016-03-01 16:45:12 +0100106 my $perplexity=$c->param('perplexity') || 20;
Marc Kupietzc4d62f82016-03-01 11:04:24 +0100107 my $epsilon=$c->param('epsilon') || 5;
Marc Kupietzd7aea722016-03-02 11:59:12 +0100108 my $som=$c->param('som') || 0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200109 my $searchBaseVocabFirst=$c->param('sbf') || 0;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100110 my $sort=$c->param('sort') || 0;
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100111 my $csv=$c->param('csv') || 0;
Marc Kupietzb613b052016-04-28 14:11:59 +0200112 my $json=$c->param('json') || 0;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100113 my $cutoff=$c->param('cutoff') || 500000;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100114 my $dedupe=$c->param('dedupe') || 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100115 my $res;
Marc Kupietz7b2cbeb2016-02-25 11:22:00 +0100116 my @lists;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100117 my @collocations;
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100118 if(defined($word) && $word !~ /^\s*$/) {
119 $c->inactivity_timeout(300);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100120 $word =~ s/\s+/ /g;
121 for my $w (split(' *\| *', $word)) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100122 if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe}) {
Marc Kupietz15987412017-11-07 15:56:58 +0100123 $c->app->log->info("Getting $w results from cache");
Marc Kupietzd91212f2017-11-13 10:05:09 +0100124 $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe}
Marc Kupietza5b90152016-03-15 17:39:19 +0100125 } else {
Marc Kupietz15987412017-11-07 15:56:58 +0100126 $c->app->log->info('Looking for neighbours of '.$w);
127 if($opt_i) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100128 $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe);
Marc Kupietz15987412017-11-07 15:56:58 +0100129 } else {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100130 $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe);
Marc Kupietz15987412017-11-07 15:56:58 +0100131 }
Marc Kupietz2dd2dd72017-12-01 22:08:14 +0100132 $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} = $res;
Marc Kupietza5b90152016-03-15 17:39:19 +0100133 }
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100134 push(@lists, $res->{paradigmatic});
Marc Kupietz15987412017-11-07 15:56:58 +0100135 }
136 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100137 $word =~ s/ *\| */ | /g;
Marc Kupietzb613b052016-04-28 14:11:59 +0200138 if($json) {
139 return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}});
Marc Kupietzc469f3b2017-11-13 14:07:36 +0100140 } elsif($csv) {
141 my $csv_data="";
142 for (my $i=0; $i <= $no_nbs; $i++) {
143 $csv_data .= $res->{paradigmatic}->[$i]->{word} . ", ";
144 }
145 for (my $i=0; $i < $no_nbs; $i++) {
146 $csv_data .= $res->{syntagmatic}->[$i]->{word} . ", ";
147 }
148 chop $csv_data;
149 chop $csv_data;
150 $csv_data .= "\n";
151 return $c->render(text=>$csv_data);
Marc Kupietzb613b052016-04-28 14:11:59 +0200152 } else {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100153 $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 +0200154 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200155};
156
Marc Kupietz30ca4342017-11-22 21:21:20 +0100157helper(bitvec2window => sub {
158 my ($self, $n) = @_;
159 my $str = unpack("B32", pack("N", $n));
160 $str =~ s/^\d{22}//;
161 $str =~ s/^(\d{5})/$1x/;
162 $str =~ s/0/·/g;
163 $str =~ s/1/+/g;
164 return $str;
165 });
166
Marc Kupietza5b90152016-03-15 17:39:19 +0100167$daemon->run; # app->start;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200168
169exit;
170
171__END__
172
173__C__
174#include <stdio.h>
175#include <string.h>
176#include <math.h>
177#include <malloc.h>
178#include <stdlib.h> //strlen
Marc Kupietzf0809762016-02-26 10:13:47 +0100179#include <sys/mman.h>
Marc Kupietz000ad862016-02-26 14:59:12 +0100180#include <pthread.h>
Marc Kupietzdc22b982015-10-09 09:19:34 +0200181
182#define max_size 2000
183#define max_w 50
Marc Kupietz7bc85fd2016-02-24 11:42:41 +0100184#define MAX_NEIGHBOURS 1000
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100185#define MAX_WORDS -1
Marc Kupietz000ad862016-02-26 14:59:12 +0100186#define MAX_THREADS 100
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100187#define MAX_CC 50
188#define EXP_TABLE_SIZE 1000
189#define MAX_EXP 6
Marc Kupietz271e2a42016-03-22 11:37:43 +0100190#define MIN_RESP 0.50
Marc Kupietzdc22b982015-10-09 09:19:34 +0200191
192//the thread function
193void *connection_handler(void *);
Marc Kupietz000ad862016-02-26 14:59:12 +0100194
195typedef struct {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100196 long long wordi;
197 long position;
198 float activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100199 float average;
Marc Kupietza77acce2017-11-30 16:59:07 +0100200 float cprobability; // column wise probability
Marc Kupietz4116b432017-12-06 14:15:32 +0100201 float cprobability_sum;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100202 float probability;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100203 float activation_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100204 float max_activation;
Marc Kupietz4116b432017-12-06 14:15:32 +0100205 float heat[16];
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100206} collocator;
207
208typedef struct {
209 collocator *best;
Marc Kupietz80abb442016-03-23 21:04:08 +0100210 int length;
Marc Kupietz000ad862016-02-26 14:59:12 +0100211} knn;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100212
Marc Kupietz000ad862016-02-26 14:59:12 +0100213typedef struct {
Marc Kupietz48c29682016-03-19 11:30:43 +0100214 long long wordi[MAX_NEIGHBOURS];
215 char sep[MAX_NEIGHBOURS];
216 int length;
217} wordlist;
218
219typedef struct {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100220 long cutoff;
Marc Kupietz48c29682016-03-19 11:30:43 +0100221 wordlist *wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100222 char *token;
223 int N;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100224 long from;
Marc Kupietz000ad862016-02-26 14:59:12 +0100225 unsigned long upto;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100226 collocator *best;
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100227 float *target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100228 float *window_sums;
Marc Kupietz000ad862016-02-26 14:59:12 +0100229} knnpars;
230
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200231float *M, *M2=0L, *syn1neg_window, *expTable;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100232float *window_sums;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200233char *vocab;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200234char *garbage = NULL;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100235
Marc Kupietza2e64502016-04-27 09:53:51 +0200236long long words, size, merged_end;
Marc Kupietza5f60042017-05-04 10:38:12 +0200237long long merge_words = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100238int num_threads=20;
Marc Kupietza5b90152016-03-15 17:39:19 +0100239int latin_enc=0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100240int window;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200241
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100242int init_net(char *file_name, char *net_name, int latin) {
Marc Kupietz67c20282016-02-26 09:42:00 +0100243 FILE *f, *binvecs, *binwords;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100244 int binwords_fd, binvecs_fd, net_fd, i;
Marc Kupietz82b02672016-02-26 12:32:25 +0100245 long long a, b, c, d, cn;
246 float len;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200247 double val;
Marc Kupietz82b02672016-02-26 12:32:25 +0100248
Marc Kupietz67c20282016-02-26 09:42:00 +0100249 char binvecs_fname[256], binwords_fname[256];
250 strcpy(binwords_fname, file_name);
251 strcat(binwords_fname, ".words");
252 strcpy(binvecs_fname, file_name);
253 strcat(binvecs_fname, ".vecs");
Marc Kupietzdc22b982015-10-09 09:19:34 +0200254
Marc Kupietza5b90152016-03-15 17:39:19 +0100255 latin_enc = latin;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200256 f = fopen(file_name, "rb");
257 if (f == NULL) {
258 printf("Input file %s not found\n", file_name);
259 return -1;
260 }
261 fscanf(f, "%lld", &words);
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100262 if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200263 fscanf(f, "%lld", &size);
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100264 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) {
265 printf("Converting %s to memory mappable structures\n", file_name);
Marc Kupietzf0809762016-02-26 10:13:47 +0100266 vocab = (char *)malloc((long long)words * max_w * sizeof(char));
267 M = (float *)malloc((long long)words * (long long)size * sizeof(float));
268 if (M == NULL) {
269 printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size);
270 return -1;
271 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200272 if(strstr(file_name, ".txt")) {
273 for (b = 0; b < words; b++) {
274 a = 0;
275 while (1) {
276 vocab[b * max_w + a] = fgetc(f);
277 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
278 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
279 }
280 vocab[b * max_w + a] = 0;
281 len = 0;
282 for (a = 0; a < size; a++) {
283 fscanf(f, "%lf", &val);
284 M[a + b * size] = val;
285 len += val * val;
286 }
287 len = sqrt(len);
288 for (a = 0; a < size; a++) M[a + b * size] /= len;
289 }
290 } else {
291 for (b = 0; b < words; b++) {
292 a = 0;
293 while (1) {
294 vocab[b * max_w + a] = fgetc(f);
295 if (feof(f) || (vocab[b * max_w + a] == ' ')) break;
296 if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++;
297 }
298 vocab[b * max_w + a] = 0;
299 fread(&M[b * size], sizeof(float), size, f);
300 len = 0;
301 for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size];
302 len = sqrt(len);
303 for (a = 0; a < size; a++) M[a + b * size] /= len;
304 }
305 }
Marc Kupietz67c20282016-02-26 09:42:00 +0100306 if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) {
307 fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs);
308 fclose(binvecs);
309 fwrite(vocab, sizeof(char), (long long)words * max_w, binwords);
310 fclose(binwords);
311 }
Marc Kupietz2cb667e2016-03-10 09:44:12 +0100312 }
313 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
314 M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0);
315 vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0);
316 if (M == MAP_FAILED || vocab == MAP_FAILED) {
317 close(binvecs_fd);
318 close(binwords_fd);
319 fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname);
320 exit(-1);
321 }
322 } else {
323 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
324 exit(-1);
Marc Kupietz67c20282016-02-26 09:42:00 +0100325 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200326 fclose(f);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100327
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200328 if(net_name && strlen(net_name) > 0) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100329 if( (net_fd = open(net_name, O_RDONLY)) >= 0) {
330 window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2;
331 // lseek(net_fd, sizeof(float) * words * size, SEEK_SET);
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100332 // munmap(M, sizeof(float) * words * size);
333 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 +0200334 if (M2 == MAP_FAILED) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100335 close(net_fd);
336 fprintf(stderr, "Cannot mmap %s\n", net_name);
337 exit(-1);
338 }
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100339 syn1neg_window = M2 + words * size;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100340 } else {
341 fprintf(stderr, "Cannot open %s\n", net_name);
342 exit(-1);
343 }
344 fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window);
345 }
346
347 expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float));
348 for (i = 0; i < EXP_TABLE_SIZE; i++) {
349 expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table
350 expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1)
351 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100352 window_sums = malloc(sizeof(float) * (window+1) * 2);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200353 return 0;
354}
355
Marc Kupietza2e64502016-04-27 09:53:51 +0200356long mergeVectors(char *file_name){
357 FILE *f, *binvecs, *binwords;
358 int binwords_fd, binvecs_fd, net_fd, i;
359 long long a, b, c, d, cn;
360 float len;
361 float *merge_vecs;
362 char *merge_vocab;
Marc Kupietza5f60042017-05-04 10:38:12 +0200363 /* long long merge_words, merge_size; */
364 long long merge_size;
Marc Kupietza2e64502016-04-27 09:53:51 +0200365
366 char binvecs_fname[256], binwords_fname[256];
367 strcpy(binwords_fname, file_name);
368 strcat(binwords_fname, ".words");
369 strcpy(binvecs_fname, file_name);
370 strcat(binvecs_fname, ".vecs");
371
372 f = fopen(file_name, "rb");
373 if (f == NULL) {
374 printf("Input file %s not found\n", file_name);
375 exit -1;
376 }
377 fscanf(f, "%lld", &merge_words);
378 fscanf(f, "%lld", &merge_size);
379 if(merge_size != size){
380 fprintf(stderr, "vectors must have the same length\n");
381 exit(-1);
382 }
383 if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) {
384 merge_vecs = malloc(sizeof(float) * (words + merge_words) * size);
385 merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w);
386 if (merge_vecs == NULL || merge_vocab == NULL) {
387 close(binvecs_fd);
388 close(binwords_fd);
389 fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname);
390 exit(-1);
391 }
392 read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float));
393 read(binwords_fd, merge_vocab, merge_words * max_w);
394 } else {
395 fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname);
396 exit(-1);
397 }
398 printf("Successfully reallocated memory\nMerging...\n");
399 fflush(stdout);
400 memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float));
401 memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w);
402 munmap(M, words * size * sizeof(float));
403 munmap(vocab, words * max_w);
404 M = merge_vecs;
405 vocab = merge_vocab;
406 merged_end = merge_words;
407 words += merge_words;
408 fclose(f);
409 printf("merged_end: %lld, words: %lld\n", merged_end, words);
410 return((long) merged_end);
411}
412
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200413void filter_garbage() {
414 long i;
Marc Kupietzab591a82016-04-28 14:08:49 +0200415 unsigned char *w, previous, c;
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200416 garbage = malloc(words);
417 memset(garbage, 0, words);
418 for (i = 0; i < words; i++) {
419 w = vocab + i * max_w;
420 previous = 0;
Marc Kupietzab591a82016-04-28 14:08:49 +0200421 while((c = *w++) && !garbage[i]) {
422 if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) ||
423 (previous == '-' && (c & 32)) ||
Marc Kupietz33e3aa12017-11-09 16:19:38 +0100424 (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) ||
425 c == '<'
Marc Kupietzab591a82016-04-28 14:08:49 +0200426 ) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200427 garbage[i]=1;
428 continue;
429 }
430 previous = c;
431 }
432 }
433 return;
434}
435
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100436void *getCollocators(void *args) {
437 knnpars *pars = args;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100438 int N = pars->N;
439 int cc = pars->wl->wordi[0];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100440 knn *nbs = NULL;
441 long window_layer_size = size * window * 2;
442 long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target;
443 float f, max_f, maxmax_f;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100444 float *target_sums, worstbest, wpos_sum;
445 collocator *best;
Marc Kupietzd5642582016-03-19 22:23:13 +0100446
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200447 if(M2 == NULL || cc == -1)
Marc Kupietzd5642582016-03-19 22:23:13 +0100448 return NULL;
449
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100450 a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100451 best = malloc(N * sizeof(collocator));
Marc Kupietz271e2a42016-03-22 11:37:43 +0100452 worstbest = MIN_RESP;
453
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100454 for (b = 0; b < pars->cutoff; b++)
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100455 target_sums[b]=0;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100456 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100457 best[b].wordi = -1;
458 best[b].probability = 1;
459 best[b].activation = worstbest;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100460 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100461
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100462 d = cc;
463 maxmax_f = -1;
464 maxmax_target = 0;
465
Marc Kupietz271e2a42016-03-22 11:37:43 +0100466 for (a = pars->from; a < pars->upto; a++) {
467 if(a >= window)
468 a++;
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100469 wpos_sum = 0;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100470 printf("window pos: %ld\n", a);
471 if (a != window) {
472 max_f = -1;
473 window_offset = a * size;
474 if (a > window)
475 window_offset -= size;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100476 for(target = 0; target < pars->cutoff; target ++) {
477 if(garbage && garbage[target]) continue;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100478 if(target == d)
479 continue;
480 f = 0;
481 for (c = 0; c < size; c++)
Marc Kupietz10bec2b2016-03-23 09:41:31 +0100482 f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100483 if (f < -MAX_EXP)
484 continue;
485 else if (f > MAX_EXP)
486 continue;
487 else
488 f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))];
Marc Kupietzb864ccf2016-03-21 22:40:03 +0100489 wpos_sum += f;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100490
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100491 target_sums[target] += f;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100492 if(f > worstbest) {
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100493 for (b = 0; b < N; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100494 if (f > best[b].activation) {
495 memmove(best + b + 1, best + b, (N - b -1) * sizeof(collocator));
496 best[b].activation = f;
497 best[b].wordi = target;
498 best[b].position = window-a;
Marc Kupietz33679a32016-03-22 08:49:39 +0100499 break;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100500 }
501 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100502 if(b == N - 1)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100503 worstbest = best[N-1].activation;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100504 }
505 }
506 printf("%d %.2f\n", max_target, max_f);
507 printf("%s (%.2f) ", &vocab[max_target * max_w], max_f);
508 if(max_f > maxmax_f) {
509 maxmax_f = max_f;
510 maxmax_target = max_target;
511 }
Marc Kupietz33679a32016-03-22 08:49:39 +0100512 for (b = 0; b < N; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100513 if(best[b].position == window-a)
Marc Kupietza77acce2017-11-30 16:59:07 +0100514 best[b].cprobability = best[b].activation / wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100515 } else {
516 printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]);
517 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100518 pars->window_sums[a] = wpos_sum;
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100519 }
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100520 for (b = 0; b < pars->cutoff; b++)
Marc Kupietza77acce2017-11-30 16:59:07 +0100521 pars->target_sums[b] += target_sums[b]; //(target_sums[b] / wpos_sum ) / (window * 2);
522 printf("Target-Summe von 0: %f\n", pars->target_sums[150298]);
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100523 free(target_sums);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100524 for(b=0; b<N && best[b].wordi >= 0; b++);; // THIS LOOP IS NEEDED (b...)
525// 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 +0100526// printf("\n");
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100527 nbs = malloc(sizeof(knn));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100528 nbs->best = best;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100529 nbs->length = b-1;
530 pthread_exit(nbs);
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100531}
532
Marc Kupietz30ca4342017-11-22 21:21:20 +0100533
Marc Kupietz66bfd952017-12-11 09:59:45 +0100534AV *getVecs(AV *array) {
535 int i, b;
536 AV *result = newAV();
537 for (i=0; i<=av_len(array); i++) {
538 SV** elem = av_fetch(array, i, 0);
539 if (elem != NULL) {
540 long j = (long) SvNV(*elem);
541 AV *vector = newAV();
542 for (b = 0; b < size; b++) {
543 av_push(vector, newSVnv(M[b + j * size]));
544 }
545 av_push(result, newRV_noinc(vector));
546 }
547 }
548 return result;
549}
550
Marc Kupietza2e64502016-04-27 09:53:51 +0200551wordlist *getTargetWords(char *st1, int search_backw) {
Marc Kupietz48c29682016-03-19 11:30:43 +0100552 wordlist *wl = malloc(sizeof(wordlist));
553 char st[100][max_size], sep[100];
554 long a, b=0, c=0, cn=0;
Marc Kupietza2e64502016-04-27 09:53:51 +0200555 int unmerged;
556
Marc Kupietzdc22b982015-10-09 09:19:34 +0200557 while (1) {
558 st[cn][b] = st1[c];
559 b++;
560 c++;
561 st[cn][b] = 0;
562 if (st1[c] == 0) break;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100563 if (st1[c] == ' ' || st1[c] == '-') {
564 sep[cn++] = st1[c];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200565 b = 0;
566 c++;
567 }
568 }
569 cn++;
570 for (a = 0; a < cn; a++) {
Marc Kupietza2e64502016-04-27 09:53:51 +0200571 if(search_backw) {
572 for (b = words - 1; b >= 0; b--) if (!strcmp(&vocab[b * max_w], st[a])) break;
573 } else {
574 for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break;
575 }
Marc Kupietz34a3ee92016-02-27 22:43:16 +0100576 if (b == words) b = -1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100577 wl->wordi[a] = b;
578 fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], wl->wordi[a]);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200579 if (b == -1) {
Marc Kupietze8da3062016-02-25 08:37:53 +0100580 fprintf(stderr, "Out of dictionary word!\n");
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100581 cn--;
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100582 free(wl);
583 return NULL;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200584 }
585 }
Marc Kupietz48c29682016-03-19 11:30:43 +0100586 wl->length=cn;
587 return(wl);
588}
589
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100590void *_get_neighbours(void *arg) {
591 knnpars *pars = arg;
Marc Kupietz48c29682016-03-19 11:30:43 +0100592 char *st1 = pars->token;
593 int N = pars->N;
594 long from = pars -> from;
595 unsigned long upto = pars -> upto;
596 char file_name[max_size], st[100][max_size], *sep;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100597 float dist, len, vec[max_size];
598 long long a, b, c, d, cn, *bi;
Marc Kupietz48c29682016-03-19 11:30:43 +0100599 char ch;
600 knn *nbs = NULL;
601 wordlist *wl = pars->wl;
602
Marc Kupietzc55c8872017-12-01 23:27:10 +0100603 collocator *best = pars->best;
Marc Kupietz48c29682016-03-19 11:30:43 +0100604
605 float worstbest=-1;
606
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100607 for (a = 0; a < N; a++) best[a].activation = 0;
Marc Kupietz48c29682016-03-19 11:30:43 +0100608 a = 0;
609 bi = wl->wordi;
610 cn = wl->length;
611 sep = wl->sep;
612 b = bi[0];
613 c = 0;
Marc Kupietz000ad862016-02-26 14:59:12 +0100614 if (b == -1) {
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100615 N = 0;
616 goto end;
617 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200618 for (a = 0; a < size; a++) vec[a] = 0;
619 for (b = 0; b < cn; b++) {
620 if (bi[b] == -1) continue;
Marc Kupietz95aa1c02016-03-15 09:40:43 +0100621 if(b>0 && sep[b-1] == '-')
622 for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size];
623 else
624 for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size];
Marc Kupietzdc22b982015-10-09 09:19:34 +0200625 }
626 len = 0;
627 for (a = 0; a < size; a++) len += vec[a] * vec[a];
628 len = sqrt(len);
629 for (a = 0; a < size; a++) vec[a] /= len;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100630 for (a = 0; a < N; a++) best[a].activation = -1;
Marc Kupietz000ad862016-02-26 14:59:12 +0100631 for (c = from; c < upto; c++) {
Marc Kupietz5c3887d2016-04-28 08:53:35 +0200632 if(garbage && garbage[c]) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200633 a = 0;
Marc Kupietz34020dc2016-02-25 08:44:19 +0100634// do not skip taget word
Marc Kupietze8da3062016-02-25 08:37:53 +0100635// for (b = 0; b < cn; b++) if (bi[b] == c) a = 1;
636// if (a == 1) continue;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200637 dist = 0;
638 for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size];
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100639 if(dist > worstbest) {
640 for (a = 0; a < N; a++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100641 if (dist > best[a].activation) {
642 memmove(best + a + 1, best + a, (N - a -1) * sizeof(collocator));
643 best[a].activation = dist;
644 best[a].wordi = c;
Marc Kupietzbe1b9fc2016-02-26 10:34:30 +0100645 break;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200646 }
Marc Kupietzdc22b982015-10-09 09:19:34 +0200647 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100648 worstbest = best[N-1].activation;
Marc Kupietzdc22b982015-10-09 09:19:34 +0200649 }
650 }
Marc Kupietz34020dc2016-02-25 08:44:19 +0100651
Marc Kupietz44bee3c2016-02-25 16:26:29 +0100652end:
Marc Kupietz000ad862016-02-26 14:59:12 +0100653 pthread_exit(nbs);
Marc Kupietzdc22b982015-10-09 09:19:34 +0200654}
655
Marc Kupietze0e42132017-11-24 16:44:34 +0100656int cmp_activation (const void * a, const void * b) {
657 float fb = ((collocator *)a)->activation;
658 float fa = ((collocator *)b)->activation;
659 return (fa > fb) - (fa < fb);
660}
661
662int cmp_probability (const void * a, const void * b) {
663 float fb = ((collocator *)a)->probability;
664 float fa = ((collocator *)b)->probability;
665 return (fa > fb) - (fa < fb);
666}
667
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100668
Marc Kupietzd91212f2017-11-13 10:05:09 +0100669SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe) {
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100670 HV *result = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100671 float *target_sums, vec[max_size];
Marc Kupietza5f60042017-05-04 10:38:12 +0200672 long long old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100673 long a, b, c, d, slice;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100674 knn *para_nbs[MAX_THREADS];
675 knn *syn_nbs[MAX_THREADS];
Marc Kupietz000ad862016-02-26 14:59:12 +0100676 knnpars pars[MAX_THREADS];
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100677 pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t));
Marc Kupietz48c29682016-03-19 11:30:43 +0100678 wordlist *wl;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200679 int syn_threads = (M2? window * 2 : 0);
680 int para_threads = num_threads - syn_threads;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100681
Marc Kupietzc55c8872017-12-01 23:27:10 +0100682 collocator *best;
683 posix_memalign((void **) &best, 128, 10 * N * sizeof(collocator));
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100684 memset(best, 0, 10 * N * sizeof(collocator));
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100685
Marc Kupietz000ad862016-02-26 14:59:12 +0100686 if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS;
687
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100688 if(cutoff < 1)
689 cutoff=words;
690
Marc Kupietza2e64502016-04-27 09:53:51 +0200691 wl = getTargetWords(st1, search_backw);
Marc Kupietzf9ac54e2017-11-21 09:22:29 +0100692 if(wl == NULL || wl->length < 1)
Marc Kupietz271e2a42016-03-22 11:37:43 +0100693 goto end;
Marc Kupietz48c29682016-03-19 11:30:43 +0100694
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100695 old_words = cutoff;
Marc Kupietza5f60042017-05-04 10:38:12 +0200696 if(merge_words > 0)
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100697 cutoff = merge_words * 1.25; /* HACK */
698 slice = cutoff / para_threads;
Marc Kupietza5f60042017-05-04 10:38:12 +0200699
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100700 a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float));
701 for(a = 0; a < cutoff; a++)
Marc Kupietzce3d4c62016-03-23 16:11:25 +0100702 target_sums[a] = 0;
703
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200704 printf("Starting %d threads\n", para_threads);
705 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100706 for(a=0; a < para_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100707 pars[a].cutoff = cutoff;
Marc Kupietz000ad862016-02-26 14:59:12 +0100708 pars[a].token = st1;
Marc Kupietz48c29682016-03-19 11:30:43 +0100709 pars[a].wl = wl;
Marc Kupietz000ad862016-02-26 14:59:12 +0100710 pars[a].N = N;
Marc Kupietzc55c8872017-12-01 23:27:10 +0100711 pars[a].best = &best[N*a];
Marc Kupietz000ad862016-02-26 14:59:12 +0100712 pars[a].from = a*slice;
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100713 pars[a].upto = ((a+1)*slice > cutoff? cutoff:(a+1)*slice);
Marc Kupietz000ad862016-02-26 14:59:12 +0100714 pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]);
715 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200716 if(M2) {
717 for(a=0; a < syn_threads; a++) {
Marc Kupietz2c79c5e2017-11-09 16:18:40 +0100718 pars[a + para_threads].cutoff = cutoff;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200719 pars[a + para_threads].target_sums = target_sums;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100720 pars[a + para_threads].window_sums = window_sums;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200721 pars[a + para_threads].wl = wl;
722 pars[a + para_threads].N = N;
723 pars[a + para_threads].from = a;
724 pars[a + para_threads].upto = a+1;
725 pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]);
726 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100727 }
728 printf("Waiting for para threads to join\n");
729 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100730 for (a = 0; a < para_threads; a++) pthread_join(pt[a], (void *) &para_nbs[a]);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100731 printf("Para threads joint\n");
732 fflush(stdout);
Marc Kupietz000ad862016-02-26 14:59:12 +0100733
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200734 /* if(!syn_nbs[0]) */
735 /* goto end; */
Marc Kupietz000ad862016-02-26 14:59:12 +0100736
Marc Kupietzc55c8872017-12-01 23:27:10 +0100737 qsort(best, N*para_threads, sizeof(collocator), cmp_activation);
Marc Kupietz000ad862016-02-26 14:59:12 +0100738
Marc Kupietz000ad862016-02-26 14:59:12 +0100739
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100740 long long chosen[MAX_NEIGHBOURS];
741 printf("N: %ld\n", N);
742
Marc Kupietz271e2a42016-03-22 11:37:43 +0100743 AV* array = newAV();
Marc Kupietzd91212f2017-11-13 10:05:09 +0100744 int i, j;
Marc Kupietza5f60042017-05-04 10:38:12 +0200745 int l1_words=0, l2_words=0;
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100746
747 for (a = 0, i = 0; i < N && a < N*para_threads; a++) {
Marc Kupietzd91212f2017-11-13 10:05:09 +0100748 int filtered=0;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100749 long long c = best[a].wordi;
Marc Kupietzd91212f2017-11-13 10:05:09 +0100750 if (dedupe && i > 0) {
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100751 for (j=0; j<i && !filtered; j++)
752 if (strcasestr(&vocab[c * max_w], &vocab[chosen[j] * max_w]) ||
753 strcasestr(&vocab[chosen[j] * max_w], &vocab[c * max_w])) {
754 printf("filtering %s %s\n", &vocab[chosen[j] * max_w], &vocab[c * max_w]);
Marc Kupietzd91212f2017-11-13 10:05:09 +0100755 filtered = 1;
756 }
757 if(filtered)
758 continue;
759 }
Marc Kupietza5f60042017-05-04 10:38:12 +0200760 if(merge_words > 0) {
761 if(c >= merge_words) {
762 if(l1_words > N / 2)
763 continue;
764 else
765 l1_words++;
766 } else {
767 if(l2_words > N / 2)
768 continue;
769 else
770 l2_words++;
771 }
772 }
Marc Kupietza5f60042017-05-04 10:38:12 +0200773 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 +0100774 fflush(stdout);
Marc Kupietza5f60042017-05-04 10:38:12 +0200775
Marc Kupietz271e2a42016-03-22 11:37:43 +0100776 HV* hash = newHV();
Marc Kupietza5f60042017-05-04 10:38:12 +0200777 SV* word = newSVpvf(&vocab[c * max_w], 0);
Marc Kupietzd0d3e912017-12-01 22:09:43 +0100778 chosen[i] = c;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100779 if(latin_enc == 0) SvUTF8_on(word);
Marc Kupietza5f60042017-05-04 10:38:12 +0200780 fflush(stdout);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100781 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100782 hv_store(hash, "dist", strlen("dist"), newSVnv(best[a].activation), 0);
783 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100784 AV *vector = newAV();
785 for (b = 0; b < size; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100786 av_push(vector, newSVnv(M[b + best[a].wordi * size]));
Marc Kupietz6b2975c2016-03-18 21:59:33 +0100787 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100788 hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0);
789 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietza5f60042017-05-04 10:38:12 +0200790 i++;
Marc Kupietz271e2a42016-03-22 11:37:43 +0100791 }
792 hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0);
793
Marc Kupietz50485ba2016-03-23 09:13:14 +0100794 for(b=0; b < MAX_NEIGHBOURS; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100795 best[b].wordi = -1L;
796 best[b].activation = 0;
797 best[b].probability = 0;
798 best[b].position = 0;
799 best[b].activation_sum = 0;
Marc Kupietz50485ba2016-03-23 09:13:14 +0100800 }
801
Marc Kupietza77acce2017-11-30 16:59:07 +0100802 float total_activation = 0;
803
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200804 if (M2) {
805 printf("Waiting for syn threads to join\n");
806 fflush(stdout);
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100807 for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], (void *) &syn_nbs[a]);
Marc Kupietza77acce2017-11-30 16:59:07 +0100808 for (a = 0; a <= syn_threads; a++) {
809 total_activation += window_sums[a];
810 printf("window pos: %d, sum: %f\n", a, window_sums[a]);
811 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200812 printf("syn threads joint\n");
813 fflush(stdout);
Marc Kupietz50485ba2016-03-23 09:13:14 +0100814
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200815 for(b=0; b < syn_nbs[0]->length; b++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100816 memcpy(best + b, &syn_nbs[0]->best[b], sizeof(collocator));
817 best[b].position = -1; // syn_nbs[0]->pos[b];
818 best[b].activation_sum = target_sums[syn_nbs[0]->best[b].wordi];
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100819 best[b].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +0100820 best[b].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100821 best[b].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +0100822 best[b].cprobability = syn_nbs[0]->best[b].cprobability;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200823 }
Marc Kupietz30ca4342017-11-22 21:21:20 +0100824
825 float best_window_sum[MAX_NEIGHBOURS];
826 int found_index=0, i=0, j, w;
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100827 for(a=0; a < syn_threads; a++) {
828 for(b=0; b < syn_nbs[a]->length; b++) {
829 for(i=0; i < found_index; i++)
830 if(best[i].wordi == syn_nbs[a]->best[b].wordi)
831 break;
832 if(i >= found_index) {
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100833 best[found_index].max_activation = 0.0;
Marc Kupietz4116b432017-12-06 14:15:32 +0100834 best[found_index].average = 0.0;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100835 best[found_index].probability = 0.0;
Marc Kupietza77acce2017-11-30 16:59:07 +0100836 best[found_index].cprobability = syn_nbs[a]->best[b].cprobability;
837 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 +0100838 best[found_index++].wordi = syn_nbs[a]->best[b].wordi;
839 // printf("found: %s\n", &vocab[syn_nbs[a]->index[b] * max_w]);
Marc Kupietz30ca4342017-11-22 21:21:20 +0100840 }
841 }
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100842 }
Marc Kupietza77acce2017-11-30 16:59:07 +0100843 sort_by =0; // ALWAYS AUTO-FOCUS
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100844 if(sort_by != 1 && sort_by != 2) { // sort by auto focus mean
Marc Kupietz30ca4342017-11-22 21:21:20 +0100845 printf("window: %d - syn_threads: %d, %d\n", window, syn_threads, (1 << syn_threads) -1);
Marc Kupietzbd41da32017-11-24 10:26:43 +0100846 int wpos;
Marc Kupietz4116b432017-12-06 14:15:32 +0100847 int bits_set = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100848 for(i=0; i < found_index; i++) {
Marc Kupietz4116b432017-12-06 14:15:32 +0100849 best[i].activation = best[i].probability = best[i].average = best[i].cprobability_sum = 0;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100850 for(w=1; w < (1 << syn_threads); w++) { // loop through all possible windows
Marc Kupietz4116b432017-12-06 14:15:32 +0100851 float word_window_sum = 0, word_window_average=0, word_cprobability_sum=0, word_activation_sum = 0, total_window_sum = 0;
852 bits_set = 0;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100853 for(a=0; a < syn_threads; a++) {
Marc Kupietz30ca4342017-11-22 21:21:20 +0100854 if((1 << a) & w) {
Marc Kupietzbd41da32017-11-24 10:26:43 +0100855 wpos = (a >= window? a+1 : a);
856 total_window_sum += window_sums[wpos];
Marc Kupietz30ca4342017-11-22 21:21:20 +0100857 }
858 }
Marc Kupietzbd41da32017-11-24 10:26:43 +0100859// printf("%d window-sum %f\n", w, total_window_sum);
860 for(a=0; a < syn_threads; a++) {
861 if((1 << a) & w) {
862 wpos = (a >= window? a+1 : a);
863 bits_set++;
864 for(b=0; b < syn_nbs[a]->length; b++)
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100865 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
Marc Kupietza77acce2017-11-30 16:59:07 +0100866// float acti = syn_nbs[a]->best[b].activation / total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100867// 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 +0100868// word_window_sum += syn_nbs[a]->norm[b]; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
869// 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 +0100870
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100871 word_window_sum += syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
Marc Kupietza77acce2017-11-30 16:59:07 +0100872// word_window_sum += acti - (word_window_sum * acti); syn_nbs[a]->best[b].activation; // / window_sums[wpos]; // syn_nbs[a]->norm[b];
873
Marc Kupietz4116b432017-12-06 14:15:32 +0100874 word_window_average += syn_nbs[a]->best[b].activation; // - word_window_average * syn_nbs[a]->best[b].activation; // conormalied activation sum
875 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 +0100876 word_activation_sum += syn_nbs[a]->best[b].activation;
877 if(syn_nbs[a]->best[b].activation > best[i].max_activation)
878 best[i].max_activation = syn_nbs[a]->best[b].activation;
Marc Kupietzdb2dc7e2017-12-02 12:04:03 +0100879 if(syn_nbs[a]->best[b].activation > best[i].heat[wpos] )
Marc Kupietz728b8ed2017-12-02 11:13:49 +0100880 best[i].heat[wpos] = syn_nbs[a]->best[b].activation;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100881 }
882 }
883 }
Marc Kupietz4116b432017-12-06 14:15:32 +0100884 if(bits_set) {
885 word_window_average /= bits_set;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100886// word_activation_sum /= bits_set;
887// word_window_sum /= bits_set;
Marc Kupietz4116b432017-12-06 14:15:32 +0100888 }
Marc Kupietza77acce2017-11-30 16:59:07 +0100889
890 word_window_sum /= total_window_sum;
Marc Kupietzbd41da32017-11-24 10:26:43 +0100891
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100892 if(word_window_sum > best[i].probability) {
Marc Kupietz4116b432017-12-06 14:15:32 +0100893// best[i].position = w;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100894 best[i].probability = word_window_sum;
Marc Kupietz30ca4342017-11-22 21:21:20 +0100895 }
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100896
Marc Kupietz4116b432017-12-06 14:15:32 +0100897 if(word_cprobability_sum > best[i].cprobability_sum) {
898 best[i].position = w;
899 best[i].cprobability_sum = word_cprobability_sum;
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100900 }
Marc Kupietz4116b432017-12-06 14:15:32 +0100901
902 best[i].average = word_window_average;
903// best[i].activation = word_activation_sum;
904 }
Marc Kupietz30ca4342017-11-22 21:21:20 +0100905 }
Marc Kupietze0e42132017-11-24 16:44:34 +0100906 qsort(best, found_index, sizeof(collocator), cmp_probability);
Marc Kupietz30ca4342017-11-22 21:21:20 +0100907// for(i=0; i < found_index; i++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100908// 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 +0100909// }
910
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100911 } else if(sort_by == 1) { // responsiveness any window position
912 int wpos;
913 for(i=0; i < found_index; i++) {
914 float word_window_sum = 0, word_activation_sum = 0, total_window_sum = 0;
915 for(a=0; a < syn_threads; a++) {
916 wpos = (a >= window? a+1 : a);
917 for(b=0; b < syn_nbs[a]->length; b++)
918 if(best[i].wordi == syn_nbs[a]->best[b].wordi) {
919 best[i].probability += syn_nbs[a]->best[b].probability;
920 if(syn_nbs[a]->best[b].activation > 0.25)
921 best[i].position |= 1 << wpos;
922 if(syn_nbs[a]->best[b].activation > best[i].activation) {
923 best[i].activation = syn_nbs[a]->best[b].activation;
924 }
925 }
926 }
927 }
928 qsort(best, found_index, sizeof(collocator), cmp_activation);
929 } else if(sort_by == 2) { // single window position
Marc Kupietz30ca4342017-11-22 21:21:20 +0100930 for(a=1; a < syn_threads; a++) {
931 for(b=0; b < syn_nbs[a]->length; b++) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200932 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100933 if(syn_nbs[a]->best[b].activation > best[c].activation) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200934 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100935 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200936 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100937 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
938 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 +0200939 break;
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100940 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200941 }
942 }
943 }
944 } else { // sort by mean p
945 for(a=1; a < syn_threads; a++) {
946 for(b=0; b < syn_nbs[a]->length; b++) {
947 for(c=0; c < MAX_NEIGHBOURS; c++) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100948 if(target_sums[syn_nbs[a]->best[b].wordi] > best[c].activation_sum) {
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200949 for(d=MAX_NEIGHBOURS-1; d>c; d--) {
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100950 memmove(best + d, best + d - 1, sizeof(collocator));
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200951 }
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100952 memcpy(best + c, &syn_nbs[a]->best[b], sizeof(collocator));
953 best[c].position = (1 << 2*window) - 1; // syn_nbs[a]->pos[b];
954 best[c].activation_sum = target_sums[syn_nbs[a]->best[b].wordi];
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200955 break;
956 }
Marc Kupietz271e2a42016-03-22 11:37:43 +0100957 }
Marc Kupietz6d9a6782016-03-23 17:25:25 +0100958 }
959 }
960 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200961 array = newAV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100962 for (a = 0, i=0; a < MAX_NEIGHBOURS && best[a].wordi >= 0; a++) {
963 long long c = best[a].wordi;
Marc Kupietza77acce2017-11-30 16:59:07 +0100964/*
Marc Kupietzd91212f2017-11-13 10:05:09 +0100965 if (dedupe) {
966 int filtered=0;
967 for (j=0; j<i; j++)
968 if (strcasestr(&vocab[c * max_w], chosen[j]) ||
969 strcasestr(chosen[j], &vocab[c * max_w])) {
Marc Kupietza77acce2017-11-30 16:59:07 +0100970 printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]);
971 filtered = 1;
972 }
Marc Kupietzd91212f2017-11-13 10:05:09 +0100973 if(filtered)
974 continue;
975 }
Marc Kupietza77acce2017-11-30 16:59:07 +0100976*/
Marc Kupietzb6c615d2017-12-02 10:38:20 +0100977 chosen[i++]=c;
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200978 HV* hash = newHV();
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100979 SV* word = newSVpvf(&vocab[best[a].wordi * max_w], 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +0100980 AV* heat = newAV();
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200981 if(latin_enc == 0) SvUTF8_on(word);
982 hv_store(hash, "word", strlen("word"), word , 0);
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100983 hv_store(hash, "rank", strlen("rank"), newSVuv(best[a].wordi), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +0100984 hv_store(hash, "average", strlen("average"), newSVnv(best[a].average), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100985 hv_store(hash, "prob", strlen("prob"), newSVnv(best[a].probability), 0);
Marc Kupietz4116b432017-12-06 14:15:32 +0100986 hv_store(hash, "cprob", strlen("cprob"), newSVnv(best[a].cprobability_sum), 0);
Marc Kupietzd64f3f22017-11-30 12:07:42 +0100987 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 +0100988 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 +0100989 hv_store(hash, "pos", strlen("pos"), newSVnv(best[a].position), 0);
Marc Kupietzb6c615d2017-12-02 10:38:20 +0100990 best[a].heat[5]=0;
991 for(i=10; i >= 0; i--) av_push(heat, newSVnv(best[a].heat[i]));
992 hv_store(hash, "heat", strlen("heat"), newRV_noinc((SV*)heat), 0);
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200993 av_push(array, newRV_noinc((SV*)hash));
Marc Kupietz271e2a42016-03-22 11:37:43 +0100994 }
Marc Kupietz43ee87e2016-04-25 10:50:08 +0200995 hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0);
Marc Kupietz271e2a42016-03-22 11:37:43 +0100996 }
Marc Kupietz000ad862016-02-26 14:59:12 +0100997end:
Marc Kupietz580ebdf2017-11-29 21:18:38 +0100998 words = old_words;
Marc Kupietzc8fdf3c2017-11-24 15:32:19 +0100999 free(best);
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001000 return newRV_noinc((SV*)result);
Marc Kupietz000ad862016-02-26 14:59:12 +01001001}
Marc Kupietz7bc85fd2016-02-24 11:42:41 +01001002
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001003int dump_vecs(char *fname) {
1004 long i, j;
1005 FILE *f;
1006 /* if(words>200000) */
1007 /* words=200000; */
Marc Kupietz6b2975c2016-03-18 21:59:33 +01001008
Marc Kupietz43ee87e2016-04-25 10:50:08 +02001009 if((f=fopen(fname, "w")) == NULL) {
1010 fprintf(stderr, "cannot open %s for writing\n", fname);
1011 return(-1);
1012 }
1013 fprintf(f, "%lld %lld\n", words, size);
1014 for (i=0; i < words; i++) {
1015 fprintf(f, "%s ", &vocab[i * max_w]);
1016 for(j=0; j < size - 1; j++)
1017 fprintf(f, "%f ", M[i*size + j]);
1018 fprintf(f, "%f\n", M[i*size + j]);
1019 }
1020 fclose(f);
1021 return(0);
1022}
Marc Kupietzdc22b982015-10-09 09:19:34 +02001023