blib/lib/Lingua/PT/Words2Nums.pm

filter: Statementstmt 100% 48 / 48 Branchbran 100% 8 / 8 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 96.9% 63 / 65 CC 11 SCAR 21.2
CC
11
Cov
98%
word2num
11.0
BEGIN
1.0
BEGIN
1.0
CRAP 8.3
↑ Summary
1 package Lingua::PT::Words2Nums;
2
3 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 4 require Exporter;
8
9 4 our @ISA = qw(Exporter);
10
11 4 our %EXPORT_TAGS = ( 'all' => [ qw(
12 word2num
13 ) ] );
14
15 4 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
16
17 4 our @EXPORT = qw(
18 );
19
20 4 our $VERSION = '1.05';
21
22 =encoding latin1
23
24 =head1 NAME
25
26 Lingua::PT::Words2Nums - Converts Portuguese words to numbers
27
28 =head1 SYNOPSIS
29
30 use Lingua::PT::Words2Nums qw/word2num/;
31
32 $result = num2word("cinco");
33 # $result now holds 5
34
35 =head1 DESCRIPTION
36
37 C<Lingua::PT::Words2Nums> converts Portuguese words to numbers (works with numbers
38 ranging from 0 to 999.999.999.999.999.999).
39
40 Not all possible ways to write a number have been implemented (some
41 people write "nove mil novecentos e um", some people write "nove mil,
42 novecentos e um"; C<Lingua::PT::Words2Nums> currently supports only the first way,
43 without commas; also, the word "bilião" is supported, but not "bilhão").
44
45 =cut
46
47 4 my (%values,@values,%bigvalues,@bigvalues);
48
49 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
50 4 %values = (
51 mil => 1000,
52
53 novecentos => 900,
54 oitocentos => 800,
55 setecentos => 700,
56 seiscentos => 600,
57 quinhentos => 500,
58 quatrocentos => 400,
59 trezentos => 300,
60 duzentos => 200,
61 cem => 100,
62
63 cento => 100,
64
65 noventa => 90,
66 oitenta => 80,
67 setenta => 70,
68 sessenta => 60,
69 cinquenta => 50,
70 quarenta => 40,
71 trinta => 30,
72 vinte => 20,
73
74 dezanove => 19,
75 dezoito => 18,
76 dezassete => 17,
77 dezasseis => 16,
78 quinze => 15,
79 catorze => 14,
80 treze => 13,
81 doze => 12,
82 onze => 11,
83 dez => 10,
84
85 nove => 9,
86 oito => 8,
87 sete => 7,
88 seis => 6,
89 cinco => 5,
90 quatro => 4,
91 'três' => 3,
92 dois => 2,
93 um => 1,
94 );
95
96 4 @values = sort {$values{$b} <=> $values{$a}} keys %values;
97
98 4 %bigvalues = (
99 bili => 1000000000000,
100 milh => 1000000,
101 );
102
103 4 @bigvalues = sort {$bigvalues{$b} <=> $bigvalues{$a}} keys %bigvalues;
104
105 }
106
107 =head2 word2num
108
109 Turns a word into a number
110
111 $result = num2word("cinco");
112 # $result now holds 5
113
114 =cut
115
116 10209 ▶sub word2num {
Subroutine word2num: called CC=11 SCAR=24.0
Pod documented
117 10209 ▶ $_ = shift || return undef;
Condition50%1/2
shift() || (return undef)
l!l
102090
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
118 10209 my $result = 0;
119
120 10209 for my $val (@bigvalues) {
121 20418 my $expr = "${val}ões|${val}ão";
122
123 20418 ▶ if (s/(.+)mil(?=.*(?:$expr))//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/(.+)mil(?=.*(?:$expr))//) 49 20369 20418
124 49 my $big = $1;
125 49 for my $value (@values) {
126 1862 $big =~ s/$value/
127 97 $result += ($values{$value} * $bigvalues{$val} * 1000)/e;
128 }
129 }
130
131 20418 ▶ if (s/(.+)(?:$expr)//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/(.+)(?:$expr)//) 108 20310 20418
132 108 my $sma = $1;
133 108 for my $value (@values) {
134 4104 $sma =~ s/$value/
135 194 $result += ($values{$value} * $bigvalues{$val})/e;
136 }
137 }
138
139 }
140
141 10209 ▶ if (s/(.+?)mil//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/(.+?)mil//) 8117 2092 10209
142 8117 my $thousands = $1;
143 8117 ▶ if ($thousands =~ /^\s*e?\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($thousands =~ /^\s*e?\s*$/) { } 2 8115 8117
144 2 $result += 1000;
145 }
146 else {
147 8115 for my $value (@values) {
148 308370 $thousands =~ s/$value/$result += ($values{$value} * 1000)/e;
149 }
150 }
151 }
152
153 10209 for my $value (@values) {
154 387942 s/$value/$result += $values{$value}/e;
155 }
156
157 10209 $result;
158 }
159
160 4 1;
161 __END__
↑ Summary