Module:sw-flexion

Bonjour, vous êtes venu ici pour chercher la signification du mot Module:sw-flexion. Dans DICTIOUS, vous trouverez non seulement toutes les significations du dictionnaire pour le mot Module:sw-flexion, mais vous apprendrez également son étymologie, ses caractéristiques et comment dire Module:sw-flexion au singulier et au pluriel. Tout ce que vous devez savoir sur le mot Module:sw-flexion est ici. La définition du mot Module:sw-flexion vous aidera à être plus précis et correct lorsque vous parlerez ou écrirez vos textes. Connaître la définition deModule:sw-flexion, ainsi que celles d'autres mots, enrichit votre vocabulaire et vous fournit des ressources linguistiques plus nombreuses et de meilleure qualité.

 Documentation

Ce module définit une fonction permettant d’afficher la table des flexions des noms communs et des adjectifs en swahili.

Fonctions pour modèles

generateNounTable

Retourne le wikicode pour le modèle {{sw-accord-nom}}.

Paramètres de frame
  • 1 (entier, obligatoire) : La classe du nom.
  • s (chaine, optionnel) : Le singulier du nom à afficher.
  • p (chaine, optionnel) : Le pluriel du nom à afficher.
  • p2, p3 (chaine, optionnel) : Autres pluriels du nom à afficher.
  • mode (chaine, optionnel) : La valeur sing affichera uniquement le singulier, tandis que la valeur plur affichera uniquement le pluriel.
Type de retour
chaine

generateAdjectiveTable

Retourne le wikicode pour le modèle {{sw-adj}}.

Paramètres de frame
  • 1, 2, ... (entier, optionnel) : Remplace le préfixe de la classe correspondante.
  • root (chaine, optionnel) : Forme de l’adjectif sans préfixe.
  • mode (chaine, optionnel) : La valeur sing affichera uniquement le singulier, tandis que la valeur plur affichera uniquement le pluriel.
Type de retour
chaine

local m_params = require("Module:paramètres")
local m_bases = require("Module:bases")
local bit32 = require("bit32")

local SINGULAR = 1
local PLURAL = 2
local SAME = 4

local NOUN_CLASSES = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 16, 17, 18}
local CLASS_NAMES = {
  {name = "m-wa",  sing = 1, plur = 2},
  {name = "m-mi",  sing = 3, plur = 4},
  {name = "ji-ma", sing = 5, plur = 6},
  {name = "ki-vi", sing = 7, plur = 8},
  {name = "n",     sing = 9, plur = 10},
  {name = "u",     sing = 11},
  {name = "pa",    sing = 16},
  {name = "ku",    sing = 17},
  {name = "mu",    sing = 18}
}

local p = {}

--- Show an error message as a table.
--- @param message string The message to display. May be nil.
--- @param nocat boolean If true, do not categorize the page.
--- @return string The generated table.
local function errorMessage(message, nocat)
  local text = '{| class="wikitable flextable"\n' ..
      '|+ <span style="color: red; font-weight: bold">Erreur&nbsp;!</span>\n' ..
      "|-\n" ..
      '! scope="col" | Singulier\n' ..
      '! scope="col" | Pluriel\n' ..
      "|-\n" ..
      '| scope="row" colspan="2" | <span style="color: red">' .. (message or "erreur inconnue") .. "</span>\n" ..
      "|}"
  local namespaceId = mw.title.getCurrentTitle().namespace
  if namespaceId ~= 2 and not nocat then
    -- Cf. ]
    text = text .. "]"
  end
  return text
end

--- Generate the table for the given noun.
--- @param singularForm string The singular form of the noun.
--- @param pluralForms table<number, string> The plural forms of the noun.
--- @param numberMode number The whether to show only the singular, only the plural, both, or they’re the same.
--- @return string The generated table.
local function generateNounTable(singularForm, pluralForms, numberMode)
  local showSing = bit32.band(numberMode, SINGULAR) ~= 0
  local showPlur = bit32.band(numberMode, PLURAL) ~= 0
  local isSame = bit32.band(numberMode, SAME) ~= 0

  if not showSing and not showPlur and not isSame then
    error("Invalid 'numberMode' value: " .. tostring(numberMode))
  end
  if isSame and showSing or isSame and showPlur then
    error("Invalid 'numberMode' value: " .. tostring(numberMode))
  end

  local code = '{| class="wikitable flextable"\n|-'
  if isSame then
    code = code .. "\n! Singulier et pluriel"
  end
  if showSing then
    code = code .. "\n! Singulier"
  end
  if showPlur then
    code = code .. "\n! Pluriel"
  end
  code = code .. "\n|-"
  if showSing or isSame then
    local span = 1
    if #pluralForms > span then
      span = #pluralForms
    end
    code = code .. mw.ustring.format('\n| rowspan="%s" |', span)
        .. m_bases.lien_modele(singularForm, "sw")
  end
  if showPlur then
    for _, plural in ipairs(pluralForms) do
      code = code .. "\n|" .. m_bases.lien_modele(plural, "sw") .. "\n|-"
    end
  end
  return code .. "\n|}"
end

--- Generate the table for the given adjective.
--- @param inflectedForms table<number, string> The inflected forms form of the adjective.
--- @param numberMode number The whether to show only the singular, only the plural, both.
--- @return string The generated table.
local function generateAdjectiveTable(inflectedForms, numberMode)
  local showSing = bit32.band(numberMode, SINGULAR) ~= 0
  local showPlur = bit32.band(numberMode, PLURAL) ~= 0
  
  if not showSing and not showPlur then
    error("Invalid 'numberMode' value: " .. tostring(numberMode))
  end
  
  local code = '{| class="wikitable flextable"\n|-'
  code = code .. "\n! ]"
  if showSing then
    code = code .. "\n! Singulier"
  end
  if showPlur then
    code = code .. "\n! Pluriel"
  end
  code = code .. "\n|-"
  
  for _, classes in pairs(CLASS_NAMES) do
    class_name = classes
    sing_class = classes
    plur_class = classes
    
    code = code .. "\n|-"
    code = code .. "\n! " .. class_name
    
    if showSing then
      if inflectedForms then
        code = code .. "\n| " .. m_bases.lien_modele(inflectedForms, "sw")
      else
        code = code .. "\n| —"
      end
    end
    if showPlur then
      if inflectedForms then
        code = code .. "\n| " .. m_bases.lien_modele(inflectedForms, "sw")
      else
        code = code .. "\n| —"
      end
    end
  end
  
  return code .. "\n|}"
end

--- Generate the singular of the given word.
--- @param plural string The plural form of the word.
--- @param classCode number The class number of the word.
--- @return string The word’s singular.
--- @error If the given class is not supported.
local function generateSingular(plural, classCode)
  uppercase = string.find(plural, "^")
  if uppercase then
    plural = string.gsub(plural, "^", string.lower)
  end
  
  singular = nil
  
  if classCode == 2 then
  	if string.find(plural, "^wa") then
  	  singular = string.gsub(plural, "^wa", "m")
  	elseif string.find(plural, "^w") then
  	  singular = string.gsub(plural, "^w", "m")
  	end
  elseif classCode == 4 then
  	singular = string.gsub(plural, "^mi", "m")
  elseif classCode == 6 then
    singular = string.gsub(plural, "^ma", "")
  elseif classCode == 8 then
  	if string.find(plural, "^vi") then
  	  singular = string.gsub(plural, "^vi", "ki")
  	elseif string.find(plural, "^vy") then
  	  singular = string.gsub(plural, "^vy", "ch")
  	end
  elseif classCode == 10 then
    singular = plural
  end
  
  if not singular then
    error("Impossible de générer le singulier")
  end
  if uppercase then
    return string.gsub(singular, "^", string.upper)
  else
    return singular
  end
end

--- Generate the plural of the given word.
--- @param singular string The singular form of the word.
--- @param classCode number The class number of the word.
--- @return string The word’s plural.
--- @error If the given class is not supported.
local function generatePlural(singular, classCode)
  uppercase = string.find(singular, "^")
  if uppercase then
    singular = string.gsub(singular, "^", string.lower)
  end
  
  plural = nil
  
  if classCode == 1 then
  	if string.find(singular, "^mwa") then
  	  plural = string.gsub(singular, "^mwa", "wa")
  	elseif string.find(singular, "^mu") then
  	  plural = string.gsub(singular, "^mu", "wa")
  	elseif string.find(singular, "^mw") then
  	  plural = string.gsub(singular, "^mw", "wa")
  	elseif string.find(singular, "^m") then
  	  plural = string.gsub(singular, "^m", "wa")
  	end
  elseif classCode == 3 then
  	if string.find(singular, "^mu") then
  	  plural = string.gsub(singular, "^mu", "mi")
  	elseif string.find(singular, "^mw") then
  	  plural = string.gsub(singular, "^mw", "mi")
  	elseif string.find(singular, "^m") then
  	  plural = string.gsub(singular, "^m", "mi")
  	end
  elseif classCode == 5 then
    plural = "ma" .. singular
  elseif classCode == 7 then
  	if string.find(singular, "^ki") then
  	  plural = string.gsub(singular, "^ki", "vi")
  	elseif string.find(singular, "^ch") then
  	  plural = string.gsub(singular, "^ch", "vy")
  	end
  elseif classCode == 9 then
    plural = singular
  end
  
  if not plural then
    error("Impossible de générer le pluriel.")
  end
  if uppercase then
    return string.gsub(plural, "^", string.upper)
  else
    return plural
  end
end

--- Generate the inflected forms of an adjective for all noun classes.
--- @param root string The root form of the adjective (without prefix).
--- @return table A table mapping noun class numbers to their corresponding inflected adjective forms.
local function generateInflectedForms(root)
  local inflectedForms = {}
  
  inflectedForms = "m" .. root
  inflectedForms = "wa" .. root
  inflectedForms = "m" .. root
  inflectedForms = "mi" .. root
  inflectedForms = root
  inflectedForms = "ma" .. root
  inflectedForms = "ki" .. root
  inflectedForms = "vi" .. root
  if string.find(root, "^r") then
    inflectedForms = "nd" .. string.sub(root, 2)
    inflectedForms = "nd" .. string.sub(root, 2)
  elseif string.find(root, "^") then
    inflectedForms = "n" .. root
    inflectedForms = "n" .. root
  elseif string.find(root, "^") then
    inflectedForms = "m" .. root
    inflectedForms = "m" .. root
  else
    inflectedForms =  root
    inflectedForms = root
  end
  inflectedForms = "m" .. root
  inflectedForms = "pa" .. root
  inflectedForms = "ku" .. root
  inflectedForms = "mu" .. root
  
  return inflectedForms
end

--- Extract the root of an adjective from a full form.
--- @param word string Any inflected form of the adjective or prefixed by a hyphen.
--- @return string The extracted root form of the adjective.
local function getAdjectiveRoot(word)
  if string.find(word, "^-(+)") then
    return string.gsub(word, "^-(+)", "%1")
  end
  
  return word
end

--- Generate the table for the given noun.
--- @param frame frame
--- Parameters:
---  parent.args (int): The word’s class.
---  parent.args (string, optional): The word’s singular form. Defaults to the page’s title.
---  parent.args (string, optional): Either "sing" for singular only, or "plur" for plural only.
---  parent.args (string, optional): The word’s plural form if the generated one is not correct.
---  parent.args (string, optional): The word’s second plural form.
---  parent.args (string, optional): The word’s third plural form.
--- @return string The generated table.
function p.generateNounTable(frame)
  local spec = {
     = { required = true, type = m_params.INT },
     = { },
     = { enum = { "sing", "plur" } },
  }
  local pluralArgNames = { "p", "p2", "p3" }
  for _, argName in ipairs(pluralArgNames) do
    spec = {}
  end

  local args, validArgs = m_params.process(frame:getParent().args, spec, true) -- Silent errors

  if not validArgs then
    if args == "1" and (args == m_params.EMPTY_PARAM or args == m_params.MISSING_PARAM) then
      return errorMessage("Veuillez préciser la classe nominale.<br>Voir {{]}}.")
    end
    return errorMessage(mw.ustring.format(
        "Paramètre «&nbsp;%s&nbsp;» incorrect&nbsp;: %s",
        args,
        args
    ))
  end

  local classCode = args
  valid_class = false
  for _, v in ipairs(NOUN_CLASSES) do
    if v == classCode then valid_class = true end
  end
  if not valid_class then
    return errorMessage(mw.ustring.format(
        "La classe nominale «&nbsp;%d&nbsp;» est inconnue.<br>Voir {{]}}.",
        classCode
    ))
  end

  local sing
  local pluralForms = {}
  
  local numberMode
  if args == "sing" then
    numberMode = SINGULAR
  elseif args == "plur" then
    numberMode = PLURAL
  end
  
  if classCode == 16 or classCode == 17 or classCode == 18 then
  	return errorMessage(mw.ustring.format(
      "La classe «&nbsp;%d&nbsp;» n'est pas encore implémentée",
      classCode
    ))
  elseif classCode % 2 == 1 or classCode == 11 then
  	if numberMode == PLURAL then
  	  return errorMessage(mw.ustring.format(
        "La classe «&nbsp;%d&nbsp;» (singulier) ne peut pas être uniquement au pluriel (<code>mode=plur</code>).",
        classCode
      ))
    end
    sing = args or mw.title.getCurrentTitle().text
    for _, argName in ipairs(pluralArgNames) do
      if args then
        table.insert(pluralForms, args)
      end
    end
    if #pluralForms == 0 and numberMode ~= SINGULAR then
      success, result = pcall(generatePlural, sing, classCode)
      if not success then
        return errorMessage("Impossible de générer le pluriel automatiquement."
          .. "<br/>Utiliser le paramètre <code>p</code>.")
      end
      pluralForms = result
    end
  else
  	if numberMode == SINGULAR then
  	  return errorMessage(mw.ustring.format(
        "La classe «&nbsp;%d&nbsp;» (pluriel) ne peut pas être uniquement au singulier (<code>mode=sing</code>).",
        classCode
    ))
  	end
    plural = args or mw.title.getCurrentTitle().text
    if args then
      sing = args
    else
      success, result = pcall(generateSingular, plural, classCode)
      if not success and numberMode ~= PLURAL then
        return errorMessage("Impossible de générer le singulier automatiquement."
          .. "<br/>Utiliser le paramètre <code>s</code>.")
      end
      sing = result
    end
    table.insert(pluralForms, plural)
  end
  
  if not numberMode then
    if #pluralForms == 1 and pluralForms == sing then
      numberMode = SAME
    else
      numberMode = bit32.bor(SINGULAR, PLURAL)
    end
  end
  
  return generateNounTable(sing, pluralForms, numberMode)
end

--- Generate the table for the given adjective.
--- @param frame frame
--- Parameters:
---  args (string, optional): The root form of the adjective.
---  args (string, optional): Either "sing" for singular only, or "plur" for plural only.
---  args (string, optional): Override for the inflected form in a specific noun class (where `class` is a member of NOUN_CLASSES).
--- @return string The generated table.
function p.generateAdjectiveTable(frame)
  local spec = {
     = { },
     = { enum = { "sing", "plur" } },
  }
  for _, argName in ipairs(NOUN_CLASSES) do
    spec = {}
  end

  local args, validArgs = m_params.process(frame:getParent().args, spec, true)
  
  if not validArgs then
    return errorMessage(mw.ustring.format(
        "Paramètre «&nbsp;%s&nbsp;» incorrect&nbsp;: %s",
        args,
        args
    ))
  end
  
  local root = args or getAdjectiveRoot(mw.title.getCurrentTitle().text)
  local numberMode
  if args == "sing" then
    numberMode = SINGULAR
  elseif args == "plur" then
    numberMode = PLURAL
  else
    numberMode = bit32.bor(SINGULAR, PLURAL)
  end
  
  local inflectedForms = generateInflectedForms(root)
  
  for _, class in ipairs(NOUN_CLASSES) do
    if args then
      inflectedForms = args
    end
  end
  
  return generateAdjectiveTable(inflectedForms, numberMode)
end

return p