CAB/Document.pm

filter: Statementstmt 9.9% 11 / 111 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 14.2% 3 / 21 Podpod 11.1% 2 / 18 totaltotal 7.1% 16 / 224 CC 40 SCAR 28.2
CC
40
Cov
6%
expandTypeKeys
42.0
expandTypes
30.0
getTypes
20.0
CRAP 16.8
« CAB/Queue/File.pm ↑ Summary CAB/Server/HTTP/Handler/QueryFormats.pm »
1 ## -*- Mode: CPerl -*-
2 ##
3 ## File: DTA::CAB::Document.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: generic API for whole documents passed to/from DTA::CAB::Analyzer
6  
7 package DTA::CAB::Document;
8 1 ▶use DTA::CAB::Datum;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 ##==============================================================================
13 ## Globals
14 ##==============================================================================
15  
16 1 our @ISA = qw(DTA::CAB::Datum);
17  
18 ##==============================================================================
19 ## Constructors etc.
20 ##==============================================================================
21  
22 ## $doc = CLASS_OR_OBJ->new(\@sentences,%args)
23 ## + object structure: HASH
24 ## {
25 ## body => \@sentences, ##-- DTA::CAB::Sentence objects
26 ## types => \%text2tok, ##-- maps token text type-wise to Token objects (optional)
27 ## ##
28 ## ##-- special attributes
29 ## #noTypeKeys => \@keys, ##-- token keys which should not be mapped to/from types (default='_xmlnod')
30 ## ##
31 ## ##-- dta-tokwrap attributes
32 ## xmlbase => $base,
33 ## }
34 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
35 0 ▶ return bless({
Branch: 1 branch
BranchTrueFalseTotal
$#_ >= 1 ? : 0 0 0
Condition0%0/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
36 body => ($#_>=1 ? $_[1] : []),
37 #noTypeKeys => [qw(_xmlnod)],
38 @_[2..$#_],
39 }, ref($_[0])||$_[0]);
40 }
41  
42 ##==============================================================================
43 ## Methods: ???
44 ##==============================================================================
45  
46 ## $n = $doc->nTokens()
47 0 ▶sub nTokens {
Subroutine nTokens: not called CC=2 SCAR=17.9
Pod documented
48 0 my $ntoks = 0;
49 0 $ntoks += scalar(@{$_->{tokens}}) foreach (@{$_[0]->{body}});
50 0 return $ntoks;
51 }
52  
53 ## $n = $doc->nChars()
54 ## + total number of token text characters
55 0 ▶sub nChars {
Subroutine nChars: not called CC=2 SCAR=17.9
Pod undocumented
56 0 my $nchars = 0;
57 0 $nchars += length($_->{text}) foreach (map {@{$_->{tokens}}} @{$_[0]->{body}});
58 0 return $nchars;
59 }
60  
61 ## \%types = $doc->types()
62 ## + get hash \%types = ($typeText => $typeToken, ...) mapping token text to
63 ## basic token objects (with only 'text' key defined)
64 ## + just returns cached $doc->{types} if defined
65 ## + otherwise computes & caches in $doc->{types}
66 0 ▶sub types {
Subroutine types: not called CC=2 SCAR=17.9
Pod undocumented
67 0 ▶ return $_[0]{types} if ($_[0]{types});
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"types"} 0 0 0
68 0 return $_[0]->getTypes();
69 }
70  
71 ## \%types = $doc->getTypes()
72 ## + (re-)computes hash \%types = ($typeText => $typeToken, ...) mapping token text to
73 ## token objects (with all but @{$doc->{noTypeKeys}} keys)
74 0 ▶sub getTypes {
Subroutine getTypes: not called CC=4 SCAR=30.0
Pod undocumented
75 0 my $doc = shift;
76 0 my $types = $doc->{types} = {};
77 0 ▶ my @nokeys = @{$doc->{noTypeKeys}||[qw(moot dmoot)]};
Condition0%0/2
$doc->{'noTypeKeys'} || ['moot', 'dmoot']
l!l
00
MC/DC0%0/1
$doc->{'noTypeKeys'} || ['moot', 'dmoot']
$doc->{'noTypeKeys'}
Truth table0%0/2
A
A= $doc->{'noTypeKeys'}
Aresult
11
00
78 0 my ($typ);
79 0 foreach (map {@{$_->{tokens}}} @{$doc->{body}}) {
80 0 ▶ next if (exists($types->{$_->{text}}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $$types{$_->{"text"}} 0 0 0
81 0 $typ = $types->{$_->{text}} = {%$_};
82 0 delete(@$typ{@nokeys});
83 }
84 0 return $types;
85 }
86  
87 ## \%types = $doc->getTextTypes()
88 ## + (re-)computes hash \%types = ($typeText => {text=>$typeText}, ...) mapping token text to
89 ## basic token objects (with only 'text' key defined)
90 0 ▶sub getTextTypes {
Subroutine getTextTypes: not called CC=3 SCAR=24.8
Pod undocumented
91 0 my $doc = shift;
92 0 my $types = $doc->{types} = {};
93 0 my ($typ);
94 0 foreach (map {@{$_->{tokens}}} @{$doc->{body}}) {
95 0 ▶ next if (exists($types->{$_->{text}}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $$types{$_->{"text"}} 0 0 0
96 0 $typ = $types->{$_->{text}} = {text=>$_->{text}};
97 }
98 0 return $types;
99 }
100  
101 ## \%types = $doc->extendTypes(\%types,@keys)
102 ## + extends \%types with token keys @keys
103 0 ▶sub extendTypes {
Subroutine extendTypes: not called CC=4 SCAR=30.0
Pod undocumented
104 0 my ($doc,$types,@keys) = @_;
105 0 ▶ $types = $doc->types() if (!defined($types));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $types 0 0 0
106 0 my ($tok);
107 0 foreach $tok (map {@{$_->{tokens}}} @{$doc->{body}}) {
108 0 $types->{$tok->{text}}{$_} = Storable::dclone($tok->{$_}) foreach (@keys);
109 }
110 0 return $types;
111 }
112  
113 ## $doc = $doc->expandTypes()
114 ## $doc = $doc->expandTypes(\%types)
115 ## $doc = $doc->expandTypes(\@keys,\%types)
116 ## $doc = $doc->expandTypes(\@keys,\%types,\%opts)
117 ## + expands \%types (default=$doc->{types}) map into tokens
118 ## + clobbers all keys
119 0 ▶sub expandTypes {
Subroutine expandTypes: not called CC=5 SCAR=34.0
Pod undocumented
120 0 ▶ return $_[0]->expandTypeKeys(@_[1,2]) if (@_>2);
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 0 0
121 0 my ($doc,$types) = @_;
122 0 ▶ $types = $doc->{types} if (!$types);
Branch: 1 branch
BranchTrueFalseTotal
unless $types 0 0 0
123 0 ▶ return $doc if (!$types); ##-- no {types} key
Branch: 1 branch
BranchTrueFalseTotal
unless $types 0 0 0
124 0 my ($typ);
125 0 foreach (map {@{$_->{tokens}}} @{$doc->{body}}) {
126 0 $typ = $types->{$_->{text}};
127 0 @$_{keys %$typ} = values %$typ;
128 }
129 0 return $doc;
130 }
131  
132 ## $doc = $doc->expandTypeKeys(\@typeKeys)
133 ## $doc = $doc->expandTypeKeys(\@typeKeys,\%types)
134 ## $doc = $doc->expandTypeKeys(\@typeKeys,\%types,\%opts)
135 ## + expands \%types (default=$doc->{types}) map into tokens
136 ## + only keys in \@typeKeys are expanded
137 0 ▶sub expandTypeKeys {
Subroutine expandTypeKeys: not called CC=6 SCAR=37.4
Pod undocumented
138 0 my ($doc,$keys,$types) = @_;
139 0 ▶ $types = $doc->{types} if (!$types);
Branch: 1 branch
BranchTrueFalseTotal
unless $types 0 0 0
140 0 ▶ return $doc if (!$types || !$keys || !@$keys); ##-- no {types} key, or no keys to expand
Branch: 1 branch
BranchTrueFalseTotal
unless $types and $keys and @$keys 0 0 0
Condition0%0/6
$types and $keys and @$keys
!ll&&!rl&&r
000
$types and $keys
!ll&&!rl&&r
000
MC/DC0%0/3
$types and $keys and @$keys
$types $keys @$keys
Truth table0%0/4
A and B and C
A= $typesB= $keysC= @$keys
ABCresult
0XX0
10X0
1100
1111
141 0 my ($typ,$tok);
142 0 foreach $tok (map {@{$_->{tokens}}} @{$doc->{body}}) {
143 0 $typ = $types->{$tok->{text}};
144 0 @$tok{@$keys} = @$typ{@$keys};
145 #$tok{$_}=$typ->{$_} foreach (grep {defined($typ->{$_})} @$keys); ##-- don't put undef keys into tok in the first place
146 #delete(@$tok{grep {!defined($tok->{$_})} @$keys}); ##-- ... or remove undef keys from tok after the fact
147 ## + both of these undef-pruners are kind of useless here, since undef values sometimes come back via 'map'
148 ## e.g. in (...map {$_ ? @$_ : qw()} @$w{qw(tokpp toka mlatin)}...) as used in Analyzer::Moot code
149 ## + this should really be something for e.g. analyzeClean(), but that now means something else
150 }
151 0 return $doc;
152 }
153  
154 ## $doc = $doc->clearTypes()
155 ## + clears {types} cache
156 0 ▶sub clearTypes {
Subroutine clearTypes: not called CC=1 SCAR=6.9
Pod undocumented
157 0 delete $_[0]{types};
158 0 return $_[0];
159 }
160  
161 ##==============================================================================
162 ## I/O wrappers
163  
164 ##--------------------------------------------------------------
165 ## I/O wrappers: input
166  
167 ## $doc = CLASS_OR_OBJECT->fromDocument($doc)
168 0 ▶sub fromDocument {
Subroutine fromDocument: not called CC=3 SCAR=24.8
Pod undocumented
169 0 ▶ if (ref($_[0]) && UNIVERSAL::isa($_[0],__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and UNIVERSAL::isa($_[0], __PACKAGE__)) 0 0 0
Condition0%0/3
ref $_[0] and UNIVERSAL::isa($_[0], __PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and UNIVERSAL::isa($_[0], __PACKAGE__)
ref $_[0] UNIVERSAL::isa($_[0], __PACKAGE__)
Truth table0%0/3
A and B
A= ref $_[0]B= UNIVERSAL::isa($_[0], __PACKAGE__)
ABresult
0X0
100
111
170 0 %{$_[0]} = %{$_[1]};
171 0 return $_[0];
172 }
173 0 return $_[1];
174 }
175  
176 ## $doc = CLASS_OR_OBJECT->fromFile($filename_or_fh,%fmt_options)
177 0 ▶sub fromFile {
Subroutine fromFile: not called CC=3 SCAR=24.8
Pod undocumented
178 0 ▶ my $fmt = DTA::CAB::Format->newReader(file=>$_[1],@_[2..$#_])
Branch: 1 branch
BranchTrueFalseTotal
my $fmt = "DTA::CAB::Format"->newReader("file", $_[1], @_[2 .. $#_]) or $_[0]->logconfess("fromFile(): could not create format for '$_[1]': $!") 0 0 0
179 or $_[0]->logconfess("fromFile(): could not create format for '$_[1]': $!");
180 0 ▶ my $doc = $fmt->parseFile($_[1])
Branch: 1 branch
BranchTrueFalseTotal
my $doc = $fmt->parseFile($_[1]) or $_[0]->logconfess("fromFile(): could not pase file '$_[1]': $!") 0 0 0
181 or $_[0]->logconfess("fromFile(): could not pase file '$_[1]': $!");
182 0 return $_[0]->fromDocument($doc);
183 }
184  
185 ## $doc = CLASS_OR_OBJECT->fromFh($fh,%fmt_options)
186 0 ▶sub fromFh {
Subroutine fromFh: not called CC=3 SCAR=24.8
Pod undocumented
187 0 ▶ my $fmt = DTA::CAB::Format->newReader(@_[2..$#_])
Branch: 1 branch
BranchTrueFalseTotal
my $fmt = "DTA::CAB::Format"->newReader(@_[2 .. $#_]) or $_[0]->logconfess("fromFh(): could not create format for filehandle $_[1]: $!") 0 0 0
188 or $_[0]->logconfess("fromFh(): could not create format for filehandle $_[1]: $!");
189 0 ▶ my $doc = $fmt->parseFh($_[1])
Branch: 1 branch
BranchTrueFalseTotal
my $doc = $fmt->parseFh($_[1]) or $_[0]->logconfess("fromFile(): could not pase filehandle $_[1]: $!") 0 0 0
190 or $_[0]->logconfess("fromFile(): could not pase filehandle $_[1]: $!");
191 0 return $_[0]->fromDocument($doc);
192 }
193  
194 ## $doc = CLASS_OR_OBJECT->fromString( $str,%fmt_options)
195 ## $doc = CLASS_OR_OBJECT->fromString(\$str,%fmt_options)
196 0 ▶sub fromString {
Subroutine fromString: not called CC=3 SCAR=24.8
Pod undocumented
197 0 ▶ my $fmt = DTA::CAB::Format->newReader(@_[2..$#_])
Branch: 1 branch
BranchTrueFalseTotal
my $fmt = "DTA::CAB::Format"->newReader(@_[2 .. $#_]) or $_[0]->logconfess("fromFh(): could not create format: $!") 0 0 0
198 or $_[0]->logconfess("fromFh(): could not create format: $!");
199 0 ▶ my $doc = $fmt->parseString($_[1])
Branch: 1 branch
BranchTrueFalseTotal
my $doc = $fmt->parseString($_[1]) or $_[0]->logconfess("fromFile(): could not pase string: $!") 0 0 0
200 or $_[0]->logconfess("fromFile(): could not pase string: $!");
201 0 return $_[0]->fromDocument($doc);
202 }
203  
204 ##--------------------------------------------------------------
205 ## I/O wrappers: output
206  
207 ## $doc = CLASS_OR_OBJECT->toFormat($fmt)
208 0 ▶sub toFormat {
Subroutine toFormat: not called CC=3 SCAR=24.8
Pod undocumented
209 0 my $fmt = $_[1];
210 0 ▶ $fmt->putDocumentRaw($_[0])
Branch: 1 branch
BranchTrueFalseTotal
$fmt->putDocumentRaw($_[0]) or $_[0]->logconfess("toFormat(): ", ref $fmt, "->putDocumentRaw() failed: $!") 0 0 0
211 or $_[0]->logconfess("toFormat(): ", ref($fmt), "->putDocumentRaw() failed: $!");
212 0 ▶ $fmt->flush()
Branch: 1 branch
BranchTrueFalseTotal
$fmt->flush or $_[0]->logconfess("toFormat(): ", ref $fmt, "->flush() failed: $!") 0 0 0
213 or $_[0]->logconfess("toFormat(): ", ref($fmt), "->flush() failed: $!");
214 0 return $_[0];
215 }
216  
217 ## $doc = CLASS_OR_OBJECT->toFile($filename_or_fh,%fmt_options)
218 0 ▶sub toFile {
Subroutine toFile: not called CC=3 SCAR=24.8
Pod undocumented
219 0 ▶ my $fmt = DTA::CAB::Format->newWriter(file=>$_[1],@_[2..$#_])
Branch: 1 branch
BranchTrueFalseTotal
my $fmt = "DTA::CAB::Format"->newWriter("file", $_[1], @_[2 .. $#_]) or $_[0]->logconfess("toFile(): could not create format for '$_[1]': $!") 0 0 0
220 or $_[0]->logconfess("toFile(): could not create format for '$_[1]': $!");
221 0 ▶ $fmt->toFile($_[1])
Branch: 1 branch
BranchTrueFalseTotal
$fmt->toFile($_[1]) or $_[0]->logconfess("toFile(): ", ref $fmt, "->toFile() failed for '$_[1]': $!") 0 0 0
222 or $_[0]->logconfess("toFile(): ", ref($fmt), "->toFile() failed for '$_[1]': $!");
223 0 return $_[0]->toFormat($fmt);
224 }
225  
226 ## $doc = CLASS_OR_OBJECT->toFh($fh,%fmt_options)
227 0 ▶sub toFh {
Subroutine toFh: not called CC=3 SCAR=24.8
Pod undocumented
228 0 ▶ my $fmt = DTA::CAB::Format->newWriter(@_[2..$#_])
Branch: 1 branch
BranchTrueFalseTotal
my $fmt = "DTA::CAB::Format"->newWriter(@_[2 .. $#_]) or $_[0]->logconfess("toFh(): could not create format for '$_[1]': $!") 0 0 0
229 or $_[0]->logconfess("toFh(): could not create format for '$_[1]': $!");
230 0 ▶ $fmt->toFh($_[1])
Branch: 1 branch
BranchTrueFalseTotal
$fmt->toFh($_[1]) or $_[0]->logconfess("toFh(): ", ref $fmt, "->toFh() failed for '$_[1]': $!") 0 0 0
231 or $_[0]->logconfess("toFh(): ", ref($fmt), "->toFh() failed for '$_[1]': $!");
232 0 return $_[0]->toFormat($fmt);
233 }
234  
235 ## \$str = CLASS_OR_OBJECT->toString(\$str,%fmt_options)
236 ## $str = CLASS_OR_OBJECT->toString( $str,%fmt_options)
237 0 ▶sub toString {
Subroutine toString: not called CC=4 SCAR=30.0
Pod undocumented
238 0 ▶ my $fmt = DTA::CAB::Format->newWriter(@_[2..$#_])
Branch: 1 branch
BranchTrueFalseTotal
my $fmt = "DTA::CAB::Format"->newWriter(@_[2 .. $#_]) or $_[0]->logconfess("toString(): could not create format for '$_[1]': $!") 0 0 0
239 or $_[0]->logconfess("toString(): could not create format for '$_[1]': $!");
240 0 ▶ $fmt->toString($_[1])
Branch: 1 branch
BranchTrueFalseTotal
$fmt->toString($_[1]) or $_[0]->logconfess("toString(): ", ref $fmt, "->toString() failed for '$_[1]': $!") 0 0 0
241 or $_[0]->logconfess("toString(): ", ref($fmt), "->toString() failed for '$_[1]': $!");
242 0 ▶ $_[0]->toFormat($fmt) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->toFormat($fmt) or return undef 0 0 0
243 0 return $_[1];
244 }
245  
246  
247 1 1; ##-- be happy
248  
249 __END__
« CAB/Queue/File.pm ↑ Summary CAB/Server/HTTP/Handler/QueryFormats.pm »