Modul:Reference score: Forskjell mellom sideversjoner

Fra Wikisida.no
Hopp til navigering Hopp til søk
Ingen redigeringsforklaring
(moved to conf)
Linje 1: Linje 1:
local i18n = mw.loadData( 'Module:References/i18n' )
local i18n = mw.loadData( 'Module:References/i18n' )
local conf = mw.loadData( 'Module:References/conf' )
local conf = mw.loadData( 'Module:References/conf' )

local highScore = 5
local midScore = 15
local lowScore = 25
local maxRefs = 4


local h = {}
local h = {}
Linje 28: Linje 23:


function h.scoreLanguage( claims )
function h.scoreLanguage( claims )
local keep = lowScore
local keep = conf.lowScore
for _,v in ipairs( claims ) do
for _,v in ipairs( claims ) do
if v.snaktype == 'value'
if v.snaktype == 'value'
Linje 44: Linje 39:


function h.scoreDomain( claims )
function h.scoreDomain( claims )
local keep = lowScore
local keep = conf.lowScore
for _,v in ipairs( claims ) do
for _,v in ipairs( claims ) do
if v.snaktype == 'value'
if v.snaktype == 'value'
Linje 62: Linje 57:


function h.scoreEntity( claims )
function h.scoreEntity( claims )
local keep = lowScore
local keep = conf.lowScore
for _,v in ipairs(claims) do
for _,v in ipairs(claims) do
if v.snaktype == 'value'
if v.snaktype == 'value'
Linje 78: Linje 73:


function h.scoreProperty( claims )
function h.scoreProperty( claims )
local keep = lowScore
local keep = conf.lowScore
for _,v in ipairs(claims) do
for _,v in ipairs(claims) do
-- strictly speaking this could be dropped as all should be equal
-- strictly speaking this could be dropped as all should be equal
Linje 96: Linje 91:
-- make sure we have a continuous target list
-- make sure we have a continuous target list
local t = {}
local t = {}
for i=1,lowScore do
for i=1,conf.lowScore do
t[i] = false
t[i] = false
end
end
Linje 111: Linje 106:
-- only process included (ie not excluded) references
-- only process included (ie not excluded) references
if not exclude then
if not exclude then
local keep = lowScore
local keep = conf.lowScore
-- try language of "title"
-- try language of "title"
if v.snaks.P1476 then
if v.snaks.P1476 then
Linje 151: Linje 146:
-- =mw.dumpObject(p.compact(p.score(mw.wikibase.getAllStatements('Q40826', 'P569')[1].references)))
-- =mw.dumpObject(p.compact(p.score(mw.wikibase.getAllStatements('Q40826', 'P569')[1].references)))
function h.compact( list, limit )
function h.compact( list, limit )
limit = limit or maxRefs
limit = limit or conf.maxRefs
local t = {}
local t = {}
local counter = 0
local counter = 0

Sideversjonen fra 28. aug. 2019 kl. 16:02

Dokumentasjon for denne modulen kan opprettes på Modul:Reference score/dok

local i18n = mw.loadData( 'Module:References/i18n' )
local conf = mw.loadData( 'Module:References/conf' )

local h = {}

-- Can be tested in the console with
-- `=p.categories()`
-- `=p.categories(0)`
-- `=p.categories(1)`
-- `=p.categories(2)`
function h.categories( num )
	local t = {}
	if num then
		table.insert( t, mw.message.newRawMessage( i18n['category-pages-using-references-from-statement'] ):plain() )
		if num == 1 then
			table.insert( t, mw.message.newRawMessage( i18n['category-pages-using-single-reference-from-statement'] ):plain() )
		elseif num > 1 then
			table.insert( t, mw.message.newRawMessage( i18n[string.format('category-pages-using-%d-references-from-statement', num )] ):plain() )
		end
	end
	return t
end

function h.scoreLanguage( claims )
	local keep = conf.lowScore
	for _,v in ipairs( claims ) do
		if v.snaktype == 'value'
				and v.datatype == 'monolingualtext'
				and v.datavalue.type == 'monolingualtext' then
			local score = conf.languageScore[v.datavalue.value.language]
			if score then
				-- note that higher number means lower prority
				keep = (keep < score) and keep or score
			end
		end
	end
	return keep
end

function h.scoreDomain( claims )
	local keep = conf.lowScore
	for _,v in ipairs( claims ) do
		if v.snaktype == 'value'
				and v.datatype == 'url'
				and v.datavalue.type == 'string' then
			local uri = mw.uri.new( v.datavalue.value )
			local root = string.match( uri.host, '%.([^.]+)$' )
			local score = conf.domainScore[root]
			if score then
				-- note that higher number means lower prority
				keep = (keep < score) and keep or score
			end
		end
	end
	return keep
end

function h.scoreEntity( claims )
	local keep = conf.lowScore
	for _,v in ipairs(claims) do
		if v.snaktype == 'value'
				and v.datatype == 'wikibase-item'
				and v.datavalue.type == 'wikibase-entityid' then
			local score = conf.entityScore[v.datavalue.value.id]
			if score then
				-- note that higher number means lower prority
				keep = (keep < score) and keep or score
			end
		end
	end
	return keep
end

function h.scoreProperty( claims )
	local keep = conf.lowScore
	for _,v in ipairs(claims) do
		-- strictly speaking this could be dropped as all should be equal
		local score = conf.propertyScore[v.property]
		if score then
			-- note that higher number means lower prority
			keep = (keep < score) and keep or score
		end
	end
	return keep
end

-- Can be tested in the console with
-- `=p.prioritize(mw.wikibase.getAllStatements('Q20', 'P31')[2].references)`
-- `=p.prioritize(mw.wikibase.getAllStatements('Q66363708', 'P31')[2].references)`
function h.score( list )
	-- make sure we have a continuous target list
	local t = {}
	for i=1,conf.lowScore do
		t[i] = false
	end
	-- loop over the source list
	for _,v in ipairs( list ) do
		-- is the reference excluded?
		local exclude = false
		for _,w in ipairs( conf.exclude ) do
			if v.snaks and v.snaks[w] then
				exclude = true
				break
			end
		end
		-- only process included (ie not excluded) references
		if not exclude then
			local keep = conf.lowScore
			-- try language of "title"
			if v.snaks.P1476 then
				local score = h.scoreLanguage(v.snaks.P1476)
				if score then
					-- note that higher number means lower prority
					keep = (keep < score) and keep or score
				end
			end
			-- try root domain of "reference url"
			if v.snaks.P854 then
				local score = h.scoreDomain(v.snaks.P854)
				if score then
					-- note that higher number means lower prority
					keep = (keep < score) and keep or score
				end
			end
			-- try reference to entity
			for _,w in pairs(v.snaks) do
				local score = h.scoreEntity( w )
				if score then
					-- note that higher number means lower prority
					keep = (keep < score) and keep or score
				end
			end
			-- some properties that usually imply somewhat quality
			for _,w in pairs(v.snaks) do
				local score = h.scoreProperty( w )
				-- note that higher number means lower prority
				keep = (keep < score) and keep or score
			end
			table.insert( t, keep, v )
		end
	end
	return t
end

-- Can be somewhat tested by
-- =mw.dumpObject(p.compact(p.score(mw.wikibase.getAllStatements('Q40826', 'P569')[1].references)))
function h.compact( list, limit )
	limit = limit or conf.maxRefs
	local t = {}
	local counter = 0
	for _,v in ipairs( list ) do
		if v then
			counter = counter + 1
			if limit and counter <= limit then
				table.insert( t, v )
			elseif not limit then
				table.insert( t, v )
			end
		end
	end
	return t
end

function h.render( frame, list )
	local scored = h.score( list or {} )
	local compacted = h.compact( scored )
	local wiki = ''
	local hits = 0
	for i,v in ipairs( compacted ) do
		hits = hits + 1
		-- hash will merge similar entries
		wiki = wiki .. frame:extensionTag( 'ref', mw.wikibase.formatValues( v.snaks ), { name = v.hash } )
	end
	for _,v in ipairs( h.categories( hits ) ) do
		wiki = wiki .. mw.ustring.format('[[Category:%s]]', v )
	end
	return wiki
end

return h