Module:ar-headword: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
No edit summary
adj plurals should be m-p by default
Line 317: Line 317:
table.insert(categories, 1, lang:getCanonicalName() .. " plurals")
table.insert(categories, 1, lang:getCanonicalName() .. " plurals")
--table.insert(categories, 1, lang:getCanonicalName() .. " adjective forms")
--table.insert(categories, 1, lang:getCanonicalName() .. " adjective forms")
handle_gender(args, genders, "p")
handle_gender(args, genders, "m-p")
end
end



Revision as of 06:38, 28 November 2014


This module is used by many Arabic headword-line templates.


local m_utilities = require("Module:ar-utilities")

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

local export = {}
local pos_functions = {}

-- The main entry point.
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.")
	
	-- Gather parameters
	local heads = {}
	local translits = {}
	local genders = {}
	local inflections = {}
	local categories = {"Arabic " .. poscat}
	
	local head = args["head"] or args[1] or ""
	local translit = args["tr"]; if translit == "" then translit = nil end
	local i = 1
	
	while head do
		if head then
			table.insert(heads, head)
			translits[#heads] = translit
		end
		
		i = i + 1
		head = args["head" .. i]; if head == "" then head = nil end
		translit = args["tr" .. i]; if translit == "" then translit = nil end
	end
	
	if pos_functions[poscat] then
		pos_functions[poscat](args, genders, inflections, categories)
	end
	
	if args[3] or args[4] or args[5] or args[6] or args[7] or args[8] or args[9] then
		require("Module:debug").track("ar-head/num")
	end
	
	if args["head"] then
		require("Module:debug").track("ar-head/head")
	end
	
	if args["g"] then
		require("Module:debug").track("ar-head/g")
	end
	
	return require("Module:headword").full_headword(lang, nil, heads, translits, genders, inflections, categories, nil)
end


-- If Not Empty
local function ine(arg)
	if arg == "" then
		return nil
	else
		return arg
	end
end


-- Get a list of inflections
local function params(args, argpref)
	-- Gather parameters
	local forms = {}
	
	local form = ine(args[argpref])
	local translit = ine(args[argpref .. "tr"])
	local i = 1
	
	while form do
		table.insert(forms, {term = form, translit = translit})
		
		i = i + 1
		form = ine(args[argpref .. i])
		translit = ine(args[argpref .. i .. "tr"])
	end
	
	return forms
end

-- Get a list of inflections from the arguments in ARGS based on argument
-- prefix ARGPREF, label with LABEL and insert into inflections list
-- INFLECTIONS.
local function get_forms(args, argpref, label)
	local infls = params(args, argpref)
	infls.label = label
	
	if #infls > 0 then
		return infls
	else
		return nil
	end
end

-- Handle the case where pl=-, indicating an uncountable noun.
local function handle_noun_plural(args, inflections, categories)
	if args["pl"] == "-" then
		table.insert(inflections, {label = "usually [[Appendix:Glossary#uncountable|uncountable]]"})
		table.insert(categories, lang:getCanonicalName() .. " uncountable nouns")
	else
		local forms = get_forms(args,  "pl", "plural")
		table.insert(inflections, forms)
		
		if forms then
			-- FIXME: should not use args[1] here...
			local pattern1, pattern2 = m_utilities.find_pattern(args[1], forms[1].term)
			
			if not pattern1 then
				require("Module:debug").track("ar-head/no pattern")
			elseif not pattern2 then
				require("Module:debug").track("ar-head/sound")
				require("Module:debug").track("ar-head/sound/" .. pattern1)
			end
		end
	end
end

local valid_genders = {
	["m"] = true,
	["f"] = true,
	["m-s"] = true,
	["f-s"] = true,
	["m-d"] = true,
	["f-d"] = true,
	["p"] = true,
	["m-p"] = true,
	["f-p"] = true,
}

-- Handle gender in unnamed param 2 and a second gender in param g2,
-- inserting into the list of genders in GENDER. If gender unspecified,
-- default to DEFAULT.
local function handle_gender(args, genders, default)
	local g = ine(args[2])
	local g2 = ine(args["g2"])
	g = g or default

	if valid_genders[g] then
		table.insert(genders, g)
	else
		table.insert(genders, "?")
	end
	
	if valid_genders[g2] then
		table.insert(genders, g2)
	elseif g2 then
		table.insert(genders, "?")
	end
end	

-- Part-of-speech functions

pos_functions["adjectives"] = function(args, genders, inflections, categories)
	table.insert(inflections, get_forms(args, "cons", "construct state"))
	table.insert(inflections, get_forms(args, "f", "feminine"))
	table.insert(inflections, get_forms(args, "d", "dual"))
	table.insert(inflections, get_forms(args, "dobl", "dual oblique"))
	table.insert(inflections, get_forms(args, "cpl", "common plural"))
	table.insert(inflections, get_forms(args, "cplobl", "common plural oblique"))
	table.insert(inflections, get_forms(args, "pl", "masculine plural"))
	table.insert(inflections, get_forms(args, "plobl", "masculine plural oblique"))
	table.insert(inflections, get_forms(args, "fpl", "feminine plural"))
	table.insert(inflections, get_forms(args, "fplobl", "feminine plural oblique"))
	table.insert(inflections, get_forms(args, "el", "elative"))
end


pos_functions["collective nouns"] = function(args, genders, inflections, categories)
	table.insert(categories, 1, lang:getCanonicalName() .. " nouns")
	table.insert(inflections, {label = "collective"})
	
	local g = ine(args[2]) or "m"
	if g ~= "m" then
		require("Module:debug").track("ar-head/coll nm")
	end

	handle_gender(args, genders, "m")

	-- Singulative
	local sing = ine(args["sing"])
	local singtranslit = ine(args["singtr"])
	local singg = ine(args["singg"])

	if sing then
		table.insert(inflections, {label = "singulative", {term = sing, translit = singtranslit, genders = {singg or "f"}}})
		
		if args["singg"] then
			require("Module:debug").track("ar-head/singg")
			
			if args["singg"] == "m" or args["singg"] == "f" then
				require("Module:debug").track("ar-head/singg/" .. args["singg"])
			else
				require("Module:debug").track("ar-head/singg/-")
			end
		end
	end

	table.insert(inflections, get_forms(args, "cons", "construct state"))
	table.insert(inflections, get_forms(args, "d", "dual"))
	table.insert(inflections, get_forms(args, "dobl", "dual oblique"))
	table.insert(inflections, get_forms(args, "pauc", "paucal"))
	handle_noun_plural(args, inflections, categories)
end


pos_functions["singulative nouns"] = function(args, genders, inflections, categories)
	table.insert(categories, 1, lang:getCanonicalName() .. " nouns")
	table.insert(inflections, {label = "singulative"})

	local g = ine(args[2]) or "f"
	if g ~= "f" then
		require("Module:debug").track("ar-head/sing nf")
	end

	handle_gender(args, genders, "f")

	-- Collective
	local coll = ine(args["coll"])
	local colltranslit = ine(args["colltr"])
	local collg = ine(args["collg"])
	
	if coll then
		table.insert(inflections, {label = "collective", {term = coll, translit = colltranslit, genders = {collg or "m"}}})
		
		if args["collg"] then
			require("Module:debug").track("ar-head/collg")
			
			if args["collg"] == "m" or args["collg"] == "f" then
				require("Module:debug").track("ar-head/collg/" .. args["collg"])
			else
				require("Module:debug").track("ar-head/collg/-")
			end
		end
	end
	
	table.insert(inflections, get_forms(args, "cons", "construct state"))
	table.insert(inflections, get_forms(args, "d", "dual"))
	table.insert(inflections, get_forms(args, "dobl", "dual oblique"))
	table.insert(inflections, get_forms(args, "pauc", "paucal"))
	handle_noun_plural(args, inflections, categories)
end


pos_functions["nouns"] = function(args, genders, inflections, categories)
	handle_gender(args, genders)

	table.insert(inflections, get_forms(args, "cons", "construct state"))
	table.insert(inflections, get_forms(args, "d", "dual"))
	table.insert(inflections, get_forms(args, "dobl", "dual oblique"))
	handle_noun_plural(args, inflections, categories)
	table.insert(inflections, get_forms(args, "plobl", "plural oblique"))
	table.insert(inflections, get_forms(args, "f", "feminine"))
	table.insert(inflections, get_forms(args, "m", "masculine"))
end


pos_functions["numerals"] = function(args, genders, inflections, categories)
	table.insert(categories, lang:getCanonicalName() .. " cardinal numbers")
	handle_gender(args, genders)
	
	table.insert(inflections, get_forms(args, "cons", "construct state"))
	table.insert(inflections, get_forms(args, "obl", "oblique"))
	table.insert(inflections, get_forms(args, "d", "dual"))
	table.insert(inflections, get_forms(args, "dobl", "dual oblique"))
	handle_noun_plural(args, inflections, categories)
	table.insert(inflections, get_forms(args, "plobl", "plural oblique"))
	table.insert(inflections, get_forms(args, "f", "feminine"))
	table.insert(inflections, get_forms(args, "fobl", "feminine oblique"))
	table.insert(inflections, get_forms(args, "m", "masculine"))
	table.insert(inflections, get_forms(args, "m", "masculine oblique"))
end


pos_functions["proper nouns"] = function(args, genders, inflections, categories)
	handle_gender(args, genders)

	table.insert(inflections, get_forms(args, "cons", "construct state"))
	table.insert(inflections, get_forms(args, "obl", "oblique"))
	table.insert(inflections, get_forms(args, "f", "feminine"))
	table.insert(inflections, get_forms(args, "obl", "oblique"))
	table.insert(inflections, get_forms(args, "m", "masculine"))
	table.insert(inflections, get_forms(args, "mobl", "masculine oblique"))
end


pos_functions["verbal nouns"] = function(args, genders, inflections, categories)
	table.insert(categories, 1, lang:getCanonicalName() .. " nouns")
	handle_gender(args, genders)
	
	table.insert(inflections, get_forms(args, "cons", "construct state"))
	handle_noun_plural(args, inflections, categories)
	table.insert(inflections, get_forms(args, "f", "feminine"))
	table.insert(inflections, get_forms(args, "m", "masculine"))
end

pos_functions["pronouns"] = function(args, genders, inflections, categories)
	handle_gender(args, genders)
end

pos_functions["noun plural forms"] = function(args, genders, inflections, categories)
	table.insert(categories, 1, lang:getCanonicalName() .. " plurals")
	--table.insert(categories, 1, lang:getCanonicalName() .. " noun forms")
	handle_gender(args, genders, "p")
end

pos_functions["noun dual forms"] = function(args, genders, inflections, categories)
	table.insert(categories, 1, lang:getCanonicalName() .. " duals")
	--table.insert(categories, 1, lang:getCanonicalName() .. " noun forms")
	handle_gender(args, genders, "m-d")
end

pos_functions["adjective plural forms"] = function(args, genders, inflections, categories)
	table.insert(categories, 1, lang:getCanonicalName() .. " plurals")
	--table.insert(categories, 1, lang:getCanonicalName() .. " adjective forms")
	handle_gender(args, genders, "m-p")
end

pos_functions["adjective dual forms"] = function(args, genders, inflections, categories)
	table.insert(categories, 1, lang:getCanonicalName() .. " duals")
	--table.insert(categories, 1, lang:getCanonicalName() .. " adjective forms")
	handle_gender(args, genders, "m-d")
end

pos_functions["plurals"] = function(args, genders, inflections, categories)
	handle_gender(args, genders, "p")
end

return export