跳转到内容

模組:Sem-arb-headword

維基詞典,自由的多語言詞典


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

local sc = require("Module:scripts").getByCode("Arab")
local lang = require("Module:languages").getByCode("ar")

local export = {}
local pos_functions = {}

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

-- Tracking functions

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

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

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

local function glossary_link(entry, text)
	text = text or entry
	return "[[Appendix:Glossary#" .. entry .. "|" .. text .. "]]"
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(_, key)
			if key ~= nil then
				unused_keys[key] = nil
			end
			return t[key]
		end,
		__newindex = function(_, 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 args, unused_keys = make_unused_key_tracker(frame:getParent().args)
	
	if frame.args["lang"] then
		lang = require("Module:languages").getByCode(frame.args["lang"])
	else 
		error("Please specify a language code.")
	end

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

	local saw_head = false
	local head = ine(args["head"])
	if head then
		saw_head = true
	else
		head = PAGENAME
	end
	local translit = ine(args["tr"])
	local i = 1

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

		i = i + 1
		head = ine(args["head" .. i])
		if head then
			saw_head = true
		end
		translit = ine(args["tr" .. i])
	end
	data.no_redundant_head_cat = not saw_head

	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")
		handle_infl(args, data, "pauc", "paucal")
	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",
		  "mfbysense", "mf"
		})

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

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

	local function process_gender(gender)
		if not gender then
			table.insert(data.genders, "?")
		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

	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
		if ine(args["der"]) then
			if args["der"] == "active" then
				append_cat(data, "派生自主動分詞的詞")
			elseif args["der"] == "passive" then
				append_cat(data, "派生自被動分詞的詞")
			end
		end
	end
}

function handle_sing_coll_noun_infls(args, data)
	handle_all_infl(args, data, "", "")
	handle_all_infl(args, data, "d", "雙數")
	handle_noun_plural(args, data)
	handle_all_infl(args, data, "pl", "複數", "nobase")
end

pos_functions["集合名詞"] = {
	func = function(args, data)
		data.pos_category = "名詞"
		append_cat(data, "集合名詞")
		table.insert(data.inflections, { label = glossary_link("collective") })

		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["單數名詞"] = {
	func = function(args, data)
		data.pos_category = "名詞"
		append_cat(data, "單數名詞")
		table.insert(data.inflections, { label = glossary_link("singulative") })

		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")
		handle_all_infl(args, data, "pauc", "微數", "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"])
	end
}

pos_functions["動詞性名詞"] = {
	func = function(args, data)
		handle_gender(args, data)
		handle_infl(args, data, "inst", "實例名詞") -- instance noun
	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)
		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["形容詞陰性變格形"] = {
	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["形容詞複數變格形"] = {
	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["名詞雙數變格形"] = {
	params = {
		["g"] = {},
		["g2"] = {},
	},
	func = function(args, data)
		append_cat(data, "名詞雙數變格形")
		handle_gender(args, data, "m-d", "nonlemma")
	end
}

pos_functions["主動分詞"] = {
	params = {
		[2] = {},
	},
	func = function(args, data)
		data.pos_category = "分詞"
		append_cat(data, "主動分詞")
		handle_infl(args, data, "", "")
		handle_infl(args, data, "f", "陰性")
		handle_infl(args, data, "cpl", "通性複數")
		handle_infl(args, data, "pl", "陽性複數")
		handle_infl(args, data, "fpl", "陰性複數")
	end
}

pos_functions["被動分詞"] = {
	params = {
		[2] = {},
	},
	func = function(args, data)
		data.pos_category = "分詞"
		append_cat(data, "被動分詞")
		handle_infl(args, data, "", "")
		handle_infl(args, data, "f", "陰性")
		handle_infl(args, data, "cpl", "通性複數")
		handle_infl(args, data, "pl", "陽性複數")
		handle_infl(args, data, "fpl", "陰性複數")
	end
}

local verb_forms = {
	["I"] = true,
	["II"] = true,
	["III"] = true,
	["IV"] = true,
	["V"] = true,
	["VI"] = true,
	["VII"] = true,
	["VIII"] = true,
	["IX"] = true,
	["X"] = true,
	["XI"] = 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] .. '|' .. args[1] .. '形式]]' })
				append_cat(data, args[1] .. "-形式動詞")
			else
				error("動詞變位形式無效。請提供有效的形式。")
			end
		elseif args[1] ~= "-" then
			append_cat(data, "缺少變位形式的動詞")
		end
		handle_infl(args, data, "np", "非過去時")
		handle_infl(args, data, "vn", "動詞性名詞")
		handle_infl(args, data, "ap", "主動分詞")
		handle_infl(args, data, "pp", "被動分詞")
	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, "f", "陰性")
		handle_infl(args, data, "pl", "複數")
	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, "f", "陰性")
		handle_infl(args, data, "pl", "複數")
	end
}

pos_functions["副詞"] = {
	func = function(args, data)
		handle_infl(args, data, "obl", "斜格形式")
	end
}

pos_functions["後綴"] = {
	params = {
		["g"] = {}
	},
	func = function(args, data)
		if ine(args["g"]) then
			handle_gender(args, data)
		end
		handle_infl(args, data, "f", "陰性")
		handle_infl(args, data, "pl", "複數")
	end
}

return export