Difference between revisions of "Module:LuaExtractionDiscourseLayers"

From DigiVis
Jump to: navigation, search
m (Admin moved page Module:LuaExtractionDiscourseLayer2 to Module:LuaExtractionDiscourseLayers without leaving a redirect)
(No difference)

Revision as of 14:13, 14 June 2019

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

local p = {}
function _annotationMetadataExtract(text)
	if text == nil then
		return 'No text supplied as parameter'
	else
		text = text:gsub("<div class=\"smwpre\">", "")
		text = text:gsub("%^", "{")
		text = text:gsub("%°", "}")
		text = text:gsub("%Ӷ", "[")
		text = text:gsub("%Ӻ", "]")
		text = text:gsub("&#160;", " ")
		text = text:gsub("</div>", "")
		text = mw.text.decode(text)
		local decodedJson = mw.text.jsonDecode(text, mw.text.JSON_TRY_FIXING)
		return decodedJson
	end
end

function _xpathToSortableIndex(s)
  local result = ''
  for num in s:gmatch("%d+") do
    result = result .. " " .. num
  end
  return result
end

function p.ExtractDiscourseLayerContent(frame)
	
	-- get title from arguments
	local pagetitle = frame.args[1]
	pagetitle = 'Text:' .. pagetitle:gsub("Annotationen:", "")
	if pagetitle == nil then
		return 'No title supplied as parameter'
	else
		
		local lookup = {
			argumentation2 = "annotator-hl-blue",
			innovationsdiskurs2 = "annotator-hl-red",
			narrativ2 = "annotator-hl-lime",
			wissenschaftlichereferenz2 = "annotator-hl-yellow",
			beispiel3 = "annotator-hl-blueviolet",
			praemisse3 = "annotator-hl-orange",
			schlussfolgerung3 = "annotator-hl-cyan"
		}
		local title = mw.title.makeTitle('', pagetitle)
		local pagecontent = title:getContent()

		local query = '[[Annotation of::' .. pagetitle .. ']]|?=#|?AnnotationMetadata=#'
		local result = mw.smw.ask(query)
		local rv = ''
		local t = {}
		
		if result == nil then
			return 'No annotions found for page ' .. pagetitle
		end
		
		for i=1,#result do
			-- result[i][1] -> title of page
			-- result[i]['#'] -> metadata
			
			local metadata = result[i]['#']
			metadata = _annotationMetadataExtract(metadata)
			local start = metadata['ranges'][1]['start']
			local startoffset = metadata['ranges'][1]['startOffset']
			local category = metadata['category']
			category = category:lower()
			category = category:gsub("%d%_", "")
			category = category:gsub("ä", "ae")
			local selector = type(lookup[category]) == "function" and lookup[category]() or lookup[category] or "blubb"
			local prefix = '<div class="' .. "box-layer2 " .. selector .. '">'
			local suffix = "</div>"
			local prefix_title = '<span class="box-layer2-title' .. '">'
			local suffix_title = '</span>'
			rv = rv .. prefix .. prefix_title .. metadata['category'] .. suffix_title .. '<br>' .. "" .. metadata['quote'] .. suffix
			t[i] = _xpathToSortableIndex(start)
		end

		table.sort(t)

		-- local page = mw.title.makeTitle('', annotationspage)
		-- local content = page:getContent()

		-- Todo: sort annotations by position in text by the use of XPATH info in metadata
		-- local result = ""
		--for i, e in ipairs(t) do
		--	result = result .. e .. '<br>'
		--end
		return rv
	end
	
	return 'should not have reached this point'
end
return p