モジュール:kmr-headword

出典: フリー多機能辞典『ウィクショナリー日本語版(Wiktionary)』
ナビゲーションに移動 検索に移動

このモジュールについての説明文ページを モジュール:kmr-headword/doc に作成できます

local export = {}
local pos_functions = {}

local m_scripts = require("モジュール:scripts")

local lang = require("モジュール:languages").getByCode("kmr")
local langname = lang:getCanonicalName()
local ku_sc = m_scripts.getByCode("ku-Arab")

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

local suffix_categories = {
	["形容詞"] = true,
	["副詞"] = true,
	["名詞"] = true,
	["動詞"] = true,
}

local function track(page)
	require("モジュール:debug").track("kmr-headword/" .. page)
	return true
end

local function glossary_link(entry, text)
	text = text or entry
	return "[[付録:用語集#" .. entry .. "|" .. text .. "]]"
end

local function process_arabic_spellings(spellings)
	local inflection = {}
	for _, spelling in ipairs(spellings) do
		table.insert(inflection, {term = spelling, sc = ku_sc})
	end
	inflection.label = "アラビア文字"
	return inflection
end

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
	local tracking_categories = {}
	
	local poscat = frame.args[1]
		or error("名詞等の複数形の品詞が指定されていません。引数1をモジュールの呼び出しに渡して下さい。")
	
	local params = {
		["head"] = {list = true},
		["tr"] = {list = true, allow_holes = true},
		["id"] = {},
		["ar"] = {list = true},
		["suff"] = {type = "boolean"},
		["c"] = {type = "boolean"},
	}

	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("モジュール:parameters").process(parargs, params)
	local data = {
		lang = lang,
		pos_category = poscat,
		categories = {},
		heads = args["head"],
		translits = args["tr"],
		genders = {},
		inflections = {},
		id = args["id"],
		categories = {}
	}
	
	if args["接尾辞"] then
		data.pos_category = "接尾辞"
		
		if suffix_categories[poscat] then
			local singular_poscat = poscat:gsub("s$", "")
			table.insert(data.categories, langname .. " " .. singular_poscat .. "-forming suffixes")
		else
			error("No category exists for suffixes forming " .. poscat .. ".")
		end
	end
	
	if pos_functions[poscat] then
		pos_functions[poscat].func(args, data, tracking_categories)
	end
	
	if #args["ar"] > 0 then
		table.insert(data.inflections, process_arabic_spellings(args["ar"]))
	end

	return require("モジュール:headword").full_headword(data)
		.. require("モジュール:utilities").format_categories(tracking_categories, lang)
end

pos_functions["動詞"] = {
	params = {
		[1] = {},
		[2] = {},
	},
	func = function(args, data, tracking_categories)
		if args[1] then
			table.insert(data.inflections, {label = "現在第三人称単数形", args[1]})
		end
		if args[2] then
			table.insert(data.inflections, {label = "過去形", args[2]})
		end
	end
}

pos_functions["形容詞"] = {
	params = {
		[1] = {alias_of = "比較級"},
		["comparative"] = {list = true}, --comparative(s)
		[2] = {alias_of = "最上級"},
		["superlative"] = {list = true}, --superlative(s)
		["c"] = {type = "boolean"},
	},
	func = function(args, data, tracking_categories)
		if args.comparative[1] == "-" then
			table.insert(data.inflections, {label = "比較形無し"})
			table.insert(data.categories, langname .. " 比較形無し")
		else
			if #args.comparative > 0 then
				args.comparative.label = glossary_link("比較級")
				args.comparative.accel = {form = "比較級"}
				table.insert(data.inflections, args.comparative)
			else 
				if args["c"] then
					table.insert(data.inflections, {label = glossary_link("比較級"), accel = {form = '比較級'}, {term = PAGENAME ..'tir'}})
				end
			end
		
			if #args.superlative > 0 then
				args.superlative.label = glossary_link("最上級")
				args.superlative.accel = {form = "最上級"}
				table.insert(data.inflections, args.superlative)
			end
		end
	end
}

local noun_params = {
	[1] = {alias_of = "g"},
	["g"] = {list = true}, --gender(s)
	[2] = {alias_of = "pl"},
	["pl"] = {list = true}, --plural(s)
	["f"] = {list = true}, --feminine form(s)
	["m"] = {list = true}, --masculine form(s)
}

local allowed_genders = {
	["m"] = true,
	["f"] = true,
	["n"] = true,
	["mf"] = true,
	["m-p"] = true,
	["f-p"] = true,
	["n-p"] = true,
}

local function do_nouns(pos, args, data, tracking_categories)
	local genders = {}
	for _, g in ipairs(args.g) do
		if not allowed_genders[g] then
			error("Unrecognized gender: " .. g)
		end
		if g == "mf" then
			table.insert(genders, "m")
			table.insert(genders, "f")
		else
			table.insert(genders, g)
		end
	end

	if #genders > 0 then
		data.genders = genders
	else
		data.genders = {"?"}
	end
	
	local plpos = require("モジュール:string utilities").pluralize(pos)

	-- Check for special plural signals
	local mode = nil
	
	if args.pl[1] == "?" or args.pl[1] == "!" or args.pl[1] == "-" or args.pl[1] == "-~" or args.pl[1] == "~" or args.pl[1] == "#" then
		mode = args.pl[1]
		table.remove(args.pl, 1)  -- Remove the mode parameter
	end
	
	if mode == "?" then
		-- Plural is unknown
		table.insert(data.categories, langname .. " " .. plpos .. " with unknown or uncertain plurals")
	elseif mode == "!" then
		-- Plural is not attested
		table.insert(data.inflections, {label = "plural not attested"})
		table.insert(data.categories, langname .. " " .. plpos .. " with unattested plurals")
		return
	elseif mode == "-" then
		-- Uncountable noun; may occasionally have a plural
		table.insert(data.categories, langname .. " 不可算 " .. plpos)
		table.insert(data.inflections, {label = glossary_link("不可算")})
	elseif mode == "-~" then
		table.insert(data.categories, langname .. " 不可算 " .. plpos)
		table.insert(data.inflections, {label = "usually " .. glossary_link("不可算")})
	elseif mode == "~" then
		-- Mixed countable/uncountable noun, always has a plural
		table.insert(data.inflections, {label = glossary_link("可算") .. " 及び " .. glossary_link("不可算")})
		table.insert(data.categories, langname .. " 不可算 " .. plpos)
		table.insert(data.categories, langname .. " 可算 " .. plpos)
	elseif mode == "#" or pos == "noun" then
		-- Countable nouns; the default for regular nouns but not proper nouns
		if mode == "#" then
			table.insert(data.inflections, {label = glossary_link("可算")})
		end
		-- Not enough nouns properly use - in the second argument
		-- table.insert(data.categories, langname .. " countable " .. plpos)
	end

	if #args.pl > 0 then
		args.pl.label = "複数"
		args.pl.accel = {form = "p"}
		table.insert(data.inflections, args.pl)
	end

	if #args.f > 0 then
		args.f.label = "女性"
		table.insert(data.inflections, args.f)
	end

	if #args.m > 0 then
		args.m.label = "男性"
		table.insert(data.inflections, args.m)
	end
end

pos_functions["名詞"] = {
	params = noun_params,
	func = function(args, data, tracking_categories)
		return do_nouns("名詞", args, data, tracking_categories)
	end,
}

pos_functions["固有名詞"] = {
	params = noun_params,
	func = function(args, data, tracking_categories)
		return do_nouns("固有名詞", args, data, tracking_categories)
	end,
}

return export