blib/lib/Lingua/ZHO/Numbers.pm

filter: Statementstmt 86.3% 76 / 88 Branchbran 66.6% 24 / 36 Conditioncond 57.1% 20 / 35 MC/DCmcdc 22.2% 4 / 18 Subroutinesub 73.3% 11 / 15 Podpod 100% 6 / 6 totaltotal 71.2% 141 / 198 CC 33 SCAR 32.8
CC
33
Cov
75%
_convert
42.6
charset
6.2
map_zho
2.0
CRAP 26.6
↑ Summary
1 # For Emacs: -*- mode:cperl; mode:folding; coding:utf-8; -*-
2 #
3 # (c) 2003-2010 PetaMem, s.r.o.
4 #
5  
6 package Lingua::ZHO::Numbers;
7 # ABSTRACT: Number 2 word conversion in ZHO.
8  
9 # {{{ use block
10  
11 1 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use vars qw($Charset $VERSION @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # }}}
19 # {{{ variables declaration
20  
21 1 $Lingua::ZHO::Numbers::VERSION = 0.1192;
22  
23 1 @EXPORT_OK = 'number_to_zh';
24  
25 1 $Charset = 'pinyin';
26  
27 our %MAP = (
28 ($] >= 5.006) ? eval ## no critic
29 q(
30 'traditional' => {
31 mag => [ '', split(' ', "\x{842c} \x{5104} \x{5146} \x{4eac} \x{5793} \x{79ed} \x{7a70} \x{6e9d} \x{6f97} \x{6b63} \x{8f09} \x{6975} \x{6046}\x{6cb3}\x{6c99} \x{963f}\x{50e7}\x{7947} \x{90a3}\x{7531}\x{4ed6} \x{4e0d}\x{53ef}\x{601d}\x{8b70} \x{7121}\x{91cf}\x{5927}\x{6578}") ],
32 ord => [ '', split(' ', "\x{5341} \x{767e} \x{5343}") ],
33 dig => [ split(' ', "\x{96f6} \x{4e00} \x{4e8c} \x{4e09} \x{56db} \x{4e94} \x{516d} \x{4e03} \x{516b} \x{4e5d} \x{5341}") ],
34 dot => "\x{9ede}",
35 neg => "\x{8ca0}",
36 },
37 'simplified' => {
38 mag => [ '', split(' ', "\x{4e07} \x{4ebf} \x{5146} \x{4eac} \x{5793} \x{79ed} \x{7a70} \x{6c9f} \x{6da7} \x{6b63} \x{8f7d} \x{6781} \x{6052}\x{6cb3}\x{6c99} \x{963f}\x{50e7}\x{7957} \x{90a3}\x{7531}\x{4ed6} \x{4e0d}\x{53ef}\x{601d}\x{8bae} \x{65e0}\x{91cf}\x{5927}\x{6570}") ],
39 ord => [ '', split(' ', "\x{5341} \x{767e} \x{5343}") ],
40 dig => [ split(' ', "\x{96f6} \x{4e00} \x{4e8c} \x{4e09} \x{56db} \x{4e94} \x{516d} \x{4e03} \x{516b} \x{4e5d} \x{5341}") ],
41 dot => "\x{70b9}",
42 neg => "\x{8d1f}",
43 },
44 ) : (),
45 'big5' => {
46 mag => [ '', split(' ', "\xB8U \xBB\xF5 \xA5\xFC \xA8\xCA \xAB\xB2 \xD2\xF1 \xF6\xF8 \xB7\xBE \xBC\xEE \xA5\xBF \xB8\xFC \xB7\xA5 \xAB\xED\xAAe\xA8F \xAA\xFC\xB9\xAC\xAC\xE9 \xA8\xBA\xA5\xD1\xA5L \xA4\xA3\xA5i\xAB\xE4\xC4\xB3 \xB5L\xB6q\xA4j\xBC\xC6") ],
47 ord => [ '', split(' ', "\xA4Q \xA6\xCA \xA4d") ],
48 dig => [ split(' ', "\xB9s \xA4\@ \xA4G \xA4T \xA5| \xA4\xAD \xA4\xBB \xA4C \xA4K \xA4E \xA4Q") ],
49 dot => "\xC2I",
50 neg => "\xADt",
51 },
52 'gb' => {
53 mag => [ '', split(' ', "\xCD\xF2 \xD2\xDA \xD5\xD7 \xBE\xA9 \xDB\xF2 \xEF\xF6 \xF0\xA6 \xB9\xB5 \xBD\xA7 \xD5\xFD \xD4\xD8 \xBC\xAB \xBA\xE3\xBA\xD3\xC9\xB3 \xB0\xA2\xC9\xAE\xEC\xF3 \xC4\xC7\xD3\xC9\xCB\xFB \xB2\xBB\xBF\xC9\xCB\xBC\xD2\xE9 \xCE\xDE\xC1\xBF\xB4\xF3\xCA\xFD") ],
54 ord => [ '', split(' ', "\xCA\xAE \xB0\xD9 \xC7\xA7") ],
55 dig => [ split(' ', "\xC1\xE3 \xD2\xBB \xB6\xFE \xC8\xFD \xCB\xC4 \xCE\xE5 \xC1\xF9 \xC6\xDF \xB0\xCB \xBE\xC5 \xCA\xAE") ],
56 dot => "\xB5\xE3",
57 neg => "\xB8\xBA",
58 },
59 'pinyin' => {
60 17 mag => [ '', map {$_ } qw(
61 Wan Yi Zhao Jing Gai Zi Rang Gou Jian Zheng Zai Ji
62 HengHeSha AZengZhi NaYouTa BuKeSiYi WuLiangDaShu
63 ) ],
64 1 ▶ ord => [ '', map {$_ } qw(Shi Bai Qian) ],
Branch: 1 branch
BranchTrueFalseTotal
$] >= 5.006 ? : 1 0 1
65 dig => [ qw(Ling Yi Er San Si Wu Liu Qi Ba Jiu Shi) ],
66 dot => ' Dian ',
67 neg => 'Fu ',
68 },
69 );
70 # }}}
71  
72 # {{{ import
73  
74 sub import {
75 1 ▶ my ($class, $charset) = @_;
Subroutine import: called CC=1 SCAR=0.0
76 1 $class->charset($charset);
77 1 return $class->export_to_level(1, $class);
78 }
79  
80 # }}}
81 # {{{ charset
82  
83 sub charset {
84 4 ▶ my ($class, $charset) = @_;
Subroutine charset: called CC=6 SCAR=18.3
Pod documented
85  
86 1 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
87 4 ▶ return ${"$class\::Charset"} unless defined $charset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $charset 1 3 4
88  
89 3 ▶ $charset = 'gb' if $charset =~ /^gb/i or $charset =~ /^euc-cn$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $charset =~ /^gb/i or $charset =~ /^euc-cn$/i 0 3 3
Condition33%1/3
$charset =~ /^gb/i or $charset =~ /^euc-cn$/i
l!l&&r!l&&!r
003
MC/DC0%0/2
$charset =~ /^gb/i or $charset =~ /^euc-cn$/i
$charset =~ /^gb/i $charset =~ /^euc-cn$/i
Truth table33%1/3
A or B
A= $charset =~ /^gb/iB= $charset =~ /^euc-cn$/i
ABresult
1X1
011
000
90 3 ▶ $charset = 'big5' if $charset =~ /big5/i;
Branch: 1 branch
BranchTrueFalseTotal
if $charset =~ /big5/i 1 2 3
91 3 ▶ return ${"$class\::Charset"} = lc($charset) if exists ${"$class\::MAP"}{lc($charset)};
Branch: 1 branch
BranchTrueFalseTotal
if exists ${"${class}::MAP";}{lc $charset} 3 0 3
92 }
93  
94 # }}}
95 # {{{ map_zho
96  
97 sub map_zho {
98 0 ▶ return \%MAP;
Subroutine map_zho: not called CC=1 SCAR=6.9
Pod documented
99 }
100  
101 # }}}
102 # {{{ new
103  
104 sub new {
105 0 ▶ my ($class, $num) = @_;
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
106 0 bless (\$num, $class);
107 }
108  
109 # }}}
110 # {{{ parse
111  
112 sub parse {
113 0 ▶ my ($self, $num) = @_;
Subroutine parse: not called CC=1 SCAR=6.9
Pod documented
114 0 ${$self} = $num;
115 }
116  
117 # }}}
118 # {{{ get_string
119  
120 sub get_string {
121 0 ▶ my ($self) = @_;
Subroutine get_string: not called CC=1 SCAR=6.9
Pod documented
122 0 return number_to_zh($$self);
123 }
124  
125 # }}}
126 # {{{ number_to_zh
127  
128 sub number_to_zh {
129 12 ▶ my @a = @_;
Subroutine number_to_zh: called CC=1 SCAR=0.0
Pod documented
130 12 return __PACKAGE__->_convert($MAP{$Charset}, @a);
131 }
132  
133 # }}}
134 # {{{ convert
135  
136 sub _convert {
137 12 ▶ my ($class, $map, $input) = @_;
Subroutine _convert: called CC=28 SCAR=37.5
138  
139 12 ▶ croak 'You should specify a number from interval [0, trillion)'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $input && $input =~ /\A[\-\.\d]+\z/msx or $input >= '1000000000000000' 4 8 12
Condition66%4/6
not defined $input && $input =~ /\A[\-\.\d]+\z/msx or $input >= '1000000000000000'
l!l&&r!l&&!r
408
defined $input && $input =~ /\A[\-\.\d]+\z/msx
!ll&&!rl&&r
408
MC/DC33%1/3
not defined $input && $input =~ /\A[\-\.\d]+\z/msx or $input >= '1000000000000000'
defined $input $input =~ /\A[\-\.\d]+\z/msx $input >= '1000000000000000'
Truth table50%2/4
not(A && B) or C
A= defined $inputB= $input =~ /\A[\-\.\d]+\z/msxC= $input >= '1000000000000000'
ABCresult
0XX1
10X1
1111
1100
140 if !defined $input
141 || $input !~ m{\A[\-\.\d]+\z}xms
142 || $input >= 10 ** 15;
143  
144  
145 8 $input =~ s/[^\d\.\-]//;
146  
147 8 my @dig = @{$map->{dig}};
148 8 my @ord = @{$map->{ord}};
149 8 my @mag = @{$map->{mag}};
150 8 my $dot = $map->{dot};
151 8 my $neg = $map->{neg};
152  
153 8 my $out = '';
154 8 my $delta;
155 8 ▶ if ($input =~ s/\.(.*)//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input =~ s/\.(.*)//) 0 8 8
156 0 $delta = $1;
157 }
158  
159 8 ▶ $out = $neg if $input =~ s/^\-//;
Branch: 1 branch
BranchTrueFalseTotal
if $input =~ s/^\-// 0 8 8
160 8 $input =~ s/^0+//;
161 8 ▶ $input ||= '0';
Condition100%2/2
$input ||= 0
l!l
44
MC/DC100%1/1
$input ||= 0
$input
Truth table100%2/2
A
A= $input
Aresult
11
00
162  
163 8 my @chunks;
164 8 ▶ unshift @chunks, $1 while ($input =~ s/(\d{1,4})$//g);
Branch: 1 branch
BranchTrueFalseTotal
$input =~ s/(\d{1,4})$//g and do { unshift @chunks, $1 } 12 8 20
165 8 my $mag = $#chunks;
166 8 ▶ my $zero = ($] >= 5.005) ? eval 'qr/$dig[0]$/' : quotemeta($dig[0]) . '$'; ## no critic
Branch: 1 branch
BranchTrueFalseTotal
$] >= 5.005 ? : 8 0 8
167  
168 8 foreach my $num (@chunks) {
169 12 my $tmp = '';
170  
171 12 for (reverse 0..3) {
172 48 my $n = int($num / (10 ** $_)) % 10;
173 48 ▶ next unless $tmp or $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $tmp or $n 28 20 48
Condition100%3/3
$tmp or $n
l!l&&r!l&&!r
12828
MC/DC100%2/2
$tmp or $n
$tmp $n
Truth table100%3/3
A or B
A= $tmpB= $n
ABresult
1X1
011
000
174 20 ▶ $tmp .= $dig[$n] unless ($n == 0 and $tmp =~ $zero)
Branch: 1 branch
BranchTrueFalseTotal
unless $n == 0 and $tmp =~ /$zero/ or $_ == 1 and $n == 1 and not $tmp 4 0 4
Condition41%5/12
$n == 0 and $tmp =~ /$zero/ or $_ == 1 and $n == 1 and not $tmp
l!l&&r!l&&!r
0020
$n == 0 and $tmp =~ /$zero/
!ll&&!rl&&r
2000
$_ == 1 and $n == 1 and not $tmp
!ll&&!rl&&r
2000
$_ == 1 and $n == 1
!ll&&!rl&&r
1640
MC/DC0%0/5
$n == 0 and $tmp =~ /$zero/ or $_ == 1 and $n == 1 and not $tmp
$n == 0 $tmp =~ /$zero/ $_ == 1 $n == 1 not $tmp
Truth table0%0/9
A and B or C and D and not(E)
A= $n == 0B= $tmp =~ /$zero/C= $_ == 1D= $n == 1E= not $tmp
ABCDEresult
11XXX1
0X1111
101111
0X0XX0
0X10X0
0X1100
100XX0
1010X0
101100
175 or ($_ == 1 and $n == 1 and not $tmp);
176 20 ▶ $tmp .= $ord[$_] if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 20 0 20
177 }
178  
179 12 ▶ $tmp =~ s/$zero// unless $tmp eq $dig[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $tmp eq $dig[0] 12 0 12
180 12 ▶ $tmp .= $mag[$mag] if $tmp;
Branch: 1 branch
BranchTrueFalseTotal
if $tmp 8 4 12
181 12 ▶ $tmp = $dig[0].$tmp if $num < 1000 and $mag != $#chunks
Branch: 1 branch
BranchTrueFalseTotal
if $num < 1000 and $mag != $#chunks and not $out =~ /$zero/ 0 12 12
Condition50%3/6
$num < 1000 and $mag != $#chunks and not $out =~ /$zero/
!ll&&!rl&&r
1200
$num < 1000 and $mag != $#chunks
!ll&&!rl&&r
480
MC/DC0%0/3
$num < 1000 and $mag != $#chunks and not $out =~ /$zero/
$num < 1000 $mag != $#chunks not $out =~ /$zero/
Truth table50%2/4
A and B and not(C)
A= $num < 1000B= $mag != $#chunksC= not $out =~ /$zero/
ABCresult
0XX0
10X0
1100
1111
182 and $out !~ $zero;
183 12 $out .= $tmp;
184 12 $mag--;
185 }
186  
187 8 ▶ $out =~ s/$zero// unless $out eq $dig[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $out eq $dig[0] 8 0 8
188  
189 8 ▶ if ($delta) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta) 0 8 8
190 0 $delta =~ s/^0\.//;
191 0 $out .= $dot;
192 0 $out .= $dig[$_] for split(//, $delta);
193 }
194  
195 8 ▶ return $out || $dig[0];
Condition66%2/3
$out || $dig[0]
l!l&&r!l&&!r
440
MC/DC0%0/2
$out || $dig[0]
$out $dig[0]
Truth table66%2/3
A || B
A= $outB= $dig[0]
ABresult
1X1
011
000
196 }
197  
198 # }}}
199  
200 1 1;
201 __END__
↑ Summary