Jump to content

extending a RegEx Pattern


Recommended Posts

The idea to use translation api:

i used the script from @mikell to build this func:

Func _Translate($sFrom, $from, $to)
    ;thanks to mikell (autoitscript.com)
    ;https://www.autoitscript.com/forum/topic/182893-prompt-me-how-to-see-the-text-in-the-translation-boxhttpstranslategooglecom/?do=findComment&comment=1313423
    Local $url = "https://translate.googleapis.com/translate_a/single?client=gtx"
    $url &= "&sl=" & $from & "&tl=" & $to & "&dt=t&q=" & $sFrom

    Local $oHTTP = ObjCreate("Microsoft.XMLHTTP")
    $oHTTP.Open("POST", $url, False)
    $oHTTP.Send()
    Local $sData = $oHTTP.ResponseText
    $sData = StringRegExpReplace($sData, '.*?\["(.*?)"[^\[]*', "$1" & @CRLF)
    Return $sData
EndFunc   ;==>_Translate

when i call this func with:

$sText='AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying "runtimes" required!'
MsgBox(64,'',_Translate($sText,'en','de'))

nearly all is :thumbsup: seeing here:

79_Done.jpg

only the "!" is wrong "\" but when using 'auto' instead of 'en' the result is:

80_Done.jpg

2 lines are appended. So my question is, is it possible to extend the pattern (i never worked with regex) and in best case setting @extended with the detected language?

@Trong: as you can see yet i am returning translated text and don't use GuiCtrlSetData to assign it to a EditBox.

Edited by AutoBert
Link to comment
Share on other sites

This is tricky, using one expression only there is a risk of 'catastrophic backtracking'
Personally I'd prefer to do it in 2 steps and get a resulting array (a bit longer but reliable)

Func _Trad($mytext, $from, $to)
   $url = "https://translate.googleapis.com/translate_a/single?client=gtx"
   $url &= "&sl=" & $from & "&tl=" & $to & "&dt=t&q=" & $mytext
   $oHTTP.Open("POST", $url, False)
   $oHTTP.Send()
   $sData = $oHTTP.ResponseText()
   Local $aData[2]  ; text & language
   $tmp = StringRegExpReplace($sData, '\[\[(.*?)\]\].*', "$1")
   $aData[0] = StringRegExpReplace($tmp, '.*?\["(.*?)(?<!\\)"[^\[]*', "$1" & @crlf)
   $tmp2 = StringReplace($sData, $tmp, "")
   $aData[1] = StringRegExpReplace($tmp2, '.*?"([^"]+).*', "$1")
   Return $aData
EndFunc

 

Link to comment
Share on other sites

Using this:

#include <Array.au3>

$sText='AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying "runtimes" required!'
$sData=_Trad($sText,'auto','de')
Switch @extended
    case 1
        $sLang='en'
    case 2
        $sLang='de'
EndSwitch

MsgBox(64,'Done ('&$sLang&')',$sData)

Func _Trad($mytext, $from, $to)
   Local $url = "https://translate.googleapis.com/translate_a/single?client=gtx"
   $url &= "&sl=" & $from & "&tl=" & $to & "&dt=t&q=" & $mytext
    Local $oHTTP = ObjCreate("Microsoft.XMLHTTP")
   $oHTTP.Open("POST", $url, False)
   $oHTTP.Send()
   $sData = $oHTTP.ResponseText()
   ConsoleWrite('====================================================================='&@CRLF)
   ConsoleWrite($sData&@CRLF)
   ConsoleWrite('====================================================================='&@CRLF)
   Local $aData[2]  ; text & language
   $tmp = StringRegExpReplace($sData, '\[\[(.*?)\]\].*', "$1")
   $aData[0] = StringRegExpReplace($tmp, '.*?\["(.*?)(?<!\\)"[^\[]*', "$1" & @crlf)
   $tmp2 = StringReplace($sData, $tmp, "")
   $aData[1] = StringRegExpReplace($tmp2, '.*?"([^"]+).*', "$1")
   ;_ArrayDisplay($aData)
   if $aData[1] <> '' Then SetError(0, 1); set later the Index getting from Array of supported languages.
   Return $aData[0]
EndFunc

the problem with the 2 appended lines (detected language) is solved, but in the "De-ESCaping" of the text the problem is a little bit better. In the nicght i don't saw that instead of expected :

Quote

ohne störendes "Runtimes" erforderlich!

result was:

Quote

ohne störendes \

now it's:

Quote

ohne störendes \"Runtimes\" erforderlich!

81_Done (en).jpg

seems \"  ==> " is the only problem left. But i can do this with StringReplace. The greates problem isn't clientside, so the user have to modify the result bevor using with C&P.

Edited by AutoBert
Link to comment
Share on other sites

Without your help i had to wrote whole translate func with String*-funcs and _StringBetween, so >95% work is done by you :thumbsup:  and the one needed StringReplace is already added:

Func _TranslateEX($mytext, $from, $to)
    ;thanks to mikell (autoitscript.com)
    ;idea https://www.autoitscript.com/forum/topic/182893-prompt-me-how-to-see-the-text-in-the-translation-boxhttpstranslategooglecom/?do=findComment&comment=1313423
    ;and https://www.autoitscript.com/forum/topic/183054-extending-a-regex-pattern/?do=findComment&comment=1314560
    Local $url = "https://translate.googleapis.com/translate_a/single?client=gtx"
    $url &= "&sl=" & $from & "&tl=" & $to & "&dt=t&q=" & $mytext
    Local $oHTTP = ObjCreate("Microsoft.XMLHTTP")
    $oHTTP.Open("POST", $url, False)
    $oHTTP.Send()
    $sData = $oHTTP.ResponseText()
    Local $aData[2] ; text & language
    $tmp = StringRegExpReplace($sData, '\[\[(.*?)\]\].*', "$1")
    $aData[0] = StringRegExpReplace($tmp, '.*?\["(.*?)(?<!\\)"[^\[]*', "$1" & @CRLF)
    $aData[0] = StringReplace($aData[0], '\"', '"')
    ;ConsoleWrite('====================================================================='&@CRLF)
    ;ConsoleWrite($aData[0]&@CRLF)
    ;ConsoleWrite('====================================================================='&@CRLF)
    $tmp2 = StringReplace($sData, $tmp, "")
    $aData[1] = StringRegExpReplace($tmp2, '.*?"([^"]+).*', "$1")
    ;_ArrayDisplay($aData)
    #cs
    If $aData[1] <> '' Then
        _GetLangIndex($aData[1])
        SetError(0, @extended); set later Index in Array of supported languages.
    EndIf
    #ce
    Return $aData[0]
EndFunc   ;==>_TranslateEX

Thank's a lot.

Link to comment
Share on other sites

2 hours ago, mikell said:

Using GuiRegisterMsg  you can even do simultaneous translation,

I have 2 Editcontrols one with the text to be translated, the other with the translated responcetext. So there is no need to translate simulataneous. The whole script is thougt as a example, showing how to build a script, changing tooltips in depency of the choosen language.

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

×
×
  • Create New...