blib/lib/Lingua/IND/Nums2Words.pm

filter: Statementstmt 100% 91 / 91 Branchbran 86.3% 38 / 44 Conditioncond 69.0% 58 / 84 MC/DCmcdc 22.5% 9 / 40 Subroutinesub 100% 15 / 15 Podpod 100% 9 / 9 totaltotal 77.7% 220 / 283 CC 51 SCAR 26.3
CC
51
Cov
85%
n2w4
27.2
n2w2
13.0
n2w5
6.0
CRAP 13.9
↑ Summary
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1) -*-
2  
3 package Lingua::IND::Nums2Words;
4 # ABSTRACT: Number 2 word conversion in IND.
5  
6 1 ▶use 5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # {{{ use block
11  
12 1 ▶use Export::Attrs;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # }}}
15 # {{{ variables declaration
16 1 our $VERSION = '0.2603260';
17  
18 1 our $Dec_char = ".";
19 1 our $Neg_word = "negatif";
20 1 our $Dec_word = "koma";
21 1 our $Exp_word = "dikali sepuluh pangkat";
22 1 our $Zero_word = "nol";
23  
24 1 our %Digit_words = (
25 0 => $Zero_word,
26 1 => 'satu',
27 2 => 'dua',
28 3 => 'tiga',
29 4 => 'empat',
30 5 => 'lima',
31 6 => 'enam',
32 7 => 'tujuh',
33 8 => 'delapan',
34 9 => 'sembilan'
35 );
36  
37 1 our %Mult_words = (
38 0 => '',
39 1 => 'ribu',
40 2 => 'juta',
41 3 => 'milyar',
42 4 => 'triliun'
43 );
44  
45 # }}}
46  
47 ### public subs
48 # {{{ nums2words
49  
50 sub nums2words :Export {
51 15 ▶ my @a = @_;
Subroutine nums2words: called CC=1 SCAR=0.0
Pod documented
52 15 return join_it(n2w1(@a));
53 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 # }}}
56 # {{{ nums2words_simple
57  
58 sub nums2words_simple :Export {
59 6 ▶ my @a = @_;
Subroutine nums2words_simple: called CC=1 SCAR=0.0
Pod documented
60 6 return join_it(n2w5(@a));
61 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 # }}}
64  
65 ### private subs
66  
67 # for debugging
68 1 our $DEBUG = 0;
69 13 ▶sub hmm___ { my @a = @_;print "(", (caller 1)[3], ") Hmm, ", @a if $DEBUG; return; }
Subroutine hmm___: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $Lingua::IND::Nums2Words::DEBUG 0 13 13
70 # {{{ n2w1 handle scientific notation
71 sub n2w1 {
72 15 ▶ my $num = shift // return '';
Subroutine n2w1: called CC=5 SCAR=16.6
Pod documented
Condition100%2/2
shift() // (return "")
l!l
141
MC/DC100%1/1
shift() // (return "")
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
73  
74 14 ▶ return $Zero_word if $num >= 10 ** 15; # not quadrillion and more
Branch: 1 branch
BranchTrueFalseTotal
if $num >= '1000000000000000' 1 13 14
75  
76 13 my @words;
77  
78 13 ▶ $num =~ /^(.+)[Ee](.+)$/ and
Branch: 1 branch
BranchTrueFalseTotal
$num =~ /^(.+)[Ee](.+)$/u and @words = (n2w2($1), $Lingua::IND::Nums2Words::Exp_word, n2w2($2)) or @words = n2w2($num) 13 0 13
Condition33%1/3
$num =~ /^(.+)[Ee](.+)$/u and @words = (n2w2($1), $Lingua::IND::Nums2Words::Exp_word, n2w2($2))
!ll&&!rl&&r
1300
MC/DC0%0/2
$num =~ /^(.+)[Ee](.+)$/u and @words = (n2w2($1), $Lingua::IND::Nums2Words::Exp_word, n2w2($2))
$num =~ /^(.+)[Ee](.+)$/u @words = (n2w2($1), $Lingua::IND::Nums2Words::Exp_word, n2w2($2))
Truth table33%1/3
A and B
A= $num =~ /^(.+)[Ee](.+)$/uB= @words = (n2w2($1), $Lingua::IND::Nums2Words::Exp_word, n2w2($2))
ABresult
0X0
100
111
79 @words = (n2w2($1), $Exp_word, n2w2($2)) or
80 @words = n2w2($num);
81  
82 13 return @words;
83 }
84  
85 # }}}
86 # {{{ n2w2 handle negative sign and decimal
87  
88 sub n2w2 {
89 13 ▶ my $num = shift // return '';
Subroutine n2w2: called CC=9 SCAR=25.6
Pod documented
Condition50%1/2
shift() // (return "")
l!l
130
MC/DC0%0/1
shift() // (return "")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
90 13 my $is_neg;
91 13 my @words = ();
92  
93 # negative
94 13 ▶ $num < 0 and $is_neg++;
Branch: 1 branch
BranchTrueFalseTotal
$num < 0 and ++$is_neg 2 11 13
95 13 $num =~ s/^[\s\t]*[+-]*(.*)/$1/;
96  
97 # decimal
98 $num =~ /^(.+)\Q$Dec_char\E(.+)$/o and
99 @words = (n2w3($1), $Dec_word, n2w5($2)) or
100  
101 $num =~ /^\Q$Dec_char\E(.+)$/o and
102 13 ▶ @words = ($Digit_words{0}, $Dec_word, n2w5($1)) or
Branch: 1 branch
BranchTrueFalseTotal
$num =~ /^(.+)\Q$Lingua::IND::Nums2Words::Dec_char\E(.+)$/ou and @words = (n2w3($1), $Lingua::IND::Nums2Words::Dec_word, n2w5($2)) or $num =~ /^\Q$Lingua::IND::Nums2Words::Dec_char\E(.+)$/ou and @words = ($Lingua::IND::Nums2Words::Digit_words{0}, $Lingua::IND::Nums2Words::Dec_word, n2w5($1)) or $num =~ /^(.+)(?:\Q$Lingua::IND::Nums2Words::Dec_char\E)?$/ou and @words = n2w3($1) 13 0 13
Condition35%5/14
$num =~ /^(.+)\Q$Lingua::IND::Nums2Words::Dec_char\E(.+)$/ou && (@words = (n2w3($1), $Lingua::IND::Nums2Words::Dec_word, n2w5($2))) || $num =~ /^\Q$Lingua::IND::Nums2Words::Dec_char\E(.+)$/ou && (@words = ($Lingua::IND::Nums2Words::Digit_words{'0'}, $Lingua::IND::Nums2Words::Dec_word, n2w5($1))) || $num =~ /^(.+)(?:\Q$Lingua::IND::Nums2Words::Dec_char\E)?$/ou && (@words = n2w3($1))
l!l&&r!l&&!r
0130
$num =~ /^(.+)\Q$Lingua::IND::Nums2Words::Dec_char\E(.+)$/ou and @words = (n2w3($1), $Lingua::IND::Nums2Words::Dec_word, n2w5($2)) or $num =~ /^\Q$Lingua::IND::Nums2Words::Dec_char\E(.+)$/ou and @words = ($Lingua::IND::Nums2Words::Digit_words{0}, $Lingua::IND::Nums2Words::Dec_word, n2w5($1))
l!l&&r!l&&!r
0013
$num =~ /^(.+)\Q$Lingua::IND::Nums2Words::Dec_char\E(.+)$/ou and @words = (n2w3($1), $Lingua::IND::Nums2Words::Dec_word, n2w5($2))
!ll&&!rl&&r
1300
$num =~ /^\Q$Lingua::IND::Nums2Words::Dec_char\E(.+)$/ou and @words = ($Lingua::IND::Nums2Words::Digit_words{0}, $Lingua::IND::Nums2Words::Dec_word, n2w5($1))
!ll&&!rl&&r
1300
$num =~ /^(.+)(?:\Q$Lingua::IND::Nums2Words::Dec_char\E)?$/ou and @words = n2w3($1)
!ll
013
MC/DC0%0/6
$num =~ /^(.+)\Q$Lingua::IND::Nums2Words::Dec_char\E(.+)$/ou && (@words = (n2w3($1), $Lingua::IND::Nums2Words::Dec_word, n2w5($2))) || $num =~ /^\Q$Lingua::IND::Nums2Words::Dec_char\E(.+)$/ou && (@words = ($Lingua::IND::Nums2Words::Digit_words{'0'}, $Lingua::IND::Nums2Words::Dec_word, n2w5($1))) || $num =~ /^(.+)(?:\Q$Lingua::IND::Nums2Words::Dec_char\E)?$/ou && (@words = n2w3($1))
$num =~ /^(.+)\Q$Lingua::IND::Nums2Words::Dec_char\E(.+)$/ou @words = (n2w3($1), $Lingua::IND::Nums2Words::Dec_word, n2w5($2)) $num =~ /^\Q$Lingua::IND::Nums2Words::Dec_char\E(.+)$/ou @words = ($Lingua::IND::Nums2Words::Digit_words{0}, $Lingua::IND::Nums2Words::Dec_word, n2w5($1)) $num =~ /^(.+)(?:\Q$Lingua::IND::Nums2Words::Dec_char\E)?$/ou @words = n2w3($1)
Truth table0%0/15
A and B or C and D || E and F
A= $num =~ /^(.+)\Q$Lingua::IND::Nums2Words::Dec_char\E(.+)$/ouB= @words = (n2w3($1), $Lingua::IND::Nums2Words::Dec_word, n2w5($2))C= $num =~ /^\Q$Lingua::IND::Nums2Words::Dec_char\E(.+)$/ouD= @words = ($Lingua::IND::Nums2Words::Digit_words{0}, $Lingua::IND::Nums2Words::Dec_word, n2w5($1))E= $num =~ /^(.+)(?:\Q$Lingua::IND::Nums2Words::Dec_char\E)?$/ouF= @words = n2w3($1)
ABCDEFresult
11XXXX1
0X11XX1
1011XX1
0X0X111
0X10111
100X111
1010111
0X0X0X0
0X0X100
0X100X0
0X10100
100X0X0
100X100
10100X0
1010100
103  
104 $num =~ /^(.+)(?:\Q$Dec_char\E)?$/o and
105 @words = n2w3($1);
106  
107 13 ▶ $is_neg and
Branch: 1 branch
BranchTrueFalseTotal
$is_neg and unshift @words, $Lingua::IND::Nums2Words::Neg_word 2 11 13
108 unshift @words, $Neg_word;
109  
110 13 return @words;
111 }
112  
113 # }}}
114 # {{{ n2w3 handle digits before decimal
115  
116 sub n2w3 {
117 13 ▶ my $num = shift // return '';
Subroutine n2w3: called CC=6 SCAR=17.9
Pod documented
Condition50%1/2
shift() // (return "")
l!l
130
MC/DC0%0/1
shift() // (return "")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
118 13 my @words = ();
119 13 my $order = 0;
120 13 my $t;
121  
122 13 ▶ while($num =~ /^(.*?)([\d\D*]{1,3})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num =~ /^(.*?)([\d\D*]{1,3})$/u) 25 13 38
123 25 $num = $1;
124 25 ($t = $2) =~ s/\D//g;
125 25 ▶ $t = $t || 0;
Condition100%2/2
$t || 0
l!l
241
MC/DC100%1/1
$t || 0
$t
Truth table100%2/2
A
A= $t
Aresult
11
00
126 25 ▶ unshift @words, $Mult_words{$order} if $t > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $t > 0 17 8 25
127 25 unshift @words, n2w4($t, $order);
128 25 $order++;
129 }
130  
131 13 ▶ @words = ($Zero_word) if not join('',@words)=~/\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless join("", @words) =~ /\S/u 1 12 13
132 13 hmm___ "for the left part of decimal i get: @words\n";
133 13 return @words;
134 }
135  
136 # }}}
137 # {{{ n2w4 handle clusters of thousands
138  
139 sub n2w4 {
140 25 ▶ my $num = shift // return '';
Subroutine n2w4: called CC=24 SCAR=33.0
Pod documented
Condition50%1/2
shift() // (return "")
l!l
250
MC/DC0%0/1
shift() // (return "")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
141 25 my $order = shift;
142 25 my @words = ();
143  
144 25 my $n1 = $num % 10;
145 25 my $n2 = ($num % 100 - $n1) / 10;
146 25 my $n3 = ($num - $n2*10 - $n1) / 100;
147  
148 ($n3 == 0 && $n2 == 0 && $n1 > 0) && (((
149 $n1 == 1 && $order == 1) && (@words = ("se"))) ||
150 25 ▶ (@words = ($Digit_words{$n1}) ));
Branch: 1 branch
BranchTrueFalseTotal
$n3 == 0 and $n2 == 0 and $n1 > 0 and $n1 == 1 && $order == 1 && (@words = "se") || (@words = $Lingua::IND::Nums2Words::Digit_words{$n1}) 6 19 25
Condition93%15/16
$n3 == 0 && $n2 == 0 && $n1 > 0 && ($n1 == 1 && $order == 1 && (@words = 'se') || (@words = $Lingua::IND::Nums2Words::Digit_words{$n1}))
!ll
196
$n3 == 0 and $n2 == 0 and $n1 > 0
!ll&&!rl&&r
1186
$n3 == 0 and $n2 == 0
!ll&&!rl&&r
9214
$n1 == 1 and $order == 1 and @words = "se" or @words = $Lingua::IND::Nums2Words::Digit_words{$n1}
l!l
13
$n1 == 1 and $order == 1 and @words = "se"
!ll&&!rl&&r
501
$n1 == 1 and $order == 1
!ll&&!rl&&r
231
MC/DC0%0/7
$n3 == 0 && $n2 == 0 && $n1 > 0 && ($n1 == 1 && $order == 1 && (@words = 'se') || (@words = $Lingua::IND::Nums2Words::Digit_words{$n1}))
$n3 == 0 $n2 == 0 $n1 > 0 $n1 == 1 $order == 1 @words = "se" @words = $Lingua::IND::Nums2Words::Digit_words{$n1}
Truth table30%3/10
A and B and C && D and E and F or G
A= $n3 == 0B= $n2 == 0C= $n1 > 0D= $n1 == 1E= $order == 1F= @words = "se"G= @words = $Lingua::IND::Nums2Words::Digit_words{$n1}
ABCDEFGresult
0XXXXXX0
10XXXXX0
110XXXX0
1110XX00
11110X00
11111000
111111X1
1110XX11
11110X11
11111011
151  
152 $n3 == 1 and @words = ("seratus") or
153 25 ▶ $n3 > 1 and @words = ($Digit_words{$n3}, "ratus");
Branch: 1 branch
BranchTrueFalseTotal
$n3 == 1 and @words = "seratus" or $n3 > 1 and @words = ($Lingua::IND::Nums2Words::Digit_words{$n3}, "ratus") 22 3 25
Condition85%6/7
$n3 == 1 && (@words = 'seratus') || $n3 > 1 && (@words = ($Lingua::IND::Nums2Words::Digit_words{$n3}, 'ratus'))
l!l
322
$n3 == 1 and @words = "seratus"
!ll&&!rl&&r
2203
$n3 > 1 and @words = ($Lingua::IND::Nums2Words::Digit_words{$n3}, "ratus")
!ll
166
MC/DC0%0/4
$n3 == 1 && (@words = 'seratus') || $n3 > 1 && (@words = ($Lingua::IND::Nums2Words::Digit_words{$n3}, 'ratus'))
$n3 == 1 @words = "seratus" $n3 > 1 @words = ($Lingua::IND::Nums2Words::Digit_words{$n3}, "ratus")
Truth table14%1/7
A and B || C and D
A= $n3 == 1B= @words = "seratus"C= $n3 > 1D= @words = ($Lingua::IND::Nums2Words::Digit_words{$n3}, "ratus")
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
154  
155 $n2 == 1 and (
156 $n1 == 0 and push(@words, "sepuluh") or
157 $n1 == 1 and push(@words, "sebelas") or
158 25 ▶ push(@words, $Digit_words{$n1}, "belas")
Branch: 1 branch
BranchTrueFalseTotal
$n2 == 1 and $n1 == 0 && push(@words, "sepuluh") || $n1 == 1 && push(@words, "sebelas") || push(@words, $Lingua::IND::Nums2Words::Digit_words{$n1}, "belas") 1 24 25
Condition35%5/14
$n2 == 1 && ($n1 == 0 && push(@words, 'sepuluh') || $n1 == 1 && push(@words, 'sebelas') || push(@words, $Lingua::IND::Nums2Words::Digit_words{$n1}, 'belas'))
!ll
241
$n1 == 0 and push @words, "sepuluh" or $n1 == 1 and push @words, "sebelas" or push @words, $Lingua::IND::Nums2Words::Digit_words{$n1}, "belas"
l!l&&r!l&&!r
100
$n1 == 0 and push @words, "sepuluh" or $n1 == 1 and push @words, "sebelas"
l!l&&r!l&&!r
100
$n1 == 0 and push @words, "sepuluh"
!ll&&!rl&&r
001
$n1 == 1 and push @words, "sebelas"
!ll&&!rl&&r
000
MC/DC0%0/6
$n2 == 1 && ($n1 == 0 && push(@words, 'sepuluh') || $n1 == 1 && push(@words, 'sebelas') || push(@words, $Lingua::IND::Nums2Words::Digit_words{$n1}, 'belas'))
$n2 == 1 $n1 == 0 push @words, "sepuluh" $n1 == 1 push @words, "sebelas" push @words, $Lingua::IND::Nums2Words::Digit_words{$n1}, "belas"
Truth table0%0/12
A && B and C or D and E or F
A= $n2 == 1B= $n1 == 0C= push @words, "sepuluh"D= $n1 == 1E= push @words, "sebelas"F= push @words, $Lingua::IND::Nums2Words::Digit_words{$n1}, "belas"
ABCDEFresult
0XXXXX0
10X0X00
10X1000
1100X00
1101000
111XXX1
10X11X1
11011X1
10X0X11
10X1011
1100X11
1101011
159 );
160  
161 25 ▶ $n2 > 1 and do {
Branch: 1 branch
BranchTrueFalseTotal
$n2 > 1 and do { push @words, $Lingua::IND::Nums2Words::Digit_words{$n2}, "puluh"; push @words, $Lingua::IND::Nums2Words::Digit_words{$n1} if $n1 > 0 } 7 18 25
162 7 push @words, $Digit_words{$n2}, "puluh";
163 7 ▶ push @words, $Digit_words{$n1} if $n1 > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n1 > 0 6 1 7
164 };
165  
166 ($n3 > 0 && $n2 == 0 && $n1 > 0) &&
167 25 ▶ push @words, $Digit_words{$n1} ;
Branch: 1 branch
BranchTrueFalseTotal
$n3 > 0 and $n2 == 0 and $n1 > 0 and push @words, $Lingua::IND::Nums2Words::Digit_words{$n1} 0 25 25
Condition83%5/6
$n3 > 0 and $n2 == 0 and $n1 > 0
!ll&&!rl&&r
2230
$n3 > 0 and $n2 == 0
!ll&&!rl&&r
1663
MC/DC0%0/3
$n3 > 0 and $n2 == 0 and $n1 > 0
$n3 > 0 $n2 == 0 $n1 > 0
Truth table75%3/4
A and B and C
A= $n3 > 0B= $n2 == 0C= $n1 > 0
ABCresult
0XX0
10X0
1100
1111
168  
169 25 ▶ ($n3 != 0 || $n2 != 0 || $n1 != 0) &&
Branch: 1 branch
BranchTrueFalseTotal
$n3 != 0 || $n2 != 0 || $n1 != 0 and return @words 17 8 25
Condition100%6/6
$n3 != 0 or $n2 != 0 or $n1 != 0
l!l&&r!l&&!r
1168
$n3 != 0 or $n2 != 0
l!l&&r!l&&!r
9214
MC/DC100%3/3
$n3 != 0 or $n2 != 0 or $n1 != 0
$n3 != 0 $n2 != 0 $n1 != 0
Truth table100%4/4
A or B or C
A= $n3 != 0B= $n2 != 0C= $n1 != 0
ABCresult
1XX1
01X1
0011
0000
170 return @words;
171 }
172  
173 # }}}
174 # {{{ n2w5 handle digits after decimal
175 sub n2w5 {
176 6 ▶ my $num = shift // return '';
Subroutine n2w5: called CC=6 SCAR=18.0
Pod documented
Condition100%2/2
shift() // (return "")
l!l
51
MC/DC100%1/1
shift() // (return "")
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
177  
178 5 ▶ return $Zero_word if $num >= 10 ** 15; # not quadrillion and more
Branch: 1 branch
BranchTrueFalseTotal
if $num >= '1000000000000000' 1 4 5
179  
180 4 my @words = ();
181 4 my $i;
182 my $t;
183  
184 4 ▶ for( $i=0 ; $i<=length($num)-1 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= length($num) - 1) 22 4 26
185 22 $t = substr($num, $i, 1);
186 exists $Digit_words{$t} and
187 22 ▶ push @words, $Digit_words{$t};
Branch: 1 branch
BranchTrueFalseTotal
exists $Lingua::IND::Nums2Words::Digit_words{$t} and push @words, $Lingua::IND::Nums2Words::Digit_words{$t} 22 0 22
188 }
189  
190 4 ▶ @words = ($Zero_word) if not join('',@words)=~/\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless join("", @words) =~ /\S/u 0 4 4
191 4 return @words;
192 }
193  
194 # }}}
195 # {{{ join_it join array of words, also join (se, ratus) -> seratus, etc.
196 sub join_it {
197 21 ▶ my @a = @_;
Subroutine join_it: called CC=5 SCAR=16.1
Pod documented
198 21 my $words = '';
199 21 my $w;
200  
201 21 ▶ while(defined( $w = shift @a)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($w = shift @a)) 95 21 116
202 95 $words .= $w;
203 95 ▶ $words .= ' ' unless not length $w or $w eq 'se' or not @a;
Branch: 1 branch
BranchTrueFalseTotal
unless not length $w or $w eq "se" or not @a 68 27 95
Condition100%6/6
not length $w or $w eq "se" or not @a
l!l&&r!l&&!r
20768
not length $w or $w eq "se"
l!l&&r!l&&!r
19175
MC/DC100%3/3
not length $w or $w eq "se" or not @a
not length $w $w eq "se" not @a
Truth table100%4/4
not(A) or B or not(C)
A= not length $wB= $w eq "se"C= not @a
ABCresult
1XX1
01X1
0011
0000
204 }
205 21 return $words;
206 }
207  
208 # }}}
209  
210 1 1;
211 __END__
↑ Summary