#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <UpDownConstants.au3>
#include <ButtonConstants.au3>
;~ $Words_String = "According to the results of studies of one English university, " & _
;~ "it does not matter in what order the letters are in the word. " & _
;~ "The main thing is that the first and last letters were on they places. " & _
;~ "Other letters can be fully disorder, " & _
;~ "you can still able to read the text without problems. " & _
;~ "The reason for this is that we do not read every letter individually, but all the word entirely."
$Words_String = "По результатам исследований одного английского университета, не имеет значения, " & _
"в каком порядке расположены буквы в слове. Главное, чтобы первая и последняя буквы были на месте. " & _
"Остальные буквы могут следовать в полном беспорядке, все равно текст читается без проблем. " & _
"Причиной этого является то, что мы не читаем каждую букву по отдельности, а все слово целиком."
$GUI = GUICreate("Words Transformer", 500, 300)
GUICtrlCreateLabel("Type something bellow and hit the 'Transform' button.", 20, 10)
$Words_Edit = GUICtrlCreateEdit($Words_String, 20, 40, 460, 180, BitOR($ES_WANTRETURN, $WS_VSCROLL, $ES_AUTOVSCROLL))
GUICtrlSetFont(-1, 12)
GUICtrlCreateLabel("Type Number of characters to keep in place:", 20, 231)
$CharsPreserv_Input = GUICtrlCreateInput(2, 250, 230, 40, 18, $ES_READONLY)
GUICtrlCreateUpdown(-1, $UDS_ARROWKEYS)
GUICtrlSetLimit(-1, 5, 1)
$TransformWords_Button = GUICtrlCreateButton("Transform", 20, 260, 80, 20, BitOr($GUI_SS_DEFAULT_BUTTON, $BS_DEFPUSHBUTTON))
$ResetEdit_Button = GUICtrlCreateButton("Reset to defaults", 110, 260, 120, 20)
$Close_Button = GUICtrlCreateButton("EXIT", 240, 260, 80, 20)
ControlFocus($GUI, "", $TransformWords_Button)
GUISetState(@SW_SHOW, $GUI)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE, $Close_Button
Exit
Case $ResetEdit_Button
GUICtrlSetData($Words_Edit, $Words_String)
GUICtrlSetData($CharsPreserv_Input, 2)
Case $TransformWords_Button
Local $sEdit_Read = StringFormat(GUICtrlRead($Words_Edit))
Local $iCharsPreserv_Read = Number(GUICtrlRead($CharsPreserv_Input))
If $iCharsPreserv_Read < 1 Then $iCharsPreserv_Read = 1
GUICtrlSetData($Words_Edit, TransformWords_Proc($sEdit_Read, $iCharsPreserv_Read))
EndSwitch
WEnd
Func TransformWords_Proc($sWords_String, $iCharsPreserv=1)
If StringStripWS($sWords_String, 8) = "" Then Return SetError(1, 0, $sWords_String)
Local $sRet_Words_String = "", $iLastCharIsDot = (StringRight($sWords_String, 1) = ".")
Local $sFirst_Chars, $sLast_Chars, $sMidle_Chars, $iStringLen
Local $aSplit_Words = StringRegExp($sWords_String & ".", "([а-я-А-Я\w']+).*?([\s\?\.:;,!]).*?", 3)
For $i = 0 To UBound($aSplit_Words)-1
$iStringLen = StringLen($aSplit_Words[$i])
If $iStringLen <= $iCharsPreserv+1 Or ($iCharsPreserv * 2) > $iStringLen Then
$sRet_Words_String &= $aSplit_Words[$i]
If StringRegExp($aSplit_Words[$i], "[\?\.:;,!]") Then $sRet_Words_String &= " "
ContinueLoop
EndIf
$sFirst_Chars = StringLeft($aSplit_Words[$i], $iCharsPreserv)
$sLast_Chars = StringRight($aSplit_Words[$i], $iCharsPreserv)
$sMidle_Chars = StringTrimRight(StringMid($aSplit_Words[$i], $iCharsPreserv+1), $iCharsPreserv)
$sRet_Words_String &= $sFirst_Chars & _StringRandomize($sMidle_Chars) & $sLast_Chars
Next
$sRet_Words_String = StringStripWS($sRet_Words_String, 3)
If $sRet_Words_String = "" Then Return $sWords_String
If Not $iLastCharIsDot And StringRight($sRet_Words_String, 1) = "." Then Return StringTrimRight($sRet_Words_String, 1)
Return $sRet_Words_String
EndFunc
Func _StringRandomize($sString)
If StringLen($sString) = 0 Then Return SetError(1, 0, "")
Local $aStr = StringSplit($sString, ''), $iRandom, $sTemp, $sRet_Str = ''
For $i = 1 To $aStr[0]
$sTemp = $aStr[$i]
If $i <> $aStr[0] Then
$iRandom = Random($i, $aStr[0], 1)
Else
$iRandom = $aStr[0]
EndIf
$aStr[$i] = $aStr[$iRandom]
$aStr[$iRandom] = $sTemp
$sRet_Str &= $aStr[$i]
Next
Return $sRet_Str
EndFunc