Module:CanonicalForms: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 1: Line 1:
-- Module:CanonicalForms
-- Module:CanonicalForms
-- Normalizes strings by removing wiki markup and mapping them to canonical values.
-- Normalizes strings by removing wiki markup and mapping to canonical values.
-- Mapping tables include:
-- Mapping tables:
--  * canonical: Standard display value.
--  * canonical: Display value
--  * synonyms: Synonyms (case-insensitive) mapped to canonical.
--  * synonyms: Case-insensitive terms mapped to canonical
--  * [optional] css: Associated CSS class.
--  * [optional] css: CSS class
--  * [optional] category: Auto-assignment category.
--  * [optional] category: Auto-assignment category
--
--
-- Example:
-- Example:
Line 17: Line 17:


--- Normalize an input string.
--- Normalize an input string.
-- Removes wiki markup and converts input to lowercase.
-- Removes wiki markup, converts to lowercase, and maps to canonical form.
-- Checks mappingTable for a matching synonym and returns the corresponding canonical value.
-- @param input String to normalize.
-- @param input String to normalize.
-- @param mappingTable Array of mapping groups with 'canonical', 'synonyms', and optional 'css' and 'category'.
-- @param mappingTable Mapping groups with 'canonical', 'synonyms', and optional 'css'/'category'.
-- @return canonical Matched value, or cleaned input if no match.
-- @return canonical Matched value or cleaned input if no match.
-- @return css Optional CSS class.
-- @return css Optional CSS class.
-- @return category Optional category string.
-- @return category Optional category string.
Line 29: Line 28:
     end
     end


     -- Remove wiki-link markup; eg. "[[Brand TLD]]" to "Brand TLD"
     -- Remove wiki-link markup (e.g., "[[Brand TLD]]" "Brand TLD")
     local cleanInput = input:gsub("%[%[([^|%]]+)|?[^%]]*%]%]", "%1"):lower()
     local cleanInput = input:gsub("%[%[([^|%]]+)|?[^%]]*%]%]", "%1"):lower()


     -- Create a lookup table for faster synonym matching
     -- Create lookup table for faster matching (first call only)
    -- This is done on first call with a new mappingTable
     if not mappingTable._lookupCache then
     if not mappingTable._lookupCache then
         local lookupCache = {}
         local lookupCache = {}
Line 44: Line 42:
     end
     end


     -- Direct lookup instead of nested loops
     -- Direct lookup via cache
     local match = mappingTable._lookupCache[cleanInput]
     local match = mappingTable._lookupCache[cleanInput]
     if match then
     if match then