Module:ine-headword

From Wiktionary, the free dictionary
Jump to navigation Jump to search
This module needs documentation.
Please document this module by describing its purpose and usage on the documentation page.

local export = {}
local pos_functions = {}

local lang = require("Module:languages").getByCode("ine-pro")

-- The main entry point.
-- This is the only function that can be invoked from a template.
function export.show(frame)
	local poscat = frame.args[1]
		or error("Part of speech has not been specified. Please pass parameter 1 to the module invocation.")

	local parargs = frame:getParent().args

	local params = {
		["head"] = {list = true},
	}

	if pos_functions[poscat] then
		for key, val in pairs(pos_functions[poscat].params) do
			params[key] = val
		end
	end

	local args = require("Module:parameters").process(parargs, params)

	local data = {lang = lang, pos_category = poscat, categories = {}, heads = args.head, inflections = {}}

	if pos_functions[poscat] then
		pos_functions[poscat].func(args, data)
	end
	
	return require("Module:headword").full_headword(data)
end

local consonants = {
	["p"] = "C",
	["t"] = "C",
	["ḱ"] = "C",
	["k"] = "C",
	["kʷ"] = "C",
	
	["b"] = "C",
	["d"] = "C",
	["ǵ"] = "C",
	["g"] = "C",
	["gʷ"] = "C",
	
	["bʰ"] = "C",
	["dʰ"] = "C",
	["ǵʰ"] = "C",
	["gʰ"] = "C",
	["gʷʰ"] = "C",
	
	["s"] = "š",  -- Placeholder symbol
	
	["w"] = "ṁ",  -- Placeholder symbol
	["m"] = "ṁ",  -- Placeholder symbol
	
	["y"] = "R",
	["l"] = "R",
	["r"] = "R",
	["n"] = "R",
	
	["h₁"] = "H",
	["h₂"] = "H",
	["h₃"] = "H",
	["H"] = "H",
}

local function split(rest)
	local ret = {}
	
	while mw.ustring.len(rest) > 0 do
		local longestmatch = ""
		
		for cons, _ in pairs(consonants) do
			if mw.ustring.len(cons) > mw.ustring.len(longestmatch) and mw.ustring.sub(rest, 1, mw.ustring.len(cons)) == cons then
				longestmatch = cons
			end
		end
		
		if mw.ustring.len(longestmatch) == 0 then
			return nil
		end
		
		table.insert(ret, consonants[longestmatch])
		rest = mw.ustring.sub(rest, mw.ustring.len(longestmatch) + 1)
	end
	
	return ret
end

local function get_root_shape(root)
	root = mw.ustring.gsub(root, "[()⁽⁾]", "")
	root = mw.ustring.gsub(root, "-$", "")
	local onset, coda = mw.ustring.match(root, "([^e]+)e([^e]+)")
	
	if not onset then
		return nil
	end
	
	onset = split(onset)
	coda = split(coda)
	
	if not onset or not coda then
		return nil
	end
	
	onset = table.concat(onset)
	coda = table.concat(coda)
	
	onset = mw.ustring.gsub(onset, "ṁR", "MR")
	onset = mw.ustring.gsub(onset, "ṁ", "R")
	coda = mw.ustring.gsub(coda, "ṁ", "R")
	
	onset = mw.ustring.gsub(onset, "šC", "sC")
	onset = mw.ustring.gsub(onset, "Cš", "Cs")
	onset = mw.ustring.gsub(onset, "š", "C")
	
	coda = mw.ustring.gsub(coda, "šC", "sC")
	coda = mw.ustring.gsub(coda, "Cš", "Cs")
	coda = mw.ustring.gsub(coda, "š", "C")
	
	if not (
		onset == "C" or
		onset == "CH" or
		onset == "CR" or
		
		onset == "H" or
		onset == "HC" or
		onset == "HR" or
		
		onset == "R" or
		onset == "MR" or
		
		onset == "sC" or
		onset == "sCR") then
		return nil
	end
	
	if not (
		coda == "C" or
		coda == "HC" or
		coda == "RHC" or
		coda == "RC" or
		coda == "HRC" or
		coda == "sC" or
		coda == "RsC" or
		
		coda == "H" or
		coda == "CH" or
		coda == "RCH" or
		coda == "RH" or
		
		coda == "R" or
		coda == "HR" or
		
		coda == "Cs") then
		return nil
	end
	
	return onset .. "e" .. coda
end

pos_functions["roots"] = {
	params = {
		[1] = {}, -- aspect
	},
	func = function(args, data)
		local repr = get_root_shape(mw.title.getCurrentTitle().subpageText)
		
		if repr then
			table.insert(data.categories, lang:getCanonicalName() .. " " .. repr .. "-shape roots")
		else
			table.insert(data.categories, lang:getCanonicalName() .. " irregular-shape roots")
		end
		
		if mw.ustring.find(mw.title.getCurrentTitle().subpageText, "^%(s%)") then
			table.insert(data.categories, lang:getCanonicalName() .. " roots with s-mobile")
		end
		
		local aspect = args[1]
		
		if aspect == "impf" then
			table.insert(data.inflections, {label = "imperfective"})
			table.insert(data.categories, lang:getCanonicalName() .. " imperfective roots")
		elseif aspect == "pf" then
			table.insert(data.inflections, {label = "perfective"})
			table.insert(data.categories, lang:getCanonicalName() .. " perfective roots")
		elseif aspect == "stat" then
			table.insert(data.inflections, {label = "stative"})
			table.insert(data.categories, lang:getCanonicalName() .. " stative roots")
		elseif aspect == "noun" or aspect == "nom" then
			table.insert(data.inflections, {label = "nominal"})
			table.insert(data.categories, lang:getCanonicalName() .. " nominal roots")
		elseif aspect == "adj" then
			table.insert(data.inflections, {label = "adjectival"})
			table.insert(data.categories, lang:getCanonicalName() .. " adjectival roots")
		elseif aspect then
			error("Invalid aspect")
		end
	end
}

return export