CAB/Analyzer/DocClassify.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 127 CC 6 SCAR 18.2
CC
6
Cov
0%
analyzeDocument
20.0
ensureLoaded
6.0
loadMap
6.0
CRAP 6.2
1 ## -*- Mode: CPerl -*-
2 ##
3 ## File: DTA::CAB::Analyzer::DocClassify.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: DocClassify::Mapper wrapper
6  
7 package DTA::CAB::Analyzer::DocClassify;
8 use DTA::CAB::Analyzer;
9 use DTA::CAB::Datum ':all';
10 use DocClassify;
11  
12 use Encode qw(encode decode);
13 use IO::File;
14 use Carp;
15  
16 use strict;
17  
18 ##==============================================================================
19 ## Globals
20 ##==============================================================================
21  
22 our @ISA = qw(DTA::CAB::Analyzer);
23  
24 ##==============================================================================
25 ## Constructors etc.
26 ##==============================================================================
27  
28 ## $obj = CLASS_OR_OBJ->new(%args)
29 ## + object structure:
30 ## (
31 ## ##-- Filename Options
32 ## mapFile => $filename, ##-- binary source file for 'map' (default: none) : REQUIRED
33 ##
34 ## ##-- Analysis Options
35 ## label => $label, ##-- document destination key (default='classified')
36 ## analyzeClearBody => $bool, ##-- if true, document analysis routine will wipe $doc->{body} (default=false)
37 ##
38 ## ##-- Analysis Objects
39 ## map => $map, ##-- a DocClassify::Mapper object
40 ## )
41 sub new {
42 my $that = shift;
43 my $dc = $that->SUPER::new(
44 ##-- filenames
45 mapFile => undef,
46  
47 ##-- options
48 label => 'classified',
49 analyzeClearBody => 0,
50  
51 ##-- analysis objects
52 #map => undef,
53  
54 ##-- user args
55 @_
56 );
57 return $dc;
58 }
59  
60 ## $dc = $dc->clear()
61 sub clear {
62 my $dc = shift;
63  
64 ##-- analysis sub(s)
65 $dc->dropClosures();
66  
67 ##-- analysis objects
68 delete($dc->{map});
69  
70 return $dc;
71 }
72  
73 ##==============================================================================
74 ## Methods: Generic
75 ##==============================================================================
76  
77 ## $bool = $dc->mapOk()
78 ## + should return false iff map is undefined or "empty"
79 ## + default version checks for non-empty 'map'
80 sub mapOk {
81 return defined($_[0]{map});
82 }
83  
84 ##==============================================================================
85 ## Methods: I/O
86 ##==============================================================================
87  
88 ##--------------------------------------------------------------
89 ## Methods: I/O: Input: all
90  
91 ## $bool = $dc->ensureLoaded()
92 ## + ensures model data is loaded from default files (if available)
93 sub ensureLoaded {
94 my $dc = shift;
95 ##-- ensure: map
96 if ( defined($dc->{mapFile}) && !$dc->mapOk ) {
97 return $dc->loadMap($dc->{mapFile});
98 }
99 return 1; ##-- allow empty models
100 }
101  
102 ##--------------------------------------------------------------
103 ## Methods: I/O: Input: Map
104  
105 ## $dc = $dc->loadMap($map_file)
106 sub loadMap {
107 my ($dc,$mapfile) = @_;
108 $dc->info("loading map file '$mapfile'");
109 $dc->{map} = 'DocClassify::Mapper' if (!defined($dc->{map}));
110 $dc->{map} = $dc->{map}->loadFile($mapfile)
111 or $dc->logconfess("loadFile(): load failed for '$mapfile': $!");
112 $dc->dropClosures();
113 return $dc;
114 }
115  
116 ##==============================================================================
117 ## Methods: Persistence
118 ##==============================================================================
119  
120 ##======================================================================
121 ## Methods: Persistence: Perl
122  
123 ## @keys = $class_or_obj->noSaveKeys()
124 ## + returns list of keys not to be saved
125 sub noSaveKeys {
126 my $that = shift;
127 return ($that->SUPER::noSaveKeys, qw(map));
128 }
129  
130 ## $saveRef = $obj->savePerlRef()
131 ## + inherited from DTA::CAB::Persistent
132  
133 ## $loadedObj = $CLASS_OR_OBJ->loadPerlRef($ref)
134 ## + implicitly calls $obj->clear()
135 sub loadPerlRef {
136 my ($that,$ref) = @_;
137 my $obj = $that->SUPER::loadPerlRef($ref);
138 $obj->clear();
139 return $obj;
140 }
141  
142 ##==============================================================================
143 ## Methods: Analysis
144 ##==============================================================================
145  
146 ##------------------------------------------------------------------------
147 ## Methods: Analysis: Generic
148  
149 ## $bool = $anl->canAnalyze()
150 ## + returns true if analyzer can perform its function (e.g. data is loaded & non-empty)
151 sub canAnalyze {
152 return $_[0]->mapOk();
153 }
154  
155 ##------------------------------------------------------------------------
156 ## Methods: Analysis: v1.x: API
157  
158 ## $doc = $anl->analyzeDocument($doc,\%opts)
159 ## + analyze a DTA::CAB::Document $doc
160 ## + top-level API routine
161 sub analyzeDocument {
162 my ($anl,$doc,$opts) = @_;
163 return undef if (!$anl->ensureLoaded()); ##-- uh-oh...
164 return $doc if (!$anl->canAnalyze); ##-- ok...
165 $doc = toDocument($doc);
166  
167 ##-- vars
168 my $dc = $anl; ##-- hack
169 my $lab = $dc->{label};
170  
171 my $map = $dc->{map};
172 my $dcdoc = $dc->{_dcdoc} = DocClassify::Document->new(string=>"<doc type=\"dummy\" src=\"$dc\"/>\n",label=>(ref($dc)." dummy document"));
173 my $dcsig = DocClassify::Signature->new();
174 my $sig_tf = $dcsig->{tf};
175 my $sig_Nr = \$dcsig->{N};
176  
177 ##-- populate signature from non-refs in tokens
178 %$sig_tf = qw();
179 $$sig_Nr = 0;
180 my ($s,$w,$wkey);
181 foreach $s (@{$doc->{body}}) {
182 foreach $w (@{$s->{tokens}}) {
183 $wkey = join("\t", map {"$_=$w->{$_}"} grep {!ref($w->{$_})} sort keys(%$w));
184 $sig_tf->{$wkey}++;
185 $$sig_Nr++;
186 }
187 }
188  
189 ##-- map & annotate
190 $dcdoc->{sig} = $dcsig;
191 $map->mapDocument($dcdoc);
192 $doc->{$dc->{label}} = [ $dcdoc->cats() ];
193 @{$doc->{body}} = qw() if ($dc->{analyzeClearBody});
194  
195 ##-- cleanup
196 @{$dcdoc->{cats}} = qw();
197 $dcdoc->clearCache();
198 $dcsig->clear();
199  
200 ##-- return
201 return $doc;
202 }
203  
204  
205 1; ##-- be happy
206  
207 __END__