There's more…

The translate method is overloaded, providing various translation options to be applied using the TranslateOption class. These methods are listed here:

  • sourceLanguage: Takes a language code that specifies the language being translated.
  • targetLanguage: Takes a language code that specifies the language being translated to.
  • model: Specifies the model to use. The model used can be specified using the model method, as shown next (Translate.TranslateOption.model("nmt")). The nmt and base strings are used for the NMT and PBMT models, respectively.

Add the following code to the main method. The code demonstrates the use of these methods. The first sequence specifies a target language of German using the NMT model. The second sequence uses the PBMT model, as shown:

translation = translate.translate(text, Translate.TranslateOption.sourceLanguage("el"),
Translate.TranslateOption.targetLanguage("de"), Translate.TranslateOption.model("nmt"));
System.out.printf("%s Translation: %s ", text, translation.getTranslatedText());

translation = translate.translate(text, Translate.TranslateOption.sourceLanguage("el"),
Translate.TranslateOption.targetLanguage("de"), Translate.TranslateOption.model("base"));
System.out.printf("%s Translation: %s ", text, translation.getTranslatedText());

Execute the program. You will get the following output:

Αυτοκινητόδρομος Translation: Autobahn
Αυτοκινητόδρομος Translation: Autobahn

We can also translate a list of text. To illustrate this process, we will use text found in Wikipedia for the German, Korean, and Portuguese languages that discuss the Big Bang theory. For the following references, we used their first sentence:

Add the following declaration of a list to the application:

List<String> translationList = new ArrayList<>();

Next, add the text from the preceding sources:

Use the translate method with the list that specifies a target language of English. The method returns a list of Translation objects:

List<Translation> translations = translate.translate(translationList, 
Translate.TranslateOption.targetLanguage("en"));

Display the translations as shown next. Only the first 32 characters of the original source is displayed, along with the name of the source language and the translation:

for(int i=0; i< translationList.size(); i++) {
System.out.printf(" %-32s Source Languge Code: %-32s Translation: %s ",
translationList.get(i).substring(0, Math.min(32,
translationList.get(i).length())),
translations.get(i).getSourceLanguage(),
translations.get(i).getTranslatedText());
}

When this code sequence is executed, you will get the following output:

We can also translate text that contains HTML markup. The markup is retained in the translated text, unchanged. To illustrate this capability, we will use the HTML for the first paragraph of a web page, found at https://en.wikipedia.org/wiki/Language, as shown next:

<p><b>Language</b> is a system that consists of the development, acquisition, maintenance and use of complex systems of <a href="/wiki/Communication" title="Communication">communication</a>, particularly the <a href="/wiki/Human" title="Human">human</a> ability to do so; and <b>a language</b> is any specific example of such a system.
</p>

Add the following statements to declare the text and apply the translate method:

text = "<p><b>Language</b> is a system that consists of the development, "
+ "acquisition, maintenance and use of complex systems of "
+ "<a href="/wiki/Communication" title="Communication">"
+ "communication</a>, particularly the <a href="/wiki/Human" "
+ "title="Human">human</a> ability to do so; and <b>a language</b> "
+ "is any specific example of such a system. </p";
translation = translate.translate(text, Translate.TranslateOption.targetLanguage("es"));
System.out.printf("%-32s Translation: %s ", text.substring(0, 32),
translation.getTranslatedText());

When the code is executed, you will get the following output:

<p><b>Language</b> is a system t 
Translation: <p> <b>El lenguaje</b> es un sistema que consiste en el desarrollo, adquisición, mantenimiento y uso de sistemas complejos de <a href="/wiki/Communication" title="Comunicación">comunicación</a> , particularmente la capacidad <a href="/wiki/Human" title="Humano">humana</a> para hacerlo; y <b>un lenguaje</b> es cualquier ejemplo específico de tal sistema. </p>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.168.57