Module:se-common

Definition from Wiktionary, the free dictionary
Jump to: navigation, search
Text-x-generic with pencil.svg This module needs documentation.
Please document this module by describing its purpose and usage on the documentation page.

local export = {}


local grades = {
	["d"] = {},
	["h"] = {},
	["ij"] = {},
	
	-- Single consonants
	["hp"] = {weak = "b", extra = "hpp"},
	["ht"] = {weak = "đ", extra = "htt"},
	["hc"] = {weak = "z", extra = "hcc"},
	["hč"] = {weak = "ž", extra = "hčč"},
	["hk"] = {weak = "g", extra = "hkk"},
	
	["đđ"] = {weak = "đ", extra = "đ'đ"},
	["ff"] = {weak = "f", extra = "f'f"},
	["hrr"] = {weak = "hr", extra = "hr'r"},
	["ll"] = {weak = "l", extra = "l'l"},
	["mm"] = {weak = "m", extra = "m'm"},
	["nn"] = {weak = "n", extra = "n'n"},
	["nnj"] = {weak = "nj", extra = "n'nj"},
	["rr"] = {weak = "r", extra = "r'r"},
	["ss"] = {weak = "s", extra = "s's"},
	["šš"] = {weak = "š", extra = "š'š"},
	["ŧŧ"] = {weak = "ŧ", extra = "ŧ'ŧ"},
	["vv"] = {weak = "v", extra = "v'v"},
	
	["pm"] = {weak = "m", extra = "bm"},
	["tn"] = {weak = "n", extra = "dn"},
	["tnj"] = {weak = "nj", extra = "dnj"},
	["kŋ"] = {weak = "ŋ", extra = "gŋ"},
	["dj"] = {weak = "j", extra = "ddj"},
	
	-- Clusters
	["bb"] = {weak = "pp"},
	["dd"] = {weak = "tt"},
	["zz"] = {weak = "cc"},
	["žž"] = {weak = "čč"},
	["gg"] = {weak = "kk"},
	
	["llj"] = {weak = "lj"},
	
	["đb"] = {weak = "đbb"},
	["đg"] = {weak = "đgg"},
	["đbm"] = {weak = "đmm"},
	["đgŋ"] = {weak = "đŋŋ"},
	["đv"] = {weak = "đvv"},
	
	["ib"] = {weak = "ibb"},
	["ibm"] = {weak = "imm"},
	["ic"] = {weak = "icc"},
	["id"] = {weak = "idd"},
	["ig"] = {weak = "igg"},
	["ihn"] = {weak = "ihnn"},
	["ik"] = {weak = "ikk"},
	["il"] = {weak = "ill"},
	["idn"] = {weak = "inn"},
	["igŋ"] = {weak = "iŋŋ"},
	["ip"] = {weak = "ipp"},
	["ir"] = {weak = "irr"},
	["is"] = {weak = "iss"},
	["ist"] = {weak = "istt"},
	["it"] = {weak = "itt"},
	["iv"] = {weak = "ivv"},
	
	["kc"] = {weak = "vcc"},
	["kč"] = {weak = "včč"},
	["ks"] = {weak = "vss"},
	["kš"] = {weak = "všš"},
	["kt"] = {weak = "vtt"},
	
	["lb"] = {weak = "lbb"},
	["lc"] = {weak = "lcc"},
	["lč"] = {weak = "lčč"},
	["ld"] = {weak = "ldd"},
	["lg"] = {weak = "lgg"},
	["lj"] = {weak = "ljj"},
	["lk"] = {weak = "lkk"},
	["lbm"] = {weak = "lmm"},
	["ldn"] = {weak = "lnn"},
	["lgŋ"] = {weak = "lŋŋ"},
	["ls"] = {weak = "lss"},
	["lt"] = {weak = "ltt"},
	["lv"] = {weak = "lvv"},
	
	["mb"] = {weak = "mbb"},
	["mp"] = {weak = "mpp"},
	
	["nd"] = {weak = "ndd"},
	["ns"] = {weak = "nss"},
	["nš"] = {weak = "nšš"},
	["nt"] = {weak = "ntt"},
	
	["ŋg"] = {weak = "ŋgg"},
	["ŋk"] = {weak = "ŋkk"},
	
	["rb"] = {weak = "rbb"},
	["rc"] = {weak = "rcc"},
	["rč"] = {weak = "rčč"},
	["rd"] = {weak = "rdd"},
	["rf"] = {weak = "rff"},
	["rg"] = {weak = "rgg"},
	["rj"] = {weak = "rjj"},
	["rk"] = {weak = "rkk"},
	["rbm"] = {weak = "rpm"},
	["rdn"] = {weak = "rtn"},
	["rgŋ"] = {weak = "rkŋ"},
	["rp"] = {weak = "rpp"},
	["rs"] = {weak = "rss"},
	["rš"] = {weak = "ršš"},
	["rt"] = {weak = "rtt"},
	["rv"] = {weak = "rvv"},
	
	["sb"] = {weak = "sbb"},
	["sm"] = {weak = "smm"},
	["sk"] = {weak = "skk"},
	["sp"] = {weak = "spp"},
	["st"] = {weak = "stt"},
	
	["šk"] = {weak = "škk"},
	["šm"] = {weak = "šmm"},
	["šp"] = {weak = "špp"},
	["št"] = {weak = "štt"},
	
	["tk"] = {weak = "tkk"},
	["tm"] = {weak = "tmm"},
	
	["vd"] = {weak = "vdd"},
	["vg"] = {weak = "vgg"},
	["vj"] = {weak = "vjj"},
	["vk"] = {weak = "vkk"},
	["vl"] = {weak = "vll"},
	["vdn"] = {weak = "vnn"},
	["vgŋ"] = {weak = "vŋŋ"},
	["vp"] = {weak = "vpp"},
	["vr"] = {weak = "vrr"},
	["vs"] = {weak = "vss"},
	["vt"] = {weak = "vtt"},
	["vz"] = {weak = "vzz"},
	["vž"] = {weak = "vžž"},
}

local grades2 = {}

for strong, gr in pairs(grades) do
	gr.weak = gr.weak or strong
	gr.extra = gr.extra or strong
	
	if not grades2[gr.weak] then
		grades2[gr.weak] = {weak = gr.weak}  -- Extra-strong grade of a weak grade is ambiguous, e.g. m > pm or mm
	end
	
	if not grades2[gr.extra] then
		grades2[gr.extra] = {weak = strong, extra = gr.extra}
	end
end

for strong, gr in pairs(grades2) do
	if not grades[strong] then
		grades[strong] = grades2[strong]
	end
end

grades2 = nil


local i_clusters = {"bm", "dn", "hn", "gŋ", "bb", "cc", "čč", "dd", "ff", "gg", "hnn", "kk", "ll", "mm", "nn", "ŋŋ", "pp", "rr", "ss", "šš", "tt", "vv"}

function export.stem_info(stem)
	local steminfo = {}
	
	if mw.ustring.find(stem, "[^aáeēiīoōuūy]$") or mw.ustring.find(stem, "[aáeēiīoōuūy]i$") then
		steminfo.normal = mw.ustring.gsub(stem, "i$", "j")
		stem, steminfo.cons = mw.ustring.match(steminfo.normal, "^(.-)([^aáeēiīoōuūy-]*)[aáeēiīoōuūy]+[^aáeēiīoōuūy]+$")
	else
		steminfo.normal, steminfo.vowel = mw.ustring.match(stem, "^(.-)([aáeēiīoōuūy])$")
		stem, steminfo.cons = mw.ustring.match(steminfo.normal, "(.-)([^aáeēiīoōuūy-]*)$")
	end
	
	if stem and mw.ustring.find(stem, "[aáeēiīoōuūy]i$") and grades["i" .. steminfo.cons] then
		steminfo.cons = "i" .. steminfo.cons
	end
	
	steminfo.simp = export.simplify_diphthongs(steminfo.normal, -1)
	
	return steminfo
end


function export.make_grade(cons, grade)
	if not grades[cons] then
		error("The grades for the consonant(s) \"" .. cons .. "\" have not yet been defined in Module:se-common.")
	end
	
	return grades[cons][grade]
end


local final_consonants = {
	["b"] = "t", ["hp"] = "t",
	["d"] = "t", ["ht"] = "t",
	["g"] = "t", ["hk"] = "t",
	["h"] = "t",
	["j"] = "i",
	["m"] = "n",
	["z"] = "s",
	["ž"] = "š",
	
	["ld"] = "l",
	["lg"] = "l",
	
	["rd"] = "r",
	
	["sk"] = "s",
	["sm"] = "s",
	["st"] = "s",
	
	["št"] = "š",
}

function export.make_final(stem, final)
	if final then
		return (mw.ustring.gsub(stem, "[^aáeēiīoōuūy]+$", final))
	end
	
	-- If the stem ends in a vowel, it's already ok.
	if mw.ustring.find(stem, "[aáeēiīoōuūy]$") then
		return stem
	else
		-- If the stem ends in a double consonant, remove one.
		if mw.ustring.find(stem, "[aáeēiīoōuūy]([^aáeēiīoōuūy])%1$") then
			stem = mw.ustring.sub(stem, 1, -2)
		end
		
		-- If the stem now ends in an allowed consonant, it's ok too.
		if mw.ustring.find(stem, "[aáeēiīoōuūy-][lnrsšpt]$") then
			return stem
		end
		
		local base, final = mw.ustring.match(stem, "^(.-)([^aáeēiīoōuūy-]+)$")
		
		if not final_consonants[final] then
			if mw.title.getCurrentTitle().nsText == "Template" then
				return stem
			else
				error("The consonant(s) \"" .. final .. "\" are not allowed word-finally, but this template does not know how to replace them with allowed ones.")
			end
		end
		
		return base .. final_consonants[final]
	end
end


local vowels_simp = {
	["ea"] = "ē",
	["ie"] = "ī",
	["oa"] = "ō",
	["uo"] = "ū"
}

function export.simplify_diphthongs(stem, syllable)
	local function repl(v, r)
		return (vowels_simp[v] or v) .. r
	end
	
	if syllable == -1 then
		stem = mw.ustring.gsub(stem, "([aáeēiīoōuūy]+)(i[^aáeēiīoōuūy]*)$", repl)
		stem = mw.ustring.gsub(stem, "([aáeēiīoōuūy]+)([^aáeēiīoōuūy]*)$", repl)
		return stem
	end
end


return export