| 1 | package Lingua::PT::Ords2Nums; | ||||||||||||
| 2 | |||||||||||||
| 3 | 3 | ▶ | use 5.006; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 3 | ▶ | use Lingua::PT::Words2Nums qw/word2num/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | 3 | require Exporter; | |||||||||||
| 10 | |||||||||||||
| 11 | 3 | our @ISA = qw(Exporter); | |||||||||||
| 12 | |||||||||||||
| 13 | 3 | our %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||||
| 14 | ord2num | ||||||||||||
| 15 | ) ] ); | ||||||||||||
| 16 | |||||||||||||
| 17 | 3 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||
| 18 | |||||||||||||
| 19 | 3 | our @EXPORT = qw( | |||||||||||
| 20 | ); | ||||||||||||
| 21 | |||||||||||||
| 22 | 3 | our $VERSION = '0.08'; | |||||||||||
| 23 | |||||||||||||
| 24 | 3 | my %values = ( | |||||||||||
| 25 | 'bilionésimo' => 1000000000, | ||||||||||||
| 26 | 'milionésimo' => 1000000, | ||||||||||||
| 27 | 'milésimo' => 1000, | ||||||||||||
| 28 | |||||||||||||
| 29 | 'nongentésimo' => 900, | ||||||||||||
| 30 | 'octigentésimo' => 800, | ||||||||||||
| 31 | 'septigentésimo' => 700, | ||||||||||||
| 32 | 'seiscentésimo' => 600, | ||||||||||||
| 33 | 'quingentésimo' => 500, | ||||||||||||
| 34 | 'quadrigentésimo' => 400, | ||||||||||||
| 35 | 'tricentésimo' => 300, | ||||||||||||
| 36 | 'ducentésimo' => 200, | ||||||||||||
| 37 | 'centésimo' => 100, | ||||||||||||
| 38 | |||||||||||||
| 39 | 'nonagésimo' => 90, | ||||||||||||
| 40 | 'octogésimo' => 80, | ||||||||||||
| 41 | 'septuagésimo' => 70, | ||||||||||||
| 42 | 'sexagésimo' => 60, | ||||||||||||
| 43 | 'quinquagésimo' => 50, | ||||||||||||
| 44 | 'quadragésimo' => 40, | ||||||||||||
| 45 | 'trigésimo' => 30, | ||||||||||||
| 46 | 'vigésimo' => 20, | ||||||||||||
| 47 | 'décimo' => 10, | ||||||||||||
| 48 | |||||||||||||
| 49 | 'nono' => 9, | ||||||||||||
| 50 | 'oitavo' => 8, | ||||||||||||
| 51 | 'sétimo' => 7, | ||||||||||||
| 52 | 'sexto' => 6, | ||||||||||||
| 53 | 'quinto' => 5, | ||||||||||||
| 54 | 'quarto' => 4, | ||||||||||||
| 55 | 'terceiro' => 3, | ||||||||||||
| 56 | 'segundo' => 2, | ||||||||||||
| 57 | 'primeiro' => 1, | ||||||||||||
| 58 | ); | ||||||||||||
| 59 | |||||||||||||
| 60 | # Order of application is important, but hashes are unsorted. | ||||||||||||
| 61 | # So we need to store the order of the keys in an array. | ||||||||||||
| 62 | 3 | my @key_sorted_order = ( | |||||||||||
| 63 | 'bilionésimo',# => 1000000000, | ||||||||||||
| 64 | 'milionésimo',# => 1000000, | ||||||||||||
| 65 | 'milésimo',# => 1000, | ||||||||||||
| 66 | |||||||||||||
| 67 | 'nongentésimo',# => 900, | ||||||||||||
| 68 | 'octigentésimo',# => 800, | ||||||||||||
| 69 | 'septigentésimo',# => 700, | ||||||||||||
| 70 | 'seiscentésimo',# => 600, | ||||||||||||
| 71 | 'quingentésimo',# => 500, | ||||||||||||
| 72 | 'quadrigentésimo',# => 400, | ||||||||||||
| 73 | 'tricentésimo',# => 300, | ||||||||||||
| 74 | 'ducentésimo',# => 200, | ||||||||||||
| 75 | 'centésimo',# => 100, | ||||||||||||
| 76 | |||||||||||||
| 77 | 'nonagésimo',# => 90, | ||||||||||||
| 78 | 'octogésimo',# => 80, | ||||||||||||
| 79 | 'septuagésimo',# => 70, | ||||||||||||
| 80 | 'sexagésimo',# => 60, | ||||||||||||
| 81 | 'quinquagésimo',# => 50, | ||||||||||||
| 82 | 'quadragésimo',# => 40, | ||||||||||||
| 83 | 'trigésimo',# => 30, | ||||||||||||
| 84 | 'vigésimo',# => 20, | ||||||||||||
| 85 | 'décimo',# => 10, | ||||||||||||
| 86 | |||||||||||||
| 87 | 'nono',# => 9, | ||||||||||||
| 88 | 'oitavo',# => 8, | ||||||||||||
| 89 | 'sétimo',# => 7, | ||||||||||||
| 90 | 'sexto',# => 6, | ||||||||||||
| 91 | 'quinto',# => 5, | ||||||||||||
| 92 | 'quarto',# => 4, | ||||||||||||
| 93 | 'terceiro',# => 3, | ||||||||||||
| 94 | 'segundo',# => 2, | ||||||||||||
| 95 | 'primeiro',# => 1, | ||||||||||||
| 96 | ); | ||||||||||||
| 97 | |||||||||||||
| 98 | |||||||||||||
| 99 | =head1 NAME | ||||||||||||
| 100 | |||||||||||||
| 101 | =encoding latin1 | ||||||||||||
| 102 | |||||||||||||
| 103 | Lingua::PT::Ords2Nums - Converts Portuguese ordinals to numbers | ||||||||||||
| 104 | |||||||||||||
| 105 | =head1 SYNOPSIS | ||||||||||||
| 106 | |||||||||||||
| 107 | use Lingua::PT::Ords2Nums qw/ord2num/; | ||||||||||||
| 108 | |||||||||||||
| 109 | $num = word2num('décimo primeiro') # 11 | ||||||||||||
| 110 | |||||||||||||
| 111 | =head1 DESCRIPTION | ||||||||||||
| 112 | |||||||||||||
| 113 | Converts Portuguese ordinals to numbers. Works up to 999.999.999.999 | ||||||||||||
| 114 | ('novecentos e noventa e nove bilionésimos novecentos e noventa e nove | ||||||||||||
| 115 | milionésimos novecentos e noventa e nove milésimos nongentésimo nonagésimo | ||||||||||||
| 116 | nono'). | ||||||||||||
| 117 | |||||||||||||
| 118 | =cut | ||||||||||||
| 119 | |||||||||||||
| 120 | =head2 ord2num | ||||||||||||
| 121 | |||||||||||||
| 122 | Turns an ordinal number into a regular number (decimal). | ||||||||||||
| 123 | |||||||||||||
| 124 | $num = word2num('segundo') | ||||||||||||
| 125 | # $num now holds 2 | ||||||||||||
| 126 | |||||||||||||
| 127 | =cut | ||||||||||||
| 128 | |||||||||||||
| 129 | 50 | ▶ | sub ord2num { | ||||||||||
|
Subroutine
ord2num: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 130 | 50 | ▶ | $_ = shift || return undef; | ||||||||||
|
Condition100%2/2
shift() || (return undef)
MC/DC100%1/1
shift() || (return undef)
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 131 | 49 | my $result = 0; | |||||||||||
| 132 | |||||||||||||
| 133 | 49 | s/(.*)bilionésimos/$result += (word2num($1) * 1000000000)/e; | |||||||||||
| 134 | 49 | s/(.*)milionésimos/$result += (word2num($1) * 1000000)/e; | |||||||||||
| 135 | 49 | s/(.*)milésimos/$result += (word2num($1) * 1000)/e; | |||||||||||
| 136 | |||||||||||||
| 137 | 49 | for my $value ( @key_sorted_order ) { | |||||||||||
| 138 | 1470 | s/$value/$result += $values{$value}/e; | |||||||||||
| 139 | } | ||||||||||||
| 140 | |||||||||||||
| 141 | 49 | $result; | |||||||||||
| 142 | } | ||||||||||||
| 143 | |||||||||||||
| 144 | 3 | 1; | |||||||||||
| 145 | __END__ | ||||||||||||