This module shows gender annotations, such as પું. or સ્ત્રી. બહુવચન. "Gender" is something of a misnomer, as the available annotation categories include animacy and verb aspect (though the former is sometimes also considered a part of word gender). It is used as part of other modules, or can be called from a template. The module is invoked with a list of one or more "gender specifications". A specification is a single, full "gender", and can be either a simple gender such as "m", a combination of gender and number like "m-p" or something else.

Gender specifications for Lua script writers

ફેરફાર કરો

Every specification is a list of codes, given as a single string with the codes separated by hyphens. Each code is defined within the module itself, and when displaying a specification, each code in the specification is converted into the appropriate display form. The different codes within the specification are then added together, separated by spaces. If the module is given several specifications at once (in a list), then each item in the list is separated by a comma.

Examples:

List Result
{"પુ"} પું.
{"પુ-બહુ"} પું. બહુવચન
{"પુ-જીવ-બહુ"} પું. જીવ બહુવચન
{"સ્ત્રી-દ્વિ", "પુ-બહુ"} સ્ત્રી. દ્વિવચન, પું. બહુવચન
{"પુ-બહુ", "સ્ત્રી-બહુ"} પું. બહુવચન, સ્ત્રી. બહુવચન
{"પુ", "સ્ત્રી", "બહુ"} પું., સ્ત્રી., બહુવચન

Specifications that begin with "c" (but not "c" itself) are treated specially. They are considered noun classes, and the part immediately after the "c" is simply treated as some kind of name for a noun class; usually this will be a number. Noun classes do not have sub-parts, so they will not contain hyphens. When more than one specification is given, they must all be noun classes, and they are displayed separated with a forward slash instead, and preceded by class.

Examples:

List Result
{"વર્ગ1"} વર્ગ 1
{"વર્ગ1", "વર્ગ2"} વર્ગ 1/2
{"વર્ગ1ક", "વર્ગ2ખ"} વર્ગ 1ક/2ખ

The module can be used from another module by importing it and calling the exported format_list function. It requires one parameter, which must be a table of zero or more strings. It will then return a string containing the result. For example:

local gen = require("Module:લિંગ અને વચન")
local example1 = gen.format_list({"પુ"})
local example2 = gen.format_list({"પુ", "સ્ત્રી"})
local example3 = gen.format_list({"પુ-બહુ"})

It can also be invoked from a template. The function show_list is used for this. It works the same way as the format_list function, but the specifications are passed as parameters to the module invocation, like so:

*{{#invoke:લિંગ અને વચન|show_list|પુ}}
*{{#invoke:લિંગ અને વચન|show_list|પુ|સ્ત્રી}}
*{{#invoke:લિંગ અને વચન|show_list|પુ-બહુ}}
  • પું.
  • પું., સ્ત્રી.
  • પું. બહુવચન

There is no limit to the number of parameters that can be given this way. The module will process all of its parameters until it finds one that is empty. This means that the following will display only "પું." and not "પું., ન.":

{{#invoke:લિંગ અને વચન|show_list|પુ||ન}}

--[=[
	This module creates standardised displays for gender and number.
	It converts a gender specification into Wiki/HTML format.
	
	A gender specification is a list of one of the elements listed below,
	separated by hyphens. Examples are: "વર્ગ", "ન", "સ્ત્રી-બહુ", "પુ-જીવ-બહુ"
]=]--

local export = {}

local codes = {}

-- A list of all possible "parts" that a specification can be made out of.

codes["?"] = '<abbr title="gender incomplete">?</abbr>'

-- Genders
codes["પુ"] = '<abbr title="પુંલિંગ">પું.</abbr>'
codes["સ્ત્રી"] = '<abbr title="સ્ત્રીલિંગ">સ્ત્રી.</abbr>'
codes["ન"] = '<abbr title="નપુંસકલિંગ">ન.</abbr>'
codes["વર્ગ"] = '<abbr title="common gender">વર્ગ</abbr>'

-- Additional qualifiers
codes["જીવ"] = '<abbr title="animate">જીવ</abbr>'
codes["અજીવ"] = '<abbr title="inanimate">જીવ</abbr>'
codes["વ્યક્તિ"] = '<abbr title="personal">વ્યક્તિ</abbr>'

-- Numbers
codes["એક"] = '<abbr title="singular number">એકવચન</abbr>'
codes["દ્વિ"] = '<abbr title="dual number">દ્વિવચન</abbr>'
codes["બહુ"] = '<abbr title="plural number">બહુવચન</abbr>'

-- Verb qualifiers
codes["અપૂર્ણ"] = '<abbr title="imperfective aspect">અપૂર્ણ</abbr>'
codes["સંપૂર્ણ"] = '<abbr title="perfective aspect">સંપૂર્ણ</abbr>'

-- Version of format_list that can be invoked from a template.
function export.show_list(frame)
	local args = frame.args
	local lang = args["ભાષા"]; if lang == "" then lang = nil end
	local list = {}
	local i = 1
	
	while args[i] and args[i] ~= "" do
		table.insert(list, args[i])
		i = i + 1
	end
	
	return export.format_list(list, lang)
end

-- Format one or more gender specifications, in the form of a table of specifications.
function export.format_list(list, lang)
	local is_nounclass = nil
	
	-- Iterate over each specification and format it
	for key, spec in ipairs(list) do
		local nc
		list[key], nc = export.format_specification(spec, lang)
		
		-- Ensure that the specifications are either all noun classes, or none are.
		if is_nounclass == nil then
			is_nounclass = nc
		elseif is_nounclass ~= nc then
			error("નામ વર્ગો અંદ લિંગોનું મિશ્રણ ના થાય। કૃપયા એક જ વાપર।")
		end
	end
	
	if is_nounclass then
		-- Add the processed codes together with slashes
		return "<span class=\"લિંગ\">વર્ગ " .. table.concat(list, "/") .. "</span>"
	else
		-- Add the processed codes together with commas
		return "<span class=\"લિંગ\">" .. table.concat(list, ", ") .. "</span>"
	end
end

-- Format the sub-parts of a single gender specification.
function export.format_specification(spec, lang)
	local categories = ""
	local ret = ""
	local is_nounclass = false
	
	-- If the specification starts with cX, then it is a noun class specification.
	if spec:find("^[1-9]") or spec:find("^વર્ગ[^-]") then
		is_nounclass = true
		code = spec:gsub("^વર્ગ", "")
		
		if code == "?" then
			ret = "<abbr class=\"નામ-વર્ગ\" શીર્ષક=\"નામ વર્ગ ગાયબ\">?</abbr>"
		else
			ret = "<abbr class=\"નામ-વર્ગ\" શીર્ષક=\"નામ વર્ગ " .. code .. "\">" .. code .. "</abbr>"
		end
	else
		-- Split the parts and iterate over each part, converting it into its display form
		local parts = mw.text.split(spec, "-")
		
		for key, code in ipairs(parts) do
			-- Is this code valid?
			if codes[code] then
				parts[key] = codes[code]
			else
				error("The gender specification \"" .. spec .. "\" is not valid.")
			end
		end
		
		-- Add the processed codes together with non-breaking spaces
		ret = table.concat(parts, "&nbsp;")
	end
	
	-- Do some additional checks if a language was given
	if lang then
		local m_languages = mw.loadData("Module:ભાષાઓ/સર્વઆંકડા")
		local langinfo = m_languages[lang] or error("The language code \"" .. lang .. "\" is not valid.")
		
		-- Is this an incomplete gender?
		if spec:find("?") then
			local m_utilities = require("Module:ઉપયોગીતા")
			categories = m_utilities.format_categories({langinfo.names[1] .. " શબ્દો લિંગ વગરના"}, nil)
		-- Check if the specification is valid
		elseif langinfo.genders then
			local valid_genders = {}
			for _, g in ipairs(langinfo.genders) do valid_genders[g] = true end
			
			if not valid_genders[spec] then
				local valid_string = {}
				for i, g in ipairs(langinfo.genders) do valid_string[i] = g end
				error("The gender specification \"" .. spec .. "\" is not valid for " .. langinfo.names[1] .. ". Valid are: " .. table.concat(valid_string, ", "))
			end
		end
	end
	
	return ret .. categories, is_nounclass
end

return export