Gestion de caractères accentués

Partagez des fonctions et des UDF AutoIt.
Règles du forum
.
Répondre
Jeep
Niveau 1
Niveau 1
Messages : 14
Enregistré le : jeu. 24 janv. 2019 16:34
Localisation : Belgique
Status : Hors ligne

Gestion de caractères accentués

#1

Message par Jeep »

Tout d'abord, un grand merci à tous pour la mine d'information mise à disposition. Etant nouveau sur ce site, je vous soumets deux petites fonctions qui pourront aider nos amis francophones:

La première _sStrFrAccentRemove permet de supprimer les accents dans une chaîne de caractères en se basant sur les caractères accentués du français en respectant la "case" des lettres.

Code : Tout sélectionner

#include <String.au3>
#include <StringConstants.au3>

	Func _sStrFrAccentRemove($sString)
	;&========================================================================================================================
	;& Description....: Convert characters with accent to a fom without accent based on french language
	;& Parameter(s)...:
	;&					$sString to convert
	;& Return value(s)
	;&		Function..: String converted
	;&		Error(s)..:	-
	;& -
	;& Versions.......: 1.0.0
	;& -
	;& Author(s)......: Jean-Pol Dekimpe (Jeep)
	;& Date...........: 2019/01/01
	;& Language.......: English
	;& AutoIt.........: v3.3.14.5
	;& -
	;& Remark(s)......: Conversion is based on french language
	;&========================================================================================================================
	Local $sFrom = "àáâãäåæçèéêëìíîïòóôöùúûüýÿœ", $sChar = "", $sOutString = ""
	Local $aTo = ["a", "a", "a", "a", "a", "a", "ae", "c", "e", "e", "e", "e", "i", "i", "i", "i", "o", "o", "o", "o", _
			"u", "u", "u", "u", "y", "y", "oe"]
	Local $aChars = StringSplit($sString, "")
	Local $iPos = 0

	For $i = 1 To $aChars[0]
		$sChar = $aChars[$i]
		$iPos = StringInStr($sFrom, $sChar)
		If $iPos = 0 Then
			$sOutString &= $sChar
		Else
			If StringIsUpper($sChar) Then
				$sOutString &= StringUpper($aTo[$iPos])
			Else
				$sOutString &= $aTo[$iPos]
			EndIf
		EndIf
	Next
	Return $sOutString
EndFunc   ;==>_sStrFrAccentRemove
La seconde fonction permet de mettre en capital la première lettre de chaque mot dans une phrase comprenant des caractères accentués. Si un caractère accentué doit être mis en majuscule l'accent est retiré. Contrairement à la fonction proposée en standard dans Autoit la lettre suivant le caractère apostrophe (') n'est pas mise en majuscule (sujet très discuté sur le site .com) pour ma part cela colle mieux au français.

Code : Tout sélectionner

Func _sStrTitleCase($sString, $sLanguage = "FR")
	;&========================================================================================================================
	;& Description....: Uppercase first letter of each word + support a French context
	;&					No uppercase after "'"
	;& Parameter(s)...:
	;&					$sString - string of characters to convert
	;&					$sLanguage = "FR" like French to process accent characters; They are removed before converting to
	;&                               uppercase
	;& Return value(s)
	;&		Function..: resulting string
	;&		Error(s)..: -
	;& -
	;& Versions.......: 1.0.0
	;& -
	;& Author(s)......: Jean-Pol Dekimpe (Jeep)
	;& Date...........: 2019/01/01
	;& Language.......: English
	;& AutoIt.........: v3.3.14.5
	;& -
	;& Remarks........: -
	;&========================================================================================================================
	Local $bCapNext = True
	Local $sChr = "", $sReturn = ""
	Local $aChars = StringSplit($sString, "")

	For $i = 1 To $aChars[0]
		$sChr = $aChars[$i]
		Select
			Case $bCapNext = True
				If StringRegExp($sChr, "[a-zA-Z\xC0-\xFF0-9]") Then
					If $sLanguage = "FR" Then $sChr = (_sStrFrAccentRemove($sChr))
					$sChr = StringUpper($sChr)
					$bCapNext = False
				EndIf
s.			Case ($sLanguage <> "FR" And Not StringRegExp($sChr, "[a-zA-Z\xC0-\xFF'0-9]")) Or _
					(Not StringRegExp($sChr, "(*UCP)[a-zA-Z\xC0-\xFF0-9'’]"))
				$bCapNext = True
			Case Else
				$sChr = StringLower($sChr)
		EndSelect
		$sReturn &= $sChr
	Next
	Return $sReturn
EndFunc   ;==>_sStrTitleCase
Voici un petit exemple, avec en prime une version simplifiée de ConsoleWrite (_CW). Examiner le code dans les fonctions ..._Eval, lancer l'application et observer se qui se passe dans la console.

Code : Tout sélectionner

#include <String.au3>
#include <StringConstants.au3>

_sStrFrAccentRemove_eval();
_sStrTitleCase_eval()
Exit

Func _sStrFrAccentRemove_eval()
	Local $sText = "l'élève de première année va à la piscine"
	_CW($sText)
	$sText = _sStrFrAccentRemove($sText)
	_CW($sText)
EndFunc

Func _sStrTitleCase_eval()
	;_sStrTitleCase($sString, $sLanguage = "FR")
	Local $sText = "l'élève de première année va à la piscine"
	_CW(_sStrTitleCase($sText))
	Local $sText = "I can't get now, satisfaction ..."
	_CW(_sStrTitleCase($sText))

EndFunc

;-----------------------------------------------------------------------------------------------------------------------------
Func _CW($sText, $sBOL = "Trace >", $sEOL = "<", $bNewLine = True)
	;&=======================================================================================================================
	;& Description....: Write a message on console with a specified begin and end of Line
	;& Parameter(s)...:
	;&					$sText - Text to write
	;&					$sBOL - Begin of line
	;&					$sEOL -  End of Line
	;&					$bNewLine : put a new line after writing the message
	;& Return value(s)
	;&		Function..: -
	;&		Error(s)..: -
	;& -
	;& Versions.......:	1.1.0
	;& -
	;& Author(s)......: Jean-Pol Dekimpe (Jeep)
	;& Date...........: 2019/01/15
	;& Modify ........:	-
	;& -
	;& Remark(s)......: simplify use of ConsoleWrite
	;========================================================================================================================
	If $bNewLine Then $sEOL &= @CRLF
	ConsoleWrite($sBOL & $sText & $sEOL)
EndFunc   ;==>_CW

Func _sStrFrAccentRemove($sString)
	;&========================================================================================================================
	;& Description....: Convert characters with accent to a fom without accent based on french language
	;& Parameter(s)...:
	;&					$sString to convert
	;& Return value(s)
	;&		Function..: String converted
	;&		Error(s)..:	-
	;& -
	;& Versions.......: 1.0.0
	;& -
	;& Author(s)......: Jean-Pol Dekimpe (Jeep)
	;& Date...........: 2019/01/01
	;& Modify ........:	-
	;& -
	;& Remark(s)......: Conversion is based on french language
	;&========================================================================================================================
	Local $sFrom = "àáâãäåæçèéêëìíîïòóôöùúûüýÿœ", $sChar = "", $sOutString = ""
	Local $aTo = ["a", "a", "a", "a", "a", "a", "ae", "c", "e", "e", "e", "e", "i", "i", "i", "i", "o", "o", "o", "o", _
			"u", "u", "u", "u", "y", "y", "oe"]
	Local $aChars = StringSplit($sString, "")
	Local $iPos = 0

	For $i = 1 To $aChars[0]
		$sChar = $aChars[$i]
		$iPos = StringInStr($sFrom, $sChar)
		If $iPos = 0 Then
			$sOutString &= $sChar
		Else
			If StringIsUpper($sChar) Then
				$sOutString &= StringUpper($aTo[$iPos])
			Else
				$sOutString &= $aTo[$iPos]
			EndIf
		EndIf
	Next
	Return $sOutString
EndFunc   ;==>_sStrFrAccentRemove

Func _sStrTitleCase($sString, $sLanguage = "FR")
	;&========================================================================================================================
	;& Description....: Uppercase first letter of each word + support a French context
	;&					No uppercase after "'"
	;& Parameter(s)...:
	;&					$sString - string of characters to convert
	;&					$sLanguage = "FR" like French to process accent characters; They are removed before converting to
	;&                               uppercase
	;& Return value(s)
	;&		Function..: resulting string
	;&		Error(s)..: -
	;& -
	;& Versions.......: 1.0.0
	;& -
	;& Author(s)......: Jean-Pol Dekimpe (Jeep)
	;& Date...........: 2019/01/01
	;& Language.......: English
	;& AutoIt.........: v3.3.14.5
	;& -
	;& Remarks........: -
	;&========================================================================================================================
	Local $bCapNext = True
	Local $sChr = "", $sReturn = ""
	Local $aChars = StringSplit($sString, "")

	For $i = 1 To $aChars[0]
		$sChr = $aChars[$i]
		Select
			Case $bCapNext = True
				If StringRegExp($sChr, "[a-zA-Z\xC0-\xFF0-9]") Then
					If $sLanguage = "FR" Then $sChr = (_sStrFrAccentRemove($sChr))
					$sChr = StringUpper($sChr)
					$bCapNext = False
				EndIf
			Case ($sLanguage <> "FR" And Not StringRegExp($sChr, "[a-zA-Z\xC0-\xFF'0-9]")) Or _
					(Not StringRegExp($sChr, "(*UCP)[a-zA-Z\xC0-\xFF0-9'’]"))
				$bCapNext = True
			Case Else
				$sChr = StringLower($sChr)
		EndSelect
		$sReturn &= $sChr
	Next
	Return $sReturn
EndFunc   ;==>_sStrTitleCase

n'hésitez pas à me soumettre vos commentaires, j'essayerai d'y répondre dans la mesure de mes disponibilités.
Un problème complexe peut souvent se découper en problèmes simples. Mais où est la scie ?
Répondre