Module:TA4TK:myTest: Difference between revisions

From MaRDI portal
No edit summary
No edit summary
Line 34: Line 34:
end
end


local json = mw.text.jsonDecode(queryResults)
-- Check if the input is valid
    if not json or not json.results or not json.results.bindings then
    if type(queryResults) ~= "table" then
         return "Error: Failed to decode SPARQL results."
         return "Error: Input is not a table."
     end
     end
return queryResults['results']
 
    -- Initialize a results table
    local results = {}
 
    -- Iterate through the table
    for key, value in pairs(queryResults) do
        -- Example processing: format the key-value pairs
        table.insert(results, string.format("Key: %s, Value: %s", tostring(key), tostring(value)))
    end
 
    -- Concatenate results into a string for output
    return table.concat(results, "\n")
--return queryResults['results']
end
end



Revision as of 15:12, 3 January 2025

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

-- Main table to hold all functions

local sparql = require('SPARQL') -- Load the SPARQL binding

local p = {}

function p.hello()
	local str = "Hello World!"
	return str
end

function p.renderFormula()
    local formula = "\\sin x" -- LaTeX formula
end

function p.querySPARQL()
	-- Define the SPARQL endpoint
    local endpoint = "https://query.wikidata.org/"

    -- Define the SPARQL query
    local sparqlQuery = [[
        SELECT ?item ?itemLabel WHERE {
            ?item wdt:P31 wd:Q5;
                  rdfs:label "Albert Einstein"@en.
            SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
        } LIMIT 1
    ]]
	
	local queryResults = sparql.runQuery(sparqlQuery) -- Use the runQuery method

	-- nil is returned if Blazegraph did not return a valid response
	if queryResults == nil then
		return "No results found."
	end

	 -- Check if the input is valid
    if type(queryResults) ~= "table" then
        return "Error: Input is not a table."
    end

    -- Initialize a results table
    local results = {}

    -- Iterate through the table
    for key, value in pairs(queryResults) do
        -- Example processing: format the key-value pairs
        table.insert(results, string.format("Key: %s, Value: %s", tostring(key), tostring(value)))
    end

    -- Concatenate results into a string for output
    return table.concat(results, "\n")
	--return queryResults['results']
end



return p