blib/lib/Lingua/RU/Num2Word.pm

filter: Statementstmt 96.0% 73 / 76 Branchbran 78.1% 25 / 32 Conditioncond 73.0% 19 / 26 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 84.5% 131 / 155 SCAR 33.1
CC
26
Cov
87%
_get_string
19.3
num2rus_cardinal
9.3
BEGIN
1.0
CRAP 27.4
↑ Summary
1 package Lingua::RU::Num2Word;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use POSIX qw/floor/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: Numbers to words in russian (without currency, but with specified gender)
10 1 our $VERSION = '0.04'; # VERSION
11 # AUTHORITY
12  
13 1 ▶use Exporter qw/import/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 our @EXPORT_OK = qw(&num2rus_cardinal);
15  
16 1 my %diw = (
17 0 => {
18 0 => { 0 => "ноль", 1 => 1 },
19 1 => { 0 => "", 1 => 2 },
20 2 => { 0 => "", 1 => 3 },
21 3 => { 0 => "три", 1 => 0 },
22 4 => { 0 => "четыре", 1 => 0 },
23 5 => { 0 => "пять", 1 => 1 },
24 6 => { 0 => "шесть", 1 => 1 },
25 7 => { 0 => "семь", 1 => 1 },
26 8 => { 0 => "восемь", 1 => 1 },
27 9 => { 0 => "девять", 1 => 1 },
28 10 => { 0 => "десять", 1 => 1 },
29 11 => { 0 => "одинадцать", 1 => 1 },
30 12 => { 0 => "двенадцать", 1 => 1 },
31 13 => { 0 => "тринадцать", 1 => 1 },
32 14 => { 0 => "четырнадцать", 1 => 1 },
33 15 => { 0 => "пятнадцать", 1 => 1 },
34 16 => { 0 => "шестнадцать", 1 => 1 },
35 17 => { 0 => "семнадцать", 1 => 1 },
36 18 => { 0 => "восемнадцать", 1 => 1 },
37 19 => { 0 => "девятнадцать", 1 => 1 },
38 },
39  
40 1 => {
41 2 => { 0 => "двадцать", 1 => 1 },
42 3 => { 0 => "тридцать", 1 => 1 },
43 4 => { 0 => "сорок", 1 => 1 },
44 5 => { 0 => "пятьдесят", 1 => 1 },
45 6 => { 0 => "шестьдесят", 1 => 1 },
46 7 => { 0 => "семьдесят", 1 => 1 },
47 8 => { 0 => "восемьдесят", 1 => 1 },
48 9 => { 0 => "девяносто", 1 => 1 },
49 },
50 2 => {
51 1 => { 0 => "сто", 1 => 1 },
52 2 => { 0 => "двести", 1 => 1 },
53 3 => { 0 => "триста", 1 => 1 },
54 4 => { 0 => "четыреста", 1 => 1 },
55 5 => { 0 => "пятьсот", 1 => 1 },
56 6 => { 0 => "шестьсот", 1 => 1 },
57 7 => { 0 => "семьсот", 1 => 1 },
58 8 => { 0 => "восемьсот", 1 => 1 },
59 9 => { 0 => "девятьсот", 1 => 1 }
60 }
61  
62 );
63  
64 1 my %nom = (
65 0 => { 0 => "", 1 => "", 2 => "одна", 3 => "две" },
66 1 => { 0 => "", 1 => "", 2 => "один", 3 => "два" },
67 2 => { 0 => "тысячи", 1 => "тысяч", 2 => "одна тысяча", 3 => "две тысячи" },
68 3 => {
69 0 => "миллиона",
70 1 => "миллионов",
71 2 => "один миллион",
72 3 => "два миллиона"
73 },
74 4 => {
75 0 => "миллиарда",
76 1 => "миллиардов",
77 2 => "один миллиард",
78 3 => "два миллиарда"
79 },
80 5 => {
81 0 => "триллиона",
82 1 => "триллионов",
83 2 => "один триллион",
84 3 => "два триллиона"
85 }
86 );
87  
88 1 my %genders = (
89 'FEMININE' => { 0 => "", 1 => "", 2 => "одна", 3 => "две" },
90 'MASCULINE' => { 0 => "", 1 => "", 2 => "один", 3 => "два" },
91 'NEUTER' => { 0 => "", 1 => "", 2 => "одно", 3 => "два" },
92 );
93  
94 # Stolen from Lingua::RU::Number
95  
96  
97 sub num2rus_cardinal {
98 117 ▶ my ( $number, $gender ) = @_;
Subroutine num2rus_cardinal: called CC=9 SCAR=22.3
Pod documented
99  
100 # The biggest number we know about
101 117 ▶ if ($number > 999_999_999_999_999 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number > '999999999999999') 1 116 117
102 1 return '';
103 }
104  
105 116 ▶ $gender ||= 'MASCULINE'; # masculine by default
Condition100%2/2
$gender ||= "MASCULINE"
l!l
9107
MC/DC100%1/1
$gender ||= "MASCULINE"
$gender
Truth table100%2/2
A
A= $gender
Aresult
11
00
106 116 ▶ croak "Wrong gender: $gender, should be MASCULINE|FEMININE|NEUTER" unless $gender =~ /masculine|feminine|neuter/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $gender =~ /masculine|feminine|neuter/i 1 115 116
107 115 $gender = uc $gender;
108  
109 115 ▶ return _get_string( 0, 0, 0 ) unless $number; # no extra calculations for zero
Branch: 1 branch
BranchTrueFalseTotal
unless $number 2 113 115
110  
111 113 my ( $result, $negative );
112  
113 # Negative number, just add another word
114 113 ▶ if ( $number < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number < 0) 0 113 113
115 0 $number = abs( $number );
116 0 $negative = 1;
117 }
118  
119 113 $result = "";
120 113 my $int_number = floor( $number ); # no doubles
121  
122 113 ▶ for ( my $i = 1 ; $i < 6 && $int_number >= 1 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 6 and $int_number >= 1) 116 113 229
Condition66%2/3
$i < 6 and $int_number >= 1
!ll&&!rl&&r
0113116
MC/DC50%1/2
$i < 6 and $int_number >= 1
$i < 6 $int_number >= 1
Truth table66%2/3
A and B
A= $i < 6B= $int_number >= 1
ABresult
0X0
100
111
123 116 my $tmp_number = $int_number / 1000;
124 116 my $number_part = sprintf( "%0.3f", $tmp_number - sprintf( "%d", $tmp_number ) ) * 1000;
125  
126 116 $int_number = floor $tmp_number;
127 # no doubles again
128 116 $result = _get_string( $number_part, $i, $gender ) . " " . $result;
129 }
130  
131 # Clean the result
132 113 $result =~ s/\s+/ /g;
133 113 $result =~ s/\s+$//;
134  
135 113 ▶ if ( $negative ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($negative) 0 113 113
136 0 $result = "минус $result";
137 }
138  
139 113 return $result;
140 }
141  
142 sub _get_string {
143 118 ▶ my $sum = shift;
Subroutine _get_string: called CC=18 SCAR=29.6
144 118 ▶ return unless defined $sum;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sum 0 118 118
145  
146 118 my $nominal = shift;
147 118 my $gender = shift;
148 118 my ( $result, $nom ) = ( '', -1 );
149  
150 118 ▶ if ( ( !$nominal && $sum < 100 ) || ( $nominal > 0 && $nominal < 6 && $sum < 1000 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $nominal and $sum < 100 or $nominal > 0 and $nominal < 6 and $sum < 1000) 118 0 118
Condition50%6/12
not $nominal and $sum < 100 or $nominal > 0 and $nominal < 6 and $sum < 1000
l!l&&r!l&&!r
21160
not $nominal and $sum < 100
!ll&&!rl&&r
11602
$nominal > 0 and $nominal < 6 and $sum < 1000
!ll&&!rl&&r
00116
$nominal > 0 and $nominal < 6
!ll&&!rl&&r
00116
MC/DC0%0/5
not $nominal and $sum < 100 or $nominal > 0 and $nominal < 6 and $sum < 1000
not $nominal $sum < 100 $nominal > 0 $nominal < 6 $sum < 1000
Truth table22%2/9
not(A) and B or C and D and E
A= not $nominalB= $sum < 100C= $nominal > 0D= $nominal < 6E= $sum < 1000
ABCDEresult
11XXX1
0X1111
101111
0X0XX0
0X10X0
0X1100
100XX0
1010X0
101100
151 118 my $s2 = sprintf( "%d", $sum / 100 );
152  
153 118 ▶ if ( $s2 > 0 ) { # hundreds
Branch: 1 branch
BranchTrueFalseTotal
if ($s2 > 0) 11 107 118
154 11 $result .= ' ' . $diw{2}{$s2}{0};
155 11 $nom = $diw{2}{$s2}{1};
156 }
157  
158 118 my $sx = $sum - $s2 * 100;
159  
160 118 ▶ if ( ( $sx < 20 && $sx > 0 ) || ( $sx == 0 && !$nominal ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sx < 20 and $sx > 0 or $sx == 0 and not $nominal) { } 24 94 118
Condition100%9/9
$sx < 20 and $sx > 0 or $sx == 0 and not $nominal
l!l&&r!l&&!r
22294
$sx < 20 and $sx > 0
!ll&&!rl&&r
93322
$sx == 0 and not $nominal
!ll&&!rl&&r
9312
MC/DC75%3/4
$sx < 20 and $sx > 0 or $sx == 0 and not $nominal
$sx < 20 $sx > 0 $sx == 0 not $nominal
Truth table57%4/7
A and B or C and not(D)
A= $sx < 20B= $sx > 0C= $sx == 0D= not $nominal
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
161 24 $result .= " " . $diw{0}{$sx}{0};
162 24 $nom = $diw{0}{$sx}{1};
163 }
164 else {
165 94 my $s1 = floor $sx / 10; # tens
166  
167 94 my $s0 = sprintf( "%d", $sum - $s2 * 100 - $s1 * 10 + 0.5 );
168  
169 94 ▶ if ( $s1 > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s1 > 0) 93 1 94
170 93 $result .= ' ' . $diw{1}{$s1}{0};
171 93 $nom = $diw{1}{$s1}{1};
172 }
173 94 ▶ if ( $s0 > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s0 > 0) 85 9 94
174 85 $result .= ' ' . $diw{0}{$s0}{0};
175 85 $nom = $diw{0}{$s0}{1};
176 }
177 }
178 }
179  
180 118 ▶ if ( $nom >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nom >= 0) 118 0 118
181  
182 118 ▶ if ( $nominal == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nominal == 1) { } 113 5 118
183 113 ▶ $result .= defined $nominal ? ' ' . $genders{$gender}{$nom} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $nominal ? : 113 0 113
184 }
185 else {
186 5 ▶ $result .= defined $nominal ? ' ' . $nom{$nominal}{$nom} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $nominal ? : 5 0 5
187 }
188 }
189 118 $result =~ s/^\s*//g;
190 118 $result =~ s/\s*$//g;
191  
192 118 return $result;
193 }
194  
195 1 1;
196  
197 __END__
↑ Summary