Difference between revisions of "Module:CountryAdjectiveDemonym/sandbox"

From blackwiki
Jump to navigation Jump to search
blackwiki>BrownHairedGirl
(fix sub-module names)
blackwiki>BrownHairedGirl
(separate adjectives from demonyms)
Line 4: Line 4:
  
 
local countryAdjectivesToNounsTable = { }
 
local countryAdjectivesToNounsTable = { }
local countryNounstoAdjectivesTable = { }
+
local countryNounsToAdjectivesTable = { }
 +
 
 +
local countryDemonymsToNounsTable = { }
 +
local countryNounsToDemonymsTable  = { }
 +
 
 
local countriesPrefixedByTheTable = { }
 
local countriesPrefixedByTheTable = { }
  
 
function CountryAdjectiveDemonymDoLoadData()
 
function CountryAdjectiveDemonymDoLoadData()
 
countriesPrefixedByTheTable = mw.loadData( 'Module:CountryNameDemonym/the' )
 
countriesPrefixedByTheTable = mw.loadData( 'Module:CountryNameDemonym/the' )
countryNounstoAdjectivesTable = mw.loadData( 'Module:CountryNameDemonym/adjectives' )
+
countryNounsToAdjectivesTable = mw.loadData( 'Module:CountryNameDemonym/adjectives' )
local myCounter = 0
+
countryNounsToDemonymsTable = mw.loadData( 'Module:CountryNameDemonym/demonyms' )
local myNoun, myAdj
+
local myNoun, myAdjective
for myNoun, myAdj in pairs(countryNounstoAdjectivesTable) do
+
countryAdjectivesToNounsTable[myAdj] = myNoun
+
-- first, load the adjectives table
myCounter = myCounter + 1
+
for myNoun, myAdjective in pairs(countryNounsToAdjectivesTable) do
 +
countryAdjectivesToNounsTable[myAdjective] = myNoun
 +
end
 +
 
 +
-- Now load the denomyms table
 +
local myDemonym
 +
for myNoun, myDemonym in pairs(countryNounsToDemonymsTable) do
 +
countryDemonymsToNounsTable[myDemonym] = myNoun
 
end
 
end
 
CountryAdjectiveDemonymDataLoaded = true
 
CountryAdjectiveDemonymDataLoaded = true
return myCounter
+
return
 
end
 
end
  
Line 35: Line 46:
 
end
 
end
 
return ""
 
return ""
 +
end
 +
 +
 +
function CountryAdjectiveDemonym.getCountryFromAdjective(frame)
 +
local s = frame.args[1]
 +
if not CountryAdjectiveDemonymDataLoaded then
 +
CountryAdjectiveDemonymDoLoadData()
 +
end
 +
local retval = countryAdjectivesToNounsTable[s]
 +
if retval == nil then
 +
return ""
 +
end
 +
return retval
 
end
 
end
  
Line 43: Line 67:
 
CountryAdjectiveDemonymDoLoadData()
 
CountryAdjectiveDemonymDoLoadData()
 
end
 
end
local retval = countryAdjectivesToNounsTable[s]
+
local retval = countryDemonymsToNounsTable(s)
 +
if s == nil then
 +
retval = countryAdjectivesToNounsTable[s]
 +
end
 +
if retval == nil then
 +
return ""
 +
end
 +
return retval
 +
end
 +
 
 +
 
 +
function CountryAdjectiveDemonym.getAdjectiveFromCountry(frame)
 +
local s = frame.args[1]
 +
if not CountryAdjectiveDemonymDataLoaded then
 +
CountryAdjectiveDemonymDoLoadData()
 +
end
 +
local retval = countryNounsToAdjectivesTable[s]
 
if retval == nil then
 
if retval == nil then
 
return ""
 
return ""
Line 56: Line 96:
 
CountryAdjectiveDemonymDoLoadData()
 
CountryAdjectiveDemonymDoLoadData()
 
end
 
end
local retval = countryNounstoAdjectivesTable[s]
+
local retval
 +
retval = countryNounsToDemonymsTable[s]
 +
if retval == nil then
 +
retval = countryNounsToAdjectivesTable[s]
 +
end
 
if retval == nil then
 
if retval == nil then
 
return ""
 
return ""
Line 62: Line 106:
 
return retval
 
return retval
 
end
 
end
 +
  
 
function CountryAdjectiveDemonym.stripThe(frame)
 
function CountryAdjectiveDemonym.stripThe(frame)

Revision as of 22:06, 7 October 2019

Documentation for this module may be created at Module:CountryAdjectiveDemonym/sandbox/doc

local CountryAdjectiveDemonym = { }

local CountryAdjectiveDemonymDataLoaded = false

local countryAdjectivesToNounsTable = { }
local countryNounsToAdjectivesTable  = { }

local countryDemonymsToNounsTable = { }
local countryNounsToDemonymsTable  = { }

local countriesPrefixedByTheTable = { }

function CountryAdjectiveDemonymDoLoadData()
	countriesPrefixedByTheTable = mw.loadData( 'Module:CountryNameDemonym/the' )
	countryNounsToAdjectivesTable = mw.loadData( 'Module:CountryNameDemonym/adjectives' )
	countryNounsToDemonymsTable = mw.loadData( 'Module:CountryNameDemonym/demonyms' )
	local myNoun, myAdjective
	
	-- first, load the adjectives table
	for myNoun, myAdjective in pairs(countryNounsToAdjectivesTable) do
		countryAdjectivesToNounsTable[myAdjective] = myNoun
	end

	-- Now load the denomyms table
	local myDemonym
	for myNoun, myDemonym in pairs(countryNounsToDemonymsTable) do
		countryDemonymsToNounsTable[myDemonym] = myNoun
	end
	CountryAdjectiveDemonymDataLoaded = true
	return
end


-- ############### Publicly accesible functions #######################

-- if the country name is prefixed by "the" in running text,
-- then return that prefix
-- Otherwise just return an empty string
function CountryAdjectiveDemonym.countryPrefixThe(frame)
	local s = frame.args[1]
	if not CountryAdjectiveDemonymDataLoaded then
		CountryAdjectiveDemonymDoLoadData()
	end
	if (countriesPrefixedByTheTable[s] == true) then
		return "the "
	end
	return ""
end


function CountryAdjectiveDemonym.getCountryFromAdjective(frame)
	local s = frame.args[1]
	if not CountryAdjectiveDemonymDataLoaded then
		CountryAdjectiveDemonymDoLoadData()
	end
	local retval = countryAdjectivesToNounsTable[s]
	if retval == nil then
		return ""
	end
	return retval
end


function CountryAdjectiveDemonym.getCountryFromDemonym(frame)
	local s = frame.args[1]
	if not CountryAdjectiveDemonymDataLoaded then
		CountryAdjectiveDemonymDoLoadData()
	end
	local retval = countryDemonymsToNounsTable(s)
	if s == nil then 
		retval = countryAdjectivesToNounsTable[s]
	end
	if retval == nil then
		return ""
	end
	return retval
end


function CountryAdjectiveDemonym.getAdjectiveFromCountry(frame)
	local s = frame.args[1]
	if not CountryAdjectiveDemonymDataLoaded then
		CountryAdjectiveDemonymDoLoadData()
	end
	local retval = countryNounsToAdjectivesTable[s]
	if retval == nil then
		return ""
	end
	return retval
end


function CountryAdjectiveDemonym.getDemonymFromCountry(frame)
	local s = frame.args[1]
	if not CountryAdjectiveDemonymDataLoaded then
		CountryAdjectiveDemonymDoLoadData()
	end
	local retval
	retval = countryNounsToDemonymsTable[s]
	if retval == nil then
		retval = countryNounsToAdjectivesTable[s]
	end
	if retval == nil then
		return ""
	end
	return retval
end


function CountryAdjectiveDemonym.stripThe(frame)
	local s = frame.args[1]
	if s == nil then
		return ""
	end
	if mw.ustring.match( s, "^[T]he Gambia$") ~= nil then
		return s
	end
	local stripped = mw.ustring.gsub(s, "^[tT]he ", "")
	return stripped
end


return CountryAdjectiveDemonym