| Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 1 | use utf8; |
| 2 | package IDS::DeReKoVecs::Read; |
| Marc Kupietz | 3576c62 | 2023-11-05 08:51:58 +0100 | [diff] [blame] | 3 | use LWP::Simple; |
| Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 4 | use strict; |
| 5 | use warnings; |
| 6 | use Config; |
| 7 | |
| 8 | my $src_file = undef; |
| 9 | |
| Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 10 | our $opt_p = 5676; |
| 11 | our $opt_C; |
| 12 | |
| Marc Kupietz | af43614 | 2026-07-30 14:40:10 +0900 | [diff] [blame] | 13 | # Cached values are large (a classic collocator profile is tens to hundreds of |
| 14 | # kilobytes of JSON), so the caches are size limited. Without a limit a long |
| 15 | # running worker accumulates one entry per queried word until the box runs out |
| 16 | # of memory. Defaults can be overridden via configure_cache(). |
| 17 | our $CC_CACHE_MAX_KEYS = 200; # classic collocators |
| 18 | our $SP_CACHE_MAX_KEYS = 200; # similar profiles |
| 19 | |
| Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 20 | BEGIN { |
| 21 | $src_file = __FILE__; |
| 22 | $src_file =~ s/Read.pm/derekovecs-server.c/; |
| 23 | } |
| 24 | |
| Marc Kupietz | f604691 | 2024-12-03 17:21:57 +0100 | [diff] [blame] | 25 | use Inline C => "$src_file" => CLEAN_AFTER_BUILD => 0, BUILD_NOISY => 1, ccflags => $Config{ccflags} . "-Wall -Wno-unused-result -O4 -mno-avx2 -I/usr/local/include", libs => "-L/usr/local/lib64 -L/usr/local/lib -lcollocatordb"; |
| Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 26 | #use Inline C => Config => BUILD_NOISY => 1, CFLAGS => $Config{cflags}." -O4 -mtune k9"; |
| 27 | #use Inline C => Config => CLEAN_AFTER_BUILD => 0, ccflags => $Config{ccflags}." -Ofast -march k8 -mtune k8 "; |
| 28 | |
| 29 | use Mojo::JSON qw(decode_json encode_json to_json); |
| Marc Kupietz | af43614 | 2026-07-30 14:40:10 +0900 | [diff] [blame] | 30 | use Mojo::Cache; |
| Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 31 | use Exporter qw(import); |
| 32 | |
| Marc Kupietz | 6f317a9 | 2026-07-30 15:09:54 +0900 | [diff] [blame] | 33 | our @EXPORT = qw(init_net load_sprofiles mergeVectors getVocabSize getDowntimeCalendar getCollocationAssociation getClassicCollocatorsCached getSimilarProfiles getSimilarProfilesCached getBiggestMergedDifferences getMergedEnd filter_garbage get_neighbours getVecs getWordNumber dump_vecs dump_for_numpy cos_similarity_as_json get_version getPosWiseW2VCollocators configure_cache cache_stats); |
| Marc Kupietz | af43614 | 2026-07-30 14:40:10 +0900 | [diff] [blame] | 34 | |
| 35 | my $cccache = Mojo::Cache->new(max_keys => $CC_CACHE_MAX_KEYS); # classic collocator cache |
| 36 | my $spcache = Mojo::Cache->new(max_keys => $SP_CACHE_MAX_KEYS); # similar profile cache |
| 37 | |
| 38 | # Adjust the caches at startup, e.g. |
| 39 | # configure_cache(collocators_max_keys => 500, no_cache => 1) |
| 40 | # A max_keys value <= 0 disables the respective cache. Setting a limit starts |
| 41 | # from an empty cache, so that a lowered limit takes effect immediately. |
| 42 | sub configure_cache { |
| 43 | my (%opt) = @_; |
| 44 | $opt_C = $opt{no_cache} if defined $opt{no_cache}; |
| 45 | $cccache = Mojo::Cache->new(max_keys => $opt{collocators_max_keys}) |
| 46 | if defined $opt{collocators_max_keys}; |
| 47 | $spcache = Mojo::Cache->new(max_keys => $opt{profiles_max_keys}) |
| 48 | if defined $opt{profiles_max_keys}; |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | sub cache_stats { |
| 53 | return { |
| 54 | collocators => { |
| 55 | keys => scalar keys %{$cccache->{cache} || {}}, |
| 56 | max_keys => $cccache->max_keys |
| 57 | }, |
| 58 | profiles => { |
| 59 | keys => scalar keys %{$spcache->{cache} || {}}, |
| 60 | max_keys => $spcache->max_keys |
| 61 | } |
| 62 | }; |
| 63 | } |
| Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 64 | |
| Marc Kupietz | 3576c62 | 2023-11-05 08:51:58 +0100 | [diff] [blame] | 65 | sub getDowntimeCalendar { |
| 66 | my ($url) = @_; |
| 67 | if ($url =~ m/^\s*$/) { |
| 68 | return ""; |
| 69 | } |
| 70 | my $calendar = LWP::Simple::get($url); |
| 71 | return $calendar; |
| 72 | } |
| Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 73 | |
| 74 | sub getCollocationAssociation { |
| 75 | my ($c, $word, $collocate) = @_; |
| 76 | return getCollocationScores($word, $collocate) |
| 77 | } |
| 78 | |
| 79 | sub getClassicCollocatorsCached { |
| Marc Kupietz | 3eeec65 | 2024-11-18 18:30:04 +0100 | [diff] [blame] | 80 | my ($c, $word, $compare_to) = @_; |
| Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 81 | my $s2 = ""; |
| Marc Kupietz | 6f317a9 | 2026-07-30 15:09:54 +0900 | [diff] [blame] | 82 | # The collocator db is the one of the primary model, which sits at |
| 83 | # [merged_end, words) in the merged vocabulary, so its ranks have to be |
| 84 | # shifted back before they can be looked up. |
| 85 | my $merged_end = getMergedEnd(); |
| 86 | if($word >= $merged_end) { |
| 87 | $word -= $merged_end; |
| Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 88 | } |
| 89 | |
| Marc Kupietz | 3eeec65 | 2024-11-18 18:30:04 +0100 | [diff] [blame] | 90 | my $pipe; |
| 91 | if($compare_to ne "") { |
| 92 | $c->app->log->info("comparing syn neighbours to: $compare_to/getClassicCollocators?w=$word"); |
| 93 | open $pipe, "lwp-request $compare_to/getClassicCollocators?w=$word |"; |
| Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 94 | } |
| Marc Kupietz | 3eeec65 | 2024-11-18 18:30:04 +0100 | [diff] [blame] | 95 | |
| Marc Kupietz | af43614 | 2026-07-30 14:40:10 +0900 | [diff] [blame] | 96 | my $collocators = $opt_C ? undef : $cccache->get($word); |
| 97 | if(!defined $collocators) { |
| Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 98 | $c->app->log->info("Getting classic collocates of $word."); |
| Marc Kupietz | af43614 | 2026-07-30 14:40:10 +0900 | [diff] [blame] | 99 | $collocators = getClassicCollocators($word); |
| 100 | $collocators =~ s/:(-?)(nan|inf)/:"${1}${2}"/g; |
| 101 | $collocators =~ s/"""/"\\""/g; |
| 102 | $cccache->set($word => $collocators) unless $opt_C; |
| Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 103 | } else { |
| 104 | $c->app->log->info("Getting classic collocates for $word from cache."); |
| 105 | } |
| Marc Kupietz | 3eeec65 | 2024-11-18 18:30:04 +0100 | [diff] [blame] | 106 | |
| 107 | if(defined($pipe)) { |
| 108 | while(<$pipe>) { |
| Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 109 | $s2 .= $_; |
| 110 | } |
| Marc Kupietz | 3eeec65 | 2024-11-18 18:30:04 +0100 | [diff] [blame] | 111 | close($pipe); |
| Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | if(length($s2) > 2000) { |
| Marc Kupietz | af43614 | 2026-07-30 14:40:10 +0900 | [diff] [blame] | 115 | my $d1 = decode_json($collocators); |
| Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 116 | my $d2 = decode_json($s2); |
| 117 | my %d2ld; |
| 118 | my $minLd = 14; |
| 119 | foreach my $i (@{$d2->{collocates}}) { |
| 120 | $d2ld{$i->{word}}=$i->{ld}; |
| 121 | $minLd=$i->{ld} if($i->{ld} < $minLd); |
| 122 | } |
| 123 | foreach my $i (@{$d1->{collocates}}) { |
| 124 | my $w = $i->{word}; |
| 125 | $i->{delta} = $i->{ld} - (defined $d2ld{$w} ? $d2ld{$w} : $minLd-0.1); |
| 126 | } |
| 127 | return(encode_json($d1)); |
| 128 | } else { |
| Marc Kupietz | af43614 | 2026-07-30 14:40:10 +0900 | [diff] [blame] | 129 | my $d1 = decode_json($collocators); |
| Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 130 | foreach my $i (@{$d1->{collocates}}) { |
| 131 | $i->{delta} = 0; |
| 132 | } |
| 133 | return(encode_json($d1)); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | sub getSimilarProfilesCached { |
| 138 | my ($c, $word) = @_; |
| Marc Kupietz | 6f317a9 | 2026-07-30 15:09:54 +0900 | [diff] [blame] | 139 | # like the collocator db, the precomputed profiles belong to the primary |
| 140 | # model, see getClassicCollocatorsCached() |
| 141 | my $merged_end = getMergedEnd(); |
| 142 | $word -= $merged_end if $word >= $merged_end; |
| Marc Kupietz | af43614 | 2026-07-30 14:40:10 +0900 | [diff] [blame] | 143 | my $profiles = $opt_C ? undef : $spcache->get($word); |
| 144 | if(!defined $profiles) { |
| 145 | $profiles = getSimilarProfiles($word); |
| 146 | $spcache->set($word => $profiles) unless $opt_C; |
| Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 147 | } else { |
| 148 | $c->app->log->info("Getting similar profiles for $word from cache:"); |
| 149 | } |
| Marc Kupietz | af43614 | 2026-07-30 14:40:10 +0900 | [diff] [blame] | 150 | return $profiles; |
| Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | return 1; |