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