File Coverage

blib/lib/Lingua/Han/PinYin.pm
Criterion Covered Total %
statement 76 78 97.4
branch 51 66 77.2
condition n/a
subroutine 9 9 100.0
pod 3 4 75.0
total 139 157 88.5


line stmt bran cond sub pod time code
1             package Lingua::Han::PinYin;
2              
3 6     6   124821 use strict;
  6         13  
  6         151  
4 6     6   30 use warnings;
  6         10  
  6         224  
5             our $VERSION = '0.22';
6              
7 6     6   27 use File::Spec ();
  6         13  
  6         120  
8 6     6   4304 use Lingua::Han::Utils qw/Unihan_value/;
  6         432661  
  6         6292  
9              
10             sub new {
11 6     6 0 68 my $class = shift;
12              
13 6         12 my $dir = __FILE__;
14 6         32 $dir =~ s/\.pm//o;
15 6 50       212 -d $dir or die "Directory $dir does not exists, please consider to reinstall this module.";
16              
17 6 50       45 my %args = (@_ % 2 == 1) ? %{ $_[0] } : (@_);
  0         0  
18              
19 6         10 my %py;
20 6         140 my $file = File::Spec->catfile( $dir, 'Mandarin.dat' );
21 6 50       298 open(my $fh, '<', $file) or die "Can't open $file: $!";
22 6         203 while (my $line = <$fh>) {
23 247290         280322 chomp($line);
24 247290         684438 my ( $uni, $py ) = split(/\s+/, $line);
25 247290         936521 $py{$uni} = $py;
26             }
27 6         278 close($fh);
28              
29 6         35 $args{'py'} = \%py;
30              
31 6         139 return bless \%args => $class;
32             }
33              
34             sub han2pinyin1 {
35 5     5 1 2719 my ($self, $word) = @_;
36 5         24 my $code = Unihan_value($word);
37 5         4657097 my $value = $self->{'py'}->{$code};
38 5 50       26 if (defined $value) {
39 5         19 $value = $self->_fix_val( $value );
40             } else {
41             # not found in dictionary, return original word
42 0         0 $value = $word;
43             }
44 5         24 return $value;
45             }
46              
47             sub han2pinyin {
48 36     36 1 19263 my ( $self, $hanzi ) = @_;
49              
50 36         134 my @code = Unihan_value($hanzi);
51              
52 36         15681 my @result;
53 36         73 foreach my $code (@code) {
54 69         215 my $value = $self->{'py'}->{$code};
55 69 100       157 if ( defined $value ) {
56 51         123 $value = $self->_fix_val( $value );
57             }
58             else {
59             # if it's not a Chinese, return original word
60 18         62 $value = pack( "U*", hex $code );
61             }
62 69 100       282 push @result, ($self->{capitalize} ? ucfirst $value : $value);
63             }
64              
65 36 50       177 return wantarray ? @result : join( '', @result );
66              
67             }
68              
69             sub gb2pinyin {
70 1     1 1 661 my ($self, $hanzi) = @_;
71              
72             # convert only normal Chinese letter. Ignore Chinese symbols
73             # which fall within [0xa1,0xb0) region. 0xb0==0260
74             # if it is not normal Chinese, retain original characters
75 1         6 $hanzi =~ s/[\260-\377][\200-\377]/$self->han2pinyin1($&)/ge;
  2         5  
76 1         4 return $hanzi;
77             }
78              
79             sub _fix_val {
80 56     56   104 my ( $self, $value ) = @_;
81              
82 56 100       160 if ($self->{unicode}) {
83 5         13 return $value;
84             }
85              
86             # convert into ascii
87 51 100       147 $value =~ s/ū/u/g and $value .= '1';
88 51 50       126 $value =~ s/ǖ/u/g and $value .= '1';
89 51 50       121 $value =~ s/ī/i/g and $value .= '1';
90 51 100       123 $value =~ s/ō/o/g and $value .= '1';
91 51 50       119 $value =~ s/ā/a/g and $value .= '1';
92 51 100       120 $value =~ s/ē/e/g and $value .= '1';
93              
94 51 50       116 $value =~ s/í/i/g and $value .= '2';
95 51 100       125 $value =~ s/é/e/g and $value .= '2';
96 51 100       122 $value =~ s/ú/u/g and $value .= '2';
97 51 50       119 $value =~ s/ó/o/g and $value .= '2';
98 51 100       119 $value =~ s/ǘ/v/g and $value .= '2';
99 51 100       131 $value =~ s/á/a/g and $value .= '2';
100              
101 51 50       129 $value =~ s/ě/e/g and $value .= '3';
102 51 100       124 $value =~ s/ǎ/a/g and $value .= '3';
103 51 100       210 $value =~ s/ǒ/o/g and $value .= '3';
104 51 50       125 $value =~ s/ǔ/u/g and $value .= '3';
105 51 100       130 $value =~ s/ǚ/v/g and $value .= '3';
106 51 100       131 $value =~ s/ǐ/i/g and $value .= '3';
107              
108 51 50       117 $value =~ s/ò/o/g and $value .= '4';
109 51 100       133 $value =~ s/à/a/g and $value .= '4';
110 51 50       191 $value =~ s/è/e/g and $value .= '4';
111 51 50       115 $value =~ s/ù/u/g and $value .= '4';
112 51 100       127 $value =~ s/ǜ/v/g and $value .= '4';
113 51 100       126 $value =~ s/ì/i/g and $value .= '4';
114              
115 51 100       215 $value =~ s/\d//g unless $self->{tone};
116 51         133 return $value;
117             }
118              
119             1;
120             __END__