| 1 | |||
| 2 | package Lingua::FRA::Num2Word; | ||
| 3 | # ABSTRACT: Number to word conversion in French | ||
| 4 | |||
| 5 | use 5.16.0; | ||
| 6 | use utf8; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use Carp qw(carp); | ||
| 10 | use Export::Attrs; | ||
| 11 | use vars qw( | ||
| 12 | $MODE | ||
| 13 | %NUMBER_NAMES | ||
| 14 | %ORDINALS | ||
| 15 | $OUTPUT_DECIMAL_DELIMITER | ||
| 16 | $SIGN_NAMES | ||
| 17 | ); | ||
| 18 | |||
| 19 | our $VERSION = '0.2603300'; | ||
| 20 | $SIGN_NAMES = ('moins'); | ||
| 21 | $OUTPUT_DECIMAL_DELIMITER = ('virgule'); | ||
| 22 | %NUMBER_NAMES = ( | ||
| 23 | 0 => 'zéro', | ||
| 24 | 1 => 'un', | ||
| 25 | 2 => 'deux', | ||
| 26 | 3 => 'trois', | ||
| 27 | 4 => 'quatre', | ||
| 28 | 5 => 'cinq', | ||
| 29 | 6 => 'six', | ||
| 30 | 7 => 'sept', | ||
| 31 | 8 => 'huit', | ||
| 32 | 9 => 'neuf', | ||
| 33 | 10 => 'dix', | ||
| 34 | 11 => 'onze', | ||
| 35 | 12 => 'douze', | ||
| 36 | 13 => 'treize', | ||
| 37 | 14 => 'quatorze', | ||
| 38 | 15 => 'quinze', | ||
| 39 | 16 => 'seize', | ||
| 40 | 17 => 'dix-sept', | ||
| 41 | 18 => 'dix-huit', | ||
| 42 | 19 => 'dix-neuf', | ||
| 43 | 20 => 'vingt', | ||
| 44 | 30 => 'trente', | ||
| 45 | 40 => 'quarante', | ||
| 46 | 50 => 'cinquante', | ||
| 47 | 60 => 'soixante', | ||
| 48 | 70 => 'soixante', | ||
| 49 | 80 => 'quatre-vingt', | ||
| 50 | 90 => 'quatre-vingt', | ||
| 51 | 100 => 'cent', | ||
| 52 | 1e3 => 'mille', | ||
| 53 | 1e6 => 'million', | ||
| 54 | 1e9 => 'milliard', | ||
| 55 | 1e12 => 'billion', # un million de millions | ||
| 56 | 1e18 => 'trillion', # un million de billions | ||
| 57 | 1e24 => 'quatrillion', # un million de trillions | ||
| 58 | 1e30 => 'quintillion', # un million de quatrillions | ||
| 59 | 1e36 => 'sextillion', # un million de quintillions, | ||
| 60 | # the sextillion is the biggest legal unit | ||
| 61 | ); | ||
| 62 | %ORDINALS = ( | ||
| 63 | 1 => 'premier', | ||
| 64 | 5 => 'cinqu', | ||
| 65 | 9 => 'neuv', | ||
| 66 | ); | ||
| 67 | |||
| 68 | sub num2fra_cardinal :Export { goto &number_to_fr } | ||
| 69 | sub num2fra_ordinal :Export { goto &ordinate_to_fr } | ||
| 70 | |||
| 71 | sub number_to_fr :Export { | ||
| 72 | my $number = shift; | ||
| 73 | my @fr_string = (); | ||
| 74 | |||
| 75 | # Test if $number is really a number, or return undef, from perldoc | ||
| 76 | # -q numbers | ||
| 77 | if ( $number !~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/ ) { | ||
| 78 | carp("Invalid number format: '$number'"); | ||
| 79 | return undef; | ||
| 80 | } | ||
| 81 | |||
| 82 | if ( $number > ( 1e75 - 1 ) ) { | ||
| 83 | carp("Number '$number' too big to be represented as string"); | ||
| 84 | return undef; | ||
| 85 | } | ||
| 86 | |||
| 87 | return $NUMBER_NAMES{0} if $number == 0; | ||
| 88 | |||
| 89 | # Add the 'minus' string if the number is negative. | ||
| 90 | push @fr_string, $SIGN_NAMES if abs $number != $number; | ||
| 91 | $number = abs $number; | ||
| 92 | |||
| 93 | # We deal with decimal numbers by calling number2fr twice, once for | ||
| 94 | # the integer part, and once for the decimal part. | ||
| 95 | if ( $number != int $number ) { | ||
| 96 | |||
| 97 | # XXX Ugly Hack. | ||
| 98 | ( my $decimal ) = $number =~ /\.(\d+)/; | ||
| 99 | |||
| 100 | push @fr_string, number_to_fr( int $number ), $OUTPUT_DECIMAL_DELIMITER; | ||
| 101 | |||
| 102 | # XXX | ||
| 103 | if ( $decimal =~ s/^(0+)// ) { | ||
| 104 | my $decimal_power = 10**length $1; | ||
| 105 | last unless $decimal_power; | ||
| 106 | my $fr_decimal; | ||
| 107 | $fr_decimal = number_to_fr($decimal) . ' '; | ||
| 108 | $fr_decimal .= ordinate_to_fr($decimal_power); | ||
| 109 | $fr_decimal .= 's' if $decimal > 1; | ||
| 110 | push @fr_string, $fr_decimal; | ||
| 111 | } | ||
| 112 | else { | ||
| 113 | push @fr_string, number_to_fr($decimal); | ||
| 114 | } | ||
| 115 | |||
| 116 | return join ( ' ', @fr_string ); | ||
| 117 | } | ||
| 118 | |||
| 119 | # First, we split the number by 1000 blocks | ||
| 120 | # i.e: | ||
| 121 | # $block[0] => 0 .. 999 => centaines | ||
| 122 | # $block[1] => 1000 .. 999_999 => milliers | ||
| 123 | # $block[2] => 1e6 .. 999_999_999 => millions | ||
| 124 | # $block[3] => 1e9 .. 1e12-1 => milliards | ||
| 125 | my @blocks; | ||
| 126 | while ($number) { | ||
| 127 | push @blocks, $number % 1000; | ||
| 128 | $number = int $number / 1000; | ||
| 129 | } | ||
| 130 | @blocks = reverse @blocks; | ||
| 131 | |||
| 132 | # We then go through each block, starting from the greatest | ||
| 133 | # (..., billions, millions, thousands) | ||
| 134 | foreach ( 0 .. $#blocks ) { | ||
| 135 | |||
| 136 | # No need to spell numbers like 'zero million' | ||
| 137 | next if $blocks[$_] == 0; | ||
| 138 | |||
| 139 | my $number = $blocks[$_]; | ||
| 140 | |||
| 141 | # Determine the 'size' of the block | ||
| 142 | my $power = 10**( ( $#blocks - $_ ) * 3 ); | ||
| 143 | my $hundred = int( $blocks[$_] / 100 ); | ||
| 144 | my $teens = int( $blocks[$_] % 100 / 10 ); | ||
| 145 | my $units = $blocks[$_] % 10; | ||
| 146 | |||
| 147 | # Process hundred numbers 'inside' the block | ||
| 148 | # (ie. 235 in 235000 when dealing with thousands.) | ||
| 149 | |||
| 150 | # Hundreds | ||
| 151 | if ($hundred) { | ||
| 152 | my $fr_hundred; | ||
| 153 | |||
| 154 | # We don't say 'un cent' | ||
| 155 | $fr_hundred = $NUMBER_NAMES{$hundred} . ' ' | ||
| 156 | unless $hundred == 1; | ||
| 157 | |||
| 158 | $fr_hundred .= $NUMBER_NAMES{100}; | ||
| 159 | |||
| 160 | # Cent prend un 's' quand il est multiplié par un autre | ||
| 161 | # nombre et qu'il termine l'adjectif numéral. | ||
| 162 | $fr_hundred .= 's' | ||
| 163 | if ( $hundred > 1 && !$teens && !$units && $_ == $#blocks ); | ||
| 164 | |||
| 165 | push @fr_string, $fr_hundred; | ||
| 166 | } | ||
| 167 | |||
| 168 | # Process number below 100 | ||
| 169 | my $fr_decimal; | ||
| 170 | |||
| 171 | # No tens | ||
| 172 | $fr_decimal = $NUMBER_NAMES{$units} | ||
| 173 | if ( $units && !$teens ) | ||
| 174 | && # On ne dit pas 'un mille' (A bit awkward to put here) | ||
| 175 | !( $number == 1 && ( $power == 1000 ) ); | ||
| 176 | |||
| 177 | # Cas spécial pour les 80 | ||
| 178 | # On dit 'quatre-vingts' mais 'quatre-vingt-deux' | ||
| 179 | if ( $teens == 8 ) { | ||
| 180 | $fr_decimal = $units | ||
| 181 | ? $NUMBER_NAMES{ $teens * 10 } . '-' . $NUMBER_NAMES{$units} | ||
| 182 | : $NUMBER_NAMES{ $teens * 10 } . 's'; | ||
| 183 | } | ||
| 184 | |||
| 185 | # Cas spécial pour les nombres en 70 et 90 | ||
| 186 | elsif ( $teens == 7 || $teens == 9 ) { | ||
| 187 | $units += 10; | ||
| 188 | if ( $teens == 7 && $units == 11 ) { | ||
| 189 | $fr_decimal = | ||
| 190 | $NUMBER_NAMES{ $teens * 10 } . ' et ' . $NUMBER_NAMES{$units}; | ||
| 191 | } | ||
| 192 | else { | ||
| 193 | $fr_decimal = | ||
| 194 | $NUMBER_NAMES{ $teens * 10 } . '-' . $NUMBER_NAMES{$units}; | ||
| 195 | } | ||
| 196 | |||
| 197 | } | ||
| 198 | |||
| 199 | # Un nombre s'écrit avec un trait d'union sauf s'il est associé | ||
| 200 | # à 'cent' ou à 'mille'; ou s'il est relié par 'et'. | ||
| 201 | # Nombres écrits avec des 'et': 21, 31, 51, 61, 71 | ||
| 202 | elsif ($teens) { | ||
| 203 | if ( $teens == 1 ) { | ||
| 204 | $fr_decimal = $NUMBER_NAMES{ $teens * 10 + $units }; | ||
| 205 | } | ||
| 206 | elsif ( $units == 1 || $units == 11 ) { | ||
| 207 | $fr_decimal = | ||
| 208 | $NUMBER_NAMES{ $teens * 10 } . ' et ' . $NUMBER_NAMES{$units}; | ||
| 209 | } | ||
| 210 | elsif ( $units == 0 ) { | ||
| 211 | $fr_decimal = $NUMBER_NAMES{ $teens * 10 }; | ||
| 212 | } | ||
| 213 | else { | ||
| 214 | $fr_decimal = | ||
| 215 | $NUMBER_NAMES{ $teens * 10 } . '-' . $NUMBER_NAMES{$units}; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | push @fr_string, $fr_decimal if $fr_decimal; | ||
| 220 | |||
| 221 | # Processing thousands, millions, billions, ... | ||
| 222 | if ( $power >= 1e3 ) { | ||
| 223 | my $fr_power; | ||
| 224 | |||
| 225 | if ( exists $NUMBER_NAMES{$power} ) { | ||
| 226 | $fr_power = $NUMBER_NAMES{$power}; | ||
| 227 | |||
| 228 | # Billion, milliard, etc. prennent un 's' au pluriel | ||
| 229 | $fr_power .= 's' if $number > 1 && $power >= 1e6; | ||
| 230 | |||
| 231 | push @fr_string, $fr_power; | ||
| 232 | } | ||
| 233 | |||
| 234 | # If the power we're looking dealing with doesn't exists | ||
| 235 | # (ie. 1e15, 1e21) we multiply by the lowest power we have, | ||
| 236 | # starting at 1e6. | ||
| 237 | else { | ||
| 238 | my $sub_power; | ||
| 239 | my $pow_diff = 1; | ||
| 240 | do { | ||
| 241 | $pow_diff *= 1_000_000; | ||
| 242 | $sub_power = $power / $pow_diff; | ||
| 243 | } until exists $NUMBER_NAMES{$sub_power}; | ||
| 244 | |||
| 245 | # If the power_diff doesn't exists (for really big | ||
| 246 | # numbers), we do the same dance. | ||
| 247 | unless ( exists $NUMBER_NAMES{$pow_diff} ) { | ||
| 248 | |||
| 249 | } | ||
| 250 | $fr_power = $NUMBER_NAMES{$pow_diff}; | ||
| 251 | $fr_power .= 's' if $number > 1; | ||
| 252 | $fr_power .= " de $NUMBER_NAMES{$sub_power}s"; | ||
| 253 | |||
| 254 | push @fr_string, $fr_power; | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | next; | ||
| 259 | } | ||
| 260 | |||
| 261 | return join ( ' ', @fr_string ); | ||
| 262 | } | ||
| 263 | |||
| 264 | sub ordinate_to_fr :Export { | ||
| 265 | my $number = shift; | ||
| 266 | |||
| 267 | unless ( $number > 0 ) { | ||
| 268 | carp('Ordinates must be strictly positive'); | ||
| 269 | return undef; | ||
| 270 | } | ||
| 271 | return $ORDINALS{1} if $number == 1; | ||
| 272 | |||
| 273 | my $ordinal = number_to_fr($number); | ||
| 274 | my $last_digit = $number % 10; | ||
| 275 | |||
| 276 | if ( $last_digit != 1 && exists $ORDINALS{$last_digit} ) { | ||
| 277 | my $replace = number_to_fr($last_digit); | ||
| 278 | $ordinal =~ s/$replace$/$ORDINALS{$last_digit}/; | ||
| 279 | } | ||
| 280 | |||
| 281 | $ordinal =~ s/e?$/ième/; | ||
| 282 | $ordinal =~ s/vingtsième/vingtième/; # Bug #1772 | ||
| 283 | $ordinal; | ||
| 284 | } | ||
| 285 | |||
| 286 | # | ||
| 287 | # OO Methods | ||
| 288 | # | ||
| 289 | sub new { | ||
| 290 | my $class = shift; | ||
| 291 | my $number = shift; | ||
| 292 | bless \$number, $class; | ||
| 293 | } | ||
| 294 | |||
| 295 | sub parse { | ||
| 296 | my $self = shift; | ||
| 297 | if ( $_[0] ) { $$self = shift } | ||
| 298 | $self; | ||
| 299 | } | ||
| 300 | |||
| 301 | sub get_string { | ||
| 302 | my $self = shift; | ||
| 303 | number_to_fr($$self); | ||
| 304 | } | ||
| 305 | |||
| 306 | sub get_ordinate { | ||
| 307 | my $self = shift; | ||
| 308 | ordinate_to_fr($$self); | ||
| 309 | } | ||
| 310 | |||
| 311 | |||
| 312 | # {{{ capabilities declare supported features | ||
| 313 | |||
| 314 | sub capabilities { | ||
| 315 | return { | ||
| 316 | cardinal => 1, | ||
| 317 | ordinal => 1, | ||
| 318 | }; | ||
| 319 | } | ||
| 320 | |||
| 321 | # }}} | ||
| 322 | 1; | ||
| 323 | |||
| 324 | __END__ |