blob: edf0b55df9b4bb74b1e8a35e7bf01d10a0db5f71 [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 Kupietz41d61c02024-08-05 22:11:30 +020022use Inline C => "$src_file" => CLEAN_AFTER_BUILD => 0, BUILD_NOISY => 1, ccflags => $Config{ccflags} . "-Wall -Wno-unused-result -fno-rtti -O4 -I/usr/local/include", libs => "-L/usr/local/lib64 -L/usr/local/lib -lcollocatordb";
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 Kupietz043db152023-11-05 17:47:53 +010029our @EXPORT = qw(init_net load_sprofiles getVocabSize 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;
Marc Kupietz2c84b5d2023-11-06 12:56:52 +010059 $cccache{$word} =~ s/"""/"\\""/g;
Marc Kupietzbf9bac02022-04-11 21:16:47 +020060 } else {
61 $c->app->log->info("Getting classic collocates for $word from cache.");
62 }
63 if($opt_p >= 5000 && $opt_p < 5600) { # German non-reference
64 while(<PIPE>) {
65 $s2 .= $_;
66 }
67 close(PIPE);
68 }
69
70 if(length($s2) > 2000) {
71 my $d1 = decode_json($cccache{$word});
72 my $d2 = decode_json($s2);
73 my %d2ld;
74 my $minLd = 14;
75 foreach my $i (@{$d2->{collocates}}) {
76 $d2ld{$i->{word}}=$i->{ld};
77 $minLd=$i->{ld} if($i->{ld} < $minLd);
78 }
79 foreach my $i (@{$d1->{collocates}}) {
80 my $w = $i->{word};
81 $i->{delta} = $i->{ld} - (defined $d2ld{$w} ? $d2ld{$w} : $minLd-0.1);
82 }
83 return(encode_json($d1));
84 } else {
85 my $d1 = decode_json($cccache{$word});
86 foreach my $i (@{$d1->{collocates}}) {
87 $i->{delta} = 0;
88 }
89 return(encode_json($d1));
90 }
91}
92
93sub getSimilarProfilesCached {
94 my ($c, $word) = @_;
95 if(!$spcache{$word}) {
96 $spcache{$word} = getSimilarProfiles($word);
97 } else {
98 $c->app->log->info("Getting similar profiles for $word from cache:");
99 }
100 return $spcache{$word};
101}
102
103return 1;