blib/lib/Lingua/ARA/Num2Word.pm

filter: Statementstmt 56.8% 54 / 95 Branchbran 23.8% 10 / 42 Conditioncond 22.2% 4 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 90.0% 9 / 10 Podpod 50.0% 1 / 2 totaltotal 44.0% 78 / 177 CC 29 SCAR 52.4
CC
29
Cov
44%
_convert
308.8
num2ara_cardinal
7.4
capabilities
2.0
CRAP 188.5
↑ Summary
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*-
2 package Lingua::ARA::Num2Word;
3 # ABSTRACT: Number to word conversion in Arabic
4  
5 1 ▶use 5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # {{{ use block
10  
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Export::Attrs;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # }}}
16 # {{{ variable declarations
17  
18 1 my Readonly::Scalar $COPY = 'Copyright (c) PetaMem, s.r.o. 2002-present';
19 1 our $VERSION = '0.2603270';
20  
21 # }}}
22  
23 # {{{ num2ara_cardinal convert number to text
24  
25 sub num2ara_cardinal :Export {
26 2 ▶ my $positive = shift;
Subroutine num2ara_cardinal: called CC=7 SCAR=20.1
Pod documented
27  
28 2 ▶ croak 'You should specify a number from interval [0, 999_999_999]'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $positive && $positive =~ /\A\d+\z/msux or $positive < 0 or $positive > 999999999 0 2 2
Condition33%3/9
not defined $positive && $positive =~ /\A\d+\z/msux or $positive < 0 or $positive > 999999999
l!l&&r!l&&!r
002
not defined $positive && $positive =~ /\A\d+\z/msux or $positive < 0
l!l&&r!l&&!r
002
defined $positive && $positive =~ /\A\d+\z/msux
!ll&&!rl&&r
002
MC/DC0%0/4
not defined $positive && $positive =~ /\A\d+\z/msux or $positive < 0 or $positive > 999999999
defined $positive $positive =~ /\A\d+\z/msux $positive < 0 $positive > 999999999
Truth table20%1/5
not(A && B) or C or D
A= defined $positiveB= $positive =~ /\A\d+\z/msuxC= $positive < 0D= $positive > 999999999
ABCDresult
0XXX1
10XX1
111X1
11011
11000
29 if !defined $positive
30 || $positive !~ m{\A\d+\z}xms
31 || $positive < 0
32 || $positive > 999_999_999;
33  
34 # Units (masculine counting forms)
35 2 my @ones = (
36 "صفر", # 0 صفر
37 "واحد", # 1 واحد
38 "اثنان", # 2 اثنان
39 "ثلاثة", # 3 ثلاثة
40 "أربعة", # 4 أربعة
41 "خمسة", # 5 خمسة
42 "ستة", # 6 ستة
43 "سبعة", # 7 سبعة
44 "ثمانية", # 8 ثمانية
45 "تسعة", # 9 تسعة
46 );
47  
48 # Tens
49 2 my @tens_word = (
50 q{}, # 0 (unused)
51 "عشرة", # 10 عشرة
52 "عشرون", # 20 عشرون
53 "ثلاثون", # 30 ثلاثون
54 "أربعون", # 40 أربعون
55 "خمسون", # 50 خمسون
56 "ستون", # 60 ستون
57 "سبعون", # 70 سبعون
58 "ثمانون", # 80 ثمانون
59 "تسعون", # 90 تسعون
60 );
61  
62 # Special teens (11-19)
63 2 my @teens;
64 2 $teens[11] = "أحد عشر"; # أحد عشر
65 2 $teens[12] = "اثنا عشر"; # اثنا عشر
66 # 13-19: unit + عشر
67 2 my $ashar = "عشر"; # عشر
68 2 for my $i (13..19) {
69 14 $teens[$i] = "$ones[$i - 10] $ashar";
70 }
71  
72 # Connector
73 2 my $wa = " و"; # و (and)
74  
75 # Hundred forms
76 2 my $mi_a = "مئة"; # مئة
77 2 my $mi_atan = "مئتان"; # مئتان
78  
79 # Thousand forms
80 2 my $alf = "ألف"; # ألف
81 2 my $alfan = "ألفان"; # ألفان
82 2 my $alaf = "آلاف"; # آلاف
83  
84 # Million forms
85 2 my $milyun = "مليون"; # مليون
86 2 my $milyunan = "مليونان"; # مليونان
87 2 my $malayin = "ملايين"; # ملايين
88  
89 2 ▶ return $ones[0] if $positive == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $positive == 0 1 1 2
90  
91 1 return _convert($positive, \@ones, \@tens_word, \@teens,
92 $wa, $mi_a, $mi_atan,
93 $alf, $alfan, $alaf,
94 $milyun, $milyunan, $malayin);
95 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
96  
97 # }}}
98 # {{{ _convert recursive number-to-word engine
99  
100 sub _convert {
101 1 ▶ my ($n, $ones, $tens_word, $teens,
Subroutine _convert: called CC=23 SCAR=57.3
102 $wa, $mi_a, $mi_atan,
103 $alf, $alfan, $alaf,
104 $milyun, $milyunan, $malayin) = @_;
105  
106 1 my @params = ($ones, $tens_word, $teens,
107 $wa, $mi_a, $mi_atan,
108 $alf, $alfan, $alaf,
109 $milyun, $milyunan, $malayin);
110  
111 1 ▶ return q{} if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 1 1
112  
113 # --- Millions ---
114 1 ▶ if ($n >= 1_000_000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 1000000) 0 1 1
115 0 my $millions = int($n / 1_000_000);
116 0 my $remain = $n % 1_000_000;
117  
118 0 my $out;
119 0 ▶ if ($millions == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($millions == 1) { } 0 0 0
elsif ($millions == 2) { } 0 0 0
elsif ($millions >= 3 and $millions <= 10) { } 0 0 0
120 0 $out = $milyun;
121 }
122 elsif ($millions == 2) {
123 0 $out = $milyunan;
124 }
125 ▶ elsif ($millions >= 3 && $millions <= 10) {
Condition0%0/3
$millions >= 3 and $millions <= 10
!ll&&!rl&&r
000
MC/DC0%0/2
$millions >= 3 and $millions <= 10
$millions >= 3 $millions <= 10
Truth table0%0/3
A and B
A= $millions >= 3B= $millions <= 10
ABresult
0X0
100
111
126 0 $out = _convert($millions, @params) . ' ' . $malayin;
127 }
128 else {
129 # 11+ millions: number + مليون (singular)
130 0 $out = _convert($millions, @params) . ' ' . $milyun;
131 }
132  
133 0 ▶ if ($remain) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remain) 0 0 0
134 0 $out .= "$wa" . _convert($remain, @params);
135 }
136 0 return $out;
137 }
138  
139 # --- Thousands ---
140 1 ▶ if ($n >= 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 1000) 0 1 1
141 0 my $thousands = int($n / 1000);
142 0 my $remain = $n % 1000;
143  
144 0 my $out;
145 0 ▶ if ($thousands == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($thousands == 1) { } 0 0 0
elsif ($thousands == 2) { } 0 0 0
elsif ($thousands >= 3 and $thousands <= 10) { } 0 0 0
146 0 $out = $alf;
147 }
148 elsif ($thousands == 2) {
149 0 $out = $alfan;
150 }
151 ▶ elsif ($thousands >= 3 && $thousands <= 10) {
Condition0%0/3
$thousands >= 3 and $thousands <= 10
!ll&&!rl&&r
000
MC/DC0%0/2
$thousands >= 3 and $thousands <= 10
$thousands >= 3 $thousands <= 10
Truth table0%0/3
A and B
A= $thousands >= 3B= $thousands <= 10
ABresult
0X0
100
111
152 0 $out = _convert($thousands, @params) . ' ' . $alaf;
153 }
154 else {
155 # 11+ thousands: number + ألف (singular)
156 0 $out = _convert($thousands, @params) . ' ' . $alf;
157 }
158  
159 0 ▶ if ($remain) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remain) 0 0 0
160 0 $out .= "$wa" . _convert($remain, @params);
161 }
162 0 return $out;
163 }
164  
165 # --- Hundreds ---
166 1 ▶ if ($n >= 100) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 100) 0 1 1
167 0 my $hundreds = int($n / 100);
168 0 my $remain = $n % 100;
169  
170 0 my $out;
171 0 ▶ if ($hundreds == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hundreds == 1) { } 0 0 0
elsif ($hundreds == 2) { } 0 0 0
172 0 $out = $mi_a;
173 }
174 elsif ($hundreds == 2) {
175 0 $out = $mi_atan;
176 }
177 else {
178 # 300-900: unit form (without taa marbuTa for 3-9) + مئة
179 # Use shortened forms for hundreds: ثلاث، أربع، خمس، ست، سبع، ثمان، تسع
180 0 my @hund_prefix = (
181 q{}, # 0
182 q{}, # 1
183 q{}, # 2
184 "ثلاث", # 3 ثلاث
185 "أربع", # 4 أربع
186 "خمس", # 5 خمس
187 "ست", # 6 ست
188 "سبع", # 7 سبع
189 "ثمان", # 8 ثمان
190 "تسع", # 9 تسع
191 );
192 0 $out = $hund_prefix[$hundreds] . $mi_a;
193 }
194  
195 0 ▶ if ($remain) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remain) 0 0 0
196 0 $out .= "$wa" . _convert($remain, @params);
197 }
198 0 return $out;
199 }
200  
201 # --- Teens (11-19) ---
202 1 ▶ if ($n >= 11 && $n <= 19) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 11 and $n <= 19) 0 1 1
Condition33%1/3
$n >= 11 and $n <= 19
!ll&&!rl&&r
100
MC/DC0%0/2
$n >= 11 and $n <= 19
$n >= 11 $n <= 19
Truth table33%1/3
A and B
A= $n >= 11B= $n <= 19
ABresult
0X0
100
111
203 0 return $teens->[$n];
204 }
205  
206 # --- Ten ---
207 1 ▶ if ($n == 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 10) 0 1 1
208 0 return $tens_word->[1];
209 }
210  
211 # --- Tens with units (21-99) ---
212 1 ▶ if ($n >= 20) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 20) 0 1 1
213 0 my $ten_idx = int($n / 10);
214 0 my $unit = $n % 10;
215  
216 0 ▶ if ($unit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit) 0 0 0
217 0 return $ones->[$unit] . "$wa" . $tens_word->[$ten_idx];
218 }
219 0 return $tens_word->[$ten_idx];
220 }
221  
222 # --- Units (1-9) ---
223 1 return $ones->[$n];
224 }
225  
226 # }}}
227  
228  
229 # {{{ capabilities declare supported features
230  
231 sub capabilities {
232 return {
233 0 ▶ cardinal => 1,
Subroutine capabilities: not called CC=1 SCAR=6.9
Pod undocumented
234 ordinal => 0,
235 };
236 }
237  
238 # }}}
239 1 1;
240  
241 __END__
↑ Summary