Jump to content

Module:CanonicalForms: Difference between revisions

No edit summary
// via Wikitext Extension for VSCode
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:CanonicalForms
--[[
-- This module provides generic normalization functions.
* Name: CanonicalForms
-- It removes any wiki-link markup (e.g., [[Brand TLD]]) from the input and converts it to a canonical form.
* Author: Mark W. Datysgeld
-- Templates supply their own mapping tables (arrays of mapping groups), where each group is a table with:
* Description: Text normalization utility that removes wiki markup and maps user input to canonical values using configurable lookup tables
--  • canonical: The canonical display 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)
--  • synonyms: An array of synonyms (case insensitive) that should normalize to the canonical value.
]]
--  • [optional] css: An extra property (e.g., a CSS class) associated with the canonical value.
 
--  • [optional] category: A category string for auto-assignment.
--
-- The normalize function processes an input string by stripping wiki markup,
-- converting it to lowercase, and then comparing it against the provided synonyms.
-- It returns the canonical value along with any associated css and category.
--
-- Usage example:
--  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 = {}


--- Normalize an input string based on a provided mapping table.
-- Normalize an input string
-- Strips wiki link markup, converts the string to lowercase,
-- Removes wiki markup, converts to lowercase, and maps to canonical form
-- and returns the canonical value, along with optional css and category properties if a match is found.
-- @param input The input string to normalize.
-- @param mappingTable An array of mapping groups.
-- @return canonical The canonical display value (if a match is found), or the cleaned input if no match.
-- @return css (optional) The associated extra property (e.g. CSS class) if available.
-- @return category (optional) The 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 34: Line 15:
     end
     end


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


     for _, group in ipairs(mappingTable) do
     -- Create lookup table for faster matching (first call only)
        if group.synonyms and type(group.synonyms) == "table" then
    if not mappingTable._lookupCache then
             for _, syn in ipairs(group.synonyms) do
        local lookupCache = {}
                 if lowerInput == syn:lower() then
        for _, group in ipairs(mappingTable) do
                    return group.canonical, group.css, group.category
            -- Add the canonical form itself to the lookup (in lowercase)
                end
            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
     return cleanInput, nil, nil
     return cleanInput, nil, nil
end
end


return CanonicalForms
return CanonicalForms

Latest revision as of 02:56, 25 August 2025

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

--[[
* Name: CanonicalForms
* Author: Mark W. Datysgeld
* Description: Text normalization utility that removes wiki markup and maps user input to canonical values using configurable lookup tables
* 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)
]]

local CanonicalForms = {}

-- Normalize an input string
-- Removes wiki markup, converts to lowercase, and maps to canonical form
function CanonicalForms.normalize(input, mappingTable)
    if not input or input == "" then
        return nil, nil, nil
    end

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

    -- Create lookup table for faster matching (first call only)
    if not mappingTable._lookupCache then
        local lookupCache = {}
        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
        mappingTable._lookupCache = lookupCache
    end

    -- Direct lookup via cache
    local match = mappingTable._lookupCache[cleanInput]
    if match then
        return match.canonical, match.css, match.category
    end

    return cleanInput, nil, nil
end

return CanonicalForms