blib/lib/Lingua/IND/Num2Word.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 110 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 246 CC 9 SCAR 17.9
CC
9
Cov
0%
n2w3
12.0
n2w5
12.0
hmm___
6.0
CRAP 6.0
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1) -*-
2  
3 package Lingua::IND::Num2Word;
4 # ABSTRACT: Number 2 word conversion in IND.
5  
6 use 5.16.0;
7 use utf8;
8 use warnings;
9  
10 # {{{ use block
11  
12 use Export::Attrs;
13  
14 # }}}
15 # {{{ variables declaration
16 our $VERSION = '0.2603270';
17  
18 our $Dec_char = ".";
19 our $Neg_word = "negatif";
20 our $Dec_word = "koma";
21 our $Exp_word = "dikali sepuluh pangkat";
22 our $Zero_word = "nol";
23  
24 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 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 num2ind_cardinal :Export { goto &nums2words }
51  
52 sub nums2words :Export {
53 my @a = @_;
54 return join_it(n2w1(@a));
55 }
56  
57 # }}}
58 # {{{ nums2words_simple
59  
60 sub nums2words_simple :Export {
61 my @a = @_;
62 return join_it(n2w5(@a));
63 }
64  
65 # }}}
66  
67 ### private subs
68  
69 # for debugging
70 our $DEBUG = 0;
71 sub hmm___ { my @a = @_;print "(", (caller 1)[3], ") Hmm, ", @a if $DEBUG; return; }
72 # {{{ n2w1 handle scientific notation
73 sub n2w1 {
74 my $num = shift // return '';
75  
76 return $Zero_word if $num >= 10 ** 15; # not quadrillion and more
77  
78 my @words;
79  
80 $num =~ /^(.+)[Ee](.+)$/ and
81 @words = (n2w2($1), $Exp_word, n2w2($2)) or
82 @words = n2w2($num);
83  
84 return @words;
85 }
86  
87 # }}}
88 # {{{ n2w2 handle negative sign and decimal
89  
90 sub n2w2 {
91 my $num = shift // return '';
92 my $is_neg;
93 my @words = ();
94  
95 # negative
96 $num < 0 and $is_neg++;
97 $num =~ s/^[\s\t]*[+-]*(.*)/$1/;
98  
99 # decimal
100 $num =~ /^(.+)\Q$Dec_char\E(.+)$/o and
101 @words = (n2w3($1), $Dec_word, n2w5($2)) or
102  
103 $num =~ /^\Q$Dec_char\E(.+)$/o and
104 @words = ($Digit_words{0}, $Dec_word, n2w5($1)) or
105  
106 $num =~ /^(.+)(?:\Q$Dec_char\E)?$/o and
107 @words = n2w3($1);
108  
109 $is_neg and
110 unshift @words, $Neg_word;
111  
112 return @words;
113 }
114  
115 # }}}
116 # {{{ n2w3 handle digits before decimal
117  
118 sub n2w3 {
119 my $num = shift // return '';
120 my @words = ();
121 my $order = 0;
122 my $t;
123  
124 while($num =~ /^(.*?)([\d\D*]{1,3})$/) {
125 $num = $1;
126 ($t = $2) =~ s/\D//g;
127 $t = $t || 0;
128 unshift @words, $Mult_words{$order} if $t > 0;
129 unshift @words, n2w4($t, $order);
130 $order++;
131 }
132  
133 @words = ($Zero_word) if not join('',@words)=~/\S/;
134 hmm___ "for the left part of decimal i get: @words\n";
135 return @words;
136 }
137  
138 # }}}
139 # {{{ n2w4 handle clusters of thousands
140  
141 sub n2w4 {
142 my $num = shift // return '';
143 my $order = shift;
144 my @words = ();
145  
146 my $n1 = $num % 10;
147 my $n2 = ($num % 100 - $n1) / 10;
148 my $n3 = ($num - $n2*10 - $n1) / 100;
149  
150 ($n3 == 0 && $n2 == 0 && $n1 > 0) && (((
151 $n1 == 1 && $order == 1) && (@words = ("se"))) ||
152 (@words = ($Digit_words{$n1}) ));
153  
154 $n3 == 1 and @words = ("seratus") or
155 $n3 > 1 and @words = ($Digit_words{$n3}, "ratus");
156  
157 $n2 == 1 and (
158 $n1 == 0 and push(@words, "sepuluh") or
159 $n1 == 1 and push(@words, "sebelas") or
160 push(@words, $Digit_words{$n1}, "belas")
161 );
162  
163 $n2 > 1 and do {
164 push @words, $Digit_words{$n2}, "puluh";
165 push @words, $Digit_words{$n1} if $n1 > 0;
166 };
167  
168 ($n3 > 0 && $n2 == 0 && $n1 > 0) &&
169 push @words, $Digit_words{$n1} ;
170  
171 ($n3 != 0 || $n2 != 0 || $n1 != 0) &&
172 return @words;
173 }
174  
175 # }}}
176 # {{{ n2w5 handle digits after decimal
177 sub n2w5 {
178 my $num = shift // return '';
179  
180 return $Zero_word if $num >= 10 ** 15; # not quadrillion and more
181  
182 my @words = ();
183 my $i;
184 my $t;
185  
186 for( $i=0 ; $i<=length($num)-1 ; $i++ ) {
187 $t = substr($num, $i, 1);
188 exists $Digit_words{$t} and
189 push @words, $Digit_words{$t};
190 }
191  
192 @words = ($Zero_word) if not join('',@words)=~/\S/;
193 return @words;
194 }
195  
196 # }}}
197 # {{{ join_it join array of words, also join (se, ratus) -> seratus, etc.
198 sub join_it {
199 my @a = @_;
200 my $words = '';
201 my $w;
202  
203 while(defined( $w = shift @a)) {
204 $words .= $w;
205 $words .= ' ' unless not length $w or $w eq 'se' or not @a;
206 }
207 return $words;
208 }
209  
210 # }}}
211  
212  
213 # {{{ capabilities declare supported features
214  
215 sub capabilities {
216 return {
217 cardinal => 1,
218 ordinal => 0,
219 };
220 }
221  
222 # }}}
223 1;
224 __END__