Jump to content

Text to speech Google traslator CZ letters


amirekko
 Share

Recommended Posts

Hi, sorry for bad English. I need help with this script:

http://pingturtle.com/home/post/google-text-to-speech-with-autoit

If I let convert text without Czech letters so the result is okay. However, if I write for converting words with hooks or commas above the letters, and these letters are correctly converted to speech. I need advice on how to call a URL to convert text to speech in order to use Czech letters - words.

Manually submitting Czech text in the URL is correct and the text is read correctly. Example:

http://translate.google.com/translate_tts?tl=cs&q=%C5%BElut%C3%A1%20ko%C4%8Di%C4%8Dka

 

Thank you so much for your help and advice.

Link to comment
Share on other sites

#include <_Language.au3>   ; http://autoit.oo3.co/user/autoit/UDF/_Language.au3
_Speak("Tento text ve vašem jazyce. Doufám, že zvláštní dopisy jsou vyslovovány správne.")
 

_Language.au3 UDF:

 

; #INDEX# =======================================================================================================================
; Title ...............: _Language
; File Name............: _Language.au3
; File Home............: http://autoit.oo3.co/user/autoit/UDF/_Language.au3
; File Version.........: 1.0.0.0
; Min. AutoIt Version..: v3.3.12.0
; Description .........: Translate and speak using Google
; Author ..............: Exit ( http://www.autoitscript.com/forum/user/45639-exit )
; ===============================================================================================================================

; #INCLUDES# ===================================================================================================================
#include-Once
#include <Array.au3>
#include <Sound.au3>
#include <String.au3>
#include <IE.au3>
#include <INet.au3>
;================================================================================================================================

; #CURRENT# =====================================================================================================================
; _Speak
; _Translate
; _Language_ShowAllLanguages
; --------# internal use #----------
; _Language_ClipSecurity
; _Language_DefaultLanguage
; _Language_Detect
; _Language_Info
; _Language_IsTTS
; _Language_Split
; ===============================================================================================================================

; #FUNCTION# ====================================================================================================================
; Name...........: _Speak
; Description ...: Speak text in many languages
; Syntax.........: _Speak($sText = "", $sTargetLanguage = "", $iMode = 0)
; Parameters ....: $sText - [optional] some text to speak.
; $sText="" (Default) get text from clipboard.
; $sText="<" get text from selection in active window.
; $sText="<<" get text from selection in previous window.
; $sTargetLanguage - [optional] eg. "en" (en/fr/it/es/de/pl/...).
; $sTargetLanguage="" No translation.
; $sTargetLanguage="=" Translate to local system language.
; Invalid TargetLanguage results in no output or output in local language.
; $iMode - [optional] define output.
; 0 - [Default] Soundplay the text and return the language id. Wait for complwttion.
; 1 - Soundplay the text and return the language id. Do not wait for completion.
; 2 - Return the MP3 string.
; 3 - Return the fullpath of the MP3 file.
; Return values .: Success - Returns data according to iMode and sets @error=0 and @extennded=Filesize of MP3 file.
; Failure - Returns 0 and sets @error:
; | 1 - Cannot detect language
; | 2 - Language has no Text to Speech interface
; Author ........: Exit ( http://www.autoitscript.com/forum/user/45639-exit )
; CopyLeft ......: © Freeware by "Exit" ( all wrongs reserved )
; Examples ......:
; _Speak() ; speak clipboard text
; _Speak("<<") ; speak selected text of previous window
; _Speak("","en") ; speak clipboard text in English language
; _Speak(Default,Default,2) ; return MP3 fullpath of clipboard txt
; _Speak("This is an English text")
; _Speak("Dies ist ein deutscher Text")
; _Speak("Se trata de un texto español")
; _Speak("Questo è un testo italiano")
; _Speak("Ceci est un texte en français")
; _Speak("Die Vorführung ist beendet.")
; _Speak("Die Vorführung ist beendet.","=") ; translate and Speak in local system language
;================================================================================================================================
Func _Speak($sText = "", $sTargetLanguage = "", $iMode = 0)
Local $sSourceLang, $aText, $sTemp, $sMP3 = "", $sFile, $hFile, $sReturn = "", $iFileSize = 0
$iMode = (($iMode = 1 Or $iMode = 2 Or $iMode = 3) ? $iMode : 0)
If $sText = Default Or $sText = "" Then $sText = ClipGet()
If $sText = "<<" Then Send("!{ESC}") ; back to previous window.
If StringLeft($sText, 1) = "<" Then
Sleep(100)
$sTemp = ClipGet() ; save clipboard
Send("^c") ; copy selected.
Sleep(100)
$sText = ClipGet()
ClipPut($sTemp); restore old clipboard
EndIf
If $sTargetLanguage = Default Then $sTargetLanguage = ""
If $sTargetLanguage = "=" Then $sTargetLanguage = _Language_DefaultLanguage()
If Not _Language_IsTTS($sTargetLanguage) Then Return SetError(3, 0, 0)
If Not $sTargetLanguage = "" Then $sText = _Translate($sText, $sTargetLanguage)
$sSourceLang = _Language_Detect($sText)
If @error Then Return SetError(2, 0, 0)
$aText = _Language_Split($sText)
$sFile = @TempDir & "\~" & @ScriptName & ".~Temp.mp3"
FileDelete($sFile)
$hFile = FileOpen($sFile, 2 + 16)
For $i = 0 To UBound($aText) - 1
$sMP3 &= _INetGetSource("http://translate.google.com/translate_tts?ie=UTF-8&tl=" & $sSourceLang & "&q=" & $aText[$i])
Next
FileWrite($hFile, $sMP3)
FileClose($hFile)
$iFileSize = FileGetSize($sFile)
If $iMode = 3 Then Return SetError(0, $iFileSize, $sFile)
If $iMode = 2 Then Return SetError(0, $iFileSize, $sMP3)
_SoundPlay($sFile, ($iMode ? 0 : 1))
FileDelete($sFile)
Return SetError(0, 0, $sSourceLang)
EndFunc ;==>_Speak

; #FUNCTION# ====================================================================================================================
; Name...........: _Translate
; Description ...: Translate text to many languages
; Syntax.........: _Translate($sText = "", $sTargetLanguage = "", $sOutFile = "", $iShow = 0)
; Parameters ....: $sText - [optional] some text to translate.
; $sText="" (Default) get text from clipboard.
; $sText="<" get text from selection in active window.
; $sText="<<" get text from selection in previous window.
; $sTargetLanguage - [optional] eg. "en" (en/fr/it/es/de/pl/...).
; $sTargetLanguage="" (Default) Translate to systems defeult language.
; Invalid TargetLanguage results in no output or output in systems defeult language.
; $sOutFile - [optional] define fullpath of output file.
; "" - [Default] Create no output file.
; $iShow - [optional] Disposition of browser window.
; 0 - [Default] Close hidden browser window.
; 1 - Set browser window to visible.
; Return values .: Success - Returns translated string and sets @error=0 and @extennded=Stringlen of translated string.
; Failure - Returns 0 and sets @error:
; | 1 - No internet connection to Google Translate domain
; | 2 - Unable to translate text
; | 3 - Error writing output file
; Author ........: Exit ( http://www.autoitscript.com/forum/user/45639-exit )
; CopyLeft ......: © Freeware by "Exit" ( all wrongs reserved )
;================================================================================================================================
Func _Translate($sText = "", $sTargetLanguage = "", $sOutFile = "", $iShow = 0)
Local $oIE, $oObject, $rc, $gc
If $sText = Default Or $sText = "" Then $sText = ClipGet()
If $sText = "<<" Then Send("!{ESC}") ; back to previous window.
If StringLeft($sText, 1) = "<" Then
Sleep(100)
$sTemp = ClipGet() ; save clipboard
Send("^c") ; copy selected.
Sleep(100)
$sText = ClipGet()
ClipPut($sTemp); restore old clipboard
EndIf
If $sTargetLanguage = Default Or $sTargetLanguage = "" Then $sTargetLanguage = _Language_DefaultLanguage()
If $sOutFile = Default Then $sOutFile = ""
If $iShow <> 1 Then $iShow = 0
_Language_ClipSecurity()
$oIE = _IECreate("https://translate.google.com/?hl=en&tab=wT#auto/" & $sTargetLanguage & "/", 0, 0)
If @error Then Return SetError(1, 0, 0)
$oObject = _IEGetObjById($oIE, "source")
_IEAction($oObject, "focus")
$sTemp = ClipGet() ; save clipboard
ClipPut($sText)
_IEAction($oObject, "paste")
ClipPut($sTemp); restore old clipboard
$oObject = _IEGetObjById($oIE, "result_box")
For $i = 1 To 50
$rc = _IEPropertyGet($oObject, "innertext")
If $rc <> "" Then ExitLoop
Sleep(100)
Next
$gc = ($iShow ? _IEAction($oIE, "visible") : _IEQuit($oIE))
If $rc = "" Then Return SetError(2, 0, 0)
If Not $sOutFile = "" Then
If Not FileWrite($sOutFile, $rc) Then Return SetError(3, StringLen($rc), 0)
EndIf
Return SetError(0, StringLen($rc), $rc)
EndFunc ;==>_Translate

; #FUNCTION# ====================================================================================================================
; Name...........: _Language_ShowAllLanguages
; Description ...: Show all enabled language IDs
; Syntax.........: _Language_ShowAllLanguages($iMode = 0, $iOutput = 0)
; Parameters ....: $iMode - show TTS info dynamic.
; 0= no (default) uses static inline info.
; 1= yes (use dynamic Google info. (WARNING: On excessive use, GOOGLE turn this feature off and delays response)
; $iOutput - display output
; 0 = (default) display array
; 1 = no display
; Return values .: Success - Returns zero-based two-dimesional array.
; [..][0]=language-id
; [..][1]=language-name
; [..][2]=TTS indicator
; Failure - Returns empty string and sets @error:
; 1 = No internet connection to http://translate.google.com
; Author ........: Exit ( http://www.autoitscript.com/forum/user/45639-exit )
; CopyLeft ......: © Freeware by "Exit" ( all wrongs reserved )
;================================================================================================================================
Func _Language_ShowAllLanguages($iMode = 0, $iOutput = 0)
Local $rc, $aTemp
$rc = _INetGetSource("http://translate.google.com")
If @error Then Return SetError(1, 0, 0)
$rc = _StringBetween($rc, "value=separator disabled>", "</select>")
$rc = _StringBetween($rc[1], "<option value=", "</option>")
Local $aLang[UBound($rc)][3]
For $i = 0 To UBound($rc) - 1
$aTemp = StringSplit($rc[$i], ">", 2)
$aLang[$i][0] = $aTemp[0]
$aLang[$i][1] = $aTemp[1]
If _Language_IsTTS($aTemp[0], $iMode) Then $aLang[$i][2] = "TTS enabled"
Next
If Not $iOutput Then _ArrayDisplay($aLang, UBound($aLang) & " Languages ", "", "", "", "Lang. ID|Language Name|" & ($iMode ? "TTS" : " "))
Return SetError(0, 0, $aLang)
EndFunc ;==>_Language_ShowAllLanguages

; #FUNCTION# ====================================================================================================================
; Name...........: _Language_ClipSecurity
; Description ...: Set registry to allow clipboard access from HTTPS://translate.google.com
; Syntax.........: _Language_ClipSecurity()
; Return values .: Success - Returns 1
; Failure - Returns 0 and sets @error:
; | 1 = unable to open requested key
; | 2 = unable to open requested main key
; | 3 = unable to remote connect to the registry
; | -1 = unable to open requested value
; | -2 = value type not supported
; Author ........: Exit ( http://www.autoitscript.com/forum/user/45639-exit )
; CopyLeft ......: © Freeware by "Exit" ( all wrongs reserved )
;================================================================================================================================
Func _Language_ClipSecurity()
Local $iSecTemp = RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\translate.google.com", "https")
If Not @error And $iSecTemp = 2 Then Return SetError(0, 0, 1)
$iSecTemp = RegWrite("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\translate.google.com", "https", "REG_DWORD", 2)
Return SetError(@error, 0, $iSecTemp)
EndFunc ;==>_Language_ClipSecurity

; #FUNCTION# ====================================================================================================================
; Name...........: _Language_DefaultLanguage
; Description ...: Get system default language id.
; Syntax.........: _Language_DefaultLanguage()
; Return values .: System default language id.
; Author ........: Exit ( http://www.autoitscript.com/forum/user/45639-exit )
; CopyLeft ......: © Freeware by "Exit" ( all wrongs reserved )
;================================================================================================================================
Func _Language_DefaultLanguage()
Local $aTemp = DllCall('kernel32.dll', 'int', 'GetLocaleInfoW', 'ulong', 0, 'dword', 109, 'wstr', '', 'int', 2048)
Return SetError(0, 0, $aTemp[3])
EndFunc ;==>_Language_DefaultLanguage

; #FUNCTION# ====================================================================================================================
; Name...........: _Language_Detect
; Description ...: Detect the language of a given text
; Syntax.........: _Language_Detect($sText)
; Parameters ....: $sText - Text to analyze.
; Return values .: Success - Returns the language id of the text.
; Failure - Returns empty string and sets @error:
; | 1 - Cannot detect input language
; Author ........: Exit ( http://www.autoitscript.com/forum/user/45639-exit )
; CopyLeft ......: © Freeware by "Exit" ( all wrongs reserved )
;================================================================================================================================
Func _Language_Detect($sText)
Local $rc
$sText = StringRegExpReplace($sText, "\W", " ") ; change non-word chars to blank.
$rc = _INetGetSource("http://translate.google.com/translate_a/t?client=t&ie=UTF-8&sl=auto&text=" & StringLeft($sText, 500))
$rc = _StringBetween($rc, ',[["', '"]],')
If @error Then Return SetError(1, 0, "")
$rc = $rc[UBound($rc) - 1]
If StringInStr($rc, ",") Then Return SetError(1, 0, "")
Return SetError(0, 0, $rc)
EndFunc ;==>_Language_Detect

; #FUNCTION# ====================================================================================================================
; Name...........: _Language_Info
; Description ...: Provides information of all supported languages.
; Syntax.........: _Language_Info ($sLanguage, $iMode = 0)
; Parameters ....: $sLanguage - LanguageId, eg. "en" (en/fr/it/es/de/pl/...).
; $iMode - 0= Test for all languages.
; 1= Test for TTS enabled languages.
; Return values .: Success - Returns 1
; Failure - Returns 0 and sets @error:
; | 2 - Language not found
; Author ........: Exit ( http://www.autoitscript.com/forum/user/45639-exit )
; CopyLeft ......: © Freeware by "Exit" ( all wrongs reserved )
;================================================================================================================================
Func _Language_Info($sLanguage, $iMode = 0)
Local $rc
Static Local $sAllLang = " af sq ar hy az eu bn bs bg ceb zh-TW zh-CN da de en eo et fi fr gl ka el gu ht ha iw hi hmn ig id ga is it ja jw yi kn ca km ko hr lo la lv lt ms mt mi mr mk mn ne nl no fa pl pt pa ro ru sv sr sk sl so es sw tl ta te th cs tr uk hu ur vi cy be yo zu "
Static Local $sTTSLang = " af sq ar hy bs zh-TW zh-CN da de en eo fi fr el ht hi id is it ja ca ko hr la lv mk nl no pl pt ro ru sv sr sk es sw ta th cs tr hu vi cy "
$rc = StringInStr(($iMode ? $sTTSLang : $sAllLang), " " & $sLanguage & " ")
Return SetError(($rc ? 0 : 2), 0, ($rc ? 1 : 0))
EndFunc ;==>_Language_Info

; #FUNCTION# ====================================================================================================================
; Name...........: _Language_IsTTS
; Description ...: Detect if language has TTS interface
; Syntax.........: _Language_IsTTS($sLanguage,$iMode=0)
; Parameters ....: $sLanguage - LanguageId, eg. "en" (en/fr/it/es/de/pl/...).
; $iMode - show TTS info dynamic.
; 0= no (default) uses static inline info.
; 1= yes (use dynamic Google info. (WARNING: On excessive use, GOOGLE turn this feature off and delays response)
; Return values .: Success - Returns 1
; Failure - Returns 0 and sets @error:
; | 1 - No TTS interface available
; Author ........: Exit ( http://www.autoitscript.com/forum/user/45639-exit )
; CopyLeft ......: © Freeware by "Exit" ( all wrongs reserved )
;================================================================================================================================
Func _Language_IsTTS($sLanguage, $iMode = 0)
Local $rc
If $sLanguage = "" Then $sLanguage = _Language_DefaultLanguage()
If $iMode Then
$rc = _INetGetSource("http://translate.google.com/translate_tts?ie=UTF-8&tl=" & $sLanguage & "&q=A")
Else
$rc = _Language_Info($sLanguage, 1)
EndIf
Return SetError(($rc ? 0 : 1), 0, ($rc ? 1 : 0))
EndFunc ;==>_Language_IsTTS


; #FUNCTION# ====================================================================================================================
; Name...........: _Language_Split
; Description ...: Splits a text in chunks of less than 100 chars.
; Syntax.........: _Language_Split($sText)
; Parameters ....: $sText - Text to split.
; Return values .: Array of text chunks.
; Author ........: Exit ( http://www.autoitscript.com/forum/user/45639-exit )
; CopyLeft ......: © Freeware by "Exit" ( all wrongs reserved )
;================================================================================================================================
Func _Language_Split($sText)
Local $aText[0], $sTempl, $sTemp
$sText = _Language__WinHttpURLEncode($sText & " ")
While StringLen($sText)
$sTemp = StringLeft($sText, 99)
$sTempl = StringInStr($sTemp, ".+", 0, -1)
If $sTempl Then
$sTempl += 1
Else
$sTempl = StringInStr($sTemp, "%2C+", 0, -1)
If $sTempl Then
$sTempl += 3
Else
$sTempl = StringInStr($sTemp, "+", 0, -1)
If Not $sTempl Then $sTempl = 99
EndIf
EndIf
_ArrayAdd($aText, StringLeft($sTemp, $sTempl))
$sText = StringTrimLeft($sText, $sTempl)
WEnd
Return SetError(0, 0, $aText)
EndFunc ;==>_Language_Split

Func _Language__WinHttpURLEncode($vData)
; Author... ...........: trancexx, ProgAndy
; #include <WinHttp.au3> ; http://code.google.com/p/autoit-winhttp/downloads/list
If IsBool($vData) Then Return $vData
Local $aData = StringToASCIIArray($vData, Default, Default, 2)
Local $sOut
For $i = 0 To UBound($aData) - 1
Switch $aData[$i]
Case 45, 46, 48 To 57, 65 To 90, 95, 97 To 122, 126
$sOut &= Chr($aData[$i])
Case 32
$sOut &= "+"
Case Else
$sOut &= "%" & Hex($aData[$i], 2)
EndSwitch
Next
Return $sOut
EndFunc

; # End of_Language UDF #========================================================================================================
Edited by Exit

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

  • Moderators

Hmm...

; #FUNCTION# ====================================================================================================================
; Name...........: _Language_MouseClick
; Description ...: Simulates a mouseclick
; Syntax.........: _Language_MouseClick(ByRef $o_obj, $s_id)
; Parameters ....: $o_obj - Reference to an inter explorer object.
; $s_id - Id name of the object to click.
; Return values .: 0 (unable to detect errors)
; Author ........: Exit ( http://www.autoitscript.com/forum/user/45639-exit )
; CopyLeft ......: © Freeware by "Exit" ( all wrongs reserved )
Func _Language_MouseClick(ByRef $o_obj, $s_id)
$s_JS = "function _Language_MouseClick (node, eventType) {;" & _
"var clickEvent = document.createEvent('MouseEvents'); " & _
"clickEvent.initEvent (eventType, true, true); " & _
"node.dispatchEvent (clickEvent);}" & _
"Button = document.getElementById('" & $s_id & "');" & _
"_Language_MouseClick (Button, 'mousedown');" & _
"_Language_MouseClick (Button, 'mouseup');"
Execute("$o_obj.document.parentwindow.execScript($s_JS)")
EndFunc ;==>_Language_MouseClick

 

:naughty:

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...