Ir para o conteúdo

Módulo:ConvertNumeric

Origem: Wikipédia, a enciclopédia livre.
-- Módulo para converter entre diferentes representações de números. Veja a página de discussão para documentação do usuário.
-- Para testes unitários veja: [[Module:ConvertNumeric/testcases]]
-- Ao editar, visualize com: [[Module_talk:ConvertNumeric/testcases]]
-- Primeiro, edite [[Module:ConvertNumeric/sandbox]], depois visualize com [[Module_talk:ConvertNumeric/sandbox/testcases]]
require('strict')

local ones_position = {
	[0] = 'zero',
	[1] = 'um',
	[2] = 'dois',
	[3] = 'três',
	[4] = 'quatro',
	[5] = 'cinco',
	[6] = 'seis',
	[7] = 'sete',
	[8] = 'oito',
	[9] = 'nove',
	[10] = 'dez',
	[11] = 'onze',
	[12] = 'doze',
	[13] = 'treze',
	[14] = 'quatorze',
	[15] = 'quinze',
	[16] = 'dezesseis',
	[17] = 'dezessete',
	[18] = 'dezoito',
	[19] = 'dezenove'
}

local ones_position_ord = {
	[0] = 'zero',
	[1] = 'primeiro',
	[2] = 'segundo',
	[3] = 'terceiro',
	[4] = 'quarto',
	[5] = 'quinto',
	[6] = 'sexto',
	[7] = 'sétimo',
	[8] = 'oitavo',
	[9] = 'nono',
	[10] = 'décimo',
	[11] = 'décimo primeiro',
	[12] = 'décimo segundo',
	[13] = 'décimo terceiro',
	[14] = 'décimo quarto',
	[15] = 'décimo quinto',
	[16] = 'décimo sexto',
	[17] = 'décimo sétimo',
	[18] = 'décimo oitavo',
	[19] = 'décimo nono'
}

local ones_position_plural = {
	[0] = 'zeros',
	[1] = 'uns',
	[2] = 'dois',
	[3] = 'três',
	[4] = 'quatros',
	[5] = 'cincos',
	[6] = 'seis',
	[7] = 'setes',
	[8] = 'oitos',
	[9] = 'noves',
	[10] = 'dezes',
	[11] = 'onzes',
	[12] = 'dozes',
	[13] = 'trezes',
	[14] = 'quatorzes',
	[15] = 'quinzes',
	[16] = 'dezesseis',
	[17] = 'dezessetes',
	[18] = 'dezoitos',
	[19] = 'dezenoves'
}

local tens_position = {
	[2] = 'vinte',
	[3] = 'trinta',
	[4] = 'quarenta',
	[5] = 'cinquenta',
	[6] = 'sessenta',
	[7] = 'setenta',
	[8] = 'oitenta',
	[9] = 'noventa'
}

local tens_position_ord = {
	[2] = 'vigésimo',
	[3] = 'trigésimo',
	[4] = 'quadragésimo',
	[5] = 'quinquagésimo',
	[6] = 'sexagésimo',
	[7] = 'septuagésimo',
	[8] = 'octogésimo',
	[9] = 'nonagésimo'
}

local tens_position_plural = {
	[2] = 'vinte',
	[3] = 'trinta',
	[4] = 'quarenta',
	[5] = 'cinquenta',
	[6] = 'sessenta',
	[7] = 'setenta',
	[8] = 'oitenta',
	[9] = 'noventa'
}

local groups = {
	[1] = 'mil',
	[2] = 'milhão',
	[3] = 'bilhão',
	[4] = 'trilhão',
	[5] = 'quatrilhão',
	[6] = 'quintilhão',
	[7] = 'sextilhão',
	[8] = 'septilhão',
	[9] = 'octilhão',
	[10] = 'nonilhão',
	[11] = 'decilhão',
	[12] = 'undecilhão',
	[13] = 'duodecilhão',
	[14] = 'tredecilhão',
	[15] = 'quatuordecilhão',
	[16] = 'quindecilhão',
	[17] = 'sexdecilhão',
	[18] = 'septendecilhão',
	[19] = 'octodecilhão',
	[20] = 'novemdecilhão',
	[21] = 'vigintilhão',
	[22] = 'unvigintilhão',
	[23] = 'duovigintilhão',
	[24] = 'tresvigintilhão',
	[25] = 'quattuorvigintilhão',
	[26] = 'quinquavigintilhão',
	[27] = 'sesvigintilhão',
	[28] = 'septemvigintilhão',
	[29] = 'octovigintilhão',
	[30] = 'novemvigintilhão',
	[31] = 'trigintilhão',
	[32] = 'untrigintilhão',
	[33] = 'duotrigintilhão',
	[34] = 'trestrigintilhão',
	[35] = 'quattuortrigintilhão',
	[36] = 'quinquatrigintilhão',
	[37] = 'sestrigintilhão',
	[38] = 'septentrigintilhão',
	[39] = 'octotrigintilhão',
	[40] = 'noventrigintilhão',
	[41] = 'quadragintilhão',
	[51] = 'quinquagintilhão',
	[61] = 'sexagintilhão',
	[71] = 'septuagintilhão',
	[81] = 'octogintilhão',
	[91] = 'nonagintilhão',
	[101] = 'centilhão',
	[102] = 'uncentilhão',
	[103] = 'duocentilhão',
	[104] = 'trescentilhão',
	[111] = 'decicentilhão',
	[112] = 'undecicentilhão',
	[121] = 'viginticentilhão',
	[122] = 'unviginticentilhão',
	[131] = 'trigintacentilhão',
	[141] = 'quadragintacentilhão',
	[151] = 'quinquagintacentilhão',
	[161] = 'sexagintacentilhão',
	[171] = 'septuagintacentilhão',
	[181] = 'octogintacentilhão',
	[191] = 'nonagintacentilhão',
	[201] = 'ducentilhão',
	[301] = 'trecentilhão',
	[401] = 'quadringentilhão',
	[501] = 'quingentilhão',
	[601] = 'sescentilhão',
	[701] = 'septingentilhão',
	[801] = 'octingentilhão',
	[901] = 'nongentilhão',
	[1001] = 'millinilhão',
}

local roman_numerals = {
	I = 1,
	V = 5,
	X = 10,
	L = 50,
	C = 100,
	D = 500,
	M = 1000
}

local engord_tens_end = {
	['vigésimo'] = 20,
	['trigésimo'] = 30,
	['quadragésimo'] = 40,
	['quinquagésimo'] = 50,
	['sexagésimo'] = 60,
	['septuagésimo'] = 70,
	['octogésimo'] = 80,
	['nonagésimo'] = 90,
}

local eng_tens_cont = {
	['vinte'] = 20,
	['trinta'] = 30,
	['quarenta'] = 40,
	['cinquenta'] = 50,
	['sessenta'] = 60,
	['setenta'] = 70,
	['oitenta'] = 80,
	['noventa'] = 90,
}

-- Converte um numeral romano válido (e alguns inválidos) para número. Retorna { -1, string de erro } em caso de erro.
local function roman_to_numeral(roman)
	if type(roman) ~= "string" then return -1, "numeral romano não é uma string" end
	local rev = roman:reverse()
	local raising = true
	local last = 0
	local result = 0
	for i = 1, #rev do
		local c = rev:sub(i, i)
		local next = roman_numerals[c]
		if next == nil then return -1, "numeral romano contém caractere ilegal " .. c end
		if next > last then
			result = result + next
			raising = true
		elseif next < last then
			result = result - next
			raising = false
		elseif raising then
			result = result + next
		else
			result = result - next
		end
		last = next
	end
	return result
end

-- Converte um número inteiro entre 0 e 100 para texto em português (ex: 47 -> quarenta e sete).
local function numeral_to_english_less_100(num, ordinal, plural, zero)
	local terminal_ones, terminal_tens
	if ordinal then
		terminal_ones = ones_position_ord
		terminal_tens = tens_position_ord
	elseif plural then
		terminal_ones = ones_position_plural
		terminal_tens = tens_position_plural
	else
		terminal_ones = ones_position
		terminal_tens = tens_position
	end

	if num == 0 and zero ~= nil then
		return zero
	elseif num < 20 then
		return terminal_ones[num]
	elseif num % 10 == 0 then
		return terminal_tens[num / 10]
	else
		return tens_position[math.floor(num / 10)] .. ' e ' .. terminal_ones[num % 10]
	end
end

local function standard_suffix(ordinal, plural)
	if ordinal then return '' end
	if plural then return 's' end
	return ''
end

-- Converte um número inteiro (em forma de string) entre 0 e 1000 para texto em português (ex: 47 -> quarenta e sete).
local function numeral_to_english_less_1000(num, use_and, ordinal, plural, zero)
	num = tonumber(num)
	if num < 100 then
		return numeral_to_english_less_100(num, ordinal, plural, zero)
	elseif num % 100 == 0 then
		return ones_position[num/100] .. ' cento' .. standard_suffix(ordinal, plural)
	else
		return ones_position[math.floor(num/100)] .. ' cento e ' .. numeral_to_english_less_100(num % 100, ordinal, plural, zero)
	end
end

-- Converte um ordinal em português de 'primeiro' a 'nonagésimo nono' inclusive para um número [0–99], senão -1.
local function english_to_ordinal(english)
	local eng = string.lower(english or '')

	local engord_lt20 = {}
	for k, v in pairs(ones_position_ord) do
		engord_lt20[v] = k
	end

	if engord_lt20[eng] then
		return engord_lt20[eng]
	elseif engord_tens_end[eng] then
		return engord_tens_end[eng]
	else
		local tens, ones = string.match(eng, '^([a-záéíóúâêôãõç]+)[%s%-]+([a-záéíóúâêôãõç]+)$')
		if tens and ones then
			local tens_cont = eng_tens_cont[tens]
			local ones_end = engord_lt20[ones]
			if tens_cont and ones_end then
				return tens_cont + ones_end
			end
		end
	end
	return -1
end

-- Converte um número em português de 'zero' a 'noventa e nove' inclusive para um número [0–99], senão -1.
local function english_to_numeral(english)
	local eng = string.lower(english or '')

	local eng_lt20 = {}
	for k, v in pairs(ones_position) do
		eng_lt20[v] = k
	end

	if eng_lt20[eng] then
		return eng_lt20[eng]
	elseif eng_tens_cont[eng] then
		return eng_tens_cont[eng]
	else
		local tens, ones = string.match(eng, '^([a-záéíóúâêôãõç]+)[%s%-]+([a-záéíóúâêôãõç]+)$')
		if tens and ones then
			local tens_cont = eng_tens_cont[tens]
			local ones_end = eng_lt20[ones]
			if tens_cont and ones_end then
				return tens_cont + ones_end
			end
		end
	end
	return -1
end

-- Converte um número expresso como string em notação científica para string em notação decimal padrão
-- ex: 1.23E5 -> 123000, 1.23E-5 = .0000123. A conversão é exata, nenhum arredondamento é realizado.
local function scientific_notation_to_decimal(num)
	local exponent, subs = num:gsub("^%-?%d*%.?%d*%-?[Ee]([+%-]?%d+)$", "%1")
	if subs == 0 then return num end
	exponent = tonumber(exponent)

	local negative = num:find("^%-")
	local _, decimal_pos = num:find("%.")
	local mantissa = num:gsub("^%-?(%d*)%.?(%d*)%-?[Ee][+%-]?%d+$", "%1%2")
	if negative and decimal_pos then decimal_pos = decimal_pos - 1 end
	if not decimal_pos then decimal_pos = #mantissa + 1 end

	while decimal_pos > 1 and mantissa:sub(1,1) == '0' do
		mantissa = mantissa:sub(2)
		decimal_pos = decimal_pos - 1
	end
	while exponent > 0 do
		decimal_pos = decimal_pos + 1
		exponent = exponent - 1
		if decimal_pos > #mantissa + 1 then mantissa = mantissa .. '0' end
		while decimal_pos > 1 and mantissa:sub(1,1) == '0' do
			mantissa = mantissa:sub(2)
			decimal_pos = decimal_pos - 1
		end
	end
	while exponent < 0 do
		if decimal_pos == 1 then
			mantissa = '0' .. mantissa
		else
			decimal_pos = decimal_pos - 1
		end
		exponent = exponent + 1
	end

	return (negative and '-' or '') .. mantissa:sub(1, decimal_pos - 1) .. '.' .. mantissa:sub(decimal_pos)
end

-- Arredonda um número para o inteiro mais próximo
local function round_num(x)
	if x % 1 >= 0.5 then
		return math.ceil(x)
	else
		return math.floor(x)
	end
end

-- Arredonda um número para o número de duas palavras mais próximo (round = up, down, ou "on" para arredondar para o mais próximo).
-- Números com dois dígitos antes do decimal serão arredondados para um inteiro conforme especificado por round.
-- Números maiores serão arredondados para um número com apenas um dígito diferente de zero na frente e todos os outros dígitos zero.
-- O sinal negativo é preservado e não conta para o limite de palavras.
local function round_for_english(num, round)
	if num:find("^%-?%d?%d?%.?$") then return num end

	local negative = num:find("^%-")
	if negative then
		if round == 'up' then round = 'down' elseif round == 'down' then round = 'up' end
	end

	local _, _, small_int, trailing_digits, round_digit = num:find("^%-?(%d?%d?)%.((%d)%d*)$")
	if small_int then
		if small_int == '' then small_int = '0' end
		if (round == 'up' and trailing_digits:find('[1-9]')) or (round == 'on' and tonumber(round_digit) >= 5) then
			small_int = tostring(tonumber(small_int) + 1)
		end
		return (negative and '-' or '') .. small_int
	end

	local nonzero_digits = 0
	for digit in num:gfind("[1-9]") do
		nonzero_digits = nonzero_digits + 1
	end

	num = num:gsub("%.%d*$", "")
	local _, _, lead_digit, round_digit, round_digit_2, rest = num:find("^%-?(%d)(%d)(%d)(%d*)$")
	if tonumber(lead_digit .. round_digit) < 20 and (1 + #rest) % 3 == 0 then
		lead_digit = lead_digit .. round_digit
		round_digit = round_digit_2
	else
		rest = round_digit_2 .. rest
	end

	if (round == 'up' and nonzero_digits > 1) or (round == 'on' and tonumber(round_digit) >= 5) then
		lead_digit = tostring(tonumber(lead_digit) + 1)
	end
	rest = rest:gsub("%d", "0")
	return (negative and '-' or '') .. lead_digit .. '0' .. rest
end

local denominators = {
	[2] = { 'meio', plural = 'meios' },
	[3] = { 'terço', plural = 'terços' },
	[4] = { 'quarto', plural = 'quartos' },
	[5] = { 'quinto', plural = 'quintos' },
	[6] = { 'sexto', plural = 'sextos' },
	[8] = { 'oitavo', plural = 'oitavos' },
	[9] = { 'nono', plural = 'nonos' },
	[10] = { 'décimo', plural = 'décimos' },
	[16] = { 'dezesseis avos', plural = 'dezesseis avos' },
}

-- Retorna status, fração onde:
-- status é uma string:
--     "finished" se há uma fração sem número inteiro;
--     "ok" se a fração está vazia ou é válida;
--     "unsupported" se a fração é inválida;
-- fraction é uma string dando (numerador / denominador) como texto em português, ou é "".
-- Apenas frações sem sinal com uma faixa muito limitada de valores são suportadas,
-- exceto que se whole estiver vazio, o numerador pode usar "-" para indicar negativo.
-- whole (string ou nil): nil ou "" se não há número antes da fração
-- numerator (string ou nil): numerador, se houver (padrão = 1 se um denominador for fornecido)
-- denominator (string ou nil): denominador, se houver
-- sp_us (boolean): true se sp=us (não usado em português)
-- negative_word (string): palavra para usar para sinal negativo, se whole estiver vazio
-- use_one (boolean): false: 2+1/2 → "dois e meio"; true: "dois e um meio"
local function fraction_to_english(whole, numerator, denominator, sp_us, negative_word, use_one)
	if numerator or denominator then
		local finished = (whole == nil or whole == '')
		local sign = ''
		if numerator then
			if finished and numerator:sub(1, 1) == '-' then
				numerator = numerator:sub(2)
				sign = negative_word .. ' '
			end
		else
			numerator = '1'
		end
		if not numerator:match('^%d+$') or not denominator or not denominator:match('^%d+$') then
			return 'unsupported', ''
		end
		numerator = tonumber(numerator)
		denominator = tonumber(denominator)
		local dendata = denominators[denominator]
		if not (dendata and 1 <= numerator and numerator <= 99) then
			return 'unsupported', ''
		end
		local numstr, denstr
		local sep = ' '
		if numerator == 1 then
			denstr = dendata[1]
			if finished or use_one then
				numstr = 'um'
			elseif denstr:match('^[aeiouáéíóú]') then
				numstr = 'um'
			else
				numstr = 'um'
			end
			sep = ' '
		else
			numstr = numeral_to_english_less_100(numerator)
			denstr = dendata.plural
			if not denstr then
				denstr = dendata[1] .. 's'
			end
		end
		if finished then
			return 'finished', sign .. numstr .. sep .. denstr
		end
		return 'ok', ' e ' .. numstr .. sep .. denstr
	end
	return 'ok', ''
end

-- Converte um número decimal para texto em português.
-- Retorna nil se uma fração não puder ser convertida (apenas alguns números são suportados para frações).
-- num (string ou nil): o número a converter.
--      Pode ser um decimal arbitrariamente grande, como "-123456789123456789.345", e
--      pode usar notação científica (ex: "1.23E5").
--      Pode falhar para números muito grandes não listados em "groups" como "1E4000".
--      num é nil se não há número inteiro antes de uma fração.
-- numerator (string ou nil): numerador da fração (nil se não há fração)
-- denominator (string ou nil): denominador da fração (nil se não há fração)
-- capitalize (boolean): se deve capitalizar o resultado (ex: 'Um' em vez de 'um')
-- use_and (boolean): se deve usar a palavra 'e' entre dezenas/unidades e casas superiores
-- hyphenate (boolean): se deve hifenizar todas as palavras no resultado, útil como adjetivo
-- ordinal (boolean): se deve produzir um ordinal (ex: 'primeiro' em vez de 'um')
-- plural (boolean): se deve pluralizar o número resultante
-- links: nil: não adicionar links; 'on': linkar "bilhão" e maiores para o artigo Ordens de magnitude;
--        qualquer outro texto: lista de números para linkar (ex: "bilhão,quatrilhão")
-- negative_word: palavra para usar para sinal negativo (normalmente 'negativo' ou 'menos'; nil para usar padrão)
-- round: nil ou '': sem arredondamento; 'on': arredondar para o número de duas palavras mais próximo; 'up'/'down': arredondar para cima/baixo para número de duas palavras
-- zero: palavra para usar para o valor '0' (nil para usar padrão)
-- use_one (boolean): false: 2+1/2 → "dois e meio"; true: "dois e um meio"
local function _numeral_to_english(num, numerator, denominator, capitalize, use_and, hyphenate, ordinal, plural, links, negative_word, round, zero, use_one)
	if not negative_word then
		negative_word = 'negativo'
	end
	local status, fraction_text = fraction_to_english(num, numerator, denominator, not use_and, negative_word, use_one)
	if status == 'unsupported' then
		return nil
	end
	if status == 'finished' then
		local s = fraction_text
		if hyphenate then s = s:gsub("%s", "-") end
		if capitalize then s = s:gsub("^%l", string.upper) end
		return s
	end
	num = scientific_notation_to_decimal(num)
	if round and round ~= '' then
		if round ~= 'on' and round ~= 'up' and round ~= 'down' then
			error("Modo de arredondamento inválido")
		end
		num = round_for_english(num, round)
	end

	local MINUS = '−'
	if num:sub(1, #MINUS) == MINUS then
		num = '-' .. num:sub(#MINUS + 1)
	elseif num:sub(1, 1) == '+' then
		num = num:sub(2)
	end
	local negative = num:find("^%-")
	local decimal_places, subs = num:gsub("^%-?%d*%.(%d+)$", "%1")
	if subs == 0 then decimal_places = nil end
	num, subs = num:gsub("^%-?(%d*)%.?%d*$", "%1")
	if num == '' and decimal_places then num = '0' end
	if subs == 0 or num == '' then error("Numeral decimal inválido") end

	local s = ''
	while #num > 3 do
		if s ~= '' then s = s .. ' ' end
		local group_num = math.floor((#num - 1) / 3)
		local group = groups[group_num]
		local group_digits = #num - group_num * 3
		s = s .. numeral_to_english_less_1000(num:sub(1, group_digits), false, false, false, zero) .. ' '
		if links and (((links == 'on' and group_num >= 3) or links:find(group)) and group_num <= 13) then
			s = s .. '[[Ordens_de_magnitude_(números)#10' .. group_num * 3 .. '|' .. group .. ']]'
		else
			s = s .. group
		end
		num = num:sub(1 + group_digits)
		num = num:gsub("^0*", "")
	end

	if s ~= '' and num ~= '' then
		if #num <= 2 and use_and then
			s = s .. ' e '
		else
			s = s .. ' '
		end
	end
	if s == '' or num ~= '' then
		s = s .. numeral_to_english_less_1000(num, use_and, ordinal, plural, zero)
	elseif ordinal or plural then
		s = s .. standard_suffix(ordinal, plural)
	end

	if decimal_places then
		s = s .. ' vírgula'
		for i = 1, #decimal_places do
			s = s .. ' ' .. ones_position[tonumber(decimal_places:sub(i, i))]
		end
	end

	s = s:gsub("^%s*(.-)%s*$", "%1")
	if ordinal and plural then s = s .. 's' end
	if negative and s ~= zero then s = negative_word .. ' ' .. s end
	s = s:gsub("negativo zero", "zero")
	s = s .. fraction_text
	if hyphenate then s = s:gsub("%s", "-") end
	if capitalize then s = s:gsub("^%l", string.upper) end
	return s
end

local function _numeral_to_english2(args)
	local num = tostring(args.num)

	num = num:gsub("^%s*(.-)%s*$", "%1")
	num = num:gsub(",", "")
	num = num:gsub("^<span[^<>]*></span>", "")
	if num ~= '' then
		if not num:find("^%-?%d*%.?%d*%-?[Ee]?[+%-]?%d*$") then
			local noerr, result = pcall(mw.ext.ParserFunctions.expr, num)
			if noerr then
				num = result
			end
		end
	end

	return _numeral_to_english(
		num,
		args['numerator'],
		args['denominator'],
		args['capitalize'],
		args['use_and'],
		args['hyphenate'],
		args['ordinal'],
		args['plural'],
		args['links'],
		args['negative_word'],
		args['round'],
		args['zero'],
		args['use_one']
	) or ''
end

local p = {
	roman_to_numeral = roman_to_numeral,
	spell_number = _numeral_to_english,
	spell_number2 = _numeral_to_english2,
	english_to_ordinal = english_to_ordinal,
	english_to_numeral = english_to_numeral,
}

function p._roman_to_numeral(frame)
	return roman_to_numeral(frame.args[1])
end

function p._english_to_ordinal(frame)
	return english_to_ordinal(frame.args[1])
end

function p._english_to_numeral(frame)
	return english_to_numeral(frame.args[1])
end

function p.numeral_to_english(frame)
	local args = frame.args
	return _numeral_to_english2{
		['num'] = args[1],
		['numerator'] = args['numerator'],
		['denominator'] = args['denominator'],
		['capitalize'] = args['case'] == 'U' or args['case'] == 'u',
		['use_and'] = args['sp'] ~= 'us',
		['hyphenate'] = args['adj'] == 'on',
		['ordinal'] = args['ord'] == 'on',
		['plural'] = args['pl'] == 'on',
		['links'] = args['lk'],
		['negative_word'] = args['negative'],
		['round'] = args['round'],
		['zero'] = args['zero'],
		['use_one'] = args['one'] == 'one'
	}
end

local function decToHexDigit(dec)
	local dig = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"}
	local div = math.floor(dec / 16)
	local mod = dec - (16 * div)
	if div >= 1 then return decToHexDigit(div) .. dig[mod + 1] else return dig[mod + 1] end
end

function p.decToHex(frame)
	local args = frame.args
	local parent = frame.getParent(frame)
	local pargs = {}
	if parent then pargs = parent.args end
	local text = args[1] or pargs[1] or ""
	local minlength = args.minlength or pargs.minlength or 1
	minlength = tonumber(minlength)
	local prowl = mw.ustring.gmatch(text, "(.-)(%d+)")
	local output = ""
	repeat
		local chaff, dec = prowl()
		if not (dec) then break end
		local hex = decToHexDigit(dec)
		while (mw.ustring.len(hex) < minlength) do hex = "0" .. hex end
		output = output .. chaff .. hex
	until false
	local chaff = mw.ustring.match(text, "(%D+)$") or ""
	return output .. chaff
end

return p