Modul:Detect singular: Unterschied zwischen den Versionen

modify pluralize to accept "likely" parameter (for situations where plurality is likely, but not certain)
(implement named arguments: singular, plural, link)
(modify pluralize to accept "likely" parameter (for situations where plurality is likely, but not certain))
Zeile 14: Zeile 14:
return count
return count
end
end
local singular = 1
local likelyPlural = 2
local plural = 3


-- Determine whether a string is singular or plural (i.e., it represents one
-- Determine whether a string is singular or plural (i.e., it represents one
Zeile 21: Zeile 25:
--  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)
-- Returns:
--  singular, likelyPlural, or plural (see constants above), or nil for completely unknown
function p._main(origArgs)
function p._main(origArgs)
origArgs = type(origArgs) == 'table' and origArgs or {}
origArgs = type(origArgs) == 'table' and origArgs or {}
Zeile 42: Zeile 48:
s = tostring(s)
s = tostring(s)
if plainFind(s,'forcedetectsingular') then -- magic data string to return true
if plainFind(s,'forcedetectsingular') then -- magic data string to return true
return true
return singular
end
end
if plainFind(s,'forcedetectplural') then -- magic data string to return false
if plainFind(s,'forcedetectplural') then -- magic data string to return false
return false
return plural
end
end
-- count number of list items
-- count number of list items
Zeile 51: Zeile 57:
-- if exactly one, then singular, if more than one, then plural
-- if exactly one, then singular, if more than one, then plural
if numListItems == 1 then
if numListItems == 1 then
return true
return singular
end
end
if numListItems > 1 then
if numListItems > 1 then
return false
return plural
end
end
-- if "list of" occurs inside of wlink, then it's plural
-- if "list of" occurs inside of wlink, then it's plural
if mw.ustring.find(s:lower(), '%[%[[^%]]*list of[^%]]+%]%]') then
if mw.ustring.find(s:lower(), '%[%[[^%]]*list of[^%]]+%]%]') then
return false
return plural
end
end
-- fix for trailing br tags passed through [[template:marriage]]
-- Frietjes fix: should we use it? comment out for now
-- s = mw.ustring.gsub(s, '<%s*br[^>]*>%s*(</div>)', '%1')
-- replace all wikilinks with fixed string
-- replace all wikilinks with fixed string
if rewriteLinks then
if rewriteLinks then
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
-- Five conditions: any one of them can make the string a likely plural or plural
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
s = getPlain(s)
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 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')
local hasBullets = countMatches(s,'%*+') > 1
if hasBreak or hasComma or hasAnd then
local multipleQids = mw.ustring.find(s,'Q%d+[%p%s]+Q%d+') -- has multiple QIDs in a row
return likelyPlural
-- return bool: is it singular?
end
return not (hasComma or hasAnd or hasBreak or hasBullets or multipleQids)
return singular
end
end


Zeile 80: Zeile 94:
args = type(args) == 'table' and args or {}
args = type(args) == 'table' and args or {}
local singularForm = args[3] or args.singular or ""
local singularForm = args[3] or args.singular or ""
local pluralForm = args[4] or args.plural or ""
local pluralForm = args[4] or args.plural or args.likely or ""
local likelyForm = args.likely or pluralForm
local link = args[5] or args.link
local link = args[5] or args.link
if link then
if link then
Zeile 86: Zeile 101:
singularForm = '[['..link..'|'..singularForm..']]'
singularForm = '[['..link..'|'..singularForm..']]'
pluralForm = '[['..link..'|'..pluralForm..']]'
pluralForm = '[['..link..'|'..pluralForm..']]'
likelyForm = '[['..link..'|'..likelyForm..']]'
end
end
if args[2] then
if args[2] then
Zeile 94: Zeile 110:
return ""  -- return blank on complete failure
return ""  -- return blank on complete failure
end
end
return detect and singularForm or pluralForm
if detect == singular then
return singularForm
elseif detect == likelyPlural then
return likelyForm
else
return pluralForm
end
end
end


Zeile 104: Zeile 126:
return 1
return 1
end
end
return result and 1 or ""
return result == singular and 1 or ""
end
end


Anonymer Benutzer