Module:User:JohnC5/Sandbox

From Wiktionary, the free dictionary
Jump to navigation Jump to search

This is a private module sandbox of JohnC5, for his own experimentation. Items in this module may be added and removed at JohnC5's discretion; do not rely on this module's stability.


local export = {}

local m_para = require("Module:parameters")
local m_links = require("Module:links")
local m_scripts = require("Module:scripts")
local lang = require("Module:languages").getByCode("sa")
local PAGENAME = mw.title.getCurrentTitle().text

local genders = {
	m = 'Masculine', f = 'Feminine', n = 'Neuter',
}
local numbers = {
	sdp = '', s = 'singular', p = 'plural'
}
local cases = {
	{'n', 'Nominative'}, {'v', 'Vocative'}, {'ac', 'Accusative'}, {'i', 'Instrumental'},
	{'d', 'Dative'}, {'ab', 'Ablative'}, {'g', 'Genitive'}, {'l', 'Locative'},
}
local sandhi = {
	Deva = {
		n = '[रऋॠषृॄ][ािॊोौ्ीुूेैपफबभमकखगघङहवयं]*$',  -- Checks for n > ṇ conversion
		m = '^(?:[कखगघङचछजझञटठडढणतथदधनपफबभमयरलळवशषसह]्)*[कखगघङचछजझञटठडढणतथदधनपफबभमयरलळवशषसह]ी$',  -- Checks to see if a word is monosyllabic ending in 'ī'
	},
}
local endings = {
	Deva = {
		['[कखगघङचछजझञटठडढणतथदधनपफबभमयरलळवशषसह]$'] = 'a',
		['ा$'] = 'ā',
		['ु$'] = 'u',
		['ी$'] = 'ī',
	},
}

function joinSuffix(head, suffixes, sc, scCode)
	local terms = {}
	local term = ''
	
	for _, suffix in ipairs(suffixes) do
		if mw.ustring.match(suffix, '^⌫') then --backspace
			term = mw.ustring.sub(head, 1, -2) .. mw.ustring.sub(suffix, 2, -1)
		else
			term = head .. suffix
		end
		table.insert(terms, m_links.full_link({term = term, lang = lang, sc = sc}))
	end
	return table.concat(terms, ', ')
end

function make_header(width, g, decl, head, number, sc)
	local title = genders[g] .. ' ' .. numbers[number] .. ' ' .. decl .. ' declension of ' .. m_links.full_link({term = nil, alt = head, lang = lang, sc = sc})
	
	local header = '<div class="NavFrame" style="width:' .. width .. 'em">\n'
	header = header .. '<div class="NavHead" style="background:#d9ebff">&nbsp;&nbsp;' .. title .. '</div>\n'
	header = header .. '<div class="NavContent">\n'
	header = header .. '{| style="background:#F9F9F9;text-align:center;width:' .. width ..'em" class="inflection-table"\n'
	header = header .. '|-\n'
	header = header .. '! style="background:#eff7ff" |\n'
	
	if number == 'sdp' then
		header = header .. '! style="background:#eff7ff" | Singular\n'
		header = header .. '! style="background:#eff7ff" | Dual\n'
		header = header .. '! style="background:#eff7ff" | Plural\n'
	elseif number == 's' then
		header = header .. '! style="background:#eff7ff" | Singular\n'
	elseif number == 'p' then
		header = header .. '! style="background:#eff7ff" | Plural\n'
	end
	return header
end

function make_sing_table(head, pattern, g, sc, scCode)
	local output = make_header('40', g, pattern.decl, head, 's', sc)
	local stem = mw.ustring.sub(head, 1, -1 - (pattern.stem or 0))
	local n = mw.ustring.match(stem, sandhi[scCode]['n']) ~= nil and 'n' or ''
	local m = mw.ustring.match(head, sandhi[scCode]['m']) ~= nil and 'm' or ''
	for _, case in ipairs(cases) do
		local code = case[1]
		local name = case[2]
		output = output .. '|-\n'
		output = output .. '| style="background-color:#eff7ff; font-style:italic;" | ' .. name ..'\n'
		output = output .. '| ' .. joinSuffix(stem, pattern[code .. 's' .. n .. m] or pattern[code .. 's' .. n] or pattern[code .. 's' .. m] or pattern[code .. 's'], sc, scCode) .. '\n'
	end
	output = output .. '|}</div></div>'
	return output
end

function make_plur_table(head, pattern, g, sc, scCode)
	local output = make_header('40', g, pattern.decl, head, 'p', sc)
	local stem = mw.ustring.sub(head, 1, -1 - (pattern.stem or 0))
	local n = mw.ustring.match(stem, sandhi[scCode]['n']) ~= nil and 'n' or ''
	local m = mw.ustring.match(head, sandhi[scCode]['m']) ~= nil and 'm' or ''
	for _, case in ipairs(cases) do
		local code = case[1]
		local name = case[2]
		output = output .. '|-\n'
		output = output .. '| style="background-color:#eff7ff; font-style:italic;" | ' .. name ..'\n'
		output = output .. '| ' .. joinSuffix(stem, pattern[code .. 'p' .. n .. m] or pattern[code .. 'p' .. n] or pattern[code .. 'p' .. m] or pattern[code .. 'p'], sc, scCode) .. '\n'
	end
	output = output .. '|}</div></div>'
	return output
end

function make_full_table(head, pattern, g, sc, scCode)
	local output = make_header('60', g, pattern.decl, head, 'sdp', sc)
	local stem = mw.ustring.sub(head, 1, -1 - (pattern.stem or 0))
	local n = mw.ustring.match(stem, sandhi[scCode]['n']) ~= nil and 'n' or ''
	local m = mw.ustring.match(head, sandhi[scCode]['m']) ~= nil and 'm' or ''
	for _, case in ipairs(cases) do
		local code = case[1]
		local name = case[2]
		output = output .. '|-\n'
		output = output .. '| style="background-color:#eff7ff; font-style:italic;" | ' .. name ..'\n'
		output = output .. '| ' .. joinSuffix(stem, pattern[code .. 's' .. n .. m] or pattern[code .. 's' .. n] or pattern[code .. 's' .. m] or pattern[code .. 's'], sc, scCode) .. '\n'
		output = output .. '| ' .. joinSuffix(stem, pattern[code .. 'd' .. n .. m] or pattern[code .. 'd' .. n] or pattern[code .. 'd' .. m] or pattern[code .. 'd'], sc, scCode) .. '\n'
		output = output .. '| ' .. joinSuffix(stem, pattern[code .. 'p' .. n .. m] or pattern[code .. 'p' .. n] or pattern[code .. 'p' .. m] or pattern[code .. 'p'], sc, scCode) .. '\n'
	end
	output = output .. '|}</div></div>'
	return output
end

function export.show(frame)
	local g = frame.args[1]
	local params = {
		head = {default = PAGENAME},
		decl = {default = nil},
		n = {default = 'sdp'},
		[1] = {alias_of = 'head'},
		[2] = {alias_of = 'decl'},
		[3] = {alias_of = 'n'},
	}
	local args = m_para.process(frame:getParent().args, params)
	
	local sc = m_scripts.findBestScript(args.head, lang)
	local scCode = sc:getCode()
	if scCode == 'None' then
		error('No script was detected for headword ' .. args.head .. '".')
	end

	if args.decl == nil then
		for pattern, declension in pairs(endings[scCode]) do
			if mw.ustring.match(args.head, pattern) ~= nil then
				args.decl = declension
				break
			end
		end
	end
	if args.decl == nil then
		error("No declension class could be detected. Please check the lemma form or specify the declension.")
	end

	local m_pattern = require('Module:User:JohnC5/Sandbox/' .. scCode)
	local pattern = m_pattern[args.decl][g]
	
	local output = ''
	if args.n == 'sdp' then
		output = make_full_table(args.head, pattern, g, sc, scCode)
	elseif args.n == 's' then
		output = make_sing_table(args.head, pattern, g, sc, scCode)
	elseif args.n == 'p' then
		output = make_plur_table(args.head, pattern, g, sc, scCode)
	else
		error('The number parameter ("n" or the 3rd position) must left empty or specified as "sdp" for singular-dual-plural, "s" for singular, or "p" for plural')
	end
	
	return output
end

return export