跳转到内容

模組:Find country

被永久保护的模块
维基百科,自由的百科全书

这是本页的一个历史版本,由HanTsî留言 | 贡献2022年5月3日 (二) 12:32编辑。这可能和当前版本存在着巨大的差异。

--[[ v1.00
     Test the string against the list of countries/continents.
     Return the first word which matches a country/continent name ...
     unless the "match=" parameter specifies a different match.
     If there is no match, then return an empty string ... unless
     the "nomatch" parameter specifies something different
]]

local getArgs = require('Module:Arguments').getArgs
local p = {}

-- config
local nomatch = ""
local matchnum = 1

local countryList = {
	'南非',
	'中非共和國',
	'中非共和国',
	'阿富汗',
	'阿爾巴尼亞',
	'阿尔巴尼亚',
	'阿爾及利亞',
	'阿尔及利亚',
	'美屬薩摩亞',
	'美属萨摩亚',
	'安道爾',
	'安道尔',
	'安哥拉',
	'安吉拉',
	'安圭拉',
	'安地卡及巴布達',
	'安提瓜和巴布達',
	'安提瓜和巴布达',
	'阿根廷',
	'亞美尼亞',
	'亚美尼亚',
	'阿魯巴',
	'阿鲁巴',
	'澳洲',
	'澳大利亞',
	'澳大利亚',
	'奧地利',
	'奥地利',
	'亞塞拜然',
	'阿塞拜疆',
	'巴哈馬',
	'巴哈马',
	'巴林',
	'孟加拉',
	'孟加拉国',
	'巴貝多',
	'巴巴多斯',
	'白俄羅斯',
	'白俄罗斯',
	'比利時',
	'比利时',
	'貝里斯',
	'伯利兹',
	'貝南',
	'貝寧',
	'贝宁',
	'百慕達',
	'百慕大',
	'不丹',
	'玻利維亞',
	'玻利维亚',
	'波士尼亞與赫塞哥維納',
	'波斯尼亚和黑塞哥维那',
	'波斯尼亞和黑塞哥維那',
	'波札那',
	'博茨瓦納',
	'博茨瓦纳',
	'巴西',
	'汶萊',
	'汶莱',
	'保加利亞',
	'保加利亚',
	'布吉納法索',
	'布基納法索',
	'布基纳法索',
	'蒲隆地',
	'布隆迪',
	'柬埔寨',
	'喀麥隆',
	'喀麦隆',
	'喀麥隆',
	'加拿大',
	'維德角',
	'佛得角',
	'開曼群島',
	'开曼群岛',
	'查德',
	'乍得',
	'智利',
	"中國",
	"中国",
	'中國大陸',
	'中国大陆',
	'中華人民共和國',
	'中华人民共和国',
	'哥倫比亞',
	'哥伦比亚',
	'葛摩',
	'科摩羅',
	'科摩罗',
	'剛果共和國',
	'刚果共和国',
	'剛果民主共和國',
	'刚果民主共和国',
	'剛果民主共和國',
	'剛果',
	'刚果',
	'庫克群島',
	'库克群岛',
	'哥斯大黎加',
	'哥斯達黎加',
	'哥斯达黎加',
	'克羅埃西亞',
	'克羅地亞',
	'克罗地亚',
	'古巴',
	'古拉索',
	'庫拉索',
	'库拉索',
	'賽普勒斯',
	'塞浦路斯',
	'捷克',
	'丹麥',
	'丹麦',
	'吉布地',
	'吉布提',
	'多明尼加',
	'多米尼加',
	'多米尼克',
	'東帝汶',
	'东帝汶',
	'厄瓜多',
	'厄瓜多爾',
	'厄瓜多尔',
	'埃及',
	'薩爾瓦多',
	'萨尔瓦多',
	'英格蘭',
	'英格兰',
	'厄利垂亞',
	'厄立特里亞',
	'厄立特里亚',
	'愛沙尼亞',
	'爱沙尼亚',
	'史瓦帝尼',
	'斯威士蘭',
	'斯威士兰',
	'衣索比亞',
	'埃塞俄比亞',
	'埃塞俄比亚',
	'福克蘭群島',
	'福克兰群岛',
	'法羅群島',
	'法罗群岛',
	'斐濟',
	'斐济',
	'芬蘭',
	'芬兰',
	'法國',
	'法国',
	'加彭',
	'加蓬',
	'甘比亞',
	'岡比亞',
	'冈比亚',
	'喬治亞',
	'格魯吉亞',
	'格鲁吉亚',
	'德國',
	'德国',
	'迦納',
	'加納',
	'加纳',
	'直布羅陀',
	'直布罗陀',
	'大不列顛島',
	'大不列颠岛',
	'希臘',
	'希腊',
	'格瑞那達',
	'格林納達',
	'格林纳达',
	'關島',
	'关岛',
	'瓜地馬拉',
	'危地馬拉',
	'危地马拉',
	'巴布亞紐幾內亞',
	'巴布亞新畿內亞',
	'巴布亚新几内亚',
	'赤道幾內亞',
	'赤道几内亚',
	'幾內亞比索',
	'畿內亞比紹',
	'几内亚比绍',
	'幾內亞',
	'畿內亞',
	'几内亚',
	'蓋亞那',
	'圭亞那',
	'圭亚那',
	'海地',
	'宏都拉斯',
	'洪都拉斯',
	'宏都拉斯',
	'香港',
	'匈牙利',
	'冰島',
	'冰岛',
	'印度',
	'印尼',
	'印度尼西亞',
	'印度尼西亚',
	'伊朗',
	'伊拉克',
	'北愛爾蘭',
	'北爱尔兰',
	'愛爾蘭',
	'爱尔兰',
	'以色列',
	'義大利',
	'意大利',
	'象牙海岸',
	'科特迪瓦',
	"牙買加",
	'牙买加',
	'日本',
	'約旦',
	'约旦',
	'哈薩克',
	'哈萨克斯坦',
	'肯亞',
	'肯雅',
	'肯尼亚',
	'吉里巴斯',
	'基里巴斯',
	'科索沃',
	'科威特',
	'吉爾吉斯',
	'吉尔吉斯斯坦',
	"寮國",
	"寮人民民主共和國",
	'老撾',
	'老撾人民民主共和國',
	'老挝',
	'老挝人民民主共和国',
	'拉脫維亞',
	'拉脱维亚',
	'黎巴嫩',
	'賴索托',
	'萊索托',
	'莱索托',
	'賴比瑞亞',
	'利比里亞',
	'利比里亚',
	'利比亞',
	'利比亚',
	'列支敦斯登',
	'列支敦士登',
	'立陶宛',
	'盧森堡',
	'卢森堡',
	'澳門',
	'澳门',
	'北馬其頓',
	'北马其顿',
	'馬其頓',
	'马其顿',
	'馬達加斯加',
	'马达加斯加',
	'馬拉威',
	'馬拉維',
	'马拉维',
	'馬來西亞',
	'马来西亚',
	'馬爾地夫',
	'馬爾代夫',
	'马尔代夫',
	'馬利',
	'馬里',
	'马里',
	'馬爾他',
	'馬耳他',
	'马耳他',
	'馬紹爾群島',
	'马绍尔群岛',
	'茅利塔尼亞',
	'毛里塔尼亞',
	'毛里塔尼亚',
	'模里西斯',
	'毛里裘斯',
	'毛里求斯',
	'墨西哥',
	'the Federated States of Micronesia',
	'Federated States of Micronesia',
	'Micronesia',
	'FSM',
	'Moldova',
	'Monaco',
	'Mongolia',
	'Montenegro',
	'Montserrat',
	'Morocco',
	'Mozambique',
	'Myanmar',
	'Namibia',
	'Nauru',
	'Nepal',
	'荷蘭',
	'荷兰',
	'New Caledonia',
	'紐西蘭',
	'新西兰',
	'Nicaragua',
	'Nigeria',
	'Niger',
	'北韓',
	'北韩',
	"朝鮮民主主義人民共和國",
	"朝鲜民主主义人民共和国",
	'Norway',
	'Oman',
	'the Ottoman Empire',
	'Ottoman Empire',
	'Ottoman Egypt',	
	'Pakistan',
	'Palau',
	'Mandatory Palestine',
	'Palestine',
	'the Palestinian territories',
	'Palestinian territories',
	'Panama',
	'Paraguay',
	'Peru',
	'the Philippines',
	'Philippines',
	'Poland',
	'Portugal',
	'Puerto Rico',
	'the Spanish Virgin Islands',
	'Spanish Virgin Islands',
	'Qatar',
	'Romania',
	'the Russian Empire',
	'Russian Empire',
	'the Russian Federation',
	'Russian Federation',
	'Russia',
	'Rwanda',
	'Saint Kitts and Nevis',
	'Saint Lucia',
	'Saint Vincent and the Grenadines',
	'Western Samoa',
	'Samoa',
	'San Marino',
	'São Tomé and Príncipe',
	'São Tomé and Príncipe',
	'Sao Tome and Principe',
	'Saudi Arabia',
	'Scotland',
	'Senegal',
	'Serbia',
	'Seychelles',
	'Sierra Leone',
	'Singapore',
	'Slovakia',
	'Slovenia',
	'the Solomon Islands',
	'Solomon Islands',
	'Somalia',
	'大韓民國',
	'大韩民国',
	'韓國',
	'韩国',
	'南韓',
	'南韩',
	'South Sudan',
	'Sudan',
	'Spain',
	'Sri Lanka',
	'Suriname',
	'Swaziland',
	'Sweden',
	'Switzerland',
	'Syria',
	'Tahiti',
	'中華民國',
	'中华民国',
	'台灣',
	'臺灣',
	'台湾',
	'Tajikistan',
	'Tanzania',
	'Thailand',
	'Togo',
	'Tonga',
	'Trinidad and Tobago',
	'Trinidad',
	'Tobago',
	'Tunisia',
	'Turkey',
	'Turkmenistan',
	'Turks and Caicos Islands',
	'Tuvalu',
	'Uganda',
	'Ukraine',
	'the United Arab Emirates',
	'the UAE',
	'the U.A.E.',
	'United Arab Emirates',
	'UAE',
	'U.A.E.',
	'英國',
	'英国',
	'美國',
	'美国',
	'Uruguay',
	'Uzbekistan',
	'Vanuatu',
	'Venezuela',
	'Vietnam',
	'the British Virgin Islands',
	'British Virgin Islands',
	'UK Virgin Islands',
	'U.K. Virgin Islands',
	'the United States Virgin Islands',
	'the US Virgin Islands',
	'the U.S. Virgin Islands',
	'United States Virgin Islands',
	'US Virgin Islands',
	'U.S. Virgin Islands',
	'the Virgin Islands',
	'Virgin Islands',
	'Wales',
	'Yemen',
	'Zambia',
	'Zimbabwe',
	'Find country/testcases'
}

-- returns the name of a country that is found in the string
-- ... or an empty string if there is no match
function findcountryinstring(str)

	nMatches = 0
	myMatches ={}
	str=" " .. str:gsub("^%s*(.-)%s*$", "%1") .. " "

		-- check agaist each country
		-- if there is a match, then return that country
		for i, thiscountry in ipairs(countryList) do
			if mw.ustring.find(str, thiscountry) then
				nMatches = nMatches + 1
				myMatches[nMatches] = thiscountry
			end
		end


	if (nMatches == 0) then
		-- none of the title words matches a whole country
		return nomatch
	end
	
	if ((matchnum >= 1) and (matchnum <= nMatches)) then
		return myMatches[matchnum]
	end

	if (matchnum < 0) then
		matchnum = matchnum + 1 -- so that -1 is the last match etc
		if ((matchnum + nMatches) >= 1) then
			return myMatches[matchnum + nMatches]
		end
	end
	
	-- if we get here, we have not found a match at the position specified by "matchnum"
	return nomatch
end

function p.main(frame)
	local args = getArgs(frame)
	return p._main(args)
end

function p._main(args)
	if (args['nomatch'] ~= nil) then
		nomatch = args['nomatch']
	end
	
	-- by default, we return the first match
	-- but the optional "C" paarmeter sets the "matchnum" variable, which
	-- * for a positive matchnum "n", returns the nth match if it exists
	-- * for a positive matchnum "n", returns (if it exists) the nth match
	--   counting backwards from the end.
	--   So "match=-1" returns the last match
	--   and "match=-3" returns the 3rd-last match
	if (args['match'] ~= nil) then
		matchnum = tonumber(args['match'])
		if ((matchnum == nil) or (matchnum == 0)) then
			matchnum = 1
		end
	end
	
	-- by default, we use the current page
	-- but if the "string=" parameters is supplied, we use that
	-- so we try the parameter first
	thispagename = nil
	if ((args['string'] ~= nil) and (args['string'] ~= "")) then
		-- we have a non-empty "string" parameter, so we use it
		thisstring = args['string']
	else
		-- get the page title
		thispage = mw.title.getCurrentTitle()
		thisstring = thispage.text;
	end
	
	-- now check the pagename to try to find a country
	result = findcountryinstring(thisstring)
	if (result == "") then
		return nomatch
	end
	return result
end

return p