Pinyin.pm

filter: Statementstmt 98.3% 61 / 62 Branchbran 67.6% 23 / 34 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 86.2% 100 / 116 CC 19 SCAR 13.9
CC
19
Cov
86%
string
7.1
new
4.5
_string
4.1
CRAP 4.0
« DictZH.pm ↑ Summary Cantonese.pm »
1 =head1 NAME
2  
3 Lingua::ZH::Romanize::Pinyin - Romanization of Standard Chinese language
4  
5 =head1 SYNOPSIS
6  
7 use Lingua::ZH::Romanize::Pinyin;
8  
9 my $conv = Lingua::ZH::Romanize::Pinyin->new();
10 my $roman = $conv->char( $hanji );
11 printf( "<ruby><rb>%s</rb><rt>%s</rt></ruby>", $hanji, $roman );
12  
13 my @array = $conv->string( $string );
14 foreach my $pair ( @array ) {
15 my( $raw, $ruby ) = @$pair;
16 if ( defined $ruby ) {
17 printf( "<ruby><rb>%s</rb><rt>%s</rt></ruby>", $raw, $ruby );
18 } else {
19 print $raw;
20 }
21 }
22  
23 =head1 DESCRIPTION
24  
25 Pinyin is a phonemic notation for Chinese characters.
26  
27 =head2 $conv = Lingua::ZH::Romanize::Pinyin->new();
28  
29 This constructer methods returns a new object with its dictionary cached.
30  
31 =head2 $roman = $conv->char( $hanji );
32  
33 This method returns romanized letters of a Hanji character.
34 It returns undef when $hanji is not a valid Hanji character.
35 The argument's encoding must be UTF-8.
36 Both of Simplified Chinese and Traditional Chinese are allowed.
37  
38 =head2 $roman = $conv->chars( $string );
39  
40 This method returns romanized letters of Hanji characters.
41  
42 =head2 @array = $conv->string( $string );
43  
44 This method returns a array of referenced arrays
45 which are pairs of a Hanji chacater and its romanized letters.
46  
47 $array[0] # first Chinese character's pair (array)
48 $array[1][0] # secound Chinese character itself
49 $array[1][1] # its romanized letters
50  
51 =head1 DICTIONARY
52  
53 This module internally uses a mapping table from Hanji to roman
54 which is based on C<PY.tit> which is distributed with C<cxterm>.
55  
56 =head1 MODULE DEPENDENCY
57  
58 L<Storable> module is required.
59  
60 =head1 UTF-8 FLAG
61  
62 This treats utf8 flag transparently.
63  
64 =head1 SEE ALSO
65  
66 L<Lingua::ZH::Romanize::Cantonese> for romanization of Cantonese
67  
68 L<Lingua::JA::Romanize::Japanese> for romanization of Japanese
69  
70 L<Lingua::KO::Romanize::Hangul> for romanization of Korean
71  
72 http://www.kawa.net/works/perl/romanize/romanize-e.html
73  
74 http://linuga-romanize.googlecode.com/svn/trunk/Lingua-ZH-Romanize-Pinyin/
75  
76 =head1 COPYRIGHT
77  
78 Copyright (c) 2003-2008 Yusuke Kawasaki. All rights reserved.
79  
80 =head1 LICENSE
81  
82 Any commercial use of the Software requires a license directly from
83 the author(s). Please contact the author(s) to negotiate an
84 appropriate license. Commercial use includes integration of all or
85 part of the binary or source code covered by this permission
86 notices into a product for sale or license to third parties on your
87 behalf, or distribution of the binary or source code to third
88 parties that need it to utilize a product sold or licensed on your
89 behalf.
90  
91 =cut
92  
93 package Lingua::ZH::Romanize::Pinyin;
94 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
95 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 4 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 4 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
98 4 $VERSION = "0.23";
99 4 ▶my $PERL581 = 1 if ( $] >= 5.008001 );
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.008001" 4 0 4
100  
101 4 ▶sub new {
Subroutine new: called CC=4 SCAR=15.0
Pod documented
102 4 my $package = shift;
103 4 ▶ my $store = shift || &_detect_store($package);
Condition33%1/3
shift() || &_detect_store($package)
l!l&&r!l&&!r
040
MC/DC0%0/2
shift() || &_detect_store($package)
shift() &_detect_store($package)
Truth table33%1/3
A || B
A= shift()B= &_detect_store($package)
ABresult
1X1
011
000
104 4 ▶ Carp::croak "$! - $store\n" unless ( -r $store );
Branch: 1 branch
BranchTrueFalseTotal
unless -r $store 0 4 4
105 4 ▶ my $self = Storable::retrieve($store) or Carp::croak "$! - $store\n";
Branch: 1 branch
BranchTrueFalseTotal
my $self = Storable::retrieve($store) or Carp::croak("$! - $store\n") 4 0 4
106 4 bless $self, $package;
107 4 $self;
108 }
109  
110 12 ▶sub char {
Subroutine char: called CC=4 SCAR=13.9
Pod documented
111 12 my $self = shift;
112 12 ▶ return $self->_char(@_) unless $PERL581;
Branch: 1 branch
BranchTrueFalseTotal
unless $PERL581 0 12 12
113 12 my $char = shift;
114 12 my $utf8 = utf8::is_utf8( $char );
115 12 ▶ utf8::encode( $char ) if $utf8;
Branch: 1 branch
BranchTrueFalseTotal
if $utf8 4 8 12
116 12 $char = $self->_char( $char );
117 12 ▶ utf8::decode( $char ) if $utf8;
Branch: 1 branch
BranchTrueFalseTotal
if $utf8 4 8 12
118 12 $char;
119 }
120  
121 12 ▶sub _char {
Subroutine _char: called CC=2 SCAR=6.9
122 12 my $self = shift;
123 12 my $char = shift;
124 12 ▶ return unless exists $self->{$char};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$char} 4 8 12
125 8 $self->{$char};
126 }
127  
128 8 ▶sub chars {
Subroutine chars: called CC=2 SCAR=7.0
Pod documented
129 8 my $self = shift;
130 8 my @array = $self->string(shift);
131 8 ▶ join( " ", map { $#$_ > 0 ? $_->[1] : $_->[0] } @array );
Branch: 1 branch
BranchTrueFalseTotal
$_->$#* > 0 ? : 16 0 16
132 }
133  
134 20 ▶sub string {
Subroutine string: called CC=7 SCAR=19.7
Pod documented
135 20 my $self = shift;
136 20 ▶ return $self->_string(@_) unless $PERL581;
Branch: 1 branch
BranchTrueFalseTotal
unless $PERL581 0 20 20
137 20 my $char = shift;
138 20 my $flag = utf8::is_utf8( $char );
139 20 ▶ utf8::encode( $char ) if $flag;
Branch: 1 branch
BranchTrueFalseTotal
if $flag 10 10 20
140 20 my @array = $self->_string( $char );
141 20 ▶ if ( $flag ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag) 10 10 20
142 10 foreach my $pair ( @array ) {
143 44 ▶ utf8::decode( $pair->[0] ) if defined $pair->[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $pair->[0] 44 0 44
144 44 ▶ utf8::decode( $pair->[1] ) if defined $pair->[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $pair->[1] 44 0 44
145 }
146 }
147 20 @array;
148 }
149  
150 20 ▶sub _string {
Subroutine _string: called CC=4 SCAR=14.1
151 20 my $self = shift;
152 20 my $src = shift;
153 20 my $array = [];
154 20 ▶ while ( $src =~ /([\300-\377][\200-\277]+)|([\000-\177]+)/sg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($src =~ /([\300-\377][\200-\277]+)|([\000-\177]+)/gs) 88 20 108
155 88 ▶ if ( defined $1 ) { # Chinese
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1) { } 88 0 88
156 88 my $pair = [$1];
157 88 ▶ $pair->[1] = $self->{$1} if exists $self->{$1};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$1} 88 0 88
158 88 push( @$array, $pair );
159 }
160 else {
161 0 push( @$array, [$2] ); # ASCII
162 }
163 }
164 20 @$array;
165 }
166  
167 # Pinyin.pm -> Pinyin.store
168 # Cantonese.pm -> Cantonese.store
169  
170 4 ▶sub _detect_store {
Subroutine _detect_store: called CC=2 SCAR=7.0
171 4 my $package = shift;
172 4 my $store = $INC{ join( "/", split( "::", "$package.pm" ) ) };
173 4 ▶ $store =~ s#\.pm$#.store# or Carp::croak "Invalid module name: $package\n";
Branch: 1 branch
BranchTrueFalseTotal
$store =~ s/\.pm$/.store/ or Carp::croak("Invalid module name: $package\n") 4 0 4
174 4 $store;
175 }
176  
177 4 1;
« DictZH.pm ↑ Summary Cantonese.pm »