Pergi ke kandungan

Modul:ms-terbitan

Daripada Wikikamus

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

function export.affix_table(text)
	return {
		--[[
		Imbuhan namaan (0 - 4x)
		1x: Imbuhan sisipan
		2x: Imbuhan awalan
		3x: Imbuhan akhiran
		4x: Imbuhan apitan
		]]
		[11] = { "-el-", "-el-", "tak produktif", "kepelakuan and perantian / resultatif" },
		[12] = { "-er-", "-er-", "tak produktif", "resultatif" },
		[13] = { "-em-", "-em-", "tak produktif", "resultatif / keragaman" },
		[21] = { "peN-", "peng-", "produktif", "kepelakuan / kualitatif / perantian / abstrak / sukatan; ukuran" },
		[22] = { "pe-", "pe-", "tak produktif", "pasif / pekerjaan atau penyertaan pada suatu tindakan sementara" },
		[23] = { "ke-", "ke-", "tak produktif", "pasif" },
		[24] = { "pra-", "pra-", "tak produktif", "daripada Sanskrit" },
		[31] = { "-an", "-an", "produktif", "resultatif / lokatif / kolektif / keragaman / kata nama kerjaan / buah" },
		[32] = { "-Man", "-man", "tak produktif", "pelaku" },
		[33] = { "-is", "-is", "produktif", "pelaku" },
		[34] = { "-isme", "-isme", "tak produktif", "ideologi" },
		[40] = { "ke-an", "ke- -an", "produktif", "abstrak / lokatif" },
		
		--[[
		Imbuhan kata kerja (0-3xx)
		1xx: Imbuhan Tak Transitif
			11x: Imbuhan Sisipan
			12x: Imbuhan Serentak
			13x: Imbuhan Awalan
			14x: Imbuhan Akhiran
			15x: Imbuhan Apitan
		2xx: Imbuhan Transitif
			21x: Imbuhan Awalan
			22x: Imbuhan Akhiran
		3xx: Imbuhan Ragam
			31x: Ragam Aktif
		]]
		[111] = { "-el-", "-el-", "tak produktif", "keamatan" },
		[112] = { "-em-", "-em-", "tak produktif", "keamatan" },
		[113] = { "-er-", "-er-", "tak produktif", "keamatan" },
		[121] = { "-", "-", "produktif", "penggandaan" },
		[131] = { "se-", "se-", "produktif", "kebolehbandingan" },
		[132] = { "ke-", "ke-", "tak produktif", "keamatan" },
		[140] = { "-an", "-an", "tak produktif", "pengulangan / kesalingan" },
		[150] = { "ke-an", "ke- -an", "produktif", "kemiripan / pasif" },
		[210] = { "peR-", "per-", "produktif", "pasif kausatif" },
		[221] = { "-kan", "-kan", "produktif", "penerima kausatif" },
		[222] = { "-i", "-i", "produktif", "penerima kausatif (lokatif)" },
		[310] = { "meN-", "meng-", "produktif", "fokus pelaku" },
		[320] = { "di-", "di-", "produktif", "pasif" },
		[330] = { "teR-", "ter-", "produktif", "tindakan tanpa pelaku" },
		[340] = { "beR-", "ber-", "produktif", "keadaan / kebiasaan" },
		[350] = { "mempeR-", "memper-", "produktif", "fokus pelaku kausatif" },
		[360] = { "dipeR-", "diper-", "produktif", "fokus pasif kausatif" },
		
		--[[
		Imbuhan adverbaan (4xx)
			41x: Imbuhan Sisipan
			42x: Imbuhan Serentak
			43x: Imbuhan Awalan
			44x: Imbuhan Akhiran
		]]
		[411] = { "-el-", "-el-", "tak produktif", "tempoh masa panjang" },
		[412] = { "-em-", "-em-", "tak produktif", "tempoh masa panjang" },
		[413] = { "-er-", "-er-", "tak produktif", "tempoh masa panjang" },
		[420] = { "-", "-", "produktif", "penggandaan" },
		[430] = { "se-", "se-", "tak produktif", "segera / kebiasaan" },
		[440] = { "-an", "-an", "tak produktif", "tempoh masa / emosi" },
	}
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("Modul:links"), require("Modul:languages").getByCode(language)
		local first_affix, first_others = true, true
		
		table.insert(derivations, 
			frame:expandTemplate{
				title = 'laj-atas',
				args = {
					'1',
					'Imbuhan dan kata terbitan lain',
				}
			})
		
		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, "''Terbitan berimbuhan nalar:''")
				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] .. "#Bahasa Indonesia|" .. data[1] .. "]]''" or "''redup''")
					else
						table.insert(records, data[2] ~= "-" and "''[[" .. data[2] .. "#Bahasa Melayu|" .. 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, "''Terbitan berimbuhan tidak nalar, kata terbitan lain dan kata majmuk:''")
				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") .. frame:expandTemplate{ title = 'laj-bawah' }
	end
	
	return ""
end

return export