Module:User:Benwing2/be-new

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 export = {}

local rsplit = mw.text.split
local rfind = mw.ustring.find
local rmatch = mw.ustring.match
local rsubn = mw.ustring.gsub

-- version of rsubn() that discards all but the first return value
local function rsub(term, foo, bar)
	local retval = rsubn(term, foo, bar)
	return retval
end

--[=[

Example of use:

{{subst:be-nazdecl|НВ вяхі́р, вехіра́, вехіру́, вехіро́м, вехіры́; мн. НВ вехіры́, вехіро́ў, вехіра́м, вехіра́мі, вехіра́х}}

]=]
function export.nazdecl(frame)
	local params = {
		[1] = {required = true}
	}
	local parargs = frame:getParent().args
	local args = require("Module:parameters").process(parargs, params)
	local forms = args[1]
	local function process_sg_or_pl(forms_to_parse)
		forms_to_parse = rsplit(forms_to_parse, " *, *")
		local slots = {
			["Н"] = 1,
			["Р"] = 2,
			["Д"] = 3,
			["В"] = 4,
			["Т"] = 5,
			["М"] = 6,
		}
		local forms = {}
		for _, form in ipairs(forms_to_parse) do
			if rfind(form, "^[НВРДТМ]+ ") then
				local cases, caseform = rmatch(form, "^([НВРДТМ]+) (.*)$")
				for _, case in ipairs(rsplit(cases, "")) do
					assert(not forms[slots[case]])
					forms[slots[case]] = caseform
				end
			else
				for i=1,6 do
					if not forms[i] then
						forms[i] = form
						break
					end
				end
			end
		end
		return forms
	end
	if rfind(forms, ";") then
		local sg_and_pl = rsplit(forms, " *; *")
		if #sg_and_pl ~= 2 then
			error("Saw too many semicolons, expected only one: " .. forms)
		end
		local sg, pl = unpack(sg_and_pl)
		pl = rsub(pl, "^мн%. *", "")
		sg = process_sg_or_pl(sg)
		pl = process_sg_or_pl(pl)
		local parts = {}
		table.insert(parts, "{{be-decl-noun\n")
		for i=1,6 do
			table.insert(parts, "|" .. sg[i] .. "|" .. pl[i] .. "\n")
		end
		table.insert(parts, "}}")
		return table.concat(parts)
	else
		local sg = process_sg_or_pl(forms)
		return "{{be-decl-noun-unc|" .. table.concat(sg, "|") .. "}}"
	end
end

--[=[
		НВ вяхі́р, вехіра́, вехіру́, вехіро́м, вехіры́; мн. НВ вехіры́, вехіро́ў, вехіра́м, вехіра́мі, вехіра́х
		НВ бяскрайнасць, РДМ бяскрайнасці, бяскрайнасцю
]=]

return export