Module:rad-IPA

From Laenkea
Revision as of 13:50, 18 August 2023 by TheNightAvl (talk | contribs) (Added voicing assimilation)
Jump to navigation Jump to search

See {{rad-IPA}}.


local export = {}
local getArgs = require('Module:Arguments').getArgs

local data = {
	["a"] = {
		["i"] = "ai",
		["o"] = {
			["i"] = "ɔi",
			[false] = "ɔː",
		},
		["u"] = "au",
		[false] = "a",
	},
	["á"] = "aː",
	["ả"] = "aːː",
	["â"] = "ɤ",
	["âi"] = "ɤi",
	["b"] = "b",
	["c"] = "ts",
	["d"] = {
		["x"] = "dʒ",
		["z"] = "dz",
		[false] = "d",
	},
	["ð"] = "ð",
	["e"] = {
		["a"] = "æː",
		["i"] = "ei",
		[false] = "ɛ",
	},
	["é"] = "eː",
	["f"] = "f",
	["g"] = "ɡ",
	["h"] = "h",
	["ħ"] = "ɣ",
	["i"] = {
		["e"] = {
			["a"] = "ia",
			["j"] = "iej",
			[false] = "ie",
		},
		[false] = "i",
	},
	["í"] = "iː",
	["ỉ"] = {
		["e"] = "iːe",
		[false] = "iːː",
	},
	["j"] = "j",
	["ĵ"] = "ĵ",
	["k"] = "k",
	["ķ"] = "tʃ",
	["l"] = "l",
	["m"] = "m",
	["n"] = "n",
	["ņ"] = "ŋ",
	["o"] = {
		["a"] = "ɔa",
		[false] = "ɔ",
	},
	["ó"] = "oː",
	["ø"] = {
		["a"] = "œa",
		["i"] = "ei",
		[false] = "œ",
	},
	["p"] = "p",
	["q"] = "k",
	["r"] = "r",
	["s"] = "s",
	["ș"] = "ʃ",
	["t"] = "t",
	["u"] = {
		["i"] = "ɤi",
		["o"] = {
			["a"] = "ua",
			["j"] = "uoj",
			[false] = "uo",
		},
		[false] = "u",
	},
	["ù"] = "ù",
	["û"] = "ɤ",
	["ú"] = "uː",
	["ủ"] = {
		["o"] = "uːo",
		[false] = "uːː",
	},
	["ū"] = "ū",
	["v"] = "v",
	["w"] = "w",
	["x"] = "ʒ",
	["y"] = {
		["e"] = {
			["a"] = "ia",
			["j"] = "yej",
			[false] = "ie",
		},
		[false] = "i",
	},
	["ỳ"] = "ỳ",
	["z"] = "z",
	["þ"] = "θ",
	["·"] = "·",
	["-"] = "-",
	[" "] = " ",
	["."] = "|",
}
data["à"] = data["a"]
data["è"] = data["e"]
data["ì"] = data["i"]
data["ò"] = data["o"]
data["ý"] = data["í"]
data["ỷ"] = data["ỉ"]

data[","] = data["."]
data[":"] = data["."]
data["ș"] = data["."]
data["!"] = data["."]
data["?"] = data["."]

local irregular = {
	["eurú"] = "ørú",
	["eurov"] = "ørov",
	["heņre"] = "heņgre",
	["nrao"] = "drao",
	["ryņl"] = "ryņgl",
}

-- ALL PHONES --
local valid_phone = {
	["a"] = true, ["aː"] = true, ["aːː"] = true, ["æː"] = true, ["ai"] = true,
	["au"] = true, ["b"] = true, ["ç"] = true, ["d"] = true, ["ð"] = true,
	["dz"] = true, ["dʒ"] = true, ["eː"] = true, ["ei"] = true, ["ɛ"] = true,
	["ɤ"] = true, ["ɤi"] = true, ["f"] = true, ["ɡ"] = true, ["ɣ"] = true,
	["h"] = true, ["i"] = true, ["iː"] = true, ["iːː"] = true, ["iːe"] = true,
	["ia"] = true, ["ie"] = true, ["j"] = true, ["k"] = true, ["l"] = true,
	["m"] = true, ["n"] = true, ["ŋ"] = true, ["oː"] = true, ["œ"] = true,
	["œa"] = true, ["øi"] = true, ["ɔ"] = true, ["ɔː"] = true, ["ɔa"] = true,
	["ɔi"] = true, ["p"] = true, ["r"] = true, ["s"] = true, ["ʃ"] = true,
	["t"] = true, ["ts"] = true, ["tʃ"] = true, ["u"] = true, ["uː"] = true,
	["uːː"] = true, ["uːo"] = true, ["ua"] = true, ["uo"] = true, ["v"] = true,
	["w"] = true, ["y"] = true, ["z"] = true, ["ʒ"] = true, ["θ"] = true, 
}
	local valid_phone_temp = {"iːj", "uːj", "yːj", "ù", "ū", "ĵ"}
		for _, temp in ipairs(valid_phone_temp) do
			valid_phone[temp] = true
		end
		
-- CONSONANT GROUPS --

local consonant = {
	["b"] = true, ["ç"] = true, ["d"] = true, ["ð"] = true, ["dz"] = true,
	["dʒ"] = true, ["f"] = true, ["ɡ"] = true, ["ɣ"] = true, ["h"] = true,
	["j"] = true, ["k"] = true, ["l"] = true, ["m"] = true, ["n"] = true,
	["ŋ"] = true, ["p"] = true, ["r"] = true, ["s"] = true, ["ʃ"] = true,
	["t"] = true, ["ts"] = true, ["tʃ"] = true, ["v"] = true, ["w"] = true,
	["z"] = true, ["ʒ"] = true, ["θ"] = true, 
}
	local consonant_temp = {"ĵ"}
		for _, temp in ipairs(consonant_temp) do
			consonant[temp] = true
		end
		
local obstruent = {
	["b"] = true, ["ç"] = true, ["d"] = true, ["ð"] = true, ["dz"] = true,
	["dʒ"] = true, ["f"] = true, ["ɡ"] = true, ["ɣ"] = true, ["h"] = true,
	["k"] = true, ["p"] = true, ["s"] = true, ["ʃ"] = true,
	["t"] = true, ["ts"] = true, ["tʃ"] = true, ["v"] = true,
	["z"] = true, ["ʒ"] = true, ["θ"] = true, 
}

	local obstruent_voiced = {
		["b"] = true, ["ç"] = false, ["d"] = true, ["ð"] = true, ["dz"] = true,
		["dʒ"] = true, ["f"] = false, ["ɡ"] = true, ["ɣ"] = true, ["h"] = false,
		["k"] = false, ["p"] = false, ["s"] = false, ["ʃ"] = false,
		["t"] = false, ["ts"] = false, ["tʃ"] = false, ["v"] = true,
		["z"] = true, ["ʒ"] = true, ["θ"] = false, 
	}
	
		local obstruent_devoice = {
			["b"] = "p", ["ç"] = "ç", ["d"] = "t", ["ð"] = "θ", ["dz"] = "ts",
			["dʒ"] = "tʃ", ["f"] = "f", ["ɡ"] = "k", ["ɣ"] = "h", ["h"] = "h",
			["k"] = "k", ["p"] = "p", ["s"] = "s", ["ʃ"] = "ʃ",
			["t"] = "t", ["ts"] = "ts", ["tʃ"] = "tʃ", ["v"] = "f",
			["z"] = "s", ["ʒ"] = "ʃ", ["θ"] = "θ", 
		}
		
		local obstruent_voice = {
			["b"] = "b", ["ç"] = "j", ["d"] = "d", ["ð"] = "ð", ["dz"] = "dz",
			["dʒ"] = "dʒ", ["f"] = "v", ["ɡ"] = "ɡ", ["ɣ"] = "ɣ", ["h"] = "h",
			["k"] = "ɡ", ["p"] = "b", ["s"] = "z", ["ʃ"] = "ʒ",
			["t"] = "d", ["ts"] = "dz", ["tʃ"] = "dʒ", ["v"] = "v",
			["z"] = "z", ["ʒ"] = "ʒ", ["θ"] = "ð", 
		}
		
local sibilant = {
	["dz"] = true, ["dʒ"] = true ,
	["s"] = true, ["ʃ"] = true,
	["ts"] = true, ["tʃ"] = true,
	["z"] = true, ["ʒ"] = true,
}

	local sibilant_alv = {
		["dz"] = true,
		["s"] = true,
		["ts"] = true,
		["z"] = true,
	}

	local sibilant_post = {
		["dʒ"] = true,
		["ʃ"] = true,
		["tʃ"] = true,
		["ʒ"] = true,
	}
	
	local sibilant_alv_to_post = {
		["dz"] = "dʒ",
		["s"] = "ʃ",
		["ts"] = "tʃ",
		["z"] = "ʒ",
	}
		
local glide = {
	["j"] = true, ["w"] = true,
}
	local glide_temp = {"ĵ"}
		for _, temp in ipairs(glide_temp) do
			glide[temp] = true
		end
		
local iotate = {
	["d"] = "dʒ", ["dz"] = "dʒ",
	["ɡ"] = "j", ["ɣ"] = "j",
	["h"] = "ç",
	["k"] = "tʃ",
	["l"] = "j",
	["s"] = "ʃ",
	["t"] = "tʃ", ["ts"] = "tʃ",
	["z"] = "ʒ",
}

-- VOWEL GROUPS --

local vowel = {
	["a"] = true, ["aː"] = true, ["aːː"] = true, ["æː"] = true, ["ai"] = true, ["au"] = true,
	["eː"] = true, ["ei"] = true, ["ɛ"] = true, ["ɤ"] = true, ["ɤi"] = true, ["i"] = true,
	["iː"] = true, ["iːː"] = true, ["iːe"] = true, ["ia"] = true, ["ie"] = true, ["oː"] = true,
	["œ"] = true, ["œa"] = true, ["øi"] = true, ["ɔ"] = true, ["ɔː"] = true, ["ɔa"] = true,
	["ɔi"] = true, ["u"] = true, ["uː"] = true, ["uːː"] = true, ["uːo"] = true, ["ua"] = true,
	["uo"] = true, ["y"] = true,
}
	local vowel_temp = {"iːj", "uːj", "yːj", "ù", "ū"}
		for _, temp in ipairs(vowel_temp) do
			vowel[temp] = true
		end
		
-- MISC --

local boundary = {
	[" "] = true, ["|"] = true, ["·"] = true,
}

local IPA = {}

-- PROCESS FUNCTIONS --

function generate_IPA(word)
	local s = mw.ustring.lower(word)
	
	-- Replace irregulars --
	
	for toReplace, ReplaceKey in pairs(irregular) do
		if mw.ustring.match(s, toReplace) then
			s = mw.ustring.gsub(s, toReplace, ReplaceKey)
			mw.log("Irregular spelling <" .. toReplace .. "> recognised. Treating as <" .. ReplaceKey .. ">.")
		end
	end
	
	local s_len = mw.ustring.len(s)
	IPA = {}

	local split_s = {}
	for i = 1, s_len do
	  split_s[i] = mw.ustring.sub(s, i,i)
	end
	
	-- generate_IPA: mw.log("————— BEGINNING BASE GENERATION —————")

	if s_len == 0 then
		error("Empty input.")
	end

	while s_len > 0 do
		local getData = {}
		local multiMatch = false
		local i_iteration = -2

		-- generate_IPA: mw.log("=========================\nCURRENT TEST STRING: <".. mw.ustring.upper(s) .. ">")

		if s_len < 3 then
			i_iteration = 1 - s_len
		end

		for i = i_iteration, 0 do
		  -- generate_IPA: mw.log("————— <" .. split_s[s_len + i] .. "> selected. (i = " .. i .. ") —————")
		  getData = data[split_s[s_len + i]]
		  local deadEnd = false
		  
		  if data[split_s[s_len + i]] == nil then
			error("'" .. split_s[s_len + i] .. "' is an invalid character.")
		  end
		  
		  while type(getData) == "table" do
			if i == 0 then
				if getData[false] then
				  -- generate_IPA: mw.log("Singular index recognised.")
				  getData = getData[false]
				  -- generate_IPA: mw.log("Index acquired: " .. getData)
				else
				  error(split_s[s_len] .. " is an invalid character.")
				end
			else
			  -- generate_IPA: mw.log("Tabular index recognised.")
			  for j = 1, 0 - i do
				--[[
				local currentCombo = ""
				if i == -2 and j == 2 then
					currentCombo = split_s[s_len + i + j - 2] .. " + " .. split_s[s_len + i + j - 1] .. " + " .. split_s[s_len + i + j]
					else
					currentCombo = split_s[s_len + i + j - 1] .. " + " .. split_s[s_len + i + j]
				end
				-- generate_IPA: mw.log("Testing " .. currentCombo) ]]
				if getData[split_s[s_len + i + j]] then
				  getData = getData[split_s[s_len + i + j]]
				  -- generate_IPA: mw.log("Combination recognised: " .. currentCombo .. " (j = " .. j .. ")")
				  if type(getData) == "string" then
					
					if j + i == 0 then
					  -- generate_IPA: mw.log("Index acquired: " .. getData)
					  multiMatch = true
					  break
					else
					  -- generate_IPA: mw.log('Non-final index: dead end.')
					  getData = {}
					  deadEnd = true
					  break
					end
					
				  elseif j + i == 0 and getData[false] then
					getData = getData[false]
					-- generate_IPA: mw.log("Index acquired: " .. getData)
					multiMatch = true
					break
				  elseif j + i == 0 and not getData[false] then
					error("data[" .. table.concat(getData, "][") .. "][false] is missing." )
				  else
					-- generate_IPA: mw.log("Target still tabular: reiterating.")
				  end
				  else
					-- generate_IPA: mw.log('Dead end.')
					getData = {}
					deadEnd = true
					break
				end
			  end
			  if type(getData) == "table" then break end
			end
		  end
		  if type(getData) == "string" and (i == 0 or multiMatch == true) then
			  -- generate_IPA: mw.log("Target acquired of length " .. 1 - i .. ", converting to [" .. getData .. "].")
			  table.insert(IPA, 1, getData)
			  s = mw.ustring.sub(s, 1, s_len + i - 1)
			  s_len = mw.ustring.len(s)
			  break
			elseif deadEnd == false then
				-- generate_IPA: mw.log('Non-final index: dead end.')
		  end
		end
	end

	-- generate_IPA: mw.log('————— STRING EXHAUSTED —————')
	mw.log("Base generation result: [" .. table.concat(IPA,"][") .. "]")
	return IPA
end

function resolve_vowels(phones)
	local working_phones = phones
	mw.log("————— BEGINNING VOWEL RESOLUTION —————")

	local i = 0
	while true do
		i = i + 1
		local p_prev = working_phones[i - 1]
		local p_current = working_phones[i]
		local p_next = working_phones[i + 1]
		local p_next2 = working_phones[i + 2]
		local p_next3 = working_phones[i + 3]
		local toResolve = false
		
		if p_current == nil then break end
		
		local function p_Resolve(p_new)
			working_phones[i] = p_new
			mw.log("[" .. p_current .. "] resolved to [" .. p_new .. "] in position ".. i .. ".")
			p_new = ""
		end
		
		local function p_Convert(p_new)
			working_phones[i] = p_new
			mw.log("[" .. p_current .. "] converted to [" .. p_new .. "] in position ".. i .. ".")
			p_current = p_new
			p_new = ""
		end
		
		-- Resolution of [aù] --
		if p_prev == "a" and p_current == "ù" then
			mw.log("<aù> recognised in position " .. i .. ". Converting to resolvable [u].")
			p_Convert("u")
			p_current = "u"
		end
		
		
		-- Resolution of [u], [ù] and [ū] --
		if p_current == "u" then
			mw.log("[u] found in position " .. i .. ".")
			if consonant[p_next] then
				if glide[p_next2] then
					if not vowel[p_next3] then
						mw.log("ɤCj!V environment identified.")
						p_Resolve("ɤ")
					end
				elseif not vowel[p_next2] and not glide[p_next2] then
					mw.log("ɤC!V environment identified.")
					p_Resolve("ɤ")
				end
			end
			elseif p_current == "ù" or p_current == "ū" then
				mw.log("Fixed [u] found in position " .. i .. ".")
				working_phones[i] = "u"
		end
		
		if toResolve == true then
			working_phones[i] = "ɤ"
			mw.log("[u] → [ɤ] in position ".. i .. ".")
		end
		
		-- Resolution of <ei> and  <øi> --
		if p_current == "ei" then
			for j = 1, i do
				local check_phone = working_phones[i - j]
				if boundary[check_phone] or check_phone == nil then
					mw.log("Initial [ei] found in position" .. i .. ".")
					p_Resolve("ai")
					break
				elseif not consonant[check_phone] then
					break
				end
			end
		end
		
		if vowel[p_next] then
		
			-- Resolution of prevocalic <iej>, <uoj> and <yej> -- 
			
			if p_current == "iej" or p_current == "yej" or p_current == "uoj" then
				mw.log("Pre-vocalic <" .. p_current .. "> found in position " .. i .. ".")
				if p_current == "uoj" then
					p_Resolve("uo")
					table.insert(working_phones, i + 1, "j")
					mw.log("[j] inserted to position " .. i + 1 .. ".")
				else
					p_Resolve("iː")
				end
			end
			
			-- Resolution of prevocalic [ie] and [uo] -- 
			
			if p_current == "ie" then
				p_Resolve("iː")
			elseif p_current == "uo" then
				p_Resolve("uː")
			end
		
		else
		
			-- Resolution of non-prevocalic <iej>, <uoj> and <yej> -- 
		
			for toResolve, ResolveKey in pairs({["iej"] = "ei", ["uoj"] = "ɔi", ["yej"] = "øi"}) do
				if p_current == toResolve then
					p_Resolve(ResolveKey)
				end
			end
			
		end
		
	end
	
	-- Removes placeholder phones from data --
	
	for _, temp in ipairs(vowel_temp) do
		valid_phone[temp] = nil
		vowel[temp] = nil
	end
	
	mw.log("Vowel resolution result: [" .. table.concat(working_phones,"][") .. "]")
	return working_phones
	
end

function resolve_consonants(phones)
	local working_phones = phones
	mw.log("————— BEGINNING CONSONANT RESOLUTION —————")

	local i = 0
	while true do
		i = i + 1
		local p_prev = working_phones[i - 1]
		local p_current = working_phones[i]
		local p_next = working_phones[i + 1]
		local p_next2 = working_phones[i + 2]
		local p_next3 = working_phones[i + 3]
		local toResolve = false
		
		if p_current == nil then break end
		
		local function p_Resolve(p_new)
			working_phones[i] = p_new
			mw.log("[" .. p_current .. "] resolved to [" .. p_new .. "] in position ".. i .. ".")
			p_new = ""
		end
		
		local function p_Convert(p_new)
			working_phones[i] = p_new
			mw.log("[" .. p_current .. "] converted to [" .. p_new .. "] in position ".. i .. ".")
			p_current = p_new
			p_new = ""
		end
		
		local function p_RemoveNext()
			table.remove(working_phones, i + 1)
			p_next = working_phones[i + 1]
			p_next2 = working_phones[i + 2]
			p_next3 = working_phones[i + 3]
		end
		
		-- Resolution of iotation --
		
		if iotate[p_current] and p_next == "j" then
			mw.log("[" .. p_current .. "][j] found in position " .. i .. ".")
			p_Convert(iotate[p_current])
			p_RemoveNext()
			mw.log("[j] removed from position " .. i + 1 .. ".")
			elseif p_current == "ĵ" then
				p_Convert("j")
		end
		
		-- Resolution of Ss, Sș, ts, ds, tș, dș (progressive voicing assimilation) --
		
		if p_next == "s" then
			if sibilant[p_current] then
				p_RemoveNext()
				mw.log("[s] removed from position " .. i + 1 .. " following a sibilant.")
				elseif p_current == "t" then
				p_Convert("ts")
				p_RemoveNext()
				mw.log("[t][s] → [ts] in position " .. i .. ".")
				elseif p_current == "d" then
				p_Convert("dz")
				p_RemoveNext()
				mw.log("[d][s] → [dz] in position " .. i .. ".")
			end
		elseif p_next == "ʃ" then
			if sibilant_post[p_current] == true then
				p_RemoveNext()
				mw.log("[ʃ] removed from position " .. i + 1 .. " following a postalveolar sibilant.")
				elseif sibilant_alv[p_current] then
					mw.log("[" .. p_current .. "][ʃ] → [" .. sibilant_alv_to_post[p_current] .. "] in position " .. i .. ".")
					p_Convert(sibilant_alv_to_post[p_current])
					p_RemoveNext()
				elseif p_current == "t" then
					p_Convert("tʃ")
					p_RemoveNext()
					mw.log("[t][ʃ] → [tʃ] in position " .. i .. ".")
				elseif p_current == "d" then
					p_Convert("dʒ")
					p_RemoveNext()
					mw.log("[d][ʃ] → [dʒ] in position " .. i .. ".")
			end
		end
		
		if obstruent[p_current] == true then
			mw.log("Obstruent [" .. p_current .. "] found in position " .. i .. ". Searching for cluster.")
			local final_i = i
			
			-- find voicing of final obstruent in cluster --
			for j = i + 1, #working_phones do
				local check_phone = working_phones[j]
				if obstruent[check_phone] == true then
					final_i = j
				else break
				end
			end
			
			if final_i > i then -- if cluster recognised --
				final_obs = working_phones[final_i]
				mw.log("Final obstruent in cluster is [" .. final_obs .. "], cluster length " .. final_i - i + 1 .. ".")
				
				-- assimilate --
				
				if not obstruent_voiced[p_current] == obstruent_voiced[final_obs] then
					mw.log("Voicing mismatch found in cluster at position " .. i .. ".")
					if obstruent_voiced[final_obs] == true then
						p_Convert(obstruent_voice[p_current])
						else
						p_Convert(obstruent_devoice[p_current])
					end
				else
					mw.log("No voicing mismatch found.")
				end
				else
					mw.log("No cluster found.")
			end
		end
		
		-- Resolution of geminates --
		
	end
		
	-- Removes placeholder phones from data --
	
	for _, temp in ipairs(consonant_temp) do
		valid_phone[temp] = nil
		consonant[temp] = nil
		glide[temp] = nil
	end
	
	mw.log("Vowel resolution result: [" .. table.concat(working_phones,"][") .. "]")
	return working_phones
	
end


function export.generate(frame)
	local args = getArgs(frame)
	local outputIPA = generate_IPA(args[1])
	local hj = args[2] or false
		if hj == "false" then
			hj = false
		end
	
	outputIPA = resolve_vowels(outputIPA)
	outputIPA = resolve_consonants(outputIPA)
	outputIPA = table.concat(outputIPA,"][")
	
	return "[" .. outputIPA .. "]"
end

return export

--[[
Debug console test string:
=p.generate(mw.getCurrentFrame():newChild{title="whatever",args={"rjaovs"}})
]]