Module:CountryData: Difference between revisions
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| Line 67: | Line 67: | ||
local lookup = {} | local lookup = {} | ||
-- Optimization: Pre-count number of mappings | -- Optimization: Pre-count number of mappings | ||
| Line 83: | Line 82: | ||
-- Build the lookup table with pre-counted size | -- Build the lookup table with pre-counted size | ||
for code, country in pairs(data.countries) do | for code, country in pairs(data.countries) do | ||
-- Add name field as primary display name | -- Add name field as primary display name | ||
local displayName = country.name or country.canonical_name | local displayName = country.name or country.canonical_name | ||
if displayName then | if displayName then | ||
lookup[NormalizationText.normalizeText(displayName)] = code | |||
end | end | ||
-- Add canonical_name if different from name | -- Add canonical_name if different from name | ||
if country.canonical_name and country.canonical_name ~= country.name then | if country.canonical_name and country.canonical_name ~= country.name then | ||
lookup[NormalizationText.normalizeText(country.canonical_name)] = code | |||
end | end | ||
-- Add variations | -- Add variations | ||
if country.variations and type(country.variations) == "table" then | if country.variations and type(country.variations) == "table" then | ||
-- Use pairs instead of ipairs to handle both array and object structures | |||
for _, variation in pairs(country.variations) do | for _, variation in pairs(country.variations) do | ||
lookup[NormalizationText.normalizeText(variation)] = code | |||
end | end | ||
end | end | ||
| Line 614: | Line 596: | ||
end | end | ||
-- Get semantic property name from ConfigRepository | |||
function CountryData.getSemanticPropertyName(propertyKey) | |||
local ConfigRepository = require('Module:ConfigRepository') | |||
-- Look through all template configurations | |||
for templateName, templateConfig in pairs(ConfigRepository.templates) do | |||
-- Check if this template has semantics configuration | |||
if templateConfig.semantics and templateConfig.semantics.additionalProperties then | |||
-- Check if the property key exists in additionalProperties | |||
if templateConfig.semantics.additionalProperties[propertyKey] then | |||
return propertyKey | |||
end | |||
end | |||
end | |||
-- If not found, return nil | |||
return nil | |||
end | |||
-- Get semantic properties for countries and regions | -- Get semantic properties for countries and regions | ||
-- Returns a table of properties that can be integrated with the batch processing system | -- Returns a table of properties that can be integrated with the batch processing system | ||
function CountryData.getSemanticCountryRegionProperties(countryValue | function CountryData.getSemanticCountryRegionProperties(countryValue) | ||
-- Initialize return table | -- Initialize return table | ||
local properties = {} | local properties = {} | ||
if not countryValue or countryValue == "" then | if not countryValue or countryValue == "" then | ||
return properties | return properties | ||
end | end | ||
-- | -- Get property names from ConfigRepository | ||
local countryPropertyName = "Has country" | local countryPropertyName = CountryData.getSemanticPropertyName("Has country") | ||
local regionPropertyName = "Has ICANN region" | local regionPropertyName = CountryData.getSemanticPropertyName("Has ICANN region") | ||
-- If property names are not found in ConfigRepository, we can't proceed | |||
if not countryPropertyName or not regionPropertyName then | |||
return properties | |||
end | |||
-- Split multi-value country strings | -- Split multi-value country strings | ||
| Line 647: | Line 643: | ||
end | end | ||
end | end | ||
-- Process each country | -- Process each country | ||
for _, country in ipairs(countries) do | for _, country in ipairs(countries) do | ||
local normalizedCountry = CountryData.normalizeCountryName(country) | local normalizedCountry = CountryData.normalizeCountryName(country) | ||
-- Only process recognized countries | -- Only process recognized countries | ||
if normalizedCountry ~= "(Unrecognized)" then | if normalizedCountry ~= "(Unrecognized)" then | ||
-- Add country to properties table | -- Add country to properties table | ||
properties[countryPropertyName] = properties[countryPropertyName] or {} | properties[countryPropertyName] = properties[countryPropertyName] or {} | ||
| Line 695: | Line 659: | ||
properties[regionPropertyName] = properties[regionPropertyName] or {} | properties[regionPropertyName] = properties[regionPropertyName] or {} | ||
table.insert(properties[regionPropertyName], region) | table.insert(properties[regionPropertyName], region) | ||
end | end | ||
end | end | ||
end | end | ||
return properties | return properties | ||