Translate subtitles using Machine Translation (google-api)


The following code is using google translation API which you can download form
google-api-translate-java-0.95.jar

Uses: java GoogleSubTitle [subtitle-file]

import java.io.BufferedReader;
import java.io.FileReader;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;

class GoogleSubTitle{

        private void translate(String inputfile){
                try{
                        String line  = null;
                        BufferedReader br = new BufferedReader(new FileReader(inputfile));
                        Translate.setHttpReferrer("en-hi");
                        while( (line = br.readLine())!= null ){
                                if( line.trim().length() > 0 &&
                                    "0123456789".indexOf( line.trim().charAt(0) ) < 0 ){
                                     String translatedText = Translate.execute(line.trim().toLowerCase(),Language.ENGLISH, Language.HINDI);
/* currently the system is translating english to hindi , source and target language you can change here */
                                     System.out.println(translatedText);
                                }
                                else{
                                        System.out.println(line);
                                }
                                Thread.sleep(250);
                        }
                        br.close();
                }catch(Exception e){
                        e.printStackTrace();
                }
        }
        public static void main(String[] args){
                GoogleSubTitle st = new GoogleSubTitle();
                if(args.length > 0){
                        st.translate( args[0] );
                }else{
                        System.out.println("Input File is missing");
                }
}
Advertisements

One thought on “Translate subtitles using Machine Translation (google-api)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: