[go: up one dir, main page]

Pumunta sa nilalaman

Module:NumberSpell

Mula sa Wikipedia, ang malayang ensiklopedya
-- This module converts a number into its written Tagalog/Filipino form.
-- For example, "2" becomes "dalawa", and "79" becomes "pitongpu siyam".

local getArgs = require('Module:Arguments').getArgs

local p = {}

local max = 100 -- The maximum number that can be parsed.

local ones = {
	[0] = 'sero',
	[1] = 'isa',
	[2] = 'dalawa',
	[3] = 'tatlo',
	[4] = 'apat',
	[5] = 'lima',
	[6] = 'anim',
	[7] = 'pito',
	[8] = 'walo',
	[9] = 'siyam'
}

local specials = {
	[10] = 'sampu',
	[11] = 'labing-isa',
	[12] = 'labing-dalawa',
	[13] = 'labing-tatlo',
	[15] = 'labing-lima',
	[18] = 'labing-walo',
	[20] = 'dalawampu',
	[30] = 'tatlumpu',
	[40] = 'apat na pu',
	[50] = 'limampu',
	[60] = 'anim na pu',
	[70] = 'pitumpu',
	[80] = 'walumpu',
	[90] = 'siyam na pu',
	[100] = 'sandaan'
}

local formatRules = {
	{num = 90, rule = 'siyam na pu-%s'},
	{num = 80, rule = 'walumpu-%s'},
	{num = 70, rule = 'pitumpu-%s'},
	{num = 60, rule = 'anim na pu-%s'},
	{num = 50, rule = 'limampu-%s'},
	{num = 40, rule = 'apat na pu-%s'},
	{num = 30, rule = 'tatlumpu-%s'},
	{num = 20, rule = 'dalawampu-%s'},
	{num = 10, rule = 'labing-%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