File Coverage

blib/lib/Lingua/ENG/Word2Num.pm
Criterion Covered Total %
statement 14 14 100.0
branch n/a
condition 2 2 100.0
subroutine 6 6 100.0
pod 2 2 100.0
total 24 24 100.0


line stmt bran cond sub pod time code
1             # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8; -*-
2              
3             package Lingua::ENG::Word2Num;
4             # ABSTRACT: Word 2 number conversion in ENG.
5              
6             # {{{ use block
7              
8 1     1   142625 use 5.16.0;
  1         3  
9              
10 1     1   1147 use Parse::RecDescent;
  1         50551  
  1         16  
11 1     1   955 use Export::Attrs;
  1         15068  
  1         10  
12              
13             # }}}
14             # {{{ var block
15             our $VERSION = '0.2603230';
16              
17             my $parser = eng_numerals();
18              
19             # }}}
20              
21             # {{{ w2n convert number to text
22              
23             sub w2n :Export {
24 4   100 4 1 414691 my $input = shift // return;
25              
26 3         49 return $parser->numeral($input);
27 1     1   185 }
  1         3  
  1         11  
28              
29             # }}}
30              
31             # {{{ eng_numerals create parser for numerals
32              
33             sub eng_numerals {
34 1     1 1 12 return Parse::RecDescent->new(q{
35            
36              
37             numeral: mega
38             | kOhOd
39             | { }
40              
41             number: 'twelve' { 12 }
42             | 'thirteen' { 13 }
43             | 'fourteen' { 14 }
44             | 'fifteen' { 15 }
45             | 'sixteen' { 16 }
46             | 'seventeen' { 17 }
47             | 'eighteen' { 18 }
48             | 'nineteen' { 19 }
49             | 'zero' { 0 }
50             | 'one' { 1 }
51             | 'two' { 2 }
52             | 'three' { 3 }
53             | 'four' { 4 }
54             | 'five' { 5 }
55             | 'six' { 6 }
56             | 'seven' { 7 }
57             | 'eight' { 8 }
58             | 'nine' { 9 }
59             | 'ten' { 10 }
60             | 'eleven' { 11 }
61              
62             tens: 'twenty' { 20 }
63             | 'thirty' { 30 }
64             | 'forty' { 40 }
65             | 'fifty' { 50 }
66             | 'sixty' { 60 }
67             | 'seventy' { 70 }
68             | 'eighty' { 80 }
69             | 'ninety' { 90 }
70              
71             deca: tens /(-|\s)?/ number { $item[1] + $item[3] }
72             | tens
73             | number
74              
75             hecto: number 'hundred' deca { $item[1] * 100 + $item[3] }
76             | number 'hundred' { $item[1] * 100 }
77              
78             hOd: hecto
79             | deca
80              
81             kilo: hOd /thousand,?/ hOd { $item[1] * 1000 + $item[3] }
82             | hOd /thousand,?/ { $item[1] * 1000 }
83             | /thousand,?/ hOd { 1000 + $item[2] }
84             | /thousand,?/ { 1000 }
85              
86             kOhOd: kilo
87             | hOd
88              
89             mega: hOd /millions?,?/ kOhOd { $item[1] * 1_000_000 + $item[3] }
90             | hOd /millions?,?/ { $item[1] * 1_000_000 }
91             | 'million' kOhOd { 1_000_000 + $item[2] }
92             | 'million' { 1_000_000 }
93              
94             });
95             }
96              
97             # }}}
98              
99             1;
100              
101             __END__