From cf8c6c76a12785dde72f5e1baadd0540b4c114a1 Mon Sep 17 00:00:00 2001 From: shafang Date: Tue, 31 Dec 2019 11:44:16 -0800 Subject: [PATCH 1/3] add async samples and refactor sync samples --- .../ai/textanalytics/AnalyzeSentiment.java | 8 +- .../textanalytics/AnalyzeSentimentAsync.java | 61 ++++++++++++ .../ai/textanalytics/ExtractKeyPhrases.java | 6 +- .../textanalytics/ExtractKeyPhrasesAsync.java | 47 +++++++++ .../azure/ai/textanalytics/HelloWorld.java | 8 +- .../ai/textanalytics/HelloWorldAsync.java | 6 +- .../ai/textanalytics/RecognizeEntities.java | 6 +- .../textanalytics/RecognizeEntitiesAsync.java | 53 ++++++++++ .../RecognizeLinkedEntities.java | 6 +- .../RecognizeLinkedEntitiesAsync.java | 49 ++++++++++ .../azure/ai/textanalytics/RecognizePii.java | 6 +- .../ai/textanalytics/RecognizePiiAsync.java | 54 +++++++++++ .../batch/AnalyzeSentimentBatchDocuments.java | 30 ++++-- .../AnalyzeSentimentBatchDocumentsAsync.java | 97 +++++++++++++++++++ .../batch/DetectLanguageBatchDocuments.java | 24 +++-- .../DetectLanguageBatchDocumentsAsync.java | 93 ++++++++++++++++++ .../ExtractKeyPhrasesBatchDocuments.java | 30 ++++-- .../ExtractKeyPhrasesBatchDocumentsAsync.java | 83 ++++++++++++++++ .../RecognizeEntitiesBatchDocuments.java | 31 ++++-- .../RecognizeEntitiesBatchDocumentsAsync.java | 90 +++++++++++++++++ ...RecognizeLinkedEntitiesBatchDocuments.java | 34 +++++-- ...nizeLinkedEntitiesBatchDocumentsAsync.java | 87 +++++++++++++++++ .../batch/RecognizePiiBatchDocuments.java | 38 +++++--- .../RecognizePiiBatchDocumentsAsync.java | 91 +++++++++++++++++ 24 files changed, 960 insertions(+), 78 deletions(-) create mode 100644 sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentimentAsync.java create mode 100644 sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ExtractKeyPhrasesAsync.java create mode 100644 sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeEntitiesAsync.java create mode 100644 sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeLinkedEntitiesAsync.java create mode 100644 sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizePiiAsync.java create mode 100644 sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocumentsAsync.java create mode 100644 sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocumentsAsync.java create mode 100644 sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocumentsAsync.java create mode 100644 sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocumentsAsync.java create mode 100644 sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocumentsAsync.java create mode 100644 sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocumentsAsync.java diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentiment.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentiment.java index 7273908ac285..c7405a997449 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentiment.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentiment.java @@ -20,8 +20,8 @@ public class AnalyzeSentiment { public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("subscription-key") - .endpoint("https://servicename.cognitiveservices.azure.com/") + .subscriptionKey("") + .endpoint("") .buildClient(); // The text that need be analysed. @@ -31,7 +31,7 @@ public static void main(String[] args) { final TextSentiment documentSentiment = sentimentResult.getDocumentSentiment(); System.out.printf( - "Recognized TextSentiment: %s, Positive Score: %s, Neutral Score: %s, Negative Score: %s.%n", + "Recognized sentiment: %s, positive score: %s, neutral score: %s, negative score: %s.%n", documentSentiment.getTextSentimentClass(), documentSentiment.getPositiveScore(), documentSentiment.getNeutralScore(), @@ -40,7 +40,7 @@ public static void main(String[] args) { final List sentiments = sentimentResult.getSentenceSentiments(); for (TextSentiment textSentiment : sentiments) { System.out.printf( - "Recognized Sentence TextSentiment: %s, Positive Score: %s, Neutral Score: %s, Negative Score: %s.%n", + "Recognized sentence sentiment: %s, positive score: %s, neutral score: %s, negative score: %s.%n", textSentiment.getTextSentimentClass(), textSentiment.getPositiveScore(), textSentiment.getNeutralScore(), diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentimentAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentimentAsync.java new file mode 100644 index 000000000000..7b4f1e7a23cd --- /dev/null +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentimentAsync.java @@ -0,0 +1,61 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.ai.textanalytics; + +import com.azure.ai.textanalytics.models.TextSentiment; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * Sample demonstrate how to analyze sentiment of a text input in asynchronously call. + */ +public class AnalyzeSentimentAsync { + /** + * Main method to invoke this demo about how to analyze sentiment of a text input. + * + * @param args Unused arguments to the program. + */ + public static void main(String[] args) { + // Instantiate a client that will be used to call the service. + TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() + .subscriptionKey("") + .endpoint("") + .buildAsyncClient(); + + // The text that need be analysed. + String text = "The hotel was dark and unclean."; + + client.analyzeSentiment(text).subscribe( + result -> { + final TextSentiment documentSentiment = result.getDocumentSentiment(); + System.out.printf( + "Recognized sentiment: %s, positive score: %s, neutral score: %s, negative score: %s.%n", + documentSentiment.getTextSentimentClass(), + documentSentiment.getPositiveScore(), + documentSentiment.getNeutralScore(), + documentSentiment.getNegativeScore()); + + final List sentiments = result.getSentenceSentiments(); + for (TextSentiment textSentiment : sentiments) { + System.out.printf( + "Recognized sentence sentiment: %s, positive score: %s, neutral score: %s, negative score: %s.%n", + textSentiment.getTextSentimentClass(), + textSentiment.getPositiveScore(), + textSentiment.getNeutralScore(), + textSentiment.getNegativeScore()); + } + }, + error -> System.err.println("There was an error analyzing sentiment of the text." + error), + () -> System.out.println("Sentiment analyzed.")); + + // The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep + // the thread so the program does not end before the send operation is complete. Using .block() instead of + // .subscribe() will turn this into a synchronous call. + try { + TimeUnit.SECONDS.sleep(5); + } catch (InterruptedException ignored) { + } + } +} diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ExtractKeyPhrases.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ExtractKeyPhrases.java index 73c1d6cfcf8e..e54338395f30 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ExtractKeyPhrases.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ExtractKeyPhrases.java @@ -15,15 +15,15 @@ public class ExtractKeyPhrases { public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("subscription-key") - .endpoint("https://servicename.cognitiveservices.azure.com/") + .subscriptionKey("") + .endpoint("") .buildClient(); // The text that need be analysed. String text = "My cat might need to see a veterinarian."; for (String keyPhrase : client.extractKeyPhrases(text).getKeyPhrases()) { - System.out.printf("Recognized Phrases: %s.%n", keyPhrase); + System.out.printf("Recognized phrases: %s.%n", keyPhrase); } } } diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ExtractKeyPhrasesAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ExtractKeyPhrasesAsync.java new file mode 100644 index 000000000000..fd2b480183e7 --- /dev/null +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ExtractKeyPhrasesAsync.java @@ -0,0 +1,47 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.ai.textanalytics; + +import com.azure.ai.textanalytics.models.TextSentiment; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * Sample demonstrate how to analyze key phrases of a text input in asynchronously call. + */ +public class ExtractKeyPhrasesAsync { + /** + * Main method to invoke this demo about how to extract key phrases of a text input. + * + * @param args Unused arguments to the program. + */ + public static void main(String[] args) { + // Instantiate a client that will be used to call the service. + TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() + .subscriptionKey("") + .endpoint("") + .buildAsyncClient(); + + // The text that need be analysed. + String text = "My cat might need to see a veterinarian."; + + client.extractKeyPhrases(text).subscribe( + result -> { + for (String keyPhrase : result.getKeyPhrases()) { + System.out.printf("Recognized phrases: %s.%n", keyPhrase); + } + }, + error -> System.err.println("There was an error extracting key phrases of the text." + error), + () -> System.out.println("Key phrases extracted.")); + + // The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep + // the thread so the program does not end before the send operation is complete. Using .block() instead of + // .subscribe() will turn this into a synchronous call. + try { + TimeUnit.SECONDS.sleep(5); + } catch (InterruptedException ignored) { + } + } +} diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/HelloWorld.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/HelloWorld.java index 84e172062581..b01d9b55a405 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/HelloWorld.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/HelloWorld.java @@ -20,8 +20,8 @@ public class HelloWorld { public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("subscription-key") - .endpoint("https://servicename.cognitiveservices.azure.com/") + .subscriptionKey("") + .endpoint("") .buildClient(); // The text that need be analysed. @@ -29,14 +29,14 @@ public static void main(String[] args) { final DetectLanguageResult detectLanguageResult = client.detectLanguage(text, "US"); final DetectedLanguage detectedDocumentLanguage = detectLanguageResult.getPrimaryLanguage(); - System.out.printf("Detected Primary Language: %s, ISO 6391 Name: %s, Score: %s.%n", + System.out.printf("Detected primary language: %s, ISO 6391 name: %s, score: %s.%n", detectedDocumentLanguage.getName(), detectedDocumentLanguage.getIso6391Name(), detectedDocumentLanguage.getScore()); final List detectedLanguages = detectLanguageResult.getDetectedLanguages(); for (DetectedLanguage detectedLanguage : detectedLanguages) { - System.out.printf("Other detected languages: %s, ISO 6391 Name: %s, Score: %s.%n", + System.out.printf("Other detected languages: %s, ISO 6391 name: %s, score: %s.%n", detectedLanguage.getName(), detectedLanguage.getIso6391Name(), detectedLanguage.getScore()); diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/HelloWorldAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/HelloWorldAsync.java index dab4ab8b0afc..84181b364d81 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/HelloWorldAsync.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/HelloWorldAsync.java @@ -19,8 +19,8 @@ public class HelloWorldAsync { public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("subscription-key") - .endpoint("https://servicename.cognitiveservices.azure.com/") + .subscriptionKey("") + .endpoint("") .buildAsyncClient(); // The text that need be analysed. @@ -29,7 +29,7 @@ public static void main(String[] args) { client.detectLanguage(text).subscribe( result -> { final DetectedLanguage primaryLanguage = result.getPrimaryLanguage(); - System.out.printf("Detected Language: %s, ISO 6391 Name: %s, Score: %s.%n", + System.out.printf("Detected language: %s, ISO 6391 name: %s, score: %s.%n", primaryLanguage.getName(), primaryLanguage.getIso6391Name(), primaryLanguage.getScore()); }, error -> System.err.println("There was an error detecting language of the text." + error), diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeEntities.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeEntities.java index 274bf1e90f46..f0d8e4c52e1a 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeEntities.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeEntities.java @@ -17,8 +17,8 @@ public class RecognizeEntities { public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("subscription-key") - .endpoint("https://servicename.cognitiveservices.azure.com/") + .subscriptionKey("") + .endpoint("") .buildClient(); // The text that need be analysed. @@ -26,7 +26,7 @@ public static void main(String[] args) { for (NamedEntity entity : client.recognizeEntities(text).getNamedEntities()) { System.out.printf( - "Recognized NamedEntity: %s, NamedEntity Type: %s, NamedEntity Subtype: %s, Offset: %s, Length: %s, Score: %s.%n", + "Recognized entity: %s, entity type: %s, entity subtype: %s, offset: %s, length: %s, score: %s.%n", entity.getText(), entity.getType(), entity.getSubtype() == null || entity.getSubtype().isEmpty() ? "N/A" : entity.getSubtype(), diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeEntitiesAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeEntitiesAsync.java new file mode 100644 index 000000000000..b52fa105b110 --- /dev/null +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeEntitiesAsync.java @@ -0,0 +1,53 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.ai.textanalytics; + +import com.azure.ai.textanalytics.models.NamedEntity; + +import java.util.concurrent.TimeUnit; + +/** + * Sample demonstrate how to recognize entities of a text input in asynchronously call. + */ +public class RecognizeEntitiesAsync { + /** + * Main method to invoke this demo about how to recognize entities of a text input. + * + * @param args Unused arguments to the program. + */ + public static void main(String[] args) { + // Instantiate a client that will be used to call the service. + TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() + .subscriptionKey("") + .endpoint("") + .buildAsyncClient(); + + // The text that need be analysed. + String text = "Satya Nadella is the CEO of Microsoft"; + + client.recognizeEntities(text).subscribe( + result -> { + for (NamedEntity entity : result.getNamedEntities()) { + System.out.printf( + "Recognized entity: %s, entity type: %s, entity subtype: %s, offset: %s, length: %s, score: %s.%n", + entity.getText(), + entity.getType(), + entity.getSubtype() == null || entity.getSubtype().isEmpty() ? "N/A" : entity.getSubtype(), + entity.getOffset(), + entity.getLength(), + entity.getScore()); + } + }, + error -> System.err.println("There was an error recognizing entities of the text." + error), + () -> System.out.println("Entities recognized.")); + + // The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep + // the thread so the program does not end before the send operation is complete. Using .block() instead of + // .subscribe() will turn this into a synchronous call. + try { + TimeUnit.SECONDS.sleep(5); + } catch (InterruptedException ignored) { + } + } +} diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeLinkedEntities.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeLinkedEntities.java index 20f9f166638f..a101cd833d97 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeLinkedEntities.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeLinkedEntities.java @@ -17,15 +17,15 @@ public class RecognizeLinkedEntities { public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("subscription-key") - .endpoint("https://servicename.cognitiveservices.azure.com/") + .subscriptionKey("") + .endpoint("") .buildClient(); // The text that need be analysed. String text = "Old Faithful is a geyser at Yellowstone Park."; for (LinkedEntity linkedEntity : client.recognizeLinkedEntities(text).getLinkedEntities()) { - System.out.printf("Recognized Linked NamedEntity: %s, URL: %s, Data Source: %s.%n", + System.out.printf("Recognized linked entity: %s, URL: %s, data source: %s.%n", linkedEntity.getName(), linkedEntity.getUrl(), linkedEntity.getDataSource()); diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeLinkedEntitiesAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeLinkedEntitiesAsync.java new file mode 100644 index 000000000000..2991a87fde31 --- /dev/null +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeLinkedEntitiesAsync.java @@ -0,0 +1,49 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.ai.textanalytics; + +import com.azure.ai.textanalytics.models.LinkedEntity; + +import java.util.concurrent.TimeUnit; + +/** + * Sample demonstrate how to recognize linked entities of a text input in asynchronously call. + */ +public class RecognizeLinkedEntitiesAsync { + /** + * Main method to invoke this demo about how to recognize linked entities of a text input. + * + * @param args Unused arguments to the program. + */ + public static void main(String[] args) { + // Instantiate a client that will be used to call the service. + TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() + .subscriptionKey("") + .endpoint("") + .buildAsyncClient(); + + // The text that need be analysed. + String text = "Old Faithful is a geyser at Yellowstone Park."; + + client.recognizeLinkedEntities(text).subscribe( + result -> { + for (LinkedEntity linkedEntity : result.getLinkedEntities()) { + System.out.printf("Recognized linked entity: %s, URL: %s, data source: %s.%n", + linkedEntity.getName(), + linkedEntity.getUrl(), + linkedEntity.getDataSource()); + } + }, + error -> System.err.println("There was an error recognizing linked entity of the text." + error), + () -> System.out.println("Linked entity recognized.")); + + // The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep + // the thread so the program does not end before the send operation is complete. Using .block() instead of + // .subscribe() will turn this into a synchronous call. + try { + TimeUnit.SECONDS.sleep(5); + } catch (InterruptedException ignored) { + } + } +} diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizePii.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizePii.java index 257da41ea72b..04c841845bee 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizePii.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizePii.java @@ -17,8 +17,8 @@ public class RecognizePii { public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("subscription-key") - .endpoint("https://servicename.cognitiveservices.azure.com/") + .subscriptionKey("") + .endpoint("") .buildClient(); // The text that need be analysed. @@ -26,7 +26,7 @@ public static void main(String[] args) { for (NamedEntity entity : client.recognizePiiEntities(text).getNamedEntities()) { System.out.printf( - "Recognized PII Entity: %s, Entity Type: %s, Entity Subtype: %s, Offset: %s, Length: %s, Score: %s.%n", + "Recognized personal identifiable information entity: %s, entity type: %s, entity subtype: %s, offset: %s, length: %s, score: %s.%n", entity.getText(), entity.getType() , entity.getSubtype() == null || entity.getSubtype().isEmpty() ? "N/A" : entity.getSubtype(), diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizePiiAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizePiiAsync.java new file mode 100644 index 000000000000..8600b26f8a14 --- /dev/null +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizePiiAsync.java @@ -0,0 +1,54 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.ai.textanalytics; + +import com.azure.ai.textanalytics.models.NamedEntity; + +import java.util.concurrent.TimeUnit; + +/** + * Sample demonstrate how to recognize PII(Personally Identifiable Information) entities of a text input in + * asynchronously call. + */ +public class RecognizePiiAsync { + /** + * Main method to invoke this demo about how to analyze sentiment of a text input. + * + * @param args Unused arguments to the program. + */ + public static void main(String[] args) { + // Instantiate a client that will be used to call the service. + TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() + .subscriptionKey("") + .endpoint("") + .buildAsyncClient(); + + // The text that need be analysed. + String text = "My SSN is 555-55-5555"; + + client.recognizePiiEntities(text).subscribe( + result -> { + for (NamedEntity entity : result.getNamedEntities()) { + System.out.printf( + "Recognized personal identifiable information entity: %s, entity type: %s, entity subtype: %s, offset: %s, length: %s, score: %s.%n", + entity.getText(), + entity.getType() , + entity.getSubtype() == null || entity.getSubtype().isEmpty() ? "N/A" : entity.getSubtype(), + entity.getOffset(), + entity.getLength(), + entity.getScore()); + } + }, + error -> System.err.println("There was an error recognizing PII entities of the text." + error), + () -> System.out.println("PII entities recognized.")); + + // The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep + // the thread so the program does not end before the send operation is complete. Using .block() instead of + // .subscribe() will turn this into a synchronous call. + try { + TimeUnit.SECONDS.sleep(5); + } catch (InterruptedException ignored) { + } + } +} diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocuments.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocuments.java index d6cba7bfbc77..085ff53cc8fe 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocuments.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocuments.java @@ -28,8 +28,8 @@ public class AnalyzeSentimentBatchDocuments { public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("subscription-key") - .endpoint("https://servicename.cognitiveservices.azure.com/") + .subscriptionKey("") + .endpoint("") .buildClient(); // The texts that need be analysed. @@ -38,21 +38,33 @@ public static void main(String[] args) { new TextDocumentInput("2", "The restaurant had amazing gnocchi. The hotel was dark and unclean.", "en") ); + // Request options: show statistics and model version final TextAnalyticsRequestOptions requestOptions = new TextAnalyticsRequestOptions().setShowStatistics(true); - DocumentResultCollection detectedBatchResult = client.analyzeBatchSentimentWithResponse(inputs, requestOptions, Context.NONE).getValue(); - System.out.printf("Model version: %s%n", detectedBatchResult.getModelVersion()); - final TextDocumentBatchStatistics batchStatistics = detectedBatchResult.getStatistics(); + // Analyzing batch sentiments + DocumentResultCollection analyzedBatchResult = client.analyzeBatchSentimentWithResponse( + inputs, requestOptions, Context.NONE).getValue(); + System.out.printf("Model version: %s%n", analyzedBatchResult.getModelVersion()); + + // Batch statistics + final TextDocumentBatchStatistics batchStatistics = analyzedBatchResult.getStatistics(); System.out.printf("A batch of document statistics, document count: %s, erroneous document count: %s, transaction count: %s, valid document count: %s.%n", batchStatistics.getDocumentCount(), batchStatistics.getErroneousDocumentCount(), batchStatistics.getTransactionCount(), batchStatistics.getValidDocumentCount()); - // Detecting sentiment for each of document from a batch of documents - for (AnalyzeSentimentResult analyzeSentimentResult : detectedBatchResult) { + // Analyzed sentiment for each of document from a batch of documents + for (AnalyzeSentimentResult analyzeSentimentResult : analyzedBatchResult) { + System.out.printf("Document ID: %s%n", analyzeSentimentResult.getId()); final TextSentiment documentSentiment = analyzeSentimentResult.getDocumentSentiment(); - System.out.printf("Recognized document sentiment: %s, Positive Score: %s, Neutral Score: %s, Negative Score: %s. Length of sentence: %s, Offset of sentence: %s.%n", + // Erroneous document + if (documentSentiment == null) { + System.out.printf("Cannot analyze sentiment. Error: %s%n", analyzeSentimentResult.getError().getMessage()); + continue; + } + // Valid document + System.out.printf("Recognized document sentiment: %s, positive score: %s, neutral score: %s, negative score: %s, length of sentence: %s, offset of sentence: %s.%n", documentSentiment.getTextSentimentClass(), documentSentiment.getPositiveScore(), documentSentiment.getNeutralScore(), @@ -60,7 +72,7 @@ public static void main(String[] args) { documentSentiment.getLength(), documentSentiment.getOffset()); for (TextSentiment sentenceSentiment : analyzeSentimentResult.getSentenceSentiments()) { - System.out.printf("Recognized sentence sentiment: %s, Positive Score: %s, Neutral Score: %s, Negative Score: %s. Length of sentence: %s, Offset of sentence: %s.%n", + System.out.printf("Recognized sentence sentiment: %s, positive score: %s, neutral score: %s, negative score: %s, length of sentence: %s, offset of sentence: %s.%n", sentenceSentiment.getTextSentimentClass(), sentenceSentiment.getPositiveScore(), sentenceSentiment.getNeutralScore(), diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocumentsAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocumentsAsync.java new file mode 100644 index 000000000000..5e1bead3f5ee --- /dev/null +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocumentsAsync.java @@ -0,0 +1,97 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.ai.textanalytics.batch; + +import com.azure.ai.textanalytics.TextAnalyticsAsyncClient; +import com.azure.ai.textanalytics.TextAnalyticsClientBuilder; +import com.azure.ai.textanalytics.models.AnalyzeSentimentResult; +import com.azure.ai.textanalytics.models.DocumentResultCollection; +import com.azure.ai.textanalytics.models.TextAnalyticsRequestOptions; +import com.azure.ai.textanalytics.models.TextDocumentBatchStatistics; +import com.azure.ai.textanalytics.models.TextDocumentInput; +import com.azure.ai.textanalytics.models.TextSentiment; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * Sample demonstrate how to analyze sentiment of a batch of text inputs in asynchronously call. + */ +public class AnalyzeSentimentBatchDocumentsAsync { + /** + * Main method to invoke this demo about how to analyze sentiment of a batch of text inputs. + * + * @param args Unused arguments to the program. + */ + public static void main(String[] args) { + // Instantiate a client that will be used to call the service. + TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() + .subscriptionKey("") + .endpoint("") + .buildAsyncClient(); + + // The texts that need be analysed. + List inputs = Arrays.asList( + new TextDocumentInput("1", "The hotel was dark and unclean. The restaurant had amazing gnocchi.", "en"), + new TextDocumentInput("2", "The restaurant had amazing gnocchi. The hotel was dark and unclean.", "en") + ); + + // Request options: show statistics and model version + final TextAnalyticsRequestOptions requestOptions = new TextAnalyticsRequestOptions().setShowStatistics(true); + + // Analyzing batch sentiments + client.analyzeBatchSentimentWithResponse(inputs, requestOptions).subscribe( + result -> { + DocumentResultCollection analyzedBatchResult = result.getValue(); + System.out.printf("Model version: %s%n", analyzedBatchResult.getModelVersion()); + + // Batch statistics + final TextDocumentBatchStatistics batchStatistics = analyzedBatchResult.getStatistics(); + System.out.printf("A batch of document statistics, document count: %s, erroneous document count: %s, transaction count: %s, valid document count: %s.%n", + batchStatistics.getDocumentCount(), + batchStatistics.getErroneousDocumentCount(), + batchStatistics.getTransactionCount(), + batchStatistics.getValidDocumentCount()); + + // Analyzed sentiment for each of document from a batch of documents + for (AnalyzeSentimentResult analyzeSentimentResult : analyzedBatchResult) { + System.out.printf("Document ID: %s%n", analyzeSentimentResult.getId()); + final TextSentiment documentSentiment = analyzeSentimentResult.getDocumentSentiment(); + // Erroneous document + if (documentSentiment == null) { + System.out.printf("Cannot analyze sentiment. Error: %s%n", analyzeSentimentResult.getError().getMessage()); + continue; + } + // Valid document + System.out.printf("Analyzed document sentiment: %s, positive score: %s, neutral score: %s, negative score: %s, length of sentence: %s, offset of sentence: %s.%n", + documentSentiment.getTextSentimentClass(), + documentSentiment.getPositiveScore(), + documentSentiment.getNeutralScore(), + documentSentiment.getNegativeScore(), + documentSentiment.getLength(), + documentSentiment.getOffset()); + for (TextSentiment sentenceSentiment : analyzeSentimentResult.getSentenceSentiments()) { + System.out.printf("Analyzed sentence sentiment: %s, positive score: %s, neutral score: %s, negative score: %s, length of sentence: %s, offset of sentence: %s.%n", + sentenceSentiment.getTextSentimentClass(), + sentenceSentiment.getPositiveScore(), + sentenceSentiment.getNeutralScore(), + sentenceSentiment.getNegativeScore(), + sentenceSentiment.getLength(), + sentenceSentiment.getOffset()); + } + } + }, + error -> System.err.println("There was an error analyzing sentiment of the text inputs." + error), + () -> System.out.println("Batch of sentiment analyzed.")); + + // The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep + // the thread so the program does not end before the send operation is complete. Using .block() instead of + // .subscribe() will turn this into a synchronous call. + try { + TimeUnit.SECONDS.sleep(5); + } catch (InterruptedException ignored) { + } + } +} diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocuments.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocuments.java index fbf76134e5f7..bce1b947dc99 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocuments.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocuments.java @@ -28,20 +28,24 @@ public class DetectLanguageBatchDocuments { public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("subscription-key") - .endpoint("https://servicename.cognitiveservices.azure.com/") + .subscriptionKey("") + .endpoint("") .buildClient(); // The texts that need be analysed. List inputs = Arrays.asList( - new DetectLanguageInput("1", "This is written in English.", "en"), + new DetectLanguageInput("1", "This is written in English.", "us"), new DetectLanguageInput("2", "Este es un document escrito en Español.", "es") ); + // Request options: show statistics and model version final TextAnalyticsRequestOptions requestOptions = new TextAnalyticsRequestOptions().setShowStatistics(true); + + // Detecting batch languages final DocumentResultCollection detectedBatchResult = client.detectBatchLanguagesWithResponse(inputs, requestOptions, Context.NONE).getValue(); System.out.printf("Model version: %s%n", detectedBatchResult.getModelVersion()); + // Batch statistics final TextDocumentBatchStatistics batchStatistics = detectedBatchResult.getStatistics(); System.out.printf("Batch statistics, document count: %s, erroneous document count: %s, transaction count: %s, valid document count: %s.%n", batchStatistics.getDocumentCount(), @@ -49,16 +53,22 @@ public static void main(String[] args) { batchStatistics.getTransactionCount(), batchStatistics.getValidDocumentCount()); - // Detecting languages for a document from a batch of documents + // Detected languages for a document from a batch of documents for (DetectLanguageResult detectLanguageResult : detectedBatchResult) { + System.out.printf("Document ID: %s%n", detectLanguageResult.getId()); final DetectedLanguage detectedPrimaryLanguage = detectLanguageResult.getPrimaryLanguage(); - System.out.printf("Detected primary Language for Document: %s, %s, ISO 6391 Name: %s, Score: %s.%n", - detectLanguageResult.getId(), + // Erroneous document + if (detectedPrimaryLanguage == null) { + System.out.printf("Cannot detect language. Error: %s%n", detectLanguageResult.getError().getMessage()); + continue; + } + // Valid document + System.out.printf("Detected primary language: %s, ISO 6391 name: %s, score: %s.%n", detectedPrimaryLanguage.getName(), detectedPrimaryLanguage.getIso6391Name(), detectedPrimaryLanguage.getScore()); for (DetectedLanguage detectedLanguage : detectLanguageResult.getDetectedLanguages()) { - System.out.printf("Other detected Languages: %s, ISO 6391 Name: %s, Score: %s.%n", + System.out.printf("Another detected language: %s, ISO 6391 name: %s, score: %s.%n", detectedLanguage.getName(), detectedLanguage.getIso6391Name(), detectedLanguage.getScore()); diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocumentsAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocumentsAsync.java new file mode 100644 index 000000000000..35f654c3d082 --- /dev/null +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocumentsAsync.java @@ -0,0 +1,93 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.ai.textanalytics.batch; + +import com.azure.ai.textanalytics.TextAnalyticsAsyncClient; +import com.azure.ai.textanalytics.TextAnalyticsClientBuilder; +import com.azure.ai.textanalytics.models.DetectLanguageInput; +import com.azure.ai.textanalytics.models.DetectLanguageResult; +import com.azure.ai.textanalytics.models.DetectedLanguage; +import com.azure.ai.textanalytics.models.DocumentResultCollection; +import com.azure.ai.textanalytics.models.TextAnalyticsError; +import com.azure.ai.textanalytics.models.TextAnalyticsRequestOptions; +import com.azure.ai.textanalytics.models.TextDocumentBatchStatistics; +import com.azure.core.util.Context; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * Sample demonstrate how to detect language of a batch of text inputs in asynchronously call. + */ +public class DetectLanguageBatchDocumentsAsync { + /** + * Main method to invoke this demo about how to detect language of a batch of text inputs. + * + * @param args Unused arguments to the program. + */ + public static void main(String[] args) { + // Instantiate a client that will be used to call the service. + TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() + .subscriptionKey("") + .endpoint("") + .buildAsyncClient(); + + // The texts that need be analysed. + List inputs = Arrays.asList( + new DetectLanguageInput("1", "This is written in English.", "us"), + new DetectLanguageInput("2", "Este es un document escrito en Español.", "es") + ); + + // Request options: show statistics and model version + final TextAnalyticsRequestOptions requestOptions = new TextAnalyticsRequestOptions().setShowStatistics(true); + + // Detecting batch languages + client.detectBatchLanguagesWithResponse(inputs, requestOptions).subscribe( + result -> { + final DocumentResultCollection detectedBatchResult = result.getValue(); + System.out.printf("Model version: %s%n", detectedBatchResult.getModelVersion()); + + // Batch statistics + final TextDocumentBatchStatistics batchStatistics = detectedBatchResult.getStatistics(); + System.out.printf("Batch statistics, document count: %s, erroneous document count: %s, transaction count: %s, valid document count: %s.%n", + batchStatistics.getDocumentCount(), + batchStatistics.getErroneousDocumentCount(), + batchStatistics.getTransactionCount(), + batchStatistics.getValidDocumentCount()); + + // Detected languages for a document from a batch of documents + for (DetectLanguageResult detectLanguageResult : detectedBatchResult) { + System.out.printf("Document ID: %s%n", detectLanguageResult.getId()); + final DetectedLanguage detectedPrimaryLanguage = detectLanguageResult.getPrimaryLanguage(); + // Erroneous document + if (detectedPrimaryLanguage == null) { + System.out.printf("Cannot detect language. Error: %s%n", detectLanguageResult.getError().getMessage()); + continue; + } + // Valid document + System.out.printf("Detected primary language: %s, ISO 6391 name: %s, score: %s.%n", + detectedPrimaryLanguage.getName(), + detectedPrimaryLanguage.getIso6391Name(), + detectedPrimaryLanguage.getScore()); + for (DetectedLanguage detectedLanguage : detectLanguageResult.getDetectedLanguages()) { + System.out.printf("Another detected language: %s, ISO 6391 name: %s, score: %s.%n", + detectedLanguage.getName(), + detectedLanguage.getIso6391Name(), + detectedLanguage.getScore()); + } + } + }, + error -> System.err.println("There was an error detecting language of the text inputs." + error), + () -> System.out.println("Batch of language detected.")); + + // The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep + // the thread so the program does not end before the send operation is complete. Using .block() instead of + // .subscribe() will turn this into a synchronous call. + try { + TimeUnit.SECONDS.sleep(5); + } catch (InterruptedException ignored) { + } + } +} diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocuments.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocuments.java index 4ba8ed1233da..7f0ae8403dc1 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocuments.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocuments.java @@ -27,8 +27,8 @@ public class ExtractKeyPhrasesBatchDocuments { public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("subscription-key") - .endpoint("https://servicename.cognitiveservices.azure.com/") + .subscriptionKey("") + .endpoint("") .buildClient(); // The texts that need be analysed. @@ -37,21 +37,33 @@ public static void main(String[] args) { new TextDocumentInput("2", "The pitot tube is used to measure airspeed.", "en") ); + // Request options: show statistics and model version final TextAnalyticsRequestOptions requestOptions = new TextAnalyticsRequestOptions().setShowStatistics(true); - final DocumentResultCollection detectedBatchResult = client.extractBatchKeyPhrasesWithResponse(inputs, requestOptions, Context.NONE).getValue(); - System.out.printf("Model version: %s%n", detectedBatchResult.getModelVersion()); - final TextDocumentBatchStatistics batchStatistics = detectedBatchResult.getStatistics(); + // Extracting batch key phrases + final DocumentResultCollection extractedBatchResult = client.extractBatchKeyPhrasesWithResponse(inputs, requestOptions, Context.NONE).getValue(); + System.out.printf("Model version: %s%n", extractedBatchResult.getModelVersion()); + + // Batch statistics + final TextDocumentBatchStatistics batchStatistics = extractedBatchResult.getStatistics(); System.out.printf("A batch of document statistics, document count: %s, erroneous document count: %s, transaction count: %s, valid document count: %s.%n", batchStatistics.getDocumentCount(), batchStatistics.getErroneousDocumentCount(), batchStatistics.getTransactionCount(), batchStatistics.getValidDocumentCount()); - // Detecting key phrase for each of document from a batch of documents - for (ExtractKeyPhraseResult extractKeyPhraseResult : detectedBatchResult) { - for (String keyPhrases : extractKeyPhraseResult.getKeyPhrases()) { - System.out.printf("Recognized Phrases: %s.%n", keyPhrases); + // Extracted key phrase for each of document from a batch of documents + for (ExtractKeyPhraseResult extractKeyPhraseResult : extractedBatchResult) { + System.out.printf("Document ID: %s%n", extractKeyPhraseResult.getId()); + final List documentKeyPhrases = extractKeyPhraseResult.getKeyPhrases(); + // Erroneous document + if (documentKeyPhrases == null) { + System.out.printf("Cannot extract key phrases. Error: %s%n", extractKeyPhraseResult.getError().getMessage()); + continue; + } + // Valid document + for (String keyPhrases : documentKeyPhrases) { + System.out.printf("Extracted phrases: %s.%n", keyPhrases); } } } diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocumentsAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocumentsAsync.java new file mode 100644 index 000000000000..5b704a0204a9 --- /dev/null +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocumentsAsync.java @@ -0,0 +1,83 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.ai.textanalytics.batch; + +import com.azure.ai.textanalytics.TextAnalyticsAsyncClient; +import com.azure.ai.textanalytics.TextAnalyticsClientBuilder; +import com.azure.ai.textanalytics.models.DocumentResultCollection; +import com.azure.ai.textanalytics.models.ExtractKeyPhraseResult; +import com.azure.ai.textanalytics.models.TextAnalyticsRequestOptions; +import com.azure.ai.textanalytics.models.TextDocumentBatchStatistics; +import com.azure.ai.textanalytics.models.TextDocumentInput; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * Sample demonstrate how to analyze key phrases of a batch of text inputs in asynchronously call. + */ +public class ExtractKeyPhrasesBatchDocumentsAsync { + /** + * Main method to invoke this demo about how to extract key phrases of a batch of text inputs. + * + * @param args Unused arguments to the program. + */ + public static void main(String[] args) { + // Instantiate a client that will be used to call the service. + TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() + .subscriptionKey("") + .endpoint("") + .buildAsyncClient(); + + // The texts that need be analysed. + List inputs = Arrays.asList( + new TextDocumentInput("1", "My cat might need to see a veterinarian.", "en"), + new TextDocumentInput("2", "The pitot tube is used to measure airspeed.", "en") + ); + + // Request options: show statistics and model version + final TextAnalyticsRequestOptions requestOptions = new TextAnalyticsRequestOptions().setShowStatistics(true); + + // Extracting batch key phrases + client.extractBatchKeyPhrasesWithResponse(inputs, requestOptions).subscribe( + result -> { + final DocumentResultCollection extractedBatchResult = result.getValue(); + System.out.printf("Model version: %s%n", extractedBatchResult.getModelVersion()); + + // Batch statistics + final TextDocumentBatchStatistics batchStatistics = extractedBatchResult.getStatistics(); + System.out.printf("A batch of document statistics, document count: %s, erroneous document count: %s, transaction count: %s, valid document count: %s.%n", + batchStatistics.getDocumentCount(), + batchStatistics.getErroneousDocumentCount(), + batchStatistics.getTransactionCount(), + batchStatistics.getValidDocumentCount()); + + // Extracted key phrase for each of document from a batch of documents + for (ExtractKeyPhraseResult extractKeyPhraseResult : extractedBatchResult) { + System.out.printf("Document ID: %s%n", extractKeyPhraseResult.getId()); + final List documentKeyPhrases = extractKeyPhraseResult.getKeyPhrases(); + // Erroneous document + if (documentKeyPhrases == null) { + System.out.printf("Cannot extract key phrases. Error: %s%n", extractKeyPhraseResult.getError().getMessage()); + continue; + } + // Valid document + for (String keyPhrases : extractKeyPhraseResult.getKeyPhrases()) { + System.out.printf("Extracted phrases: %s.%n", keyPhrases); + } + } + }, + error -> System.err.println("There was an error extracting key phrases of the text inputs." + error), + () -> System.out.println("Batch of key phrases extracted.")); + + // The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep + // the thread so the program does not end before the send operation is complete. Using .block() instead of + // .subscribe() will turn this into a synchronous call. + try { + TimeUnit.SECONDS.sleep(5); + } catch (InterruptedException ignored) { + } + } +} diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocuments.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocuments.java index f9540371aa8b..031712e16a84 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocuments.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocuments.java @@ -28,8 +28,8 @@ public class RecognizeEntitiesBatchDocuments { public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("subscription-key") - .endpoint("https://servicename.cognitiveservices.azure.com/") + .subscriptionKey("") + .endpoint("") .buildClient(); // The texts that need be analysed. @@ -38,24 +38,37 @@ public static void main(String[] args) { new TextDocumentInput("2", "Elon Musk is the CEO of SpaceX and Tesla.", "en") ); + // Request options: show statistics and model version final TextAnalyticsRequestOptions requestOptions = new TextAnalyticsRequestOptions().setShowStatistics(true); - final DocumentResultCollection detectedBatchResult = + + // Recognizing batch entities + final DocumentResultCollection recognizedBatchResult = client.recognizeBatchEntitiesWithResponse(inputs, requestOptions, Context.NONE).getValue(); - System.out.printf("Model version: %s%n", detectedBatchResult.getModelVersion()); + System.out.printf("Model version: %s%n", recognizedBatchResult.getModelVersion()); - final TextDocumentBatchStatistics batchStatistics = detectedBatchResult.getStatistics(); + // Batch statistics + final TextDocumentBatchStatistics batchStatistics = recognizedBatchResult.getStatistics(); System.out.printf("A batch of document statistics, document count: %s, erroneous document count: %s, transaction count: %s, valid document count: %s.%n", batchStatistics.getDocumentCount(), batchStatistics.getErroneousDocumentCount(), batchStatistics.getTransactionCount(), batchStatistics.getValidDocumentCount()); - for (RecognizeEntitiesResult recognizeEntitiesResult : detectedBatchResult) { - for (NamedEntity entity : recognizeEntitiesResult.getNamedEntities()) { - System.out.printf("Recognized NamedEntity: %s, NamedEntity Type: %s, NamedEntity Subtype: %s, Offset: %s, Length: %s, Score: %s.%n", + // Recognized entities for each of document from a batch of documents + for (RecognizeEntitiesResult recognizeEntitiesResult : recognizedBatchResult) { + System.out.printf("Document ID: %s%n", recognizeEntitiesResult.getId()); + final List namedEntities = recognizeEntitiesResult.getNamedEntities(); + // Erroneous document + if (namedEntities == null) { + System.out.printf("Cannot recognize entities. Error: %s%n", recognizeEntitiesResult.getError().getMessage()); + continue; + } + // Valid document + for (NamedEntity entity : namedEntities) { + System.out.printf("Recognized entity: %s, entity type: %s, entity subtype: %s, offset: %s, length: %s, score: %s.%n", entity.getText(), entity.getType(), - entity.getSubtype(), + entity.getSubtype() == null || entity.getSubtype().isEmpty() ? "N/A" : entity.getSubtype(), entity.getOffset(), entity.getLength(), entity.getScore()); diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocumentsAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocumentsAsync.java new file mode 100644 index 000000000000..ec6ff5ae055f --- /dev/null +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocumentsAsync.java @@ -0,0 +1,90 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.ai.textanalytics.batch; + +import com.azure.ai.textanalytics.TextAnalyticsAsyncClient; +import com.azure.ai.textanalytics.TextAnalyticsClientBuilder; +import com.azure.ai.textanalytics.models.DocumentResultCollection; +import com.azure.ai.textanalytics.models.NamedEntity; +import com.azure.ai.textanalytics.models.RecognizeEntitiesResult; +import com.azure.ai.textanalytics.models.TextAnalyticsRequestOptions; +import com.azure.ai.textanalytics.models.TextDocumentBatchStatistics; +import com.azure.ai.textanalytics.models.TextDocumentInput; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * Sample demonstrate how to recognize entities of a batch of text inputs in asynchronously call. + */ +public class RecognizeEntitiesBatchDocumentsAsync { + /** + * Main method to invoke this demo about how to recognize entities of a batch of text inputs. + * + * @param args Unused arguments to the program. + */ + public static void main(String[] args) { + // Instantiate a client that will be used to call the service. + TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() + .subscriptionKey("") + .endpoint("") + .buildAsyncClient(); + + // The texts that need be analysed. + List inputs = Arrays.asList( + new TextDocumentInput("1", "Satya Nadella is the CEO of Microsoft.", "en"), + new TextDocumentInput("2", "Elon Musk is the CEO of SpaceX and Tesla.", "en") + ); + + // Request options: show statistics and model version + final TextAnalyticsRequestOptions requestOptions = new TextAnalyticsRequestOptions().setShowStatistics(true); + + // Recognizing batch entities + client.recognizeBatchEntitiesWithResponse(inputs, requestOptions).subscribe( + result -> { + final DocumentResultCollection recognizedBatchResult = result.getValue(); + System.out.printf("Model version: %s%n", recognizedBatchResult.getModelVersion()); + + // Batch statistics + final TextDocumentBatchStatistics batchStatistics = recognizedBatchResult.getStatistics(); + System.out.printf("A batch of document statistics, document count: %s, erroneous document count: %s, transaction count: %s, valid document count: %s.%n", + batchStatistics.getDocumentCount(), + batchStatistics.getErroneousDocumentCount(), + batchStatistics.getTransactionCount(), + batchStatistics.getValidDocumentCount()); + + // Recognized entities for each of document from a batch of documents + for (RecognizeEntitiesResult recognizeEntitiesResult : recognizedBatchResult) { + System.out.printf("Document ID: %s%n", recognizeEntitiesResult.getId()); + final List namedEntities = recognizeEntitiesResult.getNamedEntities(); + // Erroneous document + if (namedEntities == null) { + System.out.printf("Cannot recognize entities. Error: %s%n", recognizeEntitiesResult.getError().getMessage()); + continue; + } + // Valid document + for (NamedEntity entity : namedEntities) { + System.out.printf("Recognized entity: %s, entity type: %s, entity subtype: %s, offset: %s, length: %s, score: %s.%n", + entity.getText(), + entity.getType(), + entity.getSubtype() == null || entity.getSubtype().isEmpty() ? "N/A" : entity.getSubtype(), + entity.getOffset(), + entity.getLength(), + entity.getScore()); + } + } + }, + error -> System.err.println("There was an error recognizing entities of the text inputs." + error), + () -> System.out.println("Batch of entities recognized.")); + + // The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep + // the thread so the program does not end before the send operation is complete. Using .block() instead of + // .subscribe() will turn this into a synchronous call. + try { + TimeUnit.SECONDS.sleep(5); + } catch (InterruptedException ignored) { + } + } +} diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocuments.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocuments.java index b072bc018669..5e41c5db234c 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocuments.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocuments.java @@ -28,8 +28,8 @@ public class RecognizeLinkedEntitiesBatchDocuments { public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("subscription-key") - .endpoint("https://servicename.cognitiveservices.azure.com/") + .subscriptionKey("") + .endpoint("") .buildClient(); // The texts that need be analysed. @@ -38,22 +38,36 @@ public static void main(String[] args) { new TextDocumentInput("2", "Mount Shasta has lenticular clouds.", "en") ); + // Request options: show statistics and model version final TextAnalyticsRequestOptions requestOptions = new TextAnalyticsRequestOptions().setShowStatistics(true); - final DocumentResultCollection detectedBatchResult = client.recognizeBatchLinkedEntitiesWithResponse(inputs, requestOptions, Context.NONE).getValue(); - System.out.printf("Model version: %s%n", detectedBatchResult.getModelVersion()); - final TextDocumentBatchStatistics batchStatistics = detectedBatchResult.getStatistics(); + // Recognizing batch entities + final DocumentResultCollection recognizedBatchResult = client.recognizeBatchLinkedEntitiesWithResponse(inputs, requestOptions, Context.NONE).getValue(); + System.out.printf("Model version: %s%n", recognizedBatchResult.getModelVersion()); + + // Batch statistics + final TextDocumentBatchStatistics batchStatistics = recognizedBatchResult.getStatistics(); System.out.printf("A batch of document statistics, document count: %s, erroneous document count: %s, transaction count: %s, valid document count: %s.%n", batchStatistics.getDocumentCount(), batchStatistics.getErroneousDocumentCount(), batchStatistics.getTransactionCount(), batchStatistics.getValidDocumentCount()); - // Detecting linked entities from a batch of documents - for (RecognizeLinkedEntitiesResult linkedEntityDocumentResult : detectedBatchResult) { - for (LinkedEntity linkedEntity : linkedEntityDocumentResult.getLinkedEntities()) { - System.out.printf("Recognized Linked NamedEntity: %s, URL: %s, Data Source: %s%n", - linkedEntity.getName(), linkedEntity.getUrl(), linkedEntity.getDataSource()); + // Recognized linked entities from a batch of documents + for (RecognizeLinkedEntitiesResult linkedEntityDocumentResult : recognizedBatchResult) { + System.out.printf("Document ID: %s%n", linkedEntityDocumentResult.getId()); + final List linkedEntities = linkedEntityDocumentResult.getLinkedEntities(); + // Erroneous document + if (linkedEntities == null) { + System.out.printf("Cannot recognize linked entities. Error: %s%n", linkedEntityDocumentResult.getError().getMessage()); + continue; + } + // Valid document + for (LinkedEntity linkedEntity : linkedEntities) { + System.out.printf("Recognized linked entity: %s, URL: %s, data source: %s%n", + linkedEntity.getName(), + linkedEntity.getUrl(), + linkedEntity.getDataSource()); } } } diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocumentsAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocumentsAsync.java new file mode 100644 index 000000000000..22a5420acba5 --- /dev/null +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocumentsAsync.java @@ -0,0 +1,87 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.ai.textanalytics.batch; + +import com.azure.ai.textanalytics.TextAnalyticsAsyncClient; +import com.azure.ai.textanalytics.TextAnalyticsClientBuilder; +import com.azure.ai.textanalytics.models.DocumentResultCollection; +import com.azure.ai.textanalytics.models.LinkedEntity; +import com.azure.ai.textanalytics.models.RecognizeLinkedEntitiesResult; +import com.azure.ai.textanalytics.models.TextAnalyticsRequestOptions; +import com.azure.ai.textanalytics.models.TextDocumentBatchStatistics; +import com.azure.ai.textanalytics.models.TextDocumentInput; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * Sample demonstrate how to recognize linked entities of a batch of text inputs in asynchronously call. + */ +public class RecognizeLinkedEntitiesBatchDocumentsAsync { + /** + * Main method to invoke this demo about how to recognize linked entities of a batch of text inputs. + * + * @param args Unused arguments to the program. + */ + public static void main(String[] args) { + // Instantiate a client that will be used to call the service. + TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() + .subscriptionKey("") + .endpoint("") + .buildAsyncClient(); + + // The texts that need be analysed. + List inputs = Arrays.asList( + new TextDocumentInput("1", "Old Faithful is a geyser at Yellowstone Park.", "en"), + new TextDocumentInput("2", "Mount Shasta has lenticular clouds.", "en") + ); + + // Request options: show statistics and model version + final TextAnalyticsRequestOptions requestOptions = new TextAnalyticsRequestOptions().setShowStatistics(true); + + // Recognizing batch entities + client.recognizeBatchLinkedEntitiesWithResponse(inputs, requestOptions).subscribe( + result -> { + final DocumentResultCollection recognizedBatchResult = result.getValue(); + System.out.printf("Model version: %s%n", recognizedBatchResult.getModelVersion()); + + // Batch statistics + final TextDocumentBatchStatistics batchStatistics = recognizedBatchResult.getStatistics(); + System.out.printf("A batch of document statistics, document count: %s, erroneous document count: %s, transaction count: %s, valid document count: %s.%n", + batchStatistics.getDocumentCount(), + batchStatistics.getErroneousDocumentCount(), + batchStatistics.getTransactionCount(), + batchStatistics.getValidDocumentCount()); + + // Recognized linked entities from a batch of documents + for (RecognizeLinkedEntitiesResult linkedEntityDocumentResult : recognizedBatchResult) { + System.out.printf("Document ID: %s%n", linkedEntityDocumentResult.getId()); + final List linkedEntities = linkedEntityDocumentResult.getLinkedEntities(); + // Erroneous document + if (linkedEntities == null) { + System.out.printf("Cannot recognize linked entities. Error: %s%n", linkedEntityDocumentResult.getError().getMessage()); + continue; + } + // Valid document + for (LinkedEntity linkedEntity : linkedEntities) { + System.out.printf("Recognized linked entities: %s, URL: %s, data source: %s%n", + linkedEntity.getName(), + linkedEntity.getUrl(), + linkedEntity.getDataSource()); + } + } + }, + error -> System.err.println("There was an error recognizing linked entities of the text inputs." + error), + () -> System.out.println("Batch of linked entities recognized.")); + + // The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep + // the thread so the program does not end before the send operation is complete. Using .block() instead of + // .subscribe() will turn this into a synchronous call. + try { + TimeUnit.SECONDS.sleep(5); + } catch (InterruptedException ignored) { + } + } +} diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocuments.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocuments.java index 4986f308012e..3e0877d43022 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocuments.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocuments.java @@ -6,6 +6,7 @@ import com.azure.ai.textanalytics.TextAnalyticsClient; import com.azure.ai.textanalytics.TextAnalyticsClientBuilder; import com.azure.ai.textanalytics.models.DocumentResultCollection; +import com.azure.ai.textanalytics.models.NamedEntity; import com.azure.ai.textanalytics.models.RecognizePiiEntitiesResult; import com.azure.ai.textanalytics.models.TextAnalyticsRequestOptions; import com.azure.ai.textanalytics.models.TextDocumentBatchStatistics; @@ -27,8 +28,8 @@ public class RecognizePiiBatchDocuments { public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("subscription-key") - .endpoint("https://servicename.cognitiveservices.azure.com/") + .subscriptionKey("") + .endpoint("") .buildClient(); // The texts that need be analysed. @@ -37,26 +38,41 @@ public static void main(String[] args) { new TextDocumentInput("2", "Visa card 4111 1111 1111 1111", "en") ); + // Request options: show statistics and model version final TextAnalyticsRequestOptions requestOptions = new TextAnalyticsRequestOptions().setShowStatistics(true); - final DocumentResultCollection detectedBatchResult = client.recognizeBatchPiiEntitiesWithResponse(inputs, requestOptions, Context.NONE).getValue(); - System.out.printf("Model version: %s%n", detectedBatchResult.getModelVersion()); - final TextDocumentBatchStatistics batchStatistics = detectedBatchResult.getStatistics(); + // Recognizing batch entities + final DocumentResultCollection recognizedBatchResult = + client.recognizeBatchPiiEntitiesWithResponse(inputs, requestOptions, Context.NONE).getValue(); + System.out.printf("Model version: %s%n", recognizedBatchResult.getModelVersion()); + + // Batch statistics + final TextDocumentBatchStatistics batchStatistics = recognizedBatchResult.getStatistics(); System.out.printf("A batch of document statistics, document count: %s, erroneous document count: %s, transaction count: %s, valid document count: %s.%n", batchStatistics.getDocumentCount(), batchStatistics.getErroneousDocumentCount(), batchStatistics.getTransactionCount(), batchStatistics.getValidDocumentCount()); - // Detecting pii entities from a batch of documents - detectedBatchResult.forEach(piiEntityDocumentResult -> - piiEntityDocumentResult.getNamedEntities().forEach(entity -> - System.out.printf("Recognized Personal Identifiable Info NamedEntity: %s, NamedEntity Type: %s, NamedEntity Subtype: %s, Offset: %s, Length: %s, Score: %s.%n", + // Recognized PII entities for each of document from a batch of documents + for (RecognizePiiEntitiesResult piiEntityDocumentResult : recognizedBatchResult) { + System.out.printf("Document ID: %s%n", piiEntityDocumentResult.getId()); + final List namedEntities = piiEntityDocumentResult.getNamedEntities(); + // Erroneous document + if (namedEntities == null) { + System.out.printf("Cannot recognize PII entities. Error: %s%n", piiEntityDocumentResult.getError().getMessage()); + continue; + } + // Valid document + for (NamedEntity entity : namedEntities) { + System.out.printf("Recognized personal identifiable information entity: %s, entity type: %s, entity subtype: %s, offset: %s, length: %s, score: %s.%n", entity.getText(), entity.getType(), - entity.getSubtype(), + entity.getSubtype() == null || entity.getSubtype().isEmpty() ? "N/A" : entity.getSubtype(), entity.getOffset(), entity.getLength(), - entity.getScore()))); + entity.getScore()); + } + } } } diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocumentsAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocumentsAsync.java new file mode 100644 index 000000000000..1dee0b2ff61e --- /dev/null +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocumentsAsync.java @@ -0,0 +1,91 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.ai.textanalytics.batch; + +import com.azure.ai.textanalytics.TextAnalyticsAsyncClient; +import com.azure.ai.textanalytics.TextAnalyticsClientBuilder; +import com.azure.ai.textanalytics.models.DocumentResultCollection; +import com.azure.ai.textanalytics.models.NamedEntity; +import com.azure.ai.textanalytics.models.RecognizePiiEntitiesResult; +import com.azure.ai.textanalytics.models.TextAnalyticsRequestOptions; +import com.azure.ai.textanalytics.models.TextDocumentBatchStatistics; +import com.azure.ai.textanalytics.models.TextDocumentInput; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * Sample demonstrate how to recognize PII(Personally Identifiable Information) entities of a text input in + * asynchronously call. + */ +public class RecognizePiiBatchDocumentsAsync { + /** + * Main method to invoke this demo about how to recognize PII entities of a batch of text inputs. + * + * @param args Unused arguments to the program. + */ + public static void main(String[] args) { + // Instantiate a client that will be used to call the service. + TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() + .subscriptionKey("") + .endpoint("") + .buildAsyncClient(); + + // The texts that need be analysed. + List inputs = Arrays.asList( + new TextDocumentInput("1", "My SSN is 555-55-5555", "en"), + new TextDocumentInput("2", "Visa card 4111 1111 1111 1111", "en") + ); + + // Request options: show statistics and model version + final TextAnalyticsRequestOptions requestOptions = new TextAnalyticsRequestOptions().setShowStatistics(true); + + // Recognizing batch entities + client.recognizeBatchPiiEntitiesWithResponse(inputs, requestOptions).subscribe( + result -> { + final DocumentResultCollection recognizedBatchResult = result.getValue(); + System.out.printf("Model version: %s%n", recognizedBatchResult.getModelVersion()); + + // Batch statistics + final TextDocumentBatchStatistics batchStatistics = recognizedBatchResult.getStatistics(); + System.out.printf("A batch of document statistics, document count: %s, erroneous document count: %s, transaction count: %s, valid document count: %s.%n", + batchStatistics.getDocumentCount(), + batchStatistics.getErroneousDocumentCount(), + batchStatistics.getTransactionCount(), + batchStatistics.getValidDocumentCount()); + + // Recognized PII entities for each of document from a batch of documents + for (RecognizePiiEntitiesResult piiEntityDocumentResult : recognizedBatchResult) { + System.out.printf("Document ID: %s%n", piiEntityDocumentResult.getId()); + final List namedEntities = piiEntityDocumentResult.getNamedEntities(); + // Erroneous document + if (namedEntities == null) { + System.out.printf("Cannot recognize PII entities. Error: %s%n", piiEntityDocumentResult.getError().getMessage()); + continue; + } + // Valid document + for (NamedEntity entity : namedEntities) { + System.out.printf("Recognized personal identifiable information entity: %s, entity type: %s, entity subtype: %s, offset: %s, length: %s, score: %s.%n", + entity.getText(), + entity.getType(), + entity.getSubtype() == null || entity.getSubtype().isEmpty() ? "N/A" : entity.getSubtype(), + entity.getOffset(), + entity.getLength(), + entity.getScore()); + } + } + }, + error -> System.err.println("There was an error recognizing PII entities of the text inputs." + error), + () -> System.out.println("Batch of PII entities recognized.")); + + // The .subscribe() creation and assignment is not a blocking call. For the purpose of this example, we sleep + // the thread so the program does not end before the send operation is complete. Using .block() instead of + // .subscribe() will turn this into a synchronous call. + try { + TimeUnit.SECONDS.sleep(5); + } catch (InterruptedException ignored) { + } + } +} From ef6ea96962a02dec3445771ee53669304f4fc026 Mon Sep 17 00:00:00 2001 From: shafang Date: Thu, 2 Jan 2020 21:05:07 -0800 Subject: [PATCH 2/3] revised samples --- .../ai/textanalytics/AnalyzeSentiment.java | 11 +++++----- .../textanalytics/AnalyzeSentimentAsync.java | 13 ++++++------ .../ai/textanalytics/ExtractKeyPhrases.java | 8 +++---- .../textanalytics/ExtractKeyPhrasesAsync.java | 11 ++++------ .../azure/ai/textanalytics/HelloWorld.java | 21 +++++++++---------- .../ai/textanalytics/HelloWorldAsync.java | 16 +++++++++----- .../ai/textanalytics/RecognizeEntities.java | 8 +++---- .../textanalytics/RecognizeEntitiesAsync.java | 8 +++---- .../RecognizeLinkedEntities.java | 8 +++---- .../RecognizeLinkedEntitiesAsync.java | 8 +++---- .../azure/ai/textanalytics/RecognizePii.java | 8 +++---- .../ai/textanalytics/RecognizePiiAsync.java | 10 ++++----- .../batch/AnalyzeSentimentBatchDocuments.java | 12 +++++------ .../AnalyzeSentimentBatchDocumentsAsync.java | 12 +++++------ .../batch/DetectLanguageBatchDocuments.java | 15 ++++++------- .../DetectLanguageBatchDocumentsAsync.java | 14 ++++++------- .../ExtractKeyPhrasesBatchDocuments.java | 13 ++++++------ .../ExtractKeyPhrasesBatchDocumentsAsync.java | 11 +++++----- .../RecognizeEntitiesBatchDocuments.java | 13 ++++++------ .../RecognizeEntitiesBatchDocumentsAsync.java | 13 ++++++------ ...RecognizeLinkedEntitiesBatchDocuments.java | 13 ++++++------ ...nizeLinkedEntitiesBatchDocumentsAsync.java | 13 ++++++------ .../batch/RecognizePiiBatchDocuments.java | 13 ++++++------ .../RecognizePiiBatchDocumentsAsync.java | 15 +++++++------ 24 files changed, 139 insertions(+), 148 deletions(-) diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentiment.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentiment.java index c7405a997449..24110cb9e533 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentiment.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentiment.java @@ -9,19 +9,19 @@ import java.util.List; /** - * Sample demonstrate how to analyze sentiment of a text input. + * Sample demonstrate how to analyze the sentiment of an input text. */ public class AnalyzeSentiment { /** - * Main method to invoke this demo about how to analyze sentiment of a text input. + * Main method to invoke this demo about how to analyze the sentiment of an input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildClient(); // The text that need be analysed. @@ -37,8 +37,7 @@ public static void main(String[] args) { documentSentiment.getNeutralScore(), documentSentiment.getNegativeScore()); - final List sentiments = sentimentResult.getSentenceSentiments(); - for (TextSentiment textSentiment : sentiments) { + for (TextSentiment textSentiment : sentimentResult.getSentenceSentiments()) { System.out.printf( "Recognized sentence sentiment: %s, positive score: %s, neutral score: %s, negative score: %s.%n", textSentiment.getTextSentimentClass(), diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentimentAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentimentAsync.java index 7b4f1e7a23cd..1f494bd95532 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentimentAsync.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentimentAsync.java @@ -9,19 +9,19 @@ import java.util.concurrent.TimeUnit; /** - * Sample demonstrate how to analyze sentiment of a text input in asynchronously call. - */ + * Sample demonstrates how to asynchronously analyze the sentiment of an input text. + * */ public class AnalyzeSentimentAsync { /** - * Main method to invoke this demo about how to analyze sentiment of a text input. + * Main method to invoke this demo about how to analyze the sentiment of an input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildAsyncClient(); // The text that need be analysed. @@ -37,8 +37,7 @@ public static void main(String[] args) { documentSentiment.getNeutralScore(), documentSentiment.getNegativeScore()); - final List sentiments = result.getSentenceSentiments(); - for (TextSentiment textSentiment : sentiments) { + for (TextSentiment textSentiment : result.getSentenceSentiments()) { System.out.printf( "Recognized sentence sentiment: %s, positive score: %s, neutral score: %s, negative score: %s.%n", textSentiment.getTextSentimentClass(), diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ExtractKeyPhrases.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ExtractKeyPhrases.java index e54338395f30..6106293cd843 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ExtractKeyPhrases.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ExtractKeyPhrases.java @@ -4,19 +4,19 @@ package com.azure.ai.textanalytics; /** - * Sample demonstrate how to analyze key phrases of a text input. + * Sample demonstrate how to extract the key phrases of an input text. */ public class ExtractKeyPhrases { /** - * Main method to invoke this demo about how to extract key phrases of a text input. + * Main method to invoke this demo about how to extract the key phrases of an input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildClient(); // The text that need be analysed. diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ExtractKeyPhrasesAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ExtractKeyPhrasesAsync.java index fd2b480183e7..8049c2bf4c6c 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ExtractKeyPhrasesAsync.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ExtractKeyPhrasesAsync.java @@ -3,25 +3,22 @@ package com.azure.ai.textanalytics; -import com.azure.ai.textanalytics.models.TextSentiment; - -import java.util.List; import java.util.concurrent.TimeUnit; /** - * Sample demonstrate how to analyze key phrases of a text input in asynchronously call. + * Sample demonstrates how to asynchronously extract the key phrases of an input text. */ public class ExtractKeyPhrasesAsync { /** - * Main method to invoke this demo about how to extract key phrases of a text input. + * Main method to invoke this demo about how to extract the key phrases of an input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildAsyncClient(); // The text that need be analysed. diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/HelloWorld.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/HelloWorld.java index b01d9b55a405..3a7b659da824 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/HelloWorld.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/HelloWorld.java @@ -9,34 +9,33 @@ import java.util.List; /** - * Sample demonstrate how to detect language of a text input. + * Sample demonstrate how to detect the language of an input text. */ public class HelloWorld { /** - * Main method to invoke this demo about how to detect language of a text input. + * Main method to invoke this demo about how to detect the language of an input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildClient(); // The text that need be analysed. String text = "hello world"; final DetectLanguageResult detectLanguageResult = client.detectLanguage(text, "US"); - final DetectedLanguage detectedDocumentLanguage = detectLanguageResult.getPrimaryLanguage(); + final DetectedLanguage detectedPrimaryLanguage = detectLanguageResult.getPrimaryLanguage(); System.out.printf("Detected primary language: %s, ISO 6391 name: %s, score: %s.%n", - detectedDocumentLanguage.getName(), - detectedDocumentLanguage.getIso6391Name(), - detectedDocumentLanguage.getScore()); + detectedPrimaryLanguage.getName(), + detectedPrimaryLanguage.getIso6391Name(), + detectedPrimaryLanguage.getScore()); - final List detectedLanguages = detectLanguageResult.getDetectedLanguages(); - for (DetectedLanguage detectedLanguage : detectedLanguages) { - System.out.printf("Other detected languages: %s, ISO 6391 name: %s, score: %s.%n", + for (DetectedLanguage detectedLanguage : detectLanguageResult.getDetectedLanguages()) { + System.out.printf("Another detected language: %s, ISO 6391 name: %s, score: %s.%n", detectedLanguage.getName(), detectedLanguage.getIso6391Name(), detectedLanguage.getScore()); diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/HelloWorldAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/HelloWorldAsync.java index 84181b364d81..d53fb874279e 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/HelloWorldAsync.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/HelloWorldAsync.java @@ -8,19 +8,19 @@ import java.util.concurrent.TimeUnit; /** - * Sample demonstrate how to detect language of a text input in asynchronously call. + * Sample demonstrates how to asynchronously detect the language of an input text. */ public class HelloWorldAsync { /** - * Main method to invoke this demo about how to detect language of a text input. + * Main method to invoke this demo about how to detect the language of an input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildAsyncClient(); // The text that need be analysed. @@ -29,8 +29,14 @@ public static void main(String[] args) { client.detectLanguage(text).subscribe( result -> { final DetectedLanguage primaryLanguage = result.getPrimaryLanguage(); - System.out.printf("Detected language: %s, ISO 6391 name: %s, score: %s.%n", + System.out.printf("Detected primary language: %s, ISO 6391 name: %s, score: %s.%n", primaryLanguage.getName(), primaryLanguage.getIso6391Name(), primaryLanguage.getScore()); + for (DetectedLanguage detectedLanguage : result.getDetectedLanguages()) { + System.out.printf("Another detected language: %s, ISO 6391 name: %s, score: %s.%n", + detectedLanguage.getName(), + detectedLanguage.getIso6391Name(), + detectedLanguage.getScore()); + } }, error -> System.err.println("There was an error detecting language of the text." + error), () -> System.out.println("Language detected.")); diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeEntities.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeEntities.java index f0d8e4c52e1a..a8253d51ff08 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeEntities.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeEntities.java @@ -6,19 +6,19 @@ import com.azure.ai.textanalytics.models.NamedEntity; /** - * Sample demonstrate how to recognize entities of a text input. + * Sample demonstrate how to recognize the entities of an input text. */ public class RecognizeEntities { /** - * Main method to invoke this demo about how to recognize entities of a text input. + * Main method to invoke this demo about how to recognize the entities of an input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildClient(); // The text that need be analysed. diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeEntitiesAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeEntitiesAsync.java index b52fa105b110..9e831c43611c 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeEntitiesAsync.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeEntitiesAsync.java @@ -8,19 +8,19 @@ import java.util.concurrent.TimeUnit; /** - * Sample demonstrate how to recognize entities of a text input in asynchronously call. + * Sample demonstrates how to asynchronously recognize the entities of an input text. */ public class RecognizeEntitiesAsync { /** - * Main method to invoke this demo about how to recognize entities of a text input. + * Main method to invoke this demo about how to recognize the entities of an input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildAsyncClient(); // The text that need be analysed. diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeLinkedEntities.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeLinkedEntities.java index a101cd833d97..e66110c99fcc 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeLinkedEntities.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeLinkedEntities.java @@ -6,19 +6,19 @@ import com.azure.ai.textanalytics.models.LinkedEntity; /** - * Sample demonstrate how to recognize linked entities of a text input. + * Sample demonstrate how to recognize the linked entities of an input text. */ public class RecognizeLinkedEntities { /** - * Main method to invoke this demo about how to recognize linked entities of a text input. + * Main method to invoke this demo about how to recognize the linked entities of an input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildClient(); // The text that need be analysed. diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeLinkedEntitiesAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeLinkedEntitiesAsync.java index 2991a87fde31..c10c3b800f83 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeLinkedEntitiesAsync.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeLinkedEntitiesAsync.java @@ -8,19 +8,19 @@ import java.util.concurrent.TimeUnit; /** - * Sample demonstrate how to recognize linked entities of a text input in asynchronously call. + * Sample demonstrates how to asynchronously recognize the linked entities of an input text. */ public class RecognizeLinkedEntitiesAsync { /** - * Main method to invoke this demo about how to recognize linked entities of a text input. + * Main method to invoke this demo about how to recognize the linked entities of an input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildAsyncClient(); // The text that need be analysed. diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizePii.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizePii.java index 04c841845bee..4ab7c6ada8e1 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizePii.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizePii.java @@ -6,19 +6,19 @@ import com.azure.ai.textanalytics.models.NamedEntity; /** - * Sample demonstrate how to recognize PII(Personally Identifiable Information) entities of a text input. + * Sample demonstrate how to recognize the PII(Personally Identifiable Information) entities of an input text. */ public class RecognizePii { /** - * Main method to invoke this demo about how to analyze sentiment of a text input. + * Main method to invoke this demo about how to recognize the PII entities of an input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildClient(); // The text that need be analysed. diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizePiiAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizePiiAsync.java index 8600b26f8a14..afd58791a33a 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizePiiAsync.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizePiiAsync.java @@ -8,20 +8,20 @@ import java.util.concurrent.TimeUnit; /** - * Sample demonstrate how to recognize PII(Personally Identifiable Information) entities of a text input in - * asynchronously call. + * Sample demonstrates how to asynchronously recognize the PII(Personally Identifiable Information) entities of an input + * text. */ public class RecognizePiiAsync { /** - * Main method to invoke this demo about how to analyze sentiment of a text input. + * Main method to invoke this demo about how to recognize the PII entities of an input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildAsyncClient(); // The text that need be analysed. diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocuments.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocuments.java index 085ff53cc8fe..e1b9644fc202 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocuments.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocuments.java @@ -17,19 +17,19 @@ import java.util.List; /** - * Sample demonstrate how to analyze sentiment of a batch of text inputs. + * Sample demonstrate how to analyze the sentiments of a batch input text. */ public class AnalyzeSentimentBatchDocuments { /** - * Main method to invoke this demo about how to analyze sentiment of a batch of text inputs. + * Main method to invoke this demo about how to analyze the sentiments of a batch input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildClient(); // The texts that need be analysed. @@ -57,13 +57,13 @@ public static void main(String[] args) { // Analyzed sentiment for each of document from a batch of documents for (AnalyzeSentimentResult analyzeSentimentResult : analyzedBatchResult) { System.out.printf("Document ID: %s%n", analyzeSentimentResult.getId()); - final TextSentiment documentSentiment = analyzeSentimentResult.getDocumentSentiment(); // Erroneous document - if (documentSentiment == null) { + if (analyzeSentimentResult.isError()) { System.out.printf("Cannot analyze sentiment. Error: %s%n", analyzeSentimentResult.getError().getMessage()); continue; } // Valid document + final TextSentiment documentSentiment = analyzeSentimentResult.getDocumentSentiment(); System.out.printf("Recognized document sentiment: %s, positive score: %s, neutral score: %s, negative score: %s, length of sentence: %s, offset of sentence: %s.%n", documentSentiment.getTextSentimentClass(), documentSentiment.getPositiveScore(), diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocumentsAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocumentsAsync.java index 5e1bead3f5ee..cc7b8983c432 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocumentsAsync.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocumentsAsync.java @@ -17,19 +17,19 @@ import java.util.concurrent.TimeUnit; /** - * Sample demonstrate how to analyze sentiment of a batch of text inputs in asynchronously call. + * Sample demonstrates how to asynchronously analyze the sentiments of a batch input text. */ public class AnalyzeSentimentBatchDocumentsAsync { /** - * Main method to invoke this demo about how to analyze sentiment of a batch of text inputs. + * Main method to invoke this demo about how to analyze the sentiments of a batch input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildAsyncClient(); // The texts that need be analysed. @@ -58,13 +58,13 @@ public static void main(String[] args) { // Analyzed sentiment for each of document from a batch of documents for (AnalyzeSentimentResult analyzeSentimentResult : analyzedBatchResult) { System.out.printf("Document ID: %s%n", analyzeSentimentResult.getId()); - final TextSentiment documentSentiment = analyzeSentimentResult.getDocumentSentiment(); // Erroneous document - if (documentSentiment == null) { + if (analyzeSentimentResult.isError()) { System.out.printf("Cannot analyze sentiment. Error: %s%n", analyzeSentimentResult.getError().getMessage()); continue; } // Valid document + final TextSentiment documentSentiment = analyzeSentimentResult.getDocumentSentiment(); System.out.printf("Analyzed document sentiment: %s, positive score: %s, neutral score: %s, negative score: %s, length of sentence: %s, offset of sentence: %s.%n", documentSentiment.getTextSentimentClass(), documentSentiment.getPositiveScore(), diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocuments.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocuments.java index bce1b947dc99..8478cd933ab1 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocuments.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocuments.java @@ -17,19 +17,19 @@ import java.util.List; /** - * Sample demonstrate how to detect language of a batch of text inputs. + * Sample demonstrate how to detect the languages of a batch input text. */ public class DetectLanguageBatchDocuments { /** - * Main method to invoke this demo about how to detect language of a batch of text inputs. + * Main method to invoke this demo about how to detect the languages of a batch input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildClient(); // The texts that need be analysed. @@ -42,7 +42,8 @@ public static void main(String[] args) { final TextAnalyticsRequestOptions requestOptions = new TextAnalyticsRequestOptions().setShowStatistics(true); // Detecting batch languages - final DocumentResultCollection detectedBatchResult = client.detectBatchLanguagesWithResponse(inputs, requestOptions, Context.NONE).getValue(); + final DocumentResultCollection detectedBatchResult = + client.detectBatchLanguagesWithResponse(inputs, requestOptions, Context.NONE).getValue(); System.out.printf("Model version: %s%n", detectedBatchResult.getModelVersion()); // Batch statistics @@ -56,13 +57,13 @@ public static void main(String[] args) { // Detected languages for a document from a batch of documents for (DetectLanguageResult detectLanguageResult : detectedBatchResult) { System.out.printf("Document ID: %s%n", detectLanguageResult.getId()); - final DetectedLanguage detectedPrimaryLanguage = detectLanguageResult.getPrimaryLanguage(); // Erroneous document - if (detectedPrimaryLanguage == null) { + if (detectLanguageResult.isError()) { System.out.printf("Cannot detect language. Error: %s%n", detectLanguageResult.getError().getMessage()); continue; } // Valid document + final DetectedLanguage detectedPrimaryLanguage = detectLanguageResult.getPrimaryLanguage(); System.out.printf("Detected primary language: %s, ISO 6391 name: %s, score: %s.%n", detectedPrimaryLanguage.getName(), detectedPrimaryLanguage.getIso6391Name(), diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocumentsAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocumentsAsync.java index 35f654c3d082..e52f2b859489 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocumentsAsync.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocumentsAsync.java @@ -9,29 +9,27 @@ import com.azure.ai.textanalytics.models.DetectLanguageResult; import com.azure.ai.textanalytics.models.DetectedLanguage; import com.azure.ai.textanalytics.models.DocumentResultCollection; -import com.azure.ai.textanalytics.models.TextAnalyticsError; import com.azure.ai.textanalytics.models.TextAnalyticsRequestOptions; import com.azure.ai.textanalytics.models.TextDocumentBatchStatistics; -import com.azure.core.util.Context; import java.util.Arrays; import java.util.List; import java.util.concurrent.TimeUnit; /** - * Sample demonstrate how to detect language of a batch of text inputs in asynchronously call. + * Sample demonstrates how to asynchronously detect the languages of a batch input text. */ public class DetectLanguageBatchDocumentsAsync { /** - * Main method to invoke this demo about how to detect language of a batch of text inputs. + * Main method to invoke this demo about how to detect the languages of a batch input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildAsyncClient(); // The texts that need be analysed. @@ -60,13 +58,13 @@ public static void main(String[] args) { // Detected languages for a document from a batch of documents for (DetectLanguageResult detectLanguageResult : detectedBatchResult) { System.out.printf("Document ID: %s%n", detectLanguageResult.getId()); - final DetectedLanguage detectedPrimaryLanguage = detectLanguageResult.getPrimaryLanguage(); // Erroneous document - if (detectedPrimaryLanguage == null) { + if (detectLanguageResult.isError()) { System.out.printf("Cannot detect language. Error: %s%n", detectLanguageResult.getError().getMessage()); continue; } // Valid document + final DetectedLanguage detectedPrimaryLanguage = detectLanguageResult.getPrimaryLanguage(); System.out.printf("Detected primary language: %s, ISO 6391 name: %s, score: %s.%n", detectedPrimaryLanguage.getName(), detectedPrimaryLanguage.getIso6391Name(), diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocuments.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocuments.java index 7f0ae8403dc1..f53ee0d52af9 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocuments.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocuments.java @@ -16,19 +16,19 @@ import java.util.List; /** - * Sample demonstrate how to analyze key phrases of a batch of text inputs. + * Sample demonstrate how to extract the key phrases of a batch input text. */ public class ExtractKeyPhrasesBatchDocuments { /** - * Main method to invoke this demo about how to extract key phrases of a batch of text inputs. + * Main method to invoke this demo about how to extract the key phrases of a batch input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildClient(); // The texts that need be analysed. @@ -55,14 +55,13 @@ public static void main(String[] args) { // Extracted key phrase for each of document from a batch of documents for (ExtractKeyPhraseResult extractKeyPhraseResult : extractedBatchResult) { System.out.printf("Document ID: %s%n", extractKeyPhraseResult.getId()); - final List documentKeyPhrases = extractKeyPhraseResult.getKeyPhrases(); // Erroneous document - if (documentKeyPhrases == null) { + if (extractKeyPhraseResult.isError()) { System.out.printf("Cannot extract key phrases. Error: %s%n", extractKeyPhraseResult.getError().getMessage()); continue; } // Valid document - for (String keyPhrases : documentKeyPhrases) { + for (String keyPhrases : extractKeyPhraseResult.getKeyPhrases()) { System.out.printf("Extracted phrases: %s.%n", keyPhrases); } } diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocumentsAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocumentsAsync.java index 5b704a0204a9..4991b96c8cd4 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocumentsAsync.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocumentsAsync.java @@ -16,19 +16,19 @@ import java.util.concurrent.TimeUnit; /** - * Sample demonstrate how to analyze key phrases of a batch of text inputs in asynchronously call. + * Sample demonstrates how to asynchronously extract the key phrases of a batch input text. */ public class ExtractKeyPhrasesBatchDocumentsAsync { /** - * Main method to invoke this demo about how to extract key phrases of a batch of text inputs. + * Main method to invoke this demo about how to extract the key phrases of a batch input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildAsyncClient(); // The texts that need be analysed. @@ -57,9 +57,8 @@ public static void main(String[] args) { // Extracted key phrase for each of document from a batch of documents for (ExtractKeyPhraseResult extractKeyPhraseResult : extractedBatchResult) { System.out.printf("Document ID: %s%n", extractKeyPhraseResult.getId()); - final List documentKeyPhrases = extractKeyPhraseResult.getKeyPhrases(); // Erroneous document - if (documentKeyPhrases == null) { + if (extractKeyPhraseResult.isError()) { System.out.printf("Cannot extract key phrases. Error: %s%n", extractKeyPhraseResult.getError().getMessage()); continue; } diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocuments.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocuments.java index 031712e16a84..7ff4a3da1dee 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocuments.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocuments.java @@ -17,19 +17,19 @@ import java.util.List; /** - * Sample demonstrate how to recognize entities of a batch of text inputs. + * Sample demonstrate how to recognize the entities of a batch input text. */ public class RecognizeEntitiesBatchDocuments { /** - * Main method to invoke this demo about how to recognize entities of a batch of text inputs. + * Main method to invoke this demo about how to recognize the entities of a batch input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildClient(); // The texts that need be analysed. @@ -57,14 +57,13 @@ public static void main(String[] args) { // Recognized entities for each of document from a batch of documents for (RecognizeEntitiesResult recognizeEntitiesResult : recognizedBatchResult) { System.out.printf("Document ID: %s%n", recognizeEntitiesResult.getId()); - final List namedEntities = recognizeEntitiesResult.getNamedEntities(); // Erroneous document - if (namedEntities == null) { + if (recognizeEntitiesResult.isError()) { System.out.printf("Cannot recognize entities. Error: %s%n", recognizeEntitiesResult.getError().getMessage()); continue; } // Valid document - for (NamedEntity entity : namedEntities) { + for (NamedEntity entity : recognizeEntitiesResult.getNamedEntities()) { System.out.printf("Recognized entity: %s, entity type: %s, entity subtype: %s, offset: %s, length: %s, score: %s.%n", entity.getText(), entity.getType(), diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocumentsAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocumentsAsync.java index ec6ff5ae055f..3a39d1a21314 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocumentsAsync.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocumentsAsync.java @@ -17,19 +17,19 @@ import java.util.concurrent.TimeUnit; /** - * Sample demonstrate how to recognize entities of a batch of text inputs in asynchronously call. + * Sample demonstrates how to asynchronously recognize the entities of a batch input text. */ public class RecognizeEntitiesBatchDocumentsAsync { /** - * Main method to invoke this demo about how to recognize entities of a batch of text inputs. + * Main method to invoke this demo about how to recognize the entities of a batch input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildAsyncClient(); // The texts that need be analysed. @@ -58,14 +58,13 @@ public static void main(String[] args) { // Recognized entities for each of document from a batch of documents for (RecognizeEntitiesResult recognizeEntitiesResult : recognizedBatchResult) { System.out.printf("Document ID: %s%n", recognizeEntitiesResult.getId()); - final List namedEntities = recognizeEntitiesResult.getNamedEntities(); // Erroneous document - if (namedEntities == null) { + if (recognizeEntitiesResult.isError()) { System.out.printf("Cannot recognize entities. Error: %s%n", recognizeEntitiesResult.getError().getMessage()); continue; } // Valid document - for (NamedEntity entity : namedEntities) { + for (NamedEntity entity : recognizeEntitiesResult.getNamedEntities()) { System.out.printf("Recognized entity: %s, entity type: %s, entity subtype: %s, offset: %s, length: %s, score: %s.%n", entity.getText(), entity.getType(), diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocuments.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocuments.java index 5e41c5db234c..a3f26d05c406 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocuments.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocuments.java @@ -17,19 +17,19 @@ import java.util.List; /** - * Sample demonstrate how to recognize linked entities of a batch of text inputs. + * Sample demonstrate how to recognize the linked entities of a batch input text. */ public class RecognizeLinkedEntitiesBatchDocuments { /** - * Main method to invoke this demo about how to recognize linked entities of a batch of text inputs. + * Main method to invoke this demo about how to recognize the linked entities of a batch input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildClient(); // The texts that need be analysed. @@ -56,14 +56,13 @@ public static void main(String[] args) { // Recognized linked entities from a batch of documents for (RecognizeLinkedEntitiesResult linkedEntityDocumentResult : recognizedBatchResult) { System.out.printf("Document ID: %s%n", linkedEntityDocumentResult.getId()); - final List linkedEntities = linkedEntityDocumentResult.getLinkedEntities(); // Erroneous document - if (linkedEntities == null) { + if (linkedEntityDocumentResult.isError()) { System.out.printf("Cannot recognize linked entities. Error: %s%n", linkedEntityDocumentResult.getError().getMessage()); continue; } // Valid document - for (LinkedEntity linkedEntity : linkedEntities) { + for (LinkedEntity linkedEntity : linkedEntityDocumentResult.getLinkedEntities()) { System.out.printf("Recognized linked entity: %s, URL: %s, data source: %s%n", linkedEntity.getName(), linkedEntity.getUrl(), diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocumentsAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocumentsAsync.java index 22a5420acba5..4b6038a093cf 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocumentsAsync.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocumentsAsync.java @@ -17,19 +17,19 @@ import java.util.concurrent.TimeUnit; /** - * Sample demonstrate how to recognize linked entities of a batch of text inputs in asynchronously call. + * Sample demonstrates how to asynchronously recognize the linked entities of a batch input text. */ public class RecognizeLinkedEntitiesBatchDocumentsAsync { /** - * Main method to invoke this demo about how to recognize linked entities of a batch of text inputs. + * Main method to invoke this demo about how to recognize the linked entities of a batch input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildAsyncClient(); // The texts that need be analysed. @@ -58,14 +58,13 @@ public static void main(String[] args) { // Recognized linked entities from a batch of documents for (RecognizeLinkedEntitiesResult linkedEntityDocumentResult : recognizedBatchResult) { System.out.printf("Document ID: %s%n", linkedEntityDocumentResult.getId()); - final List linkedEntities = linkedEntityDocumentResult.getLinkedEntities(); // Erroneous document - if (linkedEntities == null) { + if (linkedEntityDocumentResult.isError()) { System.out.printf("Cannot recognize linked entities. Error: %s%n", linkedEntityDocumentResult.getError().getMessage()); continue; } // Valid document - for (LinkedEntity linkedEntity : linkedEntities) { + for (LinkedEntity linkedEntity : linkedEntityDocumentResult.getLinkedEntities()) { System.out.printf("Recognized linked entities: %s, URL: %s, data source: %s%n", linkedEntity.getName(), linkedEntity.getUrl(), diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocuments.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocuments.java index 3e0877d43022..636297eb2e0a 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocuments.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocuments.java @@ -17,19 +17,19 @@ import java.util.List; /** - * Sample demonstrate how to recognize PII(Personally Identifiable Information) entities of a text input. + * Sample demonstrate how to recognize the PII(Personally Identifiable Information) entities of a batch input text. */ public class RecognizePiiBatchDocuments { /** - * Main method to invoke this demo about how to recognize PII entities of a batch of text inputs. + * Main method to invoke this demo about how to recognize the PII entities of a batch input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildClient(); // The texts that need be analysed. @@ -57,14 +57,13 @@ public static void main(String[] args) { // Recognized PII entities for each of document from a batch of documents for (RecognizePiiEntitiesResult piiEntityDocumentResult : recognizedBatchResult) { System.out.printf("Document ID: %s%n", piiEntityDocumentResult.getId()); - final List namedEntities = piiEntityDocumentResult.getNamedEntities(); // Erroneous document - if (namedEntities == null) { + if (piiEntityDocumentResult.isError()) { System.out.printf("Cannot recognize PII entities. Error: %s%n", piiEntityDocumentResult.getError().getMessage()); continue; } // Valid document - for (NamedEntity entity : namedEntities) { + for (NamedEntity entity : piiEntityDocumentResult.getNamedEntities()) { System.out.printf("Recognized personal identifiable information entity: %s, entity type: %s, entity subtype: %s, offset: %s, length: %s, score: %s.%n", entity.getText(), entity.getType(), diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocumentsAsync.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocumentsAsync.java index 1dee0b2ff61e..e38983e2e7d6 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocumentsAsync.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocumentsAsync.java @@ -17,20 +17,20 @@ import java.util.concurrent.TimeUnit; /** - * Sample demonstrate how to recognize PII(Personally Identifiable Information) entities of a text input in - * asynchronously call. + * Sample demonstrates how to asynchronously recognize the PII(Personally Identifiable Information) entities of a batch + * input text. */ public class RecognizePiiBatchDocumentsAsync { /** - * Main method to invoke this demo about how to recognize PII entities of a batch of text inputs. + * Main method to invoke this demo about how to recognize the PII entities of a batch input text. * * @param args Unused arguments to the program. */ public static void main(String[] args) { // Instantiate a client that will be used to call the service. TextAnalyticsAsyncClient client = new TextAnalyticsClientBuilder() - .subscriptionKey("") - .endpoint("") + .subscriptionKey("{subscription_key}") + .endpoint("https://{servicename}.cognitiveservices.azure.com/") .buildAsyncClient(); // The texts that need be analysed. @@ -59,14 +59,13 @@ public static void main(String[] args) { // Recognized PII entities for each of document from a batch of documents for (RecognizePiiEntitiesResult piiEntityDocumentResult : recognizedBatchResult) { System.out.printf("Document ID: %s%n", piiEntityDocumentResult.getId()); - final List namedEntities = piiEntityDocumentResult.getNamedEntities(); // Erroneous document - if (namedEntities == null) { + if (piiEntityDocumentResult.isError()) { System.out.printf("Cannot recognize PII entities. Error: %s%n", piiEntityDocumentResult.getError().getMessage()); continue; } // Valid document - for (NamedEntity entity : namedEntities) { + for (NamedEntity entity : piiEntityDocumentResult.getNamedEntities()) { System.out.printf("Recognized personal identifiable information entity: %s, entity type: %s, entity subtype: %s, offset: %s, length: %s, score: %s.%n", entity.getText(), entity.getType(), From 2016f6c569aba7116fff2baefc61df25a74d6c48 Mon Sep 17 00:00:00 2001 From: shafang Date: Fri, 3 Jan 2020 12:24:04 -0800 Subject: [PATCH 3/3] wording, demonstrates --- .../java/com/azure/ai/textanalytics/AnalyzeSentiment.java | 4 +--- .../java/com/azure/ai/textanalytics/ExtractKeyPhrases.java | 2 +- .../samples/java/com/azure/ai/textanalytics/HelloWorld.java | 4 +--- .../java/com/azure/ai/textanalytics/RecognizeEntities.java | 2 +- .../com/azure/ai/textanalytics/RecognizeLinkedEntities.java | 2 +- .../samples/java/com/azure/ai/textanalytics/RecognizePii.java | 2 +- .../textanalytics/batch/AnalyzeSentimentBatchDocuments.java | 2 +- .../ai/textanalytics/batch/DetectLanguageBatchDocuments.java | 2 +- .../textanalytics/batch/ExtractKeyPhrasesBatchDocuments.java | 2 +- .../textanalytics/batch/RecognizeEntitiesBatchDocuments.java | 2 +- .../batch/RecognizeLinkedEntitiesBatchDocuments.java | 2 +- .../ai/textanalytics/batch/RecognizePiiBatchDocuments.java | 2 +- 12 files changed, 12 insertions(+), 16 deletions(-) diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentiment.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentiment.java index 24110cb9e533..33c55e930677 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentiment.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentiment.java @@ -6,10 +6,8 @@ import com.azure.ai.textanalytics.models.TextSentiment; import com.azure.ai.textanalytics.models.AnalyzeSentimentResult; -import java.util.List; - /** - * Sample demonstrate how to analyze the sentiment of an input text. + * Sample demonstrates how to analyze the sentiment of an input text. */ public class AnalyzeSentiment { /** diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ExtractKeyPhrases.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ExtractKeyPhrases.java index 6106293cd843..23d752ac72ee 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ExtractKeyPhrases.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ExtractKeyPhrases.java @@ -4,7 +4,7 @@ package com.azure.ai.textanalytics; /** - * Sample demonstrate how to extract the key phrases of an input text. + * Sample demonstrates how to extract the key phrases of an input text. */ public class ExtractKeyPhrases { /** diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/HelloWorld.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/HelloWorld.java index 3a7b659da824..494f72b72f63 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/HelloWorld.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/HelloWorld.java @@ -6,10 +6,8 @@ import com.azure.ai.textanalytics.models.DetectLanguageResult; import com.azure.ai.textanalytics.models.DetectedLanguage; -import java.util.List; - /** - * Sample demonstrate how to detect the language of an input text. + * Sample demonstrates how to detect the language of an input text. */ public class HelloWorld { /** diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeEntities.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeEntities.java index a8253d51ff08..f0d940a35680 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeEntities.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeEntities.java @@ -6,7 +6,7 @@ import com.azure.ai.textanalytics.models.NamedEntity; /** - * Sample demonstrate how to recognize the entities of an input text. + * Sample demonstrates how to recognize the entities of an input text. */ public class RecognizeEntities { /** diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeLinkedEntities.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeLinkedEntities.java index e66110c99fcc..9e01255b55ae 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeLinkedEntities.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizeLinkedEntities.java @@ -6,7 +6,7 @@ import com.azure.ai.textanalytics.models.LinkedEntity; /** - * Sample demonstrate how to recognize the linked entities of an input text. + * Sample demonstrates how to recognize the linked entities of an input text. */ public class RecognizeLinkedEntities { /** diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizePii.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizePii.java index 4ab7c6ada8e1..8c35ef3b7662 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizePii.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/RecognizePii.java @@ -6,7 +6,7 @@ import com.azure.ai.textanalytics.models.NamedEntity; /** - * Sample demonstrate how to recognize the PII(Personally Identifiable Information) entities of an input text. + * Sample demonstrates how to recognize the PII(Personally Identifiable Information) entities of an input text. */ public class RecognizePii { /** diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocuments.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocuments.java index e1b9644fc202..80a1b5fbbe61 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocuments.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocuments.java @@ -17,7 +17,7 @@ import java.util.List; /** - * Sample demonstrate how to analyze the sentiments of a batch input text. + * Sample demonstrates how to analyze the sentiments of a batch input text. */ public class AnalyzeSentimentBatchDocuments { /** diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocuments.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocuments.java index 8478cd933ab1..a33ce2f8ea6b 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocuments.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocuments.java @@ -17,7 +17,7 @@ import java.util.List; /** - * Sample demonstrate how to detect the languages of a batch input text. + * Sample demonstrates how to detect the languages of a batch input text. */ public class DetectLanguageBatchDocuments { /** diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocuments.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocuments.java index f53ee0d52af9..8af363673984 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocuments.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocuments.java @@ -16,7 +16,7 @@ import java.util.List; /** - * Sample demonstrate how to extract the key phrases of a batch input text. + * Sample demonstrates how to extract the key phrases of a batch input text. */ public class ExtractKeyPhrasesBatchDocuments { /** diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocuments.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocuments.java index 7ff4a3da1dee..25c396359524 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocuments.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocuments.java @@ -17,7 +17,7 @@ import java.util.List; /** - * Sample demonstrate how to recognize the entities of a batch input text. + * Sample demonstrates how to recognize the entities of a batch input text. */ public class RecognizeEntitiesBatchDocuments { /** diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocuments.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocuments.java index a3f26d05c406..bc85a0b36a8e 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocuments.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocuments.java @@ -17,7 +17,7 @@ import java.util.List; /** - * Sample demonstrate how to recognize the linked entities of a batch input text. + * Sample demonstrates how to recognize the linked entities of a batch input text. */ public class RecognizeLinkedEntitiesBatchDocuments { /** diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocuments.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocuments.java index 636297eb2e0a..edf4b38b63f4 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocuments.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiBatchDocuments.java @@ -17,7 +17,7 @@ import java.util.List; /** - * Sample demonstrate how to recognize the PII(Personally Identifiable Information) entities of a batch input text. + * Sample demonstrates how to recognize the PII(Personally Identifiable Information) entities of a batch input text. */ public class RecognizePiiBatchDocuments { /**