local insert = table.insert
local u = require("Module:string utilities").char
local lang = require("Module:languages").getByCode("su")
local export = {}
local pos_functions = {}
-----------------------
-- Utility functions --
-----------------------
-- If Not Empty
local function ine(arg)
if arg == "" then
return nil
else
return arg
end
end
local function list_to_set(list)
local set = {}
for _, item in ipairs(list) do set = true end
return set
end
-- version of mw.ustring.gsub() that discards all but the first return value
function rsub(term, foo, bar)
local retval = mw.ustring.gsub(term, foo, bar)
return retval
end
local function make_unused_key_tracker(t)
local unused_keys = require "Module:table".listToSet(require "Module:table".keysToList(t))
local mt = {
__index = function(self, key)
if key ~= nil then
unused_keys = nil
end
return t
end,
__newindex = function(self, key, value)
t = value
end
}
local proxy_table = setmetatable({}, mt)
return proxy_table, unused_keys
end
local rfind = mw.ustring.find
local function append_cat(data, pos)
insert(data.categories, lang:getCanonicalName() .. " " .. pos)
end
function remove_links(text)
text = rsub(text, "%]*|", "")
text = rsub(text, "%[%[", "")
text = rsub(text, "%]%]", "")
return text
end
local function otherscript(inflections, args)
local title = mw.title.getCurrentTitle()
local sc = lang:findBestScript(title.subpageText)
local other_sc
if sc:getCode() == "Latn" then
local inflection = {label = "Sundanese script"}
if not tr then
tr = require("Module:su-Latn-Sund-translit").tr(
require("Module:links").remove_links(
mw.title.getCurrentTitle().text), "mak", sc:getCode())
end
insert(inflection, {term = tr})
insert(inflections, inflection)
end
end
-- The main entry point.
function export.show(frame)
local PAGENAME = mw.title.getCurrentTitle().text
local poscat = frame.args or error(
"Part of speech has not been specified. Please pass parameter 1 to the module invocation.")
local params = {
= {list = "head", allow_holes = true, default = ""},
= {default = ""}
}
local args, unused_keys = make_unused_key_tracker(frame:getParent().args)
-- Gather parameters
local data = {
lang = lang,
pos_category = poscat,
categories = {},
heads = {},
translits = {},
inflections = {},
altform = lang:findBestScript(PAGENAME):getCode() ~= "Latn"
}
otherscript(data.inflections, args)
if pos_functions then pos_functions.func(args, data) end
local unused_key_list = require"Module:table".keysToList(unused_keys)
if #unused_key_list > 0 then
local unused_key_string = require "Module:array"(unused_key_list):map(
function(key)
return "|" .. key .. "=" .. args
end):concat("\n")
error("Unused arguments: " .. unused_key_string)
end
return require("Module:headword").full_headword(data)
end
local function getargs(args, argpref, position)
-- Gather parameters
local forms = {}
if ine(args) then
form = ine(args)
else
form = ine(args)
end
local i = 1
while form do
insert(forms, {term = form})
i = i + 1
form = ine(args)
end
return forms
end
local function handle_infl(args, data, argpref, label, position)
local newinfls = getargs(args, argpref, position)
newinfls.label = label
if #newinfls > 0 then insert(data.inflections, newinfls) end
end
pos_functions = {
params = {
= { list = true },
active = { list = true },
passive = { list = true },
},
func = function(args, data)
local base = data.pagename
local generate_passive = false
if args and args == "+" then
-- Default generation for both
local active_form = "nga" .. base
local passive_form = "di" .. base
-- Count vowels
local vowel_count = 0
for _ in mw.ustring.gmatch(base, "") do
vowel_count = vowel_count + 1
if vowel_count > 1 then break end
end
if vowel_count == 1 then
active_form = "nge" .. base
else
local first_two = mw.ustring.sub(base, 1, 2)
local first = mw.ustring.sub(base, 1, 1)
if first_two == "sy" then
active_form = "nga-" .. base
elseif first_two == "pl" then
active_form = "nga" .. base
elseif first_two == "pr" then
active_form = "nga" .. base
elseif first_two == "tr" then
active_form = "nga" .. base
elseif first_two == "kl" then
active_form = "nga" .. base
elseif first_two == "kr" then
active_form = "nga" .. base
elseif first_two == "kh" then
active_form = "nga" .. base
elseif first == "k" then
active_form = "ng" .. mw.ustring.sub(base, 2)
elseif first == "t" then
active_form = "n" .. mw.ustring.sub(base, 2)
elseif first == "s" then
active_form = "ny" .. mw.ustring.sub(base, 2)
elseif first == "c" then
active_form = "ny" .. mw.ustring.sub(base, 2)
elseif first == "p" then
active_form = "m" .. mw.ustring.sub(base, 2)
elseif first:match("") then
active_form = "ng" .. base
end
end
args.active = { active_form }
args.passive = { passive_form }
else
-- If only active is specified, still generate passive
if args.active and args.active and (not args.passive or not args.passive) then
args.passive = { "di" .. base }
end
end
-- Emit inflections
if args.active and args.active then
args.active.label = "active"
table.insert(data.inflections, args.active)
end
if args.passive and args.passive then
args.passive.label = "passive"
table.insert(data.inflections, args.passive)
end
end,
}
pos_functions = {
func = function(args, data)
data.pos_category = "nouns"
handle_infl(args, data, "def", "definite", 1)
end
}
return export