From c9ceb82b1e5154c3ed4bf7c6b13daabae66a0d48 Mon Sep 17 00:00:00 2001 From: Shawn Fang <45607042+mssfang@users.noreply.github.com> Date: Tue, 7 Mar 2023 16:23:02 -0800 Subject: [PATCH] simplify read me (#33914) --- .../azure-ai-textanalytics/CHANGELOG.md | 4 +- .../azure-ai-textanalytics/README.md | 229 ++------------- .../azure/ai/textanalytics/ReadmeSamples.java | 260 ------------------ 3 files changed, 19 insertions(+), 474 deletions(-) diff --git a/sdk/textanalytics/azure-ai-textanalytics/CHANGELOG.md b/sdk/textanalytics/azure-ai-textanalytics/CHANGELOG.md index 17d18c3a83a35..cc7051e98f669 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/CHANGELOG.md +++ b/sdk/textanalytics/azure-ai-textanalytics/CHANGELOG.md @@ -1,6 +1,6 @@ # Release History -## 5.3.0-beta.2 (Unreleased) +## 5.3.0-beta.2 (2023-03-07) ### Features Added - Added the following methods for performing abstractive summarization and extractive summarization actions: @@ -17,8 +17,6 @@ - Removed class `BooleanResolution` and enum value `BooleanResolution` in the class `ResolutionKind`. - Renamed `maxSentenceCount` to `sentenceCount` in abstractive summarization. -### Bugs Fixed - ### Other Changes - Integrate synchronous workflow for sync clients so that they do not block on async client APIs. It simplifies stack traces and improves debugging experience. diff --git a/sdk/textanalytics/azure-ai-textanalytics/README.md b/sdk/textanalytics/azure-ai-textanalytics/README.md index 7a9b3c1bd1e27..7566c0afa9d49 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/README.md +++ b/sdk/textanalytics/azure-ai-textanalytics/README.md @@ -336,99 +336,17 @@ Please refer to the service documentation for a conceptual discussion of [entity ### Analyze healthcare entities Text Analytics for health is a containerized service that extracts and labels relevant medical information from unstructured texts such as doctor's notes, discharge summaries, clinical documents, and electronic health records. + +- [Healthcare entities recognition][recognize_healthcare_entities_sample] + For more information see [How to: Use Text Analytics for health][healthcare]. -```java readme-sample-recognizeHealthcareEntities -List documents = Arrays.asList(new TextDocumentInput("0", - "RECORD #333582770390100 | MH | 85986313 | | 054351 | 2/14/2001 12:00:00 AM | " - + "CORONARY ARTERY DISEASE | Signed | DIS | Admission Date: 5/22/2001 " - + "Report Status: Signed Discharge Date: 4/24/2001 ADMISSION DIAGNOSIS: " - + "CORONARY ARTERY DISEASE. HISTORY OF PRESENT ILLNESS: " - + "The patient is a 54-year-old gentleman with a history of progressive angina over the past" - + " several months. The patient had a cardiac catheterization in July of this year revealing total" - + " occlusion of the RCA and 50% left main disease , with a strong family history of coronary" - + " artery disease with a brother dying at the age of 52 from a myocardial infarction and another" - + " brother who is status post coronary artery bypass grafting. The patient had a stress" - + " echocardiogram done on July , 2001 , which showed no wall motion abnormalities," - + " but this was a difficult study due to body habitus. The patient went for six minutes with" - + " minimal ST depressions in the anterior lateral leads , thought due to fatigue and wrist pain," - + " his anginal equivalent. Due to the patient's increased symptoms and family history and" - + " history left main disease with total occasional of his RCA was referred" - + " for revascularization with open heart surgery." -)); -AnalyzeHealthcareEntitiesOptions options = new AnalyzeHealthcareEntitiesOptions().setIncludeStatistics(true); -SyncPoller - syncPoller = textAnalyticsClient.beginAnalyzeHealthcareEntities(documents, options, Context.NONE); -syncPoller.waitForCompletion(); -syncPoller.getFinalResult().forEach( - analyzeHealthcareEntitiesResultCollection -> analyzeHealthcareEntitiesResultCollection.forEach( - healthcareEntitiesResult -> { - System.out.println("Document entities: "); - AtomicInteger ct = new AtomicInteger(); - healthcareEntitiesResult.getEntities().forEach(healthcareEntity -> { - System.out.printf("\ti = %d, Text: %s, category: %s, subcategory: %s, confidence score: %f.%n", - ct.getAndIncrement(), healthcareEntity.getText(), healthcareEntity.getCategory(), - healthcareEntity.getSubcategory(), healthcareEntity.getConfidenceScore()); - IterableStream healthcareEntityDataSources = - healthcareEntity.getDataSources(); - if (healthcareEntityDataSources != null) { - healthcareEntityDataSources.forEach(healthcareEntityLink -> System.out.printf( - "\t\tEntity ID in data source: %s, data source: %s.%n", - healthcareEntityLink.getEntityId(), healthcareEntityLink.getName())); - } - }); - // Healthcare entity relation groups - healthcareEntitiesResult.getEntityRelations().forEach(entityRelation -> { - System.out.printf("\tRelation type: %s.%n", entityRelation.getRelationType()); - entityRelation.getRoles().forEach(role -> { - final HealthcareEntity entity = role.getEntity(); - System.out.printf("\t\tEntity text: %s, category: %s, role: %s.%n", - entity.getText(), entity.getCategory(), role.getName()); - }); - System.out.printf("\tRelation confidence score: %f.%n", entityRelation.getConfidenceScore()); - }); - })); -``` ### Custom entities recognition Custom NER is one of the custom features offered by Azure Cognitive Service for Language. It is a cloud-based API service that applies machine-learning intelligence to enable you to build custom models for custom named entity recognition tasks. -```java readme-sample-custom-entities-recognition -List documents = new ArrayList<>(); -documents.add( - "A recent report by the Government Accountability Office (GAO) found that the dramatic increase " - + "in oil and natural gas development on federal lands over the past six years has stretched the" - + " staff of the BLM to a point that it has been unable to meet its environmental protection " - + "responsibilities."); -documents.add( - "David Schmidt, senior vice president--Food Safety, International Food" - + " Information Council (IFIC), Washington, D.C., discussed the physical activity component." -); - -// See the service documentation for regional support and how to train a model to recognize the custom entities, -// see https://aka.ms/azsdk/textanalytics/customentityrecognition -SyncPoller syncPoller = - textAnalyticsClient.beginRecognizeCustomEntities(documents, "{project_name}", "{deployment_name}"); -syncPoller.waitForCompletion(); -syncPoller.getFinalResult().forEach(documentsResults -> { - System.out.printf("Project name: %s, deployment name: %s.%n", - documentsResults.getProjectName(), documentsResults.getDeploymentName()); - for (RecognizeEntitiesResult documentResult : documentsResults) { - System.out.println("Document ID: " + documentResult.getId()); - if (!documentResult.isError()) { - for (CategorizedEntity entity : documentResult.getEntities()) { - System.out.printf( - "\tText: %s, category: %s, confidence score: %f.%n", - entity.getText(), entity.getCategory(), entity.getConfidenceScore()); - } - } else { - System.out.printf("\tCannot recognize custom entities. Error: %s%n", - documentResult.getError().getMessage()); - } - } -}); -``` +- [Custom entities recognition][recognize_custom_entities_sample] For more information see [How to use: Custom Entities Recognition][custom_entities_recognition_overview]. @@ -437,76 +355,9 @@ Custom text classification is one of the custom features offered by Azure Cognit cloud-based API service that applies machine-learning intelligence to enable you to build custom models for text classification tasks. -- Single label classification -```java readme-sample-single-label-classification -List documents = new ArrayList<>(); -documents.add( - "A recent report by the Government Accountability Office (GAO) found that the dramatic increase " - + "in oil and natural gas development on federal lands over the past six years has stretched the" - + " staff of the BLM to a point that it has been unable to meet its environmental protection " - + "responsibilities."); -documents.add( - "David Schmidt, senior vice president--Food Safety, International Food" - + " Information Council (IFIC), Washington, D.C., discussed the physical activity component." -); -documents.add( - "I need a reservation for an indoor restaurant in China. Please don't stop the music. Play music " - + "and add it to my playlist" -); - -// See the service documentation for regional support and how to train a model to classify your documents, -// see https://aka.ms/azsdk/textanalytics/customfunctionalities -SyncPoller syncPoller = - textAnalyticsClient.beginSingleLabelClassify(documents, "{project_name}", "{deployment_name}"); -syncPoller.waitForCompletion(); -syncPoller.getFinalResult().forEach(documentsResults -> { - System.out.printf("Project name: %s, deployment name: %s.%n", - documentsResults.getProjectName(), documentsResults.getDeploymentName()); - for (ClassifyDocumentResult documentResult : documentsResults) { - System.out.println("Document ID: " + documentResult.getId()); - if (!documentResult.isError()) { - for (ClassificationCategory classification : documentResult.getClassifications()) { - System.out.printf("\tCategory: %s, confidence score: %f.%n", - classification.getCategory(), classification.getConfidenceScore()); - } - } else { - System.out.printf("\tCannot classify category of document. Error: %s%n", - documentResult.getError().getMessage()); - } - } -}); -``` - -- Multi label classification -```java readme-sample-multi-label-classification -List documents = new ArrayList<>(); -documents.add( - "I need a reservation for an indoor restaurant in China. Please don't stop the music." - + " Play music and add it to my playlist" -); +- [Single label classification][single_label_classification_sample] -// See the service documentation for regional support and how to train a model to classify your documents, -// see https://aka.ms/azsdk/textanalytics/customfunctionalities -SyncPoller syncPoller = - textAnalyticsClient.beginMultiLabelClassify(documents, "{project_name}", "{deployment_name}"); -syncPoller.waitForCompletion(); -syncPoller.getFinalResult().forEach(documentsResults -> { - System.out.printf("Project name: %s, deployment name: %s.%n", - documentsResults.getProjectName(), documentsResults.getDeploymentName()); - for (ClassifyDocumentResult documentResult : documentsResults) { - System.out.println("Document ID: " + documentResult.getId()); - if (!documentResult.isError()) { - for (ClassificationCategory classification : documentResult.getClassifications()) { - System.out.printf("\tCategory: %s, confidence score: %f.%n", - classification.getCategory(), classification.getConfidenceScore()); - } - } else { - System.out.printf("\tCannot classify category of document. Error: %s%n", - documentResult.getError().getMessage()); - } - } -}); -``` +- [Multi label classification][multi_label_classification_sample] For more information see [How to use: Custom Text Classification][custom_text_classification_overview]. @@ -526,58 +377,9 @@ set of documents. Currently, the supported features are: - Abstractive Text Summarization (API version 2022-10-01-preview and newer) - Extractive Text Summarization (API version 2022-10-01-preview and newer) -```java readme-sample-analyzeActions - List documents = Arrays.asList( - new TextDocumentInput("0", - "We went to Contoso Steakhouse located at midtown NYC last week for a dinner party, and we adore" - + " the spot! They provide marvelous food and they have a great menu. The chief cook happens to be" - + " the owner (I think his name is John Doe) and he is super nice, coming out of the kitchen and " - + "greeted us all. We enjoyed very much dining in the place! The Sirloin steak I ordered was tender" - + " and juicy, and the place was impeccably clean. You can even pre-order from their online menu at" - + " www.contososteakhouse.com, call 312-555-0176 or send email to order@contososteakhouse.com! The" - + " only complaint I have is the food didn't come fast enough. Overall I highly recommend it!") - ); - - SyncPoller syncPoller = - textAnalyticsClient.beginAnalyzeActions(documents, - new TextAnalyticsActions().setDisplayName("{tasks_display_name}") - .setExtractKeyPhrasesActions(new ExtractKeyPhrasesAction()) - .setRecognizePiiEntitiesActions(new RecognizePiiEntitiesAction()), - new AnalyzeActionsOptions().setIncludeStatistics(false), - Context.NONE); - syncPoller.waitForCompletion(); - syncPoller.getFinalResult().forEach(analyzeActionsResult -> { - System.out.println("Key phrases extraction action results:"); - analyzeActionsResult.getExtractKeyPhrasesResults().forEach(actionResult -> { - AtomicInteger counter = new AtomicInteger(); - if (!actionResult.isError()) { - for (ExtractKeyPhraseResult extractKeyPhraseResult : actionResult.getDocumentsResults()) { - System.out.printf("%n%s%n", documents.get(counter.getAndIncrement())); - System.out.println("Extracted phrases:"); - extractKeyPhraseResult.getKeyPhrases() - .forEach(keyPhrases -> System.out.printf("\t%s.%n", keyPhrases)); - } - } - }); - System.out.println("PII entities recognition action results:"); - analyzeActionsResult.getRecognizePiiEntitiesResults().forEach(actionResult -> { - AtomicInteger counter = new AtomicInteger(); - if (!actionResult.isError()) { - for (RecognizePiiEntitiesResult entitiesResult : actionResult.getDocumentsResults()) { - System.out.printf("%n%s%n", documents.get(counter.getAndIncrement())); - PiiEntityCollection piiEntityCollection = entitiesResult.getEntities(); - System.out.printf("Redacted Text: %s%n", piiEntityCollection.getRedactedText()); - piiEntityCollection.forEach(entity -> System.out.printf( - "Recognized Personally Identifiable Information entity: %s, entity category: %s, " - + "entity subcategory: %s, offset: %s, confidence score: %f.%n", - entity.getText(), entity.getCategory(), entity.getSubcategory(), entity.getOffset(), - entity.getConfidenceScore())); - } - } - }); - }); -} -``` + +Sample: [Multiple action analysis][multiple_actions_analysis_sample] + For more examples, such as asynchronous samples, refer to [here][samples_readme]. ## Troubleshooting @@ -673,15 +475,20 @@ This project has adopted the [Microsoft Open Source Code of Conduct][coc]. For m [LogLevels]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/core/azure-core/src/main/java/com/azure/core/util/logging/ClientLogger.java [samples_readme]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/textanalytics/azure-ai-textanalytics/src/samples/README.md -[detect_language_sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocuments.java +[abstractive_summary_action_sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/lro/AbstractiveSummarization.java [analyze_sentiment_sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/AnalyzeSentimentBatchDocuments.java [analyze_sentiment_with_opinion_mining_sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/AnalyzeSentimentWithOpinionMining.java +[detect_language_sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DetectLanguageBatchDocuments.java +[dynamic_classification_sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DynamicClassifyBatchDocuments.java [extract_key_phrases_sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/ExtractKeyPhrasesBatchDocuments.java +[extractive_summary_action_sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/lro/ExtractiveSummarization.java +[multi_label_classification_sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/lro/MultiLabelClassifyDocument.java +[multiple_actions_analysis_sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/lro/AnalyzeActions.java [recognize_entities_sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeEntitiesBatchDocuments.java [recognize_pii_entities_sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizePiiEntitiesBatchDocuments.java [recognize_linked_entities_sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/RecognizeLinkedEntitiesBatchDocuments.java -[abstractive_summary_action_sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/lro/AbstractiveSummarization.java -[extractive_summary_action_sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/lro/ExtractiveSummarization.java -[dynamic_classification_sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/batch/DynamicClassifyBatchDocuments.java +[recognize_healthcare_entities_sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/lro/AnalyzeHealthcareEntities.java +[recognize_custom_entities_sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/lro/RecognizeCustomEntities.java +[single_label_classification_sample]: https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/lro/SingleLabelClassifyDocument.java ![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-java%2Fsdk%2Ftextanalytics%2Fazure-ai-textanalytics%2FREADME.png) diff --git a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java index 9261f8e79c0e3..0fa7372bb8a5b 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java +++ b/sdk/textanalytics/azure-ai-textanalytics/src/samples/java/com/azure/ai/textanalytics/ReadmeSamples.java @@ -3,46 +3,20 @@ package com.azure.ai.textanalytics; -import com.azure.ai.textanalytics.models.AnalyzeActionsOperationDetail; -import com.azure.ai.textanalytics.models.AnalyzeActionsOptions; -import com.azure.ai.textanalytics.models.AnalyzeHealthcareEntitiesOperationDetail; -import com.azure.ai.textanalytics.models.AnalyzeHealthcareEntitiesOptions; -import com.azure.ai.textanalytics.models.CategorizedEntity; -import com.azure.ai.textanalytics.models.ClassificationCategory; -import com.azure.ai.textanalytics.models.ClassifyDocumentOperationDetail; -import com.azure.ai.textanalytics.models.ClassifyDocumentResult; import com.azure.ai.textanalytics.models.DetectLanguageInput; import com.azure.ai.textanalytics.models.DetectedLanguage; import com.azure.ai.textanalytics.models.DocumentSentiment; -import com.azure.ai.textanalytics.models.EntityDataSource; -import com.azure.ai.textanalytics.models.ExtractKeyPhraseResult; -import com.azure.ai.textanalytics.models.ExtractKeyPhrasesAction; -import com.azure.ai.textanalytics.models.HealthcareEntity; import com.azure.ai.textanalytics.models.PiiEntityCollection; -import com.azure.ai.textanalytics.models.RecognizeCustomEntitiesOperationDetail; -import com.azure.ai.textanalytics.models.RecognizeEntitiesResult; -import com.azure.ai.textanalytics.models.RecognizePiiEntitiesAction; -import com.azure.ai.textanalytics.models.RecognizePiiEntitiesResult; -import com.azure.ai.textanalytics.models.TextAnalyticsActions; -import com.azure.ai.textanalytics.models.TextDocumentInput; -import com.azure.ai.textanalytics.util.AnalyzeActionsResultPagedIterable; -import com.azure.ai.textanalytics.util.AnalyzeHealthcareEntitiesPagedIterable; -import com.azure.ai.textanalytics.util.ClassifyDocumentPagedIterable; -import com.azure.ai.textanalytics.util.RecognizeCustomEntitiesPagedIterable; import com.azure.core.credential.AzureKeyCredential; import com.azure.core.credential.TokenCredential; import com.azure.core.exception.HttpResponseException; import com.azure.core.http.HttpClient; import com.azure.core.http.netty.NettyAsyncHttpClientBuilder; import com.azure.core.util.Context; -import com.azure.core.util.IterableStream; -import com.azure.core.util.polling.SyncPoller; import com.azure.identity.DefaultAzureCredentialBuilder; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; /** * WARNING: MODIFYING THIS FILE WILL REQUIRE CORRESPONDING UPDATES TO README.md FILE. LINE NUMBERS ARE USED TO EXTRACT @@ -214,238 +188,4 @@ public void recognizePiiEntity() { entity.getText(), entity.getCategory(), entity.getSubcategory(), entity.getConfidenceScore())); // END: readme-sample-recognizePiiEntity } - - /** - * Code snippet for recognizing healthcare entities in documents. - */ - public void recognizeHealthcareEntities() { - // BEGIN: readme-sample-recognizeHealthcareEntities - List documents = Arrays.asList(new TextDocumentInput("0", - "RECORD #333582770390100 | MH | 85986313 | | 054351 | 2/14/2001 12:00:00 AM | " - + "CORONARY ARTERY DISEASE | Signed | DIS | Admission Date: 5/22/2001 " - + "Report Status: Signed Discharge Date: 4/24/2001 ADMISSION DIAGNOSIS: " - + "CORONARY ARTERY DISEASE. HISTORY OF PRESENT ILLNESS: " - + "The patient is a 54-year-old gentleman with a history of progressive angina over the past" - + " several months. The patient had a cardiac catheterization in July of this year revealing total" - + " occlusion of the RCA and 50% left main disease , with a strong family history of coronary" - + " artery disease with a brother dying at the age of 52 from a myocardial infarction and another" - + " brother who is status post coronary artery bypass grafting. The patient had a stress" - + " echocardiogram done on July , 2001 , which showed no wall motion abnormalities," - + " but this was a difficult study due to body habitus. The patient went for six minutes with" - + " minimal ST depressions in the anterior lateral leads , thought due to fatigue and wrist pain," - + " his anginal equivalent. Due to the patient's increased symptoms and family history and" - + " history left main disease with total occasional of his RCA was referred" - + " for revascularization with open heart surgery." - )); - AnalyzeHealthcareEntitiesOptions options = new AnalyzeHealthcareEntitiesOptions().setIncludeStatistics(true); - SyncPoller - syncPoller = textAnalyticsClient.beginAnalyzeHealthcareEntities(documents, options, Context.NONE); - syncPoller.waitForCompletion(); - syncPoller.getFinalResult().forEach( - analyzeHealthcareEntitiesResultCollection -> analyzeHealthcareEntitiesResultCollection.forEach( - healthcareEntitiesResult -> { - System.out.println("Document entities: "); - AtomicInteger ct = new AtomicInteger(); - healthcareEntitiesResult.getEntities().forEach(healthcareEntity -> { - System.out.printf("\ti = %d, Text: %s, category: %s, subcategory: %s, confidence score: %f.%n", - ct.getAndIncrement(), healthcareEntity.getText(), healthcareEntity.getCategory(), - healthcareEntity.getSubcategory(), healthcareEntity.getConfidenceScore()); - IterableStream healthcareEntityDataSources = - healthcareEntity.getDataSources(); - if (healthcareEntityDataSources != null) { - healthcareEntityDataSources.forEach(healthcareEntityLink -> System.out.printf( - "\t\tEntity ID in data source: %s, data source: %s.%n", - healthcareEntityLink.getEntityId(), healthcareEntityLink.getName())); - } - }); - // Healthcare entity relation groups - healthcareEntitiesResult.getEntityRelations().forEach(entityRelation -> { - System.out.printf("\tRelation type: %s.%n", entityRelation.getRelationType()); - entityRelation.getRoles().forEach(role -> { - final HealthcareEntity entity = role.getEntity(); - System.out.printf("\t\tEntity text: %s, category: %s, role: %s.%n", - entity.getText(), entity.getCategory(), role.getName()); - }); - System.out.printf("\tRelation confidence score: %f.%n", entityRelation.getConfidenceScore()); - }); - })); - // END: readme-sample-recognizeHealthcareEntities - } - - /** - * Code snippet for recognizing custom entities in documents. - */ - public void recognizeCustomEntities() { - // BEGIN: readme-sample-custom-entities-recognition - List documents = new ArrayList<>(); - documents.add( - "A recent report by the Government Accountability Office (GAO) found that the dramatic increase " - + "in oil and natural gas development on federal lands over the past six years has stretched the" - + " staff of the BLM to a point that it has been unable to meet its environmental protection " - + "responsibilities."); - documents.add( - "David Schmidt, senior vice president--Food Safety, International Food" - + " Information Council (IFIC), Washington, D.C., discussed the physical activity component." - ); - - // See the service documentation for regional support and how to train a model to recognize the custom entities, - // see https://aka.ms/azsdk/textanalytics/customentityrecognition - SyncPoller syncPoller = - textAnalyticsClient.beginRecognizeCustomEntities(documents, "{project_name}", "{deployment_name}"); - syncPoller.waitForCompletion(); - syncPoller.getFinalResult().forEach(documentsResults -> { - System.out.printf("Project name: %s, deployment name: %s.%n", - documentsResults.getProjectName(), documentsResults.getDeploymentName()); - for (RecognizeEntitiesResult documentResult : documentsResults) { - System.out.println("Document ID: " + documentResult.getId()); - if (!documentResult.isError()) { - for (CategorizedEntity entity : documentResult.getEntities()) { - System.out.printf( - "\tText: %s, category: %s, confidence score: %f.%n", - entity.getText(), entity.getCategory(), entity.getConfidenceScore()); - } - } else { - System.out.printf("\tCannot recognize custom entities. Error: %s%n", - documentResult.getError().getMessage()); - } - } - }); - // END: readme-sample-custom-entities-recognition - } - - /** - * Code snippet for executing single-label classification in documents. - */ - public void singleLabelClassification() { - // BEGIN: readme-sample-single-label-classification - List documents = new ArrayList<>(); - documents.add( - "A recent report by the Government Accountability Office (GAO) found that the dramatic increase " - + "in oil and natural gas development on federal lands over the past six years has stretched the" - + " staff of the BLM to a point that it has been unable to meet its environmental protection " - + "responsibilities."); - documents.add( - "David Schmidt, senior vice president--Food Safety, International Food" - + " Information Council (IFIC), Washington, D.C., discussed the physical activity component." - ); - documents.add( - "I need a reservation for an indoor restaurant in China. Please don't stop the music. Play music " - + "and add it to my playlist" - ); - - // See the service documentation for regional support and how to train a model to classify your documents, - // see https://aka.ms/azsdk/textanalytics/customfunctionalities - SyncPoller syncPoller = - textAnalyticsClient.beginSingleLabelClassify(documents, "{project_name}", "{deployment_name}"); - syncPoller.waitForCompletion(); - syncPoller.getFinalResult().forEach(documentsResults -> { - System.out.printf("Project name: %s, deployment name: %s.%n", - documentsResults.getProjectName(), documentsResults.getDeploymentName()); - for (ClassifyDocumentResult documentResult : documentsResults) { - System.out.println("Document ID: " + documentResult.getId()); - if (!documentResult.isError()) { - for (ClassificationCategory classification : documentResult.getClassifications()) { - System.out.printf("\tCategory: %s, confidence score: %f.%n", - classification.getCategory(), classification.getConfidenceScore()); - } - } else { - System.out.printf("\tCannot classify category of document. Error: %s%n", - documentResult.getError().getMessage()); - } - } - }); - // END: readme-sample-single-label-classification - } - - /** - * Code snippet for executing multi-label classification in documents. - */ - public void multiLabelClassification() { - // BEGIN: readme-sample-multi-label-classification - List documents = new ArrayList<>(); - documents.add( - "I need a reservation for an indoor restaurant in China. Please don't stop the music." - + " Play music and add it to my playlist" - ); - - // See the service documentation for regional support and how to train a model to classify your documents, - // see https://aka.ms/azsdk/textanalytics/customfunctionalities - SyncPoller syncPoller = - textAnalyticsClient.beginMultiLabelClassify(documents, "{project_name}", "{deployment_name}"); - syncPoller.waitForCompletion(); - syncPoller.getFinalResult().forEach(documentsResults -> { - System.out.printf("Project name: %s, deployment name: %s.%n", - documentsResults.getProjectName(), documentsResults.getDeploymentName()); - for (ClassifyDocumentResult documentResult : documentsResults) { - System.out.println("Document ID: " + documentResult.getId()); - if (!documentResult.isError()) { - for (ClassificationCategory classification : documentResult.getClassifications()) { - System.out.printf("\tCategory: %s, confidence score: %f.%n", - classification.getCategory(), classification.getConfidenceScore()); - } - } else { - System.out.printf("\tCannot classify category of document. Error: %s%n", - documentResult.getError().getMessage()); - } - } - }); - // END: readme-sample-multi-label-classification - } - - /** - * Code snippet for executing actions in a batch of documents. - */ - public void analyzeActions() { - // BEGIN: readme-sample-analyzeActions - List documents = Arrays.asList( - new TextDocumentInput("0", - "We went to Contoso Steakhouse located at midtown NYC last week for a dinner party, and we adore" - + " the spot! They provide marvelous food and they have a great menu. The chief cook happens to be" - + " the owner (I think his name is John Doe) and he is super nice, coming out of the kitchen and " - + "greeted us all. We enjoyed very much dining in the place! The Sirloin steak I ordered was tender" - + " and juicy, and the place was impeccably clean. You can even pre-order from their online menu at" - + " www.contososteakhouse.com, call 312-555-0176 or send email to order@contososteakhouse.com! The" - + " only complaint I have is the food didn't come fast enough. Overall I highly recommend it!") - ); - - SyncPoller syncPoller = - textAnalyticsClient.beginAnalyzeActions(documents, - new TextAnalyticsActions().setDisplayName("{tasks_display_name}") - .setExtractKeyPhrasesActions(new ExtractKeyPhrasesAction()) - .setRecognizePiiEntitiesActions(new RecognizePiiEntitiesAction()), - new AnalyzeActionsOptions().setIncludeStatistics(false), - Context.NONE); - syncPoller.waitForCompletion(); - syncPoller.getFinalResult().forEach(analyzeActionsResult -> { - System.out.println("Key phrases extraction action results:"); - analyzeActionsResult.getExtractKeyPhrasesResults().forEach(actionResult -> { - AtomicInteger counter = new AtomicInteger(); - if (!actionResult.isError()) { - for (ExtractKeyPhraseResult extractKeyPhraseResult : actionResult.getDocumentsResults()) { - System.out.printf("%n%s%n", documents.get(counter.getAndIncrement())); - System.out.println("Extracted phrases:"); - extractKeyPhraseResult.getKeyPhrases() - .forEach(keyPhrases -> System.out.printf("\t%s.%n", keyPhrases)); - } - } - }); - System.out.println("PII entities recognition action results:"); - analyzeActionsResult.getRecognizePiiEntitiesResults().forEach(actionResult -> { - AtomicInteger counter = new AtomicInteger(); - if (!actionResult.isError()) { - for (RecognizePiiEntitiesResult entitiesResult : actionResult.getDocumentsResults()) { - System.out.printf("%n%s%n", documents.get(counter.getAndIncrement())); - PiiEntityCollection piiEntityCollection = entitiesResult.getEntities(); - System.out.printf("Redacted Text: %s%n", piiEntityCollection.getRedactedText()); - piiEntityCollection.forEach(entity -> System.out.printf( - "Recognized Personally Identifiable Information entity: %s, entity category: %s, " - + "entity subcategory: %s, offset: %s, confidence score: %f.%n", - entity.getText(), entity.getCategory(), entity.getSubcategory(), entity.getOffset(), - entity.getConfidenceScore())); - } - } - }); - }); - } - // END: readme-sample-analyzeActions }