跳转到内容

模組:Bar-headword

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


local export = {}
local pos_functions = {}

local lang = require("Module:languages").getByCode("bar")
local script = require('Module:scripts').getByCode("Latn")

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

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

local function handle_infl(args, data, argpref, label)
	if args[argpref] and args[argpref..'2'] then
		local forms = {}
		local form = argpref
		local i = 1
		while args[form] do
			table.insert (forms, args[form])
			i = i + 1
			form = argpref..i
		end
		forms.label = mw.ustring.gsub(label, 'form', 'forms')
		table.insert(data.inflections, forms )
	elseif args[argpref] then
		table.insert(data.inflections, { label = label, args[argpref]})
	end
end

function export.show(frame)

	local args = frame:getParent().args
	local poscat = frame.args[1] or error("Part of speech has not been specified. Please pass parameter 1 to the module invocation.")
	poscat = string.gsub(poscat, 'form', 'forms')
	local data = {lang = lang, sc = script, pos_category = poscat, categories = {}, heads = {args["head"] or PAGENAME}, genders = {}, inflections = {}, sort_key = args["sort"]}
	
	if poscat == 'nouns' then
		if ine(args[1]) then
			args['g'] = args[1]
		end
		if ine(args[2]) then
			args['pl'] = args[2]
		end
		if ine(args[3]) then
			args['gen'] = args[3]
		end
	end
	
	if args["cat2"] then
		table.insert(data.categories, "Bavarian " .. args["cat2"])
	end
	if args["cat3"] then
		table.insert(data.categories, "Bavarian " .. args["cat3"])
	end
	
	handle_infl(args, data, "stem", "stem")
	
	if pos_functions[poscat] then
		pos_functions[poscat](args, data)
	end
	
	return require("Module:headword").full_headword(data)
end

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

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

	if g and g2 then
		append_cat(data, "有多種性別的詞")
	end
end

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


pos_functions["形容詞"] = function(args, data)
	handle_infl(args, data, "cp", "比較級")
	handle_infl(args, data, "sup", "最高級")
end

pos_functions["冠詞"] = function(args, data)
	handle_gender(args, data)
	handle_infl(args, data, "acc", "賓格")
	handle_infl(args, data, "dat", "與格")
end

pos_functions["名詞"] = function(args, data)
	handle_gender(args, data)
	handle_infl(args, data, "gen", "屬格")
	handle_infl(args, data, "pl", "複數")
	handle_infl(args, data, "f", "陰性")
	
	local g = ine(args["g"]) or default
	local g2 = ine(args["g2"])

		if is_masc_sg(g) or is_masc_sg(g2) then
			append_cat(data, "陽性名詞")
		elseif is_fem_sg(g) or is_fem_sg(g2) then
			append_cat(data, "陰性名詞")
		elseif is_neut_sg(g) or is_neut_sg(g2) then
			append_cat(data, "中性名詞")
		end
end

pos_functions["代詞"] = function(args, data)
	handle_infl(args, data, "pl", "複數")
end

pos_functions["動詞"] = function(args, data)
	handle_infl(args, data, "pp", "過去分詞")
	handle_infl(args, data, "past", "第三人稱單數過去時")
	if ine(args['past']) then
		handle_infl(args, data, "subj", "虛擬語氣")
	else
		handle_infl(args, data, "subj", "第三人稱單數虛擬語氣")
	end
end

return export