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