| 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 | ▶ | 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 Parse::RecDescent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 1 | ▶ | use Export::Attrs; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | # }}} | ||||||||||||
| 16 | # {{{ var block | ||||||||||||
| 17 | 1 | our $VERSION = '0.2603270'; | |||||||||||
| 18 | |||||||||||||
| 19 | 1 | my $parser = eng_numerals(); | |||||||||||
| 20 | |||||||||||||
| 21 | # }}} | ||||||||||||
| 22 | |||||||||||||
| 23 | # {{{ w2n convert text to number | ||||||||||||
| 24 | |||||||||||||
| 25 | sub w2n :Export { | ||||||||||||
| 26 | 4 | ▶ | my $input = shift // return; | ||||||||||
|
Subroutine
w2n: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
shift() // (return)
MC/DC100%1/1
shift() // (return)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 27 | |||||||||||||
| 28 | 3 | return $parser->numeral($input); | |||||||||||
| 29 | 1 | ▶ | } | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 30 | |||||||||||||
| 31 | # }}} | ||||||||||||
| 32 | |||||||||||||
| 33 | # {{{ eng_numerals create parser for numerals | ||||||||||||
| 34 | |||||||||||||
| 35 | sub eng_numerals { | ||||||||||||
| 36 | 1 | ▶ | return Parse::RecDescent->new(q{ | ||||||||||
|
Subroutine
eng_numerals: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 37 | <autoaction: { $item[1] } > | ||||||||||||
| 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 | 1 | 1; | |||||||||||
| 102 | |||||||||||||
| 103 | __END__ | ||||||||||||