File Coverage

blib/lib/Lingua/Han/Cantonese.pm
Criterion Covered Total %
statement 39 39 100.0
branch 6 10 60.0
condition n/a
subroutine 7 7 100.0
pod 0 2 0.0
total 52 58 89.6


line stmt bran cond sub pod time code
1             package Lingua::Han::Cantonese;
2              
3 3     3   63611 use warnings;
  3         8  
  3         93  
4 3     3   15 use strict;
  3         5  
  3         75  
5 3     3   14 use vars qw($VERSION);
  3         8  
  3         167  
6             $VERSION = '0.11';
7              
8 3     3   15 use File::Spec;
  3         4  
  3         74  
9 3     3   2301 use Lingua::Han::Utils qw/Unihan_value/;
  3         220418  
  3         1221  
10              
11             sub new {
12 2     2 0 22 my $class = shift;
13 2         5 my $dir = __FILE__; $dir =~ s/\.pm//o;
  2         11  
14 2 50       72 -d $dir or die "Directory $dir nonexistent!";
15 2         6 my $self = { @_ };
16 2         4 my %ct;
17 2         50 my $file = File::Spec->catfile($dir, 'Cantonese.dat');
18 2 50       83 open(FH, $file) or die "$file: $!";
19 2         60 while() {
20 46114         139947 my ($uni, $ct) = split(/\s+/);
21 46114         163023 $ct{$uni} = $ct;
22             }
23 2         101 close(FH);
24 2         11 $self->{'ct'} = \%ct;
25 2         22 return bless $self => $class;
26             }
27              
28             sub han2Cantonese {
29 6     6 0 4900 my ($self, $hanzi) = @_;
30              
31 6         28 my @code = Unihan_value($hanzi);
32              
33 6         555289 my @result;
34 6         19 foreach my $code (@code) {
35 30         99 my $value = $self->{'ct'}->{$code};
36 30 100       77 if (defined $value) {
37 12 50       85 $value =~ s/\d//isg unless ($self->{'tone'});
38             } else {
39             # if it's not a Chinese, return original word
40 18         68 $value = pack("U*", hex $code);
41             }
42 30         97 push @result, lc $value;
43             }
44              
45 6 50       47 return wantarray ? @result : join('', @result);
46              
47             }
48              
49             1;
50             __END__