CAB/Chain/Multi.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 183 CC 8 SCAR 20.3
CC
8
Cov
0%
chain
20.0
xmlRpcAnalyzers
12.0
ensureChain
6.0
CRAP 7.6
1 ## -*- Mode: CPerl -*-
2 ## File: DTA::CAB::Chain::Multi.pm
3 ## Author: Bryan Jurish <moocow@cpan.org>
4 ## Description: robust analysis: multi-chains
5  
6 package DTA::CAB::Chain::Multi;
7 use DTA::CAB::Datum ':all';
8 use DTA::CAB::Chain;
9 use IO::File;
10 use Carp;
11  
12 use strict;
13  
14 ##==============================================================================
15 ## Constants
16 ##==============================================================================
17 our @ISA = qw(DTA::CAB::Chain);
18  
19 ##==============================================================================
20 ## Constructors etc.
21 ##==============================================================================
22  
23 ## $obj = CLASS_OR_OBJ->new(%args)
24 ## + object structure: HASH
25 sub new {
26 my $that = shift;
27 return $that->SUPER::new(
28 ##-- user args
29 @_,
30  
31 ##-- overrides
32 chains => undef, ##-- ($chainName=>\@chainAnalyzers,...): see setupChains() method
33 chain => undef, ##-- default chain: see setupChains() method
34 );
35 }
36  
37 ##==============================================================================
38 ## Methods: Chain selection
39 ##==============================================================================
40  
41 ## $ach = $ach->setupChains()
42 ## + setup default named sub-chains in $ach->{chains}
43 ## + set default chain $ach->{chain}
44 ## + default implementation just sets ($key=>[$ach->{$key}]) for each analyzer value in %$ach
45 ## + default implementation sets default chain to sorted list of analyzer values in %$ach
46 sub setupChains {
47 my $ach = shift;
48 my @akeys = sort grep {UNIVERSAL::isa($ach->{$_},'DTA::CAB::Analyzer')} keys(%$ach);
49 my $chains = $ach->{chains} =
50 {
51 (map {("$_"=>[$ach->{$_}])} @akeys), ##-- e.g. sub.xlit, sub.lts, ...
52 'default' =>[@$ach{@akeys}],
53 };
54  
55 ##-- sanitize chains
56 foreach (values %{$ach->{chains}}) {
57 @$_ = grep {ref($_)} @$_;
58 }
59  
60 ##-- set default chain
61 $ach->{chain} = $ach->{chains}{default};
62  
63 ##-- force default labels
64 $ach->{$_}{label} = $_ foreach (grep {UNIVERSAL::isa($ach->{$_},'DTA::CAB::Analyzer')} keys(%$ach));
65  
66 ##-- return
67 return $ach;
68 }
69  
70 ## $ach = $ach->ensureChain()
71 ## + checks for $ach->{chain}, calls $ach->setupChains() if needed
72 sub ensureChain {
73 $_[0]->setupChains if (!$_[0]{chain} || !@{$_[0]{chain}});
74 return $_[0];
75 }
76  
77 ## \@analyzers = $ach->chain()
78 ## \@analyzers = $ach->chain(\%opts)
79 ## + get selected analyzer chain
80 ## + OVERRIDE calls setupChains() if $ach->{chain} is empty
81 ## + OVERRIDE checks for $opts{chain} and returns $ach->{chains}{ $opts{chain} } if available
82 ## + OVERRIDE splits $opts{chain} on /[\,\s]+/ and constructs chain
83 sub chain {
84 $_[0]->ensureChain;
85 if ($_[1] && $_[1]{chain}) {
86 return $_[0]{chains}{$_[1]{chain}} if ($_[0]{chains}{$_[1]{chain}}); ##-- pre defined chain
87 return [
88 grep {ref($_) && $_->enabled($_[1])}
89 map { @{$_[0]{chains}{$_} || ($_[0]{$_} ? [$_[0]{$_}] : undef) || $_[0]->logconfess("could not resolve chain component '$_'")} }
90 split(/[\,\s]+/,$_[1]{chain})
91 ];
92 }
93 return [grep {ref($_) && $_->enabled($_[1])} @{$_[0]{chain}}];
94 }
95  
96 ## $chainAnalyzer = $ach->getChain()
97 ## $chainAnalyzer = $ach->getChain($chainSpec)
98 ## + returns a new DTA::CAB::Chain for the appropriate spec
99 sub getChain {
100 return DTA::CAB::Chain->new( chain=>$_[0]->chain({chain=>$_[1]}) );
101 }
102  
103 ## \@analyzers = $ach->subAnalyzers()
104 ## \@analyzers = $ach->subAnalyzers(\%opts)
105 ## + returns a list of all sub-analyzers
106 ## + override returns all defined analyzers in any chain in $ach->{chains} or in values(%$ach)
107 sub subAnalyzers {
108 my $ach = shift;
109 my %subh = map {(overload::StrVal($_)=>$_)} grep {ref($_) && UNIVERSAL::isa($_,'DTA::CAB::Analyzer')} values(%$ach);
110 $subh{overload::StrVal($_)} = $_ foreach (@{$ach->chain(@_)});
111 my ($ckey, $a);
112 foreach $ckey (sort keys %{$ach->{chains}}) {
113 foreach $a (grep {$_} @{$ach->{chains}{$ckey}||[]}) {
114 $subh{overload::StrVal($a)} = $a;
115 }
116 }
117 return [values %subh];
118 }
119  
120  
121 ##==============================================================================
122 ## Methods: I/O
123 ##==============================================================================
124  
125 ## $bool = $ach->autoEnable()
126 ## + calls inherited autoEnable() method (auto-disable on all sub-analyzers)
127 ## + prunes disabled analyzers from chains?
128  
129 ## $bool = $ach->ensureLoaded()
130 ## + ensures analysis data is loaded from default files
131 ## + override calls ensureChain() before inherited method
132 ## + override sanitizes sub-chains to canAnalyze() sub-analyzers after load
133 sub ensureLoaded_OLD {
134 my $ach = shift;
135 $ach->ensureChain;
136 my $rc = 1;
137  
138 LOAD_CHAIN:
139 foreach (values %{$ach->{chains}}) {
140 foreach (grep {$_} @$_) {
141 $rc &&= $_->ensureLoaded();
142 last LOAD_CHAIN if (!$rc);
143 }
144  
145 ##-- sanitize sub-chain
146 @$_ = grep {$_ && $_->canAnalyze} @$_;
147 }
148  
149 return $rc;
150 }
151  
152 ##==============================================================================
153 ## Methods: Persistence
154 ##==============================================================================
155  
156 ##======================================================================
157 ## Methods: Persistence: Perl
158  
159 ## @keys = $class_or_obj->noSaveKeys()
160 ## + returns list of keys not to be saved
161 ## + default just greps for CODE-refs
162 ## + override appends {chain},{chains}
163 sub noSaveKeys {
164 my $ach = shift;
165 return ($ach::SUPER->noSaveKeys, qw(chain chains));
166 }
167  
168 ## $saveRef = $obj->savePerlRef()
169 ## + return reference to be saved (top-level objects only)
170 ## + inherited from DTA::CAB::Persistent
171  
172 ## $loadedObj = $CLASS_OR_OBJ->loadPerlRef($ref)
173 ## + default implementation just clobbers $CLASS_OR_OBJ with $ref and blesses
174 ## + inherited from DTA::CAB::Persistent
175  
176 ##==============================================================================
177 ## Methods: Analysis
178 ##==============================================================================
179  
180 ##------------------------------------------------------------------------
181 ## Methods: Analysis: v1.x: API
182  
183 ## $doc = $ach->analyzeDocument($doc,\%opts)
184 ## + analyze a DTA::CAB::Document $doc
185 ## + top-level API routine
186 ## + INHERITED from DTA::CAB::Analyzer
187  
188 ## $doc = $ach->analyzeTypes($doc,$types,\%opts)
189 ## + perform type-wise analysis of all (text) types in $doc->{types}
190 ## + Chain default calls $a->analyzeTypes for each analyzer $a in the chain
191 ## + INHERITED from DTA::CAB::Chain
192  
193 ## $doc = $ach->analyzeTokens($doc,\%opts)
194 ## + perform token-wise analysis of all tokens $doc->{body}[$si]{tokens}[$wi]
195 ## + default implementation just shallow copies tokens in $doc->{types}
196 ## + INHERITED from DTA::CAB::Analyzer
197  
198 ## $doc = $ach->analyzeSentences($doc,\%opts)
199 ## + perform sentence-wise analysis of all sentences $doc->{body}[$si]
200 ## + Chain default calls $a->analyzeSentences for each analyzer $a in the chain
201 ## + INHERITED from DTA::CAB::Chain
202  
203 ## $doc = $ach->analyzeLocal($doc,\%opts)
204 ## + perform local document-level analysis of $doc
205 ## + Chain default calls $a->analyzeLocal for each analyzer $a in the chain
206 ## + INHERITED from DTA::CAB::Chain
207  
208 ## $doc = $ach->analyzeClean($doc,\%opts)
209 ## + cleanup any temporary data associated with $doc
210 ## + Chain default calls $a->analyzeClean for each analyzer $a in the chain,
211 ## then superclass Analyzer->analyzeClean
212 ## + INHERITED from DTA::CAB::Chain
213  
214 ##------------------------------------------------------------------------
215 ## Methods: Analysis: v1.x: Wrappers
216  
217 ## $tok = $ach->analyzeToken($tok_or_string,\%opts)
218 ## + perform type- and token-analyses on $tok_or_string
219 ## + wrapper for $ach->analyzeDocument()
220 ## + INHERITED from DTA::CAB::Analyzer
221  
222 ## $tok = $ach->analyzeSentence($sent_or_array,\%opts)
223 ## + perform type-, token-, and sentence-analyses on $sent_or_array
224 ## + wrapper for $ach->analyzeDocument()
225 ## + INHERITED from DTA::CAB::Analyzer
226  
227 ## $rpc_xml_base64 = $anl->analyzeData($data_str,\%opts)
228 ## + analyze a raw (formatted) data string $data_str with internal parsing & formatting
229 ## + wrapper for $anl->analyzeDocument()
230 ## + INHERITED from DTA::CAB::Analyzer
231  
232 ##==============================================================================
233 ## Methods: XML-RPC
234  
235 ## \@sigs = $anl->xmlRpcSignatures()
236 ## + returns an array-ref of valid XML-RPC signatures:
237 ## [ "$returnType1 $argType1_1 $argType1_2 ...", ..., "$returnTypeN ..." ]
238 ## + inherited from DTA::CAB::Analyzer::XmlRpc
239 #sub xmlRpcMethods {
240 # my ($anl,$prefix,$opts) = @_;
241 # $prefix = $prefix ? "${prefix}." : '';
242 # $opts = {} if (!$opts);
243 # return map {$anl->SUPER::xmlRpcMethods($prefix.$_, {%$opts,chain=>$_})} keys(%{$anl->{chains}});
244 #}
245  
246 ## \%analyzerHash = $anl->xmlRpcAnalyzers()
247 ## \%analyzerHash = $anl->xmlRpcAnalyzers($prefix)
248 ## + returns pseudo hash for use with DTA::CAB::Server::XmlRpc 'as' attribute
249 sub xmlRpcAnalyzers {
250 my ($anl,$prefix) = @_;
251 $prefix = '' if (!defined($prefix));
252 $prefix .= '.' if ($prefix && $prefix !~ /\.$/);
253 $anl->ensureChain;
254 return
255 { (map {("${prefix}$_"=>bless({%$anl,chain=>$anl->{chains}{$_}},ref($anl)))} keys(%{$anl->{chains}})) };
256 }
257  
258 1; ##-- be happy
259  
260 __END__