-
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
Merged
Merged
Translate sample #348
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
2735737
added first commit of translate
puneithk fd18d9e
added correct translate dependency
puneithk 9c75dac
renamed sample file to TranslateText
puneithk 78bf1f6
added pom entries and added first sample translate code
puneithk 9d16b79
added language detect code
puneithk 292354d
removed line
puneithk a33ff9a
added source and target lang options
puneithk 8f444e9
added source and target lang function
puneithk eb03dd1
organized README functions
puneithk de5d1b1
organized README functions
puneithk a9eb8b8
added TranslateAPI Features
puneithk a53a8f3
added PrintStream as output
puneithk e8fd790
added tests
puneithk 930e00f
added style check
puneithk 1756145
fixed import order
puneithk 792cb56
added @param
puneithk cdc4117
fixed PR comments
puneithk c3c48ce
added range check
puneithk 1d8fd5e
fixed README and pom
puneithk 91c0ee9
fixed README tick
puneithk 001f400
added within instead of less and greater than for test
puneithk a28e440
added function to create translate service
puneithk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# 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 three values `langsupport`, `detect` and `translate`. | ||
|
||
``` | ||
JAR_FILE=target/translate-1.0-SNAPSHOT-jar-with-dependencies.jar | ||
java -jar $JAR_FILE <detect|translate|langsupport> <text> | ||
<optional_source> <optional_target> | ||
``` | ||
|
||
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 | ||
``` | ||
|
||
* 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 | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<!-- | ||
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. | ||
--> | ||
<project> | ||
<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> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<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> | ||
</plugins> | ||
</build> | ||
</project> |
143 changes: 143 additions & 0 deletions
143
translate/src/main/java/com/google/cloud/translate/samples/TranslateText.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* 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.RetryParams; | ||
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.TranslateOptions; | ||
import com.google.cloud.translate.Translation; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
import java.io.PrintStream; | ||
import java.util.List; | ||
|
||
public class TranslateText { | ||
/** | ||
* 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) { | ||
Translate translate = createTranslateService(); | ||
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) { | ||
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.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) { | ||
|
||
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.translatedText()); | ||
} | ||
|
||
/** | ||
* Displays a list of supported languages and codes. | ||
* | ||
* @param out print stream | ||
*/ | ||
public static void displaySupportedLanguages(PrintStream out) { | ||
Translate translate = createTranslateService(); | ||
List<Language> languages = translate.listSupportedLanguages(); | ||
|
||
for (Language language : languages) { | ||
out.printf("Name: %s, Code: %s\n", language.name(), language.code()); | ||
} | ||
} | ||
|
||
/** | ||
* Create Google Translate API Service. | ||
* | ||
* @return Google Translate Service | ||
*/ | ||
public static Translate createTranslateService() { | ||
TranslateOptions translateOption = TranslateOptions.builder() | ||
.retryParams(retryParams()) | ||
.connectTimeout(60000) | ||
.readTimeout(60000) | ||
.build(); | ||
return translateOption.service(); | ||
} | ||
|
||
/** | ||
* Retry params for the Translate API. | ||
*/ | ||
private static RetryParams retryParams() { | ||
return RetryParams.builder() | ||
.retryMaxAttempts(3) | ||
.maxRetryDelayMillis(30000) | ||
.totalRetryPeriodMillis(120000) | ||
.initialRetryDelayMillis(250) | ||
.build(); | ||
} | ||
|
||
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Are the default options not sufficient that this function is needed? Please file a bug against google-cloud-java if the defaults aren't working well for you.
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.
They do, but this is to demonstrate that we can set them and how. Obviously, developers will still have to look at the API documentation, but gives them a peek into the options.