Module:rad-pronunciation

Revision as of 21:39, 21 August 2023 by TheNightAvl (talk | contribs)

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_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 ———")
	if args[2] ~= "" then
		while args_frame[p] do
			table.insert(args, args_frame[p])
			mw.log("Argument " .. p .. ": " .. args_frame[p])
			p = p + 1
		end
	end
	
	-- All possible "modes":
	local parameter = {["IPA"] = true, ["syllables"] = true, ["homophones"] = true}
	local register = {["colloquial"] = true, ["formal"] = true}
	local dialect = {["hjádvanþs"] = true, ["coastal"] = true, ["insular"] = true}
	
	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]] then -- mode changer
			mode = args[i]
			mode_dialect = "standard"
			mode_register = "plain"
		elseif dialect[args[i]] then
			mode_dialect = args[i]
			mode_register = "plain"
		elseif register[args[i]] 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.match(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.match(working_syllables, "(%|)") then
					table.insert(syllables, working_syllables)
					mw.log("Syllables: " .. working_syllables .. " registered.")
				end
				working_syllables = ""
			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.match(auto_syllables, "(%|)") then
		table.insert(syllables, 1, auto_syllables)
	end
	
	-- format --
	
	if #IPA["standard"]["plain"] == 0 then
		error("Missing IPA input.")
	end
	
	local IPA_show = "* {{IPA|rad|" .. table.concat(IPA["standard"]["plain"], "|") .. "}}"
	
	for r, _ in pairs(register) do
		if #IPA["standard"][r] > 0 then
			IPA_show = IPA_show .. "\n** (<i>" .. r .. "</i>) {{IPA||" .. table.concat(IPA["standard"][r], "|") .. "}}"
		end
	end
	
	if #IPA["hjádvanþs"]["plain"] > 0 then
		IPA_show = IPA_show .. "\n** {{accent|rad|hjádvanþs}} {{IPA||" .. table.concat(IPA["hjádvanþs"]["plain"], "|") .. "}}"
	end
	for r, _ in pairs(register) do
		if #IPA["hjádvanþs"][r] > 0 then
			IPA_show = IPA_show .. "\n** {{accent|rad|hjádvanþs|" .. r .. "}} {{IPA||" .. table.concat(IPA["hjádvanþs"][r], "|") .. "}}"
		end
	end
	
	dialect["hjádvanþs"] = nil
	
	for d, _ in pairs(dialect) do
			
			if #IPA[d]["plain"] > 0 then
				IPA_show = IPA_show .. "\n** {{accent|rad|" .. d .. "}} {{IPA||" .. table.concat(IPA[d]["plain"], "|") .. "}}"
			end
			
			for r, _ in pairs(register) do
				if #IPA[d][r] > 0 then
					IPA_show = IPA_show .. "\n** {{accent|rad|" .. d .. "|" .. r .. "}} {{IPA||" .. table.concat(IPA[d][r], "|") .. "}}"
				end
			end
	end
	
	local homophones_show = ""
	
	if #homophones  > 0 then
		homophones_show = "\n* Homophones: {{l|rad|" .. table.concat(table.sort(homophones), "}}, {{l|rad|") .. "}}"
	end
	
	local rhymes_show = ""
	
	if #rhymes > 0 then
		rhymes_show = "\n* {{rhymes|rad|" .. table.concat(rhymes, "|") .. "}}"
	end
	
	local syllables_show = ""
		
	if #syllables > 0 then
		syllables_show = "\n* {{syllables|rad|" .. table.concat(syllables, "||") .. "}}"
	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"}})
]]