Modul:NumberSpell

Vikipediya, azad ensiklopediya
Naviqasiyaya keçin Axtarışa keçin

Bu modulun sənədləşdirmə səhifəsi Modul:NumberSpell/doc səhifəsində yaradıla bilər

-- 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] = 'sıfır',
	[1] = 'bir',
	[2] = 'iki',
	[3] = 'üç',
	[4] = 'dörd',
	[5] = 'beş',
	[6] = 'altı',
	[7] = 'yeddi',
	[8] = 'səkkiz',
	[9] = 'doqquz'
}

local specials = {
	[10] = 'on',
	[11] = 'on bir',
	[12] = 'on iki',
	[13] = 'on üç',
	[15] = 'on beş',
	[18] = 'on səkkiz',
	[20] = 'iyirmi',
	[30] = 'otuz',
	[40] = 'qırx',
	[50] = 'əlli',
	[60] = 'altmış',
	[70] = 'yetmiş',
	[80] = 'səksən',
	[90] = 'doxsan',
	[100] = 'yüz'
}

local formatRules = {
	{num = 90, rule = 'doxsan %s'},
	{num = 80, rule = 'səksən %s'},
	{num = 70, rule = 'yetmiş %s'},
	{num = 60, rule = 'altmış %s'},
	{num = 50, rule = 'əlli %s'},
	{num = 40, rule = 'qırx %s'},
	{num = 30, rule = 'otuz %s'},
	{num = 20, rule = 'iyirmi %s'},
	{num = 10, rule = 'on %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