မာတိကာသို့ ခုန်သွားရန်

မဝ်ဂျူ:mt-headword

နူ ဝိက်ရှေန်နရဳ

Documentation for this module may be created at မဝ်ဂျူ:mt-headword/doc

-- Based on [[Module:ar-headword]] by: Benwing, CodeCat
-- Adapted by Fenakhay

local lang = require("Module:languages").getByCode("mt")

local export = {}
local pos_functions = {}

local u = mw.ustring.char

local function glossary_link(entry, text)
	text = text or entry
	return "[[Appendix:Glossary#" .. entry .. "|" .. text .. "]]"
end

local function parse_qualifiers(quals, sense)
	if quals == nil and sense == nil then
		return nil
	end
	
	local qualifiers = { }

	if quals ~= nil then
		local list_quals = mw.text.split(quals, ',%s*')

		for _, qual in ipairs(list_quals) do
			table.insert(qualifiers, qual)
		end
	end

	return table.concat(qualifiers, ", ")
end

-----------------------
-- Utility functions --
-----------------------

-- If Not Empty
local function ine(arg)
	if arg == "" then
		return nil
	else
		return arg
	end
end

local function list_to_set(list)
	local set = {}
	for _, item in ipairs(list) do
		set[item] = true
	end
	return set
end

-- version of mw.ustring.gsub() that discards all but the first return value
local function rsub(term, foo, bar)
	local retval = mw.ustring.gsub(term, foo, bar)
	return retval
end

local rfind = mw.ustring.find

-- Tracking functions

local trackfn = require("Module:debug").track

local function track(page)
	trackfn("mt-headword/" .. page)
	return true
end

local function append_cat(data, pos)
	table.insert(data.categories, lang:getCanonicalName() .. " " .. pos)
end

local function remove_links(text)
	text = rsub(text, "%[%[[^|%]]*|", "")
	text = rsub(text, "%[%[", "")
	text = rsub(text, "%]%]", "")
	return text
end

local function make_unused_key_tracker(t)
	local unused_keys = require "Module:table".listToSet(require "Module:table".keysToList(t))
	local mt = {
		__index = function(self, key)
			if key ~= nil then
				unused_keys[key] = nil
			end
			return t[key]
		end,
		__newindex = function(self, key, value)
			t[key] = value
		end
	}
	local proxy_table = setmetatable({}, mt)
	return proxy_table, unused_keys
end

-- The main entry point.
function export.show(frame)
	
	local PAGENAME = mw.title.getCurrentTitle().text
	
	local poscat = frame.args[1] or error("Part of speech has not been specified. Please pass parameter 1 to the module invocation.")
	
	local params = {
		[1] = {list = "head", allow_holes = true, default = ""},
		["head"] = {default = ""}
	}
	
	local args, unused_keys = make_unused_key_tracker(frame:getParent().args)
	
	-- Gather parameters
	local data = {lang = lang, pos_category = poscat, categories = {}, heads = {}, genders = {}, inflections = {}}
	
	local head = args["head"] or PAGENAME or ""
	local i = 1
	
	while head do
		table.insert(data.heads, head)
		i = i + 1
		head = ine(args["head" .. i])
	end
	
	if pos_functions[poscat] then
		pos_functions[poscat].func(args, data)
	end
	
	local unused_key_list = require "Module:table".keysToList(unused_keys)
	if #unused_key_list > 0 then
		local unused_key_string = require "Module:array"(unused_key_list)
			:map(function(key)
					return "|" .. key .. "=" .. args[key]
			end)
			:concat("\n")
		error("Unused arguments: " .. unused_key_string)
	end

	if mw.ustring.match(PAGENAME, "[Gg]ħ") then
		append_cat(data, "terms spelled with Għ")
	end
	
	return require("Module:headword").full_headword(data)
end

local function getargs(args, argpref, defgender, position)
	-- Gather parameters
	local forms = {}

	local form

	if ine(args[position]) then
		form = ine(args[position])
	else 
		form = ine(args[argpref])
	end

	local gender = ine(args[argpref .. "g"])
	local gender2 = ine(args[argpref .. "g2"])
	local qualifiers = ine(args[argpref .. "q"])
	local senses = ine(args[argpref .. "id"])
	
	local i = 1
	
	while form do
		local genderlist = (gender or gender2) and {gender, gender2} or defgender and {defgender} or nil
		
		table.insert(forms, {term = form, gender = genderlist, qualifiers = parse_qualifiers(qualifiers, senses)})

		i = i + 1
		form = ine(args[argpref .. i])
		gender = ine(args[argpref .. i .. "g"])
		gender2 = ine(args[argpref .. i .. "g2"])
		qualifiers = ine(args[argpref .. i .. "q"])
		senses = ine(args[argpref .. i .. "id"])
	end

	return forms
end

local function handle_infl(args, data, argpref, label, defgender, position)
	local newinfls = getargs(args, argpref, defgender, position)
	newinfls.label = label

	if #newinfls > 0 then
		table.insert(data.inflections, newinfls)
	end
end

local function handle_all_infl(args, data, argpref, label, nobase, position)
	if not nobase and argpref ~= "" then
		handle_infl(args, data, argpref, label, nil, position)
	end
	
	local labelsp = label == "" and "" or label .. " "
	handle_infl(args, data, argpref .. "cons", labelsp .. "သိုၚ်ဗဒှ်လဝ်နကဵုအကာဲအရာ")
end

-- Handle the case where p=-, indicating an uncountable noun.
local function handle_noun_plural(args, data)
	if args["p"] == "-" then
		table.insert(data.inflections, {label = "မကၠောန်ကၠုၚ်လဝ်[[Appendix:Glossary#uncountable|တော်ဟွံမာန်]]"})
		
	else
		handle_infl(args, data, "p", "ကိုန်ဗဟုဝစ်")
		handle_infl(args, data, "pauc", "paucal")
	end
end

local valid_genders = list_to_set(
	{"m", "m-s",
	 "f", "f-s",
	 "m-p", "f-p", "p",
	 "d", "m-d", "f-d", "mf", "mfbysense"
	})

local function is_masc_sg(g)
	return g == "m" or g == "m-s" or g == "m-p" or g == "m-d" or g == "mf" or g == "mfbysense"
end
local function is_fem_sg(g)
	return g == "f" or g == "f-s" or g == "f-p" or g == "f-d" or g == "mf" or g == "mfbysense"
end

local function handle_gender(args, data, default, nonlemma, optional)

	local g = ine(args["g"]) or default
	local g2 = ine(args["g2"])

	local function process_gender(gender)
		if not gender and not optional then
			table.insert(data.genders, "?")
		elseif not gender and optional then
			-- do nothing
		elseif valid_genders[g] then
			table.insert(data.genders, gender)
		else
			error("Unrecognized gender: " .. gender)
		end
	end

	process_gender(g)
	if g2 then
		process_gender(g2)
	end

	if nonlemma then
		return
	end
end

-- Part-of-speech functions

pos_functions["နာမဝိသေသန"] = {
	func = function(args, data)
		if args[1] == "-" then
			local forms = {}
			forms.label = glossary_link("မနွံကဵုပၟိက်အကာဲအရာ")
			
			table.insert(data.inflections, forms)
		else
			handle_all_infl(args, data, "f", "ကိုန်ဨကဝုစ်ဣတ္တိလိင်")
			handle_all_infl(args, data, "p", "ကိုန်ဨကဝုစ်")
			handle_all_infl(args, data, "dim", "လဟုတ်စှ်ေ")
			handle_all_infl(args, data, "comp", "ပတုပ်ရံၚ်")
		end
	end
}

local function handle_sing_coll_noun_infls(args, data)
	handle_all_infl(args, data, "", "")
	handle_all_infl(args, data, "d", "dual")
	handle_noun_plural(args, data)
end

pos_functions["collective nouns"] = {
	func = function(args, data)
		data.pos_category = "နာမ်"
		
		table.insert(data.inflections, {label = "ပံၚ်ကောံ"})
		
		handle_gender(args, data, "m")
		-- Handle sing= (the corresponding singulative noun) and singg= (its gender)
		handle_infl(args, data, "sing", "ကိုန်ဨကဝုစ်", "f")
		handle_sing_coll_noun_infls(args, data)
		handle_all_infl(args, data, "dim", "လဟုတ်စှ်ေ")
	end
}

pos_functions["singulative nouns"] = {
	func = function(args, data)
		data.pos_category = "နာမ်"
		append_cat(data, "နာမ်ကိုန်ဨကဝုစ်")
		table.insert(data.inflections, {label = "ကိုန်ဨကဝုစ်"})
		
		handle_gender(args, data, "f")
		-- Handle coll= (the corresponding collective noun) and collg= (its gender)
		handle_infl(args, data, "coll", "collective", "m")
		handle_sing_coll_noun_infls(args, data)
		handle_all_infl(args, data, "dim", "လဟုတ်စှ်ေ")
	end
}

local function handle_noun_infls(args, data, singonly)
	handle_all_infl(args, data, "", "")
	
	if not singonly then
		handle_all_infl(args, data, "d", "dual")
		handle_noun_plural(args, data)
		handle_all_infl(args, data, "p", "ကိုန်ဗဟုဝစ်", "nobase")
		handle_all_infl(args, data, "pauc", "paucal", "nobase")
	end
	
	handle_all_infl(args, data, "f", "ဣတ္တိလိင်")
	handle_all_infl(args, data, "m", "ပုလ္လိၚ်")
	
	if not singonly then
		handle_all_infl(args, data, "dim", "လဟုတ်စှ်ေ")
	end
end

pos_functions["နာမ်"] = {
	func = function(args, data)
		handle_gender(args, data)
		handle_noun_infls(args, data)
		
		local g = ine(args["g"]) or default
		local g2 = ine(args["g2"])
	
		if is_masc_sg(g) or is_masc_sg(g2) then
			
		elseif is_fem_sg(g) or is_fem_sg(g2) then
			
		end
	end
}

pos_functions["ဂၞန်သင်္ချာ"] = {
	func = function(args, data)
		append_cat(data, "ဂၞန်သင်္ချာမနွံကဵုပၟိက်")
		handle_gender(args, data)
		handle_noun_infls(args, data)
	end
}

pos_functions["နာမ်မကိတ်ညဳ"] = {
	func = function(args, data)
		handle_gender(args, data, nil, nil, true)
		handle_noun_infls(args, data, "ပါဲနူကိုန်ဨကဝုစ်")
	end
}

pos_functions["နာမ်ပါင်တိုက်"] = {
	func = function(args, data)
		data.pos_category = "နာမ်"
		handle_gender(args, data)
	
		handle_all_infl(args, data, "inst", "နာမ်ဗီုပြင်ထ္ၜးဥပမာ")
		handle_all_infl(args, data, "p", "ကိုန်ဗဟုဝစ်")
	end
}

pos_functions["နာမဝိသေသနဗီုပြင်ဣတ္တိလိင်"] = {
	func = function(args, data)
		data.pos_category = "နာမဝိသေသနဗီုပြင်ဣတ္တိလိင်"
		handle_gender(args, data, "f", "nonlemma")
	end
}

pos_functions["နာမဝိသေသနဗီုပြင်ကိုန်ဗဟုဝစ်"] = {
	func = function(args, data)
		data.pos_category = "နာမဝိသေသနဗီုပြင်ကိုန်ဗဟုဝစ်"
		handle_gender(args, data, "p", "nonlemma")
	end
}

pos_functions["adjective f-pl forms"] = {
	func = function(args, data)
		data.pos_category = nil
		append_cat(data, "နာမဝိသေသနဗီုပြင်ကိုန်ဗဟုဝစ်")
		append_cat(data, "နာမဝိသေသနဗီုပြင်ဣတ္တိလိင်")
		handle_gender(args, data, nil, "nonlemma", true)
	end
}

pos_functions["ဗီုပြင်နာမ်"] = {
	params = {
		["g"] = {},
		["g2"] = {},
		},
	func = function(args, data)
		data.pos_category = "ဗီုပြင်နာမ်"
		handle_gender(args, data, nil, "nonlemma")
	end
}

pos_functions["နာမ်ဗီုပြင်ၜါလ္ပာ်"] = {
	params = {
		["g"] = {},
		["g2"] = {},
		},
	func = function(args, data)
		append_cat(data, "နာမ်ဗီုပြင်ၜါလ္ပာ်")
		handle_gender(args, data, "d", "nonlemma")
	end
}

pos_functions["noun f-pl forms"] = {
	func = function(args, data)
		data.pos_category = nil
		append_cat(data, "နာမ်ဗီုပြင်ကိုန်ဗဟုဝစ်")
		append_cat(data, "နာမ်ဣတ္တိလိင်")
		append_cat(data, "lemmas")
		handle_gender(args, data, nil, "nonlemma", true)
	end
}

pos_functions["ကြိယာ"] = {
	func = function(args, data)
		data.pos_category = "ကြိယာ"
		handle_all_infl(args, data, "imper", "ဟွံက္ဍိုပ်ပေၚ်", nil, 1)
		handle_all_infl(args, data, "pp", "လုပ်ကၠောန်စွံလဝ်နကဵုအတိတ်", nil, 2)
		handle_all_infl(args, data, "ap", "လုပ်ကၠောန်စွံလဝ်နကဵုမစိုပ်တရဴ", nil, 3)
		handle_all_infl(args, data, "vn", "နာမ်ပါၚ်တိုက်", nil, 4)
	end
}

pos_functions["ကြိယာဝိသေသန"] = {
	func = function(args, data)
		data.pos_category = "ကြိယာဝိသေသန"
		handle_all_infl(args, data, "stem", "stem")
	end
}

pos_functions["ဗီုပြင်ဂၞန်သင်္ချာ"] = {
	func = function(args, data)
		data.pos_category = "ဗီုပြင်ဂၞန်သင်္ချာ"
		handle_gender(args, data)
	end
}

pos_functions["လုပ်ကၠောန်စွံလဝ်နကဵုအတိတ်"] = {
	func = function(args, data)
		data.pos_category = "လုပ်ကၠောန်စွံလဝ်"
		append_cat(data, "လုပ်ကၠောန်စွံလဝ်နကဵုအတိတ်")
		handle_all_infl(args, data, "f", "ဣတ္တိလိင်")
		handle_all_infl(args, data, "p", "ကိုန်ဗဟုဝစ်")
	end
}

pos_functions["active participles"] = {
	func = function(args, data)
		data.pos_category = "participles"
		append_cat(data, "active participles")
		handle_all_infl(args, data, "f", "feminine")
		handle_all_infl(args, data, "p", "plural")
	end
}

pos_functions["determiners"] = {
	func = function(args, data)
		data.pos_category = "determiners"
		handle_all_infl(args, data, "f", "ဣတ္တိလိင်")
		handle_all_infl(args, data, "p", "ကိုန်ဗဟုဝစ်")
	end
}

pos_functions["အဆက်လက္ကရဴ"] = {
	func = function(args, data)
		data.pos_category = "အဆက်လက္ကရဴ"
		handle_gender(args, data, nil, nil, true)
		handle_all_infl(args, data, "f", "ဣတ္တိလိင်")
		handle_all_infl(args, data, "d", "ၜါလ္ပာ်")
		handle_all_infl(args, data, "p", "ကိုန်ဗဟုဝစ်")
	end
}

pos_functions["prefixes"] = {
	func = function(args, data)
		data.pos_category = "prefixes"
		handle_gender(args, data, nil, nil, true)
		handle_all_infl(args, data, "f", "feminine")
		handle_all_infl(args, data, "d", "dual")
		handle_all_infl(args, data, "p", "plural")
	end
}

return export