| 1 | package Parse::Number::EN; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | our $DATE = '2016-06-14'; # DATE | |||||||||
| 4 | 2 | our $VERSION = '0.07'; # VERSION | |||||||||
| 5 | |||||||||||
| 6 | # TODO: make it OO and customize thousand sep & decimal point | ||||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use 5.010001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 2 | ▶ | use Exporter qw(import); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 2 | our @EXPORT_OK = qw($Pat parse_number_en); | |||||||||
| 14 | |||||||||||
| 15 | 2 | our %SPEC; | |||||||||
| 16 | |||||||||||
| 17 | #our $Pat = qr/(?: | ||||||||||
| 18 | # [+-]? | ||||||||||
| 19 | # (?: | ||||||||||
| 20 | # (?:\d{1,3}(?:[,]\d{3})+ | \d+) (?:[.]\d*)? | # english | ||||||||||
| 21 | # [.]\d+ | ||||||||||
| 22 | # ) | ||||||||||
| 23 | # (?:[Ee][+-]?\d+)? | ||||||||||
| 24 | # )/x; | ||||||||||
| 25 | |||||||||||
| 26 | # non /x version | ||||||||||
| 27 | 2 | our $Pat = '(?:[+-]?(?:(?:\d{1,3}(?:[,]\d{3})+|\d+)(?:[.]\d*)?|[.]\d+)(?:[Ee][+-]?\d+)?)'; | |||||||||
| 28 | |||||||||||
| 29 | $SPEC{parse_number_en} = { | ||||||||||
| 30 | 2 | v => 1.1, | |||||||||
| 31 | summary => 'Parse number from English text', | ||||||||||
| 32 | description => <<'_', | ||||||||||
| 33 | |||||||||||
| 34 | This function can parse number with thousand separators (e.g. 10,000). | ||||||||||
| 35 | |||||||||||
| 36 | In the future percentage (e.g. 10.2%) and fractions (e.g. 1/3, 2 1/2) might also | ||||||||||
| 37 | be supported. | ||||||||||
| 38 | |||||||||||
| 39 | _ | ||||||||||
| 40 | args => { | ||||||||||
| 41 | text => { | ||||||||||
| 42 | summary => 'The input text that contains number', | ||||||||||
| 43 | schema => 'str*', | ||||||||||
| 44 | req => 1, | ||||||||||
| 45 | pos => 0, | ||||||||||
| 46 | }, | ||||||||||
| 47 | }, | ||||||||||
| 48 | result_naked => 1, | ||||||||||
| 49 | }; | ||||||||||
| 50 | 30 | ▶ | sub parse_number_en { | ||||||||
|
Subroutine
parse_number_en: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 51 | 30 | my %args = @_; | |||||||||
| 52 | 30 | my $text = $args{text}; | |||||||||
| 53 | |||||||||||
| 54 | 30 | ▶ | return undef unless $text =~ s/^\s*($Pat)//s; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 24 | my $n = $1; | |||||||||
| 56 | 24 | $n =~ s/,//g; | |||||||||
| 57 | 24 | $n+0; | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 2 | 1; | |||||||||
| 61 | # ABSTRACT: Parse number from English text | ||||||||||
| 62 | |||||||||||
| 63 | __END__ | ||||||||||