blib/lib/Lingua/IND/Words2Nums.pm

filter: Statementstmt 70.9% 100 / 141 Branchbran 53.8% 28 / 52 Conditioncond 35.2% 12 / 34 MC/DCmcdc 31.5% 6 / 19 Subroutinesub 82.3% 14 / 17 Podpod 100% 10 / 10 totaltotal 62.2% 170 / 273 CC 41 SCAR 25.7
CC
41
Cov
62%
w2n5
42.0
ordinal2cardinal
20.0
w2n4
15.5
CRAP 13.1
↑ Summary
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1) -*-
2  
3 package Lingua::IND::Words2Nums;
4 # ABSTRACT: convert Indonesian verbage to number.
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.2603300';
17  
18 1 our %Digits = (
19 nol => 0, kosong => 0,
20 se => 1, satu => 1,
21 dua => 2,
22 tiga => 3,
23 empat => 4,
24 lima => 5,
25 enam => 6,
26 tujuh => 7,
27 delapan => 8,
28 sembilan => 9
29 );
30  
31 1 our %Mults = (
32 puluh => 1e1,
33 ratus => 1e2,
34 ribu => 1e3,
35 juta => 1e6,
36 milyar => 1e9, milyard => 1e9, miliar => 1e9, miliard => 1e9,
37 triliun => 1e12, trilyun => 1e12
38 );
39  
40 1 our %Words = (
41 %Digits,
42 %Mults,
43 belas => 0
44 );
45  
46 1 our $Neg_pat = '(?:negatif|min|minus)';
47 1 our $Exp_pat = '(?:(?:di)?kali(?:kan)? sepuluh pangkat)';
48 1 our $Dec_pat = '(?:koma|titik)';
49  
50 # }}}
51  
52 ### public subs
53 # {{{ words2nums
54  
55 6 ▶sub words2nums :Export {
Subroutine words2nums: called CC=1 SCAR=0.0
Pod documented
56 6 my @a = @_;
57 6 return w2n1(@a);
58 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 # }}}
61 # {{{ words2nums
62  
63 0 ▶sub words2nums_simple :Export {
Subroutine words2nums_simple: not called CC=1 SCAR=6.9
Pod documented
64 0 my @a = @_;
65 0 return w2n5(@a);
66 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 # }}}
69  
70 ### private subs
71  
72 # for debugging
73 1 our $DEBUG = 0;
74 66 ▶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::Words2Nums::DEBUG 0 66 66
75  
76 # {{{ handle exponential
77 6 ▶sub w2n1 {
Subroutine w2n1: called CC=6 SCAR=23.5
Pod documented
78 6 ▶ my $words = shift // '';
Condition100%2/2
shift() // ""
l!l
42
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
79 6 $words = lc $words;
80 6 my ($num1, $num2);
81  
82 6 ▶ if( $words =~ /(.+)\b$Exp_pat\b(.+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($words =~ /(.+)\b$Lingua::IND::Words2Nums::Exp_pat\b(.+)/u) { } 0 6 6
83 0 hmm___ "it's an exponent.\n";
84 0 $num1 = w2n2($1);
85 0 $num2 = w2n2($2);
86 0 hmm___ "\$num1 is $num1, \$num2 is $num2\n";
87 0 ▶ not defined $num1 or not defined $num2 and return;
Branch: 1 branch
BranchTrueFalseTotal
defined $num1 and defined $num2 || (return) 0 0 0
Condition0%0/5
defined $num1 && (defined $num2 || (return))
!ll&&!rl&&r
000
defined $num2 or return
l!l
00
MC/DC0%0/2
defined $num1 && (defined $num2 || (return))
defined $num1 defined $num2
Truth table0%0/3
A && B
A= defined $num1B= defined $num2
ABresult
0X0
100
111
88 0 return $num1 * 10 ** $num2
89 } else {
90 6 hmm___ "not an exponent.\n";
91 6 $num1 = w2n2($words);
92 6 ▶ not defined $num1 and return;
Branch: 1 branch
BranchTrueFalseTotal
defined $num1 or return 4 2 6
93 4 hmm___ "\$num1 = $num1\n";
94 4 return $num1
95 }
96 }
97  
98 # }}}
99 # {{{ handle negative
100  
101 6 ▶sub w2n2 {
Subroutine w2n2: called CC=4 SCAR=16.5
Pod documented
102 6 my $words = lc shift;
103 6 my $num1;
104  
105 6 ▶ if( $words =~ /^[\s\t]*$Neg_pat\b(.+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($words =~ /^[\s\t]*$Lingua::IND::Words2Nums::Neg_pat\b(.+)/u) { } 0 6 6
106 0 hmm___ "it's negative.\n";
107 0 $num1 = -w2n3($1);
108 0 ▶ not defined $num1 and return;
Branch: 1 branch
BranchTrueFalseTotal
defined $num1 or return 0 0 0
109 0 hmm___ "\$num1 = $num1\n";
110 0 return $num1
111 } else {
112 6 hmm___ "it's not negative.\n";
113 6 $num1 = w2n3($words);
114 6 ▶ not defined $num1 and return;
Branch: 1 branch
BranchTrueFalseTotal
defined $num1 or return 4 2 6
115 4 hmm___ "\$num1 = $num1\n";
116 4 return $num1
117 }
118 }
119  
120 # }}}
121 # {{{ handle decimal
122  
123 6 ▶sub w2n3 {
Subroutine w2n3: called CC=5 SCAR=22.4
Pod documented
124 6 my $words = lc shift;
125 6 my ($num1, $num2);
126  
127 6 ▶ if( $words =~ /(.+)\b$Dec_pat\b(.+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($words =~ /(.+)\b$Lingua::IND::Words2Nums::Dec_pat\b(.+)/u) { } 0 6 6
128 0 hmm___ "it has decimals.\n";
129 0 $num1 = w2n4($1);
130 0 $num2 = w2n5($2);
131 0 ▶ not defined $num1 or not defined $num2 and return;
Branch: 1 branch
BranchTrueFalseTotal
defined $num1 and defined $num2 || (return) 0 0 0
Condition0%0/5
defined $num1 && (defined $num2 || (return))
!ll&&!rl&&r
000
defined $num2 or return
l!l
00
MC/DC0%0/2
defined $num1 && (defined $num2 || (return))
defined $num1 defined $num2
Truth table0%0/3
A && B
A= defined $num1B= defined $num2
ABresult
0X0
100
111
132 0 hmm___ "\$num1 is $num1, \$num2 is $num2\n";
133 0 return $num1 + "0.".$num2
134 } else {
135 6 hmm___ "it's an integer.\n";
136 6 $num1 = w2n4($words);
137 6 ▶ not defined $num1 and return;
Branch: 1 branch
BranchTrueFalseTotal
defined $num1 or return 4 2 6
138 4 hmm___ "\$num1 is $num1\n";
139 4 return $num1
140 }
141 }
142  
143 # }}}
144 # {{{ handle words before decimal (e.g, 'seratus dua puluh tiga', ...)
145  
146 6 ▶sub w2n4 {
Subroutine w2n4: called CC=14 SCAR=27.4
Pod documented
147 6 my @words = &split_it( lc shift );
148 6 my ($num, $mult);
149 6 my $seen_digits = 0;
150 6 my ($aa, $subtot, $tot);
151 6 my @nums = ();
152  
153 6 ▶ (defined $words[0] and $words[0] eq 'ERR') and return;
Branch: 1 branch
BranchTrueFalseTotal
defined $words[0] and $words[0] eq "ERR" and return 2 4 6
Condition100%3/3
defined $words[0] and $words[0] eq "ERR"
!ll&&!rl&&r
222
MC/DC100%2/2
defined $words[0] and $words[0] eq "ERR"
defined $words[0] $words[0] eq "ERR"
Truth table100%3/3
A and B
A= defined $words[0]B= $words[0] eq "ERR"
ABresult
0X0
100
111
154 4 hmm___ "the words are @words.\n";
155  
156 4 for my $w (@words) {
157 14 ▶ if( defined $Digits{$w} ) { # digits (satuan)
Branch: 2 branches
BranchTrueFalseTotal
if (defined $Lingua::IND::Words2Nums::Digits{$w}) { } 8 6 14
elsif ($w eq 'belas') { } 0 6 6
158 8 hmm___ "saw a digit: $w.\n";
159 0 $seen_digits and do { push @nums, ((10 * (pop @nums)) + $Digits{$w}) }
160 8 ▶ or do { push @nums, $Digits{$w} ; $seen_digits = 1 }
Branch: 1 branch
BranchTrueFalseTotal
$seen_digits and do { push @nums, 10 * pop(@nums) + $Lingua::IND::Words2Nums::Digits{$w} } or do { push @nums, $Lingua::IND::Words2Nums::Digits{$w}; $seen_digits = 1 } 0 8 8
Condition33%1/3
$seen_digits and do { push @nums, 10 * pop(@nums) + $Lingua::IND::Words2Nums::Digits{$w} }
!ll&&!rl&&r
800
MC/DC0%0/2
$seen_digits and do { push @nums, 10 * pop(@nums) + $Lingua::IND::Words2Nums::Digits{$w} }
$seen_digits do { push @nums, 10 * pop(@nums) + $Lingua::IND::Words2Nums::Digits{$w} }
Truth table33%1/3
A and B
A= $seen_digitsB= do { push @nums, 10 * pop(@nums) + $Lingua::IND::Words2Nums::Digits{$w} }
ABresult
0X0
100
111
161 }
162  
163 elsif( $w eq 'belas' ) { # special case, teens (belasan)
164 0 hmm___ "saw a teen: $w.\n";
165 0 ▶ return unless $seen_digits ; # (salah penulisan belasan)
Branch: 1 branch
BranchTrueFalseTotal
unless $seen_digits 0 0 0
166 0 push @nums, 10 + pop @nums;
167 0 $seen_digits = 0;
168 }
169  
170 else{ # must be a multiplier
171 6 hmm___ "saw a multiplier: $w.\n";
172 6 ▶ return unless @nums ; # (salah penulisan puluhan/pengali)
Branch: 1 branch
BranchTrueFalseTotal
unless @nums 0 6 6
173  
174 6 $a = 0 ; $subtot = 0;
175 10 do { $aa = pop @nums ; $subtot += $aa }
176 6 ▶ until ( $aa > $Mults{$w} || !@nums );
Branch: 1 branch
BranchTrueFalseTotal
($aa > $Lingua::IND::Words2Nums::Mults{$w} or not @nums) or do { do { $aa = pop @nums; $subtot += $aa } } 6 4 10
Condition100%3/3
$aa > $Lingua::IND::Words2Nums::Mults{$w} or not @nums
l!l&&r!l&&!r
424
MC/DC100%2/2
$aa > $Lingua::IND::Words2Nums::Mults{$w} or not @nums
$aa > $Lingua::IND::Words2Nums::Mults{$w} not @nums
Truth table100%3/3
A or not(B)
A= $aa > $Lingua::IND::Words2Nums::Mults{$w}B= not @nums
ABresult
1X1
011
000
177  
178 6 ▶ if( $aa > $Mults{$w} ) { push @nums, $aa; $subtot -= $aa }
Branch: 1 branch
BranchTrueFalseTotal
if ($aa > $Lingua::IND::Words2Nums::Mults{$w}) 4 2 6
179 6 push @nums, $Mults{$w}*$subtot;
180 6 $seen_digits = 0;
181 }
182 }
183  
184 # calculate total
185 4 $tot = 0;
186 4 ▶ while( @nums ){ $tot += shift @nums }
Branch: 1 branch
BranchTrueFalseTotal
if (@nums) 8 4 12
187  
188 4 return $tot;
189 }
190  
191 # {{{ handle words after decimal (simple with no 'belas', 'puluh', 'ratus', ...)
192 0 ▶sub w2n5 {
Subroutine w2n5: not called CC=6 SCAR=37.4
Pod documented
193 0 ▶ my $words = shift // '';
Condition0%0/2
shift() // ""
l!l
00
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
194 0 my @words = &split_it( lc $words );
195 0 my ($num, $mult);
196  
197 0 ▶ (defined $words[0] and $words[0] eq 'ERR') and return;
Branch: 1 branch
BranchTrueFalseTotal
defined $words[0] and $words[0] eq "ERR" and return 0 0 0
Condition0%0/3
defined $words[0] and $words[0] eq "ERR"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $words[0] and $words[0] eq "ERR"
defined $words[0] $words[0] eq "ERR"
Truth table0%0/3
A and B
A= defined $words[0]B= $words[0] eq "ERR"
ABresult
0X0
100
111
198  
199 0 $num = 0;
200 0 $mult = 1;
201 0 for my $w (reverse @words) {
202 0 ▶ not defined $Digits{$w} and return;
Branch: 1 branch
BranchTrueFalseTotal
defined $Lingua::IND::Words2Nums::Digits{$w} or return 0 0 0
203 0 $num += $Digits{$w}*$mult;
204 0 $mult *= 10;
205 }
206  
207 0 return $num;
208 }
209  
210 # }}}
211 # {{{ split string into array of words. also splits 'sepuluh' -> (se, puluh), 'tigabelas' -> (tiga, belas), etc.
212  
213 6 ▶sub split_it {
Subroutine split_it: called CC=7 SCAR=20.3
Pod documented
214 6 my $words = lc shift;
215 6 my @words = ();
216  
217 6 for my $w ($words =~ /\b(\w+)\b/g) {
218 14 hmm___ "saw $w.\n";
219 14 ▶ if( $w =~ /^se(.+)$/ and defined $Words{$1} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($w =~ /^se(.+)$/u and defined $Lingua::IND::Words2Nums::Words{$1}) { } 2 12 14
elsif ($w =~ /^(.+)(belas|puluh|ratus|ribu|juta|mil[iy]ard?|tril[iy]un)$/u and defined $Lingua::IND::Words2Nums::Words{$1}) { } 0 12 12
elsif (defined $Lingua::IND::Words2Nums::Words{$w}) { } 10 2 12
Condition66%2/3
$w =~ /^se(.+)$/u and defined $Lingua::IND::Words2Nums::Words{$1}
!ll&&!rl&&r
1202
MC/DC50%1/2
$w =~ /^se(.+)$/u and defined $Lingua::IND::Words2Nums::Words{$1}
$w =~ /^se(.+)$/u defined $Lingua::IND::Words2Nums::Words{$1}
Truth table66%2/3
A and B
A= $w =~ /^se(.+)$/uB= defined $Lingua::IND::Words2Nums::Words{$1}
ABresult
0X0
100
111
220 2 hmm___ "i should split $w.\n";
221 2 push @words, 'se', $1 }
222 ▶ elsif( $w =~ /^(.+)(belas|puluh|ratus|ribu|juta|mil[iy]ard?|tril[iy]un)$/ and defined $Words{$1} ) {
Condition33%1/3
$w =~ /^(.+)(belas|puluh|ratus|ribu|juta|mil[iy]ard?|tril[iy]un)$/u and defined $Lingua::IND::Words2Nums::Words{$1}
!ll&&!rl&&r
1200
MC/DC0%0/2
$w =~ /^(.+)(belas|puluh|ratus|ribu|juta|mil[iy]ard?|tril[iy]un)$/u and defined $Lingua::IND::Words2Nums::Words{$1}
$w =~ /^(.+)(belas|puluh|ratus|ribu|juta|mil[iy]ard?|tril[iy]un)$/u defined $Lingua::IND::Words2Nums::Words{$1}
Truth table33%1/3
A and B
A= $w =~ /^(.+)(belas|puluh|ratus|ribu|juta|mil[iy]ard?|tril[iy]un)$/uB= defined $Lingua::IND::Words2Nums::Words{$1}
ABresult
0X0
100
111
223 0 hmm___ "i should split $w.\n";
224 0 push @words, $1, $2 }
225 elsif( defined $Words{$w} ) {
226 10 push @words, $w }
227 else {
228 2 hmm___ "i don't recognize $w.\n";
229 2 unshift @words, 'ERR';
230 2 last }
231 }
232  
233 6 return @words;
234 }
235  
236 # }}}
237 # {{{ ordinal2cardinal convert ordinal text to cardinal text
238  
239 0 ▶sub ordinal2cardinal :Export {
Subroutine ordinal2cardinal: not called CC=4 SCAR=30.0
Pod documented
240 0 ▶ my $input = shift // return;
Condition0%0/2
shift() // (return)
l!l
00
MC/DC0%0/1
shift() // (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
241  
242 # Indonesian ordinals: prefix "ke" to cardinal.
243 # Special: "pertama" (1st) → "satu"
244 0 ▶ return 'satu' if $input eq 'pertama';
Branch: 1 branch
BranchTrueFalseTotal
if $input eq "pertama" 0 0 0
245  
246 # Regular: strip "ke" prefix (e.g. "kedua" → "dua", "ketiga" → "tiga")
247 0 ▶ $input =~ s{\Ake}{}xms and return $input;
Branch: 1 branch
BranchTrueFalseTotal
$input =~ s/\Ake//msux and return $input 0 0 0
248  
249 0 return; # not an ordinal
250 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
251  
252 # }}}
253  
254 1 1;
255 __END__
↑ Summary