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   63269 use warnings;
  3         6  
  3         92  
4 3     3   16 use strict;
  3         6  
  3         71  
5 3     3   15 use vars qw($VERSION);
  3         8  
  3         161  
6             $VERSION = '0.10';
7              
8 3     3   15 use File::Spec;
  3         5  
  3         71  
9 3     3   2374 use Lingua::Han::Utils qw/Unihan_value/;
  3         221038  
  3         1184  
10              
11             sub new {
12 2     2 0 24 my $class = shift;
13 2         4 my $dir = __FILE__; $dir =~ s/\.pm//o;
  2         11  
14 2 50       71 -d $dir or die "Directory $dir nonexistent!";
15 2         7 my $self = { @_ };
16 2         4 my %ct;
17 2         52 my $file = File::Spec->catfile($dir, 'Cantonese.dat');
18 2 50       90 open(FH, $file) or die "$file: $!";
19 2         61 while() {
20 46114         139679 my ($uni, $ct) = split(/\s+/);
21 46114         160721 $ct{$uni} = $ct;
22             }
23 2         65 close(FH);
24 2         11 $self->{'ct'} = \%ct;
25 2         23 return bless $self => $class;
26             }
27              
28             sub han2Cantonese {
29 6     6 0 5044 my ($self, $hanzi) = @_;
30              
31 6         27 my @code = Unihan_value($hanzi);
32              
33 6         5028776 my @result;
34 6         18 foreach my $code (@code) {
35 30         151 my $value = $self->{'ct'}->{$code};
36 30 100       83 if (defined $value) {
37 12 50       86 $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         100 push @result, lc $value;
43             }
44              
45 6 50       54 return wantarray ? @result : join('', @result);
46              
47             }
48              
49             1;
50             __END__