Módulo:generar-pron/cub

De Wikcionario, el diccionario libre

La documentación para este módulo puede ser creada en Módulo:generar-pron/cub/doc

-- Trascripciones fonológicas para Cubeo
-- Autor original: Marquetto
-- Traducido a Lua por Tmagc

local export = {}

local insert = table.insert
local concat = table.concat
local sort = table.sort

local m_str = require("Módulo:String")

local u = m_str.char
local strmatchit = m_str.gmatch
local strlen = m_str.ulen
local substr = m_str.sub
local strfind = m_str.find
local strsubn = m_str.gsub
local strsubrep = m_str.gsub_rep
local strsplit = m_str.split
local strexplode = m_str.explode_utf8
local strlower = m_str.lower
local strstrip = m_str.strip
local strnfd = m_str.toNFD
local strnfc = m_str.toNFC
local strhtml = m_str.encode_html

-- sustitución descartando todo salvo el string retornado
local function strsub(text, pattern, repl, n)
   local t, _ = strsubn(text, pattern, repl, n)
   return t
end

local AC = u(0x0301) -- acute =  ́
local TILDE = u(0x0303) -- tilde =  ̃

local ACCENT_C = "["..AC..TILDE.."]"

local PUNTUACION = "[%(%)%[%]%{%}¡!¿?.,;:–—]"
local PUNTUACION_EXTRA = "[%(%)%[%]%{%}¡!¿?.,;:–—\"“”„‟‘’«»»«‹››‹'´]"

local vowels = "aeiɨou" -- faltarían las tildes y las virgulillas --> las tratamos por separado

local function aislar_diacriticos(text)
	text = strnfd(text)
	text = strsubn(text, "." .. TILDE , {
		["n" .. TILDE] = "ñ",
		["N" .. TILDE] = "Ñ",
	})
	return text
end


local function reorder_accents(text)
	local function reorder_accent_string(accentstr)
		local accents = strexplode(accentstr)
		local accent_order = {
			[TILDE] = 1,
			[AC] = 2,
		}
		sort(accents, function(ac1, ac2)
			return accent_order[ac1] < accent_order[ac2]
		end)
		return concat(accents)
	end
	text = strsub(text, "(" .. ACCENT_C .. "+)", reorder_accent_string)
	-- Remove duplicate accents.
	text = strsubrep(text, "(" .. ACCENT_C .. ")%1", "%1")
	return text
end

local revert_ortography_2 = {    
    ["ch"] = "t͡ʃ",
    ["qu"] = "k",
}

local revert_ortography_1 = {
	["s"] = "t͡ʃ",
    ["v"] = "w",
    ["c"] = "k",
    ["j"] = "h",
    ["ʉ"] = "ɨ",
    ["r"] = "ɾ"
}

local revert_ortography_0 = { --aislamos esto para que no vuelva a sustituir la j por x
    ["y"] = "j",
}

local function normalizar(texto)
	texto = strlower(texto)
	texto = aislar_diacriticos(texto)
	texto = reorder_accents(texto)
	texto = strsubrep(texto, PUNTUACION, " | ") -- convierto lo que delimite fragmentos a los IPA foot boundaries |
	texto = strsubrep(texto, PUNTUACION_EXTRA, "") -- elimino la puntuación restante que haya quedado
	texto = strsubrep(texto, "[%-‐]", " ") --los guiones pasan a ser espacios (austro-húngaro, franco-italiano)

    texto = strsubrep(texto, "%s*|%s*|%s*", " | ") --finalmente, elimino las barras y espacios de más
    texto = strsubrep(texto, "%s+", " ")
	texto = strstrip(texto, "[%s|]+")
	
	return texto
end

-- Syllabification
local function syllabification(word)
    local syllables = {}
    for m in strmatchit(word, "[".."^"..vowels.."]".."*".."["..vowels.."]"..TILDE.."?"..AC.."?") do
        insert(syllables, m)
    end
    
    if #syllables == 0 then
    	syllables = {word}
    end

    return syllables
end

-- Adding primary stress
local function give_accent(word)
    local syllables = syllabification(word)

    local L = #syllables
    local insert_position = -1
    local ignore = false

    if L == 1 then
        insert_position = 1
    else
        for i=1, L do
            if not ignore then
                if strfind(syllables[i], AC) then
                    insert_position = i
                    ignore = true
                else
                    insert_position = 2
                end
            end
            syllables[i] = strsub(syllables[i], AC, "")
        end
    end

    insert(syllables, insert_position, "ˈ")
    return concat(syllables)
end

-- Nasalization
local function nasalize_vowels(word)
    local syllables = syllabification(word)
    local L = #syllables
	
	local NASAL_CONSONANT = "[mñn]"
	local remove_nasal = {
		["m"] = "b",
		["ñ"] = "j",
		["n"] = "d"
	}

    for i=1, L do
        if strfind(syllables[i], NASAL_CONSONANT) then
            syllables[i] = strsub(syllables[i], "["..vowels.."]", "%0"..TILDE)
        end
        syllables[i] = strsub(syllables[i], NASAL_CONSONANT, remove_nasal)
    end

    return strsub(concat(syllables, "."), ".ˈ", "ˈ")
end

local function generar_pron(t)
	t = normalizar(t)
	local convertido = {}
	local fragmentos = strsplit(t, "%s*|%s*")
	
	for _,fragmento in ipairs(fragmentos) do
	    local palabras = strsplit(fragmento, "%s")
		local palabras_convertidas = {}
	    for _,p in ipairs(palabras) do
	        for a, b in pairs(revert_ortography_2) do
	            p = strsub(p, a, b)
	        end
	        for a, b in pairs(revert_ortography_1) do
	            p = strsub(p, a, b)
	        end
	        for a, b in pairs(revert_ortography_0) do
	            p = strsub(p, a, b)
	        end
	        p = give_accent(p)
	        p = nasalize_vowels(p)
	        insert(palabras_convertidas, p)
	    end
	    insert(convertido, concat(palabras_convertidas, " "))
	end
	
    return {{strhtml(strnfc(concat(convertido, " | ")))}}
end

-- Punto de entrada externo, recibe el título de página y los argumentos de plantilla
function export.procesar_pron_args(titulo, args)
	if #args["ayuda"] < 1 then
		args["ayuda"][1] = titulo
	end

	if #args["fone"] < 1 and #args["fono"] < 1 then
		-- local x = pron_abc[args["ayuda"][1]]
		-- if x then
		--	args["tl"] = x
		--	args["ayuda"][1] = x
		-- end
		local A = #args["ayuda"]
		local j = 1 -- indice de la ayuda
		local k = 1 -- cantidad de pronunciaciones insertadas (máximo 9)
		while k <= 9 and j <= A do
			local fono = generar_pron(args["ayuda"][j])
			for i,_ in ipairs(fono) do
				table.insert(args["fono"], fono[i])
				k = k + 1
				if k > 9 then
					break
				end
			end
			j = j + 1
		end

		local rim = args["fono"][1][1]
		rim = aislar_diacriticos(rim)
		rim = strsub(rim, "^.*ˈ(.-)$", "%1")
		rim = strsub(rim, ".-".."(["..vowels.."].*"..")".."$", "%1")
		args["rima"] = strnfc(rim)

	end

	return args

end

return export