diff --git a/pom.xml b/pom.xml index b2998929ed3..5eb9870018c 100644 --- a/pom.xml +++ b/pom.xml @@ -99,7 +99,7 @@ storage/xml-api/serviceaccount-appengine-sample texttospeech/cloud-client - translate + translate/cloud-client unittests diff --git a/translate/README.md b/translate/README.md deleted file mode 100644 index 0a4946a7184..00000000000 --- a/translate/README.md +++ /dev/null @@ -1,87 +0,0 @@ -# Google Cloud Translate Sample - - -Open in Cloud Shell - -This sample demonstrates the use of [Google Cloud Translate -API][Translate-Docs] for translating and detecting language text. - -[Translate-Docs]: https://cloud.google.com/translate/docs/ - -## Java Version - -This sample requires you to have -[Java8](https://docs.oracle.com/javase/8/docs/technotes/guides/install/install_overview.html). - -## Download Maven - -This sample uses the [Apache Maven][maven] build system. Before getting started, -be -sure to [download][maven-download] and [install][maven-install] it. When you use -Maven as described here, it will automatically download the needed client -libraries. - -[maven]: https://maven.apache.org -[maven-download]: https://maven.apache.org/download.cgi -[maven-install]: https://maven.apache.org/install.html - -## Authentication -This sample uses API Key for authentication. - - * Visit the [Google Cloud Console](https://console.cloud.google.com) and navigate to: - ``` - API Manager > Credentials > Create credentials > API Key - ``` - - * Set the environment variable `GOOGLE_API_KEY` - ``` - export GOOGLE_API_KEY= - ``` - -## Run the sample - -To build the sample, we use Maven. - -```bash -mvn clean compile assembly:single -``` - -We can then run the assembled JAR file with the `java` command. The variable -$COMMAND takes three values `langsupport`, `detect` and `translate`. - -``` -JAR_FILE=target/translate-1.0-jar-with-dependencies.jar -java -jar $JAR_FILE - -``` - -Example Usage: - -``` -INPUT="A quick brown fox jumped over a lazy dog." -SOURCE_LANG="en" -TARGET_LANG="fr" -``` - -Translate API Features: - - * List the languages supported by the API - ``` - java -jar $JAR_FILE langsupport - ``` - - * List the languages supported for given target language - ``` - java -jar $JAR_FILE langsupport $TARGET_LANG - ``` - - * Detect input text language - ``` - java -jar $JAR_FILE detect "$INPUT" - ``` - - * Translate input text (with options) - ``` - java -jar $JAR_FILE translate "$INPUT" - java -jar $JAR_FILE translate "$INPUT" $SOURCE_LANG $TARGET_LANG - ``` diff --git a/translate/pom.xml b/translate/pom.xml deleted file mode 100644 index 10ed080e64b..00000000000 --- a/translate/pom.xml +++ /dev/null @@ -1,75 +0,0 @@ - - - 4.0.0 - com.example.cloud.translate.samples - translate - 1.0 - translate - http://maven.apache.org - - - - com.google.cloud.samples - shared-configuration - 1.0.9 - - - - 1.8 - 1.8 - UTF-8 - - - - - com.google.cloud - google-cloud-translate - 1.52.0 - - - junit - junit - 4.12 - test - - - com.google.truth - truth - 0.42 - - - - - - maven-assembly-plugin - - - - com.example.cloud.translate.samples.TranslateText - - - - jar-with-dependencies - - - - - - diff --git a/translate/src/main/java/com/example/cloud/translate/samples/TranslateText.java b/translate/src/main/java/com/example/cloud/translate/samples/TranslateText.java deleted file mode 100644 index 2281f37845b..00000000000 --- a/translate/src/main/java/com/example/cloud/translate/samples/TranslateText.java +++ /dev/null @@ -1,173 +0,0 @@ -/* - * Copyright 2016 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.cloud.translate.samples; - -import com.google.cloud.translate.Detection; -import com.google.cloud.translate.Language; -import com.google.cloud.translate.Translate; -import com.google.cloud.translate.Translate.LanguageListOption; -import com.google.cloud.translate.Translate.TranslateOption; -import com.google.cloud.translate.TranslateOptions; -import com.google.cloud.translate.Translation; -import com.google.common.collect.ImmutableList; -import java.io.PrintStream; -import java.util.List; -import java.util.Optional; - -public class TranslateText { - /** - * Detect the language of input text. - * - * @param sourceText source text to be detected for language - * @param out print stream - */ - //[START translate_detect_language] - public static void detectLanguage(String sourceText, PrintStream out) { - Translate translate = createTranslateService(); - List detections = translate.detect(ImmutableList.of(sourceText)); - System.out.println("Language(s) detected:"); - for (Detection detection : detections) { - out.printf("\t%s\n", detection); - } - } - //[END translate_detect_language] - - /** - * Translates the source text in any language to English. - * - * @param sourceText source text to be translated - * @param out print stream - */ - //[START translate_translate_text] - public static void translateText(String sourceText, PrintStream out) { - Translate translate = createTranslateService(); - Translation translation = translate.translate(sourceText); - out.printf("Source Text:\n\t%s\n", sourceText); - out.printf("Translated Text:\n\t%s\n", translation.getTranslatedText()); - } - //[END translate_translate_text] - - /** - * Translate the source text from source to target language. - * Make sure that your project is whitelisted. - * - * @param sourceText source text to be translated - * @param sourceLang source language of the text - * @param targetLang target language of translated text - * @param out print stream - */ - //[START translate_text_with_model] - public static void translateTextWithOptionsAndModel( - String sourceText, - String sourceLang, - String targetLang, - PrintStream out) { - - Translate translate = createTranslateService(); - TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang); - TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang); - - // Use translate `model` parameter with `base` and `nmt` options. - TranslateOption model = TranslateOption.model("nmt"); - - Translation translation = translate.translate(sourceText, srcLang, tgtLang, model); - out.printf("Source Text:\n\tLang: %s, Text: %s\n", sourceLang, sourceText); - out.printf("TranslatedText:\n\tLang: %s, Text: %s\n", targetLang, - translation.getTranslatedText()); - } - //[END translate_text_with_model] - - - /** - * Translate the source text from source to target language. - * - * @param sourceText source text to be translated - * @param sourceLang source language of the text - * @param targetLang target language of translated text - * @param out print stream - */ - public static void translateTextWithOptions( - String sourceText, - String sourceLang, - String targetLang, - PrintStream out) { - - Translate translate = createTranslateService(); - TranslateOption srcLang = TranslateOption.sourceLanguage(sourceLang); - TranslateOption tgtLang = TranslateOption.targetLanguage(targetLang); - - Translation translation = translate.translate(sourceText, srcLang, tgtLang); - out.printf("Source Text:\n\tLang: %s, Text: %s\n", sourceLang, sourceText); - out.printf("TranslatedText:\n\tLang: %s, Text: %s\n", targetLang, - translation.getTranslatedText()); - } - - /** - * Displays a list of supported languages and codes. - * - * @param out print stream - * @param tgtLang optional target language - */ - //[START translate_list_language_names] - //[START translate_list_codes] - public static void displaySupportedLanguages(PrintStream out, Optional tgtLang) { - Translate translate = createTranslateService(); - LanguageListOption target = LanguageListOption.targetLanguage(tgtLang.orElse("en")); - List languages = translate.listSupportedLanguages(target); - - for (Language language : languages) { - out.printf("Name: %s, Code: %s\n", language.getName(), language.getCode()); - } - } - //[END translate_list_codes] - //[END translate_list_language_names] - - /** - * Create Google Translate API Service. - * - * @return Google Translate Service - */ - public static Translate createTranslateService() { - return TranslateOptions.newBuilder().build().getService(); - } - - public static void main(String[] args) { - String command = args[0]; - String text; - - if (command.equals("detect")) { - text = args[1]; - TranslateText.detectLanguage(text, System.out); - } else if (command.equals("translate")) { - text = args[1]; - try { - String sourceLang = args[2]; - String targetLang = args[3]; - TranslateText.translateTextWithOptions(text, sourceLang, targetLang, System.out); - } catch (ArrayIndexOutOfBoundsException ex) { - TranslateText.translateText(text, System.out); - } - } else if (command.equals("langsupport")) { - try { - String target = args[1]; - TranslateText.displaySupportedLanguages(System.out, Optional.of(target)); - } catch (ArrayIndexOutOfBoundsException ex) { - TranslateText.displaySupportedLanguages(System.out, Optional.empty()); - } - } - } -} diff --git a/translate/src/test/java/com/example/cloud/translate/samples/TranslateTextTest.java b/translate/src/test/java/com/example/cloud/translate/samples/TranslateTextTest.java deleted file mode 100644 index 1ce8996b484..00000000000 --- a/translate/src/test/java/com/example/cloud/translate/samples/TranslateTextTest.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright 2016 Google Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.example.cloud.translate.samples; - -import static com.google.common.truth.Truth.assertThat; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** - * Unit tests for {@link TranslateText}. - */ -@RunWith(JUnit4.class) -public class TranslateTextTest { - - @Test public void testSupportedLanguages() throws Exception { - // Supported languages - List languages = Arrays.asList( - "Afrikaans", "Albanian", "Amharic", "Arabic", "Armenian", "Azerbaijani", "Basque", - "Belarusian", "Bengali", "Bosnian", "Bulgarian", "Catalan", "Cebuano", "Chichewa", - "Chinese", "Chinese", "Corsican", "Croatian", "Czech", "Danish", "Dutch", "English", - "Esperanto", "Estonian", "Filipino", "Finnish", "French", "Frisian", "Galician", - "Georgian", "German", "Greek", "Gujarati", "Haitian", "Hausa", "Hawaiian", "Hebrew", - "Hindi", "Hmong", "Hungarian", "Icelandic", "Igbo", "Indonesian", "Irish", "Italian", - "Japanese", "Javanese", "Kannada", "Kazakh", "Khmer", "Korean", "Kurdish", "Kyrgyz", - "Lao", "Latin", "Latvian", "Lithuanian", "Luxembourgish", "Macedonian", "Malagasy", - "Malay", "Malayalam", "Maltese", "Maori", "Marathi", "Mongolian", "Myanmar", "Nepali", - "Norwegian", "Pashto", "Persian", "Polish", "Portuguese", "Punjabi", "Romanian", - "Russian", "Samoan", "Scots", "Serbian", "Sesotho", "Shona", "Sindhi", "Sinhala", - "Slovak", "Slovenian", "Somali", "Spanish", "Sundanese", "Swahili", "Swedish", - "Tajik", "Tamil", "Telugu", "Thai", "Turkish", "Ukrainian", "Urdu", "Uzbek", - "Vietnamese", "Welsh", "Xhosa", "Yiddish", "Yoruba", "Zulu"); - - // Arrange - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(bout); - - // Act - TranslateText.displaySupportedLanguages(out, Optional.empty()); - - // Assert - String got = bout.toString(); - for (String language : languages) { - assertThat(got).contains(language); - } - } - - @Test public void testSupportedLanguagesTargetFrench() throws Exception { - //Supported languages - List languages = Arrays.asList( - "Afrikaans", "Albanais", "Allemand", "Amharique", "Anglais", "Arabe", "Arménien", - "Azéri", "Basque", "Bengali", "Biélorusse", "Birman", "Bosniaque", "Bulgare", "Catalan", - "Cebuano", "Chichewa", "Chinois (simplifié)", "Chinois (traditionnel)", "Cingalais", - "Coréen", "Corse", "Créole haïtien", "Croate", "Danois", "Espagnol", "Espéranto", - "Estonien", "Finnois", "Français", "Frison", "Gaélique (Écosse)", "Galicien", - "Gallois", "Géorgien", "Grec", "Gujarati", "Haoussa", "Hawaïen", "Hébreu", "Hindi", - "Hmong", "Hongrois", "Igbo", "Indonésien", "Irlandais", "Islandais", "Italien", - "Japonais", "Javanais", "Kannada", "Kazakh", "Khmer", "Kirghiz", "Kurde", "Laotien", - "Latin", "Letton", "Lituanien", "Luxembourgeois", "Macédonien", "Malaisien", "Malayalam", - "Malgache", "Maltais", "Maori", "Marathi", "Mongol", "Néerlandais", "Népalais", "Norvégien", - "Ouzbek", "Pachtô", "Panjabi", "Persan", "Polonais", "Portugais", "Roumain", "Russe", - "Samoan", "Serbe", "Sesotho", "Shona", "Sindhî", "Slovaque", "Slovène", "Somali", - "Soundanais", "Suédois", "Swahili", "Tadjik", "Tagalog", "Tamoul", "Tchèque", "Telugu", - "Thaï", "Turc", "Ukrainien", "Urdu", "Vietnamien", "Xhosa", "Yiddish", "Yorouba","Zoulou"); - - // Arrange - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(bout); - - // Act - TranslateText.displaySupportedLanguages(out, Optional.of("fr")); - - // Assert - String got = bout.toString(); - for (String language : languages) { - bout.reset(); - out.print(language); - assertThat(got).contains(bout.toString()); - } - } - - @Test public void testEnglishLangDetection() throws Exception { - // Arrange - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(bout); - - // Act - TranslateText.detectLanguage("With power comes great responsibility.", out); - - // Assert - String got = bout.toString(); - assertThat(got).contains("language=en"); - - // Assert - Double confidence = Double.parseDouble( - got.split("confidence=")[1].split("}")[0] - ); - assertThat(confidence).isWithin(0.7).of(1.0); - } - - @Test public void testGermanLangDetection() throws Exception { - // Arrange - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(bout); - - // Act - TranslateText.detectLanguage("Mit Macht kommt große Verantwortung.", out); - - // Assert - String got = bout.toString(); - assertThat(got).contains("language=de"); - - // Assert - Double confidence = Double.parseDouble( - got.split("confidence=")[1].split("}")[0] - ); - assertThat(confidence).isWithin(0.9).of(1.0); - } - - @Test public void testDefaultIdentityTranslation() throws Exception { - // Arrange - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(bout); - - // Act - String proverb = "What you do not wish for yourself, do not do to others."; - TranslateText.translateText(proverb, out); - - // Assert - String got = bout.toString(); - assertThat(got).contains(proverb); - } - - @Test public void testGermanToSpanishTranslation() throws Exception { - // Arrange - ByteArrayOutputStream bout = new ByteArrayOutputStream(); - PrintStream out = new PrintStream(bout); - - // Act - TranslateText.translateTextWithOptions("Mit Macht kommt große Verantwortung.", "de", "es", out); - - // Assert - String got = bout.toString(); - assertThat(got).contains("Con el poder viene una gran responsabilidad."); - } -}