مودول:Categorizer

يمكن إنشاء صفحة توثيق الوحدة في مودول:Categorizer/شرح

-- version 2020101418000 from master @kabwiki

local human_ids = {	
	'Q5', -- human 
	'Q15632617', -- fictional human
	'Q15773317', -- television character
	'Q95074', -- fictional character
	'Q21070598', -- narrative entity
	'Q21070568', -- human who may be fictional
	'Q178885', -- deity
	'Q13405593'-- nature deity
}
local language_ids = {
	'Q34770', -- language 
	'Q33742', -- natural language 
	'Q38058796', -- extinct language 
	'Q2315359', --historical language
	'Q45762', --dead language
	'Q33384' -- dialect 
}
local lanfamily_ids = { 
	'Q25295' -- language family
}
local taxon_ids = { 
	'Q16521', -- taxon 
	'Q713623', -- clade 
	'Q23038290', -- fossil taxon
	'Q310890' -- monotypic taxon
}
local country_ids = { 
	'Q6256', -- country 
	'Q3624078', --  sovereign state
	'Q3024240', -- historical country
	'Q417175', --kingdom 
	'Q1048835', -- political territorial entity
	'Q107390', --federated state
	'Q35657', -- state of the United States* 
	'Q34876', -- province*
	'Q6465', -- department of France*
	'Q36784', -- region of France*
}

local city_ids = { 
	'Q5119', -- capital 
	'Q515', -- city  
	'Q486972', -- human settlement
	'Q2264924', -- port city
	'Q2989398', -- municipality of Algeria
	'Q484170', -- commune of France
	'Q1549591', -- big city 
	'Q1093829', -- city of the United States
	'Q15253706', -- like a city
	'Q7930989', -- city/town 
	'Q3327870', -- municipal corporation in the United States 
	'Q21518270', -- state or insular area capital in the United States 
	'Q174844', -- megacity 
	'Q200250', -- metropolis 
	'Q41067667', -- Q41067667
	'Q3327862', -- urban commune of Morocco
	'Q2989470', -- commune of Mauritania  
	'Q17318027', -- rural commune of Morocco
	'Q532', -- village
	'Q149621', -- district 
	'Q5084', -- hamlet
	'Q54935504', --city of Switzerland 
	'Q137773', -- ward of Japan
	'Q2590631', --municipality of Hungary 
	'Q2074737', -- municipality of Spain 
	'Q605291', -- municipality of Niger
}

local musical_band_ids = { 
	'Q215380', -- band 
	'Q56816954', --  heavy metal band 
	'Q2088357' -- musical ensemble
}
local album_ids = { 
	'Q482994', -- album 
	'Q208569' -- studio album
}
local tv_ids = { 
	'Q5398426', --television series
	'Q63952888', -- anime television series
	'Q581714', -- animated series
	'Q11424', --film
	'Q1344', -- opera *
}

local book_ids = { 
	'Q47461344', -- written work 
	'Q179461', -- religious text 
	'Q7725634', --literary work
	'Q14406742', -- comic book series
	'Q21198342', -- manga series 
	'Q10901350', -- anime and manga *
}


local religion_ids = { 
	'Q9174', -- religion 
	'Q6957341', -- major religious group
	'Q1189816', -- ethnic religion
	'Q13414953', -- religious denomination
	'Q1192063', -- Islamic schools and branches
	'Q19097', -- sect 
	'Q879146', -- Christian denomination
	'Q1530022', -- religious organization
	'Q3001185', -- Jewish denomination
	'Q5839321', -- religious school of thought
	'Q222516', -- school of Buddhism
}

local script_ids = { 
	'Q8192', -- writing system
	'Q29517555', -- natural script 
	'Q1191702', -- constructed script 
	'Q4004706', -- unicase alphabet
	'Q65045986', -- bicameral alphabet 
	'Q9779', -- alphabet 
	'Q185087', --abjad 
	'Q1049394', -- phonetic writing system
	'Q1191127', --featural writing system
}

local school_ids = { 
	'Q3918', --university 
	'Q1663017', --engineering school 
	'Q38723', --higher education institution 
	'Q847027', --grande école 
	'Q3354859', --collegiate university 
	'Q875538', --public university 
	'Q15936437', -- research university 
	'Q265662', --national university 
	'Q3914', --school 
	'Q57775519', --upper secondary school 
	'Q2511322', --lycée
}

local ph_geography_ids = { 
	'Q46831', --mountain range 
	'Q4022', --river
	'Q8502', --mountain
	'Q1437459', --non-geologically related mountain range  
	'Q39816', --valley  
	'Q23397', --lake
	'Q165', -- sea
	'Q949819', --ship canal
	'Q12284', -- canal
	'Q1267889', -- waterway
	'Q1233637', -- river mouth
	'Q124714', --spring (water) 
}

local software_ids = { 
	'Q9135', -- operating system 
	'Q218616', -- proprietary software 
	'Q20983788', -- free operating system 
	'Q7397', -- software 
	'Q341', -- free software 
	'Q6368', -- web browser 
	'Q20825628', -- GNU package
}

local categories = {
	["human"] = human_ids,
	["language"] = language_ids,
	["language family"] = lanfamily_ids,
	["taxon"] = taxon_ids,
	["city"] = city_ids,
	["country"] = country_ids,
	["musical band"] = musical_band_ids,
	["album"] = album_ids,
	["tv"] = tv_ids,
	["book"] = book_ids,
	["religion"] = religion_ids,
	["script"] = script_ids,
	["school"] = school_ids,
	["physical geography"] = ph_geography_ids,
	["software"] = software_ids,
}

local queryindex = {
	"human",
	"language",
	"language family",
	"taxon",
	"city",
	"country",
	"musical band",
	"album",
	"tv",
	"book",
	"religion",
	"script",
	"school",
	"physical geography",
	"software"
}

local p = {}
local lualinq = require "Module:LuaLinq"

function belongsTo(item_ids, category_ids)
	return lualinq.main(category_ids):any(function(x) return lualinq.main(item_ids):any(function(y) return x == y; end) end)
end

function findCategoryName(item_ids)
	for _,k in ipairs(queryindex) do
		if belongsTo(item_ids, categories[k]) then return k end
	end
    return "generic"
end

function p.findCategory(frame)
	local args = frame:getParent().args
    local itemId = nil
    if args.item then
        itemId = args.item
    end
    local item = mw.wikibase.getEntity(itemId)
    
    if item == nil or item.claims == nil then
        return nil
    end
    
    local ids = lualinq.main(item.claims["P31"]):select(function(c) return c.mainsnak.datavalue.value['id']; end)
    
    if not lualinq.main(ids):any() then
        return nil
    end
    
    local name = findCategoryName(ids)
    
    if name == "generic" then --try once more with parent
         name = findCategoryName(lualinq.main(mw.wikibase.getEntity(lualinq.main(ids):first()).claims["P279"]):select(function(c) return c.mainsnak.datavalue.value['id']; end):toArray())
    end
    
    return name
end

function p.find(frame)
	 return p.findCategory(frame)
end


return p