import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.methods.PostMethod; public class DRBLTranslator { ArrayList lines; ArrayList IsTranslated; ArrayList HasBeginQuote; ArrayList HasEndQuote; ArrayList lstrs; ArrayList rstrs; ArrayList quotes; ArrayList tstrs; ArrayList ends; String output; public DRBLTranslator() { lines = new ArrayList(); IsTranslated = new ArrayList(); HasBeginQuote = new ArrayList(); HasEndQuote = new ArrayList(); lstrs = new ArrayList(); rstrs = new ArrayList(); quotes = new ArrayList(); tstrs = new ArrayList(); ends = new ArrayList(); output = ""; } public void reset() { lines.clear(); IsTranslated.clear(); HasBeginQuote.clear(); HasEndQuote.clear(); lstrs.clear(); rstrs.clear(); quotes.clear(); tstrs.clear(); ends.clear(); output = ""; } private static String googleTranslate(String langpair, String text) { /*********************************************************************** * commons-logging commons-codec */ HttpClient client = new HttpClient(); /*********************************************************** */ client.getHostConfiguration().setHost("www.google.com", 80, "http"); /*********************************************************** */ PostMethod post = new PostMethod("/translate_t"); // post.addParameter(new NameValuePair("url", url)); post.addParameter("langpair", langpair); post.addParameter("text", text); String s = null; try { /*********************************************************** */ client.executeMethod(post); /*********************************************************** */ InputStream in = post.getResponseBodyAsStream(); BufferedReader buf = new BufferedReader(new InputStreamReader(in)); Pattern p = Pattern .compile(".*id=\"*result_box\"*\\ *dir=\"*ltr\"*\\>"); Matcher m = null; String line; while ((line = buf.readLine()) != null) { m = p.matcher(line); // System.out.println(line); if (m.find()) { s = line.substring(m.end()); line = s; p = Pattern.compile("\\<\\ */\\ *div\\ *\\>"); m = p.matcher(line); if (m.find()) { s = line.substring(0, m.start()); } break; } } // s = GetTranslation.instostr(in); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return s; } public static String instostr(InputStream in) throws IOException { StringBuffer out = new StringBuffer(); byte[] b = new byte[4096]; for (int n; (n = in.read(b)) != -1;) { out.append(new String(b, 0, n)); } return out.toString(); } public void dump() { for (int i=0; i1) tstrs.add(""); if ((i!=0 || (i==0 && rstrs.size()==1)) && (((i%300)==0) || i==(rstrs.size()-1)) ) { //System.out.println("i="+i); //System.out.println("lstr="+lstrs.get(i)); tstr = DRBLTranslator.googleTranslate(langpair, str).trim() .replaceAll("\\s*"\\s*", "\"") .replaceAll("\\s*>\\s*", ">") .replaceAll("\\s*'\\s*", "'"); str = ""; tstrs.addAll(Arrays.asList(tstr.split("\\s*
\\s*"))); //System.out.println("tstr="+tstrs.get(i)); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public void toFile(String out) { try { BufferedWriter writer = new BufferedWriter(new FileWriter(out)); writer.write(getConfiguration()); writer.flush(); writer.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void srcToFile(String langpair, String in, String out) { loadFile(in); translate(langpair); toConfiguration(); toFile(out); } public static void main(String[] args) throws HttpException, IOException { if (null == args || args.length < 3) { System.out .println("Usage: java DRBLTranslator (language_pair) (source_file) (target_file)"); System.out .println("Example: java DRBLTranslator \"en|zh-TW\" \"en_US\" \"zh_TW\""); System.exit(1); } DRBLTranslator trans = new DRBLTranslator(); trans.srcToFile(args[0], args[1], args[2]); //trans.srcToFile("en|zh-TW", "en_US", "zh_TW"); System.out.println("ok"); //System.out.println(trans.toConfiguration()); //trans.dump(); } }