ZhuYin.pm

filter: Statementstmt 58.9% 66 / 112 Branchbran 32.6% 15 / 46 Conditioncond 26.6% 4 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 50.2% 98 / 195 CC 36 SCAR 51.7
CC
36
Cov
49%
guess_zhuyin
300.4
zhuyin
7.0
AUTOLOAD
4.3
CRAP 175.2
↑ Summary ZhuYin/Dict.pm »
1 package Lingua::ZH::ZhuYin;
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 utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 Lingua::ZH::ZhuYin - The great new Lingua::ZH::ZhuYin!
10  
11 =head1 VERSION
12  
13 Version 0.04
14  
15 =cut
16  
17 3 our $VERSION = '0.04';
18 3 our $AUTOLOAD;
19 3 our %ok_field;
20 3 ▶use Encode qw/decode/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use List::Util qw/min max/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use Lingua::ZH::ZhuYin::Dict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24  
25 =head1 SYNOPSIS
26  
27 Quick summary of what the module does.
28  
29 Perhaps a little code snippet.
30  
31 use Lingua::ZH::ZhuYin;
32  
33 my $foo = Lingua::ZH::ZhuYin->new();
34 my $zhuyin = $foo->zhuyin($phrase);
35 ...
36  
37 =head1 EXPORT
38  
39 A list of functions that can be exported. You can delete this section
40 if you don't export anything, such as for a purely object-oriented module.
41  
42 =head1 FUNCTIONS
43  
44 =head2 AUTOLOAD
45  
46 =cut
47  
48 3 for my $attr ( qw(dictfile) ) { $ok_field{$attr}++; }
49  
50 5 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=4 SCAR=14.6
51 5 my $self = shift;
52 5 my $attr = $AUTOLOAD;
53 5 $attr =~ s/.*:://;
54 5 ▶ return if $attr eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $attr eq "DESTROY" 1 4 5
55  
56 4 ▶ if ($ok_field{$attr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Lingua::ZH::ZhuYin::ok_field{$attr}) { } 4 0 4
57 4 ▶ $self->{lc $attr} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4 4
58 4 return $self->{lc $attr};
59 } else {
60 0 my $superior = "SUPER::$attr";
61 0 $self->$superior(@_);
62 }
63 }
64  
65 =head2 new
66  
67 =cut
68  
69 1 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
70 1 my $class = shift;
71 1 my $self = {
72 dictfile => undef,
73 };
74 1 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
75 1 my %arg = @_;
76  
77 1 foreach (keys %arg) {
78 1 $self->{lc($_)} = $arg{$_};
79 }
80 }
81 1 bless ($self, $class);
82 1 return($self);
83 }
84  
85 =head2 zhuyin
86  
87 =cut
88  
89 2 ▶sub zhuyin {
Subroutine zhuyin: called CC=6 SCAR=19.4
Pod documented
90 2 my $self = shift;
91 2 my $word = shift;
92 2 ▶ die "DictFile does not exist" unless $self->dictfile;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->dictfile 0 2 2
93 2 my $utf8word = decode('utf8',$word);
94 2 my @zhuyins = $self->guess_zhuyin($word);
95 2 ▶ push @zhuyins , $utf8word if (! @zhuyins and length($utf8word) == 1);
Branch: 1 branch
BranchTrueFalseTotal
if not @zhuyins and length $utf8word == 1 0 2 2
Condition33%1/3
not @zhuyins and length $utf8word == 1
!ll&&!rl&&r
200
MC/DC0%0/2
not @zhuyins and length $utf8word == 1
not @zhuyins length $utf8word == 1
Truth table33%1/3
not(A) and B
A= not @zhuyinsB= length $utf8word == 1
ABresult
0X0
100
111
96 2 ▶ return '' if $zhuyins[0] eq '0';
Branch: 1 branch
BranchTrueFalseTotal
if $zhuyins[0] eq 0 0 2 2
97 2 ▶ warn 'no zhuyin found: '.$word if ! @zhuyins;
Branch: 1 branch
BranchTrueFalseTotal
unless @zhuyins 0 2 2
98 2 return \@zhuyins;
99 }
100  
101 =head2 guess_zhuyin
102  
103 =cut
104  
105 2 ▶sub guess_zhuyin {
Subroutine guess_zhuyin: called CC=26 SCAR=57.0
Pod documented
106 2 my $self = shift;
107 2 my $word = shift;
108 # perform guess zhuyin from ABCDE, ABCD E, ABC DE, AB CDE, A BCDE
109 2 my @zhuyins;
110 2 my $Dict = Lingua::ZH::ZhuYin::Dict->new($self->dictfile);
111 2 for my $i (0..(length($word) - 1)) {
112 2 @zhuyins = ();
113 2 my $offset = length($word) - $i;
114 2 my $pre_word = substr($word,0,$offset);
115 2 my $post_word = '';
116 2 ▶ $post_word = substr($word,$offset) if $i > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 0 0 2 2
117 2 my $skip = 1;
118 2 ▶ die "word error " unless $word eq $pre_word.$post_word;
Branch: 1 branch
BranchTrueFalseTotal
unless $word eq $pre_word . $post_word 0 2 2
119 2 ▶ if ($pre_word and $pre_word ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($pre_word and $pre_word ne "") 2 0 2
Condition33%1/3
$pre_word and $pre_word ne ""
!ll&&!rl&&r
002
MC/DC0%0/2
$pre_word and $pre_word ne ""
$pre_word $pre_word ne ""
Truth table33%1/3
A and B
A= $pre_wordB= $pre_word ne ""
ABresult
0X0
100
111
120 2 my @pre_zhuyins = $Dict->queryZhuYin($pre_word);
121 2 ▶ $skip = 0 if @pre_zhuyins;
Branch: 1 branch
BranchTrueFalseTotal
if @pre_zhuyins 2 0 2
122 2 push @zhuyins, @pre_zhuyins;
123 }
124 2 ▶ if ($skip == 0 and $post_word and $post_word ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip == 0 and $post_word and $post_word ne "") 0 2 2
Condition33%2/6
$skip == 0 and $post_word and $post_word ne ""
!ll&&!rl&&r
200
$skip == 0 and $post_word
!ll&&!rl&&r
020
MC/DC0%0/3
$skip == 0 and $post_word and $post_word ne ""
$skip == 0 $post_word $post_word ne ""
Truth table25%1/4
A and B and C
A= $skip == 0B= $post_wordC= $post_word ne ""
ABCresult
0XX0
10X0
1100
1111
125 0 $skip = 1;
126 0 my @post_zhuyins = $Dict->queryZhuYin($post_word);
127 0 ▶ $skip = 0 if @post_zhuyins;
Branch: 1 branch
BranchTrueFalseTotal
if @post_zhuyins 0 0 0
128 0 my @tmp_zhuyins = ();
129 0 foreach my $j (0..$#zhuyins) {
130 0 foreach my $yin (@post_zhuyins) {
131 0 push @tmp_zhuyins, $zhuyins[$j] ." ". $yin;
132 }
133 }
134 0 @zhuyins = @tmp_zhuyins;
135 }
136 2 ▶ return @zhuyins if $skip == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $skip == 0 2 0 2
137 }
138  
139 0 ▶ return if length($word) == 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $word == 1 0 0 0
140 # preform A B C D E, if each term has unique zhuyin, then we done,
141 # otherwise need further process
142 0 my @array = ();
143 0 my @ambig = ();
144 0 @zhuyins = ();
145 0 my $skip = 0;
146 0 for my $i (0..(length($word) - 1)) {
147 0 my $unichar = substr($word,$i,1);
148 0 my @uni_zhuyins = $Dict->queryZhuYin($unichar);
149 0 ▶ return '0' unless @uni_zhuyins;
Branch: 1 branch
BranchTrueFalseTotal
unless @uni_zhuyins 0 0 0
150 0 ▶ if (scalar @uni_zhuyins != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @uni_zhuyins != 1) { } 0 0 0
151 0 $array[$i] = 1;
152 0 push @ambig, $i;
153 0 $skip = 1;
154 } else {
155 0 $array[$i] = 0;
156 0 $zhuyins[$i] = $uni_zhuyins[0];
157 }
158 }
159 0 ▶ return @zhuyins if $skip == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $skip == 0 0 0 0
160  
161 # if B is amibiguos, we chcek AB, BC, ABC, BCD ...
162 # otherwise, return the first one
163 0 for my $amb (@ambig) {
164 0 my $max_length = min (max (length($word) - $amb, $amb), 4); # at most check 4-gram
165 0 my $not_found = 1;
166 0 my $len = 2;
167 0 ▶ while ($not_found && $len <= $max_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($not_found and $len <= $max_length) 0 0 0
Condition0%0/3
$not_found and $len <= $max_length
!ll&&!rl&&r
000
MC/DC0%0/2
$not_found and $len <= $max_length
$not_found $len <= $max_length
Truth table0%0/3
A and B
A= $not_foundB= $len <= $max_length
ABresult
0X0
100
111
168 0 my $pos_b = max (0, $amb - $len + 1);
169 0 my $pos_e = min (length($word), $amb);
170 0 for my $pos ($pos_b..$pos_e) {
171 0 ▶ next if $not_found == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $not_found == 0 0 0 0
172 0 my @ngram_zhuyins = $Dict->queryZhuYin(substr($word,$pos,$len));
173 0 ▶ if (scalar @ngram_zhuyins == 1) { # yatta !!!
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @ngram_zhuyins == 1) 0 0 0
174 0 my @zhuyin_array = split / /,$ngram_zhuyins[0];
175 0 $zhuyins[$amb] = $zhuyin_array[$amb-$pos];
176 0 $not_found = 0;
177 }
178 }
179 0 $len++;
180 }
181 0 ▶ if ($not_found) { # still not found
Branch: 1 branch
BranchTrueFalseTotal
if ($not_found) 0 0 0
182 0 my $unichar = substr($word,$amb,1);
183 0 my @uni_zhuyins = $Dict->queryZhuYin($unichar);
184 0 $zhuyins[$amb] = $uni_zhuyins[0];
185 }
186 }
187 0 return join " ",@zhuyins;
188 }
189  
190 =head1 AUTHOR
191  
192 Cheng-Lung Sung, C<< <clsung at cpan.org> >>
193  
194 =head1 BUGS
195  
196 Please report any bugs or feature requests to C<bug-lingua-zh-zhuyin at rt.cpan.org>, or through
197 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Lingua-ZH-ZhuYin>. I will be notified, and then you'll
198 automatically be notified of progress on your bug as I make changes.
199  
200  
201  
202  
203 =head1 SUPPORT
204  
205 You can find documentation for this module with the perldoc command.
206  
207 perldoc Lingua::ZH::ZhuYin
208  
209  
210 You can also look for information at:
211  
212 =over 4
213  
214 =item * RT: CPAN's request tracker
215  
216 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Lingua-ZH-ZhuYin>
217  
218 =item * AnnoCPAN: Annotated CPAN documentation
219  
220 L<http://annocpan.org/dist/Lingua-ZH-ZhuYin>
221  
222 =item * CPAN Ratings
223  
224 L<http://cpanratings.perl.org/d/Lingua-ZH-ZhuYin>
225  
226 =item * Search CPAN
227  
228 L<http://search.cpan.org/dist/Lingua-ZH-ZhuYin>
229  
230 =back
231  
232  
233 =head1 ACKNOWLEDGEMENTS
234  
235  
236 =head1 COPYRIGHT & LICENSE
237  
238 Copyright 2008 Cheng-Lung Sung, all rights reserved.
239  
240 This program is free software; you can redistribute it and/or modify it
241 under the same terms as Perl itself.
242  
243  
244 =cut
245  
246 3 1; # End of Lingua::ZH::ZhuYin
↑ Summary ZhuYin/Dict.pm »