Difference between revisions of "Module:Find country"

From blackwiki
Jump to navigation Jump to search
blackwiki>Le Deluge
(Initial clone of Title monthname, but will only work for single-word countries)
 
m (9 revisions imported)
 
(8 intermediate revisions by one other user not shown)
Line 1: Line 1:
 
--[[ v1.00
 
--[[ v1.00
     Split the page title into words then test each of them against
+
     Test the string against the list of countries/continents.
    the list of countries/continents.
 
    Optionally, an alternative page name may be supplied as a parameter.
 
 
     Return the first word which matches a country/continent name ...
 
     Return the first word which matches a country/continent name ...
 
     unless the "match=" parameter specifies a different match.
 
     unless the "match=" parameter specifies a different match.
Line 17: Line 15:
  
 
local countryList = {
 
local countryList = {
'testcase',
 
 
'South Africa',
 
'South Africa',
 +
'the Central African Republic',
 +
'Central African Republic',
 
'Africa',
 
'Africa',
 
'Antarctica',
 
'Antarctica',
Line 48: Line 47:
 
'Aruba',
 
'Aruba',
 
'Australia',
 
'Australia',
 +
'the Austrian Empire',
 +
'Austrian Empire',
 +
'Austria-Hungary',
 
'Austria',
 
'Austria',
 
'Azerbaijan',
 
'Azerbaijan',
Line 53: Line 55:
 
'Bahamas',
 
'Bahamas',
 
'Bahrain',
 
'Bahrain',
'Bangladeshi',
+
'Bangladesh',
 
'Barbados',
 
'Barbados',
 
'Belarus',
 
'Belarus',
Line 76: Line 78:
 
'the Cayman Islands',
 
'the Cayman Islands',
 
'Cayman Islands',
 
'Cayman Islands',
'the Central African Republic',
 
'Central African Republic',
 
 
'Chad',
 
'Chad',
 
'Chile',
 
'Chile',
 
"the People's Republic of China",
 
"the People's Republic of China",
 
"People's Republic of China",
 
"People's Republic of China",
'China',
 
 
'China PR',
 
'China PR',
 
'PR China',
 
'PR China',
 +
'China',
 
'Colombia',
 
'Colombia',
 
'Comoros',
 
'Comoros',
 
'the Republic of the Congo',
 
'the Republic of the Congo',
 +
'the Democratic Republic of the Congo',
 +
'Democratic Republic of the Congo',
 +
'Congo DR',
 +
'DR Congo',
 +
'DRC',
 
'the Congo',
 
'the Congo',
 
'Congo',
 
'Congo',
Line 100: Line 105:
 
'Czech Republic',
 
'Czech Republic',
 
'Czechia',
 
'Czechia',
'the Democratic Republic of the Congo',
 
'Democratic Republic of the Congo',
 
'Congo DR',
 
'DR Congo',
 
'DRC',
 
 
'Denmark',
 
'Denmark',
 
'Djibouti',
 
'Djibouti',
'Dominica',
 
 
'the Dominican Republic',
 
'the Dominican Republic',
 
'Dominican Republic',
 
'Dominican Republic',
 +
'Dominica',
 
'East Timor',
 
'East Timor',
 
'Timor-Leste',
 
'Timor-Leste',
Line 116: Line 116:
 
'El Salvador',
 
'El Salvador',
 
'England',
 
'England',
'Equatorial Guinea',
 
 
'Eritrea',
 
'Eritrea',
 
'Estonia',
 
'Estonia',
 +
'Eswatini',
 
'Ethiopia',
 
'Ethiopia',
 
'the Falkland Islands',
 
'the Falkland Islands',
Line 140: Line 140:
 
'Guam',
 
'Guam',
 
'Guatemala',
 
'Guatemala',
 +
'Papua New Guinea',
 +
'Equatorial Guinea',
 +
'Guinea-Bissau',
 
'Guinea',
 
'Guinea',
'Guinea-Bissau',
 
 
'Guyana',
 
'Guyana',
 
'Haiti',
 
'Haiti',
Line 152: Line 154:
 
'Iran',
 
'Iran',
 
'Iraq',
 
'Iraq',
'Ireland',
+
'Northern Ireland',
 
'the Republic of Ireland',
 
'the Republic of Ireland',
 
'Republic of Ireland',
 
'Republic of Ireland',
 +
'Ireland',
 
'Israel',
 
'Israel',
 
'Italy',
 
'Italy',
Line 220: Line 223:
 
'New Zealand',
 
'New Zealand',
 
'Nicaragua',
 
'Nicaragua',
 +
'Nigeria',
 
'Niger',
 
'Niger',
'Nigeria',
 
 
'North Korea',
 
'North Korea',
 
"the People's Democratic Republic of Korea",
 
"the People's Democratic Republic of Korea",
Line 229: Line 232:
 
'DPR Korea',
 
'DPR Korea',
 
'Korea DPR',
 
'Korea DPR',
'Northern Ireland',
 
 
'Norway',
 
'Norway',
 
'Oman',
 
'Oman',
 +
'the Ottoman Empire',
 +
'Ottoman Empire',
 +
'Ottoman Egypt',
 
'Pakistan',
 
'Pakistan',
 
'Palau',
 
'Palau',
Line 239: Line 244:
 
'Palestinian territories',
 
'Palestinian territories',
 
'Panama',
 
'Panama',
'Papua New Guinea',
 
 
'Paraguay',
 
'Paraguay',
 
'Peru',
 
'Peru',
Line 251: Line 255:
 
'Qatar',
 
'Qatar',
 
'Romania',
 
'Romania',
'Russia',
+
'the Russian Empire',
 +
'Russian Empire',
 
'the Russian Federation',
 
'the Russian Federation',
 
'Russian Federation',
 
'Russian Federation',
 +
'Russia',
 
'Rwanda',
 
'Rwanda',
 
'Saint Kitts and Nevis',
 
'Saint Kitts and Nevis',
Line 280: Line 286:
 
'Korea Republic',
 
'Korea Republic',
 
'South Sudan',
 
'South Sudan',
 +
'Sudan',
 
'Spain',
 
'Spain',
 
'Sri Lanka',
 
'Sri Lanka',
'Sudan',
 
 
'Suriname',
 
'Suriname',
 
'Swaziland',
 
'Swaziland',
Line 314: Line 320:
 
'U.A.E.',
 
'U.A.E.',
 
'the United Kingdom',
 
'the United Kingdom',
 +
'United Kingdom',
 
'the UK',
 
'the UK',
 
'the U.K.',
 
'the U.K.',
'United Kingdom',
 
 
'UK',
 
'UK',
 
'U.K.',
 
'U.K.',
Line 348: Line 354:
 
'Yemen',
 
'Yemen',
 
'Zambia',
 
'Zambia',
'Zimbabwe'
+
'Zimbabwe',
 +
'Find country/testcases'
 
}
 
}
  
-- splits a string into "words"
+
-- returns the name of a country that is found in the string
-- a "word" is a set of characters delineated at each end by one
 
--    or more whitespace characters or punctaution charaters
 
function splitIntoWords(str)
 
result = {}
 
index = 1
 
s = mw.ustring.gsub(str, "^[%s%p]+", "") -- strip leading whitespace or punctuation
 
for s2 in mw.ustring.gmatch(s, "[^%s%p]+[%s%p]*") do
 
s3 = mw.ustring.gsub(s2, "[%s%p]+$", "") -- strip trailing separators
 
result[index] = s3
 
index = index + 1
 
end
 
return result
 
end
 
 
 
-- returns the first word is the pagename which matches the name of a month
 
 
-- ... or an empty string if there is no match
 
-- ... or an empty string if there is no match
 
function findcountryinstring(str)
 
function findcountryinstring(str)
-- split the pagename into sparate words
+
 
titleWords = splitIntoWords(str)
 
 
 
nMatches = 0
 
nMatches = 0
 
myMatches ={}
 
myMatches ={}
+
str=" " .. str:gsub("^%s*(.-)%s*$", "%1") .. " "
-- check each words in turn, to see if it matches a country
+
 
for w, thisWord in ipairs(titleWords) do
 
 
-- check agaist each country
 
-- check agaist each country
 
-- if there is a match, then return that country
 
-- if there is a match, then return that country
for i, thisCountry in ipairs(countryList) do
+
for i, thiscountry in ipairs(countryList) do
if (thisCountry == thisWord) then
+
if mw.ustring.find(str, thiscountry) then
 
nMatches = nMatches + 1
 
nMatches = nMatches + 1
myMatches[nMatches] = thisCountry
+
myMatches[nMatches] = thiscountry
 
end
 
end
 
end
 
end
end
+
 
  
 
if (nMatches == 0) then
 
if (nMatches == 0) then
Line 432: Line 421:
 
 
 
-- by default, we use the current page
 
-- by default, we use the current page
-- but if the "page=" parameters is supplied, we use that
+
-- but if the "string=" parameters is supplied, we use that
 
-- so we try the parameter first
 
-- so we try the parameter first
 
thispagename = nil
 
thispagename = nil
if ((args['page'] ~= nil) and (args['page'] ~= "")) then
+
if ((args['string'] ~= nil) and (args['string'] ~= "")) then
-- we have a non-empty "page" parameter, so we use it
+
-- we have a non-empty "string" parameter, so we use it
thispagename = args['page']
+
thisstring = args['string']
 
else
 
else
 
-- get the page title
 
-- get the page title
 
thispage = mw.title.getCurrentTitle()
 
thispage = mw.title.getCurrentTitle()
thispagename = thispage.text;
+
thisstring = thispage.text;
 
end
 
end
 
 
 
-- now check the pagename to try to find a country
 
-- now check the pagename to try to find a country
result = findcountryinstring(thispagename)
+
result = findcountryinstring(thisstring)
 
if (result == "") then
 
if (result == "") then
 
return nomatch
 
return nomatch

Latest revision as of 16:35, 26 September 2020

{{Module rating }}

Usage

For use by Template:Find country. See documentation there. {{#invoke:Find country|function_name}}



--[[ 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',	
	'Africa',
	'Antarctica',
	'Central Asia',
	'South Asia',
	'South East Asia',
	'Southeast Asia',
	'Asia',
	'the Caribbean',
	'Caribbean',
	'Eurasia',
	'Europe',
	'the Middle East',
	'Middle East',
	'Central America',
	'North America',
	'South America',
	'the Americas',
	'Afghanistan',
	'Albania',
	'Algeria',
	'American Samoa',
	'Andorra',
	'Angola',
	'Anguilla',
	'Antigua and Barbuda',
	'Argentina',
	'Armenia',
	'Aruba',
	'Australia',
	'the Austrian Empire',
	'Austrian Empire',	
	'Austria-Hungary',
	'Austria',
	'Azerbaijan',
	'the Bahamas',
	'Bahamas',
	'Bahrain',
	'Bangladesh',
	'Barbados',
	'Belarus',
	'Belgium',
	'Belize',
	'Benin',
	'Bermuda',
	'Bhutan',
	'Bolivia',
	'Bosnia',
	'Botswana',
	'Brazil',
	'Brunei Darussalam',
	'Brunei',
	'Bulgaria',
	'Burkina Faso',
	'Burundi',
	'Cambodia',
	'Cameroon',
	'Canada',
	'Cape Verde',
	'the Cayman Islands',
	'Cayman Islands',
	'Chad',
	'Chile',
	"the People's Republic of China",
	"People's Republic of China",
	'China PR',
	'PR China',
	'China',
	'Colombia',
	'Comoros',
	'the Republic of the Congo',
	'the Democratic Republic of the Congo',
	'Democratic Republic of the Congo',
	'Congo DR',
	'DR Congo',
	'DRC',	
	'the Congo',
	'Congo',
	'Cook Islands',
	'Costa Rica',
	'Croatia',
	'Cuba',
	'Curaçao',
	'Curacao',
	'Cyprus',
	'the Czech Republic',
	'Czech Republic',
	'Czechia',
	'Denmark',
	'Djibouti',
	'the Dominican Republic',
	'Dominican Republic',
	'Dominica',
	'East Timor',
	'Timor-Leste',
	'Ecuador',
	'Egypt',
	'El Salvador',
	'England',
	'Eritrea',
	'Estonia',
	'Eswatini',	
	'Ethiopia',
	'the Falkland Islands',
	'Falkland Islands',
	'the Faroe Islands',
	'Faroe Islands',
	'Fiji',
	'Finland',
	'France',
	'Gabon',
	'the Gambia',
	'Gambia',
	'Georgia',
	'Germany',
	'Ghana',
	'Gibraltar',
	'Great Britain',
	'Britain',
	'Greece',
	'Grenada',
	'Guam',
	'Guatemala',
	'Papua New Guinea',	
	'Equatorial Guinea',	
	'Guinea-Bissau',
	'Guinea',
	'Guyana',
	'Haiti',
	'Honduras',
	'Hong Kong',
	'Hungary',
	'Iceland',
	'India',
	'Indonesia',
	'Iran',
	'Iraq',
	'Northern Ireland',	
	'the Republic of Ireland',
	'Republic of Ireland',
	'Ireland',
	'Israel',
	'Italy',
	'Ivory Coast',
	"Côte d'Ivoire",
	'Jamaica',
	'Japan',
	'Jordan',
	'Kazakhstan',
	'Kenya',
	'Kiribati',
	'Kosovo',
	'the Republic of Kosovo',
	'Republic of Kosovo',
	'Kuwait',
	'Kyrgyzstan',
	'the Kyrgyz Republic',
	'Kyrgyz Republic',
	"the Lao People's Democratic Republic",
	"Lao People's Democratic Republic",
	'Laos',
	'Latvia',
	'Lebanon',
	'Lesotho',
	'Liberia',
	'Libya',
	'Liechtenstein',
	'Lithuania',
	'Luxembourg',
	'Macau',
	'the Republic of Macedonia',
	'Republic of Macedonia',
	'North Macedonia',
	'Macedonia',
	'Madagascar',
	'Malawi',
	'Malaysia',
	'the Maldives',
	'Maldives',
	'Mali',
	'Malta',
	'the Marshall Islands',
	'Marshall Islands',
	'Mauritania',
	'Mauritius',
	'Mexico',
	'the Federated States of Micronesia',
	'Federated States of Micronesia',
	'Micronesia',
	'FSM',
	'Moldova',
	'Monaco',
	'Mongolia',
	'Montenegro',
	'Montserrat',
	'Morocco',
	'Mozambique',
	'Myanmar',
	'Namibia',
	'Nauru',
	'Nepal',
	'the Netherlands',
	'Netherlands',
	'New Caledonia',
	'New Zealand',
	'Nicaragua',
	'Nigeria',
	'Niger',
	'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',
	'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 Korea',
	'the Republic of Korea',
	'Korea Republic',
	'South Sudan',
	'Sudan',
	'Spain',
	'Sri Lanka',
	'Suriname',
	'Swaziland',
	'Sweden',
	'Switzerland',
	'Syria',
	'Tahiti',
	'the Republic of China',
	'Republic of China',
	'Taiwan',
	'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.',
	'the United Kingdom',
	'United Kingdom',
	'the UK',
	'the U.K.',
	'UK',
	'U.K.',
	'the United States of America',
	'the United States',
	'the USA',
	'the U.S.A.',
	'United States of America',
	'United States',
	'USA',
	'U.S.A.',
	'America',
	'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