Module:rad-pronunciation

Revision as of 00:57, 22 August 2023 by TheNightAvl (talk | contribs) (Fixed mode change bug)

This module automatically formats the Radestrian pronunciation section. To use it, add the {{rad-pr}} template to the section.


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

local m_rad_IPA = require("Module:rad-IPA")
local m_rad_syllables = require("Module:rad-syllables")
local m_syllables = require("Module:syllables")
local m_links = require("Module:links")
local m_languages = require("Module:languages")
local m_parameters = require("Module:parameters")

function table.contains(table, element)
  for _, value in pairs(table) do
    if value == element then
      return true
    end
  end
  return false
end

function export.show(frame)
	local args_frame = getArgs(frame)
	local args = {}
	local p = 1
	
	mw.log("——— Processing arguments ———")
		while args_frame[p] do
			if p == 2 and args_frame[p] == "" then break end
			table.insert(args, args_frame[p])
			mw.log("Argument " .. p .. ": " .. args_frame[p])
			p = p + 1
		end
	
	-- All possible "modes":
	local parameter = {["IPA"] = "IPA", ["syllables"] = "syllables", ["homophones"] = "homophones"}
	local register = {["colloquial"] = "colloquial", ["formal"] = "formal"}
	local dialect = {["hjádvanþs"] = "hjádvanþs", ["coastal"] = "coastal", ["insular"] = "insular"}
	
	local word = args[1]
	
	-- get automations --
	local auto_IPA = m_rad_IPA.generate({word})
	local auto_IPA_phon = m_rad_IPA.generate({word, "phon"})
	local auto_IPA_hj = m_rad_IPA.generate({word, "hj"})
	local auto_IPA_phon_hj = m_rad_IPA.generate({word, "hj", "phon"})
	local auto_syllables = m_rad_syllables.generate({word})
	
	-- create data arrays --
	local IPA = {
		["standard"] = {["plain"] = {}, ["colloquial"] = {}, ["formal"] = {},  },
		["hjádvanþs"] = {["plain"] = {}, ["colloquial"] = {}, ["formal"] = {},  },
		["coastal"] = {["plain"] = {}, ["colloquial"] = {}, ["formal"] = {},  },
		["insular"] = {["plain"] = {}, ["colloquial"] = {}, ["formal"] = {},  },
	}
	local rhymes = {}
	local syllables = {}
	local homophones = {}

	
	local function insert_rhyme(input, pos)
		local new_rhyme = m_rad_IPA.get_rhyme(input)
		if not (new_rhyme == nil or table.contains(rhymes, new_rhyme)) then
			if pos == nil then
				table.insert(rhymes, new_rhyme)
			else
				table.insert(rhymes, pos, new_rhyme)
			end
			mw.log("Rhyme registered: " .. new_rhyme)
		end
	end
	
	-- get data --
	local IPA_no_auto = false
	local syllables_no_auto = false
	local mode = "IPA"
	local mode_dialect = "standard"
	local mode_register = "plain"
	local working_syllables = ""
	
	for i = 2, #args do
		mw.log("Processing argument " .. i .. ".")
		if parameter[args[i]] ~= nil then -- mode changer
			mode = args[i]
			mode_dialect = "standard"
			mode_register = "plain"
		elseif dialect[args[i]] ~= nil then
			mode_dialect = args[i]
			mode_register = "plain"
		elseif register[args[i]] ~= nil then
			mode_register = args[i]
		
		-- begin adding data to tables -- 
		elseif mode == "IPA" then
			if args[i] == "no auto" then
				IPA_no_auto = true
			else
				table.insert(IPA[mode_dialect][mode_register], args[i])
				mw.log("IPA: added '" .. args[i] .. "' to IPA[" .. mode_dialect .. "][" .. mode_register .. "]")
				
				if mode_dialect == "standard" or mode_dialect == "hjádvanþs" then
					local just_phonemic = mw.ustring.gmatch(args[i], "%/(.*)%/")
					insert_rhyme(just_phonemic)
				end
			end
		elseif mode == "syllables" then
			if args[i] == "no auto" then
				syllables_no_auto = true
			elseif args[i] == "" then
				if mw.ustring.find(working_syllables, "(%|)") then
					table.insert(syllables, working_syllables)
					mw.log("Syllables: " .. working_syllables .. " registered.")
				end
				working_syllables = ""
			elseif args[i+1] == nil then
				working_syllables = working_syllables .. "|" .. args[i]
				if mw.ustring.find(working_syllables, "(%|)") then
					table.insert(syllables, working_syllables)
					mw.log("Syllables: " .. working_syllables .. " registered.")
				end
			else
				working_syllables = working_syllables .. "|" .. args[i]
			end
		elseif mode == "homophones" then
			table.insert(homophones, args[i])
			mw.log("Homophones: " .. args[i] .. " registered.")
		end
	end
	
	-- add auto values --
	if IPA_no_auto == false then
		table.insert(IPA["standard"]["plain"], 1, "/" .. auto_IPA .. "/")
		insert_rhyme(auto_IPA, 1)
		
		if auto_IPA_phon ~= auto_IPA then
			IPA["standard"]["plain"][1] = IPA["standard"]["plain"][1] .. " [" .. auto_IPA_phon .. "]"
		end
		
		if auto_IPA_hj ~= auto_IPA then
			table.insert(IPA["hjádvanþs"]["plain"], 1, "/" .. auto_IPA_hj .. "/")
			insert_rhyme(auto_IPA_hj, 2)
			
			if auto_IPA_phon_hj ~= auto_IPA_hj then
				IPA["hjádvanþs"]["plain"][1] = IPA["hjádvanþs"]["plain"][1] .. " [" .. auto_IPA_phon_hj .. "]"
			end
		end
	end
	
	if syllables_no_auto == false and mw.ustring.find(auto_syllables, "(%|)") then
		table.insert(syllables, 1, auto_syllables)
	end
	
	-- format --
	
	if #IPA["standard"]["plain"] == 0 then
		error("Missing IPA input.")
	end
	
	local args_temp = {}
	local accent_temp = {}
	
	args_temp = IPA["standard"]["plain"]
	table.insert(args_temp, 1, "rad")
	local IPA_show = "* " .. frame:expandTemplate{ title = "IPA", args = args_temp }
	
	for r, _ in pairs(register) do
		if #IPA["standard"][r] > 0 then
			args_temp = IPA["standard"][r]
			table.insert(args_temp, 1, "")
			IPA_show = IPA_show .. "\n** (<i>" .. r .. "</i>) " .. frame:expandTemplate{ title = "IPA", args = args_temp }
		end
	end
	
	if #IPA["hjádvanþs"]["plain"] > 0 then
		args_temp = IPA["hjádvanþs"]["plain"]
		table.insert(args_temp, 1, "")
		IPA_show = IPA_show .. "\n** " .. frame:expandTemplate{ title = "accent", args = { "rad", "hjádvanþs" } } .. " " .. frame:expandTemplate{ title = "IPA", args = args_temp }
	end
	for r, _ in pairs(register) do
		if #IPA["hjádvanþs"][r] > 0 then
			args_temp = IPA["hjádvanþs"][r]
			table.insert(args_temp, 1, "")
			IPA_show = IPA_show .. "\n** " .. frame:expandTemplate{ title = "accent", args = { "rad", "hjádvanþs", r } } .. " " .. frame:expandTemplate{ title = "IPA", args = args_temp }
		end
	end
	
	dialect["hjádvanþs"] = nil
	
	for d, _ in pairs(dialect) do
			
			if #IPA[d]["plain"] > 0 then
				args_temp = IPA[d]["plain"]
				table.insert(args_temp, 1, "")
				IPA_show = IPA_show .. "\n** " .. frame:expandTemplate{ title = "accent", args = { "rad", d } } .. " " .. frame:expandTemplate{ title = "IPA", args = args_temp }
			end
			
			for r, _ in pairs(register) do
				if #IPA[d][r] > 0 then
					args_temp = IPA[d][r]
					table.insert(args_temp, 1, "")
					IPA_show = IPA_show .. "\n** " .. frame:expandTemplate{ title = "accent", args = { "rad", d, r } } .. " " .. frame:expandTemplate{ title = "IPA", args = args_temp }
				end
			end
	end
	
	local homophones_show = ""
	
	if #homophones  > 0 then
		homophones_show = "\n* Homophones:"
		for _, item in ipairs(table.sort(homophones)) do
			homophones_show = homophones_show .. ", " .. m_links.full_link{ term = item, language = m_languages.get_by_code("rad") }
		end
		homophones_show = mw.ustring.gsub(homophones_show, "(%:%,)", "%:")
	end
	
	local rhymes_show = ""
	
	if #rhymes > 0 then
		rhymes_show = "\n* Rhymes:"
		
		for _, rhyme in ipairs(rhymes) do
			rhymes_show = rhymes_show .. " <span class=\"IPA\">[[:Category:Rhymes:Radestrian/" .. rhyme .. "|-" .. rhyme .. "]]</span>[[Category:Rhymes:Radestrian/" .. rhyme .. "]]"
		end
	end
	
	local syllables_show = ""
	
	local function split_syllables(input)
        local syllables = {}
        for syl in mw.ustring.gmatch(input, "([^%|]+)") do
                table.insert(syllables, syl)
        end
        return syllables
	end
		
	if #syllables > 0 then
		syllables_show = "\n* "
		args_temp = { "rad" }
		for set = 1, #syllables do
			if set > 1 then
				table.insert(args_temp, "")
			end
			local working_syllables = split_syllables(syllables[set])
			for syl = 1, #working_syllables do
				table.insert(args_temp, working_syllables[syl])
			end
		end
		syllables_show = syllables_show .. frame:expandTemplate{ title = "syllables", args = args_temp }
	end
	
	return IPA_show .. homophones_show .. rhymes_show .. syllables_show
	
end

return export

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