Módulo:NumberSpell
Hechura
La documentación para este módulo puede ser creada en Módulo:NumberSpell/doc
-- This module converts a number into its written English form.
-- For example, "2" becomes "two", and "79" becomes "seventy-nine".
local getArgs = require('Module:Arguments').getArgs
local p = {}
local max = 100 -- The maximum number that can be parsed.
local ones = {
[0] = 'cero',
[1] = 'uno',
[2] = 'dos',
[3] = 'tres',
[4] = 'cuatro',
[5] = 'cinco',
[6] = 'seis',
[7] = 'siete',
[8] = 'ocho',
[9] = 'nueve'
}
local specials = {
[10] = 'diez',
[11] = 'once',
[12] = 'doce',
[13] = 'trece',
[14] = 'catorce',
[15] = 'cince',
[20] = 'veinte',
[30] = 'treinta',
[40] = 'cuarenta',
[50] = 'cincuenta',
[60] = 'sesenta',
[70] = 'setenta',
[80] = 'ochenta',
[90] = 'noventa',
[100] = 'ciento'
}
local formatRules = {
{num = 90, rule = 'noventa y %s'},
{num = 80, rule = 'ochenta y %s'},
{num = 70, rule = 'setenta y %s'},
{num = 60, rule = 'sesenta y %s'},
{num = 50, rule = 'cincuenta y %s'},
{num = 40, rule = 'cuarenta y %s'},
{num = 30, rule = 'treinta y %s'},
{num = 20, rule = 'veinti%s'},
{num = 10, rule = 'dieci%s'}
}
function p.main(frame)
local args = getArgs(frame)
local num = tonumber(args[1])
local success, result = pcall(p._main, num)
if success then
return result
else
return string.format('<strong class="error">Error: %s</strong>', result) -- "result" is the error message.
end
return p._main(num)
end
function p._main(num)
if type(num) ~= 'number' or math.floor(num) ~= num or num < 0 or num > max then
error('input must be an integer between 0 and ' .. tostring(max), 2)
end
-- Check for numbers from 0 to 9.
local onesVal = ones[num]
if onesVal then
return onesVal
end
-- Check for special numbers.
local specialVal = specials[num]
if specialVal then
return specialVal
end
-- Construct the number from its format rule.
onesVal = ones[num % 10]
if not onesVal then
error('Unexpected error parsing input ' .. tostring(num))
end
for i, t in ipairs(formatRules) do
if num >= t.num then
return string.format(t.rule, onesVal)
end
end
error('No format rule found for input ' .. tostring(num))
end
return p