Modul:hi-headword

A Wikiszótárból, a nyitott szótárból

A modult a Modul:hi-headword/doc lapon tudod dokumentálni

local export = {}
local pos_functions = {}
local m_links = require("Module:links")
local m_labels = require("Module:labels")
local m_scripts = require("Module:scripts")

local lang = require("Module:languages").getByCode("hi")
local ur_lang = require("Module:languages").getByCode("ur")
local ur_sc = require("Module:scripts").getByCode("ur-Arab")
local PAGENAME = mw.title.getCurrentTitle().text

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"] = "hímnemű", ["f"] = "nőnemű"
}

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

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

local function process_urdus(urdus)
	local inflection = {}
	for _, urdu in ipairs(urdus) do
		table.insert(inflection, {term = urdu, lang = ur_lang, sc = ur_sc})
	end
	inflection.label = "Urdu spelling"
	return inflection
end

-- Auto-add links to a "space word" (after splitting on spaces). We split off
-- final punctuation, and then split on hyphens if split_hyphen is given.
-- 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

-- Auto-add links to a lemma. We split on spaces, and also on hyphens
-- if split_hyphen is given or the word has no spaces. We don't always
-- split on hyphens because of cases like "आदान-प्रदान करना" where
-- "आदान-प्रदान" should be linked as a whole. If there's no space, however, then
-- it makes sense to split on hyphens by default.
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)
	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 = {
		["head"] = {list = true},
		["tr"] = {list = true, allow_holes = true},
		["sort"] = {},
		["ur"] = {list = true},
		["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

	if not rfind(poscat, " forms?$") then
		local heads = data.heads
		if #heads == 0 then
			heads = {PAGENAME}
		end
		for _, head in ipairs(heads) do
			-- Don't trigger on prefixes or suffixes.
			if (rfind(head, " ") or rfind(head, ".%-.")) then
				table.insert(data.categories, "hindi kifejezések")
				break
			end
		end
	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  poscat == "nouns" then
		data.pos_category = "főnevek"
	elseif  poscat == "adjectives" then
		data.pos_category = "melléknevek"
	elseif  poscat == "adverbs" then
		data.pos_category = "határozószók"
	end

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

	if #args["ur"] > 0 then
		table.insert(data.inflections, process_urdus(args["ur"]))
	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("indeclinable")})
			table.insert(data.categories, "hindi indeclinable adjectives")
		end
		if args["comparative"] then
			table.insert(data.inflections, {label = "comparative", args["comparative"]})
		end
		if args["superlative"] then
			table.insert(data.inflections, {label = "superlative", args["superlative"]})
		end
		if #args["m"] > 0 then
			args["m"].label = "hímnemű"
			table.insert(data.inflections, args["m"])
		end
		if #args["f"] > 0 then
			args["f"].label = "nőnemű"
			table.insert(data.inflections, args["f"])
		end
	end,
}

local function process_genders(data, genders, plpos)
	local function cats_for_mf()
		table.insert(data.categories, "hindi masculine " .. plpos)
		table.insert(data.categories, "hindi feminine " .. plpos)
		table.insert(data.categories, "hindi masculine and feminine " .. plpos)
		table.insert(data.categories, "hindi " .. plpos .. " with multiple genders")
	end
	local seen_gender = nil
	for _, g in ipairs(genders) do
		if g == "m" or g == "f" or g == "m-p" or g == "f-p" then
			table.insert(data.genders, g)
			local actual_gender = usub(g, 1, 1)
			table.insert(data.categories, "hindi " .. gender_to_full[actual_gender] .. " " .. plpos)
			if g:find("%-p$") then
				table.insert(data.categories, "hindi pluralia tantum")
			end
			if not seen_gender then
				seen_gender = actual_gender
			elseif seen_gender ~= actual_gender then
				table.insert(data.categories, "hindi " .. plpos .. " with multiple genders")
			end
		elseif g == "mf" or g == "fm" then
			table.insert(data.genders, usub(g, 1, 1))
			table.insert(data.genders, usub(g, 2, 2))
			cats_for_mf()
		elseif g == "mfp" or g == "fmp" or g == "mf-p" or g == "fm-p" then
			table.insert(data.genders, usub(g, 1, 1) .. "-p")
			table.insert(data.genders, usub(g, 2, 2) .. "-p")
			cats_for_mf()
			table.insert(data.categories, "hindi pluralia tantum")
		elseif g == "?" then
			table.insert(data.genders, "?")
		else
			error("Invalid gender: " .. (g or "(nil)"))
		end
	end
end


local function nouns(plpos)
	return {
		params = {
			["g"] = {list = true, default = "?"},
			["f"] = {list = true},
			["m"] = {list = true},
			["ind"] = {type = "boolean"},
		},
		func = function(args, data)
			process_genders(data, args["g"], plpos)
			if args["ind"] then
				table.insert(data.inflections, {label = glossary_link("indeclinable")})
				table.insert(data.categories, "hindi indeclinable " .. plpos)
				table.insert(data.categories, "hindi " .. plpos)
			end
			if #args["m"] > 0 then
				args["m"].label = "hímnemű"
				table.insert(data.inflections, args["m"])
				table.insert(data.categories, "hindi " .. plpos)
			end
			if #args["f"] > 0 then
				args["f"].label = "nőnemű"
				table.insert(data.inflections, args["f"])
				table.insert(data.categories, "hindi " .. plpos)
			end
		end,
	}
end

pos_functions.nouns = nouns("főnevek")

pos_functions["proper nouns"] = nouns("tulajdonnevek")

pos_functions.pronouns = {
	params = {
		["g"] = {list = true},
	},
	func = function(args, data)
		process_genders(data, args["g"], "pronouns")
	end,
}

pos_functions.verbs = {
	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 = "transitive"
				table.insert(data.categories, "hindi transitive verbs")
			elseif args[1] == "i" then
				label = "intransitive"
				table.insert(data.categories, "hindi intransitive verbs")
			elseif args[1] == "it" or args[1] == "ti" then
				label = "ambitransitive"
				table.insert(data.categories, "hindi transitive verbs")
				table.insert(data.categories, "hindi intransitive verbs")
			else
				error("Unrecognized param 1=" .. args[1] .. ": Should be 'i' = intransitive, 't' = transitive, or 'it'/'ti' = ambitransitive")
			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, "hindi phrasal verbs")
			table.insert(data.categories, "hindi phrasal verbs with particle (" .. base_verb .. ")")
		end
	end,
}

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["adjective forms"] = pos_with_gender()
pos_functions["noun forms"] = pos_with_gender()
pos_functions["pronoun forms"] = pos_with_gender()
pos_functions["determiner forms"] = pos_with_gender()
pos_functions["verb forms"] = pos_with_gender()
pos_functions["postposition forms"] = pos_with_gender()

return export