Module:User:Benwing2/cy-mut

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

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


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

local export = {}

local rfind = mw.ustring.find
local usub = mw.ustring.sub
local ulower = mw.ustring.lower
local uupper = mw.ustring.upper

PAGENAME = PAGENAME or mw.title.getCurrentTitle().text

local mutation_rules = {
	['b'] = {"f", "m",},
	['c'] = {"g", "ngh", "ch",},
	['ch'] = {},
	['d'] = {"dd", "n",},
	['f'] = {},
	['g'] = {"", "ng"},
	['h'] = {},
	['l'] = {},
	['ll'] = {"l",},
	['m'] = {"f",},
	['n'] = {},
	['p'] = {"b", "mh", "ph",},
	['r'] = {},
	['rh'] = {"r",},
	['s'] = {},
	['t'] = {"d", "nh", "th",},
}

function export.get_mutation_data(term)
	local data = {}
	data.radical = term
	data.initial = usub(term, 1, 1)
	data.is_uppercase = ulower(data.initial) ~= data.initial
	data.initial = ulower(data.initial)
	data.final = usub(term, 2, -1)
	
	data.first_two = data.initial .. usub(data.final, 1, 1)
	if data.first_two == 'll' or data.first_two == 'rh' or data.first_two == 'ch' then
		data.initial = data.first_two
		data.final = usub(data.final, 2, -1)
	end
	data.vowel = false
	
	data.mut1 = nil
	data.mut2 = nil
	data.mut3 = nil
	
	if rfind(data.initial, "[aâeêiîoôuûwŵyŷ]") then
		data.vowel = true
		data.mut3 = "h" .. data.initial
	elseif mutation_rules[data.initial] then
		data.mut1 = mutation_rules[data.initial][1]
		data.mut2 = mutation_rules[data.initial][2]
		data.mut3 = mutation_rules[data.initial][3]
	else
		error("rule not specified for this word:" .. term)
	end

	return data
end

function export.show(frame)
	local args = frame:getParent().args
	local data = export.get_mutation_data(args[1] or PAGENAME)
	local function link(target, accel)
		target = target .. data.final
		if data.is_uppercase then
			target = uupper(usub(target, 1, 1)) .. usub(target, 2, -1)
		end
		return require("Module:links").full_link({lang = lang, accel = {form = accel, lemma = data.radical}, term = target})
	end
	
	data.mut1 = data.mut1 and link(data.mut1, 'soft')
	data.mut2 = data.mut2 and link(data.mut2, 'nasal')
	data.mut3 = data.mut3 and link(data.mut3, data.vowel and 'h-prothesis' or 'aspirate')

	result = '{| border="1" cellpadding="4" cellspacing="0" class="inflection-table" style="align: left; margin: 0.5em 0 0 0; border-style: solid; border: 1px solid #7f7f7f; border-right-width: 2px; border-bottom-width: 2px; border-collapse: collapse; background-color: #F8F8F8; font-size: 95%;"'
	result = result .. '\n|-'
	result = result .. '\n! colspan=4 | [[Appendix:Welsh mutations|Welsh mutation]]'
	result = result .. '\n|-'
	result = result .. '\n! [[radical]] !! [[soft mutation|soft]] !! [[nasal mutation|nasal]] !! ' .. (data.vowel and '[[h-prothesis]]' or '[[aspirate mutation|aspirate]]')
	result = result .. '\n|-'
	result = result .. '\n| ' .. require("Module:links").full_link({lang = lang, term = data.radical})
	result = result .. '\n| ' .. (data.mut1 or "''unchanged''")
	result = result .. '\n| ' .. (data.mut2 or "''unchanged''")
	result = result .. '\n| ' .. (data.mut3 or "''unchanged''")
	if data.mut1 or data.mut2 or data.mut3 then
		result = result .. '\n|-'
		result = result .. "\n| colspan=4 | <small style=\"font-size:85%;\">''Note:'' Some of these forms may be hypothetical. Not every<br />possible mutated form of every word actually occurs.</small>"
	end
	result = result .. '\n|}'
	return result
end

return export