Module:Detect singular

From The Lands Of Liberos Project

Documentation for this module may be created at Module:Detect singular/doc

local p = {}
local getArgs = require('Module:Arguments').getArgs
local yesNo = require('Module:Yesno')
local getPlain = require('Module:Text').Text().getPlain

-- function to determine whether "sub" occurs in "s"
local function plainFind(s, sub)
	return mw.ustring.find(s, sub, 1, true)

-- function to count the number of times "pattern" (a regex) occurs in "s"
local function countMatches(s, pattern)
	local _, count = mw.ustring.gsub(s, pattern, '')
	return count

-- Determine whether a string is singular or plural (i.e., it represents one
-- item or many)
-- Arguments:
--   origArgs[1]: string to process
--   origArgs.no_comma:  if false, use commas to detect plural (default false)
--   origArgs.parse_links: if false, treat wikilinks as opaque singular objects (default false)
function p._main(origArgs)
	origArgs = type(origArgs) == 'table' and origArgs or {}
	local args = {}
	-- canonicalize boolean arguments
	for key, default in pairs({no_comma=false,parse_links=false,any_comma=false,no_and=false}) do
		if origArgs[key] == nil then
			args[key] = default
			args[key] = yesNo(origArgs[key],default)
	local checkComma = not args.no_comma
	local checkAnd = not args.no_and
	local rewriteLinks = not args.parse_links
	local anyComma = args.any_comma
	local s = origArgs[1]  -- the input string
	if not s then
		return nil -- empty input returns nil
	s = tostring(s)
	if plainFind(s,'forcedetectsingular') then -- magic data string to return true
		return true
	if plainFind(s,'forcedetectplural') then -- magic data string to return false
		return false
	-- count number of list items
	local numListItems = countMatches(s,'<%s*li')
	-- if exactly one, then singular, if more than one, then plural
	if numListItems == 1 then
		return true
	if numListItems > 1 then
		return false
	-- if "list of" occurs inside of wlink, then it's plural
	if mw.ustring.find(s:lower(), '%[%[[^%]]*list of[^%]]+%]%]') then
		return false
	-- replace all wikilinks with fixed string
	if rewriteLinks then
		s = mw.ustring.gsub(s,'%b[]','WIKILINK') 
	-- Five conditions: any one of them can make the string a plural
	local hasBreak = mw.ustring.find(s,'<%s*br')
	-- For the last 4, evaluate on string stripped of wikimarkup
	s = getPlain(s)
	local commaPattern = anyComma and '[,;]' or '%D[,;]%D'  -- semi-colon similar to comma
	local hasComma = checkComma and mw.ustring.find(s, commaPattern)
	local hasAnd = checkAnd and mw.ustring.find(s,'[,%s]and%s')
	local hasBullets = countMatches(s,'%*+') > 1
	local multipleQids = mw.ustring.find(s,'Q%d+[%p%s]+Q%d+') -- has multiple QIDs in a row
	-- return bool: is it singular?
	return not (hasComma or hasAnd or hasBreak or hasBullets or multipleQids)

function p._pluralize(args)
	args = type(args) == 'table' and args or {}
	local singularForm = args[3] or args.singular or ""
	local pluralForm = args[4] or args.plural or ""
	local link = args[5] or
	if link then
		link = tostring(link)
		singularForm = '[[''|'..singularForm..']]'
		pluralForm = '[[''|'..pluralForm..']]'
	if args[2] then
		return pluralForm
	local detect = p._main(args)
	if detect == nil then
		return ""   -- return blank on complete failure
	return detect and singularForm or pluralForm

function p.main(frame)
	local args = getArgs(frame)
	-- For template, return 1 if singular, blank if plural or empty
	local result = p._main(args)
	if result == nil then
		return 1
	return result and 1 or ""

function p.pluralize(frame)
	local args = getArgs(frame)
	return p._pluralize(args)

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.