Module:Detect singular: Difference between revisions

ignore br elements immediately before </div> (per Frietjes)
m (1 revision imported from wikipedia:Module:Detect_singular)
(ignore br elements immediately before </div> (per Frietjes))
Line 14:
return count
end
 
local singular = 1
local likelyPlural = 2
local plural = 3
 
-- Determine whether a string is singular or plural (i.e., it represents one
Line 21 ⟶ 25:
-- 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)
-- Returns:
-- singular, likelyPlural, or plural (see constants above), or nil for completely unknown
function p._main(origArgs)
origArgs = type(origArgs) == 'table' and origArgs or {}
Line 41 ⟶ 47:
end
s = tostring(s)
if plainFind(s,'forcedetectsingulardata-plural="0"') then -- magic data string to return true
return truesingular
end
if plainFind(s,'forcedetectpluraldata-plural="1"') then -- magic data string to return false
return falseplural
end
-- count number of list items
Line 51 ⟶ 57:
-- if exactly one, then singular, if more than one, then plural
if numListItems == 1 then
return truesingular
end
if numListItems > 1 then
return falseplural
end
-- if "list of" occurs inside of wlink, then it's plural
if mw.ustring.find(s:lower(), '%[%[[^%]]*list of[^%]]+%]%]') then
return falseplural
end
-- fix for trailing br tags passed through [[template:marriage]]
s = mw.ustring.gsub(s, '<%s*br[^>]*>%s*(</div>)', '%1')
-- replace all wikilinks with fixed string
if rewriteLinks then
s = mw.ustring.gsub(s,'%b[]','WIKILINK')
end
-- Five conditions: any one of them can make the string a likely plural or plural
local hasBreak = mw.ustring.find(s,'<%s*br')
-- For the last 4, evaluate on string stripped of wikimarkup
s = getPlain(s)
local hasBullets = countMatches(s,'%*+') > 1
local multipleQids = mw.ustring.find(s,'Q%d+[%p%s]+Q%d+') -- has multiple QIDs in a row
if hasBullets or multipleQids then
return plural
end
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')
if hasBreak or hasComma or hasAnd then
local hasBullets = countMatches(s,'%*+') > 1
return likelyPlural
local multipleQids = mw.ustring.find(s,'Q%d+[%p%s]+Q%d+') -- has multiple QIDs in a row
end
-- return bool: is it singular?
return not (hasComma or hasAnd or hasBreak or hasBullets or multipleQids)
end
 
Line 81 ⟶ 94:
local singularForm = args[3] or args.singular or ""
local pluralForm = args[4] or args.plural or ""
local likelyForm = args.likely or pluralForm
local link = args[5] or args.link
if link then
Line 86 ⟶ 100:
singularForm = '[['..link..'|'..singularForm..']]'
pluralForm = '[['..link..'|'..pluralForm..']]'
likelyForm = '[['..link..'|'..likelyForm..']]'
end
if args[2] then
Line 94 ⟶ 109:
return "" -- return blank on complete failure
end
returnif detect and== singularFormsingular or pluralFormthen
return singularForm
elseif detect == likelyPlural then
return likelyForm
else
return pluralForm
end
end
 
Line 104 ⟶ 125:
return 1
end
return result == singular and 1 or ""
end
 
Anonymous user