diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/README.md b/sdk/textanalytics/azure-ai-textanalytics/samples/README.md index 2863de0b81a4e..6ffb49f0a5966 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/README.md +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/README.md @@ -33,6 +33,7 @@ These sample programs show common scenarios for the Text Analytics client's offe |[sample_single_label_classify.py][single_label_classify_sample] and [sample_single_label_classify_async.py][single_label_classify_sample_async]|Use a custom model to classify documents into a single category| |[sample_multi_category_classify.py][multi_category_classify_sample] and [sample_multi_category_classify_async.py][multi_category_classify_sample_async]|Use a custom model to classify documents into multiple categories| |[sample_model_version.py][sample_model_version] and [sample_model_version_async.py][sample_model_version_async]|Set the model version for pre-built Text Analytics models| +|[sample_analyze_healthcare_action.py][sample_analyze_healthcare_action] and [sample_analyze_healthcare_action_async.py][sample_analyze_healthcare_action_async]|Run a healthcare and PII analysis together| ## Prerequisites * Python 3.6 or later is required to use this package @@ -109,6 +110,8 @@ what you can do with the Azure Text Analytics client library. [multi_category_classify_sample_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_multi_category_classify_async.py [sample_model_version]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/textanalytics/azure-ai-textanalytics/samples/sample_model_version.py [sample_model_version_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_model_version_async.py +[sample_analyze_healthcare_action]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/textanalytics/azure-ai-textanalytics/samples +[sample_analyze_healthcare_action_async]: https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/textanalytics/azure-ai-textanalytics/samples [pip]: https://pypi.org/project/pip/ [azure_subscription]: https://azure.microsoft.com/free/ [azure_language_account]: https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account?tabs=singleservice%2Cwindows diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_healthcare_action_async.py b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_healthcare_action_async.py new file mode 100644 index 0000000000000..bbae85c3021e6 --- /dev/null +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_healthcare_action_async.py @@ -0,0 +1,126 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- + +""" +FILE: sample_analyze_healthcare_action_async.py + +DESCRIPTION: + This sample demonstrates how to submit a collection of text documents for analysis, which uses the + AnalyzeHealthcareEntitiesAction and RecognizePiiEntitiesAction to recognize healthcare entities, + along with any PII entities. + The response will contain results from each of the individual actions specified in the request. + +USAGE: + python sample_analyze_healthcare_action_async.py + + Set the environment variables with your own values before running the sample: + 1) AZURE_LANGUAGE_ENDPOINT - the endpoint to your Language resource. + 2) AZURE_LANGUAGE_KEY - your Language subscription key +""" + + +import os +import asyncio + + +async def sample_analyze_healthcare_action(): + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics.aio import TextAnalyticsClient + from azure.ai.textanalytics import ( + AnalyzeHealthcareEntitiesAction, + RecognizePiiEntitiesAction, + ) + + endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"] + key = os.environ["AZURE_LANGUAGE_KEY"] + + text_analytics_client = TextAnalyticsClient( + endpoint=endpoint, + credential=AzureKeyCredential(key), + ) + + documents = [ + """ + Patient needs to take 100 mg of ibuprofen, and 3 mg of potassium. Also needs to take + 10 mg of Zocor. + """, + """ + Patient needs to take 50 mg of ibuprofen, and 2 mg of Coumadin. + """ + ] + + async with text_analytics_client: + poller = await text_analytics_client.begin_analyze_actions( + documents, + display_name="Sample Text Analysis", + actions=[ + AnalyzeHealthcareEntitiesAction(), + RecognizePiiEntitiesAction(domain_filter="phi"), + ], + ) + + pages = await poller.result() + + # To enumerate / zip for async, unless you install a third party library, + # you have to read in all of the elements into memory first. + # If you're not looking to enumerate / zip, we recommend you just asynchronously + # loop over it immediately, without going through this step of reading them into memory + document_results = [] + async for page in pages: + document_results.append(page) + + for doc, action_results in zip(documents, document_results): + print(f"\nDocument text: {doc}") + healthcare_entities_result = action_results[0] + print("...Results of Analyze Healthcare Entities Action:") + if healthcare_entities_result.is_error: + print("...Is an error with code '{}' and message '{}'".format( + healthcare_entities_result.code, healthcare_entities_result.message + )) + else: + for entity in healthcare_entities_result.entities: + print(f"Entity: {entity.text}") + print(f"...Normalized Text: {entity.normalized_text}") + print(f"...Category: {entity.category}") + print(f"...Subcategory: {entity.subcategory}") + print(f"...Offset: {entity.offset}") + print(f"...Confidence score: {entity.confidence_score}") + if entity.data_sources is not None: + print("...Data Sources:") + for data_source in entity.data_sources: + print(f"......Entity ID: {data_source.entity_id}") + print(f"......Name: {data_source.name}") + if entity.assertion is not None: + print("...Assertion:") + print(f"......Conditionality: {entity.assertion.conditionality}") + print(f"......Certainty: {entity.assertion.certainty}") + print(f"......Association: {entity.assertion.association}") + for relation in healthcare_entities_result.entity_relations: + print(f"Relation of type: {relation.relation_type} has the following roles") + for role in relation.roles: + print(f"...Role '{role.name}' with entity '{role.entity.text}'") + recognize_pii_entities_result = action_results[1] + + print("Results of Recognize PII Entities action:") + if recognize_pii_entities_result.is_error: + print("...Is an error with code '{}' and message '{}'".format( + recognize_pii_entities_result.code, recognize_pii_entities_result.message + )) + else: + for entity in recognize_pii_entities_result.entities: + print(f"......Entity: {entity.text}") + print(f".........Category: {entity.category}") + print(f".........Confidence Score: {entity.confidence_score}") + + print("------------------------------------------") + + +async def main(): + await sample_analyze_healthcare_action() + + +if __name__ == '__main__': + asyncio.run(main()) diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_healthcare_action.py b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_healthcare_action.py new file mode 100644 index 0000000000000..fed1cd815f09c --- /dev/null +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_healthcare_action.py @@ -0,0 +1,111 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- + +""" +FILE: sample_analyze_healthcare_action.py + +DESCRIPTION: + This sample demonstrates how to submit a collection of text documents for analysis, which uses the + AnalyzeHealthcareEntitiesAction and RecognizePiiEntitiesAction to recognize healthcare entities, + along with any PII entities. + The response will contain results from each of the individual actions specified in the request. + +USAGE: + python sample_analyze_healthcare_action.py + + Set the environment variables with your own values before running the sample: + 1) AZURE_LANGUAGE_ENDPOINT - the endpoint to your Language resource. + 2) AZURE_LANGUAGE_KEY - your Language subscription key +""" + + +import os + + +def sample_analyze_healthcare_action(): + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics import ( + TextAnalyticsClient, + AnalyzeHealthcareEntitiesAction, + RecognizePiiEntitiesAction, + ) + + endpoint = os.environ["AZURE_LANGUAGE_ENDPOINT"] + key = os.environ["AZURE_LANGUAGE_KEY"] + + text_analytics_client = TextAnalyticsClient( + endpoint=endpoint, + credential=AzureKeyCredential(key), + ) + + documents = [ + """ + Patient needs to take 100 mg of ibuprofen, and 3 mg of potassium. Also needs to take + 10 mg of Zocor. + """, + """ + Patient needs to take 50 mg of ibuprofen, and 2 mg of Coumadin. + """ + ] + + poller = text_analytics_client.begin_analyze_actions( + documents, + display_name="Sample Text Analysis", + actions=[ + AnalyzeHealthcareEntitiesAction(), + RecognizePiiEntitiesAction(domain_filter="phi"), + ], + ) + + document_results = poller.result() + for doc, action_results in zip(documents, document_results): + print(f"\nDocument text: {doc}") + healthcare_entities_result = action_results[0] + print("...Results of Analyze Healthcare Entities Action:") + if healthcare_entities_result.is_error: + print("...Is an error with code '{}' and message '{}'".format( + healthcare_entities_result.code, healthcare_entities_result.message + )) + else: + for entity in healthcare_entities_result.entities: + print(f"Entity: {entity.text}") + print(f"...Normalized Text: {entity.normalized_text}") + print(f"...Category: {entity.category}") + print(f"...Subcategory: {entity.subcategory}") + print(f"...Offset: {entity.offset}") + print(f"...Confidence score: {entity.confidence_score}") + if entity.data_sources is not None: + print("...Data Sources:") + for data_source in entity.data_sources: + print(f"......Entity ID: {data_source.entity_id}") + print(f"......Name: {data_source.name}") + if entity.assertion is not None: + print("...Assertion:") + print(f"......Conditionality: {entity.assertion.conditionality}") + print(f"......Certainty: {entity.assertion.certainty}") + print(f"......Association: {entity.assertion.association}") + for relation in healthcare_entities_result.entity_relations: + print(f"Relation of type: {relation.relation_type} has the following roles") + for role in relation.roles: + print(f"...Role '{role.name}' with entity '{role.entity.text}'") + recognize_pii_entities_result = action_results[1] + + print("Results of Recognize PII Entities action:") + if recognize_pii_entities_result.is_error: + print("...Is an error with code '{}' and message '{}'".format( + recognize_pii_entities_result.code, recognize_pii_entities_result.message + )) + else: + for entity in recognize_pii_entities_result.entities: + print(f"......Entity: {entity.text}") + print(f".........Category: {entity.category}") + print(f".........Confidence Score: {entity.confidence_score}") + + print("------------------------------------------") + + +if __name__ == "__main__": + sample_analyze_healthcare_action()