模組:Ne-headword

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


local export = {}
local pos_functions = {}
local m_links = require("Module:links")
local lang = require('Module:languages').getByCode("ne")
local PAGENAME = mw.title.getCurrentTitle().text
local current_script = lang:findBestScript(PAGENAME)

local rfind = mw.ustring.find
local rmatch = mw.ustring.match
local rsplit = mw.text.split
local usub = mw.ustring.sub

local gender_to_full = {
	["m"] = "陽性", ["f"] = "陰性"
}


local function glossary_link(anchor, text)
	text = text or anchor
	return "[[Appendix:術語表#" .. anchor .. "|" .. text .. "]]"
end

local function track(page)
	require("Module:debug").track("ne-headword/" .. page)
end


-- Code ported from [[Module:fr-headword]].
local function add_space_word_links(space_word, split_hyphen)
	local space_word_no_punct, punct = rmatch(space_word, "^(.*)([,;:?!])$")
	space_word_no_punct = space_word_no_punct or space_word
	punct = punct or ""
	local words
	-- don't split prefixes and suffixes
	if not split_hyphen or rfind(space_word_no_punct, "^%-") or rfind(space_word_no_punct, "%-$") then
		words = {space_word_no_punct}
	else
		words = rsplit(space_word_no_punct, "%-")
	end
	local linked_words = {}
	for _, word in ipairs(words) do
		word = "[[" .. word .. "]]"
		table.insert(linked_words, word)
	end
	return table.concat(linked_words, "-") .. punct
end

local function add_lemma_links(lemma, split_hyphen)
	if not rfind(lemma, " ") then
		split_hyphen = true
	end
	local words = rsplit(lemma, " ")
	local linked_words = {}
	for _, word in ipairs(words) do
		table.insert(linked_words, add_space_word_links(word, split_hyphen))
	end
	local retval = table.concat(linked_words, " ")
	-- If we ended up with a single link consisting of the entire lemma,
	-- remove the link.
	local unlinked_retval = rmatch(retval, "^%[%[([^%[%]]*)%]%]$")
	return unlinked_retval or retval
end


function export.show(frame)
	local poscat = frame.args[1] or error("未指定詞類。請填寫參數1。")
	
	local params = {
		["head"] = {list = true},
		["tr"] = {list = true, allow_holes = true},
		["sort"] = {},
		["newa"] = {},
		["newa2"] = {},
		[1] = {},
		["pron"] = {},

		["splithyphen"] = {type = "boolean"},
	}

	if PAGENAME:find(" ") then
		track("space")
	end

	if pos_functions[poscat] then
		for key, val in pairs(pos_functions[poscat].params) do
			params[key] = val
		end
	end

	local parargs = frame:getParent().args
	local args = require("Module:parameters").process(parargs, params)

	local data = {lang = lang, sc = current_script,
		pos_category = poscat,
		heads = args["head"],
		translits = args["tr"],
		categories = {},
		genders = {},
		inflections = {},
		sort_key = args["sort"],
	}

	if #data.translits > 0 then
		track("manual-translit/" .. poscat)
	end

	local heads = data.heads
	local auto_linked_head = add_lemma_links(PAGENAME, args["splithyphen"])
	if #heads == 0 then
		data.heads = {auto_linked_head}
	else
		for _, head in ipairs(heads) do
			if head == auto_linked_head then
				track("redundant-head")
			end
		end
	end

	if pos_functions[poscat] then
		pos_functions[poscat].func(args, data)
	end

	return require("Module:headword").full_headword(data)
end
pos_functions.adjectives = {
	params = {
		["comparative"] = {},
		["superlative"] = {},
		[1] = {alias_of = "comparative"},
		[2] = {alias_of = "superlative"},
		["f"] = {list = true},
		["m"] = {list = true},
		["ind"] = {type = "boolean"},
	},
	func = function(args, data)
		if args["ind"] then
			table.insert(data.inflections, {label = glossary_link("不可比")})
			table.insert(data.categories, "尼泊爾語不可比形容詞")
		end
		if args["comparative"] then
			table.insert(data.inflections, {label = "比較級", args["comparative"]})
		end
		if args["superlative"] then
			table.insert(data.inflections, {label = "最高級", args["superlative"]})
		end
		if #args["m"] > 0 then
			args["m"].label = "陽性"
			table.insert(data.inflections, args["m"])
		end
		if #args["f"] > 0 then
			args["f"].label = "陰性"
			table.insert(data.inflections, args["f"])
		end
	end,
}

local function process_genders(data, genders)
	for _, g in ipairs(genders) do
		if g == "m" or g == "f" or g == "m-p" or g == "f-p" or g == "mf" or g == "mf-p" or g == "mfbysense" or g == "mfbysense-p" or g == "?" then
			table.insert(data.genders, g)
		else
			error("無效性別:" .. (g or "(nil)"))
		end
	end
end


local function nouns(plpos)
	return {
		params = {
			["g"] = {list = true},
			["f"] = {list = true},
			["m"] = {list = true},
			["ind"] = {type = "boolean"},
		},
		func = function(args, data)
			process_genders(data, args["g"])
			if args["ind"] then
				table.insert(data.inflections, {label = glossary_link("不可比")})
				table.insert(data.categories, "尼泊爾語不可比" .. plpos)
			end
			if #args["m"] > 0 then
				args["m"].label = "陽性"
				table.insert(data.inflections, args["m"])
			end
			if #args["f"] > 0 then
				args["f"].label = "陰性"
				table.insert(data.inflections, args["f"])
			end
		end,
	}
end

pos_functions["名詞"] = nouns("名詞")
pos_functions["專有名詞"] = nouns("專有名詞")

local function pronouns(plpos)
	return {
		params = {
			["g"] = {list = true},
		},
		func = function(args, data)
			process_genders(data, args["g"])
		end,
	}
end
pos_functions["代詞"] = pronouns("代詞")

local function verbs(plpos)
	return {
		params = {
			[1] = {},
			["g"] = {list = true},
		},
		func = function(args, data)
			data.genders = args["g"]
	
			if args[1] then
				local label, cat
				if args[1] == "t" then
					label = "及物"
					table.insert(data.categories, "尼泊爾語及物動詞")
				elseif args[1] == "i" then
					label = "不及物"
					table.insert(data.categories, "尼泊爾語不及物動詞")
				elseif args[1] == "d" then
					label = "雙及物"
					table.insert(data.categories, "尼泊爾語雙及物動詞")
				elseif args[1] == "it" or args[1] == "ti" then
					label = "可及物可不及物"
					table.insert(data.categories, "尼泊爾語及物動詞")
					table.insert(data.categories, "尼泊爾語不及物動詞")
				else
					error("無法識別的參數1=" .. args[1] .. ":應為'i' = 不及物,'t' = 及物,或'it'/'ti' = 可及物可不及物")
				end
				table.insert(data.inflections, {label = glossary_link(label)})
			end
	
			local head = data.heads[1]
			if head:find(" ") then
				local base_verb = m_links.remove_links(head):gsub("^.* ", "")
				table.insert(data.categories, "基本動詞為" .. base_verb .. "的尼泊爾語複合動詞")
			end
		end,
	}
end
pos_functions["動詞"] = verbs("動詞")

local function pos_with_gender()
	return {
		params = {
			["g"] = {list = true},
		},
		func = function(args, data)
			data.genders = args["g"]
		end,
	}
end

pos_functions.numerals = pos_with_gender()
pos_functions.suffixes = pos_with_gender()
pos_functions["形容詞變格形"] = pos_with_gender()
pos_functions["名詞變格形"] = pos_with_gender()
pos_functions["代詞變格形"] = pos_with_gender()
pos_functions["限定詞變格形"] = pos_with_gender()
pos_functions["動詞變位形式"] = pos_with_gender()
pos_functions["後置詞變格形"] = pos_with_gender()

return export