Module:Detect singular
- Afrikaans
- العربية
- Авар
- تۆرکجه
- বাংলা
- भोजपुरी
- Bikol Central
- བོད་ཡིག
- Bosanski
- Буряад
- Cebuano
- Cymraeg
- فارسی
- ગુજરાતી
- हिन्दी
- Ilokano
- Bahasa Indonesia
- ქართული
- Ikinyarwanda
- मैथिली
- മലയാളം
- Bahasa Melayu
- ꯃꯤꯇꯩ ꯂꯣꯟ
- Mfantse
- Монгол
- नेपाली
- ଓଡ଼ିଆ
- Oʻzbekcha / ўзбекча
- ਪੰਜਾਬੀ
- Papiamentu
- Português
- Shqip
- සිංහල
- Simple English
- سنڌي
- کوردی
- Српски / srpski
- Srpskohrvatski / српскохрватски
- Tagalog
- தமிழ்
- တႆး
- Українська
- Tiếng Việt
- Руски
- ꠍꠤꠟꠐꠤ
Appearance
From Wikipedia, the free encyclopedia
Warning This Lua module is used on approximately 2,370,000 pages, or roughly 4% of all pages .
To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them.
To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them.
This module depends on the following other modules:
Lua to determine whether a string is singular or plural. Designed for use in infoboxes, to determine whether labels should be singular or plural based on the corresponding data, e.g.
{{#invoke:Detect singular|pluralize|Wynken, Blynken, and Nod||singular|plural}}
→ plural
- See Template:Detect singular for main() usage. Test cases here.
- See Template:Pluralize from text for pluralize() usage (a wrapper around main()). Test cases here.
The above documentation is transcluded from Module:Detect singular/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.
localp={} localgetArgs=require('Module:Arguments').getArgs localyesNo=require('Module:Yesno') -- function to determine whether "sub" occurs in "s" localfunctionplainFind(s,sub) returnmw.ustring.find(s,sub,1,true) end -- function to count the number of times "pattern" (a regex) occurs in "s" localfunctioncountMatches(s,pattern) local_,count=mw.ustring.gsub(s,pattern,'') returncount end localsingular=1 locallikelyPlural=2 localplural=3 -- 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) -- 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: -- singular, likelyPlural, or plural (see constants above), or nil for completely unknown functionp._main(origArgs) origArgs=type(origArgs)=='table'andorigArgsor{} localargs={} -- canonicalize boolean arguments forkey,defaultinpairs({no_comma=false,parse_links=false,any_comma=false, no_and=false,parse_number=false})do iforigArgs[key]==nilthen args[key]=default else args[key]=yesNo(origArgs[key],default) end end localcheckComma=notargs.no_comma localcheckAnd=notargs.no_and localrewriteLinks=notargs.parse_links localanyComma=args.any_comma localparseNumber=args.parse_number locals=origArgs[1]-- the input string ifnotsthen returnnil-- empty input returns nil end s=tostring(s) s=mw.text.decode(s,true)--- replace HTML entities (to avoid spurious semicolons) ifplainFind(s,'data-plural="0"')then-- magic data string to return true returnsingular end ifplainFind(s,'data-plural="1"')then-- magic data string to return false returnplural end -- count number of list items localnumListItems=countMatches(s,'<%s*li') -- if exactly one, then singular, if more than one, then plural ifnumListItems==1then returnsingular end ifnumListItems>1then returnplural end -- if "list of" occurs inside of wlink, then it's plural ifmw.ustring.find(s:lower(),'%[%[[^%]]*list of[^%]]+%]%]')then returnplural end ifparseNumberthen localm=tonumber(mw.ustring.match(s,"^%s*(%d+)")) ifmthen returnm>1andpluralorsingular end 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 ifrewriteLinksthen s=mw.ustring.gsub(s,'%b[]','WIKILINK') end -- Five conditions: any one of them can make the string a likely plural or plural localhasBreak=mw.ustring.find(s,'<%s*br') -- For the last 4, evaluate on string stripped of wikimarkup localgetPlain=require('Module:Text').Text().getPlain s=getPlain(s) localhasBullets=countMatches(s,'%*+')>1 localmultipleQids=mw.ustring.find(s,'Q%d+[%p%s]+Q%d+')-- has multiple QIDs in a row ifhasBulletsormultipleQidsthen returnplural end localcommaPattern=anyCommaand'[,;]'or'%D[,;]%D'-- semi-colon similar to comma localhasComma=checkCommaandmw.ustring.find(s,commaPattern) localhasAnd=checkAndandmw.ustring.find(s,'[,%s]and%s') ifhasBreakorhasCommaorhasAndthen returnlikelyPlural end returnsingular end functionp._pluralize(args) args=type(args)=='table'andargsor{} localsingularForm=args[3]orargs.singularor"" localpluralForm=args[4]orargs.pluralor"" locallikelyForm=args.likelyorpluralForm locallink=args[5]orargs.link iflinkthen link=tostring(link) singularForm='[['..link..'|'..singularForm..']]' pluralForm='[['..link..'|'..pluralForm..']]' likelyForm='[['..link..'|'..likelyForm..']]' end ifargs[2]then returnpluralForm end localdetect=p._main(args) ifdetect==nilthen return""-- return blank on complete failure end ifdetect==singularthen returnsingularForm elseifdetect==likelyPluralthen returnlikelyForm else returnpluralForm end end functionp.main(frame) localargs=getArgs(frame) -- For template, return 1 if singular, blank if plural or empty localresult=p._main(args) ifresult==nilthen return1 end returnresult==singularand1or"" end functionp.pluralize(frame) localargs=getArgs(frame) returnp._pluralize(args) end returnp