Module:catégoriseur

Définition, traduction, prononciation, anagramme et synonyme sur le dictionnaire libre Wiktionnaire.

La documentation pour ce module peut être créée à Module:catégoriseur/Documentation

local b = require('Module:bases')
local l = require('Module:langues')
local db = mw.loadData('Module:contexte/data')

local p = {}

function _cat_contextes(contextes, lang, ecrites)
    local cats = {}
    
    for l in pairs(contextes) do
    	if not lang or (lang and l == lang) then
	    	local lang_cats = _cat_context_lang( contextes[l], l, ecrites )
	    	for i in ipairs( lang_cats ) do
	    		cats[ lang_cats[i] ] = true
	    	end
	    end
	end
	local categories = {}
	for c in pairs(cats) do
		table.insert(categories, c)
	end

    return categories
end

function _cat_context_lang(liste, langue, ecrites)
	local categories = {}
    -- Regarde chaque élément un à un
    local n = 1
    while(liste[n] ~= nil) do
        local texte = mw.text.trim(liste[n])
        n = n+1
        
        -- Ce contexte existe-t-il ?
        cont = db[texte]
        if cont then
            -- Préparation de la catégorie
            local categorie = cont['cat']
            local cattexte
            
            -- Est-il prévu de catégoriser?
            if categorie then
                -- On catégorise avec une langue
                if mw.ustring.find(categorie, "%s") then
                    -- La langue est-elle bien donnée?
                    if langue ~= nil then
                    	nom_langue = l.get_nom(langue) or langue
                        cattexte = mw.ustring.format(categorie, nom_langue)
                    -- Sinon: catégorie pour ajouter la langue plus tard
                    else
                        cattexte = 'Wiktionnaire:Contexte sans langue'
                    end
                -- On catégorise tel quel, sans langue
                else
                    cattexte = b.ucfirst(categorie)
                end
	            -- Création de la catégorie réelle
	            local cat = b.fait_categorie(cattexte, nil, ecrites)
	            table.insert(categories, cat)
            end
            
        -- Pas dans la liste: affiche quand même le texte et catégorise comme contexte non défini
        else
            local cat = b.fait_categorie('Wiktionnaire:Contexte non défini', texte, ecrites)
            table.insert(categories, cat)
        end
    end
    
    return categories
end

-- Pour générer tout ce qui est {term}, {emploi}, {région}...
function p.categorisation(frame)
    local args
    if frame.args ~= nil and frame.args[1] ~= nil then
        args = frame.args
    else
        args = frame:getParent().args
    end
    argsnum = b.trim_parametres(args)
    
    local lang = argsnum[1] or nil
    local ecrites = false
    if args.ecrite ~= nil then
    	ecrites = true
    end
    
    -- Prépare les catégories
	local trousseau = mw.loadData('Module:passe-partout')
	if trousseau.article and #{trousseau.article} > 0 and #{trousseau.article.contextes} > 0 then
    	local cats = _cat_contextes(trousseau.article.contextes, lang, ecrites)
    	return table.concat(cats, " ")
    else
    	return ''
    end
end

return p