Module:SemanticAnnotations: Difference between revisions

// via Wikitext Extension for VSCode
Tag: Reverted
// via Wikitext Extension for VSCode
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:SemanticAnnotations
--[[
-- Generates semantic annotations for templates in MediaWiki
* Name: SemanticAnnotations
* Author: Mark W. Datysgeld
* Description: Primary semantic integration module for generating semantic properties with transformation support and property limits
* Notes: Implements batching, deduplication and property limits (200 total, 25 per property); supports simple, object, complex, and subobject mappings; falls back to parser functions when mw.smw unavailable; includes pruning to prevent server crashes
]]


local p = {}
local p = {}
Line 10: Line 14:
local VALUE_LIMIT = 25    -- per individual property
local VALUE_LIMIT = 25    -- per individual property


-- Trim whitespace helper
--[[ Fallback for mw.smw.set using the #set parser function.
-- Now delegates to NormalizationText
local function trim(s)
    return NormalizationText.trim(s)
end
 
--[[ Generates semantic annotations using #set parser function
   @param args - Template parameters
   @param args - Template parameters
   @param mappings - Property mappings: {["Property"] = "param"} or complex format
   @param mappings - Property mappings: {["Property"] = "param"} or complex format
Line 82: Line 80:
     -- Return result or empty string
     -- Return result or empty string
     return propertyCount > 0 and table.concat(result, "\n") or ""
     return propertyCount > 0 and table.concat(result, "\n") or ""
end
-- Renders a table using TemplateStructure and adds annotations
function p.renderWithSemantics(args, config, semanticMappings, semanticOptions)
    local TemplateStructure = require('Module:TemplateStructure')
    local renderedTable = TemplateStructure.render(args, config)
    local annotations = p.generateAnnotations(args, semanticMappings, semanticOptions)
    return renderedTable .. "\n" .. annotations
end
-- Allows templates to append annotations directly via transclusion
function p.appendToTemplate(frame)
    local args = frame.args
    local parent = frame:getParent()
    local parentArgs = parent and parent.args or {}
   
    -- Build mappings from numbered parameters
    local mappings = {}
    local i = 1
    while args["property" .. i] and args["param" .. i] do
        mappings[args["property" .. i]] = args["param" .. i]
        i = i + 1
    end
   
    -- Extract options and generate annotations
    local options = {
        visible = args.visible == "true",
        prefix = args.prefix or ""
    }
    return p.generateAnnotations(parentArgs, mappings, options)
end
end


Line 206: Line 174:
end
end


-- Enhanced version supporting complex mappings (fallback when mw.smw unavailable)
-- Enhanced fallback for mw.smw.set with complex mapping support.
function p.generateEnhancedAnnotations(args, mappings, options)
function p.generateEnhancedAnnotations(args, mappings, options)
     args = args or {}
     args = args or {}
Line 229: Line 197:
         local fullPropertyName = prefix .. property
         local fullPropertyName = prefix .. property
          
          
         if type(mapping) == "table" and mapping[1] then
         if type(mapping) == "string" then
            -- This is a direct array of values, not a mapping definition.
            -- Use these values directly.
            for _, value in ipairs(mapping) do
                processSimpleMapping(properties, fullPropertyName, value, transform[property], default[property])
            end
        elseif type(mapping) == "string" then
             -- Simple string mapping with case-insensitive lookup
             -- Simple string mapping with case-insensitive lookup
            -- Try exact match first, then lowercase
             local value = args[mapping] or args[mapping:lower()]
             local value = args[mapping] or args[mapping:lower()]
             processSimpleMapping(properties, fullPropertyName, value, transform[property], default[property])
             propertyCount = propertyCount + addSimplePropertyToResult(result,  
                fullPropertyName, value, transform[property], default[property])
         elseif type(mapping) == "table" then
         elseif type(mapping) == "table" then
             if mapping.param then
             if mapping.param then
Line 295: Line 257:
end
end


--[[ Sets semantic properties with native API or fallback
--[[ Sets semantic properties using the native mw.smw API.
  This is the primary function for setting semantic data.
   @param args - Template parameters
   @param args - Template parameters
   @param mappings - Property mappings in formats:
   @param mappings - Property mappings in formats:
Line 320: Line 283:
         local fullPropertyName = prefix .. property
         local fullPropertyName = prefix .. property
          
          
         if type(mapping) == "table" and mapping[1] then
         if type(mapping) == "string" then
            -- This is a direct array of values, not a mapping definition.
            -- Use these values directly.
            for _, value in ipairs(mapping) do
                processSimpleMapping(properties, fullPropertyName, value, transform[property], default[property])
            end
        elseif type(mapping) == "string" then
             -- Simple string mapping with case-insensitive lookup
             -- Simple string mapping with case-insensitive lookup
             -- Try exact match first, then lowercase
             -- Try exact match first, then lowercase