Advertisement
J2897

IBM Watson Language Translator - AutoHotkey Interface

Oct 18th, 2018
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Released under the GNU General Public License version 3+ by J2897.
  2. #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
  3. ;#Warn  ; Enable warnings to assist with detecting common errors.
  4. SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
  5. SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
  6. FileEncoding UTF-8 ; Converts text from files into Unicode format.
  7.  
  8. ; Typing "{gt" will launch Google Translate.
  9. ; Typing "{np" will launch Naver Papago.
  10. ; Anything you type between curly braces will be automatically translated.
  11. ; The closing curly brace is what triggers the action.
  12.  
  13. ; You need a username and password to use this service...
  14. ; https://console.bluemix.net/docs/services/language-translator/getting-started.html
  15.  
  16. ; Other requirements...
  17. ; 1. curl:                  https://curl.haxx.se/download.html
  18. ; 2. JQ:                    https://stedolan.github.io/jq/download/
  19. ; 3. AutoHotkey v1.1.30.00: https://autohotkey.com/download/
  20.  
  21. ; Note that, if a Spell Checker changes your words, those changes will not be recorded.
  22.  
  23. LANGUAGES := "en-ko" ; Translate from and to: https://console.bluemix.net/docs/services/language-translator/identifiable-languages.html
  24. USERNAME := "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
  25. PASSWORD := "XXXXXXXXXXXX"
  26. API := "https://gateway.watsonplatform.net/language-translator/api/v3/translate?version=2018-05-01"
  27. IBM_TRANSLATION_JSON := A_Temp "\IBM_Watson.json"
  28. IBM_TRANSLATION_TXT := A_Temp "\IBM_Watson.txt"
  29. CURL_EXE := "C:\Users\" A_UserName "\Programs\curl\bin\curl.exe"
  30. JQ_EXE := "C:\Users\" A_UserName "\Programs\jq\jq-win32.exe"
  31.  
  32. MsgBox IBM Watson Language Translator AutoHotkey interface loaded.
  33.  
  34. ~{::
  35. Input, UserInput, V T60 L400 C, {}}, np,gt
  36. if (ErrorLevel = "Max") {
  37.     MsgBox, You entered "%UserInput%", which is the maximum length of text.
  38.     return
  39. }
  40. if (ErrorLevel = "Timeout") {
  41.     MsgBox, You entered "%UserInput%" at which time the input timed out.
  42.     return
  43. }
  44. if (ErrorLevel = "NewInput")
  45.     return
  46. If InStr(ErrorLevel, "EndKey:") {
  47.     RunWait, cmd /c "%CURL_EXE% --user %USERNAME%:%PASSWORD% --request POST --header "Content-Type: application/json" --data "{\"text\": [\"%UserInput%\"]`, \"model_id\":\"%LANGUAGES%\"}" %API% >%IBM_TRANSLATION_JSON%"
  48.     RunWait, cmd /c "type %IBM_TRANSLATION_JSON% | %JQ_EXE% -j .translations[].translation >%IBM_TRANSLATION_TXT%"
  49.     FileReadLine, FF, %IBM_TRANSLATION_TXT%, 1 
  50.     UILengh := StrLen(UserInput) + 2
  51.     Send, {backspace %UILengh%}{Text}%FF%
  52.     clipboard = %FF%
  53.     return
  54. }
  55. if (UserInput = "np") {
  56.     Send, {backspace 3}
  57.     Run, https://papago.naver.com
  58. }
  59. else if (UserInput = "gt") {
  60.     Send, {backspace 3}
  61.     Run, http://translate.google.com
  62. }
  63. return
  64.  
  65. ; Reload after saving (CTRL+S).
  66. #ifwinactive, *C:\Users\John\Code\AHK\TranslatorIBM.ahk - Notepad++
  67. ~^s::
  68. Send, ^s
  69. Reload
  70. return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement