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   111364 use strict;
  6         15  
  6         170  
4 6     6   29 use warnings;
  6         13  
  6         212  
5             our $VERSION = '0.23';
6              
7 6     6   30 use File::Spec ();
  6         21  
  6         123  
8 6     6   2289 use Lingua::Han::Utils qw/Unihan_value/;
  6         304875  
  6         5117  
9              
10             sub new {
11 6     6 0 79 my $class = shift;
12              
13 6         17 my $dir = __FILE__;
14 6         31 $dir =~ s/\.pm//o;
15 6 50       195 -d $dir or die "Directory $dir does not exists, please consider to reinstall this module.";
16              
17 6 50       48 my %args = (@_ % 2 == 1) ? %{ $_[0] } : (@_);
  0         0  
18              
19 6         13 my %py;
20 6         135 my $file = File::Spec->catfile( $dir, 'Mandarin.dat' );
21 6 50       260 open(my $fh, '<', $file) or die "Can't open $file: $!";
22 6         156 while (my $line = <$fh>) {
23 247296         385931 chomp($line);
24 247296         666657 my ( $uni, $py ) = split(/\s+/, $line);
25 247296         901178 $py{$uni} = $py;
26             }
27 6         166 close($fh);
28              
29 6         41 $args{'py'} = \%py;
30              
31 6         109 return bless \%args => $class;
32             }
33              
34             sub han2pinyin1 {
35 5     5 1 2117 my ($self, $word) = @_;
36 5         28 my $code = Unihan_value($word);
37 5         405882 my $value = $self->{'py'}->{$code};
38 5 50       22 if (defined $value) {
39 5         21 $value = $self->_fix_val( $value );
40             } else {
41             # not found in dictionary, return original word
42 0         0 $value = $word;
43             }
44 5         27 return $value;
45             }
46              
47             sub han2pinyin {
48 36     36 1 19945 my ( $self, $hanzi ) = @_;
49              
50 36         127 my @code = Unihan_value($hanzi);
51              
52 36         13718 my @result;
53 36         89 foreach my $code (@code) {
54 69         211 my $value = $self->{'py'}->{$code};
55 69 100       166 if ( defined $value ) {
56 51         166 $value = $self->_fix_val( $value );
57             }
58             else {
59             # if it's not a Chinese, return original word
60 18         45 $value = pack( "U*", hex $code );
61             }
62 69 100       247 push @result, ($self->{capitalize} ? ucfirst $value : $value);
63             }
64              
65 36 50       198 return wantarray ? @result : join( '', @result );
66              
67             }
68              
69             sub gb2pinyin {
70 1     1 1 622 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         10 $hanzi =~ s/[\260-\377][\200-\377]/$self->han2pinyin1($&)/ge;
  2         10  
76 1         8 return $hanzi;
77             }
78              
79             sub _fix_val {
80 56     56   129 my ( $self, $value ) = @_;
81              
82 56 100       170 if ($self->{unicode}) {
83 5         11 return $value;
84             }
85              
86             # convert into ascii
87 51 100       164 $value =~ s/ū/u/g and $value .= '1';
88 51 50       133 $value =~ s/ǖ/u/g and $value .= '1';
89 51 50       134 $value =~ s/ī/i/g and $value .= '1';
90 51 100       131 $value =~ s/ō/o/g and $value .= '1';
91 51 50       120 $value =~ s/ā/a/g and $value .= '1';
92 51 100       160 $value =~ s/ē/e/g and $value .= '1';
93              
94 51 50       125 $value =~ s/í/i/g and $value .= '2';
95 51 100       216 $value =~ s/é/e/g and $value .= '2';
96 51 100       133 $value =~ s/ú/u/g and $value .= '2';
97 51 50       125 $value =~ s/ó/o/g and $value .= '2';
98 51 100       136 $value =~ s/ǘ/v/g and $value .= '2';
99 51 100       134 $value =~ s/á/a/g and $value .= '2';
100              
101 51 50       124 $value =~ s/ě/e/g and $value .= '3';
102 51 100       140 $value =~ s/ǎ/a/g and $value .= '3';
103 51 100       156 $value =~ s/ǒ/o/g and $value .= '3';
104 51 50       146 $value =~ s/ǔ/u/g and $value .= '3';
105 51 100       128 $value =~ s/ǚ/v/g and $value .= '3';
106 51 100       149 $value =~ s/ǐ/i/g and $value .= '3';
107              
108 51 50       126 $value =~ s/ò/o/g and $value .= '4';
109 51 100       140 $value =~ s/à/a/g and $value .= '4';
110 51 50       166 $value =~ s/è/e/g and $value .= '4';
111 51 50       126 $value =~ s/ù/u/g and $value .= '4';
112 51 100       135 $value =~ s/ǜ/v/g and $value .= '4';
113 51 100       156 $value =~ s/ì/i/g and $value .= '4';
114              
115 51 100       210 $value =~ s/\d//g unless $self->{tone};
116 51         140 return $value;
117             }
118              
119             1;
120             __END__