Module:taln-headword: Difference between revisions

No edit summary
No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 11: Line 11:


local insert = table.insert
local insert = table.insert
local ipairs = ipairs
local list_param = { list = true, disallow_holes = true }
local bool_param = { type = "boolean" }
local function nonempty_list(list)
local ret = {}
if not list then
return ret
end
for _, v in ipairs(list) do
if v and v ~= "" then
insert(ret, v)
end
end
return ret
end
local function parse_term_list(forms, paramname)
return m_headword_utilities.parse_term_list_with_modifiers{
forms = forms,
paramname = paramname,
splitchar = ",",
}
end
local function insert_inflection(data, terms, label, accel)
m_headword_utilities.insert_inflection{
headdata = data,
terms = terms,
label = label,
accel = accel and { form = accel } or nil,
}
end
local function ensure_heads(data, args)
local heads = nonempty_list(args.head)
local trs = args.tr or {}
if not heads[1] then
local default_head = data.pagename
if not args.nolinkhead then
default_head = m_headword_utilities.add_links_to_multiword_term(default_head, {})
end
heads = { default_head }
end
for i, head in ipairs(heads) do
insert(data.heads, {
term = head,
tr = trs[i],
})
end
end


-- The main entry point.
function export.show(frame)
function export.show(frame)
local iparams = {
local iparams = {
[1] = {required = true},
[1] = { required = true },
}
}
local iargs = require("Module:parameters").process(frame.args, iparams)
local iargs = require("Module:parameters").process(frame.args, iparams)
local args = frame:getParent().args
local poscat = iargs[1]
local poscat = iargs[1]


local params = {
local params = {
["head"] = { list = true, disallow_holes = true },
["head"] = list_param,
["tr"] = { list = true, allow_holes = true },
["tr"] = { list = true, allow_holes = true },
["id"] = {},
["id"] = {},
["nolinkhead"] = { type = "boolean" },
["sort"] = {},
["nolinkhead"] = bool_param,
["pagename"] = {}, -- for testing
["pagename"] = {}, -- for testing
}
}
 
if pos_functions[poscat] then
if pos_functions[poscat] then
local posparams = pos_functions[poscat].params
local posparams = pos_functions[poscat].params
Line 39: Line 92:
end
end


    args = require("Module:parameters").process(args, params)
local args = require("Module:parameters").process(frame:getParent().args, params)
 
local pagename = args.pagename or mw.loadData("Module:headword/data").pagename
local pagename = args.pagename or mw.loadData("Module:headword/data").pagename


Line 56: Line 108:
}
}


local heads = args.head
ensure_heads(data, args)
for i = 1, #heads do
 
insert(data.heads, {
if pos_functions[poscat] and pos_functions[poscat].func then
term = heads[i],
pos_functions[poscat].func(args, data)
tr = args.tr[i],
})
end
end
 
return require("Module:headword").full_headword(data)
return require("Module:headword").full_headword(data)
end
end
Line 69: Line 119:
pos_functions.nouns = {
pos_functions.nouns = {
params = {
params = {
["g"] = {required = true, type = "genders"},
["g"] = { required = true, type = "genders", list = true, disallow_holes = true, flatten = true },
["nopl"] = {type = "boolean"},
["nopl"] = bool_param,
["pl"] = {list = true, allow_holes = false},
["pl"] = list_param,
},
},
func = function(args, data)
func = function(args, data)
local function insert_inflection(terms, label, accel)
local genders = nonempty_list(args.g)
m_headword_utilities.insert_inflection {
if not genders[1] then
headdata = data,
error("Parameter g= (gender) is required and must be non-empty")
terms = terms,
label = label,
accel = accel and {form = accel} or nil,
}
end
end
data.genders = genders
if args.nopl or (pl and pl[1] == "-") then
 
local plurals = parse_term_list(args.pl, "pl")
 
if args.nopl or (plurals[1] and plurals[1].term == "-") then
insert(data.categories, langname .. " uncountable nouns")
insert(data.categories, langname .. " uncountable nouns")
elseif pl then
elseif plurals[1] then
insert_inflection(pl, "plural", "p")
insert_inflection(data, plurals, "plural", "p")
else
insert(data.categories, langname .. " countable nouns")
end
end
data.g = args.g
end,
end,
}
}
Line 95: Line 144:
pos_functions.verbs = {
pos_functions.verbs = {
params = {
params = {
["pres"] = {list = true, allow_holes = false},
["pres"] = list_param,
["past"] = {list = true, allow_holes = false},
["past"] = list_param,
["part"] = {list = true, allow_holes = false},
["part"] = list_param,
},
},
func = function(args, data)
func = function(args, data)
local function insert_inflection(terms, label, accel)
local pres = parse_term_list(args.pres, "pres")
m_headword_utilities.insert_inflection {
local past = parse_term_list(args.past, "past")
headdata = data,
local part = parse_term_list(args.part, "part")
terms = terms,
 
label = label,
if pres[1] then
accel = accel and {form = accel} or nil,
insert_inflection(data, pres, "first-person singular present", "1|s|pres|ind")
}
end
if pres then
insert_inflection(pres, "first-person singular present", "1|s|pres|ind")
end
end
if past[1] then
if past then
insert_inflection(data, past, "first-person singular past", "1|s|past|ind")
insert_inflection(past, "first-person singular past", "1|s|past|ind")
end
end
if part[1] then
if part then
insert_inflection(data, part, "past participle", "pastpart")
insert_inflection(part, "past participle", "pastpart")
end
end
end,
end,