Class Translate


  • public class Translate
    extends java.lang.Object
    Translate (普通话, Simplified & Traditional Chinese) Characters.

    This provides a marginally simpler way to access Google Cloud Server's Translate API, with a specification-requirement of using Mandarin. It converts the JSON, automatically, into Java Vector's of Strings. Translations can be provided, of Mandarin, in English, Spanish, and even the "Traditional Character Set" of Mandarin.


Stateless Class: This class neither contains any program-state, nor can it be instantiated. The @StaticFunctional Annotation may also be called 'The Spaghetti Report'. Static-Functional classes are, essentially, C-Styled Files, without any constructors or non-static member field. It is very similar to the Java-Bean @Stateless Annotation.
  • 1 Constructor(s), 1 declared private, zero-argument constructor
  • 5 Method(s), 5 declared static
  • 0 Field(s)


    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method
      static void article​(Vector<String> simpSentencesIN, Vector<Vector<String>> sentencesOUT, Vector<Vector<Vector<String>>> wordTablesOUT, Vector<String> DOUTArr, Vector<Boolean> DOUTErrorBoolArr)
      static boolean block​(String simpSentenceIN, Vector<String> sentencesOUT, Vector<Vector<String>> wordTableOUT, Appendable DOUT)
      static String getEnglish​(String chineseWord)
      static String getPinYin​(String chineseWord)
      static String[] sentenceZH​(String chinese)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • article

        🡇    
        public static void article​
                    (java.util.Vector<java.lang.String> simpSentencesIN,
                     java.util.Vector<java.util.Vector<java.lang.String>> sentencesOUT,
                     java.util.Vector<java.util.Vector<java.util.Vector<java.lang.String>>> wordTablesOUT,
                     java.util.Vector<java.lang.String> DOUTArr,
                     java.util.Vector<java.lang.Boolean> DOUTErrorBoolArr)
                throws java.io.IOException
        
        Throws:
        java.io.IOException
        Code:
        Exact Method Body:
         for (String simpSentence : simpSentencesIN)
         {
             Vector<String>          sentences   = new Vector<String>();
             Vector<Vector<String>>  wordTable   = new Vector<Vector<String>>();
             StringBuilder           DOUT        = new StringBuilder();
             boolean                 error       = block(simpSentence, sentences, wordTable, DOUT);
        
             sentencesOUT.add(sentences);
             wordTablesOUT.add(wordTable);
             DOUTArr.add(DOUT.toString());
             DOUTErrorBoolArr.add(Boolean.valueOf(error));
         }
        
      • block

        🡅  🡇    
        public static boolean block​
                    (java.lang.String simpSentenceIN,
                     java.util.Vector<java.lang.String> sentencesOUT,
                     java.util.Vector<java.util.Vector<java.lang.String>> wordTableOUT,
                     java.lang.Appendable DOUT)
                throws java.io.IOException
        
        Throws:
        java.io.IOException - The interface java.lang.Appendable mandates that the IOException must be treated as a checked exception for all output operations. Therefore IOException is a required exception in this method' throws clause.
        Code:
        Exact Method Body:
         String[]    gtScrape        = sentenceZH(simpSentenceIN);
         String      pronSentence    = gtScrape[0];
         String      englSentence    = gtScrape[1];
        
         sentencesOUT.removeAllElements();
         sentencesOUT.add(simpSentenceIN);
         sentencesOUT.add(pronSentence);
         sentencesOUT.add(englSentence);
        
         Vector<String>  simpWords   = new Vector<String>();
         Vector<String>  pronWords   = new Vector<String>();
         boolean         errorParse  = PinYinParse.parse
                             (DOUT, simpSentenceIN, pronSentence, simpWords, pronWords);
        
         if (pronWords.size() != simpWords.size()) throw new IllegalStateException(
             "The pronunciation and the character vector's should be the exact same length.\n" +
             "pronWords.size() = " + pronWords.size() + " and simpWords.size() = " + 
             simpWords.size()
         );
        
         int len = pronWords.size();
         for (int i=0; i < len; i++)
         {
             Vector<String> vocabEntryRow = new Vector<String>();
        
             vocabEntryRow.add(simpWords.elementAt(i));
             vocabEntryRow.add(pronWords.elementAt(i));
             vocabEntryRow.add(""); //Dictionary.lookupTrad(simp, pron));
             vocabEntryRow.add(""); //Dictionary.lookupEngl(simp, pron));
        
             wordTableOUT.add(vocabEntryRow);
         }
         return errorParse;
        
      • sentenceZH

        🡅  🡇    
        public static java.lang.String[] sentenceZH​(java.lang.String chinese)
        This receives as input a sentence in simplified Mandarin Chinese. If it finds a period in it, it breaks the sentence up into smaller bricks based around the period. It queries Google Translate using this sentence.
        Parameters:
        chinese - Any sentence, paragraph, phrase or word in Simplified-Mandarin
        Returns:
        Two separate Strings returned in a String array - two elements long.
        1. ret[0] - The pronunciation (罗马拼音) String scraped from a call to Google Translate
        2. ret[0] - The English - also scraped from a call to http://translate.google.com
        Code:
        Exact Method Body:
         if (chinese.indexOf('\n') != -1) throw new IllegalArgumentException("CHINESE:\t" + chinese + "\nContains a newline!");
        
         String[]        cArr            = chinese.trim().split("。");
         StringBuilder   completePron    = new StringBuilder();
         StringBuilder   completeEngl    = new StringBuilder();
        
         for (int i=0; i < cArr.length; i++)
         {
             // Prepare the queries and scrape http://translate.google.com/query web-page.
             Vector<HTMLNode>    page        = null;
             int                 retryCount  = 0;
        
             while ((page == null) && (retryCount < 6))
                 try {
                     String          chineseQ    = URLs.toProperURLV2(cArr[i] + "。");
                     BufferedReader  br          = Scrape.openConn_iso_8859_1("https://translate.google.com/?q=" + chineseQ);
                     page                        = HTMLPage.getPageTokens(br, false);
                 } catch (Exception e) {
                     retryCount++;
                     System.out.println("RETRY-SCRAPE Google Translate:\n" + "Attempt #" + retryCount + "\n" + e.getMessage());
                 }
        
             // Get Chinese PinYin as Sentence
             StringBuilder       pron    = new StringBuilder();
             Vector<HTMLNode>    partial = InnerTagGetInclusive.first(page, "div", "id", TextComparitor.EQ_CASE_INSENSITIVE, "src-translit");
        
             Util.removeAllTagNodes(partial);
             for (HTMLNode n : partial) pron.append(((TextNode) n).str);
             completePron.append(Escape.replace(pron.toString()).trim() + "  ");
        
        
             // Get English from Translate Website as a Sentence
             StringBuilder   engl    = new StringBuilder();
             partial                 = InnerTagGetInclusive.first(page, "span", "id", TextComparitor.EQ_CASE_INSENSITIVE, "result_box");
             Util.removeAllTagNodes(partial);
             for (HTMLNode n : partial) engl.append(((TextNode) n).str);
             completeEngl.append(Escape.replace(engl.toString().replaceAll("\\\\u200b", "")).trim() + "  ");
         }
         String [] retArr = { completePron.toString(), completeEngl.toString() };
         return retArr;
        
      • getPinYin

        🡅  🡇    
        public static java.lang.String getPinYin​(java.lang.String chineseWord)
                                          throws java.io.IOException
        Retrieves the PinYin pronunciation from Google Translate Servers for a single Chinese Word.
        Parameters:
        chineseWord - Any single word in simplified Mandarin Chinese
        Returns:
        The Pinyin Pronunciation of that word, stripped by Google Translate Servers.
        Throws:
        java.io.IOException
        Code:
        Exact Method Body:
         BufferedReader      br      = Scrape.openConn_iso_8859_1("https://translate.google.com/?q=" + chineseWord + "&source=zh-CN");
         Vector<HTMLNode>    page    = HTMLPage.getPageTokens(br, false);
         Vector<HTMLNode>    partial = InnerTagGetInclusive.first(page, "div", "id", TextComparitor.EQ_CASE_INSENSITIVE, "src-translit");
         String              pron    = "";
        
         Util.removeAllTagNodes(partial);
         for (HTMLNode n : partial) pron += ((TextNode) n).str;
         return Escape.replace(pron);
        
      • getEnglish

        🡅    
        public static java.lang.String getEnglish​(java.lang.String chineseWord)
                                           throws java.io.IOException
        Retrieves the Google Translate (English) Textbox-defintion for a particular Mandarin Chinese Word.
        NOTE: This is not the information under the primary/main translation-text-box, this is the translation-text-box word itself.
        Parameters:
        chineseWord - Any single word in simplified Mandarin Chinese
        Returns:
        The Google Translate Server's best attempt at a Translation.
        Throws:
        java.io.IOException
        Code:
        Exact Method Body:
         BufferedReader      br      = Scrape.openConn_iso_8859_1("https://translate.google.com/?q=" + chineseWord);
         Vector<HTMLNode>    page    = HTMLPage.getPageTokens(br, false, null, "matches.txt", null);
         Vector<HTMLNode>    partial = InnerTagGetInclusive.first(page, "span", "id", TextComparitor.EQ_CASE_INSENSITIVE, "result_box");
         String              engl    = "";
        
         Util.removeAllTagNodes(partial);
         for (HTMLNode n : partial) engl += ((TextNode) n).str;
         return Escape.replace(engl);