Modul:Find country

Daripada Wikipedia, ensiklopedia bebas.

Pendokumenan untuk modul ini boleh diciptakan di Modul:Find country/doc

--[[ 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 = {
	'Afrika Selatan',
	'Republik Afrika Tengah',
	'Afrika',
	'Antartika',
	'Asia Tengah',
	'Asia Selatan',
	'Asia Tenggara',
	'Asia',
	'Caribbean',
	'Eurasia',
	'Eropah',
	'Timur Tengah',
	'Amerika Utara',
	'Amerika Tengah',
	'Amerika Selatan',
	'Oceania',
	'Oseania',
	'Afghanistan',
	'Albania',
	'Algeria',
	'Samoa Amerika',
	'Andorra',
	'Angola',
	'Anguilla',
	'Antigua dan Barbuda',
	'Argentina',
	'Armenia',
	'Aruba',
	'Australia',
	'Empayar Austria',
	'Austria-Hungary',
	'Austria',
	'Azerbaijan',
	'Bahamas',
	'Bahamas',
	'Bahrain',
	'Bangladesh',
	'Barbados',
	'Belarus',
	'Belgium',
	'Belize',
	'Benin',
	'Bermuda',
	'Bhutan',
	'Bolivia',
	'Bosnia dan Herzegovina',
	'Botswana',
	'Brazil',
	'Brunei Darussalam',
	'Brunei',
	'Bulgaria',
	'Burkina Faso',
	'Burundi',
	'Cambodia',
	'Cameroon',
	'Kanada',
	'Cape Verde',
	'Kepulauan Cayman',
	'Chad',
	'Chile',
	"Republik Rakyat China",
	'RR China',
	'China',
	'Colombia',
	'Comoros',
	'Republik Congo',
	'Republik Demokratik Congo',
	'Congo DR',
	'DR Congo',
	'DRC',	
	'Congo',
	'Kepulauan Cook',
	'Costa Rica',
	'Croatia',
	'Cuba',
	'Curaçao',
	'Curacao',
	'Cyprus',
	'Republik Czech',
	'Czechia',
	'Denmark',
	'Djibouti',
	'Republik Dominica',
	'Dominica',
	'Timor-Leste',
	'Ecuador',
	'Mesir',
	'El Salvador',
	'England',
	'Eritrea',
	'Estonia',
	'Eswatini',
	'Habsyah',
	'Ethiopia',
	'Kepulauan Falkland',
	'Kepulauan Faroe',
	'Fiji',
	'Finland',
	'Perancis',
	'Gabon',
	'Gambia',
	'Georgia',
	'Jerman',
	'Ghana',
	'Gibraltar',
	'Great Britain',
	'Britain',
	'Greece',
	'Yunani',
	'Grenada',
	'Guam',
	'Guatemala',
	'Papua New Guinea',	
	'Guinea Khatulistiwa',	
	'Guinea-Bissau',
	'Guinea',
	'Guyana',
	'Haiti',
	'Honduras',
	'Hong Kong',
	'Hungary',
	'Iceland',
	'India',
	'Indonesia',
	'Iran',
	'Iraq',
	'Ireland Utara',	
	'Republik Ireland',
	'Ireland',
	'Israel',
	'Itali',
	'Ivory Coast',
	"Côte d'Ivoire",
	'Jamaica',
	'Jepun',
	'Jordan',
	'Kazakhstan',
	'Kenya',
	'Kiribati',
	'Kosovo',
	'Republik Kosovo',
	'Kuwait',
	'Kyrgyzstan',
	'Republik Kyrgyz',
	"Republik Demokratik Rakyat Laos",
	"Republik Demokratik Rakyat Lao",
	'Laos',
	'Latvia',
	'Lebanon',
	'Lesotho',
	'Liberia',
	'Libya',
	'Liechtenstein',
	'Lithuania',
	'Luxembourg',
	'Macau',
	'Republik Macedonia',
	'Macedonia Utara',
	'Macedonia',
	'Madagascar',
	'Malawi',
	'Malaysia',
	'Maldives',
	'Mali',
	'Malta',
	'Kepulauan Marshall',
	'Mauritania',
	'Mauritius',
	'Mexico',
	'Persekutuan Negeri Micronesia',
	'Micronesia',
	'FSM',
	'Moldova',
	'Monaco',
	'Mongolia',
	'Montenegro',
	'Montserrat',
	'Morocco',
	'Mozambique',
	'Myanmar',
	'Namibia',
	'Nauru',
	'Nepal',
	'Belanda',
	'New Caledonia',
	'New Zealand',
	'Nicaragua',
	'Nigeria',
	'Niger',
	'Korea Utara',
	"Republik Demokratik Rakyat Korea",
	'DPR Korea',
	'Korea DPR',
	'Norway',
	'Oman',
	'Empayar Uthmaniyah',
	'Mesir Uthmaniyah',	
	'Pakistan',
	'Palau',
	'Mandat Palestin',
	'Palestin',
	'wilayah Palestin',
	'Panama',
	'Paraguay',
	'Peru',
	'Filipina',
	'Poland',
	'Portugal',
	'Puerto Rico',
	'Kepulayan Virgin Sepanyol',
	'Qatar',
	'Romania',
	'Empayar Rusia',
	'Persekutuan Rusia',
	'Rusia',
	'Rwanda',
	'Saint Kitts dan Nevis',
	'Saint Lucia',
	'Saint Vincent dan Kepulauan Grenadine',
	'Samoa Barat',
	'Samoa',
	'San Marino',
	'São Tomé dan Príncipe',
	'São Tomé dan Príncipe',
	'Sao Tome dan Principe',
	'Arab Saudi',
	'Scotland',
	'Senegal',
	'Serbia',
	'Seychelles',
	'Sierra Leone',
	'Singapura',
	'Slovakia',
	'Slovenia',
	'Kepulauan Solomon',
	'Somalia',
	'Korea Selatan',
	'Republik Korea',
	'Sudan Selatan',
	'Sudan',
	'Spain',
	'Sri Lanka',
	'Suriname',
	'Swaziland',
	'Sweden',
	'Switzerland',
	'Syria',
	'Tahiti',
	'Republik China',
	'Taiwan',
	'Tajikistan',
	'Tanzania',
	'Thailand',
	'Togo',
	'Tonga',
	'Trinidad dan Tobago',
	'Trinidad',
	'Tobago',
	'Tunisia',
	'Turkey',
	'Turki',
	'Turkmenistan',
	'Kepulauan Turks dan Caicos',
	'Tuvalu',
	'Uganda',
	'Ukraine',
	'Emiriah Arab Bersatu',
	'UAE',
	'U.A.E.',
	'United Kingdom',
	'UK',
	'U.K.',
	'Amerika Syarikat',
	'Amerika',
	'AS',
	'A.S',
	'US',
	'U.S.',
	'USA',
	'U.S.A.',
	'Uruguay',
	'Uzbekistan',
	'Vanuatu',
	'Venezuela',
	'Vietnam',
	'Kepulauan Virgin British',
	'Kepulauan Virgin UK',
	'Kepulauan Virgin U.K.',
	'Kepulauan Virgin Amerika Syarikat',
	'Kepulauan Virgin AS',
	'Kepulauan Virgin A.S.',
	'US Virgin Islands',
	'Kepulauan Virgin ',
	'Wales',
	'Yaman',
	'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