-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Translate sample #348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Translate sample #348
Changes from 16 commits
2735737
fd18d9e
9c75dac
78bf1f6
9d16b79
292354d
a33ff9a
8f444e9
eb03dd1
de5d1b1
a9eb8b8
a53a8f3
e8fd790
930e00f
1756145
792cb56
cdc4117
c3c48ce
1d8fd5e
91c0ee9
001f400
a28e440
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| # Google Cloud Translate Sample | ||
|
|
||
| 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 | ||
|
|
||
| ## 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 two values `detect' and `translate'. | ||
|
|
||
| ``` | ||
| MAIN_CLASS=com.google.cloud.translate.samples.TranslateText | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could set this in your manifest when you create the jar -- actually it looks like it is done.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
| JAR_FILE=target/translate-1.0-SNAPSHOT-jar-with-dependencies.jar | ||
| java -cp $JAR_FILE $MAIN_CLASS <detect|translate|langsupport> <text> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| <optional_source> <optional_target> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indent this line 4 spaces to show it is a continuation of the previous line.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| ``` | ||
|
|
||
| Example Usage: | ||
|
|
||
| ``` | ||
| INPUT="A quick brown fox jumped over a lazy dog." | ||
| SOURCE_LANG="en" | ||
| TARGET_LANG="fr" | ||
| ``` | ||
|
|
||
| Translate API Features: | ||
|
|
||
| * Languages supported by the API | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change this line to: List the languages supported by the API.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| ``` | ||
| java -cp $JAR_FILE $MAIN_CLASS langsupport | ||
| ``` | ||
|
|
||
| * Detect input text language | ||
| ``` | ||
| java -cp $JAR_FILE $MAIN_CLASS detect "$INPUT" | ||
| ``` | ||
|
|
||
| * Translate input text (with options) | ||
| ``` | ||
| java -cp $JAR_FILE $MAIN_CLASS translate "$INPUT" | ||
| java -cp $JAR_FILE $MAIN_CLASS translate "$INPUT" $SOURCE_LANG $TARGET_LANG | ||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| <?xml version="1.0"?> | ||
| <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a license header after the Optionally, you can also delete the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <groupId>com.google.cloud.translate.samples</groupId> | ||
| <artifactId>translate</artifactId> | ||
| <version>1.0-SNAPSHOT</version> | ||
| <name>translate</name> | ||
| <url>http://maven.apache.org</url> | ||
| <properties> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| </properties> | ||
| <parent> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>shared-configuration</artifactId> | ||
| <version>1.0.0</version> | ||
| <relativePath>../java-repo-tools</relativePath> | ||
| </parent> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>google-cloud-translate</artifactId> | ||
| <version>0.3.0</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.12</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.truth</groupId> | ||
| <artifactId>truth</artifactId> | ||
| <version>0.30</version> | ||
| </dependency> | ||
| </dependencies> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <artifactId>maven-assembly-plugin</artifactId> | ||
| <configuration> | ||
| <archive> | ||
| <manifest> | ||
| <mainClass>com.google.cloud.translate.samples.TranslateText</mainClass> | ||
| </manifest> | ||
| </archive> | ||
| <descriptorRefs> | ||
| <descriptorRef>jar-with-dependencies</descriptorRef> | ||
| </descriptorRefs> | ||
| </configuration> | ||
| </plugin> | ||
| <plugin> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Compiler plugin isn't really required, you can set in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <version>3.3</version> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <configuration> | ||
| <source>1.8</source> | ||
| <target>1.8</target> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </project> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /* | ||
| * Copyright 2016 Google Inc. All Rights Reserved. | ||
| * | ||
| * 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.google.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.TranslateOption; | ||
| import com.google.cloud.translate.Translation; | ||
| import com.google.cloud.translate.testing.RemoteTranslateHelper; | ||
| import com.google.common.collect.ImmutableList; | ||
|
|
||
| import java.io.PrintStream; | ||
| import java.util.List; | ||
|
|
||
| public class TranslateText { | ||
| private static final Translate TRANSLATE = RemoteTranslateHelper.create().options().service(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure this is the class we need to use for a sample? The fact that this is in a package called
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably want to pass the Consider even having each function create a Translate object, depending on how these snippets will be used, that could be the most clear.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done ptal |
||
|
|
||
| /** | ||
| * Detect the language of input text. | ||
| * | ||
| * @param sourceText source text to be detected for language | ||
| * @param out print stream | ||
| */ | ||
| public static void detectLanguage(String sourceText, PrintStream out) { | ||
| List<Detection> detections = TRANSLATE.detect(ImmutableList.of(sourceText)); | ||
| System.out.println("Language(s) detected:"); | ||
| for (Detection detection : detections) { | ||
| out.printf("\t%s\n", detection); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Translates the source text in any language to english. | ||
| * | ||
| * @param sourceText source text to be translated | ||
| * @param out print stream | ||
| */ | ||
| public static void translateText(String sourceText, PrintStream out) { | ||
| Translation translation = TRANSLATE.translate(sourceText); | ||
| out.printf("Source Text:\n\t%s\n", sourceText); | ||
| out.printf("Translated Text:\n\t%s\n", translation.translatedText()); | ||
| } | ||
|
|
||
| /** | ||
| * 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) { | ||
|
|
||
| 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.translatedText()); | ||
| } | ||
|
|
||
| /** | ||
| * Displays a list of supported languages and codes. | ||
| * | ||
| * @param out print stream | ||
| */ | ||
| public static void displaySupportedLanguages(PrintStream out) { | ||
| List<Language> languages = TRANSLATE.listSupportedLanguages(); | ||
|
|
||
| for (Language language : languages) { | ||
| out.printf("Name: %s, Code: %s\n", language.name(), language.code()); | ||
| } | ||
| } | ||
|
|
||
| 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")) { | ||
| TranslateText.displaySupportedLanguages(System.out); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /* | ||
| * Copyright 2016 Google Inc. All Rights Reserved. | ||
| * | ||
| * 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.google.cloud.translate.samples; | ||
|
|
||
| import static com.google.common.truth.Truth.assertThat; | ||
|
|
||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.junit.runners.JUnit4; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.PrintStream; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Unit tests for {@link Analyze}. | ||
| */ | ||
| @RunWith(JUnit4.class) | ||
| public class TranslateTextTest { | ||
|
|
||
| @Test public void testSupportedLanguages() throws Exception { | ||
| // Supported languages | ||
| List<String> 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); | ||
|
|
||
| // Assert | ||
| String got = bout.toString(); | ||
| for (String language : languages) { | ||
| assertThat(got).contains(language); | ||
| } | ||
| } | ||
|
|
||
| @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, confidence=0.79742646"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the confidence from the test. That's way too precise. Very fragile if the model changes at all.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, but its still better to have the notion of confidence captured so that if confidence drops to say 0.2 then we catch that. Let me make appropriate changes.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright, maybe just the first few digits, then? This is too precise. I don't care if it goes from 0.797 to 0.798, but maybe we do care if it goes from 0.79 to 0.80? (Probably we don't, but we do care if it changes to below 0.5?)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow, your comparing to a string? That's not a great idea. (not my first choice of wording). Confidence will change with time, why not claim it will be within a range of .7 .. .9 This stuff tends to jump around as the model changes.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, Les it was not a great idea :) Fixed it. |
||
| } | ||
|
|
||
| @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, confidence=0.9293963"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove the confidence from the test.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
|
|
||
| } | ||
|
|
||
| @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."); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wary about this one, too. Since I'm sure there is more than one possible translation. Maybe change it to look for a few important words (like responsabilidad)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, we only get one translation. If this changes it will be good to know why and make modifications to the test appropriately. I think just added keywords is too flexible. |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit. I think wrong kind of end-quote was used. ' instead of `.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done