Module:de-headword: difference between revisions

From Wiktionary, the free dictionary
Jump to navigation Jump to search
Content deleted Content added
No edit summary
+
Line 247: Line 247:
local args = require("Module:parameters").process(args, params)
local args = require("Module:parameters").process(args, params)
--TODO: detect type (thanks Kenny!)
if not args.type then
--TODO: detect type (thanks Kenny!)
end
if args.type == 'strong' then
if args.type == 'strong' then
if not args.class then
if not args.class then

Revision as of 08:20, 27 February 2016

This module needs documentation.
Please document this module by describing its purpose and usage on the documentation page.

local export = {}
local pos_functions = {}

local legal_gender = {
	["m"] = true,
	["m-s"] = true,
	["m-p"] = true,
	["f"] = true,
	["f-s"] = true,
	["f-p"] = true,
	["n"] = true,
	["n-s"] = true,
	["n-p"] = true,
	["?"] = true,
	["?-s"] = true,
	["?-p"] = true,
	["p"] = true,
}

local gender_names = {
	["m"] = "masculine",
	["m-s"] = "masculine",
	["m-p"] = "masculine",
	["f"] = "feminine",
	["f-s"] = "feminine",
	["f-p"] = "feminine",
	["n"] = "neuter",
	["n-s"] = "neuter",
	["n-p"] = "neuter",
	["?"] = "unknown gender",
	["?-s"] = "unknown gender",
	["?-p"] = "unknown gender",
	["p"] = "unknown gender",
}

local legal_verb_classes = {
	['1'] = true,
	['2'] = true,
	['3'] = true,
	['4'] = true,
	['5'] = true,
	['6'] = true,
	['7'] = true,
}

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

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
	local args = frame:getParent().args
	NAMESPACE = mw.title.getCurrentTitle().nsText
	PAGENAME = mw.title.getCurrentTitle().text
	
	local head = args["head"]; if head == "" then head = nil end
	
	local poscat = frame.args[1] or error("Part of speech has not been specified. Please pass parameter 1 to the module invocation.")
	local class = frame.args[2]; if class == "" then class = nil end
	
	local genders = {}
	local inflections = {}
	local categories = {"German " .. poscat}
	local sort_key = {}
	
	if pos_functions[poscat] then
		local head2 = pos_functions[poscat](class, args, genders, inflections, categories, poscat)
		head = head or head2
	end
	
	return
		require("Module:headword").full_headword(lang, sc, head, nil, genders,
			inflections, NAMESPACE == "" and categories or {"German " .. poscat}, nil)
end

pos_functions.adjectives = function(class, args, genders, inflections, categories, poscat)
	params = {
		[1] = {list = "comp"},
		[2] = {list = "sup"},
		["head"] = {default = PAGENAME},
	}
	local args = require("Module:parameters").process(args, params)
	
	if args[1][1] == '-' then
		table.insert(inflections, {label = 'not comparable'})
		table.insert(categories, 'German uncomparable adjectives')
		return args.head
	end
	
	if #args[1] > 0 then
		for i, form in ipairs(args[1]) do
			args[1][i] = {term = (form == 'er' and PAGENAME .. 'er' or form),
				accel = 'comparative-form-of'}
		end
	else
		args[1] = {request = true}
		table.insert(categories, 'de-adj lacking comparative')
	end
	args[1].label = "[[Appendix:Glossary#comparative|comparative]]"
	table.insert(inflections, args[1])
	
	if #args[2] > 0 then
		for i, form in ipairs(args[2]) do
			args[2][i] = {term = (form == 'sten' and PAGENAME .. 'sten' or form),
				accel = 'superlative-form-of'}
		end
	else
		args[2] = {request = true}
		table.insert(categories, 'de-adj lacking superlative')
	end
	args[2].label = "[[Appendix:Glossary#superlative|superlative]]"
	table.insert(inflections, args[2])
	
	return args.head
end

pos_functions.nouns = function(class, args, genders, inflections, categories, poscat)
	params = {
		[1] = {list = 'g', default = '?'},
		[2] = {list = 'gen',
			default = (
				args[1] == 'm' or
				args[1] == 'm-s' or
				args[1] == 'n' or
				args[1] == 'n-s'
				) and PAGENAME .. 's' or PAGENAME},
		[3] = {list = 'pl'},
		[4] = {list = 'dim'},
		["head"] = {default = PAGENAME},
		["m"] = {list = true},
		["f"] = {list = true},
	}
	if poscat == 'nouns' then
		params[3].default = PAGENAME .. 'en'
	end
	local args = require("Module:parameters").process(args, params)
	
	for _, g in ipairs(args[1]) do
		if legal_gender[g] then
			table.insert(genders, g)
			table.insert(categories, "German " .. gender_names[g] .. " nouns")
		else
			error("Gender “" .. g .. "” is not an valid German gender.")
		end
	end
	
	for i, form in ipairs(args[2]) do
		args[2][i] = {term = form, accel = 'genitive-form-of'}
	end
	args[2].label = "genitive"
	table.insert(inflections, args[2])
	
	if args[3][1] == '-' then
		table.insert(inflections, {label = 'no plural'})
		table.insert(categories, 'German uncountable nouns')
	elseif #args[3] > 0 then
		for i, form in ipairs(args[3]) do
			args[3][i] = {term = form, accel = 'plural-form-of'}
		end
		args[3].label = "plural"
		table.insert(inflections, args[3])
	end
	
	if #args[4] > 0 then
		for i, form in ipairs(args[4]) do
			args[4][i] = {
				term = form,
				genders = {'n'},
				accel = 'diminutive-form-of gender-n'
				}
		end
		args[4].label = "diminutive"
		table.insert(inflections, args[4])
	end
	
	if #args.f > 0 then
		args.f.label = "feminine"
		table.insert(inflections, args.f)
	end
	
	if #args.m > 0 then
		args.m.label = "masculine"
		table.insert(inflections, args.m)
	end
	
	return args.head
end

pos_functions["proper nouns"] = pos_functions.nouns

pos_functions.adjectives = function(class, args, genders, inflections, categories, poscat)
	params = {
		[1] = {list = "comp"},
		[2] = {list = "sup"},
		["head"] = {default = PAGENAME},
	}
	local args = require("Module:parameters").process(args, params)
	
	if args[1][1] == '-' then
		table.insert(inflections, {label = 'not comparable'})
		table.insert(categories, 'German uncomparable adjectives')
		return args.head
	end
	
	if #args[1] > 0 then
		for i, form in ipairs(args[1]) do
			args[1][i] = {term = (form == 'er' and PAGENAME .. 'er' or form),
				accel = 'comparative-form-of'}
		end
	else
		args[1] = {request = true}
		table.insert(categories, 'de-adj lacking comparative')
	end
	args[1].label = "[[Appendix:Glossary#comparative|comparative]]"
	table.insert(inflections, args[1])
	
	if #args[2] > 0 then
		for i, form in ipairs(args[2]) do
			args[2][i] = {term = (form == 'sten' and PAGENAME .. 'sten' or form),
				accel = 'superlative-form-of'}
		end
	else
		args[2] = {request = true}
		table.insert(categories, 'de-adj lacking superlative')
	end
	args[2].label = "[[Appendix:Glossary#superlative|superlative]]"
	table.insert(inflections, args[2])
	
	return args.head
end

pos_functions.verbs = function(class, args, genders, inflections, categories, poscat)
	params = {
		[1] = {list = 'present ='},
		[2] = {list = 'past ='},
		[3] = {list = 'past participle ='},
		["head"] = {default = PAGENAME},
		["type"] = {default = class},
		["auxiliary"] = {default = 'haben'},
	}
	
	if class ~= 'weak' or args.type ~= 'weak' then
		params["past subjunctive"] = {list = true}
	end
	if class == 'strong' or args.type == 'strong' then
		params.class = {default = nil}
	end
	local args = require("Module:parameters").process(args, params)
	
	if not args.type then
		--TODO: detect type (thanks Kenny!)
	end
	
	if args.type == 'strong' then
		if not args.class then
			table.insert(inflections, {label = '[[Appendix:Glossary#strong verb|strong]]'})
			table.insert(categories, 'German strong verbs without classes')
		elseif legal_verb_classes[args.class] then
			table.insert(inflections, {label = 'class ' .. args.class .. ' [[Appendix:Glossary#strong verb|strong]]'})
			table.insert(categories, 'German class ' .. args.class .. ' strong verbs')
		else
			error("Strong verb class “" .. args.class .. "” is not an valid German class (1-7).")
		end
		table.insert(categories, 'German strong verbs')
	elseif args.type == 'weak' then
		table.insert(inflections, {label = '[[Appendix:Glossary#weak verb|weak]]'})
		table.insert(categories, 'German weak verbs')
	elseif args.type == 'irregular' then
		table.insert(inflections, {label = '[[Appendix:Glossary#irregular|irregular]]'})
		table.insert(categories, 'German irregular verbs')
	elseif not args.type then
		table.insert(categories, 'German verbs without conjugation types')
	else
		error("Verb type “" .. args.type .. "” is not an valid German type (“strong” or “weak”).")
	end
	
	if #args[1] == 0 then
		args[1] = {request = true}
	end
	args[1].label = 'third-person singular simple present'
	table.insert(inflections, args[1])
	
	if #args[2] == 0 then
		args[2] = {request = true}
	end
	args[2].label = 'past tense'
	table.insert(inflections, args[2])
	
	if #args[3] == 0 then
		args[3] = {request = true}
	end
	args[3].label = 'past participle'
	table.insert(inflections, args[3])
	
	if #args["past subjunctive"] > 0 then
		args["past subjunctive"].label = "past subjunctive"
		table.insert(inflections, args["past subjunctive"])
	end
	
	if args.auxiliary == 'haben' then
		args.auxiliary = {'haben'}
		table.insert(categories, 'German verbs using haben as auxiliary')
	elseif args.auxiliary == 'sein' then
		args.auxiliary = {'sein'}
		table.insert(categories, 'German verbs using sein as auxiliary')
	elseif args.auxiliary == 'both' then
		args.auxiliary = {'haben', 'sein'}
		table.insert(categories, 'German verbs using haben and sein as auxiliary')
	else
		error("Verb auxiliary “" .. args.auxiliary .. "” is not an valid German auxiliary (“haben”, “sein”, or “both”).")
	end
	args.auxiliary.label = "auxiliary"
	table.insert(inflections, args.auxiliary)
	
	return args.head
end

return export