Modul:Reference score: Forskjell mellom sideversjoner
Hopp til navigering
Hopp til søk
moved to conf |
Ingen redigeringsforklaring |
||
| Linje 1: | Linje 1: | ||
--@table for localization | |||
local i18n = mw.loadData( 'Module:References/i18n' ) | local i18n = mw.loadData( 'Module:References/i18n' ) | ||
-- @table for configuration | |||
local conf = mw.loadData( 'Module:References/conf' ) | local conf = mw.loadData( 'Module:References/conf' ) | ||
-- @table for export | |||
local h = {} | local h = {} | ||
-- | --- Make a list of categories. | ||
- | -- Note this probably should be moved out of this module. | ||
-- | -- @tparam number num of references | ||
-- | -- @treturn table of strings | ||
-- | function h.makeCategories( num ) | ||
function h. | |||
local t = {} | local t = {} | ||
if num then | if num then | ||
| Linje 22: | Linje 25: | ||
end | end | ||
--- Generate a language score for the claims. | |||
-- @tparam table claims to process | |||
-- @treturn number best score | |||
function h.scoreLanguage( claims ) | function h.scoreLanguage( claims ) | ||
local keep = conf.lowScore | local keep = conf.lowScore | ||
| Linje 38: | Linje 44: | ||
end | end | ||
--- Generate a root domain score for the claims. | |||
-- @tparam table claims to process | |||
-- @treturn number best score | |||
function h.scoreDomain( claims ) | function h.scoreDomain( claims ) | ||
local keep = conf.lowScore | local keep = conf.lowScore | ||
| Linje 56: | Linje 65: | ||
end | end | ||
--- Generate an entity score for the claims. | |||
-- @tparam table claims to process | |||
-- @treturn number best score | |||
function h.scoreEntity( claims ) | function h.scoreEntity( claims ) | ||
local keep = conf.lowScore | local keep = conf.lowScore | ||
| Linje 72: | Linje 84: | ||
end | end | ||
--- Generate a property score for the claims. | |||
-- @tparam table claims to process | |||
-- @treturn number best score | |||
function h.scoreProperty( claims ) | function h.scoreProperty( claims ) | ||
local keep = conf.lowScore | local keep = conf.lowScore | ||
| Linje 85: | Linje 100: | ||
end | end | ||
-- | --- Generate scores for the references. | ||
-- | -- Note that the generated list is sparse, and is using false as marker. | ||
-- | -- @tparam table list to process | ||
-- @treturn table of references | |||
function h.score( list ) | function h.score( list ) | ||
-- make sure we have a continuous target list | -- make sure we have a continuous target list | ||
| Linje 143: | Linje 159: | ||
end | end | ||
-- | --- Compact the sparse list. | ||
-- = | -- Note that the input list is sparse, and using false as marker. | ||
-- @tparam table list of any | |||
-- @tparam[limit=conf.maxRefs] nil|number limit for truncation of list | |||
-- @treturn table | |||
function h.compact( list, limit ) | function h.compact( list, limit ) | ||
limit = limit or conf.maxRefs | limit = limit or conf.maxRefs | ||
| Linje 162: | Linje 181: | ||
end | end | ||
--- Render references. | |||
-- @tparam table frame | |||
-- @tparam table list of references | |||
-- @treturn string | |||
function h.render( frame, list ) | function h.render( frame, list ) | ||
local scored = h.score( list or {} ) | local scored = h.score( list or {} ) | ||
| Linje 172: | Linje 195: | ||
wiki = wiki .. frame:extensionTag( 'ref', mw.wikibase.formatValues( v.snaks ), { name = v.hash } ) | wiki = wiki .. frame:extensionTag( 'ref', mw.wikibase.formatValues( v.snaks ), { name = v.hash } ) | ||
end | end | ||
for _,v in ipairs( h. | for _,v in ipairs( h.makeCategories( hits ) ) do | ||
wiki = wiki .. mw.ustring.format('[[Category:%s]]', v ) | wiki = wiki .. mw.ustring.format('[[Category:%s]]', v ) | ||
end | end | ||
Sideversjonen fra 28. aug. 2019 kl. 14:28
Dokumentasjon for denne modulen kan opprettes på Modul:Reference score/dok
--@table for localization
local i18n = mw.loadData( 'Module:References/i18n' )
-- @table for configuration
local conf = mw.loadData( 'Module:References/conf' )
-- @table for export
local h = {}
--- Make a list of categories.
-- Note this probably should be moved out of this module.
-- @tparam number num of references
-- @treturn table of strings
function h.makeCategories( 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
--- Generate a language score for the claims.
-- @tparam table claims to process
-- @treturn number best score
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
--- Generate a root domain score for the claims.
-- @tparam table claims to process
-- @treturn number best score
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
--- Generate an entity score for the claims.
-- @tparam table claims to process
-- @treturn number best score
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
--- Generate a property score for the claims.
-- @tparam table claims to process
-- @treturn number best score
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
--- Generate scores for the references.
-- Note that the generated list is sparse, and is using false as marker.
-- @tparam table list to process
-- @treturn table of 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
--- Compact the sparse list.
-- Note that the input list is sparse, and using false as marker.
-- @tparam table list of any
-- @tparam[limit=conf.maxRefs] nil|number limit for truncation of list
-- @treturn table
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
--- Render references.
-- @tparam table frame
-- @tparam table list of references
-- @treturn string
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.makeCategories( hits ) ) do
wiki = wiki .. mw.ustring.format('[[Category:%s]]', v )
end
return wiki
end
return h