blib/lib/Lingua/MKD/Num2Word.pm

filter: Statementstmt 98.4% 62 / 63 Branchbran 100% 32 / 32 Conditioncond 93.3% 14 / 15 MC/DCmcdc 87.5% 7 / 8 Subroutinesub 88.8% 8 / 9 Podpod 50.0% 1 / 2 totaltotal 96.1% 124 / 129 CC 22 SCAR 23.5
CC
22
Cov
98%
num2mkd_cardinal
16.0
_num_to_words
7.0
capabilities
2.0
CRAP 10.4
↑ Summary
1 # For Emacs: -*- mode:cperl; mode:folding; coding:utf-8; -*-
2  
3 package Lingua::MKD::Num2Word;
4 # ABSTRACT: Number to word conversion in Macedonian
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  
19 1 my %token1 = qw( 0 нула 1 еден 2 два
20 3 три 4 четири 5 пет
21 6 шест 7 седум 8 осум
22 9 девет 10 десет 11 единаесет
23 12 дванаесет 13 тринаесет 14 четиринаесет
24 15 петнаесет 16 шестнаесет 17 седумнаесет
25 18 осумнаесет 19 деветнаесет
26 );
27 1 my %token2 = qw( 20 дваесет 30 триесет 40 четириесет
28 50 педесет 60 шеесет 70 седумдесет
29 80 осумдесет 90 деведесет
30 );
31 1 my %token3 = ( 100, 'сто', 200, 'двесте', 300, 'триста',
32 400, 'четиристотини', 500, 'петстотини', 600, 'шестстотини',
33 700, 'седумстотини', 800, 'осумстотини', 900, 'деветстотини'
34 );
35  
36 # }}}
37  
38 # {{{ _num_to_words internal: convert 1-999 to words
39  
40 sub _num_to_words {
41 34 ▶ my ($number) = @_;
Subroutine _num_to_words: called CC=7 SCAR=19.5
42  
43 34 ▶ return $token1{$number} if exists $token1{$number};
Branch: 1 branch
BranchTrueFalseTotal
if exists $token1{$number} 13 21 34
44  
45 21 ▶ if ($number < 100) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number < 100) 6 15 21
46 6 my $rem = $number % 10;
47 6 ▶ if ($rem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rem == 0) 4 2 6
48 4 return $token2{$number};
49 }
50 2 return $token2{$number - $rem} . ' и ' . $token1{$rem};
51 }
52  
53 15 my $hund_rem = $number % 100;
54 15 my $hund_val = $number - $hund_rem;
55  
56 15 ▶ if ($hund_rem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hund_rem == 0) 6 9 15
57 6 return $token3{$hund_val};
58 }
59  
60 # hundreds + remainder: "и" goes before the last component
61 # 101 = сто и еден, 120 = сто и дваесет, 123 = сто дваесет и три
62 9 my $tens_rem = $hund_rem % 10;
63 9 ▶ if ($hund_rem < 20 || $tens_rem == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hund_rem < 20 or $tens_rem == 0) 2 7 9
Condition100%3/3
$hund_rem < 20 or $tens_rem == 0
l!l&&r!l&&!r
117
MC/DC100%2/2
$hund_rem < 20 or $tens_rem == 0
$hund_rem < 20 $tens_rem == 0
Truth table100%3/3
A or B
A= $hund_rem < 20B= $tens_rem == 0
ABresult
1X1
011
000
64 # single component remainder: hundred "и" remainder
65 2 return $token3{$hund_val} . ' и ' . _num_to_words($hund_rem);
66 }
67  
68 # two-component remainder (e.g. 23): hundred tens "и" unit
69 7 return $token3{$hund_val} . ' ' . $token2{$hund_rem - $tens_rem} . ' и ' . $token1{$tens_rem};
70 }
71  
72 # }}}
73  
74 # {{{ num2mkd_cardinal number to string conversion
75  
76 sub num2mkd_cardinal :Export {
77 36 ▶ my $number = shift;
Subroutine num2mkd_cardinal: called CC=16 SCAR=27.7
Pod documented
78  
79 36 ▶ 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
Condition88%8/9
not defined $number && $number =~ /\A\d+\z/msux or $number < 0 or $number > 999999999
l!l&&r!l&&!r
2133
not defined $number && $number =~ /\A\d+\z/msux or $number < 0
l!l&&r!l&&!r
2034
defined $number && $number =~ /\A\d+\z/msux
!ll&&!rl&&r
1134
MC/DC75%3/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 table80%4/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
80 if !defined $number
81 || $number !~ m{\A\d+\z}xms
82 || $number < 0
83 || $number > 999_999_999;
84  
85 33 ▶ return $token1{0} if $number == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $number == 0 1 32 33
86  
87 32 my $result = '';
88  
89 # {{{ millions
90 32 ▶ if ($number >= 1_000_000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number >= 1000000) 5 27 32
91 5 my $millions = int($number / 1_000_000);
92 5 $number %= 1_000_000;
93  
94 5 ▶ if ($millions == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($millions == 1) { } 2 3 5
95 2 $result = 'еден милион';
96 }
97 else {
98 3 $result = _num_to_words($millions) . ' милиони';
99 }
100 }
101 # }}}
102  
103 # {{{ thousands
104 32 ▶ if ($number >= 1_000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number >= 1000) 10 22 32
105 10 my $thousands = int($number / 1_000);
106 10 $number %= 1_000;
107  
108 10 ▶ $result .= ' ' if $result ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $result ne "" 2 8 10
109  
110 10 ▶ if ($thousands == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($thousands == 1) { } 6 4 10
elsif ($thousands == 2) { } 1 3 4
111 6 $result .= 'илјада';
112 }
113 elsif ($thousands == 2) {
114 1 $result .= 'две илјади';
115 }
116 else {
117 3 $result .= _num_to_words($thousands) . ' илјади';
118 }
119 }
120 # }}}
121  
122 # {{{ remainder (0-999)
123 32 ▶ if ($number > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number > 0) 26 6 32
124 26 ▶ if ($result ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result ne '') { } 7 19 26
125 # "и" before remainder when there is no compound hundreds group:
126 # i.e. remainder < 100 (no hundreds at all) or round hundreds (100,200,...,900)
127 7 ▶ if ($number < 100 || $number % 100 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number < 100 or $number % 100 == 0) { } 4 3 7
Condition100%3/3
$number < 100 or $number % 100 == 0
l!l&&r!l&&!r
313
MC/DC100%2/2
$number < 100 or $number % 100 == 0
$number < 100 $number % 100 == 0
Truth table100%3/3
A or B
A= $number < 100B= $number % 100 == 0
ABresult
1X1
011
000
128 4 $result .= ' и ' . _num_to_words($number);
129 }
130 else {
131 3 $result .= ' ' . _num_to_words($number);
132 }
133 }
134 else {
135 19 $result = _num_to_words($number);
136 }
137 }
138 # }}}
139  
140 32 return $result;
141 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
142  
143 # }}}
144  
145  
146 # {{{ capabilities declare supported features
147  
148 sub capabilities {
149 return {
150 0 ▶ cardinal => 1,
Subroutine capabilities: not called CC=1 SCAR=6.9
Pod undocumented
151 ordinal => 0,
152 };
153 }
154  
155 # }}}
156 1 1;
157  
158 __END__
↑ Summary