Module:Find country

From Noble Work

Documentation for this module may be created at Module: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 = {
	'South Africa',
	'the Central African Republic',
	'Central African Republic',	
	'Central Asia',
	'South Asia',
	'South East Asia',
	'Southeast Asia',
	'the Caribbean',
	'the Middle East',
	'Middle East',
	'Central America',
	'North America',
	'South America',
	'the Americas',
	'American Samoa',
	'Antigua and Barbuda',
	'the Austrian Empire',
	'Austrian Empire',	
	'the Bahamas',
	'Bosnia and Herzegovina',
	'Brunei Darussalam',
	'Burkina Faso',
	'Cape Verde',
	'the Cayman Islands',
	'Cayman Islands',
	"the People's Republic of China",
	"People's Republic of China",
	'China PR',
	'PR China',
	'the Republic of the Congo',
	'the Democratic Republic of the Congo',
	'Democratic Republic of the Congo',
	'Congo DR',
	'DR Congo',
	'the Congo',
	'Cook Islands',
	'Costa Rica',
	'the Czech Republic',
	'Czech Republic',
	'the Dominican Republic',
	'Dominican Republic',
	'East Timor',
	'El Salvador',
	'the Falkland Islands',
	'Falkland Islands',
	'the Faroe Islands',
	'Faroe Islands',
	'the Gambia',
	'Great Britain',
	'Papua New Guinea',	
	'Equatorial Guinea',	
	'Hong Kong',
	'Northern Ireland',	
	'the Republic of Ireland',
	'Republic of Ireland',
	'Ivory Coast',
	"Côte d'Ivoire",
	'the Republic of Kosovo',
	'Republic of Kosovo',
	'the Kyrgyz Republic',
	'Kyrgyz Republic',
	"the Lao People's Democratic Republic",
	"Lao People's Democratic Republic",
	'the Republic of Macedonia',
	'Republic of Macedonia',
	'North Macedonia',
	'the Maldives',
	'the Marshall Islands',
	'Marshall Islands',
	'the Federated States of Micronesia',
	'Federated States of Micronesia',
	'the Netherlands',
	'New Caledonia',
	'New Zealand',
	'North Korea',
	"the People's Democratic Republic of Korea",
	"the Democratic People's Republic of Korea",
	"Democratic People's Republic of Korea",
	"People's Democratic Republic of Korea",
	'DPR Korea',
	'Korea DPR',
	'the Ottoman Empire',
	'Ottoman Empire',
	'Ottoman Egypt',	
	'Mandatory Palestine',
	'the Palestinian territories',
	'Palestinian territories',
	'the Philippines',
	'Puerto Rico',
	'the Spanish Virgin Islands',
	'Spanish Virgin Islands',
	'the Russian Empire',
	'Russian Empire',
	'the Russian Federation',
	'Russian Federation',
	'Saint Kitts and Nevis',
	'Saint Lucia',
	'Saint Vincent and the Grenadines',
	'Western Samoa',
	'San Marino',
	'São Tomé and Príncipe',
	'São Tomé and Príncipe',
	'Sao Tome and Principe',
	'Saudi Arabia',
	'Sierra Leone',
	'the Solomon Islands',
	'Solomon Islands',
	'South Korea',
	'the Republic of Korea',
	'Korea Republic',
	'South Sudan',
	'Sri Lanka',
	'the Republic of China',
	'Republic of China',
	'Trinidad and Tobago',
	'Turks and Caicos Islands',
	'the United Arab Emirates',
	'the UAE',
	'the U.A.E.',
	'United Arab Emirates',
	'the United Kingdom',
	'United Kingdom',
	'the UK',
	'the U.K.',
	'the United States of America',
	'the United States',
	'the USA',
	'the U.S.A.',
	'United States of America',
	'United States',
	'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',
	'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

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

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

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

function p._main(args)
	if (args['nomatch'] ~= nil) then
		nomatch = args['nomatch']
	-- 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
	-- 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']
		-- get the page title
		thispage = mw.title.getCurrentTitle()
		thisstring = thispage.text;
	-- now check the pagename to try to find a country
	result = findcountryinstring(thisstring)
	if (result == "") then
		return nomatch
	return result

return p