CCDICT.pm

filter: Statementstmt 98.3% 122 / 124 Branchbran 86.5% 45 / 52 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 21 / 21 Podpod 100% 3 / 3 totaltotal 93.3% 196 / 210 CC 30 SCAR 27.4
CC
30
Cov
94%
parse_source_file
27.6
new
2.0
_add_entry
2.0
CRAP 15.5
« CCDICT/Storage/BerkeleyDB.pm ↑ Summary CCDICT/Romanization/Pinyin.pm »
1 package Lingua::ZH::CCDICT;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use vars qw ($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 $VERSION = '0.05';
11  
12 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Sub::Name qw( subname );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Lingua::ZH::CCDICT::Romanization;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Lingua::ZH::CCDICT::Romanization::Pinyin;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 my %storage;
19 1 ▶BEGIN { %storage = map { lc $_ => $_ } ( qw( InMemory XML BerkeleyDB ) ) }
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 sub new
23 1 ▶{
Subroutine new: called CC=2 SCAR=6.9
Pod documented
24 1 my $class = shift;
25 my %p = validate_with( params => \@_,
26 spec =>
27 { storage =>
28 { callbacks =>
29 { 'is a valid storage type' =>
30 1 ▶ sub { $storage{ lc $_[0] } } },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 },
32 },
33 1 allow_extra => 1,
34 );
35  
36 1 my $storage_class = __PACKAGE__ . '::Storage::' . $storage{ lc delete $p{storage} };
37  
38 1 ▶ eval "use $storage_class";
Subroutine BEGIN: called CC=1 SCAR=0.0
39 1 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
40  
41 1 return $storage_class->new(%p);
42 }
43  
44 1 my %Ignore = map { $_ => 1 } qw( fUTF8 fCNS11643 fGB fBig5 );
45  
46 1 my %CCDICTToInternal =
47 ( fTotalStrokes => 'stroke_count',
48 fCangjie => 'cangjie',
49 fFourCorner => 'four_corner',
50 );
51  
52 1 my %RomanizationToInternal =
53 ( fHakka => 'pinjim',
54 fCantonese => 'jyutping',
55 fMandarin => 'pinyin',
56 );
57  
58 1 my %RomanizationType = map { $_ => 1 } values %RomanizationToInternal;
59  
60 sub parse_source_file
61 1 ▶{
Subroutine parse_source_file: called CC=27 SCAR=33.2
Pod documented
62 1 my $self = shift;
63 1 my $file = shift;
64 1 ▶ my $status_fh = $ENV{CCDICT_VERBOSE} ? \*STDERR : undef;
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'CCDICT_VERBOSE'} ? : 0 1 1
65 1 my $lines_per = 5000;
66  
67 1 ▶ unless ( defined $file )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 1 0 1
68 {
69 1 my $pack_as_file = File::Spec->catfile( split /::/, __PACKAGE__ );
70 1 $pack_as_file .= '.pm';
71  
72 1 (my $dir = $INC{$pack_as_file}) =~ s/\.pm$//;
73  
74 1 $file = File::Spec->catfile( $dir, 'Data.pm' );
75 }
76  
77 1 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Cannot read ${file}: $!" 1 0 1
78 or die "Cannot read $file: $!";
79  
80 1 my $last_char;
81 my %entry;
82 1 ▶ while (<$fh>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 298555 1 298556
83 {
84 298555 chomp;
85  
86 298555 ▶ next unless substr( $_, 0, 1 ) eq 'U';
Branch: 1 branch
BranchTrueFalseTotal
unless substr($_, 0, 1) eq "U" 14 298541 298555
87  
88 298541 my ( $unicode, $type, $data ) = split /\t/, $_, 3;
89  
90 298541 s/^\s+|\s+$//g for $unicode, $type, $data;
91  
92 298541 ▶ next if $Ignore{$type};
Branch: 1 branch
BranchTrueFalseTotal
if $Ignore{$type} 94361 204180 298541
93  
94 204180 my ( $codepoint, $homograph ) = $unicode =~ /U\+([\dABCDEF]+)\.(\d)/;
95  
96 204180 ▶ die "Bad line (line #$.):\n$_\n\n" unless $codepoint;
Branch: 1 branch
BranchTrueFalseTotal
unless $codepoint 0 204180 204180
97  
98 # not sure how to handle this, to be honest.
99 204180 ▶ next if $homograph;
Branch: 1 branch
BranchTrueFalseTotal
if $homograph 6896 197284 204180
100  
101 # generate a real Unicode character in Perl
102 197284 my $unicode_char = chr( hex($codepoint) );
103  
104 197284 ▶ $last_char = $unicode_char unless defined $last_char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $last_char 1 197283 197284
105  
106 # this relies on the fact that data for each char is grouped
107 # together on consecutive lines in the ccdict.txt file.
108 197284 ▶ if ( $unicode_char ne $last_char )
Branch: 1 branch
BranchTrueFalseTotal
if ($unicode_char ne $last_char) 27493 169791 197284
109 {
110 27493 $self->_add_entry( $last_char, { %entry, unicode => $last_char } );
111  
112 27493 %entry = ();
113  
114 27493 $last_char = $unicode_char;
115 }
116  
117 197284 ▶ if ( exists $CCDICTToInternal{$type} )
Branch: 4 branches
BranchTrueFalseTotal
if (exists $CCDICTToInternal{$type}) { } 79290 117994 197284
elsif (exists $RomanizationToInternal{$type}) { } 68974 49020 117994
elsif ($type eq 'fR/S') { } 27495 21525 49020
elsif ($type eq 'fEnglish') { } 21525 0 21525
118 {
119 79290 my $internal = $CCDICTToInternal{$type};
120  
121 79290 $entry{$internal} = $data;
122 }
123 elsif ( exists $RomanizationToInternal{$type} )
124 {
125 68974 my $internal = $RomanizationToInternal{$type};
126  
127 68974 my $class = $self->_romanization_class($internal);
128  
129 68974 my $is_obsolete = 0;
130 68974 foreach my $syl ( split /[\s;]+/, $data )
131 {
132 # Deal with various mysterious bits and mistakes
133 134089 ▶ next if $syl eq '}';
Branch: 1 branch
BranchTrueFalseTotal
if $syl eq "}" 3 134086 134089
134 134086 ▶ next if $syl eq '(also)';
Branch: 1 branch
BranchTrueFalseTotal
if $syl eq "(also)" 5792 128294 134086
135 128294 ▶ next if $syl eq '(old)';
Branch: 1 branch
BranchTrueFalseTotal
if $syl eq "(old)" 1 128293 128294
136  
137 128293 ▶ next if $syl =~ /^p?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $syl =~ /^p?\d+$/ 6 128287 128293
138  
139 128287 ▶ next if $syl =~ /^\(coll/;
Branch: 1 branch
BranchTrueFalseTotal
if $syl =~ /^\(coll/ 2 128285 128287
140  
141 128285 $syl =~ s/^{?([^}]+)}$/$1/;
142 128285 $syl =~ s/{[^}]+}?$//;
143  
144 128285 ▶ if ( $syl eq 'obs' )
Branch: 1 branch
BranchTrueFalseTotal
if ($syl eq "obs") 23 128262 128285
145 {
146 23 $is_obsolete = 1;
147 23 next;
148 }
149  
150 128262 my $romanized = $class->new( syllable => $syl,
151 obsolete => $is_obsolete,
152 );
153  
154 128262 ▶ next unless $romanized;
Branch: 1 branch
BranchTrueFalseTotal
unless $romanized 122 128140 128262
155  
156 128140 push @{ $entry{$internal} }, $romanized;
157 }
158 }
159 elsif ( $type eq 'fR/S' )
160 {
161 27495 my ( $radical, $index ) = split /\./, $data;
162  
163 27495 $entry{radical} = $radical;
164 27495 $entry{index} = $index;
165 }
166 elsif ( $type eq 'fEnglish' )
167 {
168 $entry{english} =
169 21525 ▶ [ grep { defined && length } split /\s*\[\d\d?\]\s*/, $data ];
Condition66%2/3
defined $_ && length $_
!ll&&!rl&&r
0670734796
MC/DC50%1/2
defined $_ && length $_
defined $_ length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
170 }
171 else
172 {
173 0 die "Invalid line: $_\n";
174 }
175  
176 197284 ▶ if ( ! ( $. % $lines_per ) && $status_fh )
Branch: 1 branch
BranchTrueFalseTotal
if (not $. % $lines_per and $status_fh) 0 197284 197284
Condition66%2/3
not $. % $lines_per and $status_fh
!ll&&!rl&&r
197238460
MC/DC0%0/2
not $. % $lines_per and $status_fh
not $. % $lines_per $status_fh
Truth table66%2/3
not(A) and B
A= not $. % $lines_perB= $status_fh
ABresult
0X0
100
111
177 {
178 0 print $status_fh "$. lines processed\n";
179 }
180 }
181  
182 1 $self->_add_entry( $last_char, { %entry, unicode => $last_char } );
183 }
184  
185 sub _add_entry
186 27494 ▶{
Subroutine _add_entry: called CC=2 SCAR=6.9
187 27494 my $self = shift;
188 27494 my $unicode = shift;
189 27494 my $entry = shift;
190  
191 27494 ▶ return unless defined $entry->{radical};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entry->{"radical"} 10 27484 27494
192  
193 27484 $self->_real_add_entry( $unicode, $entry );
194 }
195  
196 sub _romanization_class
197 68974 ▶{
Subroutine _romanization_class: called CC=2 SCAR=6.9
198 return
199 68974 ▶ ( $_[1] eq 'pinyin' ?
Branch: 1 branch
BranchTrueFalseTotal
$_[1] eq 'pinyin' ? : 26400 42574 68974
200 'Lingua::ZH::CCDICT::Romanization::Pinyin' :
201 'Lingua::ZH::CCDICT::Romanization'
202 );
203 }
204  
205 sub _is_romanization_type
206 8 ▶{
Subroutine _is_romanization_type: called CC=1 SCAR=0.0
207 8 return $RomanizationType{ $_[1] };
208 }
209  
210 sub _InternalTypes
211 1 ▶{
Subroutine _InternalTypes: called CC=1 SCAR=0.0
212 1 return values %CCDICTToInternal, values %RomanizationToInternal, 'index', 'radical';
213 }
214  
215 # Some of these may be overridden in subclasses, but they provide an
216 # easy default
217 1 foreach my $type ( __PACKAGE__->_InternalTypes() )
218 {
219 my $sub =
220 __PACKAGE__->_is_romanization_type($type)
221 1 ▶ ? sub { my $s = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
222 1 $s->_match( $type => map { lc } @_ ) }
223 2 ▶ : sub { my $s = shift;
Subroutine match_four_corner: called CC=1 SCAR=0.0
Pod documented
224 2 ▶ $s->_match( $type => @_ ) };
Branch: 1 branch
BranchTrueFalseTotal
__PACKAGE__->_is_romanization_type($type) ? : 3 5 8
225  
226 8 my $sub_name = "match_$type";
227 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
228  
229 8 *{$sub_name} = subname $sub_name => $sub;
230 }
231  
232  
233 1 1;
234  
235 __END__
« CCDICT/Storage/BerkeleyDB.pm ↑ Summary CCDICT/Romanization/Pinyin.pm »