blib/lib/Lingua/EN/Numericalize.pm

filter: Statementstmt 100% 82 / 82 Branchbran 90.9% 40 / 44 Conditioncond 52.3% 11 / 21 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 10 / 10 Podpod 16.6% 1 / 6 totaltotal 84.3% 146 / 173 CC 41 SCAR 21.5
CC
41
Cov
90%
str2nbr
15.0
word2num
8.2
compound
8.2
CRAP 8.6
↑ Summary
1 #!/usr/bin/perl
2  
3 =head1 NAME
4  
5 Lingua::EN::Numericalize - Replaces English descriptions of numbers with numerals
6  
7 =cut
8  
9 package Lingua::EN::Numericalize;
10  
11 2 require Exporter;
12 2 our @ISA = qw/Exporter/;
13 2 our @EXPORT = qw/&str2nbr/;
14 2 our $VERSION = substr q$Revision: 1.52 $, 10;
15  
16 2 local $\ = $/;
17 2 our $debug = 0;
18 2 our $UK = 0;
19  
20 =head1 SYNOPSIS
21  
22 use Lingua::EN::Numericalize;
23 print str2nbr("one thousand maniacs");
24  
25 $_ = "six hundred three-score and six";
26 str2nbr();
27 print;
28  
29 $Lingua::EN::Numericalize::UK = 1;
30 print str2nbr("one billion"); # 1,000,000,000,000
31  
32 =head1 DESCRIPTION
33  
34 This module interpolates English descriptions of numbers in a given string with their numeric counterparts. It supports both ordinal and cardinal numbers, negative numbers, and very large numbers.
35  
36 The module exports a single function into the caller's namespace as follows:
37  
38 =over
39  
40 =item B<str2nbr [string = $_]>
41  
42 This function receives an optional string (using $_ if none is passed) and converts all English text that describes a number into its numeric equivalent. When called in a void context, the function sets $_ to the new value.
43  
44 =back
45  
46 The module's behaviour is affected by the following variables:
47  
48 =over
49  
50 =cut
51  
52 101 ▶sub str2nbr {
Subroutine str2nbr: called CC=15 SCAR=27.1
Pod documented
53 101 my $s = lc(shift);
54 101 ▶ local $_ if wantarray();
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 101 101
55
56 101 $s =~ s/$_/$strrep{$_}/eeg for keys %strrep;
57  
58 101 my @ret;
59 101 for (split /\b/, $s) {
60 355 ▶ push(@ret, $_), next if /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\d+$/ 23 332 355
61 332 ▶ push(@ret, $_), next if /[^a-zA-Z0-9]/;
Branch: 1 branch
BranchTrueFalseTotal
if /[^a-zA-Z0-9]/ 128 204 332
62 204 push(@ret, word2num());
63 }
64  
65 # generate number sequences
66  
67 101 my $i = 0;
68 101 ▶ while ($i < $#ret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#ret) 259 101 360
69 259 ▶ $ret[$i] = [ $ret[$i] ], $n = 1 if isnbr($ret[$i]);
Branch: 1 branch
BranchTrueFalseTotal
if isnbr($ret[$i]) 42 217 259
70 259 ▶ if (ref($ret[$i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ret[$i]) 248 11 259
71 248 my $next = $ret[$i + 1];
72 248 ▶ if (isnbr($next)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isnbr($next)) 113 135 248
73 113 push @{$ret[$i]}, $next;
74 113 splice(@ret, $i + 1, 1);
75 113 next;
76 }
77 135 my $nexxt = $ret[$i + 2];
78 135 ▶ if (isconj($next) && (isnbr($nexxt) || isconj($nexxt))) {
Branch: 1 branch
BranchTrueFalseTotal
if (isconj($next) and isnbr($nexxt) || isconj($nexxt)) 132 3 135
Condition66%4/6
isconj($next) and isnbr($nexxt) || isconj($nexxt)
!ll&&!rl&&r
30132
isnbr($nexxt) or isconj($nexxt)
l!l&&r!l&&!r
108240
MC/DC33%1/3
isconj($next) and isnbr($nexxt) || isconj($nexxt)
isconj($next) isnbr($nexxt) isconj($nexxt)
Truth table75%3/4
A and B or C
A= isconj($next)B= isnbr($nexxt)C= isconj($nexxt)
ABCresult
0XX0
1000
11X1
1011
79 132 splice(@ret, $i + 1, 1);
80 132 next;
81 }
82 }
83 14 $i++;
84 }
85  
86 # calculate sequences
87  
88 101 ▶ ref && ($_ = seq2int(@$_)) for @ret;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ and $_ = seq2int(@$_) 42 73 115
89
90 101 $_ = join "", @ret;
91 }
92  
93 =item B<$Lingua::EN::Numericalize::UK>
94  
95 This variable may be set to indicate that the UK meaning of C<billion> should be used. By default, this module uses the American meaning of this word :( Please note that all the related larger numbers e.g. trillion, quadrillion, etc. assume the chosen behaviour as well.
96  
97 =item B<$Lingua::EN::Numericalize::debug>
98  
99 If set to true, the module outputs on standard error messages useful for debugging.
100  
101 =back
102  
103 =cut
104  
105 639 ▶sub isnbr {
Subroutine isnbr: called CC=3 SCAR=11.0
Pod undocumented
106 639 ▶ ! /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ && return for @_;
Branch: 1 branch
BranchTrueFalseTotal
/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ or return 263 376 639
107 263 return 1;
108 }
109  
110 159 ▶sub isconj {
Subroutine isconj: called CC=4 SCAR=14.8
Pod undocumented
111 159 ▶ my $w = shift || $_;
Condition33%1/3
shift() || $_
l!l&&r!l&&!r
15900
MC/DC0%0/2
shift() || $_
shift() $_
Truth table33%1/3
A || B
A= shift()B= $_
ABresult
1X1
011
000
112 159 ▶ $w =~ /$_/ && return 1 for @conj;
Branch: 1 branch
BranchTrueFalseTotal
$w =~ /$_/ and return 1 156 298 454
113 }
114
115 # splits "fourtytwo", "onehundred", etc.
116  
117 204 ▶sub compound {
Subroutine compound: called CC=8 SCAR=21.0
Pod undocumented
118 204 ▶ my $w = shift || $_ || return;
Condition40%2/5
shift() || $_ || (return)
l!l
2040
shift() or $_
l!l&&r!l&&!r
20400
MC/DC0%0/2
shift() || $_ || (return)
shift() $_
Truth table33%1/3
A or B
A= shift()B= $_
ABresult
1X1
011
000
119
120 204 $w =~ s/(\d)$_$/$1$abb{$_}/ for keys %abb;
121  
122 204 my @ret; my @w2n = keys %word2num;
123 204 ▶ for (my $i = 0; $i < @w2n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @w2n) 8746 73 8819
124 8746 ▶ push(@ret, $word2num{$w2n[$i]}), $i = 0
Branch: 1 branch
BranchTrueFalseTotal
if $w =~ s/$w2n[$i]$// 136 8610 8746
125 if $w =~ s/$w2n[$i]$//;
126 8746 ▶ last unless $w;
Branch: 1 branch
BranchTrueFalseTotal
unless $w 131 8615 8746
127 }
128 204 ▶ push @ret, $w if $w;
Branch: 1 branch
BranchTrueFalseTotal
if $w 73 131 204
129 204 reverse @ret;
130 }
131  
132 204 ▶sub word2num {
Subroutine word2num: called CC=8 SCAR=21.1
Pod undocumented
133 204 ▶ my $w = shift || $_ || return;
Condition40%2/5
shift() || $_ || (return)
l!l
2040
shift() or $_
l!l&&r!l&&!r
02040
MC/DC0%0/2
shift() || $_ || (return)
shift() $_
Truth table33%1/3
A or B
A= shift()B= $_
ABresult
1X1
011
000
134  
135 $w =~ s/$_/$tokrep{$_}/g
136 204 for keys %tokrep;
137  
138 204 my @ret;
139 204 for $w (compound($w)) {
140 209 my $o = $w;
141 209 for (keys %suffix) {
142 714 ▶ my ($m) = $w =~ /(.*)$_$/; $m ||= "";
Condition100%2/2
$m ||= ""
l!l
54660
MC/DC100%1/1
$m ||= ""
$m
Truth table100%2/2
A
A= $m
Aresult
11
00
143 $w = $suffix{$_}->($word2num{$m}), last
144 714 ▶ if $word2num{$m};
Branch: 1 branch
BranchTrueFalseTotal
if $word2num{$m} 53 661 714
145 }
146 209 push @ret, $w;
147 }
148  
149 204 @ret;
150 }
151  
152 142 ▶sub seq2int {
Subroutine seq2int: called CC=6 SCAR=17.9
Pod undocumented
153 142 my @seq = @_;
154 142 ▶ print "seq2int(): ", join "-", @seq if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 142 142
155 142 my ($i, $max) = (0) x 2;
156 142 ▶ ($max < $seq[$_]) && ($max = $seq[$_], $i = $_) for 0 .. $#seq;
Branch: 1 branch
BranchTrueFalseTotal
$max < $seq[$_] and $max = $seq[$_], $i = $_ 183 129 312
157 142 ▶ if ($i == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0) 92 50 142
158 92 my $ret = 0;
159 92 $ret += $_ for @seq;
160 92 return $ret;
161 }
162 50 $seq[$i] * seq2int(@seq[0 .. $i - 1]) + seq2int(@seq[$i + 1 .. $#seq]);
163 }
164  
165 # conjunctions are valid separators for text numbers
166  
167 2 our @conj = ('and', 'of', '\s+', '-', ',');
168  
169 # abbreviations
170  
171 2 ▶our %abb = (
Branch: 1 branch
BranchTrueFalseTotal
$UK ? : 0 2 2
172 k => "0" x 3,
173 m => "0" x 6,
174 b => "0" x ($UK ? 12 : 9),
175 );
176  
177 2 our %strrep = (
178 'milion' => q/"million"/, # common mispelling
179 '(\d)\s*,\s*(\d)' => q/"$1$2"/, # commas in numbers ok to remove
180 q/baker('?s)?(\s+)?dozen/ => q/"baker"/, # colloquialism
181 '(\d)(st|nd|rd|th)' => q/"$1"/,
182 );
183  
184 2 our %tokrep = (
185 'th$' => "", # cardinals
186 '(s?e)?s$' => "", # pluralis
187 'tie' => "ty", # four[tie]th
188 );
189  
190 our %suffix = (
191 2 ▶ teen => sub { 10 + shift },
Subroutine __ANON__: called CC=1 SCAR=0.0
192 30 ▶ ty => sub { 10 * shift },
Subroutine __ANON__: called CC=1 SCAR=0.0
193 1 ▶ illiard => sub { 10 ** (9 + 6 * (shift() - 1)) },
Subroutine __ANON__: called CC=1 SCAR=0.0
194 20 ▶ illion => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
195 20 my $k = shift;
196 20 ▶ return 1e6 if $k == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $k == 1 7 13 20
197 13 ▶ my $n = $UK ? 6 * $k : 3 * ($k - 1);
Branch: 1 branch
BranchTrueFalseTotal
$UK ? : 0 13 13
198 13 10 ** ($n + 6);
199 },
200 2 );
201  
202 2 our %latin = (
203 un => 1,
204 duo => 2,
205 tre => 3, tr => 3,
206 quattuor => 4, quadr => 4,
207 quin => 5, quint => 5,
208 sex => 6, sext => 6,
209 septen => 7, sept => 7,
210 octo => 8, oct => 8,
211 novem => 9, non => 9,
212 dec => 10,
213 undec => 11,
214 duodec => 12,
215 tredec => 13,
216 quattuordec => 14,
217 quindec => 15,
218 hex => 16,
219 vigint => 20, vig => 20,
220 trig => 30,
221 cent => 100,
222 );
223
224 2 our %word2num = (
225 naught => 0,
226 first => 1,
227 second => 2,
228 third => 3,
229 zero => 0,
230 one => 1,
231 two => 2,
232 three => 3, thir => 3,
233 four => 4, for => 4,
234 five => 5, fif => 5,
235 six => 6,
236 seven => 7,
237 eight => 8, eigh => 8,
238 nine => 9, nin => 9,
239 ten => 10,
240 eleven => 11,
241 twelve => 12, twelf => 12,
242 twen => 2,
243 hundred => 100,
244 thousand => 1000,
245  
246 m => 1, # million/milliard
247 b => 2, # billion
248
249 googol => 10 ** 100,
250 googolplex => 10 ** (10 ** 100),
251 score => 20,
252 gros => 12 * 12, # gross
253 dozen => 12,
254 baker => 13,
255 eleventyone => 111,
256 eleventyfirst => 111,
257 );
258  
259 2 %word2num = (%word2num, %latin);
260  
261 2 1;
262  
263 __END__
↑ Summary