blib/lib/Lingua/CES/Num2Word.pm

filter: Statementstmt 53.1% 60 / 113 Branchbran 37.5% 27 / 72 Conditioncond 27.0% 13 / 48 MC/DCmcdc 11.5% 3 / 26 Subroutinesub 80.0% 8 / 10 Podpod 66.6% 2 / 3 totaltotal 41.5% 113 / 272 CC 53 SCAR 57.5
CC
53
Cov
43%
num2ces_ordinal
702.0
num2ces_cardinal
45.0
capabilities
2.0
CRAP 314.9
↑ Summary
1 # For Emacs: -*- mode:cperl; mode:folding; coding:utf-8; -*-
2  
3 package Lingua::CES::Num2Word;
4 # ABSTRACT: Number to word conversion in Czech
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 Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Export::Attrs;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # }}}
16 # {{{ var block
17 1 our $VERSION = '0.2603270';
18 1 my %token1 = qw( 0 nula 1 jedna 2 dva
19 3 tři 4 čtyři 5 pět
20 6 šest 7 sedm 8 osm
21 9 devět 10 deset 11 jedenáct
22 12 dvanáct 13 třináct 14 čtrnáct
23 15 patnáct 16 šestnáct 17 sedmnáct
24 18 osmnáct 19 devatenáct
25 );
26 1 my %token2 = qw( 20 dvacet 30 třicet 40 čtyřicet
27 50 padesát 60 šedesát 70 sedmdesát
28 80 osmdesát 90 devadesát
29 );
30 1 my %token3 = ( 100, 'sto', 200, 'dvě stě', 300, 'tři sta',
31 400, 'čtyři sta', 500, 'pět set', 600, 'šest set',
32 700, 'sedm set', 800, 'osm set', 900, 'devět set'
33 );
34  
35 # }}}
36  
37 # {{{ num2ces_cardinal number to string conversion
38  
39 sub num2ces_cardinal :Export {
40 34 ▶ my $result = '';
Subroutine num2ces_cardinal: called CC=28 SCAR=38.1
Pod documented
41 34 my $number = shift;
42  
43 34 ▶ croak 'You should specify a number from interval [0, 999_999_999]'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $number && $number =~ /\A\d+\z/msux or $number < 0 or $number > 999999999 1 33 34
Condition44%4/9
not defined $number && $number =~ /\A\d+\z/msux or $number < 0 or $number > 999999999
l!l&&r!l&&!r
0133
not defined $number && $number =~ /\A\d+\z/msux or $number < 0
l!l&&r!l&&!r
0034
defined $number && $number =~ /\A\d+\z/msux
!ll&&!rl&&r
0034
MC/DC25%1/4
not defined $number && $number =~ /\A\d+\z/msux or $number < 0 or $number > 999999999
defined $number $number =~ /\A\d+\z/msux $number < 0 $number > 999999999
Truth table40%2/5
not(A && B) or C or D
A= defined $numberB= $number =~ /\A\d+\z/msuxC= $number < 0D= $number > 999999999
ABCDresult
0XXX1
10XX1
111X1
11011
11000
44 if !defined $number
45 || $number !~ m{\A\d+\z}xms
46 || $number < 0
47 || $number > 999_999_999;
48  
49 33 my $reminder = 0;
50  
51 33 ▶ if ($number < 20) {
Branch: 5 branches
BranchTrueFalseTotal
if ($number < 20) { } 13 20 33
elsif ($number < 100) { } 8 12 20
elsif ($number < 1000) { } 8 4 12
elsif ($number < 1000000) { } 2 2 4
elsif ($number < 1000000000) { } 2 0 2
52 13 $result = $token1{$number};
53 }
54 elsif ($number < 100) {
55 8 $reminder = $number % 10;
56 8 ▶ if ($reminder == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reminder == 0) { } 1 7 8
57 1 $result = $token2{$number};
58 }
59 else {
60 7 $result = $token2{$number - $reminder}.' '.num2ces_cardinal($reminder);
61 }
62 }
63 elsif ($number < 1_000) {
64 8 $reminder = $number % 100;
65 8 ▶ if ($reminder != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reminder != 0) { } 7 1 8
66 7 $result = $token3{$number - $reminder}.' '.num2ces_cardinal($reminder);
67 }
68 else {
69 1 $result = $token3{$number};
70 }
71 }
72 elsif ($number < 1_000_000) {
73 2 $reminder = $number % 1_000;
74 2 ▶ my $tmp1 = ($reminder != 0) ? ' '.num2ces_cardinal($reminder) : '';
Branch: 1 branch
BranchTrueFalseTotal
$reminder != 0 ? : 2 0 2
75 2 my $tmp2 = substr($number, 0, length($number)-3);
76 2 my $tmp3 = $tmp2 % 100;
77 2 my $tmp4 = $tmp2 % 10;
78  
79 2 ▶ if ($tmp3 < 9 || $tmp3 > 20) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp3 < 9 or $tmp3 > 20) { } 2 0 2
Condition33%1/3
$tmp3 < 9 or $tmp3 > 20
l!l&&r!l&&!r
020
MC/DC0%0/2
$tmp3 < 9 or $tmp3 > 20
$tmp3 < 9 $tmp3 > 20
Truth table33%1/3
A or B
A= $tmp3 < 9B= $tmp3 > 20
ABresult
1X1
011
000
80 2 ▶ if ($tmp4 == 1 && $tmp2 == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tmp4 == 1 and $tmp2 == 1) { } 0 2 2
elsif ($tmp4 == 1) { } 0 2 2
elsif ($tmp4 > 1 and $tmp4 < 5) { } 1 1 2
Condition33%1/3
$tmp4 == 1 and $tmp2 == 1
!ll&&!rl&&r
200
MC/DC0%0/2
$tmp4 == 1 and $tmp2 == 1
$tmp4 == 1 $tmp2 == 1
Truth table33%1/3
A and B
A= $tmp4 == 1B= $tmp2 == 1
ABresult
0X0
100
111
81 0 $tmp2 = 'tisíc';
82 }
83 elsif ($tmp4 == 1) {
84 0 $tmp2 = num2ces_cardinal($tmp2 - $tmp4).' jeden tisíc';
85 }
86 ▶ elsif($tmp4 > 1 && $tmp4 < 5) {
Condition66%2/3
$tmp4 > 1 and $tmp4 < 5
!ll&&!rl&&r
011
MC/DC50%1/2
$tmp4 > 1 and $tmp4 < 5
$tmp4 > 1 $tmp4 < 5
Truth table66%2/3
A and B
A= $tmp4 > 1B= $tmp4 < 5
ABresult
0X0
100
111
87 1 $tmp2 = num2ces_cardinal($tmp2).' tisíce';
88 }
89 else {
90 1 $tmp2 = num2ces_cardinal($tmp2).' tisíc';
91 }
92 }
93 else {
94 0 $tmp2 = num2ces_cardinal($tmp2).' tisíc';
95 }
96 2 $result = $tmp2.$tmp1;
97 }
98 elsif ($number < 1_000_000_000) {
99 2 $reminder = $number % 1_000_000;
100 2 ▶ my $tmp1 = ($reminder != 0) ? ' '.num2ces_cardinal($reminder) : '';
Branch: 1 branch
BranchTrueFalseTotal
$reminder != 0 ? : 2 0 2
101 2 my $tmp2 = substr($number, 0, length($number)-6);
102 2 my $tmp3 = $tmp2 % 100;
103 2 my $tmp4 = $tmp2 % 10;
104  
105 2 ▶ if ($tmp3 < 9 || $tmp3 > 20) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp3 < 9 or $tmp3 > 20) { } 2 0 2
Condition66%2/3
$tmp3 < 9 or $tmp3 > 20
l!l&&r!l&&!r
110
MC/DC0%0/2
$tmp3 < 9 or $tmp3 > 20
$tmp3 < 9 $tmp3 > 20
Truth table66%2/3
A or B
A= $tmp3 < 9B= $tmp3 > 20
ABresult
1X1
011
000
106 2 ▶ if ($tmp4 == 1 && $tmp2 == 1) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tmp4 == 1 and $tmp2 == 1) { } 0 2 2
elsif ($tmp4 == 1) { } 0 2 2
elsif ($tmp4 > 1 and $tmp4 < 5) { } 1 1 2
Condition33%1/3
$tmp4 == 1 and $tmp2 == 1
!ll&&!rl&&r
200
MC/DC0%0/2
$tmp4 == 1 and $tmp2 == 1
$tmp4 == 1 $tmp2 == 1
Truth table33%1/3
A and B
A= $tmp4 == 1B= $tmp2 == 1
ABresult
0X0
100
111
107 0 $tmp2 = 'milion';
108 }
109 elsif ($tmp4 == 1) {
110 0 $tmp2 = num2ces_cardinal($tmp2 - $tmp4).' jeden milion';
111 }
112 ▶ elsif($tmp4 > 1 && $tmp4 < 5) {
Condition66%2/3
$tmp4 > 1 and $tmp4 < 5
!ll&&!rl&&r
011
MC/DC50%1/2
$tmp4 > 1 and $tmp4 < 5
$tmp4 > 1 $tmp4 < 5
Truth table66%2/3
A and B
A= $tmp4 > 1B= $tmp4 < 5
ABresult
0X0
100
111
113 1 $tmp2 = num2ces_cardinal($tmp2).' miliony';
114 }
115 else {
116 1 $tmp2 = num2ces_cardinal($tmp2).' milionů';
117 }
118 }
119 else {
120 0 $tmp2 = num2ces_cardinal($tmp2).' milionů';
121 }
122  
123 2 $result = $tmp2.$tmp1;
124 }
125  
126 33 return $result;
127 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
128  
129 # }}}
130  
131 # {{{ num2ces_ordinal number to ordinal string conversion
132  
133 sub num2ces_ordinal :Export {
134 0 ▶ my $number = shift;
Subroutine num2ces_ordinal: not called CC=26 SCAR=65.5
Pod documented
135  
136 0 ▶ croak 'You should specify a number from interval [0, 999_999_999]'
Branch: 1 branch
BranchTrueFalseTotal
if not defined $number && $number =~ /\A\d+\z/msux or $number < 0 or $number > 999999999 0 0 0
Condition0%0/9
not defined $number && $number =~ /\A\d+\z/msux or $number < 0 or $number > 999999999
l!l&&r!l&&!r
000
not defined $number && $number =~ /\A\d+\z/msux or $number < 0
l!l&&r!l&&!r
000
defined $number && $number =~ /\A\d+\z/msux
!ll&&!rl&&r
000
MC/DC0%0/4
not defined $number && $number =~ /\A\d+\z/msux or $number < 0 or $number > 999999999
defined $number $number =~ /\A\d+\z/msux $number < 0 $number > 999999999
Truth table0%0/5
not(A && B) or C or D
A= defined $numberB= $number =~ /\A\d+\z/msuxC= $number < 0D= $number > 999999999
ABCDresult
0XXX1
10XX1
111X1
11011
11000
137 if !defined $number
138 || $number !~ m{\A\d+\z}xms
139 || $number < 0
140 || $number > 999_999_999;
141  
142 # Irregular ordinals 0-10
143 0 my %irregular = (
144 0 => 'nultý',
145 1 => 'první',
146 2 => 'druhý',
147 3 => 'třetí',
148 4 => 'čtvrtý',
149 5 => 'pátý',
150 6 => 'šestý',
151 7 => 'sedmý',
152 8 => 'osmý',
153 9 => 'devátý',
154 10 => 'desátý',
155 );
156  
157 0 ▶ return $irregular{$number} if exists $irregular{$number};
Branch: 1 branch
BranchTrueFalseTotal
if exists $irregular{$number} 0 0 0
158  
159 # Irregular teens 11-19
160 0 my %teens = (
161 11 => 'jedenáctý',
162 12 => 'dvanáctý',
163 13 => 'třináctý',
164 14 => 'čtrnáctý',
165 15 => 'patnáctý',
166 16 => 'šestnáctý',
167 17 => 'sedmnáctý',
168 18 => 'osmnáctý',
169 19 => 'devatenáctý',
170 );
171  
172 0 ▶ return $teens{$number} if exists $teens{$number};
Branch: 1 branch
BranchTrueFalseTotal
if exists $teens{$number} 0 0 0
173  
174 # Tens ordinals
175 0 my %tens_ord = (
176 20 => 'dvacátý',
177 30 => 'třicátý',
178 40 => 'čtyřicátý',
179 50 => 'padesátý',
180 60 => 'šedesátý',
181 70 => 'sedmdesátý',
182 80 => 'osmdesátý',
183 90 => 'devadesátý',
184 );
185  
186 # Hundreds ordinals
187 0 my %hundreds_ord = (
188 100 => 'stý',
189 200 => 'dvoustý',
190 300 => 'třístý',
191 400 => 'čtyřstý',
192 500 => 'pětistý',
193 600 => 'šestistý',
194 700 => 'sedmistý',
195 800 => 'osmistý',
196 900 => 'devítistý',
197 );
198  
199 # For compound numbers: cardinal prefix + ordinal of the last part
200 # Czech ordinals for compound numbers use the cardinal for all but the
201 # last significant part, which gets the ordinal suffix.
202 # e.g., 21 = "dvacátý první", 100 = "stý", 125 = "sto dvacátý pátý"
203  
204 # For numbers >= 1000, use cardinal for the large part, ordinal for remainder
205 0 ▶ if ($number >= 1_000_000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number >= 1000000) 0 0 0
206 0 my $millions = int($number / 1_000_000);
207 0 my $remainder = $number % 1_000_000;
208 0 ▶ if ($remainder == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remainder == 0) 0 0 0
209 0 my $prefix = num2ces_cardinal($millions);
210 0 ▶ if ($millions == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($millions == 1) 0 0 0
211 0 return 'miliontý';
212 }
213 0 return $prefix . ' miliontý';
214 }
215 0 my $prefix = num2ces_cardinal($millions);
216 0 ▶ my $mil_word = ($millions == 1) ? 'milion' :
Branch: 2 branches
BranchTrueFalseTotal
$millions == 1 ? : 0 0 0
$millions >= 2 && $millions <= 4 ? : 0 0 0
Condition0%0/3
$millions >= 2 and $millions <= 4
!ll&&!rl&&r
000
MC/DC0%0/2
$millions >= 2 and $millions <= 4
$millions >= 2 $millions <= 4
Truth table0%0/3
A and B
A= $millions >= 2B= $millions <= 4
ABresult
0X0
100
111
217 ($millions >= 2 && $millions <= 4) ? 'miliony' : 'milionů';
218 0 ▶ $mil_word = 'milion' if $millions == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $millions == 1 0 0 0
219 0 return $prefix . ' ' . $mil_word . ' ' . num2ces_ordinal($remainder);
220 }
221  
222 0 ▶ if ($number >= 1_000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number >= 1000) 0 0 0
223 0 my $thousands = int($number / 1_000);
224 0 my $remainder = $number % 1_000;
225 0 ▶ if ($remainder == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remainder == 0) 0 0 0
226 0 ▶ if ($thousands == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($thousands == 1) 0 0 0
227 0 return 'tisící';
228 }
229 0 return num2ces_cardinal($thousands) . ' tisící';
230 }
231 0 my $prefix = num2ces_cardinal($thousands * 1_000);
232 # Use just the thousands cardinal part (without remainder)
233 # then ordinal of remainder
234 0 my $thou_cardinal;
235 0 ▶ if ($thousands == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($thousands == 1) { } 0 0 0
236 0 $thou_cardinal = 'tisíc';
237 }
238 else {
239 0 ▶ $thou_cardinal = num2ces_cardinal($thousands) .
Branch: 1 branch
BranchTrueFalseTotal
$thousands % 100 >= 2 && $thousands % 100 <= 4 && !($thousands % 100 >= 12 && $thousands % 100 <= 14) ? : 0 0 0
Condition0%0/9
$thousands % 100 >= 2 and $thousands % 100 <= 4 and not $thousands % 100 >= 12 && $thousands % 100 <= 14
!ll&&!rl&&r
000
$thousands % 100 >= 2 and $thousands % 100 <= 4
!ll&&!rl&&r
000
$thousands % 100 >= 12 && $thousands % 100 <= 14
!ll&&!rl&&r
000
MC/DC0%0/4
$thousands % 100 >= 2 and $thousands % 100 <= 4 and not $thousands % 100 >= 12 && $thousands % 100 <= 14
$thousands % 100 >= 2 $thousands % 100 <= 4 $thousands % 100 >= 12 $thousands % 100 <= 14
Truth table0%0/5
A and B and not(C && D)
A= $thousands % 100 >= 2B= $thousands % 100 <= 4C= $thousands % 100 >= 12D= $thousands % 100 <= 14
ABCDresult
0XXX0
10XX0
11110
110X1
11101
240 (($thousands % 100 >= 2 && $thousands % 100 <= 4 &&
241 !($thousands % 100 >= 12 && $thousands % 100 <= 14))
242 ? ' tisíce' : ' tisíc');
243 }
244 0 return $thou_cardinal . ' ' . num2ces_ordinal($remainder);
245 }
246  
247 0 ▶ if ($number >= 100) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number >= 100) 0 0 0
248 0 my $h = int($number / 100) * 100;
249 0 my $remainder = $number % 100;
250 0 ▶ if ($remainder == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remainder == 0) 0 0 0
251 0 return $hundreds_ord{$h};
252 }
253 0 return $token3{$h} . ' ' . num2ces_ordinal($remainder);
254 }
255  
256 # 20-99 compound
257 0 ▶ if ($number >= 20) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number >= 20) 0 0 0
258 0 my $t = int($number / 10) * 10;
259 0 my $remainder = $number % 10;
260 0 ▶ if ($remainder == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remainder == 0) 0 0 0
261 0 return $tens_ord{$t};
262 }
263 0 return $tens_ord{$t} . ' ' . $irregular{$remainder};
264 }
265  
266 # Should not reach here
267 0 return;
268 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
269  
270 # }}}
271  
272 # {{{ capabilities declare supported features
273  
274 sub capabilities {
275 return {
276 0 ▶ cardinal => 1,
Subroutine capabilities: not called CC=1 SCAR=6.9
Pod undocumented
277 ordinal => 1,
278 };
279 }
280  
281 # }}}
282 1 1;
283  
284 __END__
↑ Summary