Skip to content
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

Update classify text samples to v1 #914

Merged
merged 3 commits into from
Nov 9, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions language/analysis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ java -cp target/language-entities-1.0-jar-with-dependencies.jar \
"The quick brown fox jumped over the lazy dog."
```

Analyze categories in text
```
java -cp target/language-entities-1.0-jar-with-dependencies.jar \
com.google.cloud.language.samples.Analyze classify \
"Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets."
```

Analyze categories in GCS file
```
java -cp target/language-entities-1.0-jar-with-dependencies.jar \
com.google.cloud.language.samples.Analyze classify \
"gs://cloud-samples-tests/natural-language/android-text.txt"
```

Included with the sample are `demo.sh` and `demo.bat` which show additional
examples of usage.

Expand Down
2 changes: 1 addition & 1 deletion language/analysis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ limitations under the License.
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-language</artifactId>
<version>0.26.0-beta</version>
<version>0.27.0-beta</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
import com.google.cloud.language.v1.AnalyzeSentimentResponse;
import com.google.cloud.language.v1.AnalyzeSyntaxRequest;
import com.google.cloud.language.v1.AnalyzeSyntaxResponse;
import com.google.cloud.language.v1.ClassificationCategory;
import com.google.cloud.language.v1.ClassifyTextRequest;
import com.google.cloud.language.v1.ClassifyTextResponse;
import com.google.cloud.language.v1.Document;
import com.google.cloud.language.v1.Document.Type;
import com.google.cloud.language.v1.EncodingType;
Expand Down Expand Up @@ -55,7 +58,13 @@ public static void main(String[] args) throws Exception {
String command = args[0];
String text = args[1];

if (command.equals("entities")) {
if (command.equals("classify")) {
if (text.startsWith("gs://")) {
classifyFile(text);
} else {
classifyText(text);
}
} else if (command.equals("entities")) {
if (text.startsWith("gs://")) {
analyzeEntitiesFile(text);
} else {
Expand Down Expand Up @@ -289,12 +298,65 @@ public static List<Token> analyzeSyntaxFile(String gcsUri) throws Exception {
}
// [END analyze_syntax_file]
}

/**
* Detects categories in text using the Language Beta API.
*/
public static void classifyText(String text) throws Exception {
// [START classify_text]
// Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
try (LanguageServiceClient language = LanguageServiceClient.create()) {
// set content to the text string
Document doc = Document.newBuilder()
.setContent(text)
.setType(Type.PLAIN_TEXT)
.build();
ClassifyTextRequest request = ClassifyTextRequest.newBuilder()
.setDocument(doc)
.build();
// detect categories in the given text
ClassifyTextResponse response = language.classifyText(request);

for (ClassificationCategory category : response.getCategoriesList()) {
System.out.printf("Category name : %s, Confidence : %.3f\n",
category.getName(), category.getConfidence());
}
}
// [END classify_text]
}

/**
* Detects categories in a GCS hosted file using the Language Beta API.
*/
public static void classifyFile(String gcsUri) throws Exception {
// [START classify_file]
// Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
try (LanguageServiceClient language = LanguageServiceClient.create()) {
// set the GCS content URI path
Document doc = Document.newBuilder()
.setGcsContentUri(gcsUri)
.setType(Type.PLAIN_TEXT)
.build();
ClassifyTextRequest request = ClassifyTextRequest.newBuilder()
.setDocument(doc)
.build();
// detect categories in the given file
ClassifyTextResponse response = language.classifyText(request);

for (ClassificationCategory category : response.getCategoriesList()) {
System.out.printf("Category name : %s, Confidence : %.3f\n",
category.getName(), category.getConfidence());
}
}
// [END classify_file]
}

/**
* Detects the entity sentiments in the string {@code text} using the Language Beta API.
*/
public static void entitySentimentText(String text) throws Exception {
// [START entity_sentiment_text]
// Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient
// Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
try (LanguageServiceClient language = LanguageServiceClient.create()) {
Document doc = Document.newBuilder()
.setContent(text).setType(Type.PLAIN_TEXT).build();
Expand Down Expand Up @@ -325,7 +387,7 @@ public static void entitySentimentText(String text) throws Exception {
*/
public static void entitySentimentFile(String gcsUri) throws Exception {
// [START entity_sentiment_file]
// Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient
// Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
try (LanguageServiceClient language = LanguageServiceClient.create()) {
Document doc = Document.newBuilder()
.setGcsContentUri(gcsUri)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ public void setUp() {
System.setOut(out);
}

@Test
public void analyzeCategoriesInTextReturnsExpectedResult() throws Exception {
Analyze.classifyText("Android is a mobile operating system developed by Google, "
+ "based on the Linux kernel and designed primarily for touchscreen "
+ "mobile devices such as smartphones and tablets.");
String got = bout.toString();
assertThat(got).contains("Computers & Electronics");
}

@Test
public void analyzeCategoriesInFileReturnsExpectedResult() throws Exception {
String gcsFile = "gs://" + PROJECT_ID + "/natural-language/android_text.txt";
Analyze.classifyFile(gcsFile);
String got = bout.toString();
assertThat(got).contains("Computers & Electronics");
}

@Test
public void analyzeEntities_withEntities_returnsLarryPage() throws Exception {
Analyze.analyzeEntitiesText(
Expand Down