| 1 | package Lingua::Han::Cantonese; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use vars qw($VERSION); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | $VERSION = '0.12'; | |||||||||
| 7 | |||||||||||
| 8 | 3 | ▶ | use File::Spec; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use Lingua::Han::Utils qw/Unihan_value/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 12 | 2 | my $class = shift; | |||||||||
| 13 | 2 | my $dir = __FILE__; $dir =~ s/\.pm//o; | |||||||||
| 14 | 2 | ▶ | -d $dir or die "Directory $dir nonexistent!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 15 | 2 | my $self = { @_ }; | |||||||||
| 16 | 2 | my %ct; | |||||||||
| 17 | 2 | my $file = File::Spec->catfile($dir, 'Cantonese.dat'); | |||||||||
| 18 | 2 | ▶ | open(FH, $file) or die "$file: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 19 | 2 | ▶ | while(<FH>) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 46116 | my ($uni, $ct) = split(/\s+/); | |||||||||
| 21 | 46116 | $ct{$uni} = $ct; | |||||||||
| 22 | } | ||||||||||
| 23 | 2 | close(FH); | |||||||||
| 24 | 2 | $self->{'ct'} = \%ct; | |||||||||
| 25 | 2 | return bless $self => $class; | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 6 | ▶ | sub han2Cantonese { | ||||||||
|
Subroutine
han2Cantonese: called CC=5 SCAR=16.2
Pod
undocumented
| |||||||||||
| 29 | 6 | my ($self, $hanzi) = @_; | |||||||||
| 30 | |||||||||||
| 31 | 6 | my @code = Unihan_value($hanzi); | |||||||||
| 32 | |||||||||||
| 33 | 6 | my @result; | |||||||||
| 34 | 6 | foreach my $code (@code) { | |||||||||
| 35 | 30 | my $value = $self->{'ct'}->{$code}; | |||||||||
| 36 | 30 | ▶ | if (defined $value) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 12 | ▶ | $value =~ s/\d//isg unless ($self->{'tone'}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | } else { | ||||||||||
| 39 | # if it's not a Chinese, return original word | ||||||||||
| 40 | 18 | $value = pack("U*", hex $code); | |||||||||
| 41 | } | ||||||||||
| 42 | 30 | push @result, lc $value; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 6 | ▶ | return wantarray ? @result : join('', @result); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | |||||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 3 | 1; | |||||||||
| 50 | __END__ | ||||||||||