模組:Gmw-adjectives



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

local export = {}

local function detect_j(stem)
	local rest, nucleus, coda = mw.ustring.match(stem, "^(.-)([aeiouāēīōū]+)([bdfghjklmnprʀstþw]+)$")

	if mw.ustring.len(coda) > 1 or mw.ustring.find(nucleus, "^[āēīōū]$") or mw.ustring.find(nucleus, "^[aeiou][aeiou]$") then
		return "ij"
	elseif mw.ustring.find(rest, "^[bdfghjklmnprʀstþw]*$") then
		return "j"
	else
		error("Stem consists of multiple syllables or its weight cannot be otherwise be determined")
	end
end

local function geminate(stem)
	stem = mw.ustring.gsub(stem, "w$", "uw")
	stem = mw.ustring.gsub(stem, "([bdghklmnpstþ])$", "%1%1")
	return stem
end


-- Inflection functions

function export.reg(frame)
	local params = {
		[1] = {default = mw.title.getCurrentTitle().nsText == "Template" and "{{{1}}}" or mw.title.getCurrentTitle().subpageText},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)

	local data = {
		forms = {},
		info = nil,
		categories = {},
	}

	local stem, suffix = mw.ustring.match(args[1], "^(.-)([iīu])$")
	local stem_u

	if suffix == "ī" then
		stem = stem .. "ij"
		data.info = "''ja''-詞幹"
		table.insert(data.categories, lang:getCanonicalName() .. "ja-詞幹形容詞")
	elseif suffix == "i" then
		stem = geminate(stem) .. "j"
		data.info = "''ja''-詞幹"
		table.insert(data.categories, lang:getCanonicalName() .. "ja-詞幹形容詞")
	elseif suffix == "u" then
		stem_u = stem
		stem = stem .. "w"
		data.info = "''wa''-詞幹"
		table.insert(data.categories, lang:getCanonicalName() .. "wa-詞幹形容詞")
	else
		stem = args[1]
		data.info = "''a''-詞幹"
		table.insert(data.categories, lang:getCanonicalName() .. "a-詞幹形容詞")
	end
	
	stem_u = stem_u or stem

	data.forms["m|nom|s"] = {args[1]}
	data.forms["m|acc|s"] = {stem .. "anā"}
	data.forms["m|gen|s"] = {stem .. "as"}
	data.forms["m|dat|s"] = {stem_u .. "umē"}
	data.forms["m|ins|s"] = {stem_u .. "u"}

	data.forms["f|nom|s"] = {stem_u .. "u"}
	data.forms["f|acc|s"] = {stem .. "ā"}
	data.forms["f|gen|s"] = {stem .. "eʀā"}
	data.forms["f|dat|s"] = {stem .. "eʀē"}
	data.forms["f|ins|s"] = {stem .. "eʀu"}

	data.forms["n|nom|s"] = {args[1]}
	data.forms["n|acc|s"] = {args[1]}
	data.forms["n|gen|s"] = {stem .. "as"}
	data.forms["n|dat|s"] = {stem_u .. "umē"}
	data.forms["n|ins|s"] = {stem_u .. "u"}

	data.forms["m|nom|p"] = {stem .. "ē"}
	data.forms["m|acc|p"] = {stem .. "ā"}
	data.forms["m|gen|p"] = {stem .. "eʀō"}
	data.forms["m|dat|p"] = {stem .. "ēm", stem_u .. "um"}
	data.forms["m|ins|p"] = {stem .. "ēm", stem_u .. "um"}

	data.forms["f|nom|p"] = {stem .. "ō"}
	data.forms["f|acc|p"] = {stem .. "ā"}
	data.forms["f|gen|p"] = {stem .. "eʀō"}
	data.forms["f|dat|p"] = {stem .. "ēm", stem_u .. "um"}
	data.forms["f|ins|p"] = {stem .. "ēm", stem_u .. "um"}

	data.forms["n|nom|p"] = {stem_u .. "u"}
	data.forms["n|acc|p"] = {stem_u .. "u"}
	data.forms["n|gen|p"] = {stem .. "eʀō"}
	data.forms["n|dat|p"] = {stem .. "ēm", stem_u .. "um"}
	data.forms["n|ins|p"] = {stem .. "ēm", stem_u .. "um"}

	return make_table(data)
end

-- Make the table
function make_table(data)
	local function repl(param)
		if param == "info" then
			return mw.getContentLanguage():ucfirst(data.info or "")
		end
		
		local forms = data.forms[param]
		
		if not forms then
			return "—"
		end
		
		local ret = {}
		
		for key, subform in ipairs(forms) do
			table.insert(ret, require("Module:links").full_link({lang = lang, alt = "*" .. subform}))
		end
		
		return table.concat(ret, ", ")
	end
	
	local wikicode = {}
	table.insert(wikicode, [=[
{| class="inflection-table vsSwitcher" data-toggle-category="曲折變化" style="border: solid 1px #CCCCCC;"
|- style="background: #CCCCCC; text-align: left;"
! class="vsToggleElement" colspan="4" | {{{info}}}
|- class="vsShow"
! style="width: 9em;" | 單數
! style="width: 15em; background: #DDDDDD;" colspan="3" | 陽性
|- class="vsShow"
! style="background: #EEEEEE" | 主格
| colspan="3" | {{{m|nom|s}}}
|- class="vsShow"
! style="background: #EEEEEE" | 屬格
| colspan="3" | {{{m|gen|s}}}
|- class="vsHide"
! style="width: 9em;" | 單數
! style="width: 15em; background: #DDDDDD;" | 陽性
! style="width: 15em; background: #DDDDDD;" | 陰性
! style="width: 15em; background: #DDDDDD;" class="vsHide" | 中性
|- class="vsHide"
! style="background: #EEEEEE" | 主格
| {{{m|nom|s}}}
| {{{f|nom|s}}}
| {{{n|nom|s}}}
|- class="vsHide"
! style="background: #EEEEEE" | 賓格
| {{{m|acc|s}}}
| {{{f|acc|s}}}
| {{{n|acc|s}}}
|- class="vsHide"
! style="background: #EEEEEE" | 屬格
| {{{m|gen|s}}}
| {{{f|gen|s}}}
| {{{n|gen|s}}}
|- class="vsHide"
! style="background: #EEEEEE" | 與格
| {{{m|dat|s}}}
| {{{f|dat|s}}}
| {{{n|dat|s}}}
|- class="vsHide"
! style="background: #EEEEEE" | 工具格
| {{{m|ins|s}}}
| {{{f|ins|s}}}
| {{{n|ins|s}}}
|- class="vsHide"
! 複數
! style="background: #DDDDDD;" | 陽性
! style="background: #DDDDDD;" | 陰性
! style="background: #DDDDDD;" | 中性
|- class="vsHide"
! style="background: #EEEEEE" | 主格
| {{{m|nom|p}}}
| {{{f|nom|p}}}
| {{{n|nom|p}}}
|- class="vsHide"
! style="background: #EEEEEE" | 賓格
| {{{m|acc|p}}}
| {{{f|acc|p}}}
| {{{n|acc|p}}}
|- class="vsHide"
! style="background: #EEEEEE" | 屬格
| {{{m|gen|p}}}
| {{{f|gen|p}}}
| {{{n|gen|p}}}
|- class="vsHide"
! style="background: #EEEEEE" | 與格
| {{{m|dat|p}}}
| {{{f|dat|p}}}
| {{{n|dat|p}}}
|- class="vsHide"
! style="background: #EEEEEE" | 工具格
| {{{m|ins|p}}}
| {{{f|ins|p}}}
| {{{n|ins|p}}}
|}]=])

	return mw.ustring.gsub(table.concat(wikicode), "{{{([#!]?[a-z0-9|]+)}}}", repl) .. require("Module:utilities").format_categories(data.categories, lang)
end

return export