Class GCSTAPI


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

    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.

    Static (Functional) API: The methods in this class are all (100%) defined with the Java Key-Word / Key-Concept 'static'. Furthermore, there is no way to obtain an instance of this class, because there are no public (nor private) constructors. Java's Spring-Boot, MVC feature is *not* utilized because it flies directly in the face of the light-weight data-classes philosophy. This has many advantages over the rather ornate Component Annotations (@Component, @Service, @AutoWired, etc... 'Java Beans') syntax:

    • The methods here use the key-word 'static' which means (by implication) that there is no internal-state. Without any 'internal state' there is no need for constructors in the first place! (This is often the complaint by MVC Programmers).
    • A 'Static' (Functional-Programming) API expects to use fewer data-classes, and light-weight data-classes, making it easier to understand and to program.
    • The Vectorized HTML data-model allows more user-control over HTML parse, search, update & scrape. Also, memory management, memory leakage, and the Java Garbage Collector ought to be intelligible through the 'reuse' of the standard JDK class Vector for storing HTML Web-Page data.

    The power that object-oriented programming extends to a user is (mostly) limited to data-representation. Thinking of "Services" as "Objects" (Spring-MVC, 'Java Beans') is somewhat 'over-applying' the Object Oriented Programming Model. Like most classes in the Java-HTML JAR Library, this class backtracks to a more C-Styled Functional Programming Model (no Objects) - by re-using (quite profusely) the key-word static with all of its methods, and by sticking to Java's well-understood class Vector

    Static Fields: The methods in this class do not create any internal state that is maintained - however there are a few private & static fields defined. These fields are instantiated only once during the Class Loader phase (and only if this class shall be used), and serve as data 'lookup' fields (static constants). View this class' source-code in the link provided below to see internally used data.

    There are two internal fields, one for the URL of Google Cloud Platform's translation API, and the second holds a Regular Expression for parsing the returned JSON String.



    • 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.
        Code:
        Exact Field Declaration Expression:
        1
        public static String key;
        
    • 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:
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
         String  ret     = "";
         String  q       = baseQ + key + "&source=" + srcLang.gcsLanguageCode + "&target=" + targetLang.gcsLanguageCode + "&q=" + URLs.toProperURLV2(text);
         String	json    = null;
        
         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()) ret += m.group(1) + "\n";
        
             return ret;
        
         } 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:
         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        15
        16
        17
        18
        19
        20
        21
        22
        23
        24
        25
        26
        27
        28
        29
        30
        31
        32
        33
        34
         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;