Module:Process ask query results 2: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| (3 intermediate revisions by the same user not shown) | |||
| Line 14: | Line 14: | ||
local property_names_array = string_to_array(property_names_arg, ",") | local property_names_array = string_to_array(property_names_arg, ",") | ||
local source_page_positions = {} | |||
-- build a table of the form {index: {page name, start pos, end pos}, ...} where "start pos" and "end pos" are the positions in the | |||
-- in the full query response string of the starting and ending characters of the page name | |||
local i = 1 | |||
local j = 1 | |||
while i <= #full_query_arg do | |||
ch1, ch2 = mw.ustring.find(full_query_arg, "__source_page_", i) | |||
if ch1 ~= nil then | |||
source_page_positions[j] = {ch1, ch2} | |||
j = j + 1 | |||
i = ch2 + 1 | |||
else | |||
i = #full_query_arg + 1 | |||
end | |||
end | |||
--[[ print it out so that I check whether it was done correctly | |||
local output_string_1 = "<ul>" | |||
for i, v in ipairs(source_page_positions) do | |||
output_string_1 = output_string_1 .. "<li>" .. i .. ": {" .. v[1] .. ", " .. v[2] .. "}" .. "</li>" | |||
end | |||
output_string_1 = output_string_1 .. "</ul>" | |||
]] | |||
-- To find the substring of full_query_arg associated with each source, we need to group the end position of source page indentifier with beginning position | |||
-- of the following source page indentifier, so that we can extract the substring following each page name. In the subtable for each page name, we are going | |||
-- to add the starting position of the following page name. | |||
-- | |||
-- | |||
-- | |||
I = #source_page_positions | |||
for i = 1, I do | |||
-- we should check that the table elements are not nil before attempting the assignment | |||
if i < I then | |||
source_page_positions[i][3] = source_page_positions[i + 1][1] - 1 | |||
else | |||
source_page_positions[i][3] = #full_query_arg | |||
end | |||
end | |||
-- check to see that we have partioned the entire string | |||
total_characters = 0 | |||
for i, v in ipairs(source_page_positions) do | |||
total_characters = total_characters + v[3] - v[1] + 1 | |||
end | |||
if total_characters ~= #full_query_arg then | |||
--table.insert(my_log, "Some part of the query response string was not associated with a page name when the string was partitioned.") | |||
else | |||
--print("full partition of full_query_arg was achieved.") | |||
end | |||
full_query_table = {} | |||
for i, positions in ipairs(source_page_positions) do | |||
substring = mw.ustring.sub(full_query_arg, positions[1], positions[3]) | |||
property_positions = {} | |||
temp_subtable = {} | |||
char1, char2 = 0 | |||
for j, name in ipairs(property_names_array) do | |||
ch1, ch2 = mw.ustring.find(substring, name) | |||
table.insert(property_positions, {ch1, ch2}) | |||
end | |||
--print("length of property_positions = " .. #property_positions) | |||
table.sort(property_positions, function(a, b) return a[1] < b[1] end) | |||
K = #property_positions | |||
for k = 1, K do | |||
-- we should check that the table elements are not nil before attempting the assignment | |||
if k < K then | |||
property_positions[k][3] = property_positions[k + 1][1] - 1 | |||
else | |||
property_positions[k][3] = #substring | |||
end | |||
end | |||
for k = 1, K do | |||
temp_subtable[string.sub(substring, property_positions[k][1], property_positions[k][2])] = string.sub(substring, property_positions[k][2] + 1, property_positions[k][3]) | |||
end | |||
full_query_table[i] = temp_subtable | |||
end | |||
--[[ | |||
local output_string_2 = "<ul>" | |||
for i, subtbl in ipairs(full_query_table) do | |||
output_string_2 = output_string_2 .. "<li>This is for " .. i .. ": " .. "<ul>" | |||
for j, v in pairs(subtbl) do | |||
output_string_2 = output_string_2 .. "<li>" .. j .. ": " .. v .. "</li>" | |||
end | |||
output_string_2 = output_string_2 .. "</ul></li>" | |||
end | |||
output_string_2 = output_string_2 .. "</ul>" | |||
]] | |||
Latest revision as of 00:16, 11 December 2025
Documentation for this module may be created at Module:Process ask query results 2/doc
local p = {}
-- this code was developed in plainlist_processing_1_b.lua
function p.processData(frame)
local page_names_query_arg = frame.args["page_names_query"]
local full_query_arg = frame.args["full_query"]
local property_names_arg = frame.args["property_names_list"]
require("Module:String to array")
local page_names_array = string_to_array(page_names_query_arg, "#")
local property_names_array = string_to_array(property_names_arg, ",")
local source_page_positions = {}
-- build a table of the form {index: {page name, start pos, end pos}, ...} where "start pos" and "end pos" are the positions in the
-- in the full query response string of the starting and ending characters of the page name
local i = 1
local j = 1
while i <= #full_query_arg do
ch1, ch2 = mw.ustring.find(full_query_arg, "__source_page_", i)
if ch1 ~= nil then
source_page_positions[j] = {ch1, ch2}
j = j + 1
i = ch2 + 1
else
i = #full_query_arg + 1
end
end
--[[ print it out so that I check whether it was done correctly
local output_string_1 = "<ul>"
for i, v in ipairs(source_page_positions) do
output_string_1 = output_string_1 .. "<li>" .. i .. ": {" .. v[1] .. ", " .. v[2] .. "}" .. "</li>"
end
output_string_1 = output_string_1 .. "</ul>"
]]
-- To find the substring of full_query_arg associated with each source, we need to group the end position of source page indentifier with beginning position
-- of the following source page indentifier, so that we can extract the substring following each page name. In the subtable for each page name, we are going
-- to add the starting position of the following page name.
--
--
--
I = #source_page_positions
for i = 1, I do
-- we should check that the table elements are not nil before attempting the assignment
if i < I then
source_page_positions[i][3] = source_page_positions[i + 1][1] - 1
else
source_page_positions[i][3] = #full_query_arg
end
end
-- check to see that we have partioned the entire string
total_characters = 0
for i, v in ipairs(source_page_positions) do
total_characters = total_characters + v[3] - v[1] + 1
end
if total_characters ~= #full_query_arg then
--table.insert(my_log, "Some part of the query response string was not associated with a page name when the string was partitioned.")
else
--print("full partition of full_query_arg was achieved.")
end
full_query_table = {}
for i, positions in ipairs(source_page_positions) do
substring = mw.ustring.sub(full_query_arg, positions[1], positions[3])
property_positions = {}
temp_subtable = {}
char1, char2 = 0
for j, name in ipairs(property_names_array) do
ch1, ch2 = mw.ustring.find(substring, name)
table.insert(property_positions, {ch1, ch2})
end
--print("length of property_positions = " .. #property_positions)
table.sort(property_positions, function(a, b) return a[1] < b[1] end)
K = #property_positions
for k = 1, K do
-- we should check that the table elements are not nil before attempting the assignment
if k < K then
property_positions[k][3] = property_positions[k + 1][1] - 1
else
property_positions[k][3] = #substring
end
end
for k = 1, K do
temp_subtable[string.sub(substring, property_positions[k][1], property_positions[k][2])] = string.sub(substring, property_positions[k][2] + 1, property_positions[k][3])
end
full_query_table[i] = temp_subtable
end
--[[
local output_string_2 = "<ul>"
for i, subtbl in ipairs(full_query_table) do
output_string_2 = output_string_2 .. "<li>This is for " .. i .. ": " .. "<ul>"
for j, v in pairs(subtbl) do
output_string_2 = output_string_2 .. "<li>" .. j .. ": " .. v .. "</li>"
end
output_string_2 = output_string_2 .. "</ul></li>"
end
output_string_2 = output_string_2 .. "</ul>"
]]
return "<ul><li>" .. type(page_names_array) .. "</li><li>" .. type(property_names_array) .. "</li><li>" .. property_names_arg .. "</li></ul>"
end
return p