blib/lib/Lingua/UKR/Word2Num.pm

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