CAB/Analyzer/GermaNet.pm untested

filter: Statementstmt 0.0% 0 / 106 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 256 CC 29 SCAR 34.2
CC
29
Cov
0%
load
56.0
synset_str
56.0
relation_closure
42.0
CRAP 30.6
1 ## -*- Mode: CPerl -*-
2 ##
3 ## File: DTA::CAB::Analyzer::GermaNet.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: wrapper for GermaNet relation expanders
6  
7 package DTA::CAB::Analyzer::GermaNet;
8 use DTA::CAB::Analyzer ':child';
9 use Storable;
10 use Carp;
11  
12 use strict;
13  
14 ##==============================================================================
15 ## Globals
16 ##==============================================================================
17  
18 our @ISA = qw(DTA::CAB::Analyzer);
19  
20 our ($HAVE_GERMANET_FLAT);
21 BEGIN{
22 eval 'use GermaNet::Flat;' if (!UNIVERSAL::can('GermaNet::Flat','new'));
23 $HAVE_GERMANET_FLAT = UNIVERSAL::can('GermaNet::Flat','new') ? 1 : 0;
24 }
25  
26 our %FILE2GN = qw(); ##-- maps source files to GermaNet::GermaNet objects (for data sharing)
27  
28 ##--------------------------------------------------------------
29 ## Globals: Accessors
30  
31 ##==============================================================================
32 ## Constructors etc.
33 ##==============================================================================
34  
35 ## $obj = CLASS_OR_OBJ->new(%args)
36 ## + object structure:
37 ## (
38 ## ##-- Filename Options
39 ## gnFile=> $dirname_or_binfile, ##-- default: none
40 ##
41 ## ##-- Runtime
42 ## gn => $gn_obj, ##-- underlying GermaNet::Flat object
43 ## max_depth => $depth, ##-- default maximum closure depth for relation_closure() [default=128]
44 ##
45 ## ##-- Analysis Output
46 ## label => $lab, ##-- analyzer label
47 ## )
48 sub new {
49 my $that = shift;
50 my $gna = $that->SUPER::new(
51 ##-- filenames
52 gnFile => undef,
53  
54 ##-- runtime
55 max_depth => 128,
56 gn => undef,
57  
58 ##-- analysis output
59 label => 'gnet',
60  
61 ##-- user args
62 @_
63 );
64 return $gna;
65 }
66  
67 ## $gna = $gna->clear()
68 sub clear {
69 my $gna = shift;
70 delete $gna->{gn};
71 return $gna;
72 }
73  
74  
75 ##==============================================================================
76 ## Methods: Embedded API
77 ##==============================================================================
78  
79 ## $bool = $gna->gnOk()
80 ## + returns false iff gn is undefined or "empty"
81 sub gnOk {
82 return defined($_[0]{gn});
83 }
84  
85 ##==============================================================================
86 ## Methods: I/O
87 ##==============================================================================
88  
89 ##--------------------------------------------------------------
90 ## Methods: I/O: Input: all
91  
92 ## $rc = $gna->load()
93 ## $rc = $gna->load($gnFile)
94 ## + force re-load
95 sub load {
96 my ($gna,$gnFile) = @_;
97 $gna->{gnFile} = ($gnFile ||= $gna->{gnFile});
98 if (!$gnFile) {
99 return 0;
100 }
101 elsif (!$HAVE_GERMANET_FLAT) {
102 $gna->warn("GermaNet::Flat module unvailable -- cannot load $gnFile");
103 return 0;
104 }
105 elsif (exists $FILE2GN{$gnFile}) {
106 ##-- binary data sharing
107 $gna->{gn} = $FILE2GN{$gnFile};
108 }
109 elsif (-d $gnFile) {
110 $gna->info("loading GermaNet data from XML directory $gnFile ...");
111 }
112 elsif ($gnFile =~ /\.[cb]?db$/i) {
113 $gna->info("attaching GermaNet data to file $gnFile ...");
114 }
115 elsif ($gnFile =~ /\.(?:sto|bin)$/i) {
116 $gna->info("loading GermaNet data from binary file $gnFile ...");
117 }
118 else {
119 $gna->info("loading GermaNet data from text file $gnFile");
120 }
121 $gna->{gn} = $FILE2GN{$gnFile} = GermaNet::Flat->load($gnFile);
122 return $gna->gnOk();
123 }
124  
125 ## $bool = $gna->ensureLoaded()
126 ## + ensures analyzer data is loaded from default file(s)
127 sub ensureLoaded {
128 my $gna = shift;
129 my $rc = 1;
130 if (!$HAVE_GERMANET_FLAT) {
131 $gna->warn("GermaNet::GermaNet module unvailable disabling analyzer '$gna->{label}'") if ($gna->{enabled});
132 $gna->{enabled} = 0;
133 }
134 elsif (defined($gna->{gnFile}) && !$gna->gnOk) {
135 $rc &&= $gna->load();
136 }
137 return $rc;
138 }
139  
140 ##==============================================================================
141 ## Methods: Persistence
142 ##==============================================================================
143  
144 ##======================================================================
145 ## Methods: Persistence: Perl
146  
147 ## @keys = $class_or_obj->noSaveKeys()
148 ## + returns list of keys not to be saved
149 sub noSaveKeys {
150 my $that = shift;
151 return ($that->SUPER::noSaveKeys, qw(gn));
152 }
153  
154 ## $saveRef = $obj->savePerlRef()
155 ## + inherited from DTA::CAB::Persistent
156  
157 ## $loadedObj = $CLASS_OR_OBJ->loadPerlRef($ref)
158 sub loadPerlRef {
159 my ($that,$ref) = @_;
160 my $obj = $that->SUPER::loadPerlRef($ref);
161 return $obj;
162 }
163  
164 ##==============================================================================
165 ## Methods: Analysis
166 ##==============================================================================
167  
168 ##------------------------------------------------------------------------
169 ## Methods: Analysis: Generic
170  
171 ## $bool = $anl->canAnalyze()
172 ## + returns true if analyzer can perform its function (e.g. data is loaded & non-empty)
173 ## + override calls gnOk()
174 sub canAnalyze {
175 return $_[0]->gnOk();
176 }
177  
178 ##------------------------------------------------------------------------
179 ## Methods: Analysis: v1.x: API
180  
181 ## $doc = $anl->analyzeTypes($doc,\%types,\%opts)
182 ## + perform type-wise analysis of all (text) types in $doc->{types}
183 ## + NOT IMPLEMENTED HERE!
184 #sub analyzeTypes { };
185  
186 ##==============================================================================
187 ## Methods: Utils
188  
189 ## @terms = $gna->synset_terms($synset)
190 sub synset_terms {
191 # return map {s/\s/_/g; $_} map {@{$_->get_orth_forms}} @{$_[1]->get_lex_units};
192 return @{$_[0]{gn}->synset_terms($_[1])};
193 }
194  
195 ## \@terms = $gna->synsets_terms(\@synsets)
196 ## + unique elements only
197 sub synsets_terms {
198 #my $gna = shift;
199 #my $prev='';
200 my $gn = $_[0]{gn};
201 return $gn->auniq( $gn->relation('lex2orth',$gn->relation('syn2lex',$_[1])) );
202 }
203  
204 ## $str = $gna->synset_str($synset,%opts)
205 ## + %opts:
206 ## show_ids => $bool, ##-- default=1
207 ## show_lex => $bool, ##-- default=1
208 ## canonical => $bool, ##-- default=1
209 sub synset_str {
210 my ($gna,$syn,%opts) = @_;
211 return 'undef' if (!defined($syn));
212 %opts = (show_ids=>1,show_lex=>1,canonical=>1) if (!%opts);
213 my $str = (($opts{show_ids}
214 ? ($syn.($opts{show_lex} || $opts{canonical} ? ':' : ''))
215 : '')
216 .($opts{show_lex}
217 ? ($opts{canonical}
218 ? $gna->{gn}->relation('lex2orth',$gna->{gn}->relation('syn2lex',$syn)->[0])->[0]
219 : $gna->{gn}->relation('lex2orth',$gna->{gn}->relation('syn2lex',$syn)))
220 : ''));
221 return $str;
222 }
223  
224 ## $str = $gna->path_str(\@synsets)
225 ## $str = $gna->path_str( @synsets)
226 sub path_str {
227 return join('/',map {$_[0]->synset_str($_)} (UNIVERSAL::isa($_[1],'ARRAY') ? @{$_[1]} : @_));
228 }
229  
230 ## %relation_alias : global relation aliases
231 my %relation_alias = (
232 'hyperonymy'=>'has_hypernym',
233 'hypernyms' =>'has_hypernym',
234 'hypernym' =>'has_hypernym',
235 'hyponymy' =>'has_hyponym',
236 'hyponyms' =>'has_hyponym',
237 'hyponym' =>'has_hyponym',
238 );
239  
240 ## @synsets = $gna->relation_closure($synset,$relation,$max_depth,\%syn2depth); ##-- list context
241 ## $synsets = $gna->relation_closure($synset,$relation,$max_depth,\%syn2depth); ##-- scalar context
242 ## + returns transitive + reflexive closure of relation $relation (up to $max_depth=$gna->{max_depth})
243 sub relation_closure {
244 my ($gna,$synset,$rel,$maxdepth,$syn2depth) = @_;
245 $maxdepth //= $gna->{max_depth};
246 $maxdepth = 65536 if (!defined($maxdepth) || $maxdepth < 0);
247  
248 $syn2depth = {$synset=>0} if (!$syn2depth); ##-- $synset => $depth, ...
249 my $gn = $gna->{gn};
250 my @queue = ($synset);
251 my @syns = qw();
252 my ($syn,$depth,$next);
253 while (defined($syn=shift(@queue))) {
254 push(@syns,$syn);
255 $depth = $syn2depth->{$syn};
256 if ((!defined($maxdepth) || $depth < $maxdepth) && defined($next=$gn->relation($relation_alias{$rel}//$rel,$syn))) {
257 foreach (@$next) {
258 next if (exists $syn2depth->{$_});
259 $syn2depth->{$_} = $depth+1;
260 push(@queue,$_);
261 }
262 }
263 }
264 return wantarray ? @syns : \@syns;
265 }
266  
267 ## @paths = $gna->synset_paths($synset,$maxdepth)
268 ## + returns all paths to $synset from root
269 sub synset_paths {
270 my ($gna,$synset,$depth) = @_;
271 $depth //= $gna->{max_depth};
272 $depth = 65536 if (!defined($depth) || $depth < 0);
273  
274 my (@paths,$i,$path,$hyps);
275 my @queue = ([0,$synset]); ##-- queue items: [$depth,@path...]
276  
277 while (defined($path=shift(@queue))) {
278 $i = shift(@$path);
279 if ($i>=$depth) {
280 push(@paths,$path);
281 next;
282 }
283 $hyps = $path->[0]->relation('has_hyponym');
284 if ($hyps && @$hyps) {
285 if (@$hyps==1) {
286 unshift(@$path, $i+1, $hyps->[0]); ##-- re-use path for 1st hyponym
287 push(@queue, $path);
288 } else {
289 push(@queue, map {[$i+1, $_, @$path]} @$hyps);
290 }
291 } else {
292 push(@paths, $path);
293 }
294 }
295 return wantarray ? @paths : \@paths;
296 }
297  
298  
299 1; ##-- be happy
300  
301 __END__