Jump to content

script runs successfully but no result


Recommended Posts

i have a script that tries to batch translate the text files in a folder from chinese to enlgish. the script runs successfully but doesnt create translated files

 

1468432069_Screenshot2023-01-15101306.png.415768f53f54e22b6416fd95d448725c.png

 

#Require
# Microsoft Azure Account and Bing Translator Text API Subscription Key
# AutoIt v3

; Create the FileListToArray() function
Func FileListToArray($folder, $search)
    Local $file, $i = 1
    Local $files[1] = 0
    Local $fileHandle = FileFindFirstFile($folder & $search)
    If @error = 0 Then
        $file = FileFindNextFile($fileHandle)
        While $file <> ""
            $files[$i] = $file
            $i += 1
            ReDim $files[$i]
            $file = FileFindNextFile($fileHandle)
        WEnd
        FileClose($fileHandle)
    Else
        MsgBox(16, "Error", "The folder does not exist or has no files matching the specified search criteria.")
        Exit
    EndIf
    Return $files
EndFunc

; Function to translate text using Bing Translator API
Func TranslateText($text, $from_lang, $to_lang)
    Local $subscriptionKey = ""
    Local $url = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&from=" & $from_lang & "&to=" & $to_lang

    Local $oHTTP = ObjCreate("WinHttp.WinHttpRequest.5.1")
    $oHTTP.Open("POST", $url)
    $oHTTP.SetRequestHeader("Content-Type", "application/json")
    $oHTTP.SetRequestHeader("Ocp-Apim-Subscription-Key", $subscriptionKey)
    $oHTTP.Send('[{"Text": "' & $text & '"}]')

    If $oHTTP.Status <> 200 Then
        MsgBox(16, "Error", "An error occurred while connecting to the Bing Translator API. HTTP status code: " & $oHTTP.Status & " " & $oHTTP.StatusText)
        Exit
    EndIf

    Local $response = $oHTTP.ResponseText
    Local $json = JSON_Decode($response)
    If @error <> 0 Then
        MsgBox(16, "Error", "An error occurred while decoding the JSON response from the Bing Translator API.")
        Exit
    EndIf
    Return $json[0]["translations"][0]["text"]
EndFunc

# Main Script

; Specify the folder containing the text files
Local $folder = @WorkingDir & "\test\"

; Get an array of all the files in the folder
Local $files = FileListToArray($folder, "*.txt")

; Define the source and target languages
Local $from_lang = "zh"
Local $to_lang = "en"

; Loop through the array of files
For $i = 1 To UBound($files)-1
    ; Read the text file
    Local $text = FileRead($folder & $files[$i])
    If @error <> 0 Then
        MsgBox(16, "Error", "An error occurred while reading the text file:" & $folder & $files[$i])
Exit
EndIf

; Translate the text
Local $translated_text = TranslateText($text, $from_lang, $to_lang)

; Write the translated text to a new file
FileOpen($folder & "translated_" & $files[$i], 2)
While @error = 5
    Sleep(1000)
    FileOpen($folder & "translated_" & $files[$i], 2)
WEnd
FileWrite($folder & "translated_" & $files[$i], $translated_text)
If @error <> 0 Then
    MsgBox(16, "Error", "An error occurred while writing the translated text to the file:" & $folder & "translated_" & $files[$i])
    Exit
EndIf
Next

; Notify the user that the translation is complete
MsgBox(0, "Translation Complete", "All files in the folder have been translated and saved with the prefix 'translated_'.")

 

Link to post
Share on other sites

Seems to me that you are getting zero file from FileListToArray.   I could be wrong though since I do not have your computer.  But overall, you need to learn how to debug your own scripts.  Put some ConsoleWrite and _ArrayDisplay after key statements to follow the path of your script and understand why it does not work as expected.

Link to post
Share on other sites

Hi.

There is an existing function to list files to an array.

I cannot test your translation part of your script, as I do not have...

#Require
# Microsoft Azure Account and Bing Translator Text API Subscription Key
# AutoIt v3

Due to notepad++.exe your files are UTF-8, so modify this code to PUT the content to the translation engine:

 

#include <FileConstants.au3>
#include <Debug.au3>
#include <File.au3>

$Dir = @ScriptDir
$aFiles = _FileListToArray(@ScriptDir, "*.txt")
_DebugArrayDisplay($aFiles)

For $i = 1 To $aFiles[0]
    $h = FileOpen($Dir & "\" & $aFiles[$i], $FO_UTF8)
    $Content = FileRead($h)
    FileClose($h)
    MsgBox(0, $aFiles[$i], $Content)
Next

 

Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...