Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 1 | use utf8; |
| 2 | package IDS::DeReKoVecs::Read; |
| 3 | use strict; |
| 4 | use warnings; |
| 5 | use Config; |
| 6 | |
| 7 | my $src_file = undef; |
| 8 | |
| 9 | our $mergedEnd=0; |
| 10 | our %cache; |
| 11 | our %cccache; # classic collocator cache |
| 12 | our %spcache; # similar profile cache |
| 13 | our $opt_p = 5676; |
| 14 | our $opt_C; |
| 15 | |
| 16 | BEGIN { |
| 17 | $src_file = __FILE__; |
| 18 | $src_file =~ s/Read.pm/derekovecs-server.c/; |
| 19 | } |
| 20 | |
| 21 | use Inline C => "$src_file" => CLEAN_AFTER_BUILD => 0, BUILD_NOISY => 1, ccflags => $Config{ccflags} . "-Wall -fno-rtti -O4 -I/usr/local/kl/include", libs => "-L/usr/local/kl/lib64 -l:libcollocatordb.so.1.3.0"; |
| 22 | #use Inline C => Config => BUILD_NOISY => 1, CFLAGS => $Config{cflags}." -O4 -mtune k9"; |
| 23 | #use Inline C => Config => CLEAN_AFTER_BUILD => 0, ccflags => $Config{ccflags}." -Ofast -march k8 -mtune k8 "; |
| 24 | |
| 25 | use Mojo::JSON qw(decode_json encode_json to_json); |
| 26 | use Exporter qw(import); |
| 27 | |
Marc Kupietz | 9ad564d | 2022-04-12 13:25:22 +0200 | [diff] [blame^] | 28 | our @EXPORT = qw(init_net load_sprofiles getCollocationAssociation getClassicCollocatorsCached getSimilarProfiles getSimilarProfilesCached getBiggestMergedDifferences filter_garbage get_neighbours getWordNumber); |
Marc Kupietz | bf9bac0 | 2022-04-11 21:16:47 +0200 | [diff] [blame] | 29 | |
| 30 | |
| 31 | sub getCollocationAssociation { |
| 32 | my ($c, $word, $collocate) = @_; |
| 33 | return getCollocationScores($word, $collocate) |
| 34 | } |
| 35 | |
| 36 | sub getClassicCollocatorsCached { |
| 37 | my ($c, $word) = @_; |
| 38 | my $s2 = ""; |
| 39 | if($word > $mergedEnd) { |
| 40 | $word-=$mergedEnd; |
| 41 | } |
| 42 | |
| 43 | if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference |
| 44 | open PIPE, "GET http://corpora.ids-mannheim.de/openlab/derekovecs/getClassicCollocators?w=$word |"; |
| 45 | } |
| 46 | if($opt_C || !$cccache{$word}) { |
| 47 | $c->app->log->info("Getting classic collocates of $word."); |
| 48 | $cccache{$word} = getClassicCollocators($word); |
| 49 | $cccache{$word} =~ s/:(-?)(nan|inf)/:"${1}${2}"/g; |
| 50 | } else { |
| 51 | $c->app->log->info("Getting classic collocates for $word from cache."); |
| 52 | } |
| 53 | if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference |
| 54 | while(<PIPE>) { |
| 55 | $s2 .= $_; |
| 56 | } |
| 57 | close(PIPE); |
| 58 | } |
| 59 | |
| 60 | if(length($s2) > 2000) { |
| 61 | my $d1 = decode_json($cccache{$word}); |
| 62 | my $d2 = decode_json($s2); |
| 63 | my %d2ld; |
| 64 | my $minLd = 14; |
| 65 | foreach my $i (@{$d2->{collocates}}) { |
| 66 | $d2ld{$i->{word}}=$i->{ld}; |
| 67 | $minLd=$i->{ld} if($i->{ld} < $minLd); |
| 68 | } |
| 69 | foreach my $i (@{$d1->{collocates}}) { |
| 70 | my $w = $i->{word}; |
| 71 | $i->{delta} = $i->{ld} - (defined $d2ld{$w} ? $d2ld{$w} : $minLd-0.1); |
| 72 | } |
| 73 | return(encode_json($d1)); |
| 74 | } else { |
| 75 | my $d1 = decode_json($cccache{$word}); |
| 76 | foreach my $i (@{$d1->{collocates}}) { |
| 77 | $i->{delta} = 0; |
| 78 | } |
| 79 | return(encode_json($d1)); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | sub getSimilarProfilesCached { |
| 84 | my ($c, $word) = @_; |
| 85 | if(!$spcache{$word}) { |
| 86 | $spcache{$word} = getSimilarProfiles($word); |
| 87 | } else { |
| 88 | $c->app->log->info("Getting similar profiles for $word from cache:"); |
| 89 | } |
| 90 | return $spcache{$word}; |
| 91 | } |
| 92 | |
| 93 | return 1; |