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

Translate English subtitles to Hindi (using MaTra)


Following source-code is using C-DAC Mumbai’s Machine Translation System MaTra. It also need json-org.jar for json parsing which you can download from json-org.jar

Uses: java MaTraSubTitle [english-subtitle-file]

import org.json.JSONObject;
import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;

public class MaTraSubTitle{

        private void translate(String inputfile){
                try{
                        String line  = null;
                        BufferedReader br = new BufferedReader(new FileReader(inputfile));

                        while( (line = br.readLine())!= null ){

                                if( line.trim().length() > 0 &&
                                    "0123456789".indexOf( line.trim().charAt(0) ) < 0 ){
                                        String translation = getTranslate(line.trim());
                                        String parseT  = parse( translation );
                                        parseT = parseT.replaceAll("&#x","\\\\u");
                                        parseT = parseT.replaceAll("&#","\\\\U");
                                        System.out.println(convert(parseT) );
                                }
                                else{
                                        System.out.println(line);
                                }
                                Thread.sleep(250);
                        }
                        br.close();

                }catch(Exception e){
                        e.printStackTrace();
                }
        }

        private String getTranslate(String sent){
                String translation = "";
                sent = sent.replaceAll(" ","%20");
                try{
                        URL matra = new URL("http://www.cdacmumbai.in/matra/matra_ajax.jsp?sentence='"+sent+"'");
                        URLConnection uc = matra.openConnection();
                        BufferedReader in = new BufferedReader( new InputStreamReader( uc.getInputStream()));
                        String inputLine;
                        while ((inputLine = in.readLine()) != null)
                                translation += " "+ inputLine;
                        in.close();

                }catch(Exception e){
                        e.printStackTrace();
                }
        return translation.trim();
        }
        public static String convert(String s)
        {
                int i=0,len=s.length();
                char c;
                StringBuffer sb = new StringBuffer(len);
                while (i<len) {
                        c = s.charAt(i++);
                        if (c=='\\') {
                                if (i<len) {
                                        c = s.charAt(i++);
                                        if (c=='u') {
                                                c = (char) Integer.parseInt(s.substring(i,i+4).trim(),16);
                                                i += 4;
                                        } // add other cases here as desired...
                                        else if( c== 'U'){
                                                c = (char) Integer.parseInt(s.substring(i,i+4).trim(),10);
                                                i += 4;
                                        }
                                }
                        } // fall through: \ escapes itself, quotes any character but u
                        sb.append(c);
                }
                return sb.toString().replaceAll(";","");

        }


        private String parse(String translation){
                 String value = translation;
                 try{
                        JSONObject obj = new JSONObject( translation ) ;
                        value = obj.getString("Devanagari");
                }catch(Exception e){
                        e.printStackTrace();
                }
        return value;
        }

        public static void main(String[] args){
                MaTraSubTitle st = new MaTraSubTitle();
                if(args.length > 0){
                        st.translate( args[0] );
                }else{
                        System.out.println("Input File is missing");
                }
        }
}

Unicode to string conversion in Java


To convert the given Unicode to an character:
i.e String unicode = “0”+91+D1+””; to a Unicode character

Following method can use in Java

char getUnicode(String codePoint) {
   return (char)  Integer.parseInt(codePoint, 16);
}
or 

String string = "0061";
char c = (char) Integer.parseInt(string, 16);
System.out.println(c);

Hello world!


Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

Blog at WordPress.com.

%d bloggers like this: