Jump to content

Module:ms-derivations

From Wiktionary, the free dictionary

Malay affix-derived forms module. See {{ms-der}}.


local export = {}
local gsub = mw.ustring.gsub

function export.affix_table(text)
	return {
		--noun-producing
		[11] = { "-el-", "-el-", "nonproductive", "agentive and instrumental / resultative" },
		[12] = { "-er-", "-er-", "nonproductive", "resultative" },
		[13] = { "-em-", "-em-", "nonproductive", "resultative / variety" },
		[21] = { "peN-", "peng-", "productive", "agentive / qualitative / instrumental / abstract / measure" },
		[22] = { "pe-", "pe-", "nonproductive", "passive / name of profession" },
		[23] = { "ke-", "ke-", "nonproductive", "passive" },
		[24] = { "pra-", "pra-", "nonproductive", "(from Sanskrit)" },
		[31] = { "-an", "-an", "productive", "resultative / locative / collective / variety / verbal noun / fruit" },
		[32] = { "-Man", "-man", "nonproductive", "agent" },
		[33] = { "-is", "-is", "nonproductive", "agent" },
		[34] = { "-isme", "-isme", "nonproductive", "ideology" },
		[40] = { "ke-an", "ke- -an", "productive", "abstract / locative" },
		
		--verb-producing
		[111] = { "-el-", "-el-", "nonproductive", "intensity" },
		[112] = { "-em-", "-em-", "nonproductive", "intensity" },
		[113] = { "-er-", "-er-", "nonproductive", "intensity" },
		[121] = { "-", "-", "productive", "reduplication" },
		[131] = { "se-", "se-", "productive", "comparability" },
		[132] = { "ke-", "ke-", "nonproductive", "intensity" },
		[140] = { "-an", "-an", "nonproductive", "repetition / reciprocity" },
		[150] = { "ke-an", "ke- -an", "productive", "resemblance / passive" },
		[210] = { "peR-", "per-", "productive", "causative passive" },
		[221] = { "-kan", "-kan", "productive", "causative benefactive" },
		[222] = { "-i", "-i", "productive", "causative (locative) benefactive" },
		[310] = { "meN-", "meng-", "productive", "agent focus" },
		[320] = { "di-", "di-", "productive", "patient focus" },
		[330] = { "teR-", "ter-", "productive", "agentless action" },
		[340] = { "beR-", "ber-", "productive", "stative / habitual" },
		[350] = { "mempeR-", "memper-", "productive", "causative agent focus" },
		[360] = { "dipeR-", "diper-", "productive", "causative passive focus" },
		
		--adverb-producing
		[411] = { "-el-", "-el-", "nonproductive", "longer timespan" },
		[412] = { "-em-", "-em-", "nonproductive", "longer timespan" },
		[413] = { "-er-", "-er-", "nonproductive", "longer timespan" },
		[420] = { "-", "-", "productive", "reduplication" },
		[430] = { "se-", "se-", "nonproductive", "immediacy / habitual" },
		[440] = { "-an", "-an", "nonproductive", "timespan / emotion" },
	}
end

local pattern_replacements = {
	["N#([lmnrwy])"] = "%1", 
	["N#([bf])"] = "m%1", 
	["N#p"] = "m", 
	["N#t"] = "n",
	["N#([cdjz])"] = "n%1", 
	["N#([gh])"] = "ng%1", 
	["N#kh?"] = "ng", 
	["N#s"] = "ny",
	["R#r"] = "r",
	["aM"] = "w",
}

local letter_replacements = {
	["M"] = "m", ["N#"] = "ng", ["R#"] = "r"
}

function export.affixation(text, affix)
	if affix == "-" then
		text = text .. "-" .. text
	elseif mw.ustring.match(affix, "^%-.+%-$") then
		first_consonant = mw.ustring.match(text, "^[^aeiou]+")
		text = first_consonant .. mw.ustring.sub(affix, 2, -2) .. mw.ustring.sub(text, mw.ustring.len(first_consonant) + 1, -1)
	else
		text = gsub(affix, "%-", "#" .. text)
		text = gsub(text, "N([^aeiou]*)([aeiou])([^aeiou]*)$", "nge%1%2%3")
		text = gsub(text, "meN#p", "mem")
		for pattern, replacement in pairs(pattern_replacements) do
			text = gsub(text, pattern, replacement)
		end
		text = gsub(text, "[MNR]#?", letter_replacements)
		text = gsub(text, "#", "")
	end
	return text
end

function export.show(frame)
	local args = frame:getParent().args
	local p, derivations = {}, {}
	local title = args["word"] or mw.title.getCurrentTitle().text
	local lang = frame.args["lang"]
	args["word"] = nil
	
	if args[1] then
		for index, item in ipairs(args) do
			table.insert(p, (item ~= "") and item or nil)
		end
		local language = args["lang"] or "ms"
		local m_links, lang = require("Module:links"), require("Module:languages").getByCode(language)
		local first_affix, first_others = true, true
		
		table.insert(derivations, 
			'<div class="NavFrame">\n<div class="NavHead" style="line-height:200%">Affixed terms and other derivations' ..
			'</div>\n<div class="NavContent" style="text-align:left; background-color:#F5F5FF; font-size:105%">\n')
		
		for _, affix in ipairs(p) do
			affix = mw.ustring.gsub(affix, "\n$", "")
			local word, meaning, final_form = title, nil, nil
			if mw.ustring.match(affix, ":") then
				local xplet = mw.text.split(affix, ":")
				if #xplet == 3 then
					affix, final_form, meaning = xplet[1], xplet[2], xplet[3]
				else
					affix, meaning = xplet[1], xplet[2]
				end
			end
			
			if mw.ustring.match(affix, "[0-9%+]") then
				if first_affix then
					table.insert(derivations, "''Regular affixed derivations:''")
				end
				local records, notes = {}, {}
				for number in mw.text.gsplit(affix, "+") do
					local number = tonumber(number) or number
					local data = export.affix_table("")[number] or { number, nil, nil }
					word = export.affixation(word, data[1])
					if args["lang"] == "id" then
						table.insert(records, data[2] ~= "-" and "''[[" .. data[2] .. "#Indonesian|" .. data[1] .. "]]''" or "''redup''")
					else
						table.insert(records, data[2] ~= "-" and "''[[" .. data[2] .. "#Malay|" .. data[1] .. "]]''" or "''redup''")
					end
					table.insert(notes, data[4])
				end
				table.insert(derivations, "* " .. m_links.full_link({lang = lang, term = final_form or word, gloss = meaning or nil}) .. " <small>[" .. table.concat(notes, " + ") .. "] (" .. table.concat(records, " + ") .. ") </small>")
				first_affix = false
			else
				if first_others then
					table.insert(derivations, "''Irregular affixed derivations, other derivations and compound words:''")
				end
				word = affix
				first_others = false
				table.insert(derivations, "* " .. m_links.full_link({lang = lang, term = word, gloss = meaning or nil}))
			end
		end
		return table.concat(derivations, "\n") .. "</div></div>"
	end
	
	return ""
end

return export