« Module:Matériau » : différence entre les versions
mAucun résumé des modifications |
correction bug, commentaires |
||
| Ligne 1 : | Ligne 1 : | ||
local p = {} | local p = {} | ||
local wikidata = require | local wikidata = require 'Module:Interface Wikidata'.fromLua | ||
local linguistic = require | local linguistic = require 'Module:Linguistique' | ||
local speciallabels = { -- libellés définis localement, qui priment sur ceux de Wikidata | local speciallabels = { -- libellés définis localement, qui priment sur ceux de Wikidata | ||
| Ligne 16 : | Ligne 16 : | ||
return nil | return nil | ||
end | end | ||
local | |||
-- créé une table de chaînes avec les différents matériaux | |||
local withP518, withoutP518 = {}, {} | |||
local function addStatement(statement) -- ajout d'un chaîne à la table des valeurs | |||
-- valeur de base | |||
local mainid = wikidata.getmainid(statement) | |||
local str = wikidata.formatEntity(mainid, {speciallabels = speciallabels}) | |||
if (not statement.qualifiers) or (not statement.qualifiers.P518) then | |||
table.insert(withoutP518, str) | |||
return | |||
end | |||
for _, qualif in pairs(statement.qualifiers.P518) do | |||
local key = wikidata.getid(qualif) | |||
withP518[key] = withP518[key] or {} | |||
table.insert(withP518[key], mainid) | |||
end | |||
end | |||
for i, j in pairs(materials) do | for i, j in pairs(materials) do | ||
addStatement(j) | |||
end | end | ||
-- concaténation des différentes tables de valeur | |||
mainstr = linguistic.conj( | --- valeurs sans qualificatifs | ||
local mainstr = linguistic.conj(withoutP518) | |||
if | |||
supportstr = ' sur ' .. linguistic.conj( | --- valeurs avec P518 utilisant la préposition "sur" | ||
local supportstr | |||
if withP518['Q861259'] then | |||
local v = withP518['Q861259'] | |||
supportstr = ' sur ' .. linguistic.conj(v) | |||
vals['Q861259'] = nil | vals['Q861259'] = nil | ||
end | end | ||
if mainstr or supportstr then | if mainstr or supportstr then | ||
mainstr = (mainstr or '').. (supportstr or '') | mainstr = (mainstr or '').. (supportstr or '') | ||
end | end | ||
--- autres qualificatifs : préposition "en" | |||
local withpartstr | |||
local strtable = {} | |||
for part, materials in pairs(withP518) do | |||
local partstr = wikidata.getLabel(part) | |||
local v = {} | |||
for i, j in pairs(materials) do | |||
table.insert(v, wikidata.formatEntity(j)) | |||
end | |||
local str = partstr .. ' en ' .. linguistic.conj(v) | |||
table.insert(strtable, str) | |||
end | |||
withpartstr = linguistic.conj(strtable, 'comma') | |||
-- concaténation finale | |||
mainstr = linguistic.conj({mainstr, withpartstr}, 'comma') | mainstr = linguistic.conj({mainstr, withpartstr}, 'comma') | ||
return mainstr | |||
return | |||
end | end | ||
return p | return p | ||
Version du 27 août 2015 à 06:03
local p = {} local wikidata = require 'Module:Interface Wikidata'.fromLua local linguistic = require 'Module:Linguistique'
local speciallabels = { -- libellés définis localement, qui priment sur ceux de Wikidata Q296955 = 'huile', Q22731 = 'pierre', Q4259259 = 'toile', }
function p.formatFromItem(item, args) args = args or {} -- affiche le matériau en fonction de Property:P186, et de son qualificatif P518 ('s'applique à') local materials = wikidata.getClaims{entity = item, property = 'P186'} if not materials then return nil end
-- créé une table de chaînes avec les différents matériaux local withP518, withoutP518 = {}, {}
local function addStatement(statement) -- ajout d'un chaîne à la table des valeurs -- valeur de base local mainid = wikidata.getmainid(statement) local str = wikidata.formatEntity(mainid, {speciallabels = speciallabels})
if (not statement.qualifiers) or (not statement.qualifiers.P518) then table.insert(withoutP518, str) return end
for _, qualif in pairs(statement.qualifiers.P518) do local key = wikidata.getid(qualif) withP518[key] = withP518[key] or {} table.insert(withP518[key], mainid) end end for i, j in pairs(materials) do addStatement(j) end
-- concaténation des différentes tables de valeur
--- valeurs sans qualificatifs local mainstr = linguistic.conj(withoutP518)
--- valeurs avec P518 utilisant la préposition "sur" local supportstr if withP518['Q861259'] then local v = withP518['Q861259'] supportstr = ' sur ' .. linguistic.conj(v) vals['Q861259'] = nil end if mainstr or supportstr then mainstr = (mainstr or ).. (supportstr or ) end
--- autres qualificatifs : préposition "en" local withpartstr local strtable = {} for part, materials in pairs(withP518) do local partstr = wikidata.getLabel(part) local v = {} for i, j in pairs(materials) do table.insert(v, wikidata.formatEntity(j)) end local str = partstr .. ' en ' .. linguistic.conj(v) table.insert(strtable, str) end withpartstr = linguistic.conj(strtable, 'comma')
-- concaténation finale mainstr = linguistic.conj({mainstr, withpartstr}, 'comma')
return mainstr end return p