blib/lib/Lingua/BEL/Word2Num.pm

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