Module:TA4TK:myTest: Difference between revisions
From MaRDI portal
No edit summary |
No edit summary Tag: Manual revert |
||
(104 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
local sparql = require('SPARQL') | local sparql = require('SPARQL') | ||
local mwHtml = require('mw.html') | local mwHtml = require('mw.html') | ||
local json = require("mw.text") | |||
local p = {} | local p = {} | ||
function p.getImageWithLegend(frame) | function p.getImageWithLegend(frame) | ||
local | local pidLocalImage = "P1088" -- Property ID for the local image | ||
local | local pidMediaLegend = "P648" -- Property ID for the (qualifier) media legend | ||
local | local qid = "Q4610" -- The target entity | ||
local width = "480px" | local width = "480px" | ||
local defaultLegend = "No legend available." | local defaultLegend = "No legend available." | ||
Line 16: | Line 17: | ||
-- Attempt to retrieve entity data | -- Attempt to retrieve entity data | ||
local entity = mw.wikibase.getEntity( | local entity = mw.wikibase.getEntity(qid) | ||
if not entity or not entity.claims[ | if not entity or not entity.claims[pidLocalImage] then | ||
return "No image found" | return "No image found" | ||
end | end | ||
local imageClaims = entity.claims[ | local imageClaims = entity.claims[pidLocalImage] | ||
-- Extract the first image filename from P1088 | -- Extract the first image filename from P1088 | ||
local imageFilename1 = imageClaims[1].mainsnak.datavalue.value | local imageFilename1 = imageClaims[1].mainsnak.datavalue.value | ||
Line 29: | Line 30: | ||
end | end | ||
imageLegend1 = imageClaims[1].qualifiers[ | imageLegend1 = imageClaims[1].qualifiers[pidMediaLegend][1].datavalue.value.text | ||
-- Extract the second image filename from P1088 | -- Extract the second image filename from P1088 | ||
Line 37: | Line 38: | ||
return string.format("[[File:%s|thumb|center|%s|%s]]", imageFilename1,width, imageLegend1) | return string.format("[[File:%s|thumb|center|%s|%s]]", imageFilename1,width, imageLegend1) | ||
else | else | ||
imageLegend2 = imageClaims[2].qualifiers[ | imageLegend2 = imageClaims[2].qualifiers[pidMediaLegend][1].datavalue.value.text | ||
end | end | ||
Line 45: | Line 46: | ||
end | end | ||
function convertJsonToTable(jsonResults) | function p.convertJsonToTable(jsonResults) | ||
local resultsTable = {} | local resultsTable = {} | ||
if jsonResults and jsonResults.results and jsonResults.results.bindings then | if jsonResults and jsonResults.results and jsonResults.results.bindings then | ||
Line 57: | Line 58: | ||
end | end | ||
end | end | ||
return resultsTable | |||
end | end | ||
Line 166: | Line 138: | ||
end | end | ||
local | local resultString = p.convertJsonToCommaSeparatedList(jsonResults) | ||
return resultString | |||
return | |||
end | end | ||
-- Function to build the list | -- Function to build the list | ||
function p. | function p.getFormulations(frame) | ||
-- Constructing the SPARQL query with dynamic entity target1 | -- Constructing the SPARQL query with dynamic entity target1 | ||
local sparqlQuery = [[ | local sparqlQuery = [[ | ||
SELECT ?Formula ?defining_formulation | |||
SELECT ? | # Q4610 refers to Electron Shuttling Model | ||
# P715 refers to contains | |||
# P29 refers to defining formulation | |||
} | WHERE { | ||
wd:Q4610 wdt:P715 ?IDFormula. | |||
?IDFormula rdfs:label ?Formula. | |||
?IDFormula wdt:P29 ?defining_formulation | |||
} | |||
]] | ]] | ||
Line 203: | Line 172: | ||
end | end | ||
local | --local testResults = p.convertJsonToCommaSeparatedList(jsonResults) | ||
--return testResults | |||
local resultTable = p.convertJsonToTable(jsonResults) | |||
-- return resultTable | |||
--return "<pre>" .. mw.text.nowiki(json.jsonEncode(resultTable)) .. "</pre>" | |||
local jsonString = mw.text.jsonEncode(jsonResults) | |||
-- return "<pre>" .. mw.text.nowiki(jsonString) .. "</pre>" | |||
return p.extractDefiningFormulations(jsonString) | |||
end | |||
local json = require("mw.text") -- Load the JSON library | |||
function p.extractDefiningFormulations(jsonString) | |||
local frame = mw.getCurrentFrame() | |||
local data = mw.text.jsonDecode(jsonString) -- Convert JSON string to Lua table | |||
if not data or not data.results or not data.results.bindings then | |||
return "Error: Invalid JSON structure" | |||
end | |||
local formulations = {} -- Table to store extracted values | |||
-- Loop through the bindings and collect "defining_formulation" values | |||
for _, item in ipairs(data.results.bindings) do | |||
if not item or not item.defining_formulation or not item.defining_formulation.value then | |||
return "Error: Missing item.defining_formulation.value" | |||
else | |||
local mathML = item.defining_formulation.value -- Get the MathML string | |||
local equationLabel = item.Formula.value | |||
-- Use pattern matching to find the alttext attribute | |||
local alttext = mathML:match('alttext="([^"]+)"') | |||
-- Pattern to match content after "\displaystyle " and before the last "}" | |||
local match = alttext:match("\\displaystyle%s*(.*)}%s*$") | |||
--table.insert(formulations, item.defining_formulation.value) | |||
local mathTag = frame:extensionTag{ | |||
name = "math", | |||
content = match | |||
} | |||
-- table.insert(formulations, "| " .. mathTag) -- Add row to the table | |||
table.insert(formulations, "| " .. equationLabel .. " || " .. mathTag) | |||
-- table.insert(formulations, match) | |||
end | |||
end | |||
-- Construct the Wikitext table | |||
local wikitextTable = "{| class='wikitable'\n" .. table.concat(formulations, "\n|-\n") .. "\n|}" | |||
return wikitextTable | |||
end | |||
function p.extractDefiningFormulationsWithQuantities(jsonString) | |||
local frame = mw.getCurrentFrame() | |||
local data = mw.text.jsonDecode(jsonString) -- Convert JSON string to Lua table | |||
if not data or not data.results or not data.results.bindings then | |||
return "Error: Invalid JSON structure" | |||
end | |||
local formulations = {} -- Table to store extracted values | |||
return | -- Loop through the bindings and collect "defining_formulation" values | ||
for _, item in ipairs(data.results.bindings) do | |||
if not item or not item.defining_formulation or not item.defining_formulation.value then | |||
return "Error: Missing item.defining_formulation.value" | |||
else | |||
local mathML = item.defining_formulation.value -- Get the MathML string | |||
local equationLabel = item.Formula.value | |||
-- Use pattern matching to find the alttext attribute | |||
local alttext = mathML:match('alttext="([^"]+)"') | |||
-- Pattern to match content after "\displaystyle " and before the last "}" | |||
local match = alttext:match("\\displaystyle%s*(.*)}%s*$") | |||
--table.insert(formulations, item.defining_formulation.value) | |||
local mathTag = frame:extensionTag{ | |||
name = "math", | |||
content = match | |||
} | |||
local quantityMathML = item.in_defining_formula.value | |||
-- Match the content inside alttext="{\displaystyle ... }" | |||
local alttext = string.match(quantityMathML, 'alttext="{\\displaystyle%s*(.-)%s*}"') | |||
local mathQuantity = frame:extensionTag{ | |||
name = "math", | |||
content = alttext | |||
} | |||
-- table.insert(formulations, "| " .. mathTag) -- Add row to the table | |||
table.insert(formulations, "| " .. equationLabel .. " || " .. mathTag) | |||
table.insert(formulations, "| " .. mathQuantity .. " || " .. "symbol represents:") | |||
end | |||
end | |||
-- Construct the Wikitext table | |||
local wikitextTable = "{| class='wikitable'\n" .. table.concat(formulations, "\n|-\n") .. "\n|}" | |||
return wikitextTable | |||
end | |||
function p.extractQuantities(frame) | |||
local pidInDefiningFormula = "P597" -- Property ID for in defining formula | |||
local pidSymbolRepresents = "P598" -- Property ID for the (qualifier) symbol represents | |||
local qid = "Q4577" -- The target entity | |||
-- Attempt to retrieve entity data | |||
local entity = mw.wikibase.getEntity(qid) | |||
if not entity or not entity.claims[pidInDefiningFormula] then | |||
return "No formulation found" | |||
end | |||
local imageClaims = entity.claims[pidInDefiningFormula] | |||
-- Extract the first image filename from P1088 | |||
local imageFilename1 = imageClaims[1].mainsnak.datavalue.value | |||
if not imageFilename1 then | |||
return "No valid formulation found" | |||
end | |||
quantity = imageClaims[1].qualifiers[pidSymbolRepresents][1].datavalue.value.text | |||
--return quantity | |||
-- return imageClaims[1].qualifiers | |||
-- return "<pre>" .. mw.text.nowiki(json.jsonEncode(imageClaims[1].qualifiers)) .. "</pre>" | |||
-- return "<pre>" .. mw.text.nowiki(json.jsonEncode( imageClaims[1].qualifiers.P598[1].datavalue.value.id)) .. "</pre>" | |||
-- Attempt to retrieve entity data | |||
quantityId = imageClaims[1].qualifiers.P598[1].datavalue.value.id | |||
local label = mw.wikibase.label(quantityId) | |||
return label | |||
end | end | ||
-- Function to build the list | |||
function p.getFormulationsWithQuantities(frame) | |||
-- Constructing the SPARQL query with dynamic entity target1 | |||
local sparqlQuery = [[ | |||
SELECT ?Formula ?defining_formulation ?in_defining_formula | |||
# Q4610 refers to Electron Shuttling Model | |||
# P715 refers to contains | |||
# P29 refers to defining formula | |||
# P597 refers to in defining formula | |||
WHERE { | |||
wd:Q4610 wdt:P715 ?IDFormula. | |||
?IDFormula rdfs:label ?Formula. | |||
?IDFormula wdt:P29 ?defining_formulation. | |||
?IDFormula wdt:P597 ?in_defining_formula | |||
} | |||
]] | |||
-- Executing the SPARQL query and retrieving results in JSON format | |||
local jsonResults = sparql.runQuery(sparqlQuery) | |||
-- Handle error in SPARQL query execution | |||
if jsonResults and jsonResults.error then | |||
mw.log("Error in SPARQL query: " .. tostring(jsonResults.error)) | |||
return nil | |||
end | |||
if not jsonResults then | |||
return "Could not fetch data." | |||
end | |||
--local testResults = p.convertJsonToCommaSeparatedList(jsonResults) | |||
--return testResults | |||
local resultTable = p.convertJsonToTable(jsonResults) | |||
--return resultTable | |||
--return "<pre>" .. mw.text.nowiki(json.jsonEncode(resultTable)) .. "</pre>" | |||
local jsonString = mw.text.jsonEncode(jsonResults) | |||
-- return "<pre>" .. mw.text.nowiki(jsonString) .. "</pre>" | |||
return p.extractDefiningFormulationsWithQuantities(jsonString) | |||
end | |||
-- Function to build the description | -- Function to build the description |
Latest revision as of 14:37, 13 February 2025
Documentation for this module may be created at Module:TA4TK:myTest/doc
-- Required modules for SPARQL queries and HTML table generation
local sparql = require('SPARQL')
local mwHtml = require('mw.html')
local json = require("mw.text")
local p = {}
function p.getImageWithLegend(frame)
local pidLocalImage = "P1088" -- Property ID for the local image
local pidMediaLegend = "P648" -- Property ID for the (qualifier) media legend
local qid = "Q4610" -- The target entity
local width = "480px"
local defaultLegend = "No legend available."
local imageLegend1 = "No legend available."
local imageLegend2 = "No legend available."
-- Attempt to retrieve entity data
local entity = mw.wikibase.getEntity(qid)
if not entity or not entity.claims[pidLocalImage] then
return "No image found"
end
local imageClaims = entity.claims[pidLocalImage]
-- Extract the first image filename from P1088
local imageFilename1 = imageClaims[1].mainsnak.datavalue.value
if not imageFilename1 then
return "No valid image found"
end
imageLegend1 = imageClaims[1].qualifiers[pidMediaLegend][1].datavalue.value.text
-- Extract the second image filename from P1088
local imageFilename2 = imageClaims[2].mainsnak.datavalue.value
if not imageFilename2 then
--return one image
return string.format("[[File:%s|thumb|center|%s|%s]]", imageFilename1,width, imageLegend1)
else
imageLegend2 = imageClaims[2].qualifiers[pidMediaLegend][1].datavalue.value.text
end
--return two images
return string.format("[[File:%s|thumb|left|%s|%s]] [[File:%s|thumb|center|%s|%s]]", imageFilename1, width, imageLegend1, imageFilename2, width, imageLegend2)
end
function p.convertJsonToTable(jsonResults)
local resultsTable = {}
if jsonResults and jsonResults.results and jsonResults.results.bindings then
local bindings = jsonResults.results.bindings
for j=0, #bindings do
local row = {}
for key, value in pairs(bindings[j]) do
table.insert(row, value.value)
end
table.insert(resultsTable, row)
end
end
return resultsTable
end
function p.hello()
local str = "Hello World!"
return str
end
function p.renderFormula()
return mw.getCurrentFrame():extensionTag{
name="math",
content= "\\sin x",
}
end
-- Function to convert JSON results into a comma-separated string
function p.convertJsonToCommaSeparatedList(jsonResults)
local resultsString = ""
if jsonResults and jsonResults.results and jsonResults.results.bindings then
local bindings = jsonResults.results.bindings
for i = 0, #bindings do
local binding = bindings[i]
if binding.valueLabel and binding.valueLabel.value then
if resultsString ~= "" then
resultsString = resultsString .. ", "
end
local name = binding.valueLabel.value
if string.find(name, "https://") then
name = "Unnamed task"
end
local link = binding.value.value
link = link:gsub("entity/Q", "wiki/Task:")
local nameAndLink = "[" .. link .. " " .. name .. "]"
resultsString = resultsString .. nameAndLink
end
end
end
return resultsString
end
-- Function to build the list
function p.buildList(frame)
-- Retrieve target1 from frame arguments or return error message if not set
local target1 = frame.args[1]
if not target1 or target1 == '' then
return "No ID given"
end
-- Constructing the SPARQL query with dynamic entity target1
local sparqlQuery = [[
PREFIX target1: <https://staging.mardi4nfdi.org/entity/]] .. target1 .. [[>
SELECT ?value ?valueLabel WHERE {
?value wdt:P715 target1:.
target1: rdfs:label ?valueLabel
}
]]
-- Executing the SPARQL query and retrieving results in JSON format
local jsonResults = sparql.runQuery(sparqlQuery)
-- Handle error in SPARQL query execution
if jsonResults and jsonResults.error then
mw.log("Error in SPARQL query: " .. tostring(jsonResults.error))
return nil
end
if not jsonResults then
return "Could not fetch data."
end
local resultString = p.convertJsonToCommaSeparatedList(jsonResults)
return resultString
end
-- Function to build the list
function p.getFormulations(frame)
-- Constructing the SPARQL query with dynamic entity target1
local sparqlQuery = [[
SELECT ?Formula ?defining_formulation
# Q4610 refers to Electron Shuttling Model
# P715 refers to contains
# P29 refers to defining formulation
WHERE {
wd:Q4610 wdt:P715 ?IDFormula.
?IDFormula rdfs:label ?Formula.
?IDFormula wdt:P29 ?defining_formulation
}
]]
-- Executing the SPARQL query and retrieving results in JSON format
local jsonResults = sparql.runQuery(sparqlQuery)
-- Handle error in SPARQL query execution
if jsonResults and jsonResults.error then
mw.log("Error in SPARQL query: " .. tostring(jsonResults.error))
return nil
end
if not jsonResults then
return "Could not fetch data."
end
--local testResults = p.convertJsonToCommaSeparatedList(jsonResults)
--return testResults
local resultTable = p.convertJsonToTable(jsonResults)
-- return resultTable
--return "<pre>" .. mw.text.nowiki(json.jsonEncode(resultTable)) .. "</pre>"
local jsonString = mw.text.jsonEncode(jsonResults)
-- return "<pre>" .. mw.text.nowiki(jsonString) .. "</pre>"
return p.extractDefiningFormulations(jsonString)
end
local json = require("mw.text") -- Load the JSON library
function p.extractDefiningFormulations(jsonString)
local frame = mw.getCurrentFrame()
local data = mw.text.jsonDecode(jsonString) -- Convert JSON string to Lua table
if not data or not data.results or not data.results.bindings then
return "Error: Invalid JSON structure"
end
local formulations = {} -- Table to store extracted values
-- Loop through the bindings and collect "defining_formulation" values
for _, item in ipairs(data.results.bindings) do
if not item or not item.defining_formulation or not item.defining_formulation.value then
return "Error: Missing item.defining_formulation.value"
else
local mathML = item.defining_formulation.value -- Get the MathML string
local equationLabel = item.Formula.value
-- Use pattern matching to find the alttext attribute
local alttext = mathML:match('alttext="([^"]+)"')
-- Pattern to match content after "\displaystyle " and before the last "}"
local match = alttext:match("\\displaystyle%s*(.*)}%s*$")
--table.insert(formulations, item.defining_formulation.value)
local mathTag = frame:extensionTag{
name = "math",
content = match
}
-- table.insert(formulations, "| " .. mathTag) -- Add row to the table
table.insert(formulations, "| " .. equationLabel .. " || " .. mathTag)
-- table.insert(formulations, match)
end
end
-- Construct the Wikitext table
local wikitextTable = "{| class='wikitable'\n" .. table.concat(formulations, "\n|-\n") .. "\n|}"
return wikitextTable
end
function p.extractDefiningFormulationsWithQuantities(jsonString)
local frame = mw.getCurrentFrame()
local data = mw.text.jsonDecode(jsonString) -- Convert JSON string to Lua table
if not data or not data.results or not data.results.bindings then
return "Error: Invalid JSON structure"
end
local formulations = {} -- Table to store extracted values
-- Loop through the bindings and collect "defining_formulation" values
for _, item in ipairs(data.results.bindings) do
if not item or not item.defining_formulation or not item.defining_formulation.value then
return "Error: Missing item.defining_formulation.value"
else
local mathML = item.defining_formulation.value -- Get the MathML string
local equationLabel = item.Formula.value
-- Use pattern matching to find the alttext attribute
local alttext = mathML:match('alttext="([^"]+)"')
-- Pattern to match content after "\displaystyle " and before the last "}"
local match = alttext:match("\\displaystyle%s*(.*)}%s*$")
--table.insert(formulations, item.defining_formulation.value)
local mathTag = frame:extensionTag{
name = "math",
content = match
}
local quantityMathML = item.in_defining_formula.value
-- Match the content inside alttext="{\displaystyle ... }"
local alttext = string.match(quantityMathML, 'alttext="{\\displaystyle%s*(.-)%s*}"')
local mathQuantity = frame:extensionTag{
name = "math",
content = alttext
}
-- table.insert(formulations, "| " .. mathTag) -- Add row to the table
table.insert(formulations, "| " .. equationLabel .. " || " .. mathTag)
table.insert(formulations, "| " .. mathQuantity .. " || " .. "symbol represents:")
end
end
-- Construct the Wikitext table
local wikitextTable = "{| class='wikitable'\n" .. table.concat(formulations, "\n|-\n") .. "\n|}"
return wikitextTable
end
function p.extractQuantities(frame)
local pidInDefiningFormula = "P597" -- Property ID for in defining formula
local pidSymbolRepresents = "P598" -- Property ID for the (qualifier) symbol represents
local qid = "Q4577" -- The target entity
-- Attempt to retrieve entity data
local entity = mw.wikibase.getEntity(qid)
if not entity or not entity.claims[pidInDefiningFormula] then
return "No formulation found"
end
local imageClaims = entity.claims[pidInDefiningFormula]
-- Extract the first image filename from P1088
local imageFilename1 = imageClaims[1].mainsnak.datavalue.value
if not imageFilename1 then
return "No valid formulation found"
end
quantity = imageClaims[1].qualifiers[pidSymbolRepresents][1].datavalue.value.text
--return quantity
-- return imageClaims[1].qualifiers
-- return "<pre>" .. mw.text.nowiki(json.jsonEncode(imageClaims[1].qualifiers)) .. "</pre>"
-- return "<pre>" .. mw.text.nowiki(json.jsonEncode( imageClaims[1].qualifiers.P598[1].datavalue.value.id)) .. "</pre>"
-- Attempt to retrieve entity data
quantityId = imageClaims[1].qualifiers.P598[1].datavalue.value.id
local label = mw.wikibase.label(quantityId)
return label
end
-- Function to build the list
function p.getFormulationsWithQuantities(frame)
-- Constructing the SPARQL query with dynamic entity target1
local sparqlQuery = [[
SELECT ?Formula ?defining_formulation ?in_defining_formula
# Q4610 refers to Electron Shuttling Model
# P715 refers to contains
# P29 refers to defining formula
# P597 refers to in defining formula
WHERE {
wd:Q4610 wdt:P715 ?IDFormula.
?IDFormula rdfs:label ?Formula.
?IDFormula wdt:P29 ?defining_formulation.
?IDFormula wdt:P597 ?in_defining_formula
}
]]
-- Executing the SPARQL query and retrieving results in JSON format
local jsonResults = sparql.runQuery(sparqlQuery)
-- Handle error in SPARQL query execution
if jsonResults and jsonResults.error then
mw.log("Error in SPARQL query: " .. tostring(jsonResults.error))
return nil
end
if not jsonResults then
return "Could not fetch data."
end
--local testResults = p.convertJsonToCommaSeparatedList(jsonResults)
--return testResults
local resultTable = p.convertJsonToTable(jsonResults)
--return resultTable
--return "<pre>" .. mw.text.nowiki(json.jsonEncode(resultTable)) .. "</pre>"
local jsonString = mw.text.jsonEncode(jsonResults)
-- return "<pre>" .. mw.text.nowiki(jsonString) .. "</pre>"
return p.extractDefiningFormulationsWithQuantities(jsonString)
end
-- Function to build the description
function p.buildDescription(frame)
-- Retrieve target1 from frame arguments or return error message if not set
local target1 = frame.args[1]
if not target1 or target1 == '' then
return "No ID given"
end
-- Constructing the SPARQL query with dynamic entity target1
local sparqlQuery = [[
SELECT ?Description
WHERE {
wd:Q4610 wdt:P896 ?Description.
}
]]
-- Executing the SPARQL query and retrieving results in JSON format
local jsonResults = sparql.runQuery(sparqlQuery)
return jsonResults['results']['bindings'][0]['Description']['value']
end
function p.getImage(frame)
local entityId = "Q4610" -- The target entity
local propertyId = "P1088" -- Property assumed to store the image
local width = "480px" -- Desired image width
-- Attempt to retrieve entity data
local entity = mw.wikibase.getEntity(entityId)
if not entity or not entity.claims[propertyId] then
return "No image found"
end
-- Extract the first image filename from P1088
local imageFilename1 = entity.claims[propertyId][1].mainsnak.datavalue.value
if not imageFilename1 then
return "No valid image found"
end
-- Extract the second image filename from P1088
local imageFilename2 = entity.claims[propertyId][2].mainsnak.datavalue.value
if not imageFilename2 then
--return one image
return string.format("[[File:%s|thumb|center|%s]]", imageFilename1, width)
else
--return two images
return string.format("[[File:%s|left|%s]] [[File:%s|center|%s]] \n \n \n ", imageFilename1, width, imageFilename2, width)
end
end
return p