Class GCSTAPI


  • public class GCSTAPI
    extends java.lang.Object
    Google Cloud Services, Translation API Wrapper.

    This handles the "Google Cloud Server Translate API"

    NOTE: Google already has a Java-based Translate-API set of "jar files." Unfortunately, I have never been permitted to use them by "the dude." I have been "encouraged" to make the web-calls (with JSON responses) myself, and they have worked great. Generally, the Google jar-files/libraries for its Google-Cloud-Server API's are quite difficult to understand. Google tried to "do everything computer" and really wound up making very simple concepts difficult, and the difficult stuff stayed/remained difficult. ALSO: These methods can handle generalized (read: for any human-language) that Google can handle.



    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 fields. It is a concept very similar to the Java-Bean's @Stateless Annotation.

    • 1 Constructor(s), 1 declared private, zero-argument constructor
    • 2 Method(s), 2 declared static
    • 3 Field(s), 3 declared static, 2 declared final
    • Fields excused from final modifier (with explanation):
      Field 'key' is not final. Reason: CONFIGURATION


    • Field Summary

      Fields 
      Modifier and Type Field
      static String key
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method
      static String sentence​(String text, LC srcLang, LC targetLang)
      static Vector<String> wordByWord​(Vector<String> words, LC srcLang, LC targetLang)
      • Methods inherited from class java.lang.Object

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

      • key

        🡇     🗕  🗗  🗖
        public static java.lang.String key
        This is a key that may be obtained from Google Corporation. Complete the form for "Cloud Server Login" and then try adding the Translation package. Google expects you to pay for translation services, but single articles are usually a penny or so. Long articles with many translations can become expensive. Set this key to a specific value to register it.

        NOTE: This class only has static methods for brevity and simplicity. If you wish to use multiple keys for different projects, well, copy the code from these two methods and add them to one of your own classes - for "multi-threaded" applications. Otherwise you might also just change/update the public static String key field whenver you wish to bill your translations to a different billing project, card or account.

        ALSO NOTE: Use of this Java Package will not transmit your key to me (Ralph P. Torello), I'm not trying to steal your google key or account. Obviously, I cannot prove this Java Package does not make outgoing connections to any server I own, but, Guess What? It DOESN'T!

        If you do not set the key field here, Google Cloud Server Translate API will not respond to your query. Google bills each query on a word by word basis. See their metrics on the GCS Translate API documentation website.
    • Method Detail

      • sentence

        🡅  🡇     🗕  🗗  🗖
        public static java.lang.String sentence​(java.lang.String text,
                                                LC srcLang,
                                                LC targetLang)
                                         throws java.io.IOException
        This method takes what is expected to be a sentence in (any) foreign language. The foreign-language used is passed as a two-character String from Google Translate' list of two-character language codes. The Target language may be any. Usually it is English/Spanish.

        Parameters:
        text - Any string in a foreign language. This is expected to be a single sentence.
        srcLang - Two-Character String Language Code identifying the foreign language used. See the enum "LC" (Language Code)
        targetLang - Two-Character String Language Code of the "target" or "destination" language. See the enum "LC" (Language Code)
        Returns:
        Translated Sentence - generated by a call to Google Cloud Server Translate API.
        Throws:
        java.io.IOException
        See Also:
        LC
        Code:
        Exact Method Body:
         StringBuffer    sb      = new StringBuffer();
         String	        json    = null;
        
         // REST API Query String
         String q = baseQ + key + "&source=" + srcLang.gcsLanguageCode +
             "&target=" + targetLang.gcsLanguageCode +
             "&q=" + URLs.toProperURLV2(text);
        
         try
         {
             json        = Scrape.scrapePage(Scrape.openConn_UTF8(q)).toString();
             Matcher m   = P1.matcher(json);
        
             // Since only a Single text-string was queried, it is better to return the result
             // as a single string.
        
             while (m.find()) sb.append(m.group(1) + "\n");
        
             return sb.toString();
         }
         catch (Exception e)
         {
             System.out.println(
                 '\n' +
                 "TextTranslate.sentence(q=\n" + q + '\n' +
                 "TextTranslate.sentence(text=\n" + text + '\n' +
                 "TextTranslate.sentence(json=\n" + json
             );
        
             throw e;
         }
        
      • wordByWord

        🡅     🗕  🗗  🗖
        public static java.util.Vector<java.lang.String> wordByWord​
                    (java.util.Vector<java.lang.String> words,
                     LC srcLang,
                     LC targetLang)
                throws java.io.IOException
        
        This is similar to sentence, but the input is expected to be a series of individual vocabulary words. The source and target/destination language codes are necessary. A Vector of equal length to the original input Vector is returned. It contains each vocabulary word from the original Vector in the destination/target language.
        Parameters:
        words - Any string in a foreign language. This is expected to be a single sentence.
        srcLang - Two-Character String Language Code identifying the foreign language used. See the enum "LC" (Language Code)
        targetLang - Two-Character String Language Code of the "target" or "destination" language. See the enum "LC" (Language Code)
        Returns:
        Translated Sentence - generated by a call to Google Cloud Server Translate API.
        Throws:
        java.io.IOException
        See Also:
        Scrape, LC
        Code:
        Exact Method Body:
         int             i       = 0;
         int             len     = words.size();
         Vector<String>  ret     = new Vector<String>();
         String          q       = null;
         String          json    = null;
        
         while (i < len)
         {
             try
             {
                 q = baseQ + key + "&source=" + srcLang.gcsLanguageCode +
                         "&target=" + targetLang.gcsLanguageCode;
        
                 for (int j=0; (j < 50) && ((j + i) < len); j++)
                     q += "&q=" + URLs.toProperURLV2(words.elementAt(j + i));
        
                 i += 50;
        
                 json = Scrape.scrapePage(Scrape.openConn_UTF8(q)).toString();
                 Matcher	m = P1.matcher(json);
        
                 while (m.find()) ret.addElement(m.group(1).trim());
             }
             catch (Exception e)
             {
                 System.out.println(
                     '\n' +
                     "TextTranslate.wordByWord, q=\n" + q + '\n' +
                     "TextTranslate.wordByWord, json=\n" + json
                 );
        
                 throw e;
             }
         }
        
         return ret;