Aller au contenu

Module:Linguistique

Cette page fait l’objet d’une mesure de semi-protection étendue.
Une page de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 27 septembre 2014 à 15:43 et modifiée en dernier par Zolo (discuter | contributions) (Nouvelle page : -- module d'origine multilingue utilisé pour Wikidata --, pourrait être simplifié pour usage francophone mais risque de diminuer l'interopérabilité local p = {} local f = re...). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.
(diff) ← Version précédente | Version actuelle (diff) | Version suivante → (diff)

 Documentation[voir] [modifier] [historique] [purger]

Utilisation

Fonctions exportables (liste incomplète) :

  • vowelfirst(str) retourne true si le premier caractère de la chaîne est une voyelle.
  • inparentheses(str, lang, space), si str est nil, retourne nil, sinon, retourne str entourée de parenthèses et précédée du caractère ou de la chaîne précisée dans le paramètre space.
  • of(str, gender, number, determiner, raw) retourne "de " .. str ou d'une chaîne similaire mais plus correcte grammaticalement. Paramètres :
  • str la chaîne de départ
  • gender (m ou (f) pour adaptation grammaticale ("du", "de la"). Défaut : "n".
  • number (s ou p) pour les accords grammaticaux ("des"). Défaut : s.
  • determiner (true ou false) pour adaptation grammaticale ("du" -> "de l'") Défaut : false.
  • raw chaîne non formatée pour savoir plus facilement si elle commence par une consonne ou une voyelle pour les élisions.
  • conj(args, conjtype) : concatène les arguments non nuls de la table args, en utilisant conjtype comme séparateur. Valeurs de conjtype :
  • "new line" : ajoute une ligne entre chaque arg, et met une majuscule au début de chaque argument.
  • "or" sépare l'avant-dernier et le dernier "or" par une " ou ", et les autres par une virgule.
  • "and" : sépare l'avant-dernier et le dernier "et" par une " ou ", et les autres par une virgule.
  • "comma" virgule entre chaque arg
  • autre chaîne tous les termes séparés par le terme indiqué
  • default = "and"
  • texteLien(str)
  • ucfirst(str) retourne la chaîne indiquée, en ajoutant une majuscule au premier caractère affiché
-- module d'origine multilingue utilisé pour Wikidata
--, pourrait être simplifié pour usage francophone mais risque de diminuer l'interopérabilité

local p = {}
local f = require('Module:Fallback')
local lang = 'fr'

local vowels = 'aeiouyąăẵằẳặȃắâẫấầẩậãäǟāáàȁǎảẚåǻḁạǡæǣǽĕȇêễếềểệḙẽḛëēḕéḗèȅěẻẹęȩḝǝĭȋîĩḭïḯīíìȉǐỉịįıŏȏôỗốồổộõṏṍöōṑóṓòȍǒỏọǫǭơỡớờởợøǿŭȗûṷũṻṹṵüǖǘǜǚṳūúùȕǔủůụųưữứừửựŷỹÿȳýỳỷẙỵ'
local function wordor(lang) 
	return f._langSwitch(require('Module:I18n/or'), lang)
end

local function comma(lang)
	m = mw.message.newFallbackSequence( "comma-separator" )
	m:inLanguage(lang)
	return m:plain()
end

function p.fullstop(lang)
	return f._langSwitch({
		en = '. ',
		fr = '. ',
	}, lang)
end

local function wordand(lang)
	local andtable = { -- languages with a problem with the MediaWiki:And
		['pl'] = ' i',
		['no'] = ' og',
		['zh'] = '和'
	}
	if andtable[lang] then
		return andtable[lang]
	end
	m = mw.message.newFallbackSequence( "and" )
	m:inLanguage(lang)
	return m:plain()
end

local function wordsep(lang) -- default separator between words
	m = mw.message.newFallbackSequence( "Word-separator" )
	m:inLanguage(lang)
	return m:plain()
end	

local function isin(str, pattern)
	if str and pattern and mw.ustring.find(str, pattern, 1, true ) then
		return true
	end
end

local function langisin(str, lang)
	return isin(str, lang .. ' ') -- space is necessary to avoid false positives like zh in zh-hans
end

local function processgender(str)
	if (str == 'f') or (str == 'fem') or (str == 'feminine') then 
		return 'feminine'
	elseif (str == 'n') or (str == 'neutral') then 
		return 'neutral'
	else
		return 'masculine'
	end
end

local function processnumber(str)
	if (str == 'p') or (str == 'plural') then
		return 'plural'
	else 
		return 'singular'
	end
end

function p.vowelfirst (str)
	if str then return isin(vowels, str[1]) end
end

function p.inparentheses(str, lang)
	--todo: define language with exotic parentheses
	if str == '' then
		return str
	else 
		return ' (' .. str .. ')' 
		-- needs internationalization. 
		--Needs leading space in Enlgish because as some languages do not use it, it is part of the formatting
	end
end

function p.of(word, lang, raw, gender, number, determiner) -- rough translation of "of" in various languages
-- note that the cases when on "of" is employed varies a lot among languages, so it is more prudent to call this from lang specific function only
	if not raw then 
		raw = word
	end
	gender = processgender(gender)
	number = processnumber(number)
	-- raw is the string without the Wikiformatting so that it correctly analyses the string that is [[:fr:Italie|Italie]] -> 'italie'
	-- any way to automate this ?
	
	-- todo: ca to replace Template:Of/ca
	
	if lang == 'fr' then 
		if number == 'plural' then
			return 'des ' .. word
		elseif p.vowelfirst(raw) then
			return 'de l\'' .. word
		elseif gender == 'feminine' then
			return 'de la ' .. word
		elseif derterminer then
			return 'du ' .. word
		else
			return 'de ' .. word
		end
	end	

end

function p.noungroup(noun, adj, lang)
	if not noun or noun == '' then 
		return nil -- not '' so that it is not counted as a string by mw.listToText
	end
	if not adj or adj == ''
		then return noun
	end
	-- adjective before the noun
	if langisin('de de-at de-ch en en-ca en-gb pl zh zh-hans zh-hant zh-my zh-cn zh-sg zh-tw ', lang) then
		return adj .. wordsep(lang) .. noun
	-- adjective after the noun
	elseif langisin('fr fr-ca es it') then
		return noun .. wordsep(lang) .. adj
	else
		return noun ' (' .. adj .. ')'
	end
end

function p.conj(args, lang, conjtype)
	if (not args) or (#args == 0) then
		return nil
	end
	local newargs = {}
	for i, j in pairs(args) do
		if type(j) ~= 'nil' then
			table.insert(newargs, j)
		end
	end
	args = newargs
	if conjtype == 'comma' then
		return mw.text.listToText(args, comma(lang), comma(lang))
	elseif conjtype == 'or' then 
		return mw.text.listToText(args, comma(lang), wordor(lang)  .. wordsep(lang))
	elseif conjtype == 'explicit or' then -- adds "or" betwen all words when the context can be confusing
		return mw.text.listToText(args, wordor(lang) .. wordsep(lang), wordor(lang)  .. wordsep(lang))
	elseif conjtype then
		return mw.text.listToText(args, conjtype, conjtype)
	else 
		return mw.text.listToText(args, comma(lang), wordand(lang) .. wordsep(lang))
	end
end

function p.conjfromWiki(frame)
	args = frame.args
	if not args or not args[1] then
		args = mw.getCurrentFrame():getParent().args
	end
	local conjtype = args.type
	local lang = args.lang
	if not lang or mw.text.trim(lang) == '' then
		lang = frame:preprocess( "{{int:lang}}" )
	end
	newargs = {}  -- transform args metatable into a table so it can be concetenated
	for i, j in pairs(args) do
			if type(i) == 'number' then
				j = mw.text.trim(j)
				if j ~= '' then
					table.insert(newargs, j)
				end
			else 
				if i ~= 'type' and i ~= 'lang' then 
					return 'error: bad parameter in template:Conj: ' .. i .. '[[Category:Pages with incorrect template usage/Conj|A]]'
				end
			end
	end
	return p.conj(newargs, lang, conjtype)
end

return p