blib/lib/eGuideDog/Dict/Cantonese.pm

filter: Statementstmt 86.2% 132 / 153 Branchbran 75.8% 44 / 58 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 85.7% 12 / 14 Podpod 55.5% 5 / 9 totaltotal 82.0% 196 / 239 CC 34 SCAR 24.1
CC
34
Cov
83%
get_jyutping
24.9
import_zhy_list
9.0
import_unihan
7.0
CRAP 11.1
↑ Summary update_dict.pl »
1 package eGuideDog::Dict::Cantonese;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Encode::CNMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 require Exporter;
10  
11 3 our @ISA = qw(Exporter);
12  
13 # Items to export into callers namespace by default. Note: do not export
14 # names by default without a very good reason. Use EXPORT_OK instead.
15 # Do not simply export all your public functions/methods/constants.
16  
17 # This allows declaration use eGuideDog::Dict::Cantonese ':all';
18 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
19 # will save memory.
20 3 our %EXPORT_TAGS = ( 'all' => [ qw(
21
22 ) ] );
23  
24 3 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
25  
26 3 our @EXPORT = qw(
27
28 );
29  
30 3 our $VERSION = '0.41';
31  
32  
33 # Preloaded methods go here.
34  
35 1 ▶sub new() {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
36 1 my $self = {};
37 1 $self->{jyutping} = {}; # The most probably phonetic symbol
38 1 $self->{chars} = {}; # all phonetic symbols (array ref)
39 1 $self->{words} = {}; # word phonetic symbols (array ref)
40 1 $self->{word_index} = {}; # the first char to words (array ref)
41 1 bless $self, __PACKAGE__;
42  
43 # load zhy_list
44 1 my $dir = __FILE__;
45 1 $dir =~ s/[.]pm$//;
46  
47 1 ▶ if(-e "$dir/Cantonese.dict") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$dir/Cantonese.dict") 1 0 1
48 1 my $dict = retrieve("$dir/Cantonese.dict");
49 1 $self->{jyutping} = $dict->{jyutping};
50 1 $self->{chars} = $dict->{chars};
51 1 $self->{words} = $dict->{words};
52 1 $self->{word_index} = $dict->{word_index};
53 }
54  
55 1 return $self;
56 }
57  
58 1 ▶sub update_dict {
Subroutine update_dict: called CC=1 SCAR=0.0
Pod undocumented
59 1 my $self = shift;
60  
61 1 $self->{jyutping} = {};
62 1 $self->{chars} = {};
63 1 $self->{words} = {};
64 1 $self->{word_index} = {};
65  
66 1 $self->import_unihan("Cantonese.txt");
67 1 $self->import_zhy_list("zhy_list");
68  
69 my $dict = {jyutping => $self->{jyutping},
70 chars => $self->{chars},
71 words => $self->{words},
72 word_index => $self->{word_index},
73 1 };
74 1 store($dict, "Cantonese.dict");
75 }
76  
77 1 ▶sub import_unihan {
Subroutine import_unihan: called CC=7 SCAR=19.5
Pod undocumented
78 1 my ($self, $cantonese_txt) = @_;
79 1 open(DATA_FILE, '<', $cantonese_txt);
80 1 ▶ while(<DATA_FILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DATA_FILE)) 20014 1 20015
81 20014 chomp;
82 20014 my @line = split(/\s+/, $_);
83 20014 my $char = chr(hex($line[0]));
84 20014 my @phons = @line[1 .. $#line];
85 20014 ▶ if (not defined $self->{chars}->{$char}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"chars"}{$char}) 17357 2657 20014
86 17357 $self->{chars}->{$char} = \@phons;
87 }
88 20014 my $char_simp = utf8_to_simputf8($char);
89 20014 ▶ if ($char_simp !~ /[?]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($char_simp =~ /[?]/) 11160 8854 20014
90 11160 ▶ if (!defined $self->{chars}->{$char_simp}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"chars"}{$char_simp}) 1598 9562 11160
91 1598 $self->{chars}->{$char_simp} = \@phons;
92 }
93 }
94 20014 my $char_trad = utf8_to_tradutf8($char);
95 20014 ▶ if ($char_trad !~ /[?]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($char_trad =~ /[?]/) 13201 6813 20014
96 13201 ▶ if (!defined $self->{chars}->{$char_trad}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"chars"}{$char_trad}) 1117 12084 13201
97 1117 $self->{chars}->{$char_trad} = \@phons;
98 }
99 }
100 }
101 1 close(DATA_FILE);
102 }
103  
104 31469 ▶sub add_symbol {
Subroutine add_symbol: called CC=4 SCAR=14.2
Pod undocumented
105 31469 my ($self, $char, $symbol) = @_;
106  
107 31469 ▶ if (not $self->{chars}->{$char}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'chars'}{$char}) { } 0 31469 31469
108 0 $self->{chars}->{$char} = [$symbol];
109 0 return 1;
110 } else {
111 31469 foreach (@{$self->{chars}->{$char}}) {
112 39416 ▶ if ($symbol eq $_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbol eq $_) 30879 8537 39416
113 30879 return 0;
114 }
115 }
116 590 $self->{chars}->{$char} = [@{$self->{chars}->{$char}}, $symbol];
117 590 return 1;
118 }
119 }
120  
121 1 ▶sub import_zhy_list {
Subroutine import_zhy_list: called CC=9 SCAR=22.0
Pod undocumented
122 1 my ($self, $zhy_list) = @_;
123  
124 1 open(ZHY_LIST, '<:utf8', $zhy_list);
125 1 ▶ while (my $line = <ZHY_LIST>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline ZHY_LIST)) 24187 1 24188
126 24187 ▶ if ($line =~ /^(.)\s([^\s]*)\s$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^(.)\s([^\s]*)\s$/) { } 20133 4054 24187
elsif ($line =~ /^[(]([^)]*)[)]\s([^\s]*)\s$/) { } 3971 83 4054
127 20133 ▶ if ($1 && $2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 and $2) 20104 29 20133
Condition66%2/3
$1 and $2
!ll&&!rl&&r
02920104
MC/DC50%1/2
$1 and $2
$1 $2
Truth table66%2/3
A and B
A= $1B= $2
ABresult
0X0
100
111
128 20104 $self->{jyutping}->{$1} = $2;
129 20104 $self->add_symbol($1, $2);
130 }
131 } elsif ($line =~ /^[(]([^)]*)[)]\s([^\s]*)\s$/) {
132 3971 my @chars = split(/ /, $1);
133 3971 my @symbols = split(/[|]/, $2);
134 3971 ▶ if ($#chars != $#symbols) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#chars != $#symbols) 6 3965 3971
135 6 warn "Dictionary error:" . "@chars" . "-" . "@symbols";
136 6 next;
137 }
138 3965 my $word = join("", @chars);
139 3965 ▶ if ($self->{word_index}->{$chars[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'word_index'}{$chars[0]}) { } 3256 709 3965
140 3256 push(@{$self->{word_index}->{$chars[0]}}, $word);
141 } else {
142 709 $self->{word_index}->{$chars[0]} = [$word];
143 }
144 3965 $self->{words}->{$word} = \@symbols;
145 3965 ▶ for (my $i = 0; $i <= $#chars; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#chars) 11365 3965 15330
146 11365 $self->add_symbol($chars[$i], $symbols[$i]);
147 }
148 }
149 }
150 1 close(ZHY_LIST);
151  
152 # add numbers
153 1 $self->{jyutping}->{"0"} = "ling4";
154 1 $self->{jyutping}->{"1"} = "jat1";
155 1 $self->{jyutping}->{"2"} = "ji6";
156 1 $self->{jyutping}->{"3"} = "saam1";
157 1 $self->{jyutping}->{"4"} = "sei3";
158 1 $self->{jyutping}->{"5"} = "ng5";
159 1 $self->{jyutping}->{"6"} = "luk6";
160 1 $self->{jyutping}->{"7"} = "cat1";
161 1 $self->{jyutping}->{"8"} = "baat3";
162 1 $self->{jyutping}->{"9"} = "gau2";
163 }
164  
165 3 ▶sub get_jyutping {
Subroutine get_jyutping: called CC=14 SCAR=32.2
Pod documented
166 3 my ($self, $str) = @_;
167  
168 3 ▶ if (not utf8::is_utf8($str)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not utf8::is_utf8($str)) { } 0 3 3
elsif (not $str) { } 0 3 3
169 0 ▶ if (not utf8::decode($str)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::decode($str)) 0 0 0
170 0 warn "$str is not in utf8 encoding.";
171 0 return undef;
172 }
173 } elsif (not $str) {
174 0 return undef;
175 }
176  
177 3 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 1 2 3
178 1 my @jyutping;
179 1 ▶ for (my $i = 0; $i < length($str); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $str) 2 1 3
180 2 my $char = substr($str, $i, 1);
181 2 my @words = $self->get_words($char);
182 2 my $longest_word = '';
183 2 foreach my $word (@words) {
184 0 ▶ if (index($str, $word) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($str, $word) == 0) 0 0 0
185 0 ▶ if (length($word) > length($longest_word)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $word > length $longest_word) 0 0 0
186 0 $longest_word = $word;
187 }
188 }
189 }
190 2 ▶ if ($longest_word) {
Branch: 1 branch
BranchTrueFalseTotal
if ($longest_word) { } 0 2 2
191 0 push(@jyutping, @{$self->{words}->{$longest_word}});
192 0 $i += $#{$self->{words}->{$longest_word}};
193 } else {
194 2 push(@jyutping, $self->{jyutping}->{$char});
195 }
196 }
197 1 return @jyutping;
198 } else {
199 2 my $char = substr($str, 0, 1);
200 2 my @words = $self->get_words($char);
201 2 my $longest_word = '';
202 2 foreach my $word (@words) {
203 52 ▶ if (index($str, $word) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($str, $word) == 0) 1 51 52
204 1 ▶ if (length($word) > length($longest_word)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $word > length $longest_word) 1 0 1
205 1 $longest_word = $word;
206 }
207 }
208 }
209 2 ▶ if ($longest_word) {
Branch: 1 branch
BranchTrueFalseTotal
if ($longest_word) { } 1 1 2
210 1 return $self->{words}->{$longest_word}->[0];
211 } else {
212 1 return $self->{jyutping}->{$char};
213 }
214 }
215 }
216  
217 5 ▶sub get_words {
Subroutine get_words: called CC=2 SCAR=6.9
Pod documented
218 5 my ($self, $char) = @_;
219  
220 5 ▶ if ($self->{word_index}->{$char}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'word_index'}{$char}) { } 3 2 5
221 3 return @{$self->{word_index}->{$char}};
222 } else {
223 2 return ();
224 }
225 }
226  
227 0 ▶sub is_multi_phon {
Subroutine is_multi_phon: not called CC=1 SCAR=6.9
Pod documented
228 0 my ($self, $char) = @_;
229 0 return $#{$self->{chars}->{$char}};
230 }
231  
232 0 ▶sub get_multi_phon {
Subroutine get_multi_phon: not called CC=2 SCAR=17.9
Pod documented
233 0 my ($self, $char) = @_;
234 0 ▶ if ($self->{chars}->{$char}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'chars'}{$char}) { } 0 0 0
235 0 return @{$self->{chars}->{$char}};
236 } else {
237 0 return undef;
238 }
239 }
240  
241 3 1;
242 __END__
↑ Summary update_dict.pl »