Module:CanonicalForms: Difference between revisions

No edit summary
// via Wikitext Extension for VSCode
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:CanonicalForms
--[[
-- Provides normalization functions by stripping wiki-link markup and mapping input to canonical values.
* Name: CanonicalForms
-- Templates supply mapping tables, each containing:
* Author: Mark W. Datysgeld
--  • canonical: The standard display value.
* Description: Text normalization utility that removes wiki markup and maps user input to canonical values using configurable lookup tables
--  • synonyms:  Case-insensitive alternatives mapping to the canonical value.
* Notes: Example usage: local mapping = { { canonical = "gTLD", synonyms = {"generic", "g"} }, { canonical = "ccTLD", synonyms = {"country", "cc"} } }; local canonical, css, category = require('Module:CanonicalForms').normalize(inputString, mapping)
--  • [optional] css: An associated CSS class.
]]
--  • [optional] category: A category string for auto-assignment.
--
-- The normalize function removes wiki markup, converts input to lowercase,
-- and matches it against provided synonyms, returning the canonical value, css, and category if applicable.
--
-- Example usage:
--  local mapping = {
--    { canonical = "gTLD", synonyms = {"generic", "gtld", "tld"} },
--    { canonical = "ccTLD", synonyms = {"cctld", "country", "cc"} }
--  }
--  local canonical, css, category = require('Module:CanonicalForms').normalize(inputString, mapping)


local CanonicalForms = {}
local CanonicalForms = {}


--- Normalizes an input string based on a mapping table.
-- Normalize an input string
-- Strips wiki-link markup, converts to lowercase, and returns the canonical value.
-- Removes wiki markup, converts to lowercase, and maps to canonical form
-- @param input The input string to normalize.
-- @param mappingTable An array of mapping groups.
-- @return canonical The matched canonical value, or the cleaned input if no match is found.
-- @return css (optional) An associated CSS class if available.
-- @return category (optional) An associated category string if available.
function CanonicalForms.normalize(input, mappingTable)
function CanonicalForms.normalize(input, mappingTable)
     if not input or input == "" then
     if not input or input == "" then
Line 31: Line 15:
     end
     end


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


     for _, group in ipairs(mappingTable) do
     -- Create lookup table for faster matching (first call only)
        for _, syn in ipairs(group.synonyms or {}) do
    if not mappingTable._lookupCache then
            if cleanInput == syn:lower() then
        local lookupCache = {}
                return group.canonical, group.css, group.category
        for _, group in ipairs(mappingTable) do
            -- Add the canonical form itself to the lookup (in lowercase)
            lookupCache[group.canonical:lower()] = group
           
            -- Add all synonyms to the lookup
            for _, syn in ipairs(group.synonyms or {}) do
                lookupCache[syn:lower()] = group
             end
             end
         end
         end
        mappingTable._lookupCache = lookupCache
    end
    -- Direct lookup via cache
    local match = mappingTable._lookupCache[cleanInput]
    if match then
        return match.canonical, match.css, match.category
     end
     end