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   60647 use warnings;
  3         12  
  3         125  
4 3     3   21 use strict;
  3         9  
  3         101  
5 3     3   22 use vars qw($VERSION);
  3         12  
  3         204  
6             $VERSION = '0.12';
7              
8 3     3   23 use File::Spec;
  3         9  
  3         101  
9 3     3   1556 use Lingua::Han::Utils qw/Unihan_value/;
  3         190067  
  3         1054  
10              
11             sub new {
12 2     2 0 26 my $class = shift;
13 2         18 my $dir = __FILE__; $dir =~ s/\.pm//o;
  2         14  
14 2 50       69 -d $dir or die "Directory $dir nonexistent!";
15 2         9 my $self = { @_ };
16 2         6 my %ct;
17 2         46 my $file = File::Spec->catfile($dir, 'Cantonese.dat');
18 2 50       71 open(FH, $file) or die "$file: $!";
19 2         56 while() {
20 46116         132242 my ($uni, $ct) = split(/\s+/);
21 46116         150565 $ct{$uni} = $ct;
22             }
23 2         82 close(FH);
24 2         15 $self->{'ct'} = \%ct;
25 2         24 return bless $self => $class;
26             }
27              
28             sub han2Cantonese {
29 6     6 0 4798 my ($self, $hanzi) = @_;
30              
31 6         27 my @code = Unihan_value($hanzi);
32              
33 6         8063 my @result;
34 6         19 foreach my $code (@code) {
35 30         87 my $value = $self->{'ct'}->{$code};
36 30 100       72 if (defined $value) {
37 12 50       67 $value =~ s/\d//isg unless ($self->{'tone'});
38             } else {
39             # if it's not a Chinese, return original word
40 18         56 $value = pack("U*", hex $code);
41             }
42 30         92 push @result, lc $value;
43             }
44              
45 6 50       39 return wantarray ? @result : join('', @result);
46              
47             }
48              
49             1;
50             __END__