| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 65 | 1 | my $lines_per = 5000; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | ▶ | unless ( defined $file ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 78 | or die "Cannot read $file: $!"; | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 1 | my $last_char; | |||||||||||||||||||||||||||
| 81 | my %entry; | ||||||||||||||||||||||||||||
| 82 | 1 | ▶ | while (<$fh>) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | { | ||||||||||||||||||||||||||||
| 84 | 298555 | chomp; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 298555 | ▶ | next unless substr( $_, 0, 1 ) eq 'U'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # not sure how to handle this, to be honest. | ||||||||||||||||||||||||||||
| 99 | 204180 | ▶ | next if $homograph; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 134 | 134086 | ▶ | next if $syl eq '(also)'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 128294 | ▶ | next if $syl eq '(old)'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 128293 | ▶ | next if $syl =~ /^p?\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 128287 | ▶ | next if $syl =~ /^\(coll/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 128285 | $syl =~ s/^{?([^}]+)}$/$1/; | |||||||||||||||||||||||||||
| 142 | 128285 | $syl =~ s/{[^}]+}?$//; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 128285 | ▶ | if ( $syl eq 'obs' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 $_
MC/DC50%1/2
defined $_ && length $_
defined $_
length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
| |||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | else | ||||||||||||||||||||||||||||
| 172 | { | ||||||||||||||||||||||||||||
| 173 | 0 | die "Invalid line: $_\n"; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 197284 | ▶ | if ( ! ( $. % $lines_per ) && $status_fh ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $. % $lines_per and $status_fh
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||