diff --git a/language/snippets/src/main/java/beta/example/language/AnalyzeBeta.java b/language/snippets/src/main/java/beta/example/language/AnalyzeBeta.java deleted file mode 100644 index 9bf8bbc148d..00000000000 --- a/language/snippets/src/main/java/beta/example/language/AnalyzeBeta.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright 2017 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 beta.example.language; - -import com.example.language.Analyze; -import com.google.cloud.language.v1beta2.AnalyzeSentimentResponse; -import com.google.cloud.language.v1beta2.ClassificationCategory; -import com.google.cloud.language.v1beta2.ClassifyTextRequest; -import com.google.cloud.language.v1beta2.ClassifyTextResponse; -import com.google.cloud.language.v1beta2.Document; -import com.google.cloud.language.v1beta2.Document.Type; -import com.google.cloud.language.v1beta2.LanguageServiceClient; -import com.google.cloud.language.v1beta2.Sentiment; - -/** - * A sample application that uses the Natural Language API to perform entity, sentiment and syntax - * analysis. - */ -public class AnalyzeBeta { - - /** Detects entities,sentiment and syntax in a document using the Natural Language API. */ - public static void main(String[] args) throws Exception { - if (args.length < 2 || args.length > 4) { - System.err.println("Usage:"); - System.err.printf( - "\tjava %s \"command\" \"text to analyze\" \"language\" \n", - Analyze.class.getCanonicalName()); - System.exit(1); - } - String command = args[0]; - String text = args[1]; - String lang = null; - if (args.length > 2) { - lang = args[2]; - } - - if (command.equals("classify")) { - if (text.startsWith("gs://")) { - classifyFile(text); - } else { - classifyText(text); - } - } else if (command.equals("sentiment")) { - analyzeSentimentText(text, lang); - } - } - - /** Detects sentiments from the string {@code text}. */ - public static Sentiment analyzeSentimentText(String text, String lang) throws Exception { - // [START language_beta_sentiment_text] - // Instantiate a beta client : com.google.cloud.language.v1beta2.LanguageServiceClient - try (LanguageServiceClient language = LanguageServiceClient.create()) { - // NL auto-detects the language, if not provided - Document doc; - if (lang != null) { - doc = - Document.newBuilder() - .setLanguage(lang) - .setContent(text) - .setType(Type.PLAIN_TEXT) - .build(); - } else { - doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build(); - } - AnalyzeSentimentResponse response = language.analyzeSentiment(doc); - Sentiment sentiment = response.getDocumentSentiment(); - if (sentiment != null) { - System.out.println("Found sentiment."); - System.out.printf("\tMagnitude: %.3f\n", sentiment.getMagnitude()); - System.out.printf("\tScore: %.3f\n", sentiment.getScore()); - } else { - System.out.println("No sentiment found"); - } - return sentiment; - } - // [END language_beta_sentiment_text] - } - - /** Detects categories in text using the Language Beta API. */ - public static void classifyText(String text) throws Exception { - // [START language_classify_text] - // Instantiate a beta client : com.google.cloud.language.v1beta2.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 language_classify_text] - } - - /** Detects categories in a GCS hosted file using the Language Beta API. */ - public static void classifyFile(String gcsUri) throws Exception { - // [START language_classify_file] - // Instantiate a beta client : com.google.cloud.language.v1beta2.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 language_classify_file] - } -} diff --git a/language/snippets/src/main/java/com/example/language/Analyze.java b/language/snippets/src/main/java/com/example/language/Analyze.java index 04aaef4a887..aae0d6dcba0 100644 --- a/language/snippets/src/main/java/com/example/language/Analyze.java +++ b/language/snippets/src/main/java/com/example/language/Analyze.java @@ -282,7 +282,9 @@ public static void classifyText(String text) throws Exception { try (LanguageServiceClient language = LanguageServiceClient.create()) { // Set content to the text string Document doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build(); - V2Model v2Model = V2Model.setContentCategoriesVersion(ContentCategoriesVersion.V2).build(); + V2Model v2Model = V2Model.newBuilder() + .setContentCategoriesVersion(ContentCategoriesVersion.V2) + .build(); ClassificationModelOptions options = ClassificationModelOptions.newBuilder().setV2Model(v2Model).build(); ClassifyTextRequest request = diff --git a/language/snippets/src/test/java/beta/example/language/AnalyzeBetaIT.java b/language/snippets/src/test/java/beta/example/language/AnalyzeBetaIT.java deleted file mode 100644 index 5c4f1101f1d..00000000000 --- a/language/snippets/src/test/java/beta/example/language/AnalyzeBetaIT.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2017 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 beta.example.language; - -import static com.google.common.truth.Truth.assertThat; - -import com.example.language.Analyze; -import com.google.cloud.language.v1beta2.Sentiment; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -/** Integration (system) tests for {@link Analyze}. */ -@RunWith(JUnit4.class) -@SuppressWarnings("checkstyle:abbreviationaswordinname") -public class AnalyzeBetaIT { - - private ByteArrayOutputStream bout; - private PrintStream out; - private PrintStream originalPrintStream; - - @Before - public void setUp() { - bout = new ByteArrayOutputStream(); - out = new PrintStream(bout); - originalPrintStream = System.out; - System.setOut(out); - } - - @After - public void tearDown() { - // restores print statements in the original method - System.out.flush(); - System.setOut(originalPrintStream); - } - - @Test - public void analyzeSentiment_returnPositiveGerman() throws Exception { - Sentiment sentiment = - AnalyzeBeta.analyzeSentimentText("Ich hatte die schönste Erfahrung mit euch allen.", "DE"); - assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F); - assertThat(sentiment.getScore()).isGreaterThan(0.0F); - } - - @Test - public void analyzeCategoriesInTextReturnsExpectedResult() throws Exception { - AnalyzeBeta.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://cloud-samples-data/language/android.txt"; - AnalyzeBeta.classifyFile(gcsFile); - String got = bout.toString(); - assertThat(got).contains("Computers & Electronics"); - } -}