CAB/Analyzer/Dict/CDB.pm untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 127 CC 6 SCAR 14.7
CC
6
Cov
0%
ensureLoaded
12.0
clear
6.0
dictHash
6.0
CRAP 4.3
1 ## -*- Mode: CPerl -*-
2 ##
3 ## File: DTA::CAB::Analyzer::Dict::CDB.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: generic analysis dictionary API using Lingua::TT::CDBFile
6  
7 package DTA::CAB::Analyzer::Dict::CDB;
8 use DTA::CAB::Analyzer ':child';
9 use DTA::CAB::Analyzer::Dict;
10 use DTA::CAB::Format;
11 use Lingua::TT::CDBFile;
12 use IO::File;
13 use Carp;
14 use CDB_File;
15 use utf8;
16 use strict;
17  
18 ##==============================================================================
19 ## Globals
20 ##==============================================================================
21  
22 our @ISA = qw(DTA::CAB::Analyzer::Dict);
23  
24 ##==============================================================================
25 ## Constructors etc.
26 ##==============================================================================
27  
28 ## $obj = CLASS_OR_OBJ->new(%args)
29 ## + object structure:
30 ## (
31 ## ##-- Filename Options
32 ## dictFile => $filename, ##-- CDB filename (default=undef)
33 ##
34 ## ##-- Analysis Output
35 ## label => $lab, ##-- analyzer label
36 ## analyzeCode => $code, ##-- pseudo-accessor to perform actual analysis for token ($_); see DTA::CAB::Analyzer::Dict for details
37 ##
38 ## ##-- Analysis objects
39 ## dbf => $dbf, ##-- underlying Lingua::TT::CDBFile object (default=undef)
40 ## dba => \%dbopts, ##-- options for underlying db: default: {mode=>'r',utf8=>1},
41 ## )
42 sub new {
43 my $that = shift;
44 my $dic = $that->SUPER::new(
45 ##-- filenames
46 dictFile => undef,
47  
48 ##-- analysis objects
49 dbf=>undef,
50 dba=>{
51 mode=>'r',
52 utf8=>1,
53 },
54  
55  
56 ##-- analysis output
57 label => 'dict',
58 analyzeCode => ('utf8::decode '.$DTA::CAB::Analyzer::Dict::CODE_DEFAULT),
59  
60 ##-- user args
61 @_
62 );
63 delete($dic->{ttd}); ##-- don't inherit 'ttd' (in-memory hash dict) key
64 return $dic;
65 }
66  
67 ## $dic = $dic->clear()
68 ## + just closes db
69 sub clear {
70 my $dic = shift;
71 $dic->{dbf}->close if ($dic->{dbf} && $dic->{dbf}->opened);
72 delete($dic->{dbf});
73 return $dic;
74 }
75  
76  
77 ##==============================================================================
78 ## Methods: Embedded API
79 ##==============================================================================
80  
81 ## $bool = $dic->dictOk()
82 ## + returns false iff dict is undefined or "empty"
83 sub dictOk {
84 return $_[0]{dbf} && $_[0]{dbf}->opened;
85 }
86  
87 ## \%key2val = $dict->dictHash()
88 ## + returns a (possibly tie()d hash) representing dict contents
89 ## + override returns $dict->{dbf}{data} or a new empty hash
90 sub dictHash {
91 return $_[0]{dbf} && $_[0]{dbf}->opened ? $_[0]{dbf}{data} : {};
92 }
93  
94 ## $val_or_undef = $dict->dictLookup($key)
95 ## + get stored value for key $key
96 ## + default returns $dict->{ttd}{dict}{$key} or undef
97 sub dictLookup {
98 return undef if (!$_[0]{dbf} || !$_[0]{dbf}->opened);
99 return $_[0]{dbf}->fetch($_[1]);
100 }
101  
102  
103 ##==============================================================================
104 ## Methods: I/O
105 ##==============================================================================
106  
107 ##--------------------------------------------------------------
108 ## Methods: I/O: Input: all
109  
110 ## $bool = $dic->ensureLoaded()
111 ## + ensures analyzer data is loaded from default files
112 sub ensureLoaded {
113 my $dic = shift;
114 my $rc = 1;
115 if ( defined($dic->{dictFile}) && !$dic->dictOk ) {
116 $dic->info("opening CDB file '$dic->{dictFile}'");
117 $dic->{dbf} = Lingua::TT::CDBFile->new(%{$dic->{dba}||{}});
118 $rc &&= $dic->{dbf}->open($dic->{dictFile});
119 }
120 $dic->logwarn("error opening CDB file '$dic->{dictFile}': $!") if (!$rc);
121 return $rc;
122 }
123  
124 ##==============================================================================
125 ## Methods: Persistence
126 ##==============================================================================
127  
128 ##======================================================================
129 ## Methods: Persistence: Perl
130  
131 ## @keys = $class_or_obj->noSaveKeys()
132 ## + returns list of keys not to be saved
133 sub noSaveKeys {
134 my $that = shift;
135 return ($that->SUPER::noSaveKeys, qw(dbf));
136 }
137  
138 ## @keys = $class_or_obj->noSaveBinKeys()
139 ## + returns list of keys not to be saved
140 sub noSaveBinKeys {
141 my $that = shift;
142 return ($that->SUPER::noSaveBinKeys, qw(dbf));
143 }
144  
145 ## $saveRef = $obj->savePerlRef()
146 ## + inherited from DTA::CAB::Persistent
147  
148 ## $loadedObj = $CLASS_OR_OBJ->loadPerlRef($ref)
149 ## + implicitly calls $obj->clear()
150 sub loadPerlRef {
151 my ($that,$ref) = @_;
152 my $obj = $that->SUPER::loadPerlRef($ref);
153 return $obj;
154 }
155  
156 ##==============================================================================
157 ## Methods: Analysis
158 ##==============================================================================
159  
160 ##------------------------------------------------------------------------
161 ## Methods: Analysis: Generic
162  
163 ## $bool = $anl->canAnalyze()
164 ## + returns true if analyzer can perform its function (e.g. data is loaded & non-empty)
165 ## + INHERITED from dict: calls dictOk()
166  
167 ##------------------------------------------------------------------------
168 ## Methods: Analysis: v1.x: API
169  
170 ## $doc = $anl->analyzeTypes($doc,\%types,\%opts)
171 ## + perform type-wise analysis of all (text) types in $doc->{types}
172 ## + INHERITED from Dict
173  
174  
175 1; ##-- be happy
176  
177 __END__