Module:User:Surjection/urj-pro-IPA

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This is a private module sandbox of Surjection, for their own experimentation. Items in this module may be added and removed at Surjection's discretion; do not rely on this module's stability.


-- This is a toy not meant for serious use.

local export = {}
local langcode = "urj-fin-pro"

local LONG = "ː"
local SEMILONG = "ˑ"
local PRIMARY_STRESS = "ˈ"
local SECONDARY_STRESS = "ˌ"
local AUTO_STRESS = "ˌ"
local NONSYLLABIC = mw.ustring.char(0x032F)
local UNRELEASED = mw.ustring.char(0x031A)
local CENTRAL = mw.ustring.char(0x031E)

local GRAPHEME = ".[" .. mw.ustring.char(0x0300) .. "-" .. mw.ustring.char(0x036F) .. "]*"

local vowels = {
	["a"] = "ɑ", ["ü"] = "y", ["ä"] = "æ", ["ë"] = "ɤ", ["ï"] = "ɯ̟", ["i̮"] = "ɯ",
	["ᴕ"] = "V", ["ɜ"] = "V", ["A"] = "V", ["E"] = "V"
}

local consonants = {
	["ś"] = "ɕ", ["š"] = "ʃ", ["ć"] = "t͡ɕ", ["č"] = "t͡ʃ",
	["d"] = "ð", ["ď"] = "ɟ", ["ń"] = "ɲ", ["ľ́"] = "ʎ", ["x"] = "ɣ",
}

local IPA_VOWELS = ""
local IPA_CONSONANTS = ""

for _, value in pairs(vowels) do IPA_VOWELS = IPA_VOWELS .. value end
for _, value in pairs(consonants) do IPA_CONSONANTS = IPA_CONSONANTS .. value end

local function normalize(text)
	text = mw.ustring.gsub(text, "ǝ", "ə")
	text = mw.ustring.gsub(text, "δ̕", "ď")
	text = mw.ustring.gsub(text, "δ", "d")
	text = mw.ustring.gsub(text, "γ", "x")
	
	local function schwa(m, x) return m .. mw.ustring.gsub(mw.ustring.gsub(x, "ə", "ə̑"), "ə̑̑", "ə̑") end
	text = mw.ustring.gsub(text, "(i̮)([^-]+)", schwa)
	text = mw.ustring.gsub(text, "([aouë])([^-]+)", schwa)
	
	return text
end

function export.convert(text, is_phonetic)
	local prefix = mw.ustring.find(text, "%-$")
	local suffix = mw.ustring.find(text, "^%-")
	
	if prefix then text = mw.ustring.gsub(text, "%-$", "") end
	if suffix then text = mw.ustring.gsub(text, "^%-", "") end
	
	text = normalize(text)
	
	text = mw.ustring.gsub(text, "([kptnmlrśćšč])%1", "%1" .. LONG)
	
	text = mw.ustring.gsub(text, "n([kg])", "ŋ%1")
	text = mw.ustring.gsub(text, "%-", SECONDARY_STRESS)
	
	if is_phonetic then
		text = mw.ustring.gsub(text, "ə̑?", { ["ə"] = "ə̟̆", ["ə̑"] = "ə̠̆" })
	else
		text = mw.ustring.gsub(text, "ə̑?", "ə")
	end

	text = mw.ustring.gsub(text, "i̮", vowels)
	text = mw.ustring.gsub(text, "ᴕ̮̈", "V")
	text = mw.ustring.gsub(text, "[aAEäüïëᴕɜ]", vowels)
	text = mw.ustring.gsub(text, "[śšdďľ́ńx]", consonants)
	
	if is_phonetic then
		-- automatic secondary stress
		local old_text = text
		text = ""
		
		local IPA_VOWEL = "^[" .. IPA_VOWELS .. "]$"
		local vowels_total = 0
		local vowels_found = 0
		local do_not_stress = {}
		local distance = 0
		
		for phone in mw.ustring.gmatch(old_text, GRAPHEME) do
			if mw.ustring.match(phone, IPA_VOWEL) then
				vowels_total = vowels_total + 1
			elseif mw.ustring.match(phone, "[" .. PRIMARY_STRESS .. SECONDARY_STRESS .. "]") then
				do_not_stress[vowels_total] = true
			end
		end
		
		do_not_stress[vowels_total] = true
		
		for phone in mw.ustring.gmatch(old_text, GRAPHEME) do
			if mw.ustring.match(phone, IPA_VOWEL) then
				vowels_found = vowels_found + 1
				if not do_not_stress[vowels_found] then
					if distance == 2 then
						text = text .. AUTO_STRESS
						distance = 0
					else
						distance = distance + 1
					end
				end
			elseif mw.ustring.match(phone, "[" .. PRIMARY_STRESS .. SECONDARY_STRESS .. "]") then
				distance = 0
			end

			text = text .. phone
		end
		
		if mw.ustring.match(text, "^" .. AUTO_STRESS, last_vowel) then
			text = mw.ustring.sub(text, 1, last_vowel - 1) .. mw.ustring.sub(text, last_vowel + 1)
		end
		
		text = mw.ustring.gsub(text, "([" .. IPA_CONSONANTS .. "ćč])" .. AUTO_STRESS, AUTO_STRESS .. "%1")
		
		text = mw.ustring.gsub(text, "([eoɤ])", "%1" .. CENTRAL)
	end
	
	if suffix then
		text = "-" .. text
	else
		text = PRIMARY_STRESS .. text
	end
	text = mw.ustring.gsub(text, " ", " " .. PRIMARY_STRESS)
	
	if prefix then
		text = text .. "-"
	end
	
	text = mw.ustring.gsub(text, "[ćč]", consonants)
	
	return text
end

local use_UPA_stress = true
local UPA_SHORT = mw.ustring.char(0x0306)
local UPA_LONG = mw.ustring.char(0x0304)
local UPA_TIE = mw.ustring.char(0x0361)
local UPA_UNRELEASED = mw.ustring.char(0x2FE)

local UPA_vowels = {
	["ë"] = "e̮",
}

local UPA_consonants = {
	["ć"] = "t͡ś", ["č"] = "t͡š",
	["d"] = "δ", ["x"] = "γ",
}

function export.convert_UPA(text, is_phonetic)
	local prefix = mw.ustring.find(text, "%-$")
	local suffix = mw.ustring.find(text, "^%-")
	
	if prefix then text = mw.ustring.gsub(text, "%-$", "") end
	if suffix then text = mw.ustring.gsub(text, "^%-", "") end
	
	text = normalize(text)
	
	if not is_phonetic then
		-- weak and semi-long consonants were probably allophones
		text = mw.ustring.gsub(text, "[bdg]", {["b"] = "p", ["d"] = "t", ["g"] = "k"})
		text = mw.ustring.gsub(text, SEMILONG, LONG)
	end
	
	text = mw.ustring.gsub(text, "n([kg])", "ŋ%1")
	text = mw.ustring.gsub(text, "%-", SECONDARY_STRESS)
	
	if is_phonetic then
		text = mw.ustring.gsub(text, "ə̑", "ə̭")
	else
		text = mw.ustring.gsub(text, "ə̑", "ə")
	end
	text = mw.ustring.gsub(text, "[ë]", UPA_vowels)
	text = mw.ustring.gsub(text, "[dx]", UPA_consonants)
	
	if is_phonetic then
		-- automatic secondary stress
		local old_text = text
		text = ""
		
		local UPA_VOWEL = "^[aeiouäöü]̮?$"
		local vowels_total = 0
		local vowels_found = 0
		local do_not_stress = {}
		local distance = 0
		
		for phone in mw.ustring.gmatch(old_text, GRAPHEME) do
			if mw.ustring.match(phone, UPA_VOWEL) then
				vowels_total = vowels_total + 1
			elseif mw.ustring.match(phone, "[" .. PRIMARY_STRESS .. SECONDARY_STRESS .. "]") then
				do_not_stress[vowels_total] = true
			end
		end
		
		do_not_stress[vowels_total] = true
		
		for phone in mw.ustring.gmatch(old_text, GRAPHEME) do
			if mw.ustring.match(phone, UPA_VOWEL) then
				vowels_found = vowels_found + 1
				if not do_not_stress[vowels_found] then
					if distance == 2 then
						text = text .. AUTO_STRESS
						distance = 0
					else
						distance = distance + 1
					end
				end
			elseif mw.ustring.match(phone, "[" .. PRIMARY_STRESS .. SECONDARY_STRESS .. "]") then
				distance = 0
			end

			text = text .. phone
		end
		
		if mw.ustring.match(text, "^" .. AUTO_STRESS, last_vowel) then
			text = mw.ustring.sub(text, 1, last_vowel - 1) .. mw.ustring.sub(text, last_vowel + 1)
		end
		
		text = mw.ustring.gsub(text, "([" .. IPA_CONSONANTS .. "γβб])" .. AUTO_STRESS, AUTO_STRESS .. "%1")
	end
	
	if suffix then
		text = "-" .. text
	else
		text = PRIMARY_STRESS .. text
	end
	text = mw.ustring.gsub(text, " ", " " .. PRIMARY_STRESS)
	
	if prefix then
		text = text .. "-"
	end
	
	text = mw.ustring.gsub(text, "[ćč]", UPA_consonants)
	text = mw.ustring.gsub(text, "(t" .. UPA_TIE .. ".)%1", "t%1")
	text = mw.ustring.gsub(text, "ə", "ǝ")
	
    if use_UPA_stress then
        text = mw.ustring.gsub(text, "([ˈˌ])(.-)([aeiouäöü" .. mw.ustring.char(0x0300) .. "-" .. mw.ustring.char(0x036F) .. "]+)", "%2%3%1")
        text = mw.ustring.gsub(text, "ˈ", "·")
        text = mw.ustring.gsub(text, "ˌ", ":")
    end
	
	return text
end

function export.show(frame)
	local title = mw.title.getCurrentTitle().text
	if type(frame) == "table" then
		title = frame:getParent().args[1] or mw.ustring.gsub(title, "^Proto-Finnic/", "")
	end
	
	local phonemic = export.convert(title, false)
	local phonetic = export.convert(title, true)
	
	return require "Module:IPA".format_IPA_full(
		require("Module:languages").getByCode(langcode),
		{
			{pron = "*/" .. phonemic .. "/"},
			{pron = "*[" .. phonetic .. "]"}
		})

end

return export