Skip to content

Commit 54537d2

Browse files
authored
Update classify text samples to v1 (GoogleCloudPlatform#914)
1 parent 5a3c8b6 commit 54537d2

File tree

4 files changed

+97
-4
lines changed

4 files changed

+97
-4
lines changed

language/analysis/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,20 @@ java -cp target/language-entities-1.0-jar-with-dependencies.jar \
7676
"The quick brown fox jumped over the lazy dog."
7777
```
7878

79+
Analyze categories in text
80+
```
81+
java -cp target/language-entities-1.0-jar-with-dependencies.jar \
82+
com.google.cloud.language.samples.Analyze classify \
83+
"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."
84+
```
85+
86+
Analyze categories in GCS file
87+
```
88+
java -cp target/language-entities-1.0-jar-with-dependencies.jar \
89+
com.google.cloud.language.samples.Analyze classify \
90+
"gs://cloud-samples-tests/natural-language/android-text.txt"
91+
```
92+
7993
Included with the sample are `demo.sh` and `demo.bat` which show additional
8094
examples of usage.
8195

language/analysis/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ limitations under the License.
3232
<dependency>
3333
<groupId>com.google.cloud</groupId>
3434
<artifactId>google-cloud-language</artifactId>
35-
<version>0.26.0-beta</version>
35+
<version>0.27.0-beta</version>
3636
</dependency>
3737
<dependency>
3838
<groupId>com.google.guava</groupId>

language/analysis/src/main/java/com/google/cloud/language/samples/Analyze.java

+65-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import com.google.cloud.language.v1.AnalyzeSentimentResponse;
2424
import com.google.cloud.language.v1.AnalyzeSyntaxRequest;
2525
import com.google.cloud.language.v1.AnalyzeSyntaxResponse;
26+
import com.google.cloud.language.v1.ClassificationCategory;
27+
import com.google.cloud.language.v1.ClassifyTextRequest;
28+
import com.google.cloud.language.v1.ClassifyTextResponse;
2629
import com.google.cloud.language.v1.Document;
2730
import com.google.cloud.language.v1.Document.Type;
2831
import com.google.cloud.language.v1.EncodingType;
@@ -55,7 +58,13 @@ public static void main(String[] args) throws Exception {
5558
String command = args[0];
5659
String text = args[1];
5760

58-
if (command.equals("entities")) {
61+
if (command.equals("classify")) {
62+
if (text.startsWith("gs://")) {
63+
classifyFile(text);
64+
} else {
65+
classifyText(text);
66+
}
67+
} else if (command.equals("entities")) {
5968
if (text.startsWith("gs://")) {
6069
analyzeEntitiesFile(text);
6170
} else {
@@ -289,12 +298,65 @@ public static List<Token> analyzeSyntaxFile(String gcsUri) throws Exception {
289298
}
290299
// [END analyze_syntax_file]
291300
}
301+
302+
/**
303+
* Detects categories in text using the Language Beta API.
304+
*/
305+
public static void classifyText(String text) throws Exception {
306+
// [START classify_text]
307+
// Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
308+
try (LanguageServiceClient language = LanguageServiceClient.create()) {
309+
// set content to the text string
310+
Document doc = Document.newBuilder()
311+
.setContent(text)
312+
.setType(Type.PLAIN_TEXT)
313+
.build();
314+
ClassifyTextRequest request = ClassifyTextRequest.newBuilder()
315+
.setDocument(doc)
316+
.build();
317+
// detect categories in the given text
318+
ClassifyTextResponse response = language.classifyText(request);
319+
320+
for (ClassificationCategory category : response.getCategoriesList()) {
321+
System.out.printf("Category name : %s, Confidence : %.3f\n",
322+
category.getName(), category.getConfidence());
323+
}
324+
}
325+
// [END classify_text]
326+
}
327+
328+
/**
329+
* Detects categories in a GCS hosted file using the Language Beta API.
330+
*/
331+
public static void classifyFile(String gcsUri) throws Exception {
332+
// [START classify_file]
333+
// Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
334+
try (LanguageServiceClient language = LanguageServiceClient.create()) {
335+
// set the GCS content URI path
336+
Document doc = Document.newBuilder()
337+
.setGcsContentUri(gcsUri)
338+
.setType(Type.PLAIN_TEXT)
339+
.build();
340+
ClassifyTextRequest request = ClassifyTextRequest.newBuilder()
341+
.setDocument(doc)
342+
.build();
343+
// detect categories in the given file
344+
ClassifyTextResponse response = language.classifyText(request);
345+
346+
for (ClassificationCategory category : response.getCategoriesList()) {
347+
System.out.printf("Category name : %s, Confidence : %.3f\n",
348+
category.getName(), category.getConfidence());
349+
}
350+
}
351+
// [END classify_file]
352+
}
353+
292354
/**
293355
* Detects the entity sentiments in the string {@code text} using the Language Beta API.
294356
*/
295357
public static void entitySentimentText(String text) throws Exception {
296358
// [START entity_sentiment_text]
297-
// Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient
359+
// Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
298360
try (LanguageServiceClient language = LanguageServiceClient.create()) {
299361
Document doc = Document.newBuilder()
300362
.setContent(text).setType(Type.PLAIN_TEXT).build();
@@ -325,7 +387,7 @@ public static void entitySentimentText(String text) throws Exception {
325387
*/
326388
public static void entitySentimentFile(String gcsUri) throws Exception {
327389
// [START entity_sentiment_file]
328-
// Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient
390+
// Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient
329391
try (LanguageServiceClient language = LanguageServiceClient.create()) {
330392
Document doc = Document.newBuilder()
331393
.setGcsContentUri(gcsUri)

language/analysis/src/test/java/com/google/cloud/language/samples/AnalyzeIT.java

+17
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ public void setUp() {
5252
System.setOut(out);
5353
}
5454

55+
@Test
56+
public void analyzeCategoriesInTextReturnsExpectedResult() throws Exception {
57+
Analyze.classifyText("Android is a mobile operating system developed by Google, "
58+
+ "based on the Linux kernel and designed primarily for touchscreen "
59+
+ "mobile devices such as smartphones and tablets.");
60+
String got = bout.toString();
61+
assertThat(got).contains("Computers & Electronics");
62+
}
63+
64+
@Test
65+
public void analyzeCategoriesInFileReturnsExpectedResult() throws Exception {
66+
String gcsFile = "gs://" + PROJECT_ID + "/natural-language/android_text.txt";
67+
Analyze.classifyFile(gcsFile);
68+
String got = bout.toString();
69+
assertThat(got).contains("Computers & Electronics");
70+
}
71+
5572
@Test
5673
public void analyzeEntities_withEntities_returnsLarryPage() throws Exception {
5774
Analyze.analyzeEntitiesText(

0 commit comments

Comments
 (0)