Class Pin1Yin1


  • public class Pin1Yin1
    extends java.lang.Object
    Pin1Yin1 (罗马拼音 - Online Internet Translation Service) - Documentation.

    This class is named after the server Pin1Yin1.com - and it queries that address for Chinese Translations.

    IMPORTANT NOTE: This class uses "itself" as a result-return object. Specifically, when a result from the server is returned, the answers are encapsulated in an instance of a class Pin1Yin1 instantiation.
    
    Pin1Yin1 response = Pin1Yin1.getResponse("中国政府网_中央人民政府门户网站", false);
    System.out.println("Here are the respose vocabulary words:\n" + response.toString());
    


    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.

    This class has multiple, internal static final constants (Regular Expressions to be exact) - used to parse JSON Data from the server.



    • Field Detail

      • PIN1_YIN1_URL

        public static final java.lang.String PIN1_YIN1_URL
        See Also:
        Constant Field Values
        Code:
        Exact Field Declaration Expression:
        1
        public static final String PIN1_YIN1_URL = "https://pin1yin1.com/pinyin/convert/?c=";
        
      • simpSENTENCE

        public final java.lang.String simpSENTENCE
        This is the original query-string in Simplified Chinese.

        NOTE: How accurate the traditional/simplified conversion is is not known, and probably not exact.
        Code:
        Exact Field Declaration Expression:
        1
        public final String			simpSENTENCE;
        
      • tradSENTENCE

        public final java.lang.String tradSENTENCE
        This is the original query-string in Traditional Chinese
        Code:
        Exact Field Declaration Expression:
        1
        public final String         tradSENTENCE;
        
      • simp

        public final java.lang.String[] simp
        The Simplified Conversion. In this version, characters are grouped by "Word Boundaries"
        Code:
        Exact Field Declaration Expression:
        1
        public final String[]		simp;
        
      • pron

        public final java.lang.String[] pron
        The Pronunciation of each element in the "Word Boundaries" version.
        Code:
        Exact Field Declaration Expression:
        1
        public final String[]       pron;
        
      • trad

        public final java.lang.String[] trad
        The traditional conversion. In this version, also, characters are grouped by "Word Boundaries"
        Code:
        Exact Field Declaration Expression:
        1
        public final String[]       trad;
        
      • engl

        public final java.lang.String[] engl
        These are the English Translations of each word. Some Chinese Words are translated into multiple english words.
        Code:
        Exact Field Declaration Expression:
        1
        public final String[]       engl;
        
      • pronSINGLE_WORD

        public final java.lang.String[] pronSINGLE_WORD
        This is a 1-to-1 mapping between the chinese characters in the query, and their mandarin pronunciation. Specifically, each element in this array contains a single ('one') syllable of pinyin romanization - a.k.a. one chinese character's pronunciation. The array final String[] pron contains as many syllables as the "Grouped By" Chinese Word does.
        Code:
        Exact Field Declaration Expression:
        1
        public final String[]       pronSINGLE_WORD;
        
      • wordGroups

        public final int[] wordGroups
        This is just an array of integers that was actually returned from the server. It identifies the number of Chinese Characters that are in each Chinese Word.
        Code:
        Exact Field Declaration Expression:
        1
        public final int[]			wordGroups;
        
      • JSON

        public final java.lang.String JSON
        Unless specifically requested, this variable is null. It is the JSON as a String returned from the server
        Code:
        Exact Field Declaration Expression:
        1
        public final String			JSON;
        
    • Method Detail

      • main

        public static void main​(java.lang.String[] argv)
                         throws java.io.IOException
        Throws:
        java.io.IOException
        Code:
        Exact Method Body:
        1
         System.out.println(getResponse((argv.length == 1) ? argv[0] : sampleText, true).toString());
        
      • getResponse

        public static Pin1Yin1 getResponse​(java.lang.String chineseText,
                                           boolean saveJSON)
                                    throws java.io.IOException
        This translates a sentence from Mandarin into English. The response object is an instance of this exact class.
        Parameters:
        chineseText - This is a text String input, and is expected to be in Mandarin. The server does not seem to mind whether the input is in "Simplified" or "Traditional."
        saveJSON - If this is true, the field public final String JSON will contain the original JSON string retrieved from the server. If this is FALSE, the JSON field will be null.
        Returns:
        An instance of this class, with all of the constant final fields filled in.
        Throws:
        java.io.IOException
        Code:
        Exact Method Body:
        1
         return new Pin1Yin1(chineseText, saveJSON);
        
      • toString

        public java.lang.String toString()
        This converts this result to a String
        Overrides:
        toString in class java.lang.Object
        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
         StringBuilder sb = new StringBuilder();
         if (JSON != null) sb.append(JSON + "\n");
         sb.append("Simp: " + simpSENTENCE + "\n" + "Trad: " + tradSENTENCE);
        
         sb.append("\nSimp-Words:\t");
         for (String s : simp) sb.append(s + ", ");
         int len = sb.length();
         sb.delete(len - 2, len); // DELETE THE ENDING COMMA-SPACE 
        
         sb.append("\nPron-Words:\t");
         for (String s : pron) sb.append(s + ", ");
         len = sb.length();
         sb.delete(len - 2, len); // DELETE THE ENDING COMMA-SPACE 
        
         sb.append("\nTrad-Words:\t");
         for (String s : trad) sb.append(s + ", ");
         len = sb.length();
         sb.delete(len - 2, len); // DELETE THE ENDING COMMA-SPACE 
        
         sb.append("\nEngl-Words:\t");
         for (String s : engl) sb.append("[" + s + "] ");
        
         return sb.toString();