Modul:Detect singular: Unterschied zwischen den Versionen

add comments
(flip argument meaning)
(add comments)
Zeile 3: Zeile 3:
local yesNo = require('Module:Yesno')
local yesNo = require('Module:Yesno')


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


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


-- 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)
function p._main(origArgs)
local args = {}
local args = {}
Zeile 48: Zeile 56:
s = mw.ustring.gsub(s,'%b[]','WIKILINK')  
s = mw.ustring.gsub(s,'%b[]','WIKILINK')  
end
end
-- Five conditions: any one of them can make the string a plural
local hasComma = checkComma and mw.ustring.find(s, '%D[,;]%D') -- semi-colon similar to comma
local hasComma = checkComma and mw.ustring.find(s, '%D[,;]%D') -- semi-colon similar to comma
local hasAnd = mw.ustring.find(s,'[,%s]and%s')
local hasAnd = mw.ustring.find(s,'[,%s]and%s')
Zeile 53: Zeile 62:
local hasBullets = countMatches(s,'%*+') > 1
local hasBullets = countMatches(s,'%*+') > 1
local multipleQids = mw.ustring.find(s,'Q%d+[%p%s]+Q%d+') -- has multiple QIDs in a row
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)
return not (hasComma or hasAnd or hasBreak or hasBullets or multipleQids)
end
end
Zeile 69: Zeile 79:
function p.main(frame)
function p.main(frame)
local args = getArgs(frame)
local args = getArgs(frame)
-- For template, return 1 if singular, blank if not
if p._main(args) then
if p._main(args) then
return 1
return 1
Anonymer Benutzer