Module:snon-mut: Difference between revisions

Melinoë (talk | contribs)
No edit summary
Undo revision 526485 by Sware (talk)
Tag: Undo
 
(45 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local export = {}
local gsub = mw.ustring.gsub
local match = mw.ustring.match
local gmatch = mw.ustring.gmatch
local sub = mw.ustring.sub
local upper = mw.ustring.upper


local yesno = require("Module:yesno")
local m_l = require("Module:links")


local toNFC = mw.ustring.toNFC
local lang = require("Module:languages").getByCode("snon")
local toNFD = mw.ustring.toNFD
local PAGENAME = mw.loadData("Module:headword/data").pagename
local ulower = mw.ustring.lower
local umatch = mw.ustring.match
local usub = mw.ustring.sub


local GRAVE = "\204\128"
local export = {}


local lang = require("Module:languages").getByCode("snon")
local words = mw.text.split("hello world hi", "%s")
local PAGENAME = mw.loadData("Module:headword/data").pagename
local MARKER = "<sup>△</sup>"


local mutation_rules = {
local first = table.remove(words, 1)
["b"] = {"bb", "m", "bh"},
local ords = words
["bh"] = {"b", "m", "gh"},
["c"] = {"g", "ngh", "ch"},
["ch"] = {"gh", "ngh"},
["d"] = {"dd", "n", "dh"},
["dh"] = {"d", "n", "gh"},
["g"] = {"gg", "ng", "gh"},
["h"] = {"gh", "nh", "ch"},
["l"] = {"lh"},
["m"] = {"mh"},
["n"] = {"nh"},
["p"] = {"b", "mh", "ph"},
["ph"] = {"bh", "mh", "h"},
["r"] = {"rr", "rh"},
["s"] = {"z", "sh"},
["t"] = {"d", "nh", "th"},
["th"] = {"dh", "nh", "ch"},
}


function export.get_mutation_data(term)
function export.mutations(ret, args)
local data = {radical = term}
local words = mw.text.split(ret.radical or args.word, "%s")
local lower_radical = mw.ustring.lower(words[1])
local first_letter = match(lower_radical, "^%W*(%w)")
local is_proper = ret.radical ~= lower_radical
local vowels = "([aeiouàèìòù])"
local apostrophe
local term_lower = term:gsub("^.[\128-\191]*", ulower) -- term with lowercase initial
words[1] = " "
data.is_uppercase = term_lower ~= term
words = table.concat(words, " ")
local normalized = toNFD(term_lower)
data.vowel = normalized:match("^[aeiouøy]") and true or false
if data.vowel then
if args.len then
data.final = term_lower
ret.lenition = args.len
data.mut1 = "gh-"
elseif match(first_letter, "[bcdfgmptsnv]") then
data.mut2 = "n-"
ret.lenition = ret.radical:gsub("^(.)", "%1h") .. words
data.mut3 = "ch-"
elseif match(first_letter, vowels) then -- h-prothesis
return data
ret.lenition = "h'" .. lower_radical .. words
apostrophe = true
else
ret.lenition = "-"
end
end
local initial, final, mut
local eclipsis_prefixes = {
for i = 3, 1, -1 do
["b"] = "m", ["c"] = "g", ["d"] = "n", ["f"] = "v", ["g"] = "n",
initial = usub(normalized, 1, i)
["p"] = "b", ["t"] = "d", ["h"] = "gh", ["s"] = "z", ["v"] = "b",
mut = mutation_rules[initial]
}
if mut then
final = usub(normalized, i + 1)
if args.ecl then
break
ret.eclipsis = args.ecl
elseif i == 1 then
elseif eclipsis_prefixes[first_letter] then
error(("no mutation rule found for %s"):format(term))
ret.eclipsis = eclipsis_prefixes[first_letter] .. sub(lower_radical, 2, -1) .. words
end
elseif match(first_letter, vowels) then
ret.eclipsis = "n'" .. lower_radical .. words
apostrophe = true
elseif match(lower_radical, "^ghi" .. vowels) then
ret.eclipsis = gsub(lower_radical, "^ghi" .. vowels, "ni%1") .. words
else
ret.eclipsis = "-"
end
end
if type(mut) == "table" then
if is_proper then
for k, v in pairs(mut) do
if apostrophe then
if tonumber(k) then
ret.eclipsis = "n" .. gsub(ret.eclipsis, "^n'(%w)", upper)
data["mut" .. k] = v
ret.lenition = "h" .. gsub(ret.lenition, "^h'(%w)", upper)
end
else
ret.eclipsis = gsub(ret.eclipsis, "^(%w)", upper)
ret.lenition = gsub(ret.lenition, "^(%w)", upper)
end
end
data.colloquial = mut.colloquial
end
end
data.final = toNFC(final)
return ret, apostrophe
return data
end
end


local function construct_mutation(data, accel_form, initial, override)
function export.mutation_table(frame)
local normal_mutation
if not initial then
normal_mutation = nil
else
normal_mutation = initial .. data.final
if data.is_uppercase then
normal_mutation = require("Module:string utilities").ucfirst(normal_mutation)
end
end
local has_override = override
if not yesno(override, true) or override == "-" then -- TODO: yesno to be removed.
override = nil
end
local has_irreg_mutation = has_override and override ~= normal_mutation
local mutation
-- don't combine the following into A and B or C because `override` may be nil
if has_override then
mutation = override
else
mutation = normal_mutation
end
local marker = has_irreg_mutation and MARKER or ""
if mutation then
return require("Module:Link").full_link({lang = lang, accel = {form = accel_form, lemma = data.radical}, term = mutation}) .. marker, true, has_irreg_mutation
else
return "''unchanged''" .. marker, false, has_irreg_mutation
end
end
 
function export.show(frame)
local params = {
local params = {
[1] = {},
[1] = {default = PAGENAME, template_default = "bèdh"},
["soft"] = {},
["ecl"] = {},
["nasal"] = {},
["len"] = {},
["aspirate"] = {},
["nocat"] = {type = "boolean"},
["temp_len"] = {type = "boolean"},
["temp_ecl"] = {type = "boolean"},
}
}
local parargs = frame:getParent().args
    local args = require("Module:parameters").process(parargs, params)
local title = args[1] or PAGENAME
local args = require("Module:parameters").process(frame:getParent().args, params)
local data = export.get_mutation_data(title)
 
local is_vowel
local soft, has_soft, has_irreg_soft = construct_mutation(data, "soft", data.mut1, args.soft)
local ret = {radical = args[1]}
local nasal, has_nasal, has_irreg_nasal  = construct_mutation(data, "nasal", data.mut2, args.nasal)
ret, is_vowel = export.mutations(ret, args)
local aspirate, has_aspirate, has_irreg_aspirate = construct_mutation(data, data.vowel and "h-prothesis" or "aspirate", data.mut3, args.aspirate)
 
if args.temp_len then
local result = frame:expandTemplate{ title = 'inflection-table-top',
return ret.lenition == "-" and ret.radical or ret.lenition
args = { title = '[[Appendix:Scots Norse mutations|Mutated forms]] of ' .. require("Module:links").full_link({lang = lang, alt = title}, 'term'), palette = 'green' } }
result = result .. '\n! [[radical]]'
result = result .. '\n! ' .. (data.vowel and 'gh-prothesis' or '[[soft mutation|voiced]]')
result = result .. '\n! ' .. (data.vowel and 'n-prothesis' or '[[nasal mutation|nasal]]')
result = result .. '\n! ' .. (data.vowel and 'ch-prothesis' or '[[aspirate mutation|aspirate]]')
result = result .. '\n|-'
result = result .. '\n| ' .. require("Module:links").full_link({lang = lang, term = data.radical})
result = result .. '\n| ' .. soft
result = result .. '\n| ' .. nasal
result = result .. '\n| ' .. aspirate
notes = ''
if has_irreg_soft or has_irreg_nasal or has_irreg_aspirate then
notes = notes .. '<p style="font-size:85%;">' .. MARKER .. 'Irregular.</p>'
end
end
if has_soft or has_nasal or has_aspirate then
notes = notes .. '<p style="font-size:85%;"><i>Note:</i> Certain mutated forms of some words can never occur in standard Scots Norse.<br>All possible mutated forms are displayed for convenience.</p>'
if args.temp_ecl then
return ret.eclipsis == "-" and ret.radical or ret.eclipsis
end
end
result = result .. '\n' .. frame:expandTemplate{ title = 'inflection-table-bottom', args = { notes = notes } }
if not args.nocat and (has_irreg_soft or has_irreg_nasal or has_irreg_aspirate) then
local links = {}
result = result .. require("Module:utilities").format_categories("Welsh terms with irregular mutation", lang)
for k, v in pairs(ret) do
if v == "-" then
links[k] = "''not applicable''"
else
links[k] = m_l.full_link{lang = lang, term = v}
end
end
end
return result
local table_args = {
palette = "blue",
notes = "<p style=font-size:85%;>''Note:'' Certain mutated forms of some words can never occur in standard Scots Norse.<br>All possible mutated forms are displayed for convenience.</p>",
title = "[[Scots Norse#Mutation|Mutated forms]] of " .. ret.radical,
-- class = "wide",
}
local contents = [[
|-
! radical !! ]] .. (is_vowel and "h-prothesis" or "lenition") .. [[ !! eclipsis
|-
| {radical} || {lenition} || {eclipsis}
]]
contents = require('Module:string utilities').format(contents, links)
return require("Module:inflection table").inflection_table(frame, table_args, contents)
end
end


return export
return export