CAB/Analyzer/GermaNet/RelationClosure.pm untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 110 CC 8 SCAR 33.9
CC
8
Cov
0%
analyzeSentences
56.0
doAnalyze
6.0
new
2.0
CRAP 29.6
1 ## -*- Mode: CPerl -*-
2 ##
3 ## File: DTA::CAB::Analyzer::GermaNet::RelationClosure.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: wrapper for GermaNet relation expanders
6  
7 package DTA::CAB::Analyzer::GermaNet::RelationClosure;
8 use DTA::CAB::Analyzer ':child';
9 use DTA::CAB::Analyzer::GermaNet;
10 use Carp;
11  
12 use strict;
13  
14 ##==============================================================================
15 ## Globals
16 ##==============================================================================
17  
18 our @ISA = qw(DTA::CAB::Analyzer::GermaNet);
19  
20 ##--------------------------------------------------------------
21 ## Globals: Accessors
22  
23 ## $DEFAULT_ANALYZE_GET
24 ## + default coderef or eval-able string for {analyzeGet}
25 our $DEFAULT_ANALYZE_GET = _am_lemma('$_->{moot}').' || '._am_word('$_->{moot}',_am_xlit);
26  
27 ##==============================================================================
28 ## Constructors etc.
29 ##==============================================================================
30  
31 ## $obj = CLASS_OR_OBJ->new(%args)
32 ## + object structure:
33 ## (
34 ## ##-- NEW in GermaNet::RelationClosure
35 ## relations => \@relns, ##-- relations whose closure to compute (default: qw(has_hyperonym has_hyponym))
36 ## analyzeGet => $code, ##-- accessor: coderef or string: source text (default=$DEFAULT_ANALYZE_GET; return undef for no analysis)
37 ## allowRegex => $regex, ##-- only analyze types matching $regex
38 ## ${lab}_max_depth => $max_depth, ##-- maximum expansion depth
39 ##
40 ## ##-- INHERITED from Analyzer::GermaNet
41 ## gnFile=> $dirname_or_binfile, ##-- default: none
42 ## gn => $gn_obj, ##-- underlying GermaNet object
43 ## max_depth => $depth, ##-- default maximum closure depth for relation_closure() [default=128]
44 ## label => $lab, ##-- analyzer label
45 ## )
46 sub new {
47 my $that = shift;
48 my $gna = $that->SUPER::new(
49 ##-- filenames
50 gnFile => undef,
51  
52 ##-- runtime
53 relations => [qw(has_hypernym has_hyponym)],
54 max_depth => 128,
55 analyzeGet => $DEFAULT_ANALYZE_GET,
56 allowRegex => undef,
57  
58 ##-- analysis output
59 label => 'gnet',
60  
61 ##-- user args
62 @_
63 );
64 return $gna;
65 }
66  
67  
68 ##==============================================================================
69 ## Methods: Analysis
70 ##==============================================================================
71  
72 ##------------------------------------------------------------------------
73 ## Methods: Analysis: utils
74  
75 ## $bool = $anl->doAnalyze(\%opts, $name)
76 ## + override: only allow analyzeSentences()
77 sub doAnalyze {
78 my $anl = shift;
79 return 0 if (defined($_[1]) && $_[1] ne 'Sentences');
80 return $anl->SUPER::doAnalyze(@_);
81 }
82  
83 ## $doc = $anl->analyzeSentences($doc,\%opts)
84 ## + perform sentence-wise analysis of $doc
85 sub analyzeSentences {
86 my ($gna,$doc,$opts) = @_;
87  
88 ##-- setup common variables
89 my $lab = $gna->{label};
90 my $gn = $gna->{gn};
91 my $relations = $gna->{relations} || [];
92 my $max_depth = $opts->{"${lab}_max_depth"} // $gna->{"${lab}_max_depth"} // $opts->{max_depth} // $gna->{max_depth};
93 my $allow_re = defined($gna->{allowRegex}) ? qr($gna->{allowRegex}) : undef;
94 my $aget_code = defined($gna->{analyzeGet}) ? $gna->{analyzeGet} : $DEFAULT_ANALYZE_GET;
95 my $aget = $gna->accessClosure($aget_code);
96 my %cache = qw();
97  
98 my ($w,$lemma, $synsets, $syn,@syns,$cached);
99 foreach (map {@{$_->{tokens}}} @{$doc->{body}}) {
100 next if (defined($allow_re) && $_->{text} !~ $allow_re);
101 $w = $_;
102 $lemma = $aget->();
103 if (defined($cached=$cache{$lemma})) {
104 ##-- used cached data
105 delete $w->{$lab};
106 $w->{$lab} = $cached if (@$cached);
107 next;
108 }
109  
110 ##-- lookup
111 $synsets = $gn->get_synsets($lemma) // [];
112 push(@$synsets, grep {exists($gn->{rel}{"syn2lex:$_"})} $lemma); ##-- allow synset names as 'lemma' queries
113 @syns = map {
114 $syn = $_;
115 map {
116 $gna->relation_closure($syn, $_, $max_depth)
117 } @$relations
118 } @$synsets;
119  
120 $w->{$lab} = $cache{$lemma} = [grep {$_ ne 'GNROOT'} @{$gna->synsets_terms(\@syns)}];
121 delete($w->{$lab}) if (!@{$w->{$lab}});
122 }
123  
124 return $doc;
125 }
126  
127  
128 1; ##-- be happy
129  
130 __END__