Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 1 | #!/usr/local/bin/perl |
| 2 | use Inline C; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 3 | use Inline C => Config => CLEAN_AFTER_BUILD => 0, ccflags => $Config{ccflags}." -O4"; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 4 | #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 Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 6 | use Mojolicious::Lite; |
Marc Kupietz | c489336 | 2016-02-25 08:04:46 +0100 | [diff] [blame] | 7 | use Mojo::JSON qw(decode_json encode_json to_json); |
Marc Kupietz | 247500f | 2015-10-09 11:29:01 +0200 | [diff] [blame] | 8 | use Encode qw(decode encode); |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 9 | use Getopt::Std; |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 10 | use Mojo::Server::Daemon; |
Marc Kupietz | ffef930 | 2017-11-07 15:58:01 +0100 | [diff] [blame] | 11 | use Cwd; |
| 12 | app->static->paths->[0] = getcwd; |
| 13 | |
Marc Kupietz | d422739 | 2016-03-01 16:45:12 +0100 | [diff] [blame] | 14 | plugin 'Log::Access'; |
Marc Kupietz | b3422c1 | 2017-07-04 14:12:11 +0200 | [diff] [blame] | 15 | plugin "RequestBase"; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 16 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 17 | our $opt_i = 0; # latin1-input? |
| 18 | our $opt_l = undef; |
| 19 | our $opt_p = 5676; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 20 | our $opt_m; |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 21 | our $opt_M; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 22 | our $opt_n = ''; |
| 23 | our $opt_d; |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 24 | our $opt_G; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 25 | |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 26 | my %marked; |
Marc Kupietz | 793413b | 2016-04-02 21:48:57 +0200 | [diff] [blame] | 27 | my $training_args=""; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 28 | my $mergedEnd=0; |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 29 | my %cache; |
Marc Kupietz | 793413b | 2016-04-02 21:48:57 +0200 | [diff] [blame] | 30 | |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 31 | getopts('d:Gil:p:m:n:M:'); |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 32 | |
| 33 | if($opt_M) { |
Marc Kupietz | ed93021 | 2016-04-27 15:42:38 +0200 | [diff] [blame] | 34 | open my $handle, '<:encoding(UTF-8)', $opt_M |
| 35 | or die "Can't open '$opt_M' for reading: $!"; |
| 36 | while(<$handle>) { |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 37 | foreach my $mw (split /\s+/) { |
| 38 | $marked{$mw}=1 |
| 39 | } |
| 40 | } |
Marc Kupietz | ed93021 | 2016-04-27 15:42:38 +0200 | [diff] [blame] | 41 | close($handle); |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 42 | } |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 43 | |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 44 | # -cbow 1 -size 200 -window 8 -negative 25 -hs 0 -sample 1e-4 -threads 40 -binary 1 -iter 15 |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 45 | if(!$ARGV[0]) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 46 | init_net("vectors15.bin", $opt_n, ($opt_i? 1 : 0)); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 47 | } else { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 48 | init_net($ARGV[0], $opt_n, ($opt_i? 1 : 0)); |
Marc Kupietz | 793413b | 2016-04-02 21:48:57 +0200 | [diff] [blame] | 49 | if(open(FILE, "$ARGV[0].args")) { |
| 50 | $training_args = <FILE>; |
| 51 | } |
| 52 | close(FILE); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 53 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 54 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 55 | if($opt_m) { |
| 56 | $mergedEnd = mergeVectors($opt_m); |
| 57 | } |
| 58 | |
Marc Kupietz | 6ed8187 | 2016-04-27 14:04:04 +0200 | [diff] [blame] | 59 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 60 | if($opt_d) { # -d: dump vecs and exit |
| 61 | dump_vecs($opt_d); |
| 62 | exit; |
| 63 | } |
| 64 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 65 | my $daemon = Mojo::Server::Daemon->new( |
| 66 | app => app, |
| 67 | listen => ['http://'.($opt_l ? $opt_l : '*').":$opt_p"] |
| 68 | ); |
| 69 | |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 70 | if($opt_G) { |
| 71 | print "Filtering garbage\n"; |
| 72 | filter_garbage(); |
| 73 | } |
| 74 | |
Marc Kupietz | 554aff5 | 2017-11-09 14:42:09 +0100 | [diff] [blame] | 75 | get '*/js/*' => sub { |
Marc Kupietz | ffef930 | 2017-11-07 15:58:01 +0100 | [diff] [blame] | 76 | my $c = shift; |
| 77 | my $url = $c->req->url; |
| 78 | $url =~ s@/derekovecs@@g; |
| 79 | $c->app->log->info("GET: " . $url); |
| 80 | $c->reply->static($url); |
| 81 | }; |
| 82 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 83 | get '/' => sub { |
| 84 | my $c = shift; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 85 | $c->app->log->info("get: ".$c->req->url->to_abs); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 86 | my $word=$c->param('word'); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 87 | my $no_nbs=$c->param('n') || 100; |
| 88 | my $no_iterations=$c->param('N') || 2000; |
Marc Kupietz | d422739 | 2016-03-01 16:45:12 +0100 | [diff] [blame] | 89 | my $perplexity=$c->param('perplexity') || 20; |
Marc Kupietz | c4d62f8 | 2016-03-01 11:04:24 +0100 | [diff] [blame] | 90 | my $epsilon=$c->param('epsilon') || 5; |
Marc Kupietz | d7aea72 | 2016-03-02 11:59:12 +0100 | [diff] [blame] | 91 | my $som=$c->param('som') || 0; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 92 | my $searchBaseVocabFirst=$c->param('sbf') || 0; |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 93 | my $sort=$c->param('sort') || 0; |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 94 | my $json=$c->param('json') || 0; |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 95 | my $cutoff=$c->param('cutoff') || 1000000; |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame^] | 96 | my $dedupe=$c->param('dedupe') || 0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 97 | my $res; |
Marc Kupietz | 7b2cbeb | 2016-02-25 11:22:00 +0100 | [diff] [blame] | 98 | my @lists; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 99 | my @collocations; |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 100 | if(defined($word) && $word !~ /^\s*$/) { |
| 101 | $c->inactivity_timeout(300); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 102 | $word =~ s/\s+/ /g; |
| 103 | for my $w (split(' *\| *', $word)) { |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame^] | 104 | if ($cache{$w.$cutoff.$no_nbs.$sort.$dedupe}) { |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 105 | $c->app->log->info("Getting $w results from cache"); |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame^] | 106 | $res = $cache{$w.$cutoff.$no_nbs.$sort.$dedupe} |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 107 | } else { |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 108 | $c->app->log->info('Looking for neighbours of '.$w); |
| 109 | if($opt_i) { |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame^] | 110 | $res = get_neighbours(encode("iso-8859-1", $w), $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe); |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 111 | } else { |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame^] | 112 | $res = get_neighbours($w, $no_nbs, $sort, $searchBaseVocabFirst, $cutoff, $dedupe); |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 113 | } |
| 114 | $cache{$w} = $res; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 115 | } |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 116 | push(@lists, $res->{paradigmatic}); |
Marc Kupietz | 1598741 | 2017-11-07 15:56:58 +0100 | [diff] [blame] | 117 | } |
| 118 | } |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 119 | $word =~ s/ *\| */ | /g; |
Marc Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 120 | if($json) { |
| 121 | return $c->render(json => {word => $word, list => \@lists, collocators=>$res->{syntagmatic}}); |
| 122 | } else { |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame^] | 123 | $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 Kupietz | b613b05 | 2016-04-28 14:11:59 +0200 | [diff] [blame] | 124 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 125 | }; |
| 126 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 127 | $daemon->run; # app->start; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 128 | |
| 129 | exit; |
| 130 | |
| 131 | __END__ |
| 132 | |
| 133 | __C__ |
| 134 | #include <stdio.h> |
| 135 | #include <string.h> |
| 136 | #include <math.h> |
| 137 | #include <malloc.h> |
| 138 | #include <stdlib.h> //strlen |
Marc Kupietz | f080976 | 2016-02-26 10:13:47 +0100 | [diff] [blame] | 139 | #include <sys/mman.h> |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 140 | #include <pthread.h> |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 141 | |
| 142 | #define max_size 2000 |
| 143 | #define max_w 50 |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 144 | #define MAX_NEIGHBOURS 1000 |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 145 | #define MAX_WORDS -1 |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 146 | #define MAX_THREADS 100 |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 147 | #define MAX_CC 50 |
| 148 | #define EXP_TABLE_SIZE 1000 |
| 149 | #define MAX_EXP 6 |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 150 | #define MIN_RESP 0.50 |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 151 | |
| 152 | //the thread function |
| 153 | void *connection_handler(void *); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 154 | |
| 155 | typedef struct { |
| 156 | long long *index; |
| 157 | float *dist; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 158 | float *norm; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 159 | long long *pos; |
Marc Kupietz | 80abb44 | 2016-03-23 21:04:08 +0100 | [diff] [blame] | 160 | int length; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 161 | } knn; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 162 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 163 | typedef struct { |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 164 | long long wordi[MAX_NEIGHBOURS]; |
| 165 | char sep[MAX_NEIGHBOURS]; |
| 166 | int length; |
| 167 | } wordlist; |
| 168 | |
| 169 | typedef struct { |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 170 | long cutoff; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 171 | wordlist *wl; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 172 | char *token; |
| 173 | int N; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 174 | long from; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 175 | unsigned long upto; |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 176 | float *target_sums; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 177 | } knnpars; |
| 178 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 179 | float *M, *M2=0L, *syn1neg_window, *expTable; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 180 | char *vocab; |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 181 | char *garbage = NULL; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 182 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 183 | long long words, size, merged_end; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 184 | long long merge_words = 0; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 185 | int num_threads=20; |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 186 | int latin_enc=0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 187 | int window; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 188 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 189 | int init_net(char *file_name, char *net_name, int latin) { |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 190 | FILE *f, *binvecs, *binwords; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 191 | int binwords_fd, binvecs_fd, net_fd, i; |
Marc Kupietz | 82b0267 | 2016-02-26 12:32:25 +0100 | [diff] [blame] | 192 | long long a, b, c, d, cn; |
| 193 | float len; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 194 | double val; |
Marc Kupietz | 82b0267 | 2016-02-26 12:32:25 +0100 | [diff] [blame] | 195 | |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 196 | char binvecs_fname[256], binwords_fname[256]; |
| 197 | strcpy(binwords_fname, file_name); |
| 198 | strcat(binwords_fname, ".words"); |
| 199 | strcpy(binvecs_fname, file_name); |
| 200 | strcat(binvecs_fname, ".vecs"); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 201 | |
Marc Kupietz | a5b9015 | 2016-03-15 17:39:19 +0100 | [diff] [blame] | 202 | latin_enc = latin; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 203 | f = fopen(file_name, "rb"); |
| 204 | if (f == NULL) { |
| 205 | printf("Input file %s not found\n", file_name); |
| 206 | return -1; |
| 207 | } |
| 208 | fscanf(f, "%lld", &words); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 209 | if(MAX_WORDS > 0 && words > MAX_WORDS) words = MAX_WORDS; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 210 | fscanf(f, "%lld", &size); |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 211 | if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) < 0 || (binwords_fd = open(binwords_fname, O_RDONLY)) < 0) { |
| 212 | printf("Converting %s to memory mappable structures\n", file_name); |
Marc Kupietz | f080976 | 2016-02-26 10:13:47 +0100 | [diff] [blame] | 213 | vocab = (char *)malloc((long long)words * max_w * sizeof(char)); |
| 214 | M = (float *)malloc((long long)words * (long long)size * sizeof(float)); |
| 215 | if (M == NULL) { |
| 216 | printf("Cannot allocate memory: %lld MB %lld %lld\n", (long long)words * size * sizeof(float) / 1048576, words, size); |
| 217 | return -1; |
| 218 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 219 | if(strstr(file_name, ".txt")) { |
| 220 | for (b = 0; b < words; b++) { |
| 221 | a = 0; |
| 222 | while (1) { |
| 223 | vocab[b * max_w + a] = fgetc(f); |
| 224 | if (feof(f) || (vocab[b * max_w + a] == ' ')) break; |
| 225 | if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++; |
| 226 | } |
| 227 | vocab[b * max_w + a] = 0; |
| 228 | len = 0; |
| 229 | for (a = 0; a < size; a++) { |
| 230 | fscanf(f, "%lf", &val); |
| 231 | M[a + b * size] = val; |
| 232 | len += val * val; |
| 233 | } |
| 234 | len = sqrt(len); |
| 235 | for (a = 0; a < size; a++) M[a + b * size] /= len; |
| 236 | } |
| 237 | } else { |
| 238 | for (b = 0; b < words; b++) { |
| 239 | a = 0; |
| 240 | while (1) { |
| 241 | vocab[b * max_w + a] = fgetc(f); |
| 242 | if (feof(f) || (vocab[b * max_w + a] == ' ')) break; |
| 243 | if ((a < max_w) && (vocab[b * max_w + a] != '\n')) a++; |
| 244 | } |
| 245 | vocab[b * max_w + a] = 0; |
| 246 | fread(&M[b * size], sizeof(float), size, f); |
| 247 | len = 0; |
| 248 | for (a = 0; a < size; a++) len += M[a + b * size] * M[a + b * size]; |
| 249 | len = sqrt(len); |
| 250 | for (a = 0; a < size; a++) M[a + b * size] /= len; |
| 251 | } |
| 252 | } |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 253 | if( (binvecs = fopen(binvecs_fname, "wb")) != NULL && (binwords = fopen(binwords_fname, "wb")) != NULL) { |
| 254 | fwrite(M, sizeof(float), (long long)words * (long long)size, binvecs); |
| 255 | fclose(binvecs); |
| 256 | fwrite(vocab, sizeof(char), (long long)words * max_w, binwords); |
| 257 | fclose(binwords); |
| 258 | } |
Marc Kupietz | 2cb667e | 2016-03-10 09:44:12 +0100 | [diff] [blame] | 259 | } |
| 260 | if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) { |
| 261 | M = mmap(0, sizeof(float) * (long long)words * (long long)size, PROT_READ, MAP_SHARED, binvecs_fd, 0); |
| 262 | vocab = mmap(0, sizeof(char) * (long long)words * max_w, PROT_READ, MAP_SHARED, binwords_fd, 0); |
| 263 | if (M == MAP_FAILED || vocab == MAP_FAILED) { |
| 264 | close(binvecs_fd); |
| 265 | close(binwords_fd); |
| 266 | fprintf(stderr, "Cannot mmap %s or %s\n", binwords_fname, binvecs_fname); |
| 267 | exit(-1); |
| 268 | } |
| 269 | } else { |
| 270 | fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname); |
| 271 | exit(-1); |
Marc Kupietz | 67c2028 | 2016-02-26 09:42:00 +0100 | [diff] [blame] | 272 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 273 | fclose(f); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 274 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 275 | if(net_name && strlen(net_name) > 0) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 276 | if( (net_fd = open(net_name, O_RDONLY)) >= 0) { |
| 277 | window = (lseek(net_fd, 0, SEEK_END) - sizeof(float) * words * size) / words / size / sizeof(float) / 2; |
| 278 | // lseek(net_fd, sizeof(float) * words * size, SEEK_SET); |
Marc Kupietz | 10bec2b | 2016-03-23 09:41:31 +0100 | [diff] [blame] | 279 | // munmap(M, sizeof(float) * words * size); |
| 280 | M2 = mmap(0, sizeof(float) * words * size + sizeof(float) * 2 * window * size * words, PROT_READ, MAP_SHARED, net_fd, 0); |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 281 | if (M2 == MAP_FAILED) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 282 | close(net_fd); |
| 283 | fprintf(stderr, "Cannot mmap %s\n", net_name); |
| 284 | exit(-1); |
| 285 | } |
Marc Kupietz | 10bec2b | 2016-03-23 09:41:31 +0100 | [diff] [blame] | 286 | syn1neg_window = M2 + words * size; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 287 | } else { |
| 288 | fprintf(stderr, "Cannot open %s\n", net_name); |
| 289 | exit(-1); |
| 290 | } |
| 291 | fprintf(stderr, "Successfully memmaped %s. Determined window size: %d\n", net_name, window); |
| 292 | } |
| 293 | |
| 294 | expTable = (float *) malloc((EXP_TABLE_SIZE + 1) * sizeof(float)); |
| 295 | for (i = 0; i < EXP_TABLE_SIZE; i++) { |
| 296 | expTable[i] = exp((i / (float) EXP_TABLE_SIZE * 2 - 1) * MAX_EXP); // Precompute the exp() table |
| 297 | expTable[i] = expTable[i] / (expTable[i] + 1); // Precompute f(x) = x / (x + 1) |
| 298 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 302 | long mergeVectors(char *file_name){ |
| 303 | FILE *f, *binvecs, *binwords; |
| 304 | int binwords_fd, binvecs_fd, net_fd, i; |
| 305 | long long a, b, c, d, cn; |
| 306 | float len; |
| 307 | float *merge_vecs; |
| 308 | char *merge_vocab; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 309 | /* long long merge_words, merge_size; */ |
| 310 | long long merge_size; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 311 | |
| 312 | char binvecs_fname[256], binwords_fname[256]; |
| 313 | strcpy(binwords_fname, file_name); |
| 314 | strcat(binwords_fname, ".words"); |
| 315 | strcpy(binvecs_fname, file_name); |
| 316 | strcat(binvecs_fname, ".vecs"); |
| 317 | |
| 318 | f = fopen(file_name, "rb"); |
| 319 | if (f == NULL) { |
| 320 | printf("Input file %s not found\n", file_name); |
| 321 | exit -1; |
| 322 | } |
| 323 | fscanf(f, "%lld", &merge_words); |
| 324 | fscanf(f, "%lld", &merge_size); |
| 325 | if(merge_size != size){ |
| 326 | fprintf(stderr, "vectors must have the same length\n"); |
| 327 | exit(-1); |
| 328 | } |
| 329 | if( (binvecs_fd = open(binvecs_fname, O_RDONLY)) >= 0 && (binwords_fd = open(binwords_fname, O_RDONLY)) >= 0) { |
| 330 | merge_vecs = malloc(sizeof(float) * (words + merge_words) * size); |
| 331 | merge_vocab = malloc(sizeof(char) * (words + merge_words) * max_w); |
| 332 | if (merge_vecs == NULL || merge_vocab == NULL) { |
| 333 | close(binvecs_fd); |
| 334 | close(binwords_fd); |
| 335 | fprintf(stderr, "Cannot reserve memory for %s or %s\n", binwords_fname, binvecs_fname); |
| 336 | exit(-1); |
| 337 | } |
| 338 | read(binvecs_fd, merge_vecs, merge_words * size * sizeof(float)); |
| 339 | read(binwords_fd, merge_vocab, merge_words * max_w); |
| 340 | } else { |
| 341 | fprintf(stderr, "Cannot open %s or %s\n", binwords_fname, binvecs_fname); |
| 342 | exit(-1); |
| 343 | } |
| 344 | printf("Successfully reallocated memory\nMerging...\n"); |
| 345 | fflush(stdout); |
| 346 | memcpy(merge_vecs + merge_words * size, M, words * size * sizeof(float)); |
| 347 | memcpy(merge_vocab + merge_words * max_w, vocab, words * max_w); |
| 348 | munmap(M, words * size * sizeof(float)); |
| 349 | munmap(vocab, words * max_w); |
| 350 | M = merge_vecs; |
| 351 | vocab = merge_vocab; |
| 352 | merged_end = merge_words; |
| 353 | words += merge_words; |
| 354 | fclose(f); |
| 355 | printf("merged_end: %lld, words: %lld\n", merged_end, words); |
| 356 | return((long) merged_end); |
| 357 | } |
| 358 | |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 359 | void filter_garbage() { |
| 360 | long i; |
Marc Kupietz | ab591a8 | 2016-04-28 14:08:49 +0200 | [diff] [blame] | 361 | unsigned char *w, previous, c; |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 362 | garbage = malloc(words); |
| 363 | memset(garbage, 0, words); |
| 364 | for (i = 0; i < words; i++) { |
| 365 | w = vocab + i * max_w; |
| 366 | previous = 0; |
Marc Kupietz | ab591a8 | 2016-04-28 14:08:49 +0200 | [diff] [blame] | 367 | while((c = *w++) && !garbage[i]) { |
| 368 | if( ((c <= 90 && c >= 65) && (previous >= 97 && previous <= 122)) || |
| 369 | (previous == '-' && (c & 32)) || |
Marc Kupietz | 33e3aa1 | 2017-11-09 16:19:38 +0100 | [diff] [blame] | 370 | (previous == 0xc2 && (c == 0xa4 || c == 0xb6 )) || |
| 371 | c == '<' |
Marc Kupietz | ab591a8 | 2016-04-28 14:08:49 +0200 | [diff] [blame] | 372 | ) { |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 373 | garbage[i]=1; |
| 374 | continue; |
| 375 | } |
| 376 | previous = c; |
| 377 | } |
| 378 | } |
| 379 | return; |
| 380 | } |
| 381 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 382 | void *getCollocators(knnpars *pars) { |
| 383 | int N = pars->N; |
| 384 | int cc = pars->wl->wordi[0]; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 385 | knn *nbs = NULL; |
| 386 | long window_layer_size = size * window * 2; |
| 387 | long a, b, c, d, e, window_offset, target, max_target=0, maxmax_target; |
| 388 | float f, max_f, maxmax_f; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 389 | float *target_sums, *bestf, *bestn, worstbest, wpos_sum; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 390 | long long *besti, *bestp; |
Marc Kupietz | d564258 | 2016-03-19 22:23:13 +0100 | [diff] [blame] | 391 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 392 | if(M2 == NULL || cc == -1) |
Marc Kupietz | d564258 | 2016-03-19 22:23:13 +0100 | [diff] [blame] | 393 | return NULL; |
| 394 | |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 395 | a = posix_memalign((void **) &target_sums, 128, pars->cutoff * sizeof(float)); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 396 | besti = malloc(N * sizeof(long long)); |
| 397 | bestp = malloc(N * sizeof(long long)); |
| 398 | bestf = malloc(N * sizeof(float)); |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 399 | bestn = malloc(N * sizeof(float)); |
| 400 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 401 | worstbest = MIN_RESP; |
| 402 | |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 403 | for (b = 0; b < pars->cutoff; b++) |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 404 | target_sums[b]=0; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 405 | for (b = 0; b < N; b++) { |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 406 | besti[b] = -1; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 407 | bestn[b] = 1; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 408 | bestf[b] = worstbest; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 409 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 410 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 411 | d = cc; |
| 412 | maxmax_f = -1; |
| 413 | maxmax_target = 0; |
| 414 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 415 | for (a = pars->from; a < pars->upto; a++) { |
| 416 | if(a >= window) |
| 417 | a++; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 418 | wpos_sum = 0; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 419 | printf("window pos: %ld\n", a); |
| 420 | if (a != window) { |
| 421 | max_f = -1; |
| 422 | window_offset = a * size; |
| 423 | if (a > window) |
| 424 | window_offset -= size; |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 425 | for(target = 0; target < pars->cutoff; target ++) { |
| 426 | if(garbage && garbage[target]) continue; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 427 | if(target == d) |
| 428 | continue; |
| 429 | f = 0; |
| 430 | for (c = 0; c < size; c++) |
Marc Kupietz | 10bec2b | 2016-03-23 09:41:31 +0100 | [diff] [blame] | 431 | f += M2[d* size + c] * syn1neg_window[target * window_layer_size + window_offset + c]; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 432 | if (f < -MAX_EXP) |
| 433 | continue; |
| 434 | else if (f > MAX_EXP) |
| 435 | continue; |
| 436 | else |
| 437 | f = expTable[(int) ((f + MAX_EXP) * (EXP_TABLE_SIZE / MAX_EXP / 2))]; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 438 | wpos_sum += f; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 439 | |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 440 | target_sums[target] += f; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 441 | if(f > worstbest) { |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 442 | for (b = 0; b < N; b++) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 443 | if (f > bestf[b]) { |
Marc Kupietz | 33679a3 | 2016-03-22 08:49:39 +0100 | [diff] [blame] | 444 | memmove(bestf + b + 1, bestf + b, (N - b -1) * sizeof(float)); |
| 445 | memmove(besti + b + 1, besti + b, (N - b -1) * sizeof(long long)); |
| 446 | memmove(bestp + b + 1, bestp + b, (N - b -1) * sizeof(long long)); |
| 447 | bestf[b] = f; |
| 448 | besti[b] = target; |
| 449 | bestp[b] = window-a; |
| 450 | break; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 451 | } |
| 452 | } |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 453 | if(b == N - 1) |
| 454 | worstbest = bestf[N-1]; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | printf("%d %.2f\n", max_target, max_f); |
| 458 | printf("%s (%.2f) ", &vocab[max_target * max_w], max_f); |
| 459 | if(max_f > maxmax_f) { |
| 460 | maxmax_f = max_f; |
| 461 | maxmax_target = max_target; |
| 462 | } |
Marc Kupietz | 33679a3 | 2016-03-22 08:49:39 +0100 | [diff] [blame] | 463 | for (b = 0; b < N; b++) |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 464 | if(bestp[b] == window-a) |
| 465 | bestn[b] = bestf[b] / wpos_sum; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 466 | } else { |
| 467 | printf("\x1b[1m%s\x1b[0m ", &vocab[d*max_w]); |
| 468 | } |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 469 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 470 | } |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 471 | for (b = 0; b < pars->cutoff; b++) |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 472 | pars->target_sums[b] += (target_sums[b] / wpos_sum ) / (window * 2); |
| 473 | free(target_sums); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 474 | for(b=0; b<N && besti[b] >= 0; b++) // THIS LOOP IS NEEDED (b...) |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 475 | printf("%s %.2f %d * ", &vocab[besti[b]*max_w], bestf[b], bestp[b]); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 476 | printf("\n"); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 477 | nbs = malloc(sizeof(knn)); |
| 478 | nbs->index = besti; |
| 479 | nbs->dist = bestf; |
Marc Kupietz | b864ccf | 2016-03-21 22:40:03 +0100 | [diff] [blame] | 480 | nbs->norm = bestn; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 481 | nbs->pos = bestp; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 482 | nbs->length = b-1; |
| 483 | pthread_exit(nbs); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 484 | } |
| 485 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 486 | wordlist *getTargetWords(char *st1, int search_backw) { |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 487 | wordlist *wl = malloc(sizeof(wordlist)); |
| 488 | char st[100][max_size], sep[100]; |
| 489 | long a, b=0, c=0, cn=0; |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 490 | int unmerged; |
| 491 | |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 492 | while (1) { |
| 493 | st[cn][b] = st1[c]; |
| 494 | b++; |
| 495 | c++; |
| 496 | st[cn][b] = 0; |
| 497 | if (st1[c] == 0) break; |
Marc Kupietz | 95aa1c0 | 2016-03-15 09:40:43 +0100 | [diff] [blame] | 498 | if (st1[c] == ' ' || st1[c] == '-') { |
| 499 | sep[cn++] = st1[c]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 500 | b = 0; |
| 501 | c++; |
| 502 | } |
| 503 | } |
| 504 | cn++; |
| 505 | for (a = 0; a < cn; a++) { |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 506 | if(search_backw) { |
| 507 | for (b = words - 1; b >= 0; b--) if (!strcmp(&vocab[b * max_w], st[a])) break; |
| 508 | } else { |
| 509 | for (b = 0; b < words; b++) if (!strcmp(&vocab[b * max_w], st[a])) break; |
| 510 | } |
Marc Kupietz | 34a3ee9 | 2016-02-27 22:43:16 +0100 | [diff] [blame] | 511 | if (b == words) b = -1; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 512 | wl->wordi[a] = b; |
| 513 | fprintf(stderr, "Word: \"%s\" Position in vocabulary: %lld\n", st[a], wl->wordi[a]); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 514 | if (b == -1) { |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 515 | fprintf(stderr, "Out of dictionary word!\n"); |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 516 | cn--; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 517 | break; |
| 518 | } |
| 519 | } |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 520 | wl->length=cn; |
| 521 | return(wl); |
| 522 | } |
| 523 | |
| 524 | void *_get_neighbours(knnpars *pars) { |
| 525 | char *st1 = pars->token; |
| 526 | int N = pars->N; |
| 527 | long from = pars -> from; |
| 528 | unsigned long upto = pars -> upto; |
| 529 | char file_name[max_size], st[100][max_size], *sep; |
| 530 | float dist, len, *bestd, vec[max_size]; |
| 531 | long long a, b, c, d, cn, *bi, *besti; |
| 532 | char ch; |
| 533 | knn *nbs = NULL; |
| 534 | wordlist *wl = pars->wl; |
| 535 | |
| 536 | besti = malloc(N * sizeof(long long)); |
| 537 | bestd = malloc(N * sizeof(float)); |
| 538 | |
| 539 | float worstbest=-1; |
| 540 | |
| 541 | for (a = 0; a < N; a++) bestd[a] = 0; |
| 542 | a = 0; |
| 543 | bi = wl->wordi; |
| 544 | cn = wl->length; |
| 545 | sep = wl->sep; |
| 546 | b = bi[0]; |
| 547 | c = 0; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 548 | if (b == -1) { |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 549 | N = 0; |
| 550 | goto end; |
| 551 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 552 | for (a = 0; a < size; a++) vec[a] = 0; |
| 553 | for (b = 0; b < cn; b++) { |
| 554 | if (bi[b] == -1) continue; |
Marc Kupietz | 95aa1c0 | 2016-03-15 09:40:43 +0100 | [diff] [blame] | 555 | if(b>0 && sep[b-1] == '-') |
| 556 | for (a = 0; a < size; a++) vec[a] -= M[a + bi[b] * size]; |
| 557 | else |
| 558 | for (a = 0; a < size; a++) vec[a] += M[a + bi[b] * size]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 559 | } |
| 560 | len = 0; |
| 561 | for (a = 0; a < size; a++) len += vec[a] * vec[a]; |
| 562 | len = sqrt(len); |
| 563 | for (a = 0; a < size; a++) vec[a] /= len; |
| 564 | for (a = 0; a < N; a++) bestd[a] = -1; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 565 | for (c = from; c < upto; c++) { |
Marc Kupietz | 5c3887d | 2016-04-28 08:53:35 +0200 | [diff] [blame] | 566 | if(garbage && garbage[c]) continue; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 567 | a = 0; |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 568 | // do not skip taget word |
Marc Kupietz | e8da306 | 2016-02-25 08:37:53 +0100 | [diff] [blame] | 569 | // for (b = 0; b < cn; b++) if (bi[b] == c) a = 1; |
| 570 | // if (a == 1) continue; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 571 | dist = 0; |
| 572 | for (a = 0; a < size; a++) dist += vec[a] * M[a + c * size]; |
Marc Kupietz | be1b9fc | 2016-02-26 10:34:30 +0100 | [diff] [blame] | 573 | if(dist > worstbest) { |
| 574 | for (a = 0; a < N; a++) { |
| 575 | if (dist > bestd[a]) { |
Marc Kupietz | 33679a3 | 2016-03-22 08:49:39 +0100 | [diff] [blame] | 576 | memmove(bestd + a + 1, bestd + a, (N - a -1) * sizeof(float)); |
| 577 | memmove(besti + a + 1, besti + a, (N - a -1) * sizeof(long long)); |
Marc Kupietz | be1b9fc | 2016-02-26 10:34:30 +0100 | [diff] [blame] | 578 | bestd[a] = dist; |
| 579 | besti[a] = c; |
| 580 | break; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 581 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 582 | } |
Marc Kupietz | be1b9fc | 2016-02-26 10:34:30 +0100 | [diff] [blame] | 583 | worstbest = bestd[N-1]; |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 584 | } |
| 585 | } |
Marc Kupietz | 34020dc | 2016-02-25 08:44:19 +0100 | [diff] [blame] | 586 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 587 | nbs = malloc(sizeof(knn)); |
| 588 | nbs->index = besti; |
| 589 | nbs->dist = bestd; |
| 590 | nbs->length = N; |
Marc Kupietz | 44bee3c | 2016-02-25 16:26:29 +0100 | [diff] [blame] | 591 | end: |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 592 | pthread_exit(nbs); |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 593 | } |
| 594 | |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 595 | |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame^] | 596 | SV *get_neighbours(char *st1, int N, int sort_by, int search_backw, long cutoff, int dedupe) { |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 597 | HV *result = newHV(); |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 598 | float *target_sums, bestd[MAX_NEIGHBOURS], bestn[MAX_NEIGHBOURS], bests[MAX_NEIGHBOURS], vec[max_size]; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 599 | long long old_words; |
Marc Kupietz | 50485ba | 2016-03-23 09:13:14 +0100 | [diff] [blame] | 600 | long besti[MAX_NEIGHBOURS], bestp[MAX_NEIGHBOURS], a, b, c, d, slice; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 601 | knn *para_nbs[MAX_THREADS]; |
| 602 | knn *syn_nbs[MAX_THREADS]; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 603 | knnpars pars[MAX_THREADS]; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 604 | pthread_t *pt = (pthread_t *)malloc((num_threads+1) * sizeof(pthread_t)); |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 605 | wordlist *wl; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 606 | int syn_threads = (M2? window * 2 : 0); |
| 607 | int para_threads = num_threads - syn_threads; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 608 | |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 609 | if(N>MAX_NEIGHBOURS) N=MAX_NEIGHBOURS; |
| 610 | |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 611 | if(cutoff < 1) |
| 612 | cutoff=words; |
| 613 | |
Marc Kupietz | a2e6450 | 2016-04-27 09:53:51 +0200 | [diff] [blame] | 614 | wl = getTargetWords(st1, search_backw); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 615 | if(wl->length < 1) |
| 616 | goto end; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 617 | |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 618 | old_words = cutoff; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 619 | if(merge_words > 0) |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 620 | cutoff = merge_words * 1.25; /* HACK */ |
| 621 | slice = cutoff / para_threads; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 622 | |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 623 | a = posix_memalign((void **) &target_sums, 128, cutoff * sizeof(float)); |
| 624 | for(a = 0; a < cutoff; a++) |
Marc Kupietz | ce3d4c6 | 2016-03-23 16:11:25 +0100 | [diff] [blame] | 625 | target_sums[a] = 0; |
| 626 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 627 | printf("Starting %d threads\n", para_threads); |
| 628 | fflush(stdout); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 629 | for(a=0; a < para_threads; a++) { |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 630 | pars[a].cutoff = cutoff; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 631 | pars[a].token = st1; |
Marc Kupietz | 48c2968 | 2016-03-19 11:30:43 +0100 | [diff] [blame] | 632 | pars[a].wl = wl; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 633 | pars[a].N = N; |
| 634 | pars[a].from = a*slice; |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 635 | pars[a].upto = ((a+1)*slice > cutoff? cutoff:(a+1)*slice); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 636 | pthread_create(&pt[a], NULL, _get_neighbours, (void *) &pars[a]); |
| 637 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 638 | if(M2) { |
| 639 | for(a=0; a < syn_threads; a++) { |
Marc Kupietz | 2c79c5e | 2017-11-09 16:18:40 +0100 | [diff] [blame] | 640 | pars[a + para_threads].cutoff = cutoff; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 641 | pars[a + para_threads].target_sums = target_sums; |
| 642 | pars[a + para_threads].wl = wl; |
| 643 | pars[a + para_threads].N = N; |
| 644 | pars[a + para_threads].from = a; |
| 645 | pars[a + para_threads].upto = a+1; |
| 646 | pthread_create(&pt[a + para_threads], NULL, getCollocators, (void *) &pars[a + para_threads]); |
| 647 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 648 | } |
| 649 | printf("Waiting for para threads to join\n"); |
| 650 | fflush(stdout); |
| 651 | for (a = 0; a < para_threads; a++) pthread_join(pt[a], ¶_nbs[a]); |
| 652 | printf("Para threads joint\n"); |
| 653 | fflush(stdout); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 654 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 655 | /* if(!syn_nbs[0]) */ |
| 656 | /* goto end; */ |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 657 | |
| 658 | for(b=0; b < N; b++) { |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 659 | besti[b] = para_nbs[0]->index[b]; |
| 660 | bestd[b] = para_nbs[0]->dist[b]; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 661 | } |
| 662 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 663 | for(a=1; a < para_threads; a++) { |
| 664 | for(b=0; b < para_nbs[a]->length && para_nbs[a]->index[b] >= 0; b++) { |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 665 | for(c=0; c < N * para_threads; c++) { |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 666 | if(para_nbs[a]->dist[b] > bestd[c]) { |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 667 | for(d=N-1; d>c; d--) { |
| 668 | bestd[d] = bestd[d-1]; |
| 669 | besti[d] = besti[d-1]; |
| 670 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 671 | besti[c] = para_nbs[a]->index[b]; |
| 672 | bestd[c] = para_nbs[a]->dist[b]; |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 673 | break; |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | } |
| 678 | |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame^] | 679 | char *chosen[600]; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 680 | AV* array = newAV(); |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame^] | 681 | int i, j; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 682 | int l1_words=0, l2_words=0; |
| 683 | for (a = 0, i = 0; i < N && a < 600; a++) { |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame^] | 684 | int filtered=0; |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 685 | long long c = besti[a]; |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame^] | 686 | if (dedupe && i > 0) { |
| 687 | for (j=0; j<i; j++) |
| 688 | if (strcasestr(&vocab[c * max_w], chosen[j]) || |
| 689 | strcasestr(chosen[j], &vocab[c * max_w])) { |
| 690 | printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]); |
| 691 | filtered = 1; |
| 692 | } |
| 693 | if(filtered) |
| 694 | continue; |
| 695 | } |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 696 | if(merge_words > 0) { |
| 697 | if(c >= merge_words) { |
| 698 | if(l1_words > N / 2) |
| 699 | continue; |
| 700 | else |
| 701 | l1_words++; |
| 702 | } else { |
| 703 | if(l2_words > N / 2) |
| 704 | continue; |
| 705 | else |
| 706 | l2_words++; |
| 707 | } |
| 708 | } |
| 709 | fflush(stdout); |
| 710 | printf("%s l1:%d l2:%d i:%d a:%ld\n", &vocab[c * max_w], l1_words, l2_words, i, a); |
| 711 | |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 712 | HV* hash = newHV(); |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 713 | SV* word = newSVpvf(&vocab[c * max_w], 0); |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame^] | 714 | chosen[i] = &vocab[c * max_w]; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 715 | if(latin_enc == 0) SvUTF8_on(word); |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 716 | fflush(stdout); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 717 | hv_store(hash, "word", strlen("word"), word , 0); |
| 718 | hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0); |
| 719 | hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0); |
| 720 | AV *vector = newAV(); |
| 721 | for (b = 0; b < size; b++) { |
| 722 | av_push(vector, newSVnv(M[b + besti[a] * size])); |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 723 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 724 | hv_store(hash, "vector", strlen("vector"), newRV_noinc((SV*)vector), 0); |
| 725 | av_push(array, newRV_noinc((SV*)hash)); |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 726 | i++; |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 727 | } |
| 728 | hv_store(result, "paradigmatic", strlen("paradigmatic"), newRV_noinc((SV*)array), 0); |
| 729 | |
Marc Kupietz | 50485ba | 2016-03-23 09:13:14 +0100 | [diff] [blame] | 730 | for(b=0; b < MAX_NEIGHBOURS; b++) { |
| 731 | besti[b] = -1L; |
| 732 | bestd[b] = 0; |
| 733 | bestn[b] = 0; |
| 734 | bestp[b] = 0; |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 735 | bests[b] = 0; |
Marc Kupietz | 50485ba | 2016-03-23 09:13:14 +0100 | [diff] [blame] | 736 | } |
| 737 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 738 | if (M2) { |
| 739 | printf("Waiting for syn threads to join\n"); |
| 740 | fflush(stdout); |
| 741 | for (a = 0; a < syn_threads; a++) pthread_join(pt[a+para_threads], &syn_nbs[a]); |
| 742 | printf("syn threads joint\n"); |
| 743 | fflush(stdout); |
Marc Kupietz | 50485ba | 2016-03-23 09:13:14 +0100 | [diff] [blame] | 744 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 745 | for(b=0; b < syn_nbs[0]->length; b++) { |
| 746 | besti[b] = syn_nbs[0]->index[b]; |
| 747 | bestd[b] = syn_nbs[0]->dist[b]; |
| 748 | bestn[b] = syn_nbs[0]->norm[b]; |
| 749 | bestp[b] = syn_nbs[0]->pos[b]; |
| 750 | bests[b] = target_sums[syn_nbs[0]->index[b]]; |
| 751 | } |
| 752 | |
| 753 | if(sort_by != 1) { // sort by responsiveness |
| 754 | for(a=1; a < syn_threads; a++) { |
| 755 | for(b=0; b < syn_nbs[a]->length; b++) { |
| 756 | for(c=0; c < MAX_NEIGHBOURS; c++) { |
| 757 | if(syn_nbs[a]->dist[b] > bestd[c]) { |
| 758 | for(d=MAX_NEIGHBOURS-1; d>c; d--) { |
| 759 | bestd[d] = bestd[d-1]; |
| 760 | besti[d] = besti[d-1]; |
| 761 | bestn[d] = bestn[d-1]; |
| 762 | bestp[d] = bestp[d-1]; |
| 763 | } |
| 764 | besti[c] = syn_nbs[a]->index[b]; |
| 765 | bestd[c] = syn_nbs[a]->dist[b]; |
| 766 | bestn[c] = syn_nbs[a]->norm[b]; |
| 767 | bestp[c] = syn_nbs[a]->pos[b]; |
| 768 | break; |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 769 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | } |
| 773 | } else { // sort by mean p |
| 774 | for(a=1; a < syn_threads; a++) { |
| 775 | for(b=0; b < syn_nbs[a]->length; b++) { |
| 776 | for(c=0; c < MAX_NEIGHBOURS; c++) { |
| 777 | if(target_sums[syn_nbs[a]->index[b]] > bests[c]) { |
| 778 | for(d=MAX_NEIGHBOURS-1; d>c; d--) { |
| 779 | bestd[d] = bestd[d-1]; |
| 780 | besti[d] = besti[d-1]; |
| 781 | bestn[d] = bestn[d-1]; |
| 782 | bestp[d] = bestp[d-1]; |
| 783 | bests[d] = bests[d-1]; |
| 784 | } |
| 785 | besti[c] = syn_nbs[a]->index[b]; |
| 786 | bestd[c] = syn_nbs[a]->dist[b]; |
| 787 | bestn[c] = syn_nbs[a]->norm[b]; |
| 788 | bestp[c] = syn_nbs[a]->pos[b]; |
| 789 | bests[c] = target_sums[syn_nbs[a]->index[b]]; |
| 790 | break; |
| 791 | } |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 792 | } |
Marc Kupietz | 6d9a678 | 2016-03-23 17:25:25 +0100 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 796 | array = newAV(); |
Marc Kupietz | d91212f | 2017-11-13 10:05:09 +0100 | [diff] [blame^] | 797 | for (a = 0, i=0; a < MAX_NEIGHBOURS && besti[a] >= 0; a++) { |
| 798 | long long c = besti[a]; |
| 799 | if (dedupe) { |
| 800 | int filtered=0; |
| 801 | for (j=0; j<i; j++) |
| 802 | if (strcasestr(&vocab[c * max_w], chosen[j]) || |
| 803 | strcasestr(chosen[j], &vocab[c * max_w])) { |
| 804 | printf("filtering %s %s\n", chosen[j], &vocab[c * max_w]); |
| 805 | filtered = 1; |
| 806 | } |
| 807 | if(filtered) |
| 808 | continue; |
| 809 | } |
| 810 | chosen[i++]=&vocab[c * max_w]; |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 811 | HV* hash = newHV(); |
| 812 | SV* word = newSVpvf(&vocab[besti[a] * max_w], 0); |
| 813 | if(latin_enc == 0) SvUTF8_on(word); |
| 814 | hv_store(hash, "word", strlen("word"), word , 0); |
Marc Kupietz | b18978b | 2017-11-09 14:51:17 +0100 | [diff] [blame] | 815 | hv_store(hash, "rank", strlen("rank"), newSVuv(besti[a]), 0); |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 816 | hv_store(hash, "dist", strlen("dist"), newSVnv(bestd[a]), 0); |
| 817 | hv_store(hash, "norm", strlen("norm"), newSVnv(bestn[a]), 0); |
| 818 | hv_store(hash, "sum", strlen("sum"), newSVnv(target_sums[besti[a]]), 0); |
| 819 | hv_store(hash, "pos", strlen("pos"), newSVnv(bestp[a]), 0); |
| 820 | av_push(array, newRV_noinc((SV*)hash)); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 821 | } |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 822 | hv_store(result, "syntagmatic", strlen("syntagmatic"), newRV_noinc((SV*)array), 0); |
Marc Kupietz | 271e2a4 | 2016-03-22 11:37:43 +0100 | [diff] [blame] | 823 | } |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 824 | end: |
Marc Kupietz | a5f6004 | 2017-05-04 10:38:12 +0200 | [diff] [blame] | 825 | words = old_words; |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 826 | return newRV_noinc((SV*)result); |
Marc Kupietz | 000ad86 | 2016-02-26 14:59:12 +0100 | [diff] [blame] | 827 | } |
Marc Kupietz | 7bc85fd | 2016-02-24 11:42:41 +0100 | [diff] [blame] | 828 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 829 | int dump_vecs(char *fname) { |
| 830 | long i, j; |
| 831 | FILE *f; |
| 832 | /* if(words>200000) */ |
| 833 | /* words=200000; */ |
Marc Kupietz | 6b2975c | 2016-03-18 21:59:33 +0100 | [diff] [blame] | 834 | |
Marc Kupietz | 43ee87e | 2016-04-25 10:50:08 +0200 | [diff] [blame] | 835 | if((f=fopen(fname, "w")) == NULL) { |
| 836 | fprintf(stderr, "cannot open %s for writing\n", fname); |
| 837 | return(-1); |
| 838 | } |
| 839 | fprintf(f, "%lld %lld\n", words, size); |
| 840 | for (i=0; i < words; i++) { |
| 841 | fprintf(f, "%s ", &vocab[i * max_w]); |
| 842 | for(j=0; j < size - 1; j++) |
| 843 | fprintf(f, "%f ", M[i*size + j]); |
| 844 | fprintf(f, "%f\n", M[i*size + j]); |
| 845 | } |
| 846 | fclose(f); |
| 847 | return(0); |
| 848 | } |
Marc Kupietz | dc22b98 | 2015-10-09 09:19:34 +0200 | [diff] [blame] | 849 | |