Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help with using GoogleTranslate.java program #5

Closed
OpalJellyfish opened this issue Aug 9, 2018 · 1 comment
Closed

Help with using GoogleTranslate.java program #5

OpalJellyfish opened this issue Aug 9, 2018 · 1 comment

Comments

@OpalJellyfish
Copy link

OpalJellyfish commented Aug 9, 2018

Hi, I am getting an exception when I run the program given below. The input file is a text file containing the details of 500 tweets. JavaGoogleSpeechAPISample is a class which is the same as this: java-google-speech-api/src/main/java/com/darkprograms/speech/translator/GoogleTranslate.java . When I executed the program the first time, it managed to translate 19 tweets but when I tried executing it again, it immediately gave me this exception without translating any tweets. Please help me fix this issue.

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 503 for URL: http://www.google.com/sorry/index?continue=http://translate.google.com/translate_a/single%3Fclient%3Dwebapp%26hl%3Den%26sl%3Dauto%26tl%3Den%26q%3DRT%26multires%3D1%26otf%3D0%26pc%3D0%26trs%3D1%26ssel%3D0%26tsel%3D0%26kc%3D1%26dt%3Dt%26ie%3DUTF-8%26oe%3DUTF-8%26tk%3D922019.533213&hl=en&q=EgR1wEmUGN2RsNsFIhkA8aeDSx_FqW2PwJSg7Q9x4dKugT-g9ylxMgFy
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
	at gettingtweets3.JavaGoogleSpeechAPISample.urlToText(JavaGoogleSpeechAPISample.java:173)
	at gettingtweets3.JavaGoogleSpeechAPISample.translate(JavaGoogleSpeechAPISample.java:149)
	at gettingtweets3.JavaGoogleSpeechAPISample.translate(JavaGoogleSpeechAPISample.java:130)
	at gettingtweets3.TranslatingTweets4.main(TranslatingTweets4.java:45)
package gettingtweets3;

import java.util.*;
import java.util.regex.*;
import java.io.*;
import java.io.IOException;

public class TranslatingTweets4 {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        Scanner console = new Scanner(System.in);
        Scanner input = checkFileName(console);
        PrintStream output = outputFile(console);
        System.out.println();
        
        while (input.hasNextLine()) {
            String line = input.nextLine();
            Pattern pattern = Pattern.compile("^(.*?said:\\s*)(.*)$");
            Matcher matcher = pattern.matcher(line);
            
            if(matcher.find()) {
                //Prints the offset after the last character matched.
                output.print(matcher.group(1));
                String tweet = matcher.group(2);
                
//                //Translate tweet by tweet
//                output.println(JavaGoogleSpeechAPISample.translate("en", tweet));
                
                //Translate word by word
                Scanner tweetScan = new Scanner(tweet);
                while (tweetScan.hasNext()) {
                    String word = tweetScan.next();
                    output.print(JavaGoogleSpeechAPISample.translate("en", word) + " ");                          
                }
                output.println(); //for translating word by word
            }
        }
    }
    
    //Uses the given Scanner object to prompt the user for an input file name
    //until the user provides a valid input file name. Then returns a scanner
    //to read the contents of the input file.
    //Parameter needed:
    // console = a user-input scanner which prompts the user for an input file name
    //           and reads the user's input
    public static Scanner checkFileName(Scanner console) throws FileNotFoundException {
       System.out.print("Input file name: ");
       File inputFile = new File(console.nextLine());
       while (!inputFile.canRead()) {
          System.out.print("File not found. Try again: ");
          inputFile = new File(console.nextLine());
       }
       System.out.println();
       return new Scanner(inputFile);
    }
   
    //Uses the given Scanner object to prompt the user for an output file name.
    //Then returns a PrintStream to print the completed mad lib into the output file.
    //Parameter needed:
    // console = a user-input scanner which prompts the user for an output file name
    //           and reads the user's input
    public static PrintStream outputFile(Scanner console) throws FileNotFoundException {
       System.out.print("Output file name: ");
       return new PrintStream(new File(console.nextLine()));
    }
}

@goxr3plus

@goxr3plus
Copy link
Owner

Which key did you used ? Did you created you own ?

GSpeechDuplex duplex = new GSpeechDuplex("GOOGLE_API_KEY");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants