| 1 | # For Emacs: -*- mode:cperl; eval: (folding-mode 1); coding:utf-8; -*- | ||
| 2 | |||
| 3 | package Lingua::EUS::Num2Word; | ||
| 4 | # ABSTRACT: Number 2 word conversion in EUS. | ||
| 5 | |||
| 6 | use 5.16.0; | ||
| 7 | use utf8; | ||
| 8 | use warnings; | ||
| 9 | |||
| 10 | # {{{ use block | ||
| 11 | |||
| 12 | use Carp; | ||
| 13 | use Export::Attrs; | ||
| 14 | |||
| 15 | # }}} | ||
| 16 | # {{{ variables declaration | ||
| 17 | |||
| 18 | our $VERSION = '0.2603270'; | ||
| 19 | |||
| 20 | # The Bask numeral system is vigesimal (base 20). So far, going to | ||
| 21 | # 999_999_999_999. | ||
| 22 | |||
| 23 | our %num2alpha :Export = ( | ||
| 24 | 0 => 'zero', | ||
| 25 | 1 => 'bat', | ||
| 26 | 2 => 'bi', | ||
| 27 | 3 => 'hiru', | ||
| 28 | 4 => 'lau', | ||
| 29 | 5 => 'bost', | ||
| 30 | 6 => 'sei', | ||
| 31 | 7 => 'zazpi', | ||
| 32 | 8 => 'zortzi', | ||
| 33 | 9 => 'bederatzi', | ||
| 34 | 10 => 'hamar', | ||
| 35 | 11 => 'hamaika', | ||
| 36 | 12 => 'hamabi', | ||
| 37 | 13 => 'hamahiru', | ||
| 38 | 14 => 'hamalau', | ||
| 39 | 15 => 'hamabost', | ||
| 40 | 16 => 'hamasei', | ||
| 41 | 17 => 'hamazazpi', | ||
| 42 | 18 => 'hemezortzi', | ||
| 43 | 19 => 'hemeretzi', | ||
| 44 | 20 => 'hogei', | ||
| 45 | 40 => 'berrogei', | ||
| 46 | 60 => 'hirurogei', | ||
| 47 | 80 => 'laurogei', | ||
| 48 | 100 => 'ehun', | ||
| 49 | 200 => 'berrehun', | ||
| 50 | 300 => 'hirurehun', | ||
| 51 | 400 => 'laurehun', | ||
| 52 | 500 => 'bostehun', | ||
| 53 | 600 => 'seiehun', | ||
| 54 | 700 => 'zazpiehun', | ||
| 55 | 800 => 'zortziehun', | ||
| 56 | 900 => 'bederatziehun', | ||
| 57 | 1000 => 'mila', | ||
| 58 | 1000000 => 'milioi bat', | ||
| 59 | 1000000000 => 'mila milioi' | ||
| 60 | ); | ||
| 61 | |||
| 62 | #Names for quantifiers, every block of 3 digits | ||
| 63 | #(thousands, millions, billions) | ||
| 64 | my %block2alpha = ( | ||
| 65 | block1 => 'mila', | ||
| 66 | block2 => 'milioi', | ||
| 67 | block3 => 'mila milioi' | ||
| 68 | ); | ||
| 69 | |||
| 70 | # }}} | ||
| 71 | |||
| 72 | #This function accepts an integer (scalar) as a parameter and | ||
| 73 | #returns a string (array), which is its Bask cardinal equivalent. | ||
| 74 | # {{{ cardinal2alpha | ||
| 75 | |||
| 76 | sub num2eus_cardinal :Export { goto &cardinal2alpha } | ||
| 77 | |||
| 78 | sub cardinal2alpha :Export { | ||
| 79 | my $orig_num = shift // return; | ||
| 80 | my @result = (); | ||
| 81 | my ( $thousands, $hundreds, $tens, $units ); | ||
| 82 | my $num = $orig_num; | ||
| 83 | |||
| 84 | #Input validation | ||
| 85 | unless ( $num =~ /^\d+$/ ) { | ||
| 86 | carp "Entry $num not valid. Should be numeric characters only"; | ||
| 87 | return; | ||
| 88 | } | ||
| 89 | |||
| 90 | if ( $num > 999_999_999_999 or $num < 0 ) { | ||
| 91 | carp "Entry $num not valid. Number should be an integer between 0 and 999,999,999,999"; | ||
| 92 | return; | ||
| 93 | } | ||
| 94 | |||
| 95 | #Handling special cases | ||
| 96 | return $num2alpha{0} if $num == 0; | ||
| 97 | return $num2alpha{$num} if $num2alpha{$num}; | ||
| 98 | |||
| 99 | my $len = length($num); | ||
| 100 | |||
| 101 | #Main logic: cutting number by block of 3 digits | ||
| 102 | while ( $len > 3 ) { | ||
| 103 | |||
| 104 | $num = reverse($num); | ||
| 105 | |||
| 106 | #Dealing with the part off the block(s) of three | ||
| 107 | my $extra_digits = substr( $num, int( ( $len - 1 ) / 3 ) * 3 ); | ||
| 108 | $extra_digits = reverse($extra_digits); | ||
| 109 | push ( @result, triple_digit_handling($extra_digits) ) | ||
| 110 | unless $extra_digits == 1; | ||
| 111 | |||
| 112 | #Adding name for the quantifier | ||
| 113 | my $quantif = 'block' . ( int( ( $len - 1 ) / 3 ) ); | ||
| 114 | push ( @result, $block2alpha{$quantif} ) unless $num =~ /000$/; | ||
| 115 | |||
| 116 | #Special case for 1 million: adding the term for "one" | ||
| 117 | push ( @result, $num2alpha{1} ) if $len == 7 && $extra_digits == 1; | ||
| 118 | |||
| 119 | #Adding "eta" after millions (except when there's no thousand) | ||
| 120 | my $whats_left = substr( reverse($num), length($extra_digits) ); | ||
| 121 | if ( ( $len <= 8 and $len >= 7 ) | ||
| 122 | && $whats_left != 0 | ||
| 123 | && ( reverse($num) !~ /^[^0]000/ ) ) | ||
| 124 | { | ||
| 125 | push ( @result, "eta" ); | ||
| 126 | } | ||
| 127 | |||
| 128 | #Adding 'eta' for hundreds, except when there are tens and/or units | ||
| 129 | if ( length($num) <= 6 ) { | ||
| 130 | ( $units, $tens, $hundreds, $thousands, my @rest ) = | ||
| 131 | split ( //, reverse($orig_num) ); | ||
| 132 | |||
| 133 | if ( ( $hundreds != 0 && $tens == 0 && $units == 0 ) | ||
| 134 | || ( $hundreds == 0 && ( $tens || $units ) ) && $num !~ /^0/ | ||
| 135 | || ( $thousands == 0 && $hundreds == 0 && ( $tens || $units ) ) | ||
| 136 | ) | ||
| 137 | { | ||
| 138 | push ( @result, "eta" ); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | #Dealing with the remaining digits | ||
| 143 | $num = reverse($num); | ||
| 144 | $num = substr( $num, length($extra_digits) ); | ||
| 145 | $len = length($num); | ||
| 146 | |||
| 147 | } #end while len > 3 | ||
| 148 | |||
| 149 | if ( $len <= 3 ) { | ||
| 150 | push ( @result, triple_digit_handling($num) ); | ||
| 151 | return "@result"; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | # }}} | ||
| 156 | |||
| 157 | #This function takes an integer (scalar) as a parameter, which is | ||
| 158 | #a 3-digit number or less, and returns a string (array), which is | ||
| 159 | #its Bask equivalent. | ||
| 160 | # {{{ triple_digit_handling | ||
| 161 | |||
| 162 | sub triple_digit_handling { | ||
| 163 | my $num = shift; | ||
| 164 | my @result = (); | ||
| 165 | my ( $hundreds, $tens, $units, @tens_n_units ); | ||
| 166 | |||
| 167 | #Handling exceptional cases | ||
| 168 | return if $num > 999 || $num < 0; | ||
| 169 | return if $num == 0; | ||
| 170 | return $num2alpha{$num} if $num2alpha{$num}; | ||
| 171 | |||
| 172 | my $len = length($num); | ||
| 173 | |||
| 174 | #Handling 2-digit numbers | ||
| 175 | if ( $len == 2 ) { | ||
| 176 | ( $tens, $units ) = split ( //, sprintf( "%02d", $num ) ); | ||
| 177 | @result = double_digit_handling( $tens, $units ); | ||
| 178 | return @result; | ||
| 179 | } | ||
| 180 | |||
| 181 | #Handling 3-digit numbers | ||
| 182 | if ( $len == 3 ) { | ||
| 183 | ( $hundreds, $tens, $units ) = split ( //, sprintf( "%03d", $num ) ); | ||
| 184 | unless ( $hundreds == 0 ) { | ||
| 185 | $hundreds *= 100; | ||
| 186 | push ( @result, $num2alpha{$hundreds} ); | ||
| 187 | push ( @result, "eta" ) if $tens || $units; | ||
| 188 | } | ||
| 189 | |||
| 190 | @tens_n_units = double_digit_handling( $tens, $units ); | ||
| 191 | push ( @result, @tens_n_units ); | ||
| 192 | return @result; | ||
| 193 | } | ||
| 194 | |||
| 195 | } | ||
| 196 | |||
| 197 | # }}} | ||
| 198 | |||
| 199 | #This function takes two integers (scalars) as parameters (tens and units) | ||
| 200 | #and returns a string (array), which is their Bask equivalent. | ||
| 201 | # {{{ double_digit_handling | ||
| 202 | |||
| 203 | sub double_digit_handling { | ||
| 204 | my $diz = shift; | ||
| 205 | my $unit = shift; | ||
| 206 | my $num = "$diz$unit"; | ||
| 207 | my @result; | ||
| 208 | |||
| 209 | #Handling exceptional cases | ||
| 210 | return if $num == 0; | ||
| 211 | |||
| 212 | return $num2alpha{$num} if $num2alpha{$num}; | ||
| 213 | |||
| 214 | return $num2alpha{$unit} unless $diz; | ||
| 215 | |||
| 216 | #Dealing with base 20 | ||
| 217 | if ( $diz =~ /[3579]/ ) { | ||
| 218 | $diz -= 1; | ||
| 219 | $unit += 10; | ||
| 220 | } | ||
| 221 | $diz = $diz * 10; | ||
| 222 | |||
| 223 | if ($unit) { push ( @result, "$num2alpha{$diz}ta" ); } | ||
| 224 | else { push ( @result, $num2alpha{$diz} ); } | ||
| 225 | push ( @result, $num2alpha{$unit} ); | ||
| 226 | |||
| 227 | return @result; | ||
| 228 | } | ||
| 229 | |||
| 230 | # }}} | ||
| 231 | |||
| 232 | #This function accepts an integer (scalar) as a parameter and | ||
| 233 | #returns a string (array), which is its Bask ordinal equivalent. | ||
| 234 | # {{{ ordinal2alpha | ||
| 235 | |||
| 236 | sub ordinal2alpha :Export { | ||
| 237 | my $num = shift // return; | ||
| 238 | my @result; | ||
| 239 | |||
| 240 | #Handling special cases | ||
| 241 | return unless $num =~ /^\d+$/; | ||
| 242 | return if ( $num < 0 || $num > 999_999_999_999 ); | ||
| 243 | return "lehenengo" if $num == 1; | ||
| 244 | |||
| 245 | push ( @result, join ( '', cardinal2alpha($num), "garren" ) ); | ||
| 246 | return "@result"; | ||
| 247 | } | ||
| 248 | |||
| 249 | # }}} | ||
| 250 | |||
| 251 | |||
| 252 | # {{{ capabilities declare supported features | ||
| 253 | |||
| 254 | sub capabilities { | ||
| 255 | return { | ||
| 256 | cardinal => 1, | ||
| 257 | ordinal => 0, | ||
| 258 | }; | ||
| 259 | } | ||
| 260 | |||
| 261 | # }}} | ||
| 262 | 1; | ||
| 263 | __END__ |