Jump to content

Module:Cher-common

From Wiktionary, the free dictionary


local export = {}

local m_str_utils = require("Module:string utilities")
local m_tbl = require("Module:table")

local find = m_str_utils.find
local format = m_str_utils.format

-- map Latin consonants to Cherokee syllables where each consonant maps to a corresponding vowel
export.syl_list = {
	-- order of syllables: a, e, i, o, u, v
	[""] = {"Ꭰ", "Ꭱ", "Ꭲ", "Ꭳ", "Ꭴ", "Ꭵ"},
	k = {"Ꭷ"},
	g = {"Ꭶ", "Ꭸ", "Ꭹ", "Ꭺ", "Ꭻ", "Ꭼ"},
	h = {"Ꭽ", "Ꭾ", "Ꭿ", "Ꮀ", "Ꮁ", "Ꮂ"},
	l = {"Ꮃ", "Ꮄ", "Ꮅ", "Ꮆ", "Ꮇ", "Ꮈ"},
	m = {"Ꮉ", "Ꮊ", "Ꮋ", "Ꮌ", "Ꮍ", "Ᏽ"}, -- Ᏽ is obsolete
	n = {"Ꮎ", "Ꮑ", "Ꮒ", "Ꮓ", "Ꮔ", "Ꮕ"},
	hn = {"Ꮏ"},
	qu = {"Ꮖ", "Ꮗ", "Ꮘ", "Ꮙ", "Ꮚ", "Ꮛ"},
	s = {"Ꮜ", "Ꮞ", "Ꮟ", "Ꮠ", "Ꮡ", "Ꮢ"},
	d = {"Ꮣ", "Ꮥ", "Ꮧ", "Ꮩ", "Ꮪ", "Ꮫ"},
	t = {"Ꮤ", "Ꮦ", "Ꮨ"},
	dl = {"Ꮬ"},
	tl = {"Ꮭ", "Ꮮ", "Ꮯ", "Ꮰ", "Ꮱ", "Ꮲ"},
	ts = {"Ꮳ", "Ꮴ", "Ꮵ", "Ꮶ", "Ꮷ", "Ꮸ"},
	w = {"Ꮹ", "Ꮺ", "Ꮻ", "Ꮼ", "Ꮽ", "Ꮾ"},
	y = {"Ꮿ", "Ᏸ", "Ᏹ", "Ᏺ", "Ᏻ", "Ᏼ"}
}

-- vowel order in Cherokee
export.vowel_order = {"a", "e", "i", "o", "u", "v"}

-- generates the relevant Cherokee syllable by either swapping the vowel or consonant
-- of given syllable or building the syllable from a vowel/consonant pair
-- source can be a lowercase Latin char or syllable, but target must be a lowercase Latin char
function export.gen_syll(source, target)
	-- check if target is not a lowercase Latin char
	if not find(target, "^%l+$") then
		error("Target must be a lowercase Latin letter")
	end

	-- verify if source and target are vowels
	local source_is_vowel = m_tbl.contains(export.vowel_order, source)
	local target_is_vowel = m_tbl.contains(export.vowel_order, target)

	-- building syllable from Latin chars
	if find(source, "^%l+$") then
		-- check if source and target aren't both vowels or both consonants
		if source_is_vowel == target_is_vowel then
			error("The source and target should not be both consonants or both vowels")
		end

		-- source is vowel, target is consonant
		if source_is_vowel then
			local vowel_i = m_tbl.keyFor(export.vowel_order, source)
			local cons_sylls = export.syl_list[target]

			if not cons_sylls then
				error(format("'%s' is not a valid consonant", target))
			end

			return vowel_i <= #cons_sylls and cons_sylls[vowel_i] or nil
		-- source is consonant, target is vowel
		else
			local vowel_i = m_tbl.keyFor(export.vowel_order, target)
			local cons_sylls = export.syl_list[source]

			if not cons_sylls then
				error(format("'%s' is not a valid consonant", source))
			end

			return vowel_i <= #cons_sylls and cons_sylls[vowel_i] or nil
		end
	end

	-- swapping vowel or consonant for Cherokee syllable
	if target_is_vowel then
		-- swapping vowels
		for cons, syls in pairs(export.syl_list) do
			local syl_i = m_tbl.keyFor(syls, source)
			if syl_i then
				local target_index = m_tbl.keyFor(export.vowel_order, target)
				return export.syl_list[cons][target_index]
			end
		end
	else
		-- swapping consonants
		for _, syls in pairs(export.syl_list) do
			local syl_i = m_tbl.keyFor(syls, source)
			if syl_i then
				local target_syls = export.syl_list[target]
				if not target_syls then
					error(format("'%s' is not a valid consonant or vowel", target))
				end
				return syl_i <= #target_syls and target_syls[syl_i] or nil
			end
		end
	end

	-- returning error otherwise
	error(format("Unable to generate syllable for source '%s' and target '%s'", source, target))
end

return export