Module:Test/Zolo

Lua
CodeDiscussionLinksLink count SubpagesDocumentationTestsResultsSandboxLive code All modules

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

Code

local p = {}
local wikidata = require('Module:Wikidata')
local lang = 'fr'

local list = {'301', '762', '930', '1544', '3165', '4724', '5443', '5585', '5592', '5597', '5600', '5604', '7793', '7799', '7814', '8053', '8867', '11812', '12750', '12754', '13374', '13382', '13384', '13387', '17676', '17919', '19165', '19167', '20882', '23766', '25035', '25328', '27040', '29761', '30047', '31475', '38905', '41508', '44323', '44911', '45011', '45399', '45417', '45566', '46123', '46830', '46868', '47163', '47361', '47780', '48174', '49712', '49744', '51129', '51131', '51136', '51137', '51141', '51193', '51199', '51204', '51205', '51215', '51217', '51221', '51233', '51988', '52431', '54090', '57481', '57588', '57759', '57984', '59087', '59620', '60045', '60163', '60239', '60694', '60739', '60758', '61055', '61071', '61130', '61188', '61256', '61259', '61332', '61342', '61349', '61622', '61892', '61965', '62130', '62260', '62322', '62351', '62570', '62598', '62629', '62637', '62789', '62973', '62993', '63191', '63199', '63757', '63906', '64094', '64104', '64412', '64434', '64479', '64487', '64872', '65392', '65476', '65803', '65847', '65901', '66108', '66113', '66149', '66169', '66359', '66464', '66481', '66490', '66677', '66724', '66726', '66735', '66855', '66894', '66897', '66929', '67031', '67034', '67064', '67306', '67316', '67350', '67407', '67411', '67490', '67612', '67897', '67976', '68116', '68157', '68169', '68214', '68243', '68348', '68462', '68746', '68768', '68820', '68872', '69059', '69102', '69257', '69384', '69473', '69715', '70102', '70246', '70337', '70410', '70487', '70696', '70722', '70880', '70903', '71505', '71592', '71685', '71874', '71886', '72120', '72148', '72322', '72422', '72574', '72604', '72681', '72916', '72959', '73050', '73115', '73131', '73334', '73345', '73673', '73874', '74402', '74505', '74985', '75067', '75147', '75184', '75191', '75204', '75226', '75298', '75465', '75843', '75849', '75949', '75964', '76015', '76076', '76247', '76278', '76347', '76368', '76426', '76427', '76435', '76573', '76700', '76704', '76726', '76729', '76730', '76734', '76841', '76846', '76903', '77103', '77138', '77311', '77329', '77353', '77357', '77380', '77382', '77383', '77422', '77431', '77553', '77559', '77561', '77702', '77707', '77806', '78032', '78179', '78229', '78279', '78291', '78292', '78382', '78484', '78550', '78626', '78638', '78680', '78694', '78705', '78710', '78775', '78792', '78819', '78856', '78858', '78884', '78885', '78968', '78979', '79098', '79113', '79166', '82840', '84250', '84287', '84305', '84312', '84473', '84507', '84541', '84588', '84655', '84673', '84788', '84841', '84902', '85000', '85017', '85096', '85204', '85218', '85439', '85488', '85541', '85548', '85557', '85661', '85669', '85724', '85804', '85809', '85834', '85897', '85911', '86002', '86064', '86122', '86158', '86346', '86479', '86517', '86616', '86782', '86917', '86933', '86955', '87005', '87055', '87071', '87075', '87087', '87111', '87174', '87321', '87470', '87480', '87733', '87757', '87785', '87808', '87809', '87972', '87997', '88018', '88035', '88057', '88163', '88198', '88297', '88357', '88567', '88651', '88652', '88656', '88690', '88783', '88785', '88791', '88902', '88916', '88949', '89027', '89360', '89437', '89592', '89599', '89660', '89678', '89682', '89724', '89736', '89820', '89854', '90033', '90049', '90119', '90193', '90504', '90665', '91225', '91242', '91280', '91407', '91519', '91616', '91721', '91771', '91823', '91832', '91860', '91864', '91895', '91920', '91956', '91976', '92016', '92102', '92142', '92241', '92314', '92368', '92539', '93847', '93977', '93987', '94005', '94253', '94321', '94351', '94647', '94659', '94665', '95198', '95216', '95223', '95246', '95325', '95330', '95501', '95595', '95677', '95716', '95724', '95782', '95804', '95816', '95866', '95917', '95970', '96020', '96037', '96052', '96151', '96187', '96239', '96271', '96500', '96545', '96639', '96786', '96787', '96793', '96901', '96956', '97133', '97135', '97388', '97422', '97434', '97455', '97565', '97664', '97672', '97829', '97875', '97895', '97990', '98013', '98089', '98111', '98169', '98171', '98185', '98192', '98329', '98350', '98519', '98539', '98559', '98689', '98694', '98708', '98909', '99178', '99186', '99235', '99298', '99304', '99378', '99399', '99416', '99421', '99468', '99652', '99790', '99822', '100228', '100261', '100281', '100283', '100377', '100384', '100542', '100559', '100601', '100608', '100965', '101000', '101021', '101056', '101126', '101193', '101268', '101450', '101463', '101655', '101777', 
	'101791', '101795', '101875', '101921', '101934', '101938', '101958', '102153', '102179', '102284', '102289', '102293', '102370', '102376', '102387', '102436', '102489', '102544', '102553', '102773', '102921', '102980', '102989', '103137', '103151', '103234', '103240', '103241', '103249', '103302', '103319', '103371', '103638', '103671', '103704', '103780', '103790', '104018', '104043', '104292', '104317', '104323', '104350', '104368', '104432', '104491', '104495', '104551', '104898', '104993', '105212', '105213', '105214', '105293', '105374', '105427', '105467', '105625', '105874', '106030', '106079', '106130', '106263', '106276', '106328', '106449', '106569', '106615', '106679', '106696', '106913', '106952', '107036', '107075', '107187', '107201', '107206', '107210', '107292', '107324', '107377', '107504', '107536', '107736', '107771', '107910', '107922', '108001', '108010', '108049', '108051', '108136', '108190', '108215', '108229', '108241', '108267', '108295', '108310', '108321', '108335', '108411', '108464', '108625', '108629'}--, '108738', '108831', '108899', '109013', '109049', '109136', '109166', '109271', '109337', '109349', '109445', '109450', '109585', '109637', '109837', '109887', '109985', '110040', '110062', '110204', '110271', '110318', '110375', '110385', '110798', '110801', '110880', '111097', '111137', '111145', '111198', '111377', '111513', '111611', '111684', '112007', '112231', '112392', '112397', '112426', '112575', '112818', '112856', '112992', '113014', '113049', '113113', '113203', '113219', '113229', '113257', '113342', '113553', '113770', '113775', '113991', '114083', '114112', '114254', '114357', '114444', '114599', '114630', '114847', '114911', '114980', '115191', '115315', '115483', '115575', '115713', '115920', '115944', '116083', '116101', '116136', '116212', '116230', '116310'}

local chart = require('Module:Chart')

local function getyear(statement)
	local datestr = statement.qualifiers.P585[1].datavalue.value.time
	return string.sub(datestr, 9, 12) 
end

function p.census(frame)
	local args = frame.args
	local width = args.width
	if not width or width == '' then
		width = '1000'
	end
	local entity = args.item
	local method = args.method
	local request = {entity = entity, property = 'P1082', rank = 'valid', withdate = 'true', sorttype = 'chronological'}
	
	-- options de filtrage des données
	if method == 'census' then
		request.qualifier = 'P459'
		request.qualifiervalue = 'Q39825'
	end
	
	local rawdata = wikidata.getClaims(request)
	if not rawdata or #rawdata < 2 then
		return 'pas de données' 
	end
	 -- à adapter pour av-JC
	local mindate, maxdate = getyear(rawdata[1]), getyear(rawdata[#rawdata])

	local labeltable, valtable = {}, {}
	local sourcelist = '' -- stocke les valuers contenue en référence dans la propriété p248 (string plus pratique que table)
	for i = tonumber(mindate), tonumber(maxdate) do
		if i % 20 == 0 then -- ajoute la date une année sur 10, à raffiner
			table.insert(labeltable, i)
		else
			table.insert(labeltable, '')
		end
		table.insert(valtable, '0')
	end
	
	for i, j in pairs(rawdata) do
		local year = tonumber(getyear(j))
		local val = string.sub(j.mainsnak.datavalue.value.amount, 2) -- retire le signe '+'
		valtable[year - mindate +1] = val
		if j.references then
			for k, reference in pairs(j.references) do
				local src = 'autres'
				if reference.snaks.P248 then
					src = reference.snaks.P248[1].datavalue.value["numeric-id"]
				end
				if not string.find(sourcelist, src) then
					sourcelist = sourcelist .. src .. ' ' 
					break
				end
			end
		end
	end

	local sourcetable = mw.text.split(mw.text.trim(sourcelist), ' ')
	for i, j in pairs(sourcetable) do
		if tonumber(j) then -- si la source indiquée correspond à un numéro d'élément
			sourcetable[i] = wikidata.formatEntityId('Q' .. j, {lang=lang})
		end
	end
	local numsources = #sourcetable
	local sourcetext = require('Module:Linguistic').conj(sourcetable, lang) -- table.concat(sourcetable, ', ')
	if numsources > 1 then
		sourcetext = 'Sources : ' .. sourcetext
	elseif numsources == 1 then
		sourcetext = 'Source : ' .. sourcetext
	end
	local title = ' <b>Population de ' .. wikidata.formatEntityId(entity, {lang=lang, link = 'wikipedia'}) .. '</b>'

	local text = title .. '<br />' .. sourcetext .. '<br />'
	frame.args = {}
	frame.args['x legends'] = table.concat(labeltable, ':')
	frame.args['group 1'] = table.concat(valtable, ':')
	frame.args['group names'] = 'Some'
	frame.args['width'] = width	
	local image = chart['bar chart'](frame)

	return text .. image
end

function p.statistics(frame)
	local numval = tonumber(frame.args[limit]) or 180
	if numval > #list then numval = #list end

	local width = frame.args.width
	if not width or width == '' then
		width = '1000'
	end
	local valtable, nametable = {}, {}
	
	local i = 0
	while (i < 20) do -- initialie stattable
		table.insert(valtable, 0)
		table.insert(nametable, tostring(1800 + 10*i))
		i = i + 1
	end 
	local nodates = {}
	local outofrange = {}
	for i, j in pairs(list) do 
		if i > numval then break end
		local entity = mw.wikibase.getEntityObject('Q' .. j)
		local claims = wikidata.getClaims({entity=entity, property = 'P569'})
		if not claims or claims[1].mainsnak.snaktype ~= 'value' then
			table.insert(nodates, entity.id)
		else
			local timestr = claims[1].mainsnak.datavalue.value.time
			local decade = tonumber(string.sub(timestr, 9, 11)) 
			if string.sub(1,1) == '-' or (decade < 180) or (decade > 199) then -- years before Christ or out of the desired range
				table.insert(outofrange, entity.id)-- '-': BC years 
			else 
				valtable[decade - 179] = valtable[decade - 179] + 1
			end 
		end
	end

	local text = numval .. ' values analyzed <br />'
	for i, j in pairs(nametable) do
		text = text .. j .. ': ' .. valtable[i] .. '<br />'
	end	

	-- create diagram
	frame.args = {}
	frame.args['x legends'] = table.concat(nametable, ':')
	frame.args['group 1'] = table.concat(valtable, ':')
	frame.args['group names'] = 'Some'
	frame.args['width'] = width	
	local image = chart['bar chart'](frame)

	-- report invalid values
	text = text .. '<br />out of range: ' .. table.concat(outofrange, ', ') .. '<br />no date detected: ' .. table.concat(nodates, ', ')

	return text .. image
end
local numberwords = { --words that are part of the house number, not the street name
	'ter', 'bis', 'quater', 'quinquies', 'sexies', 'septies', 'octies', 'novies', 'decies', '-',' ',
	 ',' -- the comma at the end should be removed, but not the others
}
local blanknumberwords = { --words that are mark a street number but should be removed
	"n°", "nr", "numéro", "no", 
}
	
local commonabbrevs = {
	["av. "] = 'avenue ',
	["av "] = 'avenue ',
	["pl. "] = 'place ',
	["pl "] = 'place ',
	["bd"] = 'boulevard',
	["bld"] = 'boulevard',
	["boul."] = 'boulevard',
	["brd"] = 'boulevard',
	["bvd"] = 'boulevard',
}

local function firstchar(str)
	return string.sub(str, 1, 1)
end

local function transfer(source, target, numchar) -- move the numchar first chars from one string to another
	target = target .. string.sub(source, 1, numchar)
	source = string.sub(source, 1 + numchar, #source)
	return source, target
end

local function parseloop(street, number)
	local oldstreet = street -- track old version to check there is a transformatoin
	while tonumber(firstchar(street)) do
		street, number = transfer(street, number, 1)
	end
	for i, word in pairs(numberwords) do
		if mw.ustring.find(street, word) == 1 then
			street, number = transfer(street, number, #word)
		end
	end
	for i, word in pairs(blanknumberwords) do
		if mw.ustring.find(street, word) == 1 then
			street = string.sub(street, #word +1, #street)
		end
	end
	if street ~= oldstreet then
		street, number = parseloop(street, number)
	end
	return street, number
end

function p.parse(str)
	local street, number = parseloop(str, '')
	street, number = mw.text.trim(street), mw.text.trim(number)
	if string.sub(street, #street, #street) == ',' then
		street = string.sub(street, 1, #street - 1)
	end
	for abbrev, longvalue in pairs(commonabbrevs) do
		if mw.ustring.find(street, abbrev) then
			street = street .. ' <i>or</i> ' .. mw.ustring.gsub(street, abbrev, longvalue)
		end
	end
	return '<b>' .. str .. ':</b> <street> '.. street .. ' <number> ' .. number .. '<br />'
end

function p.parsetest(frame)
	local testcases = 
	{
	'35 avenue des Champs Elysées',
	'n°35 avenue des Champs Elysées',
	'35, avenue des Champs Elysées', 
	'35 bis avenue des Champs Elysées',
	'35 bis av. des Champs Elysées',
	'35-40 avenue des Champs Elysées',
 	}
 
	local out = ''
	for i, j in pairs(testcases) do
		out = out .. p.parse(j)
	end
	return out
end

function p.parsetest(frame)
	local testcases = {}
	local defaulttestcases = 
	{
	'35 avenue des Champs Elysées',
	'n°35 avenue des Champs Elysées',
	'35, avenue des Champs Elysées', 
	'35 bis avenue des Champs Elysées',
	'35 bis av. des Champs Elysées',
	'35-40 avenue des Champs Elysées',
	 }
 	if frame.args then
 		for i, arg in pairs(frame.args) do
 			local claims = wikidata.getClaims{entity = arg, property = 'P969'}
 			if claims then
 				for j, claim in pairs(claims) do
 					table.insert(testcases, wikidata.formatStatement(claim))
 				end
 			end
 		end
 	else
 		claims = defaulttestcases
 	end
	local out = ''
	for i, j in pairs(testcases) do
		out = out .. p.parse(j)
	end
	return out
end

return p