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

မဝ်ဂျူ:apc-headword

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

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

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

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

local export = {}
local pos_functions = {}

local u = mw.ustring.char

-----------------------
-- 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
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

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

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

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 = ""},
		["tr"] = {list = true, allow_holes = true},
	}

	local args, unused_keys = make_unused_key_tracker(frame:getParent().args)

	-- Gather parameters
	local data = {lang = lang, pos_category = poscat, categories = {}, heads = {}, translits = {}, genders = {}, inflections = {}}

	local head = args["head"] or PAGENAME or ""
	local translit = ine(args["tr"])
	local i = 1

	local irreg_translit = false

	while head do
		table.insert(data.heads, head)
		data.translits[#data.heads] = translit

		i = i + 1
		head = ine(args["head" .. i])
		translit = ine(args["tr" .. 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

	return require("Module:headword").full_headword(data)
end

-- Get a list of inflections. See handle_infl() for meaning of ARGS, ARGPREF
-- and DEFGENDER.
local function getargs(args, argpref, defgender)
	-- Gather parameters
	local forms = {}

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

	local translit = ine(args[argpref .. "tr"])
	local gender = ine(args[argpref .. "g"])
	local gender2 = ine(args[argpref .. "g2"])
	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, translit = translit, genders = genderlist})

		i = i + 1
		form = ine(args[argpref .. i])
		translit = ine(args[argpref .. i .. "tr"])
		gender = ine(args[argpref .. i .. "g"])
		gender2 = ine(args[argpref .. i .. "g2"])
	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 .. "construct state")
end

-- Handle the case where pl=-, indicating an uncountable noun.
local function handle_noun_plural(args, data)
	if args["pl"] == "-" then
		table.insert(data.inflections, {label = "usually [[Appendix:Glossary#uncountable|uncountable]]"})
		append_cat(data, "uncountable nouns")
	else
		handle_infl(args, data, "pl", "plural")
	end
end

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

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

-- Handle gender in unnamed param 2 and a second gender in param g2, inserting
-- into the list of genders in GENDER. Also, if a lemma, insert categories
-- into CATS if the gender is unexpected for the form of the noun or if multiple
-- genders occur. If gender unspecified, default to DEFAULT, which may be
-- omitted.
local function handle_gender(args, data, default, nonlemma)

	local PAGENAME = mw.title.getCurrentTitle().text

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

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

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

	if nonlemma then
		return
	end

	if g and g2 then
		append_cat(data, "terms with multiple genders")
	end
end

-- Part-of-speech functions

pos_functions["နာမဝိသေသန"] = {
	func = function(args, data)
		handle_all_infl(args, data, "", "")
		handle_all_infl(args, data, "f", "ဣတ္တိလိင်")
		handle_all_infl(args, data, "cpl", "ကိုန်ဗဟုဝစ်သာမည")
		handle_all_infl(args, data, "pl", "ကိုန်ဗဟုဝစ်ပုလ္လိၚ်")
		handle_all_infl(args, data, "fpl", "ကိုန်ဗဟုဝစ်ဣတ္တိလိင်")
		handle_all_infl(args, data, "dim", "လဟုတ်စှ်ေ")
		handle_infl(args, data, "el", "elative")
	end
}

function handle_sing_coll_noun_infls(args, data)
	handle_all_infl(args, data, "", "")
	handle_all_infl(args, data, "d", "dual")
	handle_all_infl(args, data, "pauc", "paucal")
	handle_noun_plural(args, data)
	handle_all_infl(args, data, "pl", "plural", "nobase")
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", "singulative", "f")
		handle_sing_coll_noun_infls(args, data)
	end
}

pos_functions["singulative nouns"] = {
	func = function(args, data)
		data.pos_category = "နာမ်"
		
		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)
	end
}

function handle_noun_infls(args, data, singonly)
	handle_all_infl(args, data, "", "")

	if not singonly then
		handle_all_infl(args, data, "d", "ၜါလ္ပာ်")
		handle_noun_plural(args, data)
		handle_all_infl(args, data, "pl", "ကိုန်ဗဟုဝစ်", "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)
		
		handle_gender(args, data)
		handle_noun_infls(args, data)
	end
}

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

pos_functions["သဗ္ဗနာမ်"] = {
	params = {
		["g"] = {},
		["g2"] = {}
		},
	func = function(args, data)
		if ine(args["g"]) or ine(args["g2"]) then
			handle_gender(args, data)
		end
		handle_infl(args, data, "encl", "enclitic form")
		handle_infl(args, data, "f", "ဣတ္တိလိင်")
		handle_infl(args, data, "pl", "ကိုန်ဗဟုဝစ်")
	end
}

pos_functions["adjective feminine forms"] = {
	params = {
		["g"] = {},
		["g2"] = {},
		["pl"] = {},
		["islemma"] = { type = boolean },
		},
	func = function(args, data)
		data.pos_category = "ဗီုပြင်နာမဝိသေသန"
		handle_noun_plural(args, data)
		handle_gender(args, data, "f", "nonlemma")
	end
}

pos_functions["adjective plural forms"] = {
	params = {
		["g"] = {},
		["g2"] = {},
		},
	func = function(args, data)
		data.pos_category = "ဗီုပြင်နာမဝိသေသန"
		handle_gender(args, data, "p", "nonlemma")
	end
}

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

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

pos_functions["active participles"] = {
	params = {
		[2] = {},
		},
	func = function(args, data)
		data.pos_category = "participles"
		append_cat(data, "active participles")
		handle_infl(args, data, "", "")
		handle_infl(args, data, "f", "feminine")
		handle_infl(args, data, "cpl", "common plural")
		handle_infl(args, data, "pl", "masculine plural")
		handle_infl(args, data, "fpl", "feminine plural")
	end
}

pos_functions["passive participles"] = {
	params = {
		[2] = {},
		},
	func = function(args, data)
		data.pos_category = "participles"
		append_cat(data, "passive participles")
		handle_infl(args, data, "", "")
		handle_infl(args, data, "f", "feminine")
		handle_infl(args, data, "cpl", "common plural")
		handle_infl(args, data, "pl", "masculine plural")
		handle_infl(args, data, "fpl", "feminine plural")
	end
}

local verb_forms = {
["I"]  = true,
["II"] = true,
["III"] = true,
["IV"]  = true,
["V"] = true,
["VI"] = true,
["VII"]  = true,
["VIII"] = true,
["IX"] = true,
["X"]  = true,
["Iq"] = true,
["IIq"] = true,
}

pos_functions["ကြိယာ"] = {
	func = function(args, data)
		data.pos_category = "ကြိယာ"
		if ine(args[1]) then
			if verb_forms[args[1]] then
				table.insert(data.inflections, {label = '[[:en:Appendix:Arabic verbs#Form ' .. args[1] .. '|form ' .. args[1] .. ']]'})
				
			else
				error("Invalid verb form. Please provide a valid one.")
			end
		elseif args[1] ~= "-" then
			append_cat(data, "verbs lacking forms")
		end
		handle_infl(args, data, "np", "non-past")
		handle_infl(args, data, "vn", "verbal noun")
		handle_infl(args, data, "ap", "active participle")
		handle_infl(args, data, "pp", "passive participle")
	end
}

pos_functions["determiners"] = {
	params = {
		["g"] = {},
		["g2"] = {}
		},
	func = function(args, data)
		if ine(args["g"]) or ine(args["g2"]) then
			handle_gender(args, data)
		end
		handle_infl(args, data, "f", "feminine")
		handle_infl(args, data, "pl", "plural")
	end
}

pos_functions["suffixes"] = {
	params = {
		["g"] = {}
		},
	func = function(args, data)
		if ine(args["g"]) then
			handle_gender(args, data)
		end
		handle_infl(args, data, "f", "feminine")
		handle_infl(args, data, "pl", "plural")
	end
}

return export