blib/lib/Lingua/RUS/Word2Num.pm

filter: Statementstmt 100% 32 / 32 Branchbran n/a Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 100% 46 / 46 CC 2 SCAR 1.8
CC
2
Cov
100%
w2n
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
↑ Summary
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*-
2  
3 package Lingua::RUS::Word2Num;
4 # ABSTRACT: Word to number conversion in Russian
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 Export::Attrs;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Parse::RecDescent;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # }}}
17 # {{{ variable declarations
18 1 our $VERSION = '0.2603270';
19 1 my $parser = ru_numerals();
20  
21 # }}}
22  
23 # {{{ w2n convert text to number
24  
25 sub w2n :Export {
26 7 ▶ my $input = shift // return;
Subroutine w2n: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
shift() // (return)
l!l
61
MC/DC100%1/1
shift() // (return)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
27  
28 6 $input .= " "; # Grant end space before normalizing
29  
30 6 $input =~ s/тысячи /тысяч /g; # Thousand variations. Normalize to тысяч
31 6 $input =~ s/тысячa /тысяч /g;
32 6 $input =~ s/тысяча /тысяч /g;
33  
34 6 $input =~ s/миллиона /миллион /g; # Million variations. Normalize to миллион
35 6 $input =~ s/миллионов /миллион /g;
36  
37 6 return $parser->numeral($input);
38 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 # }}}
41 # {{{ ru_numerals create parser for numerals
42  
43 sub ru_numerals {
44 1 ▶ return Parse::RecDescent->new(q{
Subroutine ru_numerals: called CC=1 SCAR=0.0
Pod documented
45 numeral: <rulevar: local $number = 0>
46 numeral: million { return $item[1]; } # root parse. go from maximum to minimum value
47 | millenium { return $item[1]; }
48 | century { return $item[1]; }
49 | decade { return $item[1]; }
50 | { return undef; }
51  
52 number: 'девятнадцать ' { $return = 19; } # try to find a word from 0 to 19
53 | 'восемнадцать ' { $return = 18; }
54 | 'семнадцать ' { $return = 17; }
55 | 'шестнадцать ' { $return = 16; }
56 | 'пятнадцать ' { $return = 15; }
57 | 'четырнадцать ' { $return = 14; }
58 | 'тринадцать ' { $return = 13; }
59 | 'двенадцать ' { $return = 12; }
60 | 'одинадцать ' { $return = 11; }
61 | 'десять ' { $return = 10; }
62 | 'девять ' { $return = 9; }
63 | 'восемь ' { $return = 8; }
64 | 'семь ' { $return = 7; }
65 | 'шесть ' { $return = 6; }
66 | 'пять ' { $return = 5; }
67 | 'четыре ' { $return = 4; }
68 | 'три ' { $return = 3; }
69 | 'два ' { $return = 2; }
70 | 'две ' { $return = 2; }
71 | 'одна ' { $return = 1; }
72 | 'один ' { $return = 1; }
73 | 'ноль ' { $return = 0; }
74  
75 tens: 'двадцать ' { $return = 20; } # try to find a word that represents
76 | 'тридцать ' { $return = 30; } # values 20,30,..,90
77 | 'сорок ' { $return = 40; }
78 | 'пятьдесят ' { $return = 50; }
79 | 'шестьдесят ' { $return = 60; }
80 | 'семьдесят ' { $return = 70; }
81 | 'восемьдесят ' { $return = 80; }
82 | 'девяносто ' { $return = 90; }
83  
84 hundreds: 'сто ' { $return = 100; } # try to find a word that represents
85 | 'сотня ' { $return = 100; } # values 200,300,..,900
86 | 'двести ' { $return = 200; }
87 | 'триста ' { $return = 300; }
88 | 'четыреста ' { $return = 400; }
89 | 'пятьсот ' { $return = 500; }
90 | 'шестьсот ' { $return = 600; }
91 | 'семьсот ' { $return = 700; }
92 | 'восемьсот ' { $return = 800; }
93 | 'девятьсот ' { $return = 900; }
94  
95 decade: tens(?) number(?) # try to find words that represents values
96 { $return = -1; # from 0 to 99
97 for (@item) {
98 if (ref $_ && defined $$_[0]) {
99 $return += $$_[0] if ($return != -1);
100 $return = $$_[0] if ($return == -1);
101 }
102 }
103 $return = undef if ($return == -1);
104 }
105  
106 century: hundreds(?) decade(?) # try to find words that represents values
107 { $return = 0; # from 100 to 999
108 for (@item) {
109 $return += $$_[0] if (ref $_ && defined $$_[0]);
110 }
111 $return ||= undef;
112 }
113  
114 millenium: century(?) decade(?) 'тысяч ' century(?) decade(?) # try to find words that represents values
115 { $return = 0; # from 1.000 to 999.999
116 for (@item) {
117 if (ref $_ && defined $$_[0]) {
118 $return += $$_[0];
119 } elsif ($_ eq "тысяч ") {
120 $return = ($return>0) ? $return * 1000 : 1000;
121 }
122 }
123 $return ||= undef;
124 }
125  
126 million: century(?) decade(?) # try to find words that represents values
127 'миллион ' # from 1.000.000 to 999.999.999
128 millenium(?) century(?) decade(?)
129 { $return = 0;
130 for (@item) {
131 if (ref $_ && defined $$_[0]) {
132 $return += $$_[0];
133 } elsif ($_ eq "миллион ") {
134 $return = ($return>0) ? $return * 1000000 : 1000000;
135 }
136 }
137 $return ||= undef;
138 }
139 });
140 }
141  
142 # }}}
143  
144 1 1;
145  
146 __END__
↑ Summary