| 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 to number conversion in English |
|
5
|
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
136481
|
use 5.16.0; |
|
|
1
|
|
|
|
|
5
|
|
|
7
|
1
|
|
|
1
|
|
8
|
use utf8; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
15
|
|
|
8
|
1
|
|
|
1
|
|
38
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
118
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# {{{ use block |
|
11
|
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
1520
|
use Parse::RecDescent; |
|
|
1
|
|
|
|
|
48499
|
|
|
|
1
|
|
|
|
|
7
|
|
|
13
|
1
|
|
|
1
|
|
574
|
use Export::Attrs; |
|
|
1
|
|
|
|
|
11797
|
|
|
|
1
|
|
|
|
|
6
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# }}} |
|
16
|
|
|
|
|
|
|
# {{{ var block |
|
17
|
|
|
|
|
|
|
our $VERSION = '0.2603300'; |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $parser = eng_numerals(); |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# }}} |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
# {{{ w2n convert text to number |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub w2n :Export { |
|
26
|
4
|
|
100
|
4
|
1
|
319580
|
my $input = shift // return; |
|
27
|
|
|
|
|
|
|
|
|
28
|
3
|
|
|
|
|
40
|
return $parser->numeral($input); |
|
29
|
1
|
|
|
1
|
|
119
|
} |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
4
|
|
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# }}} |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# {{{ eng_numerals create parser for numerals |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub eng_numerals { |
|
36
|
1
|
|
|
1
|
1
|
11
|
return Parse::RecDescent->new(q{ |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
numeral: mega |
|
40
|
|
|
|
|
|
|
| kOhOd |
|
41
|
|
|
|
|
|
|
| { } |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
number: 'twelve' { 12 } |
|
44
|
|
|
|
|
|
|
| 'thirteen' { 13 } |
|
45
|
|
|
|
|
|
|
| 'fourteen' { 14 } |
|
46
|
|
|
|
|
|
|
| 'fifteen' { 15 } |
|
47
|
|
|
|
|
|
|
| 'sixteen' { 16 } |
|
48
|
|
|
|
|
|
|
| 'seventeen' { 17 } |
|
49
|
|
|
|
|
|
|
| 'eighteen' { 18 } |
|
50
|
|
|
|
|
|
|
| 'nineteen' { 19 } |
|
51
|
|
|
|
|
|
|
| 'zero' { 0 } |
|
52
|
|
|
|
|
|
|
| 'one' { 1 } |
|
53
|
|
|
|
|
|
|
| 'two' { 2 } |
|
54
|
|
|
|
|
|
|
| 'three' { 3 } |
|
55
|
|
|
|
|
|
|
| 'four' { 4 } |
|
56
|
|
|
|
|
|
|
| 'five' { 5 } |
|
57
|
|
|
|
|
|
|
| 'six' { 6 } |
|
58
|
|
|
|
|
|
|
| 'seven' { 7 } |
|
59
|
|
|
|
|
|
|
| 'eight' { 8 } |
|
60
|
|
|
|
|
|
|
| 'nine' { 9 } |
|
61
|
|
|
|
|
|
|
| 'ten' { 10 } |
|
62
|
|
|
|
|
|
|
| 'eleven' { 11 } |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
tens: 'twenty' { 20 } |
|
65
|
|
|
|
|
|
|
| 'thirty' { 30 } |
|
66
|
|
|
|
|
|
|
| 'forty' { 40 } |
|
67
|
|
|
|
|
|
|
| 'fifty' { 50 } |
|
68
|
|
|
|
|
|
|
| 'sixty' { 60 } |
|
69
|
|
|
|
|
|
|
| 'seventy' { 70 } |
|
70
|
|
|
|
|
|
|
| 'eighty' { 80 } |
|
71
|
|
|
|
|
|
|
| 'ninety' { 90 } |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
deca: tens /(-|\s)?/ number { $item[1] + $item[3] } |
|
74
|
|
|
|
|
|
|
| tens |
|
75
|
|
|
|
|
|
|
| number |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
hecto: number 'hundred' deca { $item[1] * 100 + $item[3] } |
|
78
|
|
|
|
|
|
|
| number 'hundred' { $item[1] * 100 } |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
hOd: hecto |
|
81
|
|
|
|
|
|
|
| deca |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
kilo: hOd /thousand,?/ hOd { $item[1] * 1000 + $item[3] } |
|
84
|
|
|
|
|
|
|
| hOd /thousand,?/ { $item[1] * 1000 } |
|
85
|
|
|
|
|
|
|
| /thousand,?/ hOd { 1000 + $item[2] } |
|
86
|
|
|
|
|
|
|
| /thousand,?/ { 1000 } |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
kOhOd: kilo |
|
89
|
|
|
|
|
|
|
| hOd |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
mega: hOd /millions?,?/ kOhOd { $item[1] * 1_000_000 + $item[3] } |
|
92
|
|
|
|
|
|
|
| hOd /millions?,?/ { $item[1] * 1_000_000 } |
|
93
|
|
|
|
|
|
|
| 'million' kOhOd { 1_000_000 + $item[2] } |
|
94
|
|
|
|
|
|
|
| 'million' { 1_000_000 } |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
}); |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# }}} |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# {{{ ordinal2cardinal convert ordinal text to cardinal text |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub ordinal2cardinal :Export { |
|
104
|
0
|
|
0
|
0
|
1
|
|
my $input = shift // return; |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# English ordinals: irregulars, -ieth (tens), regular -th |
|
107
|
|
|
|
|
|
|
# For compounds like "twenty-first", only the last element is ordinal. |
|
108
|
|
|
|
|
|
|
|
|
109
|
0
|
|
|
|
|
|
my %irregular = ( |
|
110
|
|
|
|
|
|
|
'first' => 'one', |
|
111
|
|
|
|
|
|
|
'second' => 'two', |
|
112
|
|
|
|
|
|
|
'third' => 'three', |
|
113
|
|
|
|
|
|
|
'fifth' => 'five', |
|
114
|
|
|
|
|
|
|
'eighth' => 'eight', |
|
115
|
|
|
|
|
|
|
'ninth' => 'nine', |
|
116
|
|
|
|
|
|
|
'twelfth' => 'twelve', |
|
117
|
|
|
|
|
|
|
); |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# Compound: "twenty-first" or "twenty first" → convert last word only |
|
120
|
|
|
|
|
|
|
# Num2Word produces space-separated: "twenty first", "one hundred third" |
|
121
|
0
|
0
|
|
|
|
|
if ($input =~ m{\A (?.+) [-\s] (?\S+) \z}xms) { |
|
122
|
0
|
|
|
|
|
|
my $prefix = $+{prefix}; |
|
123
|
0
|
|
|
|
|
|
my $last = $+{last}; |
|
124
|
0
|
0
|
|
|
|
|
my $sep = ($input =~ m{-}) ? '-' : ' '; |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
# Convert the last (ordinal) element to cardinal |
|
127
|
0
|
|
0
|
|
|
|
my $cardinal = ordinal2cardinal($last) // return; |
|
128
|
0
|
|
|
|
|
|
return "${prefix}${sep}${cardinal}"; |
|
129
|
|
|
|
|
|
|
} |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# Irregular standalone |
|
132
|
0
|
0
|
|
|
|
|
return $irregular{$input} if exists $irregular{$input}; |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# Tens ending in -ieth: twentieth→twenty, thirtieth→thirty, etc. |
|
135
|
0
|
0
|
|
|
|
|
if ($input =~ s{ieth\z}{y}xms) { |
|
136
|
0
|
|
|
|
|
|
return $input; |
|
137
|
|
|
|
|
|
|
} |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
# Regular -th: fourteenth→fourteen, hundredth→hundred, etc. |
|
140
|
0
|
0
|
|
|
|
|
if ($input =~ s{th\z}{}xms) { |
|
141
|
0
|
|
|
|
|
|
return $input; |
|
142
|
|
|
|
|
|
|
} |
|
143
|
|
|
|
|
|
|
|
|
144
|
0
|
|
|
|
|
|
return; # not an ordinal |
|
145
|
1
|
|
|
1
|
|
623
|
} |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
6
|
|
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# }}} |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
1; |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
__END__ |