blib/lib/Lingua/ZHO/Numbers.pm

filter: Statementstmt 86.8% 79 / 91 Branchbran 66.6% 24 / 36 Conditioncond 57.1% 20 / 35 MC/DCmcdc 22.2% 4 / 18 Subroutinesub 75.0% 12 / 16 Podpod 100% 6 / 6 totaltotal 71.7% 145 / 202 CC 33 SCAR 32.6
CC
33
Cov
76%
_convert
42.6
charset
6.2
map_zho
2.0
CRAP 26.0
↑ Summary
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1); 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 1 ▶use 5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # {{{ use block
14  
15 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use vars qw($Charset @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # }}}
21 # {{{ variables declaration
22 1 our $VERSION = '0.2603260';
23  
24 1 @EXPORT_OK = 'number_to_zh';
25  
26 1 $Charset = 'pinyin';
27  
28 our %MAP = (
29 ($] >= 5.006) ? eval ## no critic
30 q(
31 'traditional' => {
32 mag => [ '', split(' ', "萬 億 兆 京 垓 秭 穰 溝 澗 正 載 極 恆河沙 阿僧祇 那由他 不可思議 無量大數") ],
33 ord => [ '', split(' ', "十 百 千") ],
34 dig => [ split(' ', "零 一 二 三 四 五 六 七 八 九 十") ],
35 dot => "點",
36 neg => "負",
37 },
38 'simplified' => {
39 mag => [ '', split(' ', "万 亿 兆 京 垓 秭 穰 沟 涧 正 载 极 恒河沙 阿僧祗 那由他 不可思议 无量大数") ],
40 ord => [ '', split(' ', "十 百 千") ],
41 dig => [ split(' ', "零 一 二 三 四 五 六 七 八 九 十") ],
42 dot => "点",
43 neg => "负",
44 },
45 ) : (),
46 'big5' => {
47 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") ],
48 ord => [ '', split(' ', "\xA4Q \xA6\xCA \xA4d") ],
49 dig => [ split(' ', "\xB9s \xA4\@ \xA4G \xA4T \xA5| \xA4\xAD \xA4\xBB \xA4C \xA4K \xA4E \xA4Q") ],
50 dot => "\xC2I",
51 neg => "\xADt",
52 },
53 'gb' => {
54 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") ],
55 ord => [ '', split(' ', "\xCA\xAE \xB0\xD9 \xC7\xA7") ],
56 dig => [ split(' ', "\xC1\xE3 \xD2\xBB \xB6\xFE \xC8\xFD \xCB\xC4 \xCE\xE5 \xC1\xF9 \xC6\xDF \xB0\xCB \xBE\xC5 \xCA\xAE") ],
57 dot => "\xB5\xE3",
58 neg => "\xB8\xBA",
59 },
60 'pinyin' => {
61 17 mag => [ '', map {$_ } qw(
62 Wan Yi Zhao Jing Gai Zi Rang Gou Jian Zheng Zai Ji
63 HengHeSha AZengZhi NaYouTa BuKeSiYi WuLiangDaShu
64 ) ],
65 1 ▶ ord => [ '', map {$_ } qw(Shi Bai Qian) ],
Branch: 1 branch
BranchTrueFalseTotal
$] >= 5.006 ? : 1 0 1
66 dig => [ qw(Ling Yi Er San Si Wu Liu Qi Ba Jiu Shi) ],
67 dot => ' Dian ',
68 neg => 'Fu ',
69 },
70 );
71 # }}}
72  
73 # {{{ import
74  
75 sub import {
76 1 ▶ my ($class, $charset) = @_;
Subroutine import: called CC=1 SCAR=0.0
77 1 $class->charset($charset);
78 1 return $class->export_to_level(1, $class);
79 }
80  
81 # }}}
82 # {{{ charset
83  
84 sub charset {
85 4 ▶ my ($class, $charset) = @_;
Subroutine charset: called CC=6 SCAR=18.3
Pod documented
86  
87 1 ▶ no strict 'refs'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
88 4 ▶ return ${"$class\::Charset"} unless defined $charset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $charset 1 3 4
89  
90 3 ▶ $charset = 'gb' if $charset =~ /^gb/i or $charset =~ /^euc-cn$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $charset =~ /^gb/iu or $charset =~ /^euc-cn$/iu 0 3 3
Condition33%1/3
$charset =~ /^gb/iu or $charset =~ /^euc-cn$/iu
l!l&&r!l&&!r
003
MC/DC0%0/2
$charset =~ /^gb/iu or $charset =~ /^euc-cn$/iu
$charset =~ /^gb/iu $charset =~ /^euc-cn$/iu
Truth table33%1/3
A or B
A= $charset =~ /^gb/iuB= $charset =~ /^euc-cn$/iu
ABresult
1X1
011
000
91 3 ▶ $charset = 'big5' if $charset =~ /big5/i;
Branch: 1 branch
BranchTrueFalseTotal
if $charset =~ /big5/iu 1 2 3
92 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
93 }
94  
95 # }}}
96 # {{{ map_zho
97  
98 sub map_zho {
99 0 ▶ return \%MAP;
Subroutine map_zho: not called CC=1 SCAR=6.9
Pod documented
100 }
101  
102 # }}}
103 # {{{ new
104  
105 sub new {
106 0 ▶ my ($class, $num) = @_;
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
107 0 bless (\$num, $class);
108 }
109  
110 # }}}
111 # {{{ parse
112  
113 sub parse {
114 0 ▶ my ($self, $num) = @_;
Subroutine parse: not called CC=1 SCAR=6.9
Pod documented
115 0 ${$self} = $num;
116 }
117  
118 # }}}
119 # {{{ get_string
120  
121 sub get_string {
122 0 ▶ my ($self) = @_;
Subroutine get_string: not called CC=1 SCAR=6.9
Pod documented
123 0 return number_to_zh($$self);
124 }
125  
126 # }}}
127 # {{{ number_to_zh
128  
129 sub number_to_zh {
130 12 ▶ my @a = @_;
Subroutine number_to_zh: called CC=1 SCAR=0.0
Pod documented
131 12 return __PACKAGE__->_convert($MAP{$Charset}, @a);
132 }
133  
134 # }}}
135 # {{{ convert
136  
137 sub _convert {
138 12 ▶ my ($class, $map, $input) = @_;
Subroutine _convert: called CC=28 SCAR=37.5
139  
140 12 ▶ croak 'You should specify a number from interval [0, trillion)'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $input && $input =~ /\A[\-\.\d]+\z/msux or $input >= '1000000000000000' 4 8 12
Condition66%4/6
not defined $input && $input =~ /\A[\-\.\d]+\z/msux or $input >= '1000000000000000'
l!l&&r!l&&!r
408
defined $input && $input =~ /\A[\-\.\d]+\z/msux
!ll&&!rl&&r
408
MC/DC33%1/3
not defined $input && $input =~ /\A[\-\.\d]+\z/msux or $input >= '1000000000000000'
defined $input $input =~ /\A[\-\.\d]+\z/msux $input >= '1000000000000000'
Truth table50%2/4
not(A && B) or C
A= defined $inputB= $input =~ /\A[\-\.\d]+\z/msuxC= $input >= '1000000000000000'
ABCresult
0XX1
10X1
1111
1100
141 if !defined $input
142 || $input !~ m{\A[\-\.\d]+\z}xms
143 || $input >= 10 ** 15;
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/\.(.*)//u) 0 8 8
156 0 $delta = $1;
157 }
158  
159 8 ▶ $out = $neg if $input =~ s/^\-//;
Branch: 1 branch
BranchTrueFalseTotal
if $input =~ s/^\-//u 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})$//gu 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/u or $_ == 1 and $n == 1 and not $tmp 4 0 4
Condition41%5/12
$n == 0 and $tmp =~ /$zero/u or $_ == 1 and $n == 1 and not $tmp
l!l&&r!l&&!r
0020
$n == 0 and $tmp =~ /$zero/u
!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/u or $_ == 1 and $n == 1 and not $tmp
$n == 0 $tmp =~ /$zero/u $_ == 1 $n == 1 not $tmp
Truth table0%0/9
A and B or C and D and not(E)
A= $n == 0B= $tmp =~ /$zero/uC= $_ == 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/u 0 12 12
Condition50%3/6
$num < 1000 and $mag != $#chunks and not $out =~ /$zero/u
!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/u
$num < 1000 $mag != $#chunks not $out =~ /$zero/u
Truth table50%2/4
A and B and not(C)
A= $num < 1000B= $mag != $#chunksC= not $out =~ /$zero/u
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