CAB/Analyzer/Dict.pm untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 133 CC 9 SCAR 17.1
CC
9
Cov
0%
analyzeTypes
12.0
analyzeCode
12.0
dictHash
6.0
CRAP 5.5
1 ## -*- Mode: CPerl -*-
2 ##
3 ## File: DTA::CAB::Analyzer::Dict.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: generic analysis dictionary API using Lingua::TT::Dict
6  
7 package DTA::CAB::Analyzer::Dict;
8 use DTA::CAB::Analyzer ':child';
9 use DTA::CAB::Format;
10 use Lingua::TT::Dict;
11 use IO::File;
12 use Carp;
13  
14 use strict;
15  
16 ##==============================================================================
17 ## Globals
18 ##==============================================================================
19  
20 our @ISA = qw(DTA::CAB::Analyzer);
21  
22 ##--------------------------------------------------------------
23 ## Globals: Accessors
24  
25 ## + dict application is computed as:
26 ## $dic->accessClosure($dic->{analyzeCode})->();
27 ## + analysis closure compiled from $dic->{analyzeCode} can use vars:
28 ## $dic ##-- analyzer object
29 ## $anl ##-- analyzer object (alias provided by Analyzer::accessClosure)
30 ## $lab ##-- $dic->{label}
31 ## $dhash ##-- $dic->dictHash()
32 ## #$doc ##-- document being analyzed
33 ## #$types ##-- types being analyzed with analyzeTypes()
34 ## #$opts ##-- user options to analyzeTypes()
35 ## + the following lexical temporaries are provided:
36 ## $key ##-- temporary; unused here
37 ## $val ##-- temporary; unused here
38 ## @keys ##-- temporary; unused here
39 ## @vals ##-- temporary; unused here
40 ## %vals ##-- temporary; unused here
41 ##==============================================================================
42  
43 our $CODE_DEFAULT = '$_->{$lab}=$dhash->{'._am_xlit().'};'; # '._am_clean('$_->{$lab}'); ##-- useless, since expandTypes() puts undef back!
44  
45 ##==============================================================================
46 ## Constructors etc.
47 ##==============================================================================
48  
49 ## $obj = CLASS_OR_OBJ->new(%args)
50 ## + object structure:
51 ## (
52 ## ##-- Filename Options
53 ## dictFile=> $filename, ##-- default: none
54 ##
55 ## ##-- Analysis Output
56 ## label => $lab, ##-- analyzer label
57 ## analyzeCode => $code, ##-- pseudo-accessor ($code->()): apply dict to current token ($_)
58 ##
59 ## ##-- Analysis Options
60 ## encoding => $enc, ##-- encoding of dict file (default='UTF-8')
61 ## allowRegex => $re, ##-- only lookup tokens whose text matches $re (default=none)
62 ## eqIdWeight => $w, ##-- weight for identity analyses for analyzeSet=>$DICT_SET_FST_EQ
63 ##
64 ## ##-- Analysis objects
65 ## ttd => $ttdict, ##-- underlying Lingua::TT::Dict object
66 ## )
67 sub new {
68 my $that = shift;
69 my $dic = $that->SUPER::new(
70 ##-- filenames
71 dictFile => undef,
72  
73 ##-- analysis objects
74 ttd=>Lingua::TT::Dict->new(),
75  
76 ##-- options
77 encoding => 'UTF-8',
78  
79 ##-- analysis output
80 label => 'dict',
81 analyzeCode => $CODE_DEFAULT,
82 allowRegex => undef,
83  
84 ##-- user args
85 @_
86 );
87 return $dic;
88 }
89  
90 ## $dic = $dic->clear()
91 sub clear {
92 my $dic = shift;
93 $dic->{ttd}->clear;
94 return $dic;
95 }
96  
97  
98 ##==============================================================================
99 ## Methods: Embedded API
100 ##==============================================================================
101  
102 ## $bool = $dict->dictOk()
103 ## + returns false iff dict is undefined or "empty"
104 sub dictOk {
105 return defined($_[0]{ttd}) && scalar(%{$_[0]{ttd}{dict}});
106 }
107  
108 ## \%key2val = $dict->dictHash()
109 ## + returns a (possibly tie()d hash) representing dict contents
110 ## + default just returns $dic->{ttd}{dict} or a new empty hash
111 sub dictHash {
112 return $_[0]{ttd} && $_[0]{ttd}{dict} ? $_[0]{ttd}{dict} : {};
113 }
114  
115 ## $val_or_undef = $dict->dictLookup($key)
116 ## + get stored value for key $key
117 ## + default returns $dict->{ttd}{dict}{$key} or undef
118 sub dictLookup {
119 return $_[0]{ttd} && $_[0]{ttd}{dict} ? $_[0]{ttd}{dict}{$_[1]} : undef;
120 }
121  
122 ##==============================================================================
123 ## Methods: I/O
124 ##==============================================================================
125  
126 ##--------------------------------------------------------------
127 ## Methods: I/O: Input: all
128  
129 ## $bool = $dic->ensureLoaded()
130 ## + ensures analyzer data is loaded from default files
131 sub ensureLoaded {
132 my $dic = shift;
133 my $rc = 1;
134 if ( defined($dic->{dictFile}) && !$dic->dictOk ) {
135 $dic->info("loading dictionary file '$dic->{dictFile}'");
136 $rc &&= $dic->{ttd}->loadFile($dic->{dictFile}, encoding=>$dic->{encoding});
137 }
138 return $rc;
139 }
140  
141 ##==============================================================================
142 ## Methods: Persistence
143 ##==============================================================================
144  
145 ##======================================================================
146 ## Methods: Persistence: Perl
147  
148 ## @keys = $class_or_obj->noSaveKeys()
149 ## + returns list of keys not to be saved
150 sub noSaveKeys {
151 my $that = shift;
152 return ($that->SUPER::noSaveKeys, qw(ttd));
153 }
154  
155 ## $saveRef = $obj->savePerlRef()
156 ## + inherited from DTA::CAB::Persistent
157  
158 ## $loadedObj = $CLASS_OR_OBJ->loadPerlRef($ref)
159 ## + OLD: implicitly calls $obj->clear()
160 sub loadPerlRef {
161 my ($that,$ref) = @_;
162 my $obj = $that->SUPER::loadPerlRef($ref);
163 #$obj->clear();
164 return $obj;
165 }
166  
167 ##==============================================================================
168 ## Methods: Analysis
169 ##==============================================================================
170  
171 ##------------------------------------------------------------------------
172 ## Methods: Analysis: Generic
173  
174 ## $bool = $anl->canAnalyze()
175 ## + returns true if analyzer can perform its function (e.g. data is loaded & non-empty)
176 ## + override calls dictOk()
177 sub canAnalyze {
178 return $_[0]->dictOk();
179 }
180  
181 ##------------------------------------------------------------------------
182 ## Methods: Analysis: v1.x: API
183  
184 ## $doc = $anl->analyzeTypes($doc,\%types,\%opts)
185 ## + perform type-wise analysis of all (text) types in $doc->{types}
186 sub analyzeTypes {
187 my ($dic,$doc,$types,$opts) = @_;
188  
189 ##-- setup common variables
190 my $allow_re = defined($dic->{allowRegex}) ? qr($dic->{allowRegex}) : undef;
191 my $acode = $dic->analyzeCode;
192  
193 foreach (values %$types) {
194 next if (defined($allow_re) && $_->{text} !~ $allow_re);
195 $acode->();
196 }
197  
198 return $doc;
199 }
200  
201 ##------------------------------------------------------------------------
202 ## Methods: Analysis: Utils
203  
204 ## $prefix = $dict->analyzePre()
205 sub analyzePre {
206 my $dic = shift;
207 return join('',
208 (map {"my $_;\n"}
209 '$dic=$anl',
210 '$lab=$dic->{label}',
211 '$dhash=$dic->dictHash',
212 '($key,$val,@keys,@vals,%vals)'
213 ),
214 ($dic->{analyzePre} ? $dic->{analyzePre} : qw()),
215 );
216 }
217  
218 ## $coderef = $dict->analyzeCode()
219 ## $coderef = $dict->analyzeCode($code)
220 sub analyzeCode {
221 my ($dic,$code) = @_;
222 #return $dic->analyzeCode_dummy($code) if ($dic->{label} eq 'exlex'); ##-- DEBUG
223 $code = defined($dic->{analyzeCode}) ? $dic->{analyzeCode} : $CODE_DEFAULT if (!defined($code));
224 my $acode = $dic->accessClosure($code, pre=>$dic->analyzePre);
225 return $acode;
226 }
227  
228 1; ##-- be happy
229  
230 __END__