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