blib/lib/Lingua/ZHO/Word2Num.pm

filter: Statementstmt 100% 25 / 25 Branchbran 100% 2 / 2 Conditioncond 85.7% 6 / 7 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 95.8% 46 / 48 CC 5 SCAR 9.8
CC
5
Cov
97%
w2n
5.0
BEGIN
1.0
BEGIN
1.0
CRAP 2.7
↑ Summary
1 # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8; -*-
2  
3 package Lingua::ZHO::Word2Num;
4 # ABSTRACT: Word to number conversion in Chinese
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 Parse::RecDescent;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # }}}
16 # {{{ variable declarations
17 1 our $VERSION = '0.2603260';
18 1 my $parser = zho_numerals();
19  
20 # }}}
21  
22 # {{{ w2n convert text to number
23  
24 sub w2n :Export {
25 4 ▶ my $input = shift // return;
Subroutine w2n: called CC=5 SCAR=16.1
Pod documented
Condition100%2/2
shift() // (return)
l!l
31
MC/DC100%1/1
shift() // (return)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
26  
27 3 ▶ return 0 if ($input =~ m{\b(nul|ling)\b}xmsi || $input =~ m{\A零\z}xms);
Branch: 1 branch
BranchTrueFalseTotal
if $input =~ /\b(nul|ling)\b/imsux or $input =~ /\A\x{96f6}\z/msux 1 2 3
Condition66%2/3
$input =~ /\b(nul|ling)\b/imsux or $input =~ /\A\x{96f6}\z/msux
l!l&&r!l&&!r
102
MC/DC50%1/2
$input =~ /\b(nul|ling)\b/imsux or $input =~ /\A\x{96f6}\z/msux
$input =~ /\b(nul|ling)\b/imsux $input =~ /\A\x{96f6}\z/msux
Truth table66%2/3
A or B
A= $input =~ /\b(nul|ling)\b/imsuxB= $input =~ /\A\x{96f6}\z/msux
ABresult
1X1
011
000
28  
29 2 $input .= " "; # Grant space at the end
30  
31 2 ▶ return $parser->numeral($input) || undef;
Condition100%2/2
$parser->numeral($input) || undef
l!l
11
MC/DC100%1/1
$parser->numeral($input) || undef
$parser->numeral($input)
Truth table100%2/2
A
A= $parser->numeral($input)
Aresult
11
00
32 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # }}}
35 # {{{ zho_numerals create parser for numerals
36  
37 sub zho_numerals {
38 1 ▶ return Parse::RecDescent->new(q{
Subroutine zho_numerals: called CC=1 SCAR=0.0
Pod documented
39 numeral: <rulevar: local $number = 0>
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; }
54 | 'Qi ' { $return = 7; }
55 | 'Ba ' { $return = 8; }
56 | 'Jiu ' { $return = 9; }
57 | 'Shi ' { $return = 10; }
58 | /零/ { $return = 0; }
59 | /一/ { $return = 1; }
60 | /二/ { $return = 2; }
61 | /三/ { $return = 3; }
62 | /四/ { $return = 4; }
63 | /五/ { $return = 5; }
64 | /六/ { $return = 6; }
65 | /七/ { $return = 7; }
66 | /八/ { $return = 8; }
67 | /九/ { $return = 9; }
68 | /十/ { $return = 10; }
69  
70 tens: 'YiShi ' { $return = 10; } # try to find a word that represents
71 | 'ErShi ' { $return = 20; } # values 20,30,..,90
72 | 'SanShi ' { $return = 30; }
73 | 'SiShi ' { $return = 40; }
74 | 'WuShi ' { $return = 50; }
75 | 'LiuShi ' { $return = 60; }
76 | 'QiShi ' { $return = 70; }
77 | 'BaShi ' { $return = 80; }
78 | 'JiuShi ' { $return = 90; }
79 | /一十/ { $return = 10; }
80 | /二十/ { $return = 20; }
81 | /三十/ { $return = 30; }
82 | /四十/ { $return = 40; }
83 | /五十/ { $return = 50; }
84 | /六十/ { $return = 60; }
85 | /七十/ { $return = 70; }
86 | /八十/ { $return = 80; }
87 | /九十/ { $return = 90; }
88  
89 hundreds: 'YiBai ' { $return = 100; } # try to find a word that represents
90 | 'ErBai ' { $return = 200; } # values 100,200,..,900
91 | 'SanBai ' { $return = 300; }
92 | 'SiBai ' { $return = 400; }
93 | 'WuBai ' { $return = 500; }
94 | 'LiuBai ' { $return = 600; }
95 | 'QiBai ' { $return = 700; }
96 | 'BaBai ' { $return = 800; }
97 | 'JiuBai ' { $return = 900; }
98 | /一百/ { $return = 100; }
99 | /二百/ { $return = 200; }
100 | /三百/ { $return = 300; }
101 | /四百/ { $return = 400; }
102 | /五百/ { $return = 500; }
103 | /六百/ { $return = 600; }
104 | /七百/ { $return = 700; }
105 | /八百/ { $return = 800; }
106 | /九百/ { $return = 900; }
107  
108 thousands: 'YiQian ' { $return = 1000; } # try to find a word that represents
109 | 'ErQian ' { $return = 2000; } # values 1000,2000,..,9000
110 | 'SanQian ' { $return = 3000; }
111 | 'SiQian ' { $return = 4000; }
112 | 'WuQian ' { $return = 5000; }
113 | 'LiuQian ' { $return = 6000; }
114 | 'QiQian ' { $return = 7000; }
115 | 'BaQian ' { $return = 8000; }
116 | 'JiuQian ' { $return = 9000; }
117 | /一千/ { $return = 1000; }
118 | /二千/ { $return = 2000; }
119 | /三千/ { $return = 3000; }
120 | /四千/ { $return = 4000; }
121 | /五千/ { $return = 5000; }
122 | /六千/ { $return = 6000; }
123 | /七千/ { $return = 7000; }
124 | /八千/ { $return = 8000; }
125 | /九千/ { $return = 9000; }
126  
127 tenthousands: 'YiWan ' { $return = 10000; } # try to find a word that represents
128 | 'ErWan ' { $return = 20000; } # values 10000,20000,..,90000
129 | 'SanWan ' { $return = 30000; }
130 | 'SiWan ' { $return = 40000; }
131 | 'WuWan ' { $return = 50000; }
132 | 'LiuWan ' { $return = 60000; }
133 | 'QiWan ' { $return = 70000; }
134 | 'BaWan ' { $return = 80000; }
135 | 'JiuWan ' { $return = 90000; }
136 | /一萬/ { $return = 10000; }
137 | /二萬/ { $return = 20000; }
138 | /三萬/ { $return = 30000; }
139 | /四萬/ { $return = 40000; }
140 | /五萬/ { $return = 50000; }
141 | /六萬/ { $return = 60000; }
142 | /七萬/ { $return = 70000; }
143 | /八萬/ { $return = 80000; }
144 | /九萬/ { $return = 90000; }
145  
146 decade: tens(?) number(?) number(?) # try to find words that represents values
147 { $return = 0; # from 0 to 20
148 for (@item) {
149 $return += $$_[0] if (ref $_ && defined $$_[0]);
150 }
151 }
152  
153 century: hundreds(?) decade(?) # try to find words that represents values
154 { $return = 0; # from 100 to 999
155 for (@item) {
156 $return += $$_[0] if (ref $_ && defined $$_[0]);
157 }
158 }
159  
160 millenium1: thousands(1) century(?) # try to find words that represents values
161 { $return = 0; # from 1.000 to 999.999
162 for (@item) {
163 if (ref $_ && defined $$_[0]) {
164 $return += $$_[0] if (ref $_ && defined $$_[0]);
165 }
166 }
167 }
168  
169 millenium2: tenthousands(1) thousands(?) century(?) decade(?) # try to find words that represents values
170 { $return = 0; # from 1.000 to 999.999
171 for (@item) {
172 if (ref $_ && defined $$_[0]) {
173 $return += $$_[0] if (ref $_ && defined $$_[0]);
174 }
175 }
176 }
177  
178 million: millenium2(?) millenium1(?) century(?) decade(?) # try to find words that represents values
179 wanmark # from 1.000.000 to 999.999.999.999
180 millenium2(?) millenium1(?) century(?) decade(?)
181 { $return = 0;
182 for (@item) {
183 if (ref $_ && defined $$_[0]) {
184 $return += $$_[0];
185 } elsif ($_ eq "Wan") {
186 $return = ($return>0) ? $return * 100000 : 100000;
187 }
188 }
189 }
190  
191 wanmark: ' Wan ' { $return = "Wan"; }
192 | /萬/ { $return = "Wan"; }
193 });
194 }
195  
196 # }}}
197  
198 1 1;
199  
200 __END__
↑ Summary