Module:bac à sable/Danÿa/fr-flexion

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

La documentation pour ce module peut être créée à Module:bac à sable/Danÿa/fr-flexion/Documentation

--- This module defines functions for use in French inflections tables.

local m_params = require("Module:paramètres")
local m_pron = require("Module:prononciation")
local m_table = require("Module:table")

local p = {}


----------------
-- Args class --
----------------


--- @class Args
local Args = {}
Args.__index = Args

function Args:new()
  local this = {}
  setmetatable(this, Args)

  this.title = nil

  this.ms = nil
  this.mp = {}
  this.m = nil

  this.fs = {}
  this.fp = {}
  this.f = nil

  this.msPronPrefixes = {}
  this.msProns = {}
  this.msPronRefs = {}

  this.mpPronPrefixes = {}
  this.mpProns = {}
  this.mpPronRefs = {}

  this.fsPronPrefixes = {}
  this.fsProns = {}
  this.fsPronRefs = {}

  this.fpPronPrefixes = {}
  this.fpProns = {}
  this.fpPronRefs = {}

  this.mPronPrefixes = {}
  this.mProns = {}

  this.fPronPrefixes = {}
  this.fProns = {}

  this.pronPrefixes = {}
  this.prons = {}

  this.noCat = false

  return this
end

-- Inflections

--- Returns the masculine singular form.
--- @return string
function Args:getMS()
  return self.ms or self.m
end

--- Returns the masculine plural form.
--- @return string
function Args:getMP()
  if self.mp then
    return self.mp
  elseif self.m then
    return self.m
  else
    return self:getMS() .. "s"
  end
end

--- Returns the other masculine plural forms.
--- @return table
function Args:getOtherMPs()
  return { unpack(self.mp, 2) }
end

--- Returns the feminine singular form.
--- @return string
function Args:getFS()
  if self.fs then
    return self.fs
  elseif self.f then
    return self.f
  else
    return self:getMS() .. "e"
  end
end

--- Returns the other feminine singular forms.
--- @return table
function Args:getOtherFSs()
  return { unpack(self.fs, 2) }
end

--- Returns the feminine plural form.
--- @return string
function Args:getFP()
  if self.fp then
    return self.fp
  elseif self.f then
    return self.f
  elseif self.fs then
    return self.fs .. "s"
  else
    return self:getMS() .. "es"
  end
end

--- Returns the other feminine plural forms.
--- @return table
function Args:getOtherFPs()
  return { unpack(self.fp, 2) }
end

-- Pronunciation prefixes

--- Returns the masculine singular pronunciation prefix.
--- @return string|nil
function Args:getMSPronPrefix(i)
  return self.msPronPrefixes or self.mPronPrefixes or self.pronPrefixes
end

--- Returns the masculine plural pronunciation prefix.
--- @return string|nil
function Args:getMPPronPrefix(i)
  return self.mpPronPrefixes or self.mPronPrefixes or self.pronPrefixes
end

--- Returns the feminine singular pronunciation prefix.
--- @return string|nil
function Args:getFSPronPrefix(i)
  return self.fsPronPrefixes or self.fPronPrefixes or self.pronPrefixes
end

--- Returns the feminine plural pronunciation prefix.
--- @return string|nil
function Args:getFPPronPrefix(i)
  return self.fpPronPrefixes or self.fPronPrefixes or self.pronPrefixes
end

-- Pronunciation

--- Returns the masculine singular pronunciation.
--- @return string|nil
function Args:getMSPron(i)
  return self.msProns or self.mProns or self.prons
end

--- Returns the masculine plural pronunciation.
--- @return string|nil
function Args:getMPPron(i)
  return self.mpProns or self.mProns or self.prons
end

--- Returns the feminine singular pronunciation.
--- @return string|nil
function Args:getFSPron(i)
  return self.fsProns or self.fProns or self.prons
end

--- Returns the feminine plural pronunciation.
--- @return string|nil
function Args:getFPPron(i)
  return self.fpProns or self.fProns or self.prons
end


---------------------
-- Utils functions --
---------------------


--- Prepends the right character before the given API string
--- if needed (dot, space, liaison).
--- @param api string The string to prepend the character to.
--- @return string
local function addSeparatorBeforeInvPart(api)
  if not api then
    return ""
  end

  local firstChar = mw.ustring.sub(api, 1, 1)
  if firstChar == "." or firstChar == "‿" or firstChar == " " then
    return api
  end
  -- NBSP in UTF8 is 0xC2 0xA0
  if firstChar == "\194\160" then
    return " " .. mw.ustring.sub(api, 2)
  end
  -- nbsp HTML entity replaced by regular space
  if mw.ustring.sub(api, 1, 6) == " " then
    return " " .. mw.ustring.sub(api, 7)
  end

  -- In all other cases, add a space
  return " " .. api
end

--- Splits the first argument into 2 parts: the part before the suffix and the part after it.
--- @param word string The word to split.
--- @param ms string Ending for masculine singular.
--- @param mp string Ending for masculine plural.
--- @param fs string Ending for feminine singular.
--- @param fp string Ending for feminine plural.
--- @return (string,string) The text before and after the suffix.
local function splitWord(word, ms, mp, fs, fp)
  local wordStart = ""
  local wordEnd = ""

  -- Add space so that %s matches the end of the string
  local pageTitle1 = word .. " "
  -- Hyphen has to be escaped
  local matchStart, matchEnd = mw.ustring.find(pageTitle1, ms .. "")
  if not matchStart and mp then
    matchStart, matchEnd = mw.ustring.find(pageTitle1, mp .. "")
  end
  if not matchStart and fs then
    matchStart, matchEnd = mw.ustring.find(pageTitle1, fs .. "")
  end
  if not matchStart and fp then
    matchStart, matchEnd = mw.ustring.find(pageTitle1, fp .. "")
  end

  if matchStart then
    wordStart = mw.ustring.sub(word, 1, matchStart - 1)
    if matchEnd <= mw.ustring.len(word) then
      wordEnd = mw.ustring.sub(word, matchEnd) -- comprend le séparateur
    end
    return wordStart, wordEnd
  end

  return nil, nil
end

-- TODO reprendre
local function errorTable(message, noCat)
  local text = '{| class="flextable"\n' ..
      '|+ <span style="color:red"><b>Erreur&nbsp;!</b></span>\n' ..
      '|-\n' ..
      '! scope="col" | Singulier\n' ..
      '! scope="col" | Pluriel\n' ..
      '|-\n' ..
      '| scope="row" colspan="2" | <span style="color:red">' .. message .. '</span>\n' ..
      '|}'

  -- 2 = User
  if mw.title.getCurrentTitle().namespace ~= 2 and not noCat then
    text = text .. "]"
  end

  return text
end

--- Generates the inflection table using the given Args object.
--- @param argsObject Args
--- @return string
function p.generateTable(argsObject)
  local res = '{| class="flextable flextable-fr-mfsp"\n'

  if argsObject.title then
    res = res .. mw.ustring.format("|+ %s\n", argsObject.title)
  end

  res = res .. [[|-
| class="invisible" |
! scope="col" | Singulier
! scope="col" | Pluriel
|- class="flextable-fr-m"
! scope="row" | Masculin
]]

  local formatCell = function(inflection, class, colSpan)
    local upper = mw.ustring.upper(inflection)
    local inflectionsTable = { argsObject(argsObject) }
    local otherInflections = argsObject

    if otherInflections then
      inflectionsTable = { inflectionsTable, unpack(otherInflections(argsObject)) }
    end

    inflectionsTable = m_table.map(inflectionsTable, function(v)
      return mw.ustring.format("]", v)
    end)

    local pronsTable = {}
    local done = false
    local i = 1

    while not done do
      local prefix = argsObject(argsObject, i)
      local pron = argsObject(argsObject, i)
      local ref = argsObject

      if ref then
        -- FIXME modèle déprécié ?
        ref = mw.getCurrentFrame():expandTemplate { title = "réf", args = { ref } }
      else
        ref = ""
      end

      if pron then
        table.insert(
            pronsTable,
            (prefix or "") .. m_pron.lua_pron(pron, "fr") .. ref
        )
      else
        done = true
      end
      i = i + 1
    end

    if #pronsTable == 0 then
      table.insert(pronsTable, m_pron.lua_pron("", "fr"))
    end

    local inflections = table.concat(inflectionsTable, "<br/><small>ou</small> ")
    local prons = table.concat(pronsTable, "<br/><small>ou</small> ")

    return mw.ustring.format(
        '| colspan="%d" | <span class="flextable-fr-%s">%s<br/>%s</span>\n',
        colSpan, class, inflections, prons
    )
  end

  if argsObject:getMS() ~= argsObject:getMP() or argsObject:getMSPron(1) ~= argsObject:getMPPron(1) then
    res = res .. formatCell("ms", "ms", 1)
    res = res .. formatCell("mp", "mp", 1)
  else
    res = res .. formatCell("ms", "msp", 2)
  end

  res = res .. '|- class="flextable-fr-f"\n! scope="row" | Féminin\n'

  if argsObject:getFS() ~= argsObject:getFP() or argsObject:getFSPron(1) ~= argsObject:getFPPron(1) then
    res = res .. formatCell("fs", "fs", 1)
    res = res .. formatCell("fp", "fp", 1)
  else
    res = res .. formatCell("fs", "fsp", 2)
  end

  res = res .. "|}"

  if not argsObject.noCat then
    -- TODO catégoriser dans "Singuliers manquants en français" ou "Pluriels manquants en français" si nécessaire
  end

  return res
end

local function generateRootNotFoundMessage(templateName)
  local text = mw.ustring.format(
      "Usage erroné de %s.<br/>Le titre de la page ne comporte pas<br/>une des terminaisons attendues.",
      mw.getCurrentFrame():expandTemplate { title = 'M', args = { templateName } })
  return errorTable(text) .. "]"
end


------------------------
-- Template functions --
------------------------


-- TODO utile ?
----------------------------------------------------------------------------------------
-- récupération prononciation en param 2 ou 1 selon syntaxe ancienne/nouvelle
-- retourne PronRadic, PronFinInvar
-- fonction à usage transitoire, en attendant l'abandon définitif de l'ancienne syntaxe
----------------------------------------------------------------------------------------
-- tableau des cas possibles, hors cas particuliers :
--         entrées       :         sorties
--    radical arg1 arg2  : radical PronRad PronInv
-- (1)    R     -    -   :     R      -       -      (nouvelle)
-- (2)    R     R    -   :     R      -       -      (ancienne)
-- (3)    R     P    -   :     R      P       -      (nouvelle)
-- (4)    R     R    P   :     R      P       -      (ancienne)
-- (5)    R     P    I   :     R      P       I      (nouvelle)
local function nouvelle_ancienne_syntaxe_radic4(radic, args)
  if (not args) or (args == '') then
    --(1) syntaxe nouvelle, sans prononciation ou avec param nommés
    --(c’était une erreur dans l’ancienne syntaxe)
    return args or '', addSeparatorBeforeInvPart(args)

  elseif (radic ~= args) then
    --(3,5) syntaxe nouvelle, cas général : le param 1 n’est pas le radical, donc c’est la prononciation
    --(sinon c’est une erreur à corriger à la main)
    return args, addSeparatorBeforeInvPart(args or args)

  elseif (radic == 'b') then
    --cas particuliers des radicaux monosyllabes identiques à leur API où le test précédent échoue (comme pour "beau")
    -- liste des radicaux potentiellement concernés (WT:QT/décembre 2014#Problème métaphysique avec Modèle:fr-accord-eau + Module:fr-flexion) :
    -- b, bl, d, f, (k), (kl), l, m, n, p, pl, ps, s, t, ts, v, w, z.
    if args and (args ~= radic) then
      --(5) nouvelle syntaxe avec "prononciation partie invar" en args
      return radic, addSeparatorBeforeInvPart(args)

    else
      --(2,4) ancienne syntaxe sans ou avec pron en args (de tte façon dévinée comme égale à radic)
      --(3) nouvelle sans "prononciation partie invar" en args
      return radic, addSeparatorBeforeInvPart(args)

    end
  else
    --(2,4) syntaxe ancienne sans ou avec pron en args
    return args or '', addSeparatorBeforeInvPart(args)

  end
end

function p.errorBox(frame)
  -- TODO supprimer ? utilisée uniquement dans ]
  local msg = frame.args or frame:getParent().args or "erreur inconnue"
  local nocat = frame.args or frame:getParent().args
  return errorTable(msg, nocat)
end

function p.inflectionRegular(frame)
  local args = m_params.process(frame.args, {
     = { required = true },
     = {},
  })

  local inflectionType = args
  local otherParamName = args

  local argsDefinition = {
     = {},  = {},  = {},
     = {},  = {},  = {},
     = { alias_of = "pron" },

     = {},
     = { alias_of = "pinv" },

     = { default = mw.title.getCurrentTitle().text },
     = {},
  }

  if otherParamName then
    argsDefinition = { type = "boolean" }
  end

  local parentArgs = m_params.process(frame:getParent().args, argsDefinition)

  local pronPrefix1 = parentArgs
  local pronPrefix2 = parentArgs
  local pronPrefix3 = parentArgs
  local pronRoot1 = parentArgs
  local pronRoot2 = parentArgs
  local pronRoot3 = parentArgs
  local word = parentArgs
  local tableTitle = parentArgs

  -- Inflections detection
  local inflections = mw.loadData("Module:bac à sable/Darmo117/fr-flexion/data") -- TEMP nom data
  local alternateF = parentArgs
  local fIndex = alternateF and otherParamName or 1

  local suffixMS = inflections.ms
  local suffixMP = inflections.mp
  local suffixFS = inflections.fs
  local suffixFP = inflections.fp

  local pronSuffixMS = inflections.ms
  local pronSuffixMP = inflections.mp
  local pronSuffixFS = inflections.fs
  local pronSuffixFP = inflections.fp

  -- Word root extraction
  local root, invariablePart = splitWord(word, suffixMS, suffixMP, suffixFS, suffixFP)
  -- No root found, abort
  if not root then
    return generateRootNotFoundMessage("fr-accord-" .. inflectionType)
  end

  local invariablePronPart = addSeparatorBeforeInvPart(parentArgs)

  local argsObject = Args:new()

  argsObject.title = tableTitle
  argsObject.ms = root .. suffixMS .. invariablePart
  argsObject.mp = { root .. suffixMP .. invariablePart }
  argsObject.fs = { root .. suffixFS .. invariablePart }
  argsObject.fp = { root .. suffixFP .. invariablePart }

  local generateProns = function(pronRoot, pronPrefix)
    table.insert(argsObject.msProns, pronRoot .. pronSuffixMS .. invariablePronPart)
    table.insert(argsObject.mpProns, pronRoot .. pronSuffixMP .. invariablePronPart)
    table.insert(argsObject.fsProns, pronRoot .. pronSuffixFS .. invariablePronPart)
    table.insert(argsObject.fpProns, pronRoot .. pronSuffixFP .. invariablePronPart)
    if pronPrefix then
      table.insert(argsObject.msPronPrefixes, pronPrefix)
      table.insert(argsObject.mpPronPrefixes, pronPrefix)
      table.insert(argsObject.fsPronPrefixes, pronPrefix)
      table.insert(argsObject.fpPronPrefixes, pronPrefix)
    end
  end

  if pronRoot1 then
    generateProns(pronRoot1, pronPrefix1)
    if pronRoot2 then
      generateProns(pronRoot2, pronPrefix2)
      if pronRoot3 then
        generateProns(pronRoot3, pronPrefix3)
      end
    end
  end

  return p.generateTable(argsObject)
end

function p.inflectionBoxGeneric(frame)
  local args = m_params.process(frame:getParent().args, {
     = {},

     = { default = mw.title.getCurrentTitle().text },
     = {},  = {},
     = {},

     = {},  = {},
     = {},  = {},
     = {},

     = {},  = {},  = {},
     = {},  = {},  = {},
     = {},  = {},  = {},

     = {},  = {},  = {},
     = {},  = {},  = {},
     = {},  = {},  = {},

     = {},  = {},  = {},
     = {},  = {},  = {},
     = {},  = {},  = {},

     = {},  = {},  = {},
     = {},  = {},  = {},
     = {},  = {},  = {},

     = {},  = {},  = {},
     = {},  = {},  = {},
     = {},  = {},  = {},

     = {},  = {},  = {},
     = {},  = {},  = {},
     = {},  = {},  = {},

     = {},  = {},  = {},
     = {},  = {},  = {},
     = { alias_of = "pron" },

     = { type = "boolean" },
     = {}, -- XXX ?
  })

  local argsObject = Args:new()

  argsObject.title = args

  -- TODO extraire les paramètres

  argsObject.pronPrefixes = { args, args, args }
  argsObject.prons = { args, args, args }

  argsObject.noCat = args
end

function p.inflectionBox4(frame)
  -- TODO
end

function p.inflectionBox3(frame)
  -- TODO
end

function p.inflectionBox2(frame)
  -- TODO
end

p.Args = Args -- TEMP

return p