Lingua::SPA::Numeros - Translates numbers to spanish text
version 0.0682
use Lingua::SPA::Numeros $obj = new Lingua::SPA::Numeros ('MAYUSCULAS' => 1) print $obj->Cardinal(124856), "\n"; print $obj->Real(124856.531), "\n"; $obj->{GENERO} = $FEMALE; print $obj->Ordinal(124856), "\n";
Number 2 word conversion in SPA.
This module supports the translation of cardinal, ordinal and, real numbers, the module handles integer numbers up to vigintillions (that's 1e120), since Perl does not handle such numbers natively, numbers are kept as text strings because processing does not justify using bigint.
Currently Lingua::SPA::Numeros handles numbers up to 1e127-1 (999999 vigintillions).
To create a new Lingua::SPA::Numeros, use the new class method. This method can receive as parameters any of the above mentioned fields.
Examples:
use Lingua::SPA::Numeros; # Use the fields' default values $obj = new Lingua::SPA::Numeros; # Specifies the values of some of them $obj = Lingua::SPA::Numeros->new( ACENTOS => 0, MAYUSCULAS => 1, GENERO => $FEMALE, DECIMAL => ',', SEPARADORES=> '"_' );
Specifies the character string that will be used to separate the integer from the fractional part of the number to convert. The default value for DECIMAL is '.'
Character string including all of the format characters used when representing a number. All of the characters in this string will be ignored by the parser when analyzing the number. The default value for SEPARADORES is '_'
Affects the way in which the generated string for the translated numbers is given; if it is false, the textual representation will not have any accented characters. The default value for this field is true (with accents).
If this is a true value, the textual representation of the number will be an uppercase character string. The default value for this field is false (lowercase).
If this is a true value, the textual representation of the number will be a HTML-valid string character (accents will be represented by their respective HTML entities). The default value is 0 (text).
The gender of the numbers can be $MALE, $FEMALE or $NEUTRAL, respectively for femenine, masculine or neutral numbers. The default value is $MALE.
The following table shows the efect of GENDER on translation of Cardinal and Ordinal numbers:
+---+---------------------+-----------------------------+ | N | CARDINAL | ORDINAL | | u +------+------+-------+---------+---------+---------+ | m | MALE |$FEMALE|$NEUTRAL| $MALE | $FEMALE | $NEUTRAL | +---+------+------+-------+---------+---------+---------+ | 1 | uno | una | un | primero | primera | primer | | 2 | dos | dos | dos | segundo | segunda | segundo | | 3 | tres | tres | tres | tercero | tercera | tercer | +---+------+------+-------+---------+---------+---------+
Deprecated option only for backward compatibility, use GENERO instead.
This field affects only the translation of cardinal numbers. When it is a true value, the number 1000 is translated to 'un mil' (one thousand), otherwise it is translated to the more colloquial 'mil' (thousand). The default value is 1.
Contains the character string with the text to which the negative sign (-) will be translated with. Defaults to 'menos'.
For example: default translation of -5 will yield "menos cinco".
Contains the character string with the text to which the positive sign will be translated with. Defaults to ''.
For example: default translation of 5 will yield "cinco".
A character string specifying how the decimals of a real number are to be translated. Its default value is 'con %2d ctms.' (see the real method)
By popular demand I have added the following aliases for the options:
Alias Natural Name -------------------------- ACCENTS ACENTOS UPPERCASE MAYUSCULAS SEPARATORS SEPARADORES GENDER GENERO POSITIVE POSITIVO NEGATIVE NEGATIVO FORMAT FORMATO
SYNOPSIS: $text = $obj->cardinal($num)
the number.
Translates a cardinal number ($num) to spanish text, translation is performed according to the following object ($obj) settings: DECIMAL, SEPARADORES, SEXO, ACENTOS, MAYUSCULAS, POSITIVO and NEGATIVO.
This method ignores any fraction part of the number ($num).
Textual representation of the number as a string
SYNOPSIS: $text = real($n; $genf, $genm)
Translates the real number ($n) to spanish text.
The optional $genf and $genm parameters are used to specify gender of the fraction part and fraction part magnitude in that order. If $genf is missing it will default to the GENDER option, and $genm will default to the $genf's value.
This translation is affected by the options: DECIMAL, SEPARADORES, GENDER, ACENTOS, MAYUSCULAS, POSITIVO, NEGATIVO and FORMATO.
FORMAT option is a formatting string like printf, it is used to format the fractional part before appending it to the integer part. It has the following format specifiers:
Formats the fractional part as text with precisión of N digits, for example: number '124.345' formated with string 'CON %s.' will yield the text 'ciento veinticuatro CON trescientas cuarenta y cinco milésimas', and formatted with string 'CON %2s.' will yield 'ciento veinticuatro CON treinta y cuatro centésimas'.
Formats the fractional part as a number (no translation), with precision of N digits, veri similar to sprintf's %d format, for example: number '124.045' formated with 'CON %2d ctms.' will yield: 'ciento veinticuatro CON 04 ctms.'
SYNOPSIS: $text = $obj->ordinal($num)
Translates an ordinal number ($num) to spanish text, translation is performed according to the following object ($obj) settings: DECIMAL, SEPARADORES, GENERO, ACENTOS, MAYUSCULAS, POSITIVO and NEGATIVO.
This method croacks if $num <= 0 and carps if $num has a fractional part.
Each of the options has a setter/getter with the name of the option in lowercase, all the accessors have the following sintax:
$obj->name_of_option()
Returns the current value of the option.
$obj->name_of_option( $value )
Sets the option to $value and returns $obj
$obj->accents $obj->acentos $obj->uppercase $obj->mayusculas $obj->unmil $obj->html $obj->decimal $obj->separators $obj->separadores $obj->gender $obj->genero $obj->positive $obj->positivo $obj->negative $obj->negativo $obj->format $obj->formato
Functions in this secction are generally not used, but are docummented here for completeness.
This is not part of the module's API and is subject to change.
Construction of cardinal numbers
cardinal_e2($n, $nn)
word stack.
This procedure takes $n (an integer in the range [0 .. 99], not verified) and adds the numbers text translation to $nn (a word stack), on a word by word basis. If $n == 0 nothing is pushed into $nn.
cardinal_e3($n, $nn)
cardinal_e6($n, $nn, $mag, $un_mil, $postfix)
magnitude of the number 1 for millions, 2 for billions, etc.
if true 1000 is translated as "un mil" otherwise "mil"
array representing plural & singular magnitude of the number, in this order.
This procedure takes $n, and pushes the numbers text translation into $nn, on a word by word basis, with the proper translated magnitude. If $n == 0 nothing is pushed into $nn.
cardinal_generic($n, $exp, $fmag, $gen)
exponent.
closure to format the 6 digits groups.
gender of the magnitude (optional defaults to $NEUTRAL): $FEMALE for female gender (1 -> una). $MALE for male gender (1 -> uno). $NEUTRAL for neutral gender (1 -> un).
This function translate the natural number $n to spanish words, adding gender where needed.
Translation of $n to spanish text as a list of words.
cardinal_simple($n, $exp, $un_mil; $gen)
This procedure just builds a closure with format information, to call cardinal_e6, and then calls cardinal_generic to do the work.
fraccion_mag_prefix($mag, $gp)
magnitude of the number 1 for millionths, 2 for billionths, etc.
gender and plural of the number, is the concatenation of gender and plural gender must be one of $FEMALE, $MALE or $NEUTRAL, and plural must be '' for singular and 's' for plural.
Note that $NEUTRAL + plural is a nonsense.
gender of the number (same values as $gen).
This function returns the name of the magnitude of a fraction, $mag is the number of decimal digits. For example 0.001 has $mag == 3 and translates to "milesimos" if $gp is ($MALE . 's').
Translation of $n to spanish text as a string.
fraccion_simple($n, $exp, $un_mil, $gen; $ngen)
gender of the magnitude (optional defaults to $NEUTRAL): $FEMALE for female gender (1 -> primera). $MALE for male gender (1 -> primero). $NEUTRAL for neutral gender (1 -> primer).
This function translate the fraction $n to spanish words, adding gender where needed.
Construction of ordinal numbers
ordinal_e2($n, $nn)
ordinal_e3($n, $nn)
This procedure takes $n (an integer in the range [0 .. 999], not verified) and adds the numbers text translation to $nn (a word stack), on a word by word basis. If $n == 0 nothing is pushed into $nn.
ordinal_e6($n, $nn, $mag, $un_mil, $postfix)
ordinal_simple($n, $exp; $gen)
This procedure just builds a closure with format information, to call ordinal_e6, and then calls ordinal_generic to do the work.
Everithing not fitting elsewere
parse_num($num, $dec, $sep)
Decomposes the number in its constitutive parts, and returns them in a list:
use Lingua::SPA::Numeros; ($sgn, $ent, $frc, $exp) = parse_num('123.45e10', '.', '",');
the number to decompose
decimal separator (tipically ',' or '.').
separator characters ignored by the parser, usually to mark thousands, millions, etc..
This function parses a general number and returns a list of 4 elements:
sign of the number: -1 if negative, 1 otherwise
integer part of the number
decimal (fraction) part of the number
exponent of the number
Croaks if there is a syntax error.
$obj->retval($value)
Utility method to adjust return values, transforms text following the options: ACENTOS, MAYUSCULAS y HTML.
Returns the adjusted $value.
Perl 5.006, Exporter, Carp
http://roble.pntic.mec.es/~msanto1/ortografia/numeros.htm
Jose Rey, <jrey@cpan.org>
Maintenance PetaMem s.r.o., <info@petamem.com>
Copyright (C) 2001-2009 by Jose Rey
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.
1 POD Error
The following errors were encountered while parsing the POD:
Non-ASCII character seen before =encoding in 'precisión'. Assuming UTF-8
To install Lingua::SPA::Numeros, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Lingua::SPA::Numeros
CPAN shell
perl -MCPAN -e shell install Lingua::SPA::Numeros
For more information on module installation, please visit the detailed CPAN module installation guide.