blib/lib/Lingua/RU/Num2Word.pm

filter: Statementstmt 95.9% 71 / 74 Branchbran 80.0% 24 / 30 Conditioncond 76.9% 20 / 26 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 86.0% 130 / 151 SCAR 32.6
CC
25
Cov
88%
_get_string
18.9
num2rus_cardinal
8.2
BEGIN
1.0
CRAP 26.0
↑ 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.03'; # 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 26 ▶ my ( $number, $gender ) = @_;
Subroutine num2rus_cardinal: called CC=8 SCAR=21.0
Pod documented
99  
100 26 ▶ $gender ||= 'MASCULINE'; # masculine by default
Condition100%2/2
$gender ||= "MASCULINE"
l!l
719
MC/DC100%1/1
$gender ||= "MASCULINE"
$gender
Truth table100%2/2
A
A= $gender
Aresult
11
00
101 26 ▶ 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 25 26
102 25 $gender = uc $gender;
103  
104 25 ▶ return _get_string( 0, 0, 0 ) unless $number; # no extra calculations for zero
Branch: 1 branch
BranchTrueFalseTotal
unless $number 1 24 25
105  
106 24 my ( $result, $negative );
107  
108 # Negative number, just add another word
109 24 ▶ if ( $number < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number < 0) 0 24 24
110 0 $number = abs( $number );
111 0 $negative = 1;
112 }
113  
114 24 $result = "";
115 24 my $int_number = floor( $number ); # no doubles
116  
117 24 ▶ for ( my $i = 1 ; $i < 6 && $int_number >= 1 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 6 and $int_number >= 1) 31 24 55
Condition100%3/3
$i < 6 and $int_number >= 1
!ll&&!rl&&r
12331
MC/DC100%2/2
$i < 6 and $int_number >= 1
$i < 6 $int_number >= 1
Truth table100%3/3
A and B
A= $i < 6B= $int_number >= 1
ABresult
0X0
100
111
118 31 my $tmp_number = $int_number / 1000;
119 31 my $number_part = sprintf( "%0.3f", $tmp_number - sprintf( "%d", $tmp_number ) ) * 1000;
120  
121 31 $int_number = floor $tmp_number; # no doubles again
122 31 $result = _get_string( $number_part, $i, $gender ) . " " . $result;
123 }
124  
125 # Clean the result
126 24 $result =~ s/\s+/ /g;
127 24 $result =~ s/\s+$//;
128  
129 24 ▶ if ( $negative ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($negative) 0 24 24
130 0 $result = "минус $result";
131 }
132  
133 24 return $result;
134 }
135  
136 sub _get_string {
137 32 ▶ my $sum = shift;
Subroutine _get_string: called CC=18 SCAR=29.4
138 32 ▶ return unless defined $sum;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sum 0 32 32
139  
140 32 my $nominal = shift;
141 32 my $gender = shift;
142 32 my ( $result, $nom ) = ( '', -1 );
143  
144 32 ▶ 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) 32 0 32
Condition50%6/12
not $nominal and $sum < 100 or $nominal > 0 and $nominal < 6 and $sum < 1000
l!l&&r!l&&!r
1310
not $nominal and $sum < 100
!ll&&!rl&&r
3101
$nominal > 0 and $nominal < 6 and $sum < 1000
!ll&&!rl&&r
0031
$nominal > 0 and $nominal < 6
!ll&&!rl&&r
0031
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
145 32 my $s2 = sprintf( "%d", $sum / 100 );
146  
147 32 ▶ if ( $s2 > 0 ) { # hundreds
Branch: 1 branch
BranchTrueFalseTotal
if ($s2 > 0) 9 23 32
148 9 $result .= ' ' . $diw{2}{$s2}{0};
149 9 $nom = $diw{2}{$s2}{1};
150 }
151  
152 32 my $sx = floor $sum - $s2 * 100;
153  
154 32 ▶ if ( ( $sx < 20 && $sx > 0 ) || ( $sx == 0 && !$nominal ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sx < 20 and $sx > 0 or $sx == 0 and not $nominal) { } 15 17 32
Condition100%9/9
$sx < 20 and $sx > 0 or $sx == 0 and not $nominal
l!l&&r!l&&!r
14117
$sx < 20 and $sx > 0
!ll&&!rl&&r
12614
$sx == 0 and not $nominal
!ll&&!rl&&r
1251
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
155 15 $result .= " " . $diw{0}{$sx}{0};
156 15 $nom = $diw{0}{$sx}{1};
157 }
158 else {
159 17 my $s1 = floor $sx / 10; # tens
160  
161 17 my $s0 = sprintf( "%d", $sum - $s2 * 100 - $s1 * 10 + 0.5 );
162  
163 17 ▶ if ( $s1 > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s1 > 0) 12 5 17
164 12 $result .= ' ' . $diw{1}{$s1}{0};
165 12 $nom = $diw{1}{$s1}{1};
166 }
167 17 ▶ if ( $s0 > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s0 > 0) 12 5 17
168 12 $result .= ' ' . $diw{0}{$s0}{0};
169 12 $nom = $diw{0}{$s0}{1};
170 }
171 }
172 }
173 32 ▶ if ( $nom >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nom >= 0) 27 5 32
174  
175 27 ▶ if ( $nominal == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nominal == 1) { } 23 4 27
176 23 ▶ $result .= defined $nominal ? ' ' . $genders{$gender}{$nom} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $nominal ? : 23 0 23
177 }
178 else {
179 4 ▶ $result .= defined $nominal ? ' ' . $nom{$nominal}{$nom} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $nominal ? : 4 0 4
180 }
181 }
182 32 $result =~ s/^\s*//g;
183 32 $result =~ s/\s*$//g;
184  
185 32 return $result;
186 }
187  
188 1 1;
189  
190 __END__
↑ Summary