TT/Dict.pm

filter: Statementstmt 16.2% 14 / 86 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 25.0% 4 / 16 Podpod 0.0% 0 / 11 totaltotal 9.2% 18 / 194 CC 30 SCAR 41.9
CC
30
Cov
9%
loadNativeFh
156.0
apply
90.0
__ANON__
30.0
CRAP 65.8
« TT/TextAlignment.pm ↑ Summary TT/Packed.pm »
1 ## -*- Mode: CPerl -*-
2 ## File: Lingua::TT::Enum.pm
3 ## Author: Bryan Jurish <TT/IO.pm>
4 ## Descript: TT Utils: in-memory Enum
5  
6 package Lingua::TT::Dict;
7 1 ▶use Lingua::TT::Persistent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Lingua::TT::IO;
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 & Constants
14  
15 1 our @ISA = qw(Lingua::TT::Persistent);
16  
17 ##==============================================================================
18 ## Constructors etc.
19  
20 ## $dict = CLASS_OR_OBJECT->new(%opts)
21 ## + %opts, %$dict:
22 ## dict => \%key2val, ##-- dict data
23 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
24 0 my $that = shift;
25 0 ▶ my $dict = bless({
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
26 dict=>{},
27 @_
28 }, ref($that)||$that);
29 0 return $dict;
30 }
31  
32 ## undef = $dict->clear()
33 0 ▶sub clear {
Subroutine clear: not called CC=1 SCAR=6.9
Pod undocumented
34 0 my $dict = shift;
35 0 %{$dict->{dict}} = qw();
36 0 return $dict;
37 }
38  
39 ##==============================================================================
40 ## Methods: Access and Manipulation
41  
42 ## $n_keys = $dict->size()
43 0 ▶sub size {
Subroutine size: not called CC=1 SCAR=6.9
Pod undocumented
44 0 return scalar CORE::keys %{$_[0]{dict}};
45 }
46  
47 ## @keys = $dict->keys()
48 0 ▶sub keys {
Subroutine keys: not called CC=1 SCAR=6.9
Pod undocumented
49 0 return CORE::keys %{$_[0]{dict}};
50 }
51  
52 ## $val = $dict->get($key)
53 0 ▶sub get {
Subroutine get: not called CC=1 SCAR=6.9
Pod undocumented
54 0 return $_[0]{dict}{$_[1]};
55 }
56  
57 ##==============================================================================
58 ## Methods: merge
59  
60 ## $dict = $dict->merge($dict2, %opts)
61 ## + include $dict2 entries in $dict, destructively alters $dict
62 ## + %opts:
63 ## append => $bool, ##-- if true, $dict2 values are appended to $dict1 values
64 0 ▶sub merge {
Subroutine merge: not called CC=4 SCAR=30.0
Pod undocumented
65 0 my ($d1,$d2,%opts) = @_;
66 0 ▶ if (!$opts{append}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $opts{'append'}) { } 0 0 0
67 0 @{$d1->{dict}}{CORE::keys %{$d2->{dict}}} = CORE::values %{$d2->{dict}}; ##-- clobber
68 } else {
69 0 my $h1 = $d1->{dict};
70 0 my $h2 = $d2->{dict};
71 0 my ($key,$val);
72 0 ▶ while (($key,$val)=each %$h2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$h2) 0 0 0
73 0 ▶ $h1->{$key} = exists($h1->{$key}) ? "$h1->{$key}\t$val" : $val;
Branch: 1 branch
BranchTrueFalseTotal
exists $h1->{$key} ? : 0 0 0
74 }
75 }
76 0 return $d1;
77 }
78  
79  
80 ##==============================================================================
81 ## Methods: Apply
82  
83 ## \&apply = $dict->applySub(%opts)
84 ## + returns a CODE-ref for applying dictionary analysis to a single item
85 ## + returned sub is called without arguments
86 ## - data line to be analyzed (chomped) is in $_
87 ## - output for current data line should be stored in $_
88 0 ▶sub applySub {
Subroutine applySub: not called CC=1 SCAR=6.9
Pod undocumented
89 0 my ($dict,%opts) = @_;
90 0 my $dh = $dict->{dict};
91 0 my $include_empty = $opts{allow_empty};
92 0 my ($text,$a_in,$a_dict);
93 0 ▶ return sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
94 0 ($text,$a_in) = split(/\t/,$_,2);
95 0 $a_dict = $dh->{$text};
96 0 ▶ $_ = join("\t", $text, (defined($a_in) ? $a_in : qw()), (defined($a_dict) && ($include_empty || $a_dict ne '') ? $a_dict : qw()))."\n";
Branch: 2 branches
BranchTrueFalseTotal
defined $a_in ? : 0 0 0
defined $a_dict && ($include_empty || $a_dict ne '') ? : 0 0 0
Condition0%0/6
defined $a_dict and $include_empty || $a_dict ne ""
!ll&&!rl&&r
000
$include_empty or $a_dict ne ""
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $a_dict and $include_empty || $a_dict ne ""
defined $a_dict $include_empty $a_dict ne ""
Truth table0%0/4
A and B or C
A= defined $a_dictB= $include_emptyC= $a_dict ne ""
ABCresult
0XX0
1000
11X1
1011
97 0 };
98 }
99  
100 ## $bool = $dict->apply($infh,$outfh,%opts)
101 ## + apply dict to TT data from $infh, writing output to $outfh
102 ## + uses $dict->applySub() for actual analysis
103 ## + %opts:
104 ## allow_empty => $bool, ##-- include empty analyses? (default=0)
105 0 ▶sub apply {
Subroutine apply: not called CC=9 SCAR=45.0
Pod undocumented
106 0 my ($dict,$infh,$outfh,%opts) = @_;
107 0 ▶ $infh = $infh->{fh} if (UNIVERSAL::isa($infh,'HASH') && defined($infh->{fh}));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($infh, "HASH") and defined $infh->{"fh"} 0 0 0
Condition0%0/3
UNIVERSAL::isa($infh, "HASH") and defined $infh->{"fh"}
!ll&&!rl&&r
000
MC/DC0%0/2
UNIVERSAL::isa($infh, "HASH") and defined $infh->{"fh"}
UNIVERSAL::isa($infh, "HASH") defined $infh->{"fh"}
Truth table0%0/3
A and B
A= UNIVERSAL::isa($infh, "HASH")B= defined $infh->{"fh"}
ABresult
0X0
100
111
108 0 ▶ $outfh = $outfh->{fh} if (UNIVERSAL::isa($outfh,'HASH') && defined($outfh->{fh}));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($outfh, "HASH") and defined $outfh->{"fh"} 0 0 0
Condition0%0/3
UNIVERSAL::isa($outfh, "HASH") and defined $outfh->{"fh"}
!ll&&!rl&&r
000
MC/DC0%0/2
UNIVERSAL::isa($outfh, "HASH") and defined $outfh->{"fh"}
UNIVERSAL::isa($outfh, "HASH") defined $outfh->{"fh"}
Truth table0%0/3
A and B
A= UNIVERSAL::isa($outfh, "HASH")B= defined $outfh->{"fh"}
ABresult
0X0
100
111
109 0 my $apply = $dict->applySub(%opts);
110 0 ▶ while (defined($_=<$infh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $infh)) 0 0 0
111 0 ▶ next if (/^%%/ || /^$/); ##-- ignore comments and blank lines (pass-through)
Branch: 1 branch
BranchTrueFalseTotal
if /^%%/ or /^$/ 0 0 0
Condition0%0/3
/^%%/ or /^$/
l!l&&r!l&&!r
000
MC/DC0%0/2
/^%%/ or /^$/
/^%%/ /^$/
Truth table0%0/3
A or B
A= /^%%/B= /^$/
ABresult
1X1
011
000
112 0 chomp;
113 0 $apply->();
114 }
115 continue {
116 0 ▶ $outfh->print($_) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$outfh->print($_) or return undef 0 0 0
117 }
118 0 return 1;
119 }
120  
121 ##==============================================================================
122 ## Methods: I/O
123  
124 ##--------------------------------------------------------------
125 ## Methods: I/O: generic
126  
127 ## $bool = $dict->setFhLayers($fh,%opts)
128 0 ▶sub setFhLayers {
Subroutine setFhLayers: not called CC=2 SCAR=17.9
Pod undocumented
129 0 my ($obj,$fh,%opts) = @_;
130 0 ▶ binmode($fh,":encoding($opts{encoding})") if (defined($opts{encoding}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"encoding"} 0 0 0
131 }
132  
133 ##--------------------------------------------------------------
134 ## Methods: I/O: Native
135  
136 ## $bool = $dict->saveNativeFh($fh,%opts)
137 ## + saves to filehandle
138 ## + %opts
139 ## encoding => $enc, ##-- sets $fh :encoding flag if defined; default: none
140 0 ▶sub saveNativeFh {
Subroutine saveNativeFh: not called CC=2 SCAR=17.9
Pod undocumented
141 0 my ($dict,$fh,%opts) = @_;
142 0 $dict->setFhLayers($fh,%opts);
143 0 my ($key,$val);
144 0 ▶ while (($key,$val)=each(%{$dict->{dict}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$dict{"dict"};}) 0 0 0
145 0 $fh->print($key, "\t", $val, "\n");
146 }
147 0 return $dict;
148 }
149  
150 ## $bool = $dict->loadNativeFh($fh)
151 ## + loads from handle
152 ## + %opts
153 ## encoding => $enc, ##-- sets $fh :encoding flag if defined; default: none
154 ## append => $bool, ##-- if true, multiple entries for a single key will be appended (TAB-separated)
155 0 ▶sub loadNativeFh {
Subroutine loadNativeFh: not called CC=12 SCAR=50.5
Pod undocumented
156 0 my ($dict,$fh,%opts) = @_;
157 0 $dict->setFhLayers($fh,%opts);
158 0 ▶ $dict = $dict->new() if (!ref($dict));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $dict 0 0 0
159 0 my $dh = $dict->{dict};
160 0 my ($line,$key,$val);
161 0 ▶ if ($opts{append}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'append'}) { } 0 0 0
162 ##-- append mode
163 0 ▶ while (defined($line=<$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 0 0 0
164 0 chomp($line);
165 0 ▶ next if ($line =~ /^\s*$/ || $line =~ /^%%/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ or $line =~ /^%%/ 0 0 0
Condition0%0/3
$line =~ /^\s*$/ or $line =~ /^%%/
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /^\s*$/ or $line =~ /^%%/
$line =~ /^\s*$/ $line =~ /^%%/
Truth table0%0/3
A or B
A= $line =~ /^\s*$/B= $line =~ /^%%/
ABresult
1X1
011
000
166 0 ($key,$val) = split(/\t/,$line,2);
167 0 ▶ next if (!defined($val)); ##-- don't store keys for undef values (but do for empty string)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
168 0 ▶ if (exists($dh->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dh->{$key}) { } 0 0 0
169 0 $dh->{$key} .= "\t$val";
170 } else {
171 0 $dh->{$key} = $val;
172 }
173 }
174 } else {
175 ##-- clobber mode (default)
176 0 ▶ while (defined($line=<$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 0 0 0
177 0 chomp($line);
178 0 ▶ next if ($line =~ /^\s*$/ || $line =~ /^%%/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ or $line =~ /^%%/ 0 0 0
Condition0%0/3
$line =~ /^\s*$/ or $line =~ /^%%/
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /^\s*$/ or $line =~ /^%%/
$line =~ /^\s*$/ $line =~ /^%%/
Truth table0%0/3
A or B
A= $line =~ /^\s*$/B= $line =~ /^%%/
ABresult
1X1
011
000
179 0 ($key,$val) = split(/\t/,$line,2);
180 0 ▶ next if (!defined($val)); ##-- don't store keys for undef values (but do for empty string)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
181 0 $dh->{$key} = $val;
182 }
183 }
184 0 return $dict;
185 }
186  
187 ##--------------------------------------------------------------
188 ## Methods: I/O: Bin
189  
190 ## ($serialized_string,\@other_refs) = STORABLE_freeze($obj, $cloning_flag)
191  
192 ## $obj = STORABLE_thaw($obj, $cloning_flag, $serialized_string, @other_refs)
193  
194 ##==============================================================================
195 ## Footer
196 1 1;
197  
198 __END__
« TT/TextAlignment.pm ↑ Summary TT/Packed.pm »