File Coverage

blib/lib/Lingua/ZHO/Word2Num.pm
Criterion Covered Total %
statement 22 22 100.0
branch n/a
condition 2 2 100.0
subroutine 8 8 100.0
pod 2 2 100.0
total 34 34 100.0


line stmt bran cond sub pod time code
1             # For Emacs: -*- mode:cperl; mode:folding; coding:utf-8; -*-
2              
3             package Lingua::ZHO::Word2Num;
4             # ABSTRACT: Word 2 number conversion in ZHO.
5              
6             # {{{ use block
7              
8 1     1   24721 use 5.10.1;
  1         4  
  1         53  
9              
10 1     1   6 use strict;
  1         2  
  1         54  
11 1     1   6 use warnings;
  1         7  
  1         31  
12              
13 1     1   877 use Perl6::Export::Attrs;
  1         17870  
  1         6  
14 1     1   1794 use Parse::RecDescent;
  1         43774  
  1         7  
15              
16             # }}}
17             # {{{ variable declarations
18              
19             our $VERSION = 0.0682;
20             my $parser = zho_numerals();
21              
22             # }}}
23              
24             # {{{ w2n convert number to text
25              
26             sub w2n :Export {
27 4   100 4 1 48364 my $input = shift // return;
28              
29 3         7 $input .= " "; # Grant space at the end
30              
31 3         31 return $parser->numeral($input);
32 1     1   115 }
  1         2  
  1         9  
33              
34             # }}}
35             # {{{ zho_numerals create parser for numerals
36              
37             sub zho_numerals {
38 1     1 1 5 return Parse::RecDescent->new(q{
39             numeral:
40             numeral: million { return $item[1]; } # root parse. go from maximum to minimum value
41             | millenium2 { return $item[1]; }
42             | millenium1 { return $item[1]; }
43             | century { return $item[1]; }
44             | decade { return $item[1]; }
45             | { return undef; }
46              
47             number: 'nul ' { $return = 0; } # try to find a word from 0 to 10
48             | 'Yi ' { $return = 1; }
49             | 'Er ' { $return = 2; }
50             | 'San ' { $return = 3; }
51             | 'Si ' { $return = 4; }
52             | 'Wu ' { $return = 5; }
53             | 'Liu ' { $return = 6; } | 'Qi ' { $return = 7; }
54             | 'Ba ' { $return = 8; }
55             | 'Jiu ' { $return = 9; }
56             | 'Shi ' { $return = 10; }
57              
58             tens: 'YiShi ' { $return = 10; } # try to find a word that representates
59             | 'ErShi ' { $return = 20; } # values 20,30,..,90
60             | 'SanShi ' { $return = 30; }
61             | 'SiShi ' { $return = 40; }
62             | 'WuShi ' { $return = 50; }
63             | 'LiuShi ' { $return = 60; }
64             | 'QiShi ' { $return = 70; }
65             | 'BaShi ' { $return = 80; }
66             | 'JiuShi ' { $return = 90; }
67              
68             hundreds: 'YiBai ' { $return = 100; } # try to find a word that representates
69             | 'ErBai ' { $return = 200; } # values 100,200,..,900
70             | 'SanBai ' { $return = 300; }
71             | 'SiBai ' { $return = 400; }
72             | 'WuBai ' { $return = 500; }
73             | 'LiuBai ' { $return = 600; }
74             | 'QiBai ' { $return = 700; }
75             | 'BaBai ' { $return = 800; }
76             | 'JiuBai ' { $return = 900; }
77              
78             thousands: 'YiQian ' { $return = 1000; } # try to find a word that representates
79             | 'ErQian ' { $return = 2000; } # values 1000,2000,..,9000
80             | 'SanQian ' { $return = 3000; }
81             | 'SiQian ' { $return = 4000; }
82             | 'WuQian ' { $return = 5000; }
83             | 'LiuQian ' { $return = 6000; }
84             | 'QiQian ' { $return = 7000; }
85             | 'BaQian ' { $return = 8000; }
86             | 'JiuQian ' { $return = 9000; }
87              
88             tenthousands: 'YiWan ' { $return = 10000; } # try to find a word that representates
89             | 'ErWan ' { $return = 20000; } # values 10000,20000,..,90000
90             | 'SanWan ' { $return = 30000; }
91             | 'SiWan ' { $return = 40000; }
92             | 'WuWan ' { $return = 50000; }
93             | 'LiuWan ' { $return = 60000; }
94             | 'QiWan ' { $return = 70000; }
95             | 'BaWan ' { $return = 80000; }
96             | 'JiuWan ' { $return = 90000; }
97              
98             decade: tens(?) number(?) number(?) # try to find words that represents values
99             { $return = 0; # from 0 to 20
100             for (@item) {
101             $return += $$_[0] if (ref $_ && defined $$_[0]);
102             }
103             }
104              
105             century: hundreds(?) decade(?) # try to find words that represents values
106             { $return = 0; # from 100 to 999
107             for (@item) {
108             $return += $$_[0] if (ref $_ && defined $$_[0]);
109             }
110             }
111              
112             millenium1: thousands(1) century(?) # 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] if (ref $_ && defined $$_[0]);
117             }
118             }
119             }
120              
121             millenium2: tenthousands(1) thousands(?) century(?) decade(?) # try to find words that represents values
122             { $return = 0; # from 1.000 to 999.999
123             for (@item) {
124             if (ref $_ && defined $$_[0]) {
125             $return += $$_[0] if (ref $_ && defined $$_[0]);
126             }
127             }
128             }
129              
130             million: millenium2(?) millenium1(?) century(?) decade(?) # try to find words that represents values
131             ' Wan ' # from 1.000.000 to 999.999.999.999
132             millenium2(?) millenium1(?) century(?) decade(?)
133             { $return = 0;
134             for (@item) {
135             if (ref $_ && defined $$_[0]) {
136             $return += $$_[0];
137             } elsif ($_ eq "Wan") {
138             $return = ($return>0) ? $return * 100000 : 100000;
139             }
140             }
141             }
142             });
143             }
144              
145             # }}}
146              
147             1;
148              
149             __END__