diff --git a/java-language/README.md b/java-language/README.md index d12ecbfd101b..7c3743240138 100644 --- a/java-language/README.md +++ b/java-language/README.md @@ -146,9 +146,6 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-language/tree | Sample | Source Code | Try it | | --------------------------- | --------------------------------- | ------ | | Analyze Beta | [source code](https://github.com/googleapis/java-language/blob/main/samples/snippets/src/main/java/beta/example/language/AnalyzeBeta.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-language&page=editor&open_in_editor=samples/snippets/src/main/java/beta/example/language/AnalyzeBeta.java) | -| Analyze | [source code](https://github.com/googleapis/java-language/blob/main/samples/snippets/src/main/java/com/example/language/Analyze.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-language&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/language/Analyze.java) | -| Quickstart Sample | [source code](https://github.com/googleapis/java-language/blob/main/samples/snippets/src/main/java/com/example/language/QuickstartSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-language&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/language/QuickstartSample.java) | -| Set Endpoint | [source code](https://github.com/googleapis/java-language/blob/main/samples/snippets/src/main/java/com/example/language/SetEndpoint.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-language&page=editor&open_in_editor=samples/snippets/src/main/java/com/example/language/SetEndpoint.java) | diff --git a/java-language/samples/snippets/src/main/java/beta/example/language/AnalyzeBeta.java b/java-language/samples/snippets/src/main/java/beta/example/language/AnalyzeBeta.java index ff22eb4e9231..1a7aa38ed40d 100644 --- a/java-language/samples/snippets/src/main/java/beta/example/language/AnalyzeBeta.java +++ b/java-language/samples/snippets/src/main/java/beta/example/language/AnalyzeBeta.java @@ -16,7 +16,6 @@ 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; @@ -38,7 +37,7 @@ public static void main(String[] args) throws Exception { System.err.println("Usage:"); System.err.printf( "\tjava %s \"command\" \"text to analyze\" \"language\" \n", - Analyze.class.getCanonicalName()); + AnalyzeBeta.class.getCanonicalName()); System.exit(1); } String command = args[0]; diff --git a/java-language/samples/snippets/src/main/java/com/example/language/Analyze.java b/java-language/samples/snippets/src/main/java/com/example/language/Analyze.java deleted file mode 100644 index 545f7ce20823..000000000000 --- a/java-language/samples/snippets/src/main/java/com/example/language/Analyze.java +++ /dev/null @@ -1,387 +0,0 @@ -/* - * Copyright 2016 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 com.example.language; - -import com.google.cloud.language.v1.AnalyzeEntitiesRequest; -import com.google.cloud.language.v1.AnalyzeEntitiesResponse; -import com.google.cloud.language.v1.AnalyzeEntitySentimentRequest; -import com.google.cloud.language.v1.AnalyzeEntitySentimentResponse; -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.ClassificationModelOptions; -import com.google.cloud.language.v1.ClassificationModelOptions.V2Model; -import com.google.cloud.language.v1.ClassificationModelOptions.V2Model.ContentCategoriesVersion; -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; -import com.google.cloud.language.v1.Entity; -import com.google.cloud.language.v1.EntityMention; -import com.google.cloud.language.v1.LanguageServiceClient; -import com.google.cloud.language.v1.Sentiment; -import com.google.cloud.language.v1.Token; -import java.util.List; -import java.util.Map; - -/** - * A sample application that uses the Natural Language API to perform entity, sentiment and syntax - * analysis. - */ -public class Analyze { - - /** 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) { - System.err.println("Usage:"); - System.err.printf( - "\tjava %s \"command\" \"text to analyze\"\n", Analyze.class.getCanonicalName()); - System.exit(1); - } - String command = args[0]; - String text = args[1]; - - 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 { - analyzeEntitiesText(text); - } - } else if (command.equals("sentiment")) { - if (text.startsWith("gs://")) { - analyzeSentimentFile(text); - } else { - analyzeSentimentText(text); - } - } else if (command.equals("syntax")) { - if (text.startsWith("gs://")) { - analyzeSyntaxFile(text); - } else { - analyzeSyntaxText(text); - } - } else if (command.equals("entities-sentiment")) { - if (text.startsWith("gs://")) { - entitySentimentFile(text); - } else { - entitySentimentText(text); - } - } - } - - /** Identifies entities in the string {@code text}. */ - public static void analyzeEntitiesText(String text) throws Exception { - // [START language_entities_text] - // 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(); - AnalyzeEntitiesRequest request = - AnalyzeEntitiesRequest.newBuilder() - .setDocument(doc) - .setEncodingType(EncodingType.UTF16) - .build(); - - AnalyzeEntitiesResponse response = language.analyzeEntities(request); - - // Print the response - for (Entity entity : response.getEntitiesList()) { - System.out.printf("Entity: %s", entity.getName()); - System.out.printf("Salience: %.3f\n", entity.getSalience()); - System.out.println("Metadata: "); - for (Map.Entry entry : entity.getMetadataMap().entrySet()) { - System.out.printf("%s : %s", entry.getKey(), entry.getValue()); - } - for (EntityMention mention : entity.getMentionsList()) { - System.out.printf("Begin offset: %d\n", mention.getText().getBeginOffset()); - System.out.printf("Content: %s\n", mention.getText().getContent()); - System.out.printf("Type: %s\n\n", mention.getType()); - } - } - } - // [END language_entities_text] - } - - /** Identifies entities in the contents of the object at the given GCS {@code path}. */ - public static void analyzeEntitiesFile(String gcsUri) throws Exception { - // [START language_entities_gcs] - // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient - try (LanguageServiceClient language = LanguageServiceClient.create()) { - // Set the GCS Content URI path to the file to be analyzed - Document doc = - Document.newBuilder().setGcsContentUri(gcsUri).setType(Type.PLAIN_TEXT).build(); - AnalyzeEntitiesRequest request = - AnalyzeEntitiesRequest.newBuilder() - .setDocument(doc) - .setEncodingType(EncodingType.UTF16) - .build(); - - AnalyzeEntitiesResponse response = language.analyzeEntities(request); - - // Print the response - for (Entity entity : response.getEntitiesList()) { - System.out.printf("Entity: %s\n", entity.getName()); - System.out.printf("Salience: %.3f\n", entity.getSalience()); - System.out.println("Metadata: "); - for (Map.Entry entry : entity.getMetadataMap().entrySet()) { - System.out.printf("%s : %s", entry.getKey(), entry.getValue()); - } - for (EntityMention mention : entity.getMentionsList()) { - System.out.printf("Begin offset: %d\n", mention.getText().getBeginOffset()); - System.out.printf("Content: %s\n", mention.getText().getContent()); - System.out.printf("Type: %s\n\n", mention.getType()); - } - } - } - // [END language_entities_gcs] - } - - /** Identifies the sentiment in the string {@code text}. */ - public static Sentiment analyzeSentimentText(String text) throws Exception { - // [START language_sentiment_text] - // 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(); - AnalyzeSentimentResponse response = language.analyzeSentiment(doc); - Sentiment sentiment = response.getDocumentSentiment(); - if (sentiment == null) { - System.out.println("No sentiment found"); - } else { - System.out.printf("Sentiment magnitude: %.3f\n", sentiment.getMagnitude()); - System.out.printf("Sentiment score: %.3f\n", sentiment.getScore()); - } - return sentiment; - } - // [END language_sentiment_text] - } - - /** Gets {@link Sentiment} from the contents of the GCS hosted file. */ - public static Sentiment analyzeSentimentFile(String gcsUri) throws Exception { - // [START language_sentiment_gcs] - // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient - try (LanguageServiceClient language = LanguageServiceClient.create()) { - Document doc = - Document.newBuilder().setGcsContentUri(gcsUri).setType(Type.PLAIN_TEXT).build(); - AnalyzeSentimentResponse response = language.analyzeSentiment(doc); - Sentiment sentiment = response.getDocumentSentiment(); - if (sentiment == null) { - System.out.println("No sentiment found"); - } else { - System.out.printf("Sentiment magnitude : %.3f\n", sentiment.getMagnitude()); - System.out.printf("Sentiment score : %.3f\n", sentiment.getScore()); - } - return sentiment; - } - // [END language_sentiment_gcs] - } - - /** from the string {@code text}. */ - public static List analyzeSyntaxText(String text) throws Exception { - // [START language_syntax_text] - // 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(); - AnalyzeSyntaxRequest request = - AnalyzeSyntaxRequest.newBuilder() - .setDocument(doc) - .setEncodingType(EncodingType.UTF16) - .build(); - // Analyze the syntax in the given text - AnalyzeSyntaxResponse response = language.analyzeSyntax(request); - // Print the response - for (Token token : response.getTokensList()) { - System.out.printf("\tText: %s\n", token.getText().getContent()); - System.out.printf("\tBeginOffset: %d\n", token.getText().getBeginOffset()); - System.out.printf("Lemma: %s\n", token.getLemma()); - System.out.printf("PartOfSpeechTag: %s\n", token.getPartOfSpeech().getTag()); - System.out.printf("\tAspect: %s\n", token.getPartOfSpeech().getAspect()); - System.out.printf("\tCase: %s\n", token.getPartOfSpeech().getCase()); - System.out.printf("\tForm: %s\n", token.getPartOfSpeech().getForm()); - System.out.printf("\tGender: %s\n", token.getPartOfSpeech().getGender()); - System.out.printf("\tMood: %s\n", token.getPartOfSpeech().getMood()); - System.out.printf("\tNumber: %s\n", token.getPartOfSpeech().getNumber()); - System.out.printf("\tPerson: %s\n", token.getPartOfSpeech().getPerson()); - System.out.printf("\tProper: %s\n", token.getPartOfSpeech().getProper()); - System.out.printf("\tReciprocity: %s\n", token.getPartOfSpeech().getReciprocity()); - System.out.printf("\tTense: %s\n", token.getPartOfSpeech().getTense()); - System.out.printf("\tVoice: %s\n", token.getPartOfSpeech().getVoice()); - System.out.println("DependencyEdge"); - System.out.printf("\tHeadTokenIndex: %d\n", token.getDependencyEdge().getHeadTokenIndex()); - System.out.printf("\tLabel: %s\n\n", token.getDependencyEdge().getLabel()); - } - return response.getTokensList(); - } - // [END language_syntax_text] - } - - /** Get the syntax of the GCS hosted file. */ - public static List analyzeSyntaxFile(String gcsUri) throws Exception { - // [START language_syntax_gcs] - // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient - try (LanguageServiceClient language = LanguageServiceClient.create()) { - Document doc = - Document.newBuilder().setGcsContentUri(gcsUri).setType(Type.PLAIN_TEXT).build(); - AnalyzeSyntaxRequest request = - AnalyzeSyntaxRequest.newBuilder() - .setDocument(doc) - .setEncodingType(EncodingType.UTF16) - .build(); - // Analyze the syntax in the given text - AnalyzeSyntaxResponse response = language.analyzeSyntax(request); - // Print the response - for (Token token : response.getTokensList()) { - System.out.printf("\tText: %s\n", token.getText().getContent()); - System.out.printf("\tBeginOffset: %d\n", token.getText().getBeginOffset()); - System.out.printf("Lemma: %s\n", token.getLemma()); - System.out.printf("PartOfSpeechTag: %s\n", token.getPartOfSpeech().getTag()); - System.out.printf("\tAspect: %s\n", token.getPartOfSpeech().getAspect()); - System.out.printf("\tCase: %s\n", token.getPartOfSpeech().getCase()); - System.out.printf("\tForm: %s\n", token.getPartOfSpeech().getForm()); - System.out.printf("\tGender: %s\n", token.getPartOfSpeech().getGender()); - System.out.printf("\tMood: %s\n", token.getPartOfSpeech().getMood()); - System.out.printf("\tNumber: %s\n", token.getPartOfSpeech().getNumber()); - System.out.printf("\tPerson: %s\n", token.getPartOfSpeech().getPerson()); - System.out.printf("\tProper: %s\n", token.getPartOfSpeech().getProper()); - System.out.printf("\tReciprocity: %s\n", token.getPartOfSpeech().getReciprocity()); - System.out.printf("\tTense: %s\n", token.getPartOfSpeech().getTense()); - System.out.printf("\tVoice: %s\n", token.getPartOfSpeech().getVoice()); - System.out.println("DependencyEdge"); - System.out.printf("\tHeadTokenIndex: %d\n", token.getDependencyEdge().getHeadTokenIndex()); - System.out.printf("\tLabel: %s\n\n", token.getDependencyEdge().getLabel()); - } - - return response.getTokensList(); - } - // [END language_syntax_gcs] - } - - /** Detects categories in text using the Language Beta API. */ - public static void classifyText(String text) throws Exception { - // [START language_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(); - V2Model v2Model = - V2Model.newBuilder().setContentCategoriesVersion(ContentCategoriesVersion.V2).build(); - ClassificationModelOptions options = - ClassificationModelOptions.newBuilder().setV2Model(v2Model).build(); - ClassifyTextRequest request = - ClassifyTextRequest.newBuilder() - .setDocument(doc) - .setClassificationModelOptions(options) - .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_gcs] - // 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 language_classify_gcs] - } - - /** Detects the entity sentiments in the string {@code text} using the Language Beta API. */ - public static void entitySentimentText(String text) throws Exception { - // [START language_entity_sentiment_text] - // 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(); - AnalyzeEntitySentimentRequest request = - AnalyzeEntitySentimentRequest.newBuilder() - .setDocument(doc) - .setEncodingType(EncodingType.UTF16) - .build(); - // Detect entity sentiments in the given string - AnalyzeEntitySentimentResponse response = language.analyzeEntitySentiment(request); - // Print the response - for (Entity entity : response.getEntitiesList()) { - System.out.printf("Entity: %s\n", entity.getName()); - System.out.printf("Salience: %.3f\n", entity.getSalience()); - System.out.printf("Sentiment : %s\n", entity.getSentiment()); - for (EntityMention mention : entity.getMentionsList()) { - System.out.printf("Begin offset: %d\n", mention.getText().getBeginOffset()); - System.out.printf("Content: %s\n", mention.getText().getContent()); - System.out.printf("Magnitude: %.3f\n", mention.getSentiment().getMagnitude()); - System.out.printf("Sentiment score : %.3f\n", mention.getSentiment().getScore()); - System.out.printf("Type: %s\n\n", mention.getType()); - } - } - } - // [END language_entity_sentiment_text] - } - - /** Identifies the entity sentiments in the GCS hosted file using the Language Beta API. */ - public static void entitySentimentFile(String gcsUri) throws Exception { - // [START language_entity_sentiment_gcs] - // Instantiate the Language client com.google.cloud.language.v1.LanguageServiceClient - try (LanguageServiceClient language = LanguageServiceClient.create()) { - Document doc = - Document.newBuilder().setGcsContentUri(gcsUri).setType(Type.PLAIN_TEXT).build(); - AnalyzeEntitySentimentRequest request = - AnalyzeEntitySentimentRequest.newBuilder() - .setDocument(doc) - .setEncodingType(EncodingType.UTF16) - .build(); - // Detect entity sentiments in the given file - AnalyzeEntitySentimentResponse response = language.analyzeEntitySentiment(request); - // Print the response - for (Entity entity : response.getEntitiesList()) { - System.out.printf("Entity: %s\n", entity.getName()); - System.out.printf("Salience: %.3f\n", entity.getSalience()); - System.out.printf("Sentiment : %s\n", entity.getSentiment()); - for (EntityMention mention : entity.getMentionsList()) { - System.out.printf("Begin offset: %d\n", mention.getText().getBeginOffset()); - System.out.printf("Content: %s\n", mention.getText().getContent()); - System.out.printf("Magnitude: %.3f\n", mention.getSentiment().getMagnitude()); - System.out.printf("Sentiment score : %.3f\n", mention.getSentiment().getScore()); - System.out.printf("Type: %s\n\n", mention.getType()); - } - } - } - // [END language_entity_sentiment_gcs] - } -} diff --git a/java-language/samples/snippets/src/main/java/com/example/language/QuickstartSample.java b/java-language/samples/snippets/src/main/java/com/example/language/QuickstartSample.java deleted file mode 100644 index c264dc96dd6f..000000000000 --- a/java-language/samples/snippets/src/main/java/com/example/language/QuickstartSample.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2016 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 com.example.language; - -// [START language_quickstart] -// Imports the Google Cloud client library -import com.google.cloud.language.v1.Document; -import com.google.cloud.language.v1.Document.Type; -import com.google.cloud.language.v1.LanguageServiceClient; -import com.google.cloud.language.v1.Sentiment; - -public class QuickstartSample { - public static void main(String... args) throws Exception { - // Instantiates a client - try (LanguageServiceClient language = LanguageServiceClient.create()) { - - // The text to analyze - String text = "Hello, world!"; - Document doc = Document.newBuilder().setContent(text).setType(Type.PLAIN_TEXT).build(); - - // Detects the sentiment of the text - Sentiment sentiment = language.analyzeSentiment(doc).getDocumentSentiment(); - - System.out.printf("Text: %s%n", text); - System.out.printf("Sentiment: %s, %s%n", sentiment.getScore(), sentiment.getMagnitude()); - } - } -} -// [END language_quickstart] diff --git a/java-language/samples/snippets/src/main/java/com/example/language/SetEndpoint.java b/java-language/samples/snippets/src/main/java/com/example/language/SetEndpoint.java deleted file mode 100644 index 73ab525a4fb4..000000000000 --- a/java-language/samples/snippets/src/main/java/com/example/language/SetEndpoint.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2019 Google LLC - * - * 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.example.language; - -import com.google.cloud.language.v1.Document; -import com.google.cloud.language.v1.LanguageServiceClient; -import com.google.cloud.language.v1.LanguageServiceSettings; -import com.google.cloud.language.v1.Sentiment; -import java.io.IOException; - -class SetEndpoint { - - // Change your endpoint - static void setEndpoint() throws IOException { - // [START language_set_endpoint] - LanguageServiceSettings settings = - LanguageServiceSettings.newBuilder().setEndpoint("eu-language.googleapis.com:443").build(); - - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // the "close" method on the client to safely clean up any remaining background resources. - LanguageServiceClient client = LanguageServiceClient.create(settings); - // [END language_set_endpoint] - - // The text to analyze - String text = "Hello, world!"; - Document doc = Document.newBuilder().setContent(text).setType(Document.Type.PLAIN_TEXT).build(); - - // Detects the sentiment of the text - Sentiment sentiment = client.analyzeSentiment(doc).getDocumentSentiment(); - - System.out.printf("Text: %s%n", text); - System.out.printf("Sentiment: %s, %s%n", sentiment.getScore(), sentiment.getMagnitude()); - client.close(); - } -} diff --git a/java-language/samples/snippets/src/test/java/beta/example/language/AnalyzeBetaIT.java b/java-language/samples/snippets/src/test/java/beta/example/language/AnalyzeBetaIT.java index 5c4f1101f1de..8922425b8d16 100644 --- a/java-language/samples/snippets/src/test/java/beta/example/language/AnalyzeBetaIT.java +++ b/java-language/samples/snippets/src/test/java/beta/example/language/AnalyzeBetaIT.java @@ -18,7 +18,6 @@ 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; @@ -28,7 +27,7 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -/** Integration (system) tests for {@link Analyze}. */ +/** Integration (system) tests for {@link AnalyzeBeta}. */ @RunWith(JUnit4.class) @SuppressWarnings("checkstyle:abbreviationaswordinname") public class AnalyzeBetaIT { diff --git a/java-language/samples/snippets/src/test/java/com/example/language/AnalyzeIT.java b/java-language/samples/snippets/src/test/java/com/example/language/AnalyzeIT.java deleted file mode 100644 index f80ee83cc10c..000000000000 --- a/java-language/samples/snippets/src/test/java/com/example/language/AnalyzeIT.java +++ /dev/null @@ -1,177 +0,0 @@ -/* - * Copyright 2016 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 com.example.language; - -import static com.google.common.truth.Truth.assertThat; - -import com.google.cloud.language.v1.PartOfSpeech.Tag; -import com.google.cloud.language.v1.Sentiment; -import com.google.cloud.language.v1.Token; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.List; -import java.util.stream.Collectors; -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 AnalyzeIT { - - 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 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://cloud-samples-data/language/android.txt"; - Analyze.classifyFile(gcsFile); - String got = bout.toString(); - assertThat(got).contains("Computers & Electronics"); - } - - @Test - public void analyzeEntities_withEntities_returnsLarryPage() throws Exception { - Analyze.analyzeEntitiesText( - "Larry Page, Google's co-founder, once described the 'perfect search engine' as" - + " something that 'understands exactly what you mean and gives you back exactly what" - + " you want.' Since he spoke those words Google has grown to offer products beyond" - + " search, but the spirit of what he said remains."); - String got = bout.toString(); - assertThat(got).contains("Larry Page"); - } - - @Test - public void analyzeEntities_withEntitiesFile_containsCalifornia() throws Exception { - Analyze.analyzeEntitiesFile("gs://cloud-samples-data/language/entity.txt"); - String got = bout.toString(); - assertThat(got).contains("California"); - } - - @Test - public void analyzeSentimentText_returnPositive() throws Exception { - Sentiment sentiment = - Analyze.analyzeSentimentText( - "Tom Cruise is one of the finest actors in hollywood and a great star!"); - assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F); - assertThat(sentiment.getScore()).isGreaterThan(0.0F); - } - - @Test - public void analyzeSentimentFile_returnPositiveFile() throws Exception { - Sentiment sentiment = - Analyze.analyzeSentimentFile( - "gs://cloud-samples-data/language/" + "sentiment-positive.txt"); - assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F); - assertThat(sentiment.getScore()).isGreaterThan(0.0F); - } - - @Test - public void analyzeSentimentText_returnNegative() throws Exception { - Sentiment sentiment = - Analyze.analyzeSentimentText("That was the worst performance I've seen in a while."); - assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F); - assertThat(sentiment.getScore()).isLessThan(0.0F); - } - - @Test - public void analyzeSentiment_returnNegative() throws Exception { - Sentiment sentiment = - Analyze.analyzeSentimentFile( - "gs://cloud-samples-data/language/" + "sentiment-negative.txt"); - assertThat(sentiment.getMagnitude()).isGreaterThan(0.0F); - assertThat(sentiment.getScore()).isLessThan(0.0F); - } - - @Test - public void analyzeSyntax_partOfSpeech() throws Exception { - List tokens = - Analyze.analyzeSyntaxText("President Obama was elected for the second term"); - - List got = - tokens.stream().map(e -> e.getPartOfSpeech().getTag()).collect(Collectors.toList()); - - assertThat(got) - .containsExactly( - Tag.NOUN, Tag.NOUN, Tag.VERB, Tag.VERB, Tag.ADP, Tag.DET, Tag.ADJ, Tag.NOUN) - .inOrder(); - } - - @Test - public void analyzeSyntax_partOfSpeechFile() throws Exception { - List token = - Analyze.analyzeSyntaxFile("gs://cloud-samples-data/language/" + "syntax-sentence.txt"); - - List got = - token.stream().map(e -> e.getPartOfSpeech().getTag()).collect(Collectors.toList()); - assertThat(got) - .containsExactly(Tag.DET, Tag.VERB, Tag.DET, Tag.ADJ, Tag.NOUN, Tag.PUNCT) - .inOrder(); - } - - @Test - public void analyzeEntitySentimentTextReturnsExpectedResult() throws Exception { - Analyze.entitySentimentText( - "Oranges, grapes, and apples can be " - + "found in the cafeterias located in Mountain View, Seattle, and London."); - String got = bout.toString(); - assertThat(got).contains("Seattle"); - } - - @Test - public void analyzeEntitySentimentTextEncodedReturnsExpectedResult() throws Exception { - Analyze.entitySentimentText("foo→bar"); - String got = bout.toString(); - assertThat(got).contains("offset: 4"); - } - - @Test - public void analyzeEntitySentimenFileReturnsExpectedResult() throws Exception { - Analyze.entitySentimentFile("gs://cloud-samples-data/language/president.txt"); - String got = bout.toString(); - assertThat(got).contains("Kennedy"); - } -} diff --git a/java-language/samples/snippets/src/test/java/com/example/language/QuickstartSampleIT.java b/java-language/samples/snippets/src/test/java/com/example/language/QuickstartSampleIT.java deleted file mode 100644 index 15a89ff5d963..000000000000 --- a/java-language/samples/snippets/src/test/java/com/example/language/QuickstartSampleIT.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2016 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 com.example.language; - -import static com.google.common.truth.Truth.assertThat; - -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; - -/** Tests for quickstart sample. */ -@RunWith(JUnit4.class) -@SuppressWarnings("checkstyle:abbreviationaswordinname") -public class QuickstartSampleIT { - 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 testQuickstart() throws Exception { - // Act - QuickstartSample.main(); - - // Assert - String got = bout.toString(); - assertThat(got).contains("Text: Hello, world!"); - assertThat(got).contains("Sentiment: "); - } -} diff --git a/java-language/samples/snippets/src/test/java/com/example/language/SetEndpointIT.java b/java-language/samples/snippets/src/test/java/com/example/language/SetEndpointIT.java deleted file mode 100644 index f464f4727357..000000000000 --- a/java-language/samples/snippets/src/test/java/com/example/language/SetEndpointIT.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2019 Google LLC - * - * 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.example.language; - -import static com.google.common.truth.Truth.assertThat; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -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; - -/** Tests for Natural Language Set Endpoint */ -@RunWith(JUnit4.class) -@SuppressWarnings("checkstyle:abbreviationaswordinname") -public class SetEndpointIT { - - 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 testSetEndpoint() throws IOException { - // Act - SetEndpoint.setEndpoint(); - - // Assert - String got = bout.toString(); - assertThat(got).contains("Sentiment"); - } -}