Module:Detect singular: Difference between revisions

Created page with "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) end -- 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 end..."
 
No edit summary
 
Line 2: Line 2:
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
local yesNo = require('Module:Yesno')
local yesNo = require('Module:Yesno')
local getPlain = require('Module:Text').Text().getPlain


-- function to determine whether "sub" occurs in "s"
-- function to determine whether "sub" occurs in "s"
Line 25: Line 24:
--  origArgs.no_comma:  if false, use commas to detect plural (default false)
--  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)
--  origArgs.parse_links: if false, treat wikilinks as opaque singular objects (default false)
--  origArgs.any_comma: if true, allow any comma to cause likely plural, including ones between digits (default false)
--  origArgs.no_and: if false, use existence of "and" to detect plural (default false)
--  origArgs.parse_number: if true, parse number in argument: if 1, singular; if >1, plural (default false)
-- Returns:
-- Returns:
--  singular, likelyPlural, or plural (see constants above), or nil for completely unknown
--  singular, likelyPlural, or plural (see constants above), or nil for completely unknown
Line 84: Line 86:
local hasBreak = mw.ustring.find(s,'<%s*br')
local hasBreak = mw.ustring.find(s,'<%s*br')
-- For the last 4, evaluate on string stripped of wikimarkup
-- For the last 4, evaluate on string stripped of wikimarkup
    local getPlain = require('Module:Text').Text().getPlain
s = getPlain(s)
s = getPlain(s)
local hasBullets = countMatches(s,'%*+') > 1
local hasBullets = countMatches(s,'%*+') > 1
Line 92: Line 95:
local commaPattern = anyComma and '[,;]' or '%D[,;]%D'  -- semi-colon similar to comma
local commaPattern = anyComma and '[,;]' or '%D[,;]%D'  -- semi-colon similar to comma
local hasComma = checkComma and mw.ustring.find(s, commaPattern)
local hasComma = checkComma and mw.ustring.find(s, commaPattern)
local hasAnd = checkAnd and mw.ustring.find(s,'[,%s]and%s')
local hasAnd = checkAnd and (mw.ustring.find(s,'[,%s]and%s') or mw.ustring.find(s,'&',1,true))
if hasBreak or hasComma or hasAnd then
if hasBreak or hasComma or hasAnd then
return likelyPlural
return likelyPlural