blib/lib/Lingua/AZE/Word2Num.pm

filter: Statementstmt 81.8% 9 / 11 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 86.6% 13 / 15 CC 1 SCAR 0.7
CC
1
Cov
82%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8 -*-
2 package Lingua::AZE::Word2Num;
3 # ABSTRACT: Word to number conversion in Azerbaijani
4  
5 1 ▶use 5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # {{{ use block
10  
11 0 ▶use Export::Attrs;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use Parse::RecDescent;
13  
14 # }}}
15 # {{{ var block
16 our $VERSION = '0.2603300';
17 my $parser = aze_numerals();
18  
19 # }}}
20  
21 # {{{ w2n convert text to number
22  
23 sub w2n :Export {
24 my $input = shift // return;
25  
26 return $parser->numeral($input);
27 }
28  
29 # }}}
30 # {{{ aze_numerals create parser for azerbaijani numerals
31  
32 sub aze_numerals {
33 return Parse::RecDescent->new(q{
34 <autoaction: { $item[1] } >
35  
36 numeral: mega
37 | kOhOd
38 | { }
39  
40 number: 'sıfır' { 0 }
41 | 'bir' { 1 }
42 | 'iki' { 2 }
43 | 'üç' { 3 }
44 | 'dörd' { 4 }
45 | 'beş' { 5 }
46 | 'altı' { 6 }
47 | 'yeddi' { 7 }
48 | 'səkkiz' { 8 }
49 | 'doqquz' { 9 }
50  
51 tens: 'on' { 10 }
52 | 'iyirmi' { 20 }
53 | 'otuz' { 30 }
54 | 'qırx' { 40 }
55 | 'əlli' { 50 }
56 | 'altmış' { 60 }
57 | 'yetmiş' { 70 }
58 | 'səksən' { 80 }
59 | 'doxsan' { 90 }
60  
61 deca: tens number { $item[1] + $item[2] }
62 | tens
63 | number
64  
65 hecto: number 'yüz' deca { $item[1] * 100 + $item[3] }
66 | number 'yüz' { $item[1] * 100 }
67 | 'yüz' deca { 100 + $item[2] }
68 | 'yüz' { 100 }
69  
70 hOd: hecto
71 | deca
72  
73 kilo: hOd 'min' hOd { $item[1] * 1000 + $item[3] }
74 | hOd 'min' { $item[1] * 1000 }
75 | 'min' hOd { 1000 + $item[2] }
76 | 'min' { 1000 }
77  
78 kOhOd: kilo
79 | hOd
80  
81 mega: hOd 'milyon' kOhOd { $item[1] * 1_000_000 + $item[3] }
82 | hOd 'milyon' { $item[1] * 1_000_000 }
83 });
84 }
85  
86 # }}}
87  
88 # {{{ capabilities declare supported features
89  
90 sub capabilities {
91 return {
92 w2n => 1,
93 };
94 }
95  
96 # }}}
97 # {{{ ordinal2cardinal convert ordinal text to cardinal text
98  
99 sub ordinal2cardinal :Export {
100 my $input = shift // return;
101  
102 # Known cardinal words (from parser vocabulary).
103 state $cardinals = { map { $_ => 1 } qw(
104 sıfır bir iki üç dörd beş altı yeddi səkkiz doqquz
105 on iyirmi otuz qırx əlli altmış yetmiş səksən doxsan
106 yüz min milyon
107 )};
108  
109 # Azerbaijani ordinal suffixes (vowel harmony).
110 # After vowel-final stem: -ncı -nci -ncu -ncü
111 # After consonant-final stem: -ıncı -inci -uncu -üncü
112 # No consonant softening (unlike Turkish).
113 my @harmony = (
114 [ 'ncı', 'ıncı' ],
115 [ 'nci', 'inci' ],
116 [ 'ncu', 'uncu' ],
117 [ 'ncü', 'üncü' ],
118 );
119  
120 for my $pair (@harmony) {
121 my ($short, $long) = @{$pair};
122  
123 for my $suffix ($long, $short) {
124 next unless $input =~ /\Q$suffix\E\z/xms;
125 my $candidate = $input =~ s/\Q$suffix\E\z//xmsr;
126 next unless length $candidate;
127  
128 my ($last_word) = $candidate =~ /(\S+)\z/xms;
129 return $candidate if exists $cardinals->{$last_word};
130 }
131 }
132  
133 return; # not an ordinal
134 }
135  
136 # }}}
137  
138 1;
139  
140 __END__
↑ Summary