diff --git a/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/_lro.py b/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/_lro.py index e03f06241786..482ead5e1798 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/_lro.py +++ b/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/_lro.py @@ -196,7 +196,7 @@ def cancel(self, **kwargs): # type: ignore :start-after: [START analyze_healthcare_entities_with_cancellation] :end-before: [END analyze_healthcare_entities_with_cancellation] :language: python - :dedent: 8 + :dedent: 4 :caption: Cancel an existing health operation. """ polling_interval = kwargs.pop("polling_interval", 5) diff --git a/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/_text_analytics_client.py b/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/_text_analytics_client.py index 3c2f888ef2ed..e8a3a25a177a 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/_text_analytics_client.py +++ b/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/_text_analytics_client.py @@ -95,14 +95,14 @@ class TextAnalyticsClient(TextAnalyticsClientBase): :start-after: [START create_ta_client_with_key] :end-before: [END create_ta_client_with_key] :language: python - :dedent: 8 + :dedent: 4 :caption: Creating the TextAnalyticsClient with endpoint and API key. .. literalinclude:: ../samples/sample_authentication.py :start-after: [START create_ta_client_with_aad] :end-before: [END create_ta_client_with_aad] :language: python - :dedent: 8 + :dedent: 4 :caption: Creating the TextAnalyticsClient with endpoint and token credential from Azure Active Directory. """ @@ -174,7 +174,7 @@ def detect_language( # type: ignore :start-after: [START detect_language] :end-before: [END detect_language] :language: python - :dedent: 8 + :dedent: 4 :caption: Detecting language in a batch of documents. """ country_hint_arg = kwargs.pop("country_hint", None) @@ -256,7 +256,7 @@ def recognize_entities( # type: ignore :start-after: [START recognize_entities] :end-before: [END recognize_entities] :language: python - :dedent: 8 + :dedent: 4 :caption: Recognize entities in a batch of documents. """ language_arg = kwargs.pop("language", None) @@ -355,7 +355,7 @@ def recognize_pii_entities( # type: ignore :start-after: [START recognize_pii_entities] :end-before: [END recognize_pii_entities] :language: python - :dedent: 8 + :dedent: 4 :caption: Recognize personally identifiable information entities in a batch of documents. """ language_arg = kwargs.pop("language", None) @@ -457,7 +457,7 @@ def recognize_linked_entities( # type: ignore :start-after: [START recognize_linked_entities] :end-before: [END recognize_linked_entities] :language: python - :dedent: 8 + :dedent: 4 :caption: Recognize linked entities in a batch of documents. """ language_arg = kwargs.pop("language", None) @@ -559,7 +559,7 @@ def begin_analyze_healthcare_entities( # type: ignore :start-after: [START analyze_healthcare_entities] :end-before: [END analyze_healthcare_entities] :language: python - :dedent: 8 + :dedent: 4 :caption: Recognize healthcare entities in a batch of documents. """ language_arg = kwargs.pop("language", None) @@ -667,7 +667,7 @@ def extract_key_phrases( # type: ignore :start-after: [START extract_key_phrases] :end-before: [END extract_key_phrases] :language: python - :dedent: 8 + :dedent: 4 :caption: Extract the key phrases in a batch of documents. """ language_arg = kwargs.pop("language", None) @@ -759,7 +759,7 @@ def analyze_sentiment( # type: ignore :start-after: [START analyze_sentiment] :end-before: [END analyze_sentiment] :language: python - :dedent: 8 + :dedent: 4 :caption: Analyze sentiment in a batch of documents. """ language_arg = kwargs.pop("language", None) @@ -874,7 +874,7 @@ def begin_analyze_actions( # type: ignore :start-after: [START analyze] :end-before: [END analyze] :language: python - :dedent: 8 + :dedent: 4 :caption: Start a long-running operation to perform a variety of text analysis actions over a batch of documents. """ diff --git a/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/aio/_lro_async.py b/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/aio/_lro_async.py index bc463f9865c9..c09bbe02b9f0 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/aio/_lro_async.py +++ b/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/aio/_lro_async.py @@ -175,7 +175,7 @@ async def cancel( # type: ignore :start-after: [START analyze_healthcare_entities_with_cancellation_async] :end-before: [END analyze_healthcare_entities_with_cancellation_async] :language: python - :dedent: 8 + :dedent: 4 :caption: Cancel an existing health operation. """ polling_interval = kwargs.pop("polling_interval", 5) diff --git a/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/aio/_text_analytics_client_async.py b/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/aio/_text_analytics_client_async.py index 7adc54938c75..30d43ae8599e 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/aio/_text_analytics_client_async.py +++ b/sdk/textanalytics/azure-ai-textanalytics/azure/ai/textanalytics/aio/_text_analytics_client_async.py @@ -91,14 +91,14 @@ class TextAnalyticsClient(AsyncTextAnalyticsClientBase): :start-after: [START create_ta_client_with_key_async] :end-before: [END create_ta_client_with_key_async] :language: python - :dedent: 8 + :dedent: 4 :caption: Creating the TextAnalyticsClient with endpoint and API key. .. literalinclude:: ../samples/async_samples/sample_authentication_async.py :start-after: [START create_ta_client_with_aad_async] :end-before: [END create_ta_client_with_aad_async] :language: python - :dedent: 8 + :dedent: 4 :caption: Creating the TextAnalyticsClient with endpoint and token credential from Azure Active Directory. """ @@ -172,7 +172,7 @@ async def detect_language( # type: ignore :start-after: [START detect_language_async] :end-before: [END detect_language_async] :language: python - :dedent: 8 + :dedent: 4 :caption: Detecting language in a batch of documents. """ country_hint_arg = kwargs.pop("country_hint", None) @@ -252,7 +252,7 @@ async def recognize_entities( # type: ignore :start-after: [START recognize_entities_async] :end-before: [END recognize_entities_async] :language: python - :dedent: 8 + :dedent: 4 :caption: Recognize entities in a batch of documents. """ language_arg = kwargs.pop("language", None) @@ -350,7 +350,7 @@ async def recognize_pii_entities( # type: ignore :start-after: [START recognize_pii_entities] :end-before: [END recognize_pii_entities] :language: python - :dedent: 8 + :dedent: 4 :caption: Recognize personally identifiable information entities in a batch of documents. """ language_arg = kwargs.pop("language", None) @@ -450,7 +450,7 @@ async def recognize_linked_entities( # type: ignore :start-after: [START recognize_linked_entities_async] :end-before: [END recognize_linked_entities_async] :language: python - :dedent: 8 + :dedent: 4 :caption: Recognize linked entities in a batch of documents. """ language_arg = kwargs.pop("language", None) @@ -537,7 +537,7 @@ async def extract_key_phrases( # type: ignore :start-after: [START extract_key_phrases_async] :end-before: [END extract_key_phrases_async] :language: python - :dedent: 8 + :dedent: 4 :caption: Extract the key phrases in a batch of documents. """ language_arg = kwargs.pop("language", None) @@ -625,7 +625,7 @@ async def analyze_sentiment( # type: ignore :start-after: [START analyze_sentiment_async] :end-before: [END analyze_sentiment_async] :language: python - :dedent: 8 + :dedent: 4 :caption: Analyze sentiment in a batch of documents. """ language_arg = kwargs.pop("language", None) @@ -734,7 +734,7 @@ async def begin_analyze_healthcare_entities( # type: ignore :start-after: [START analyze_healthcare_entities_async] :end-before: [END analyze_healthcare_entities_async] :language: python - :dedent: 8 + :dedent: 4 :caption: Analyze healthcare entities in a batch of documents. """ language_arg = kwargs.pop("language", None) @@ -858,7 +858,7 @@ async def begin_analyze_actions( # type: ignore :start-after: [START analyze_async] :end-before: [END analyze_async] :language: python - :dedent: 8 + :dedent: 4 :caption: Start a long-running operation to perform a variety of text analysis actions over a batch of documents. """ diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_alternative_document_input_async.py b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_alternative_document_input_async.py index 9bcc831839e1..cca027ac9f0b 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_alternative_document_input_async.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_alternative_document_input_async.py @@ -25,41 +25,38 @@ import asyncio -class AlternativeDocumentInputSampleAsync(object): - - async def alternative_document_input(self): - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics.aio import TextAnalyticsClient - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) - - documents = [ - {"id": "0", "country_hint": "US", "text": "I had the best day of my life. I decided to go sky-diving and it made me appreciate my whole life so much more. I developed a deep-connection with my instructor as well."}, - {"id": "1", "country_hint": "GB", - "text": "This was a waste of my time. The speaker put me to sleep."}, - {"id": "2", "country_hint": "MX", "text": "No tengo dinero ni nada que dar..."}, - {"id": "3", "country_hint": "FR", - "text": "L'hôtel n'était pas très confortable. L'éclairage était trop sombre."} - ] - async with text_analytics_client: - result = await text_analytics_client.detect_language(documents) - - for idx, doc in enumerate(result): - if not doc.is_error: - print("Document text: {}".format(documents[idx])) - print("Language detected: {}".format(doc.primary_language.name)) - print("ISO6391 name: {}".format(doc.primary_language.iso6391_name)) - print("Confidence score: {}\n".format(doc.primary_language.confidence_score)) - if doc.is_error: - print(doc.id, doc.error) +async def sample_alternative_document_input(): + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics.aio import TextAnalyticsClient + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) + + documents = [ + {"id": "0", "country_hint": "US", "text": "I had the best day of my life. I decided to go sky-diving and it made me appreciate my whole life so much more. I developed a deep-connection with my instructor as well."}, + {"id": "1", "country_hint": "GB", + "text": "This was a waste of my time. The speaker put me to sleep."}, + {"id": "2", "country_hint": "MX", "text": "No tengo dinero ni nada que dar..."}, + {"id": "3", "country_hint": "FR", + "text": "L'hôtel n'était pas très confortable. L'éclairage était trop sombre."} + ] + async with text_analytics_client: + result = await text_analytics_client.detect_language(documents) + + for idx, doc in enumerate(result): + if not doc.is_error: + print("Document text: {}".format(documents[idx])) + print("Language detected: {}".format(doc.primary_language.name)) + print("ISO6391 name: {}".format(doc.primary_language.iso6391_name)) + print("Confidence score: {}\n".format(doc.primary_language.confidence_score)) + if doc.is_error: + print(doc.id, doc.error) async def main(): - sample = AlternativeDocumentInputSampleAsync() - await sample.alternative_document_input() + await sample_alternative_document_input() if __name__ == '__main__': diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_actions_async.py b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_actions_async.py index 9f46de7ec3a9..5f15ca8fc8e8 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_actions_async.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_actions_async.py @@ -27,142 +27,140 @@ import os import asyncio -class AnalyzeSampleAsync(object): - - async def analyze_async(self): - # [START analyze_async] - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics.aio import TextAnalyticsClient - from azure.ai.textanalytics import ( - RecognizeEntitiesAction, - RecognizeLinkedEntitiesAction, - RecognizePiiEntitiesAction, - ExtractKeyPhrasesAction, - AnalyzeSentimentAction, - ) - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - text_analytics_client = TextAnalyticsClient( - endpoint=endpoint, - credential=AzureKeyCredential(key), +async def sample_analyze_async(): + # [START analyze_async] + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics.aio import TextAnalyticsClient + from azure.ai.textanalytics import ( + RecognizeEntitiesAction, + RecognizeLinkedEntitiesAction, + RecognizePiiEntitiesAction, + ExtractKeyPhrasesAction, + AnalyzeSentimentAction, + ) + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient( + endpoint=endpoint, + credential=AzureKeyCredential(key), + ) + + documents = [ + '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!'\ + ] + + async with text_analytics_client: + poller = await text_analytics_client.begin_analyze_actions( + documents, + display_name="Sample Text Analysis", + actions=[ + RecognizeEntitiesAction(), + RecognizePiiEntitiesAction(), + ExtractKeyPhrasesAction(), + RecognizeLinkedEntitiesAction(), + AnalyzeSentimentAction() + ] ) - documents = [ - '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!'\ - ] - - async with text_analytics_client: - poller = await text_analytics_client.begin_analyze_actions( - documents, - display_name="Sample Text Analysis", - actions=[ - RecognizeEntitiesAction(), - RecognizePiiEntitiesAction(), - ExtractKeyPhrasesAction(), - RecognizeLinkedEntitiesAction(), - AnalyzeSentimentAction() - ] - ) - - 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("\nDocument text: {}".format(doc)) - recognize_entities_result = action_results[0] - print("...Results of Recognize Entities Action:") - if recognize_entities_result.is_error: - print("...Is an error with code '{}' and message '{}'".format( - recognize_entities_result.code, recognize_entities_result.message - )) - else: - for entity in recognize_entities_result.entities: - print("......Entity: {}".format(entity.text)) - print(".........Category: {}".format(entity.category)) - print(".........Confidence Score: {}".format(entity.confidence_score)) - print(".........Offset: {}".format(entity.offset)) - - 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("......Entity: {}".format(entity.text)) - print(".........Category: {}".format(entity.category)) - print(".........Confidence Score: {}".format(entity.confidence_score)) - - extract_key_phrases_result = action_results[2] - print("...Results of Extract Key Phrases action:") - if extract_key_phrases_result.is_error: - print("...Is an error with code '{}' and message '{}'".format( - extract_key_phrases_result.code, extract_key_phrases_result.message - )) - else: - print("......Key Phrases: {}".format(extract_key_phrases_result.key_phrases)) - - recognize_linked_entities_result = action_results[3] - print("...Results of Recognize Linked Entities action:") - if recognize_linked_entities_result.is_error: - print("...Is an error with code '{}' and message '{}'".format( - recognize_linked_entities_result.code, recognize_linked_entities_result.message - )) - else: - for linked_entity in recognize_linked_entities_result.entities: - print("......Entity name: {}".format(linked_entity.name)) - print(".........Data source: {}".format(linked_entity.data_source)) - print(".........Data source language: {}".format(linked_entity.language)) - print(".........Data source entity ID: {}".format(linked_entity.data_source_entity_id)) - print(".........Data source URL: {}".format(linked_entity.url)) - print(".........Document matches:") - for match in linked_entity.matches: - print("............Match text: {}".format(match.text)) - print("............Confidence Score: {}".format(match.confidence_score)) - print("............Offset: {}".format(match.offset)) - print("............Length: {}".format(match.length)) - - analyze_sentiment_result = action_results[4] - print("...Results of Analyze Sentiment action:") - if analyze_sentiment_result.is_error: - print("...Is an error with code '{}' and message '{}'".format( - analyze_sentiment_result.code, analyze_sentiment_result.message - )) - else: - print("......Overall sentiment: {}".format(analyze_sentiment_result.sentiment)) - print("......Scores: positive={}; neutral={}; negative={} \n".format( - analyze_sentiment_result.confidence_scores.positive, - analyze_sentiment_result.confidence_scores.neutral, - analyze_sentiment_result.confidence_scores.negative, - )) - print("------------------------------------------") - - # [END analyze_async] + 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("\nDocument text: {}".format(doc)) + recognize_entities_result = action_results[0] + print("...Results of Recognize Entities Action:") + if recognize_entities_result.is_error: + print("...Is an error with code '{}' and message '{}'".format( + recognize_entities_result.code, recognize_entities_result.message + )) + else: + for entity in recognize_entities_result.entities: + print("......Entity: {}".format(entity.text)) + print(".........Category: {}".format(entity.category)) + print(".........Confidence Score: {}".format(entity.confidence_score)) + print(".........Offset: {}".format(entity.offset)) + + 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("......Entity: {}".format(entity.text)) + print(".........Category: {}".format(entity.category)) + print(".........Confidence Score: {}".format(entity.confidence_score)) + + extract_key_phrases_result = action_results[2] + print("...Results of Extract Key Phrases action:") + if extract_key_phrases_result.is_error: + print("...Is an error with code '{}' and message '{}'".format( + extract_key_phrases_result.code, extract_key_phrases_result.message + )) + else: + print("......Key Phrases: {}".format(extract_key_phrases_result.key_phrases)) + + recognize_linked_entities_result = action_results[3] + print("...Results of Recognize Linked Entities action:") + if recognize_linked_entities_result.is_error: + print("...Is an error with code '{}' and message '{}'".format( + recognize_linked_entities_result.code, recognize_linked_entities_result.message + )) + else: + for linked_entity in recognize_linked_entities_result.entities: + print("......Entity name: {}".format(linked_entity.name)) + print(".........Data source: {}".format(linked_entity.data_source)) + print(".........Data source language: {}".format(linked_entity.language)) + print(".........Data source entity ID: {}".format(linked_entity.data_source_entity_id)) + print(".........Data source URL: {}".format(linked_entity.url)) + print(".........Document matches:") + for match in linked_entity.matches: + print("............Match text: {}".format(match.text)) + print("............Confidence Score: {}".format(match.confidence_score)) + print("............Offset: {}".format(match.offset)) + print("............Length: {}".format(match.length)) + + analyze_sentiment_result = action_results[4] + print("...Results of Analyze Sentiment action:") + if analyze_sentiment_result.is_error: + print("...Is an error with code '{}' and message '{}'".format( + analyze_sentiment_result.code, analyze_sentiment_result.message + )) + else: + print("......Overall sentiment: {}".format(analyze_sentiment_result.sentiment)) + print("......Scores: positive={}; neutral={}; negative={} \n".format( + analyze_sentiment_result.confidence_scores.positive, + analyze_sentiment_result.confidence_scores.neutral, + analyze_sentiment_result.confidence_scores.negative, + )) + print("------------------------------------------") + + # [END analyze_async] async def main(): - sample = AnalyzeSampleAsync() - await sample.analyze_async() + await sample_analyze_async() if __name__ == '__main__': loop = asyncio.get_event_loop() - loop.run_until_complete(main()) \ No newline at end of file + loop.run_until_complete(main()) diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_healthcare_entities_async.py b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_healthcare_entities_async.py index 3fc43e012a3d..1be113faea02 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_healthcare_entities_async.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_healthcare_entities_async.py @@ -25,117 +25,114 @@ """ -import os import asyncio -class AnalyzeHealthcareEntitiesSampleAsync(object): - - async def analyze_healthcare_entities_async(self): - - print( - "In this sample we will be combing through the prescriptions our pharmacy has fulfilled " - "so we can catalog how much inventory we have" - ) - print( - "We start out with a list of prescription documents." - ) - - # [START analyze_healthcare_entities_async] - import os - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics import HealthcareEntityRelationType, HealthcareEntityRelationRoleType - from azure.ai.textanalytics.aio import TextAnalyticsClient - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_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_healthcare_entities(documents) - result = await poller.result() - docs = [doc async for doc in result if not doc.is_error] - - print("Let's first visualize the outputted healthcare result:") - for idx, doc in enumerate(docs): - for entity in doc.entities: - print("Entity: {}".format(entity.text)) - print("...Normalized Text: {}".format(entity.normalized_text)) - print("...Category: {}".format(entity.category)) - print("...Subcategory: {}".format(entity.subcategory)) - print("...Offset: {}".format(entity.offset)) - print("...Confidence score: {}".format(entity.confidence_score)) - if entity.data_sources is not None: - print("...Data Sources:") - for data_source in entity.data_sources: - print("......Entity ID: {}".format(data_source.entity_id)) - print("......Name: {}".format(data_source.name)) - if entity.assertion is not None: - print("...Assertion:") - print("......Conditionality: {}".format(entity.assertion.conditionality)) - print("......Certainty: {}".format(entity.assertion.certainty)) - print("......Association: {}".format(entity.assertion.association)) - for relation in doc.entity_relations: - print("Relation of type: {} has the following roles".format(relation.relation_type)) - for role in relation.roles: - print("...Role '{}' with entity '{}'".format(role.name, role.entity.text)) - print("------------------------------------------") - - print("Now, let's get all of medication dosage relations from the documents") - dosage_of_medication_relations = [ - entity_relation - for doc in docs - for entity_relation in doc.entity_relations if entity_relation.relation_type == HealthcareEntityRelationType.DOSAGE_OF_MEDICATION - ] - # [END analyze_healthcare_entities_async] - - print( - "Now, I will create a dictionary of medication to total dosage. " - "I will use a regex to extract the dosage amount. For simplicity sake, I will assume " - "all dosages are represented with numbers and have mg unit." - ) - import re - from collections import defaultdict - - medication_to_dosage = defaultdict(int) - - for relation in dosage_of_medication_relations: - # The DosageOfMedication relation should only contain the dosage and medication roles - - dosage_role = next(filter(lambda x: x.name == HealthcareEntityRelationRoleType.DOSAGE, relation.roles)) - medication_role = next(filter(lambda x: x.name == HealthcareEntityRelationRoleType.MEDICATION, relation.roles)) - - try: - dosage_value = int(re.findall(r"\d+", dosage_role.entity.text)[0]) # we find the numbers in the dosage - medication_to_dosage[medication_role.entity.text] += dosage_value - except StopIteration: - # Error handling for if there's no dosage in numbers. - pass - - [ - print("We have fulfilled '{}' total mg of '{}'".format( - dosage, medication - )) - for medication, dosage in medication_to_dosage.items() - ] +async def sample_analyze_healthcare_entities_async(): + + print( + "In this sample we will be combing through the prescriptions our pharmacy has fulfilled " + "so we can catalog how much inventory we have" + ) + print( + "We start out with a list of prescription documents." + ) + + # [START analyze_healthcare_entities_async] + import os + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics import HealthcareEntityRelationType, HealthcareEntityRelationRoleType + from azure.ai.textanalytics.aio import TextAnalyticsClient + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_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_healthcare_entities(documents) + result = await poller.result() + docs = [doc async for doc in result if not doc.is_error] + + print("Let's first visualize the outputted healthcare result:") + for idx, doc in enumerate(docs): + for entity in doc.entities: + print("Entity: {}".format(entity.text)) + print("...Normalized Text: {}".format(entity.normalized_text)) + print("...Category: {}".format(entity.category)) + print("...Subcategory: {}".format(entity.subcategory)) + print("...Offset: {}".format(entity.offset)) + print("...Confidence score: {}".format(entity.confidence_score)) + if entity.data_sources is not None: + print("...Data Sources:") + for data_source in entity.data_sources: + print("......Entity ID: {}".format(data_source.entity_id)) + print("......Name: {}".format(data_source.name)) + if entity.assertion is not None: + print("...Assertion:") + print("......Conditionality: {}".format(entity.assertion.conditionality)) + print("......Certainty: {}".format(entity.assertion.certainty)) + print("......Association: {}".format(entity.assertion.association)) + for relation in doc.entity_relations: + print("Relation of type: {} has the following roles".format(relation.relation_type)) + for role in relation.roles: + print("...Role '{}' with entity '{}'".format(role.name, role.entity.text)) + print("------------------------------------------") + + print("Now, let's get all of medication dosage relations from the documents") + dosage_of_medication_relations = [ + entity_relation + for doc in docs + for entity_relation in doc.entity_relations if entity_relation.relation_type == HealthcareEntityRelationType.DOSAGE_OF_MEDICATION + ] + # [END analyze_healthcare_entities_async] + + print( + "Now, I will create a dictionary of medication to total dosage. " + "I will use a regex to extract the dosage amount. For simplicity sake, I will assume " + "all dosages are represented with numbers and have mg unit." + ) + import re + from collections import defaultdict + + medication_to_dosage = defaultdict(int) + + for relation in dosage_of_medication_relations: + # The DosageOfMedication relation should only contain the dosage and medication roles + + dosage_role = next(filter(lambda x: x.name == HealthcareEntityRelationRoleType.DOSAGE, relation.roles)) + medication_role = next(filter(lambda x: x.name == HealthcareEntityRelationRoleType.MEDICATION, relation.roles)) + + try: + dosage_value = int(re.findall(r"\d+", dosage_role.entity.text)[0]) # we find the numbers in the dosage + medication_to_dosage[medication_role.entity.text] += dosage_value + except StopIteration: + # Error handling for if there's no dosage in numbers. + pass + + [ + print("We have fulfilled '{}' total mg of '{}'".format( + dosage, medication + )) + for medication, dosage in medication_to_dosage.items() + ] + async def main(): - sample = AnalyzeHealthcareEntitiesSampleAsync() - await sample.analyze_healthcare_entities_async() + await sample_analyze_healthcare_entities_async() if __name__ == '__main__': diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_healthcare_entities_with_cancellation_async.py b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_healthcare_entities_with_cancellation_async.py index 7688fdf469f3..5c72f67bff6c 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_healthcare_entities_with_cancellation_async.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_healthcare_entities_with_cancellation_async.py @@ -26,57 +26,54 @@ from azure.core.exceptions import HttpResponseError -class AnalyzeHealthcareEntitiesWithCancellationSampleAsync(object): - - async def analyze_healthcare_entities_with_cancellation_async(self): - # [START analyze_healthcare_entities_with_cancellation_async] - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics.aio import TextAnalyticsClient - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - text_analytics_client = TextAnalyticsClient( - endpoint=endpoint, - credential=AzureKeyCredential(key), - ) - - documents = [ - "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." - ] - - async with text_analytics_client: - poller = await text_analytics_client.begin_analyze_healthcare_entities(documents) - - try: - cancellation_poller = await poller.cancel() - await cancellation_poller.wait() - - except HttpResponseError as e: - # If the operation has already reached a terminal state it cannot be cancelled. - print(e) - - else: - print("Healthcare entities analysis was successfully cancelled.") - - # [END analyze_healthcare_entities_with_cancellation_async] +async def sample_analyze_healthcare_entities_with_cancellation_async(): + # [START analyze_healthcare_entities_with_cancellation_async] + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics.aio import TextAnalyticsClient + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient( + endpoint=endpoint, + credential=AzureKeyCredential(key), + ) + + documents = [ + "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." + ] + + async with text_analytics_client: + poller = await text_analytics_client.begin_analyze_healthcare_entities(documents) + + try: + cancellation_poller = await poller.cancel() + await cancellation_poller.wait() + + except HttpResponseError as e: + # If the operation has already reached a terminal state it cannot be cancelled. + print(e) + + else: + print("Healthcare entities analysis was successfully cancelled.") + + # [END analyze_healthcare_entities_with_cancellation_async] async def main(): - sample = AnalyzeHealthcareEntitiesWithCancellationSampleAsync() - await sample.analyze_healthcare_entities_with_cancellation_async() + await sample_analyze_healthcare_entities_with_cancellation_async() if __name__ == '__main__': diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_sentiment_async.py b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_sentiment_async.py index 2ae4e37a228b..ce4166a60f5f 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_sentiment_async.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_sentiment_async.py @@ -29,88 +29,85 @@ import asyncio -class AnalyzeSentimentSampleAsync(object): - - async def analyze_sentiment_async(self): - print( - "In this sample we will be combing through reviews customers have left about their" - "experience using our skydiving company, Contoso." - ) - print( - "We start out with a list of reviews. Let us extract the reviews we are sure are " - "positive, so we can display them on our website and get even more customers!" - ) - # [START analyze_sentiment_async] - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics.aio import TextAnalyticsClient - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) - - documents = [ - """I had the best day of my life. I decided to go sky-diving and it made me appreciate my whole life so much more. - I developed a deep-connection with my instructor as well, and I feel as if I've made a life-long friend in her.""", - """This was a waste of my time. All of the views on this drop are extremely boring, all I saw was grass. 0/10 would - not recommend to any divers, even first timers.""", - """This was pretty good! The sights were ok, and I had fun with my instructors! Can't complain too much about my experience""", - """I only have one word for my experience: WOW!!! I can't believe I have had such a wonderful skydiving company right - in my backyard this whole time! I will definitely be a repeat customer, and I want to take my grandmother skydiving too, - I know she'll love it!""" - ] - - async with text_analytics_client: - result = await text_analytics_client.analyze_sentiment(documents) - - docs = [doc for doc in result if not doc.is_error] - - print("Let's visualize the sentiment of each of these documents") - for idx, doc in enumerate(docs): - print("Document text: {}".format(documents[idx])) - print("Overall sentiment: {}".format(doc.sentiment)) - # [END analyze_sentiment_async] - - print("Now, let us extract all of the positive reviews") - positive_reviews = [doc for doc in docs if doc.sentiment == 'positive'] - - print("We want to be very confident that our reviews are positive since we'll be posting them on our website.") - print("We're going to confirm our chosen reviews are positive using two different tests") - - print( - "First, we are going to check how confident the sentiment analysis model is that a document is positive. " - "Let's go with a 90% confidence." - ) - positive_reviews = [ - review for review in positive_reviews - if review.confidence_scores.positive >= 0.9 - ] - - print( - "Finally, we also want to make sure every sentence is positive so we only showcase our best selves!" - ) - positive_reviews_final = [] - for idx, review in enumerate(positive_reviews): - print("Looking at positive review #{}".format(idx + 1)) - any_sentence_not_positive = False - for sentence in review.sentences: - print("...Sentence '{}' has sentiment '{}' with confidence scores '{}'".format( - sentence.text, - sentence.sentiment, - sentence.confidence_scores - ) +async def sample_analyze_sentiment_async(): + print( + "In this sample we will be combing through reviews customers have left about their" + "experience using our skydiving company, Contoso." + ) + print( + "We start out with a list of reviews. Let us extract the reviews we are sure are " + "positive, so we can display them on our website and get even more customers!" + ) + # [START analyze_sentiment_async] + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics.aio import TextAnalyticsClient + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) + + documents = [ + """I had the best day of my life. I decided to go sky-diving and it made me appreciate my whole life so much more. + I developed a deep-connection with my instructor as well, and I feel as if I've made a life-long friend in her.""", + """This was a waste of my time. All of the views on this drop are extremely boring, all I saw was grass. 0/10 would + not recommend to any divers, even first timers.""", + """This was pretty good! The sights were ok, and I had fun with my instructors! Can't complain too much about my experience""", + """I only have one word for my experience: WOW!!! I can't believe I have had such a wonderful skydiving company right + in my backyard this whole time! I will definitely be a repeat customer, and I want to take my grandmother skydiving too, + I know she'll love it!""" + ] + + async with text_analytics_client: + result = await text_analytics_client.analyze_sentiment(documents) + + docs = [doc for doc in result if not doc.is_error] + + print("Let's visualize the sentiment of each of these documents") + for idx, doc in enumerate(docs): + print("Document text: {}".format(documents[idx])) + print("Overall sentiment: {}".format(doc.sentiment)) + # [END analyze_sentiment_async] + + print("Now, let us extract all of the positive reviews") + positive_reviews = [doc for doc in docs if doc.sentiment == 'positive'] + + print("We want to be very confident that our reviews are positive since we'll be posting them on our website.") + print("We're going to confirm our chosen reviews are positive using two different tests") + + print( + "First, we are going to check how confident the sentiment analysis model is that a document is positive. " + "Let's go with a 90% confidence." + ) + positive_reviews = [ + review for review in positive_reviews + if review.confidence_scores.positive >= 0.9 + ] + + print( + "Finally, we also want to make sure every sentence is positive so we only showcase our best selves!" + ) + positive_reviews_final = [] + for idx, review in enumerate(positive_reviews): + print("Looking at positive review #{}".format(idx + 1)) + any_sentence_not_positive = False + for sentence in review.sentences: + print("...Sentence '{}' has sentiment '{}' with confidence scores '{}'".format( + sentence.text, + sentence.sentiment, + sentence.confidence_scores ) - if sentence.sentiment != 'positive': - any_sentence_not_positive = True - if not any_sentence_not_positive: - positive_reviews_final.append(review) + ) + if sentence.sentiment != 'positive': + any_sentence_not_positive = True + if not any_sentence_not_positive: + positive_reviews_final.append(review) - print("We now have the final list of positive reviews we are going to display on our website!") + print("We now have the final list of positive reviews we are going to display on our website!") async def main(): - sample = AnalyzeSentimentSampleAsync() - await sample.analyze_sentiment_async() + await sample_analyze_sentiment_async() if __name__ == '__main__': diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_sentiment_with_opinion_mining_async.py b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_sentiment_with_opinion_mining_async.py index 0c1c111aa6d8..3ce873e467e1 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_sentiment_with_opinion_mining_async.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_analyze_sentiment_with_opinion_mining_async.py @@ -48,93 +48,92 @@ import asyncio -class AnalyzeSentimentWithOpinionMiningSampleAsync(object): - async def analyze_sentiment_with_opinion_mining(self): - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics.aio import TextAnalyticsClient - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - text_analytics_client = TextAnalyticsClient( - endpoint=endpoint, - credential=AzureKeyCredential(key) - ) - - print("In this sample we will be a hotel owner going through reviews of their hotel to find complaints.") - - print( - "I first found a handful of reviews for my hotel. Let's see what we have to improve." - ) - - documents = [ - """ - The food and service were unacceptable, but the concierge were nice. - After talking to them about the quality of the food and the process to get room service they refunded - the money we spent at the restaurant and gave us a voucher for near by restaurants. - """, - """ - The rooms were beautiful. The AC was good and quiet, which was key for us as outside it was 100F and our baby - was getting uncomfortable because of the heat. The breakfast was good too with good options and good servicing times. - The thing we didn't like was that the toilet in our bathroom was smelly. It could have been that the toilet was broken before we arrived. - Either way it was very uncomfortable. Once we notified the staff, they came and cleaned it and left candles. - """, - """ - Nice rooms! I had a great unobstructed view of the Microsoft campus but bathrooms were old and the toilet was dirty when we arrived. - It was close to bus stops and groceries stores. If you want to be close to campus I will recommend it, otherwise, might be better to stay in a cleaner one - """ - ] - - async with text_analytics_client: - result = await text_analytics_client.analyze_sentiment(documents) - doc_result = [doc for doc in result if not doc.is_error] - - print("\nLet's first see the general sentiment of each of these reviews") - positive_reviews = [doc for doc in doc_result if doc.sentiment == "positive"] - mixed_reviews = [doc for doc in doc_result if doc.sentiment == "mixed"] - negative_reviews = [doc for doc in doc_result if doc.sentiment == "negative"] - print("...We have {} positive reviews, {} mixed reviews, and {} negative reviews. ".format( - len(positive_reviews), len(mixed_reviews), len(negative_reviews) +async def sample_analyze_sentiment_with_opinion_mining(): + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics.aio import TextAnalyticsClient + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient( + endpoint=endpoint, + credential=AzureKeyCredential(key) + ) + + print("In this sample we will be a hotel owner going through reviews of their hotel to find complaints.") + + print( + "I first found a handful of reviews for my hotel. Let's see what we have to improve." + ) + + documents = [ + """ + The food and service were unacceptable, but the concierge were nice. + After talking to them about the quality of the food and the process to get room service they refunded + the money we spent at the restaurant and gave us a voucher for near by restaurants. + """, + """ + The rooms were beautiful. The AC was good and quiet, which was key for us as outside it was 100F and our baby + was getting uncomfortable because of the heat. The breakfast was good too with good options and good servicing times. + The thing we didn't like was that the toilet in our bathroom was smelly. It could have been that the toilet was broken before we arrived. + Either way it was very uncomfortable. Once we notified the staff, they came and cleaned it and left candles. + """, + """ + Nice rooms! I had a great unobstructed view of the Microsoft campus but bathrooms were old and the toilet was dirty when we arrived. + It was close to bus stops and groceries stores. If you want to be close to campus I will recommend it, otherwise, might be better to stay in a cleaner one + """ + ] + + async with text_analytics_client: + result = await text_analytics_client.analyze_sentiment(documents) + doc_result = [doc for doc in result if not doc.is_error] + + print("\nLet's first see the general sentiment of each of these reviews") + positive_reviews = [doc for doc in doc_result if doc.sentiment == "positive"] + mixed_reviews = [doc for doc in doc_result if doc.sentiment == "mixed"] + negative_reviews = [doc for doc in doc_result if doc.sentiment == "negative"] + print("...We have {} positive reviews, {} mixed reviews, and {} negative reviews. ".format( + len(positive_reviews), len(mixed_reviews), len(negative_reviews) + )) + print( + "\nSince these reviews seem so mixed, and since I'm interested in finding exactly what it is about my hotel that should be improved, " + "let's find the complaints users have about individual aspects of this hotel" + ) + + print( + "\nIn order to do that, I'm going to extract the targets of a negative sentiment. " + "I'm going to map each of these targets to the mined opinion object we get back to aggregate the reviews by target. " + ) + target_to_complaints = {} + + for document in doc_result: + for sentence in document.sentences: + for mined_opinion in sentence.mined_opinions: + target = mined_opinion.target + if target.sentiment == 'negative': + target_to_complaints.setdefault(target.text, []) + target_to_complaints[target.text].append(mined_opinion) + + print("\nLet's now go through the aspects of our hotel people have complained about and see what users have specifically said") + + for target, complaints in target_to_complaints.items(): + print("Users have made {} complaint(s) about '{}', specifically saying that it's '{}'".format( + len(complaints), + target, + "', '".join( + [assessment.text for complaint in complaints for assessment in complaint.assessments] + ) )) - print( - "\nSince these reviews seem so mixed, and since I'm interested in finding exactly what it is about my hotel that should be improved, " - "let's find the complaints users have about individual aspects of this hotel" - ) - - print( - "\nIn order to do that, I'm going to extract the targets of a negative sentiment. " - "I'm going to map each of these targets to the mined opinion object we get back to aggregate the reviews by target. " - ) - target_to_complaints = {} - - for document in doc_result: - for sentence in document.sentences: - for mined_opinion in sentence.mined_opinions: - target = mined_opinion.target - if target.sentiment == 'negative': - target_to_complaints.setdefault(target.text, []) - target_to_complaints[target.text].append(mined_opinion) - - print("\nLet's now go through the aspects of our hotel people have complained about and see what users have specifically said") - - for target, complaints in target_to_complaints.items(): - print("Users have made {} complaint(s) about '{}', specifically saying that it's '{}'".format( - len(complaints), - target, - "', '".join( - [assessment.text for complaint in complaints for assessment in complaint.assessments] - ) - )) - - - print( - "\n\nLooking at the breakdown, I can see what aspects of my hotel need improvement, and based off of both the number and " - "content of the complaints users have made about my toilets, I need to get that fixed ASAP." - ) + + + print( + "\n\nLooking at the breakdown, I can see what aspects of my hotel need improvement, and based off of both the number and " + "content of the complaints users have made about my toilets, I need to get that fixed ASAP." + ) + async def main(): - sample = AnalyzeSentimentWithOpinionMiningSampleAsync() - await sample.analyze_sentiment_with_opinion_mining() + await sample_analyze_sentiment_with_opinion_mining() if __name__ == '__main__': diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_authentication_async.py b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_authentication_async.py index 2e4f1162e037..57956debcb27 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_authentication_async.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_authentication_async.py @@ -34,63 +34,61 @@ import asyncio -class AuthenticationSampleAsync(object): - - async def authentication_with_api_key_credential_async(self): - print("\n.. authentication_with_api_key_credential_async") - # [START create_ta_client_with_key_async] - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics.aio import TextAnalyticsClient - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - text_analytics_client = TextAnalyticsClient(endpoint, AzureKeyCredential(key)) - # [END create_ta_client_with_key_async] - - doc = [ - """ - I need to take my cat to the veterinarian. She's been coughing for a while and I thought it was just a hairball, - but now I'm now worried it might be something else. She's still very healthy so I'm not too worried though. - """ - ] - async with text_analytics_client: - result = await text_analytics_client.detect_language(doc) - - print("Language detected: {}".format(result[0].primary_language.name)) - print("Confidence score: {}".format(result[0].primary_language.confidence_score)) - - async def authentication_with_azure_active_directory_async(self): - """DefaultAzureCredential will use the values from these environment - variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET +async def sample_authentication_with_api_key_credential_async(): + print("\n.. authentication_with_api_key_credential_async") + # [START create_ta_client_with_key_async] + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics.aio import TextAnalyticsClient + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient(endpoint, AzureKeyCredential(key)) + # [END create_ta_client_with_key_async] + + doc = [ """ - print("\n.. authentication_with_azure_active_directory_async") - # [START create_ta_client_with_aad_async] - from azure.ai.textanalytics.aio import TextAnalyticsClient - from azure.identity.aio import DefaultAzureCredential + I need to take my cat to the veterinarian. She's been coughing for a while and I thought it was just a hairball, + but now I'm now worried it might be something else. She's still very healthy so I'm not too worried though. + """ + ] + async with text_analytics_client: + result = await text_analytics_client.detect_language(doc) + + print("Language detected: {}".format(result[0].primary_language.name)) + print("Confidence score: {}".format(result[0].primary_language.confidence_score)) + - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - credential = DefaultAzureCredential() +async def sample_authentication_with_azure_active_directory_async(): + """DefaultAzureCredential will use the values from these environment + variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET + """ + print("\n.. authentication_with_azure_active_directory_async") + # [START create_ta_client_with_aad_async] + from azure.ai.textanalytics.aio import TextAnalyticsClient + from azure.identity.aio import DefaultAzureCredential - text_analytics_client = TextAnalyticsClient(endpoint, credential=credential) - # [END create_ta_client_with_aad_async] + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + credential = DefaultAzureCredential() - doc = [ - """ - I need to take my cat to the veterinarian. She's been coughing for a while and I thought it was just a hairball, - but now I'm now worried it might be something else. She's still very healthy so I'm not too worried though. - """ - ] - async with text_analytics_client: - result = await text_analytics_client.detect_language(doc) + text_analytics_client = TextAnalyticsClient(endpoint, credential=credential) + # [END create_ta_client_with_aad_async] + + doc = [ + """ + I need to take my cat to the veterinarian. She's been coughing for a while and I thought it was just a hairball, + but now I'm now worried it might be something else. She's still very healthy so I'm not too worried though. + """ + ] + async with text_analytics_client: + result = await text_analytics_client.detect_language(doc) - print("Language detected: {}".format(result[0].primary_language.name)) - print("Confidence score: {}".format(result[0].primary_language.confidence_score)) + print("Language detected: {}".format(result[0].primary_language.name)) + print("Confidence score: {}".format(result[0].primary_language.confidence_score)) async def main(): - sample = AuthenticationSampleAsync() - await sample.authentication_with_api_key_credential_async() - await sample.authentication_with_azure_active_directory_async() + await sample_authentication_with_api_key_credential_async() + await sample_authentication_with_azure_active_directory_async() if __name__ == '__main__': loop = asyncio.get_event_loop() diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_detect_language_async.py b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_detect_language_async.py index 02a4b65662ea..c1e0e2f4a0e5 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_detect_language_async.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_detect_language_async.py @@ -29,59 +29,56 @@ import asyncio -class DetectLanguageSampleAsync(object): - - async def detect_language_async(self): - print( - "In this sample we own a hotel with customers from all around the globe. We want to eventually " - "translate these reviews into English so our manager can read them. However, we first need to know which language " - "they are in for more accurate translation. This is the step we will be covering in this sample\n" - ) - # [START detect_language_async] - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics.aio import TextAnalyticsClient - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) - documents = [ - """ - The concierge Paulette was extremely helpful. Sadly when we arrived the elevator was broken, but with Paulette's help we barely noticed this inconvenience. - She arranged for our baggage to be brought up to our room with no extra charge and gave us a free meal to refurbish all of the calories we lost from - walking up the stairs :). Can't say enough good things about my experience! - """, - """ - 最近由于工作压力太大,我们决定去富酒店度假。那儿的温泉实在太舒服了,我跟我丈夫都完全恢复了工作前的青春精神!加油! - """ - ] - async with text_analytics_client: - result = await text_analytics_client.detect_language(documents) - - reviewed_docs = [doc for doc in result if not doc.is_error] - - print("Let's see what language each review is in!") - - for idx, doc in enumerate(reviewed_docs): - print("Review #{} is in '{}', which has ISO639-1 name '{}'\n".format( - idx, doc.primary_language.name, doc.primary_language.iso6391_name - )) - if doc.is_error: - print(doc.id, doc.error) - # [END detect_language_async] - print( - "When actually storing the reviews, we want to map the review to their ISO639-1 name " - "so everything is more standardized" - ) - - review_to_language = {} - for idx, doc in enumerate(reviewed_docs): - review_to_language[documents[idx]] = doc.primary_language.iso6391_name +async def sample_detect_language_async(): + print( + "In this sample we own a hotel with customers from all around the globe. We want to eventually " + "translate these reviews into English so our manager can read them. However, we first need to know which language " + "they are in for more accurate translation. This is the step we will be covering in this sample\n" + ) + # [START detect_language_async] + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics.aio import TextAnalyticsClient + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) + documents = [ + """ + The concierge Paulette was extremely helpful. Sadly when we arrived the elevator was broken, but with Paulette's help we barely noticed this inconvenience. + She arranged for our baggage to be brought up to our room with no extra charge and gave us a free meal to refurbish all of the calories we lost from + walking up the stairs :). Can't say enough good things about my experience! + """, + """ + 最近由于工作压力太大,我们决定去富酒店度假。那儿的温泉实在太舒服了,我跟我丈夫都完全恢复了工作前的青春精神!加油! + """ + ] + async with text_analytics_client: + result = await text_analytics_client.detect_language(documents) + + reviewed_docs = [doc for doc in result if not doc.is_error] + + print("Let's see what language each review is in!") + + for idx, doc in enumerate(reviewed_docs): + print("Review #{} is in '{}', which has ISO639-1 name '{}'\n".format( + idx, doc.primary_language.name, doc.primary_language.iso6391_name + )) + if doc.is_error: + print(doc.id, doc.error) + # [END detect_language_async] + print( + "When actually storing the reviews, we want to map the review to their ISO639-1 name " + "so everything is more standardized" + ) + + review_to_language = {} + for idx, doc in enumerate(reviewed_docs): + review_to_language[documents[idx]] = doc.primary_language.iso6391_name async def main(): - sample = DetectLanguageSampleAsync() - await sample.detect_language_async() + await sample_detect_language_async() if __name__ == '__main__': diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_extract_key_phrases_async.py b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_extract_key_phrases_async.py index 8ea90b01746f..cb6e4bed98f5 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_extract_key_phrases_async.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_extract_key_phrases_async.py @@ -27,60 +27,57 @@ import asyncio -class ExtractKeyPhrasesSampleAsync(object): - - async def extract_key_phrases_async(self): - print( - "In this sample, we want to find the articles that mention Microsoft to read." - ) - articles_that_mention_microsoft = [] - # [START extract_key_phrases_async] - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics.aio import TextAnalyticsClient - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) - articles = [ - """ - Washington, D.C. Autumn in DC is a uniquely beautiful season. The leaves fall from the trees - in a city chockful of forrests, leaving yellow leaves on the ground and a clearer view of the - blue sky above... - """, - """ - Redmond, WA. In the past few days, Microsoft has decided to further postpone the start date of - its United States workers, due to the pandemic that rages with no end in sight... - """, - """ - Redmond, WA. Employees at Microsoft can be excited about the new coffee shop that will open on campus - once workers no longer have to work remotely... - """ - ] - - async with text_analytics_client: - result = await text_analytics_client.extract_key_phrases(articles) - - for idx, doc in enumerate(result): - if not doc.is_error: - print("Key phrases in article #{}: {}".format( - idx + 1, - ", ".join(doc.key_phrases) - )) - # [END extract_key_phrases_async] - if "Microsoft" in doc.key_phrases: - articles_that_mention_microsoft.append(str(idx + 1)) - - print( - "The articles that mention Microsoft are articles number: {}. Those are the ones I'm interested in reading.".format( - ", ".join(articles_that_mention_microsoft) - ) +async def sample_extract_key_phrases_async(): + print( + "In this sample, we want to find the articles that mention Microsoft to read." + ) + articles_that_mention_microsoft = [] + # [START extract_key_phrases_async] + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics.aio import TextAnalyticsClient + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) + articles = [ + """ + Washington, D.C. Autumn in DC is a uniquely beautiful season. The leaves fall from the trees + in a city chockful of forrests, leaving yellow leaves on the ground and a clearer view of the + blue sky above... + """, + """ + Redmond, WA. In the past few days, Microsoft has decided to further postpone the start date of + its United States workers, due to the pandemic that rages with no end in sight... + """, + """ + Redmond, WA. Employees at Microsoft can be excited about the new coffee shop that will open on campus + once workers no longer have to work remotely... + """ + ] + + async with text_analytics_client: + result = await text_analytics_client.extract_key_phrases(articles) + + for idx, doc in enumerate(result): + if not doc.is_error: + print("Key phrases in article #{}: {}".format( + idx + 1, + ", ".join(doc.key_phrases) + )) + # [END extract_key_phrases_async] + if "Microsoft" in doc.key_phrases: + articles_that_mention_microsoft.append(str(idx + 1)) + + print( + "The articles that mention Microsoft are articles number: {}. Those are the ones I'm interested in reading.".format( + ", ".join(articles_that_mention_microsoft) ) + ) async def main(): - sample = ExtractKeyPhrasesSampleAsync() - await sample.extract_key_phrases_async() + await sample_extract_key_phrases_async() if __name__ == '__main__': diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_get_detailed_diagnostics_information_async.py b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_get_detailed_diagnostics_information_async.py index fd0689a30a62..edb62819dc7f 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_get_detailed_diagnostics_information_async.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_get_detailed_diagnostics_information_async.py @@ -28,54 +28,52 @@ _LOGGER = logging.getLogger(__name__) -class GetDetailedDiagnosticsInformationSampleAsync(object): - - async def get_detailed_diagnostics_information_async(self): - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics.aio import TextAnalyticsClient - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - # This client will log detailed information about its HTTP sessions, at DEBUG level - text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key), logging_enable=True) - - documents = [ - """I had the best day of my life. I decided to go sky-diving and it made me appreciate my whole life so much more. - I developed a deep-connection with my instructor as well. - """, - "This was a waste of my time. The speaker put me to sleep.", - "No tengo dinero ni nada que dar...", - "L'hôtel n'était pas très confortable. L'éclairage était trop sombre." - ] - - json_responses = [] - - def callback(resp): - _LOGGER.debug("document_count: {}".format(resp.statistics["document_count"])) - _LOGGER.debug("valid_document_count: {}".format(resp.statistics["valid_document_count"])) - _LOGGER.debug("erroneous_document_count: {}".format(resp.statistics["erroneous_document_count"])) - _LOGGER.debug("transaction_count: {}".format(resp.statistics["transaction_count"])) - _LOGGER.debug("model_version: {}".format(resp.model_version)) - json_response = json.dumps(resp.raw_response) - json_responses.append(json_response) - - async with text_analytics_client: - result = await text_analytics_client.extract_key_phrases( - documents, - show_stats=True, - model_version="latest", - raw_response_hook=callback - ) - for doc in result: - _LOGGER.warning("Doc with id {} has these warnings: {}".format(doc.id, doc.warnings)) - - _LOGGER.debug("json response: {}".format(json_responses[0])) + +async def sample_get_detailed_diagnostics_information_async(): + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics.aio import TextAnalyticsClient + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + # This client will log detailed information about its HTTP sessions, at DEBUG level + text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key), logging_enable=True) + + documents = [ + """I had the best day of my life. I decided to go sky-diving and it made me appreciate my whole life so much more. + I developed a deep-connection with my instructor as well. + """, + "This was a waste of my time. The speaker put me to sleep.", + "No tengo dinero ni nada que dar...", + "L'hôtel n'était pas très confortable. L'éclairage était trop sombre." + ] + + json_responses = [] + + def callback(resp): + _LOGGER.debug("document_count: {}".format(resp.statistics["document_count"])) + _LOGGER.debug("valid_document_count: {}".format(resp.statistics["valid_document_count"])) + _LOGGER.debug("erroneous_document_count: {}".format(resp.statistics["erroneous_document_count"])) + _LOGGER.debug("transaction_count: {}".format(resp.statistics["transaction_count"])) + _LOGGER.debug("model_version: {}".format(resp.model_version)) + json_response = json.dumps(resp.raw_response) + json_responses.append(json_response) + + async with text_analytics_client: + result = await text_analytics_client.extract_key_phrases( + documents, + show_stats=True, + model_version="latest", + raw_response_hook=callback + ) + for doc in result: + _LOGGER.warning("Doc with id {} has these warnings: {}".format(doc.id, doc.warnings)) + + _LOGGER.debug("json response: {}".format(json_responses[0])) async def main(): - sample = GetDetailedDiagnosticsInformationSampleAsync() - await sample.get_detailed_diagnostics_information_async() + await sample_get_detailed_diagnostics_information_async() if __name__ == '__main__': diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_recognize_entities_async.py b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_recognize_entities_async.py index 70bca058dd20..5460998e4679 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_recognize_entities_async.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_recognize_entities_async.py @@ -26,55 +26,52 @@ import asyncio -class RecognizeEntitiesSampleAsync(object): - - async def recognize_entities_async(self): +async def sample_recognize_entities_async(): + print( + "In this sample, we are a catering business, and we're looking to sort the reviews " + "for our organization based off of the organization that hired us for catering" + ) + organization_to_reviews = {} + # [START recognize_entities_async] + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics.aio import TextAnalyticsClient + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) + reviews = [ + """I work for Foo Company, and we hired Contoso for our annual founding ceremony. The food + was amazing and we all can't say enough good words about the quality and the level of service.""", + """We at the Foo Company re-hired Contoso after all of our past successes with the company. + Though the food was still great, I feel there has been a quality drop since their last time + catering for us. Is anyone else running into the same problem?""", + """Bar Company is over the moon about the service we received from Contoso, the best sliders ever!!!!""" + ] + + async with text_analytics_client: + result = await text_analytics_client.recognize_entities(reviews) + + result = [review for review in result if not review.is_error] + + for idx, review in enumerate(result): + for entity in review.entities: + print("Entity '{}' has category '{}'".format(entity.text, entity.category)) + # [END recognize_entities_async] + if entity.category == 'Organization': + organization_to_reviews.setdefault(entity.text, []) + organization_to_reviews[entity.text].append(reviews[idx]) + + for organization, reviews in organization_to_reviews.items(): print( - "In this sample, we are a catering business, and we're looking to sort the reviews " - "for our organization based off of the organization that hired us for catering" - ) - organization_to_reviews = {} - # [START recognize_entities_async] - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics.aio import TextAnalyticsClient - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) - reviews = [ - """I work for Foo Company, and we hired Contoso for our annual founding ceremony. The food - was amazing and we all can't say enough good words about the quality and the level of service.""", - """We at the Foo Company re-hired Contoso after all of our past successes with the company. - Though the food was still great, I feel there has been a quality drop since their last time - catering for us. Is anyone else running into the same problem?""", - """Bar Company is over the moon about the service we received from Contoso, the best sliders ever!!!!""" - ] - - async with text_analytics_client: - result = await text_analytics_client.recognize_entities(reviews) - - result = [review for review in result if not review.is_error] - - for idx, review in enumerate(result): - for entity in review.entities: - print("Entity '{}' has category '{}'".format(entity.text, entity.category)) - # [END recognize_entities_async] - if entity.category == 'Organization': - organization_to_reviews.setdefault(entity.text, []) - organization_to_reviews[entity.text].append(reviews[idx]) - - for organization, reviews in organization_to_reviews.items(): - print( - "\n\nOrganization '{}' has left us the following review(s): {}".format( - organization, "\n\n".join(reviews) - ) + "\n\nOrganization '{}' has left us the following review(s): {}".format( + organization, "\n\n".join(reviews) ) + ) async def main(): - sample = RecognizeEntitiesSampleAsync() - await sample.recognize_entities_async() + await sample_recognize_entities_async() if __name__ == '__main__': diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_recognize_linked_entities_async.py b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_recognize_linked_entities_async.py index 9bac94d62f9b..2923dbc733f1 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_recognize_linked_entities_async.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_recognize_linked_entities_async.py @@ -30,61 +30,58 @@ import asyncio -class RecognizeLinkedEntitiesSampleAsync(object): - - async def recognize_linked_entities_async(self): - print( - "In this sample, we are students conducting research for a class project. We will extract " - "links to Wikipedia articles for all entities listed in our research documents, so we have " - "all of the necessary information for research purposes." - ) - # [START recognize_linked_entities_async] - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics.aio import TextAnalyticsClient - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) - documents = [ - """ - Microsoft was founded by Bill Gates with some friends he met at Harvard. One of his friends, - Steve Ballmer, eventually became CEO after Bill Gates as well. Steve Ballmer eventually stepped - down as CEO of Microsoft, and was succeeded by Satya Nadella. - Microsoft originally moved its headquarters to Bellevue, Wahsington in Januaray 1979, but is now - headquartered in Redmond. - """ - ] - - async with text_analytics_client: - result = await text_analytics_client.recognize_linked_entities(documents) - - docs = [doc for doc in result if not doc.is_error] - - print( - "Let's map each entity to it's Wikipedia article. I also want to see how many times each " - "entity is mentioned in a document\n\n" - ) - entity_to_url = {} - for doc in docs: - for entity in doc.entities: - print("Entity '{}' has been mentioned '{}' time(s)".format( - entity.name, len(entity.matches) - )) - if entity.data_source == "Wikipedia": - entity_to_url[entity.name] = entity.url - # [END recognize_linked_entities_async] - - print("\nNow let's see all of the Wikipedia articles we've extracted from our research documents") - for entity, url in entity_to_url.items(): - print("Link to Wikipedia article for '{}': {}".format( - entity, url +async def sample_recognize_linked_entities_async(): + print( + "In this sample, we are students conducting research for a class project. We will extract " + "links to Wikipedia articles for all entities listed in our research documents, so we have " + "all of the necessary information for research purposes." + ) + # [START recognize_linked_entities_async] + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics.aio import TextAnalyticsClient + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) + documents = [ + """ + Microsoft was founded by Bill Gates with some friends he met at Harvard. One of his friends, + Steve Ballmer, eventually became CEO after Bill Gates as well. Steve Ballmer eventually stepped + down as CEO of Microsoft, and was succeeded by Satya Nadella. + Microsoft originally moved its headquarters to Bellevue, Wahsington in Januaray 1979, but is now + headquartered in Redmond. + """ + ] + + async with text_analytics_client: + result = await text_analytics_client.recognize_linked_entities(documents) + + docs = [doc for doc in result if not doc.is_error] + + print( + "Let's map each entity to it's Wikipedia article. I also want to see how many times each " + "entity is mentioned in a document\n\n" + ) + entity_to_url = {} + for doc in docs: + for entity in doc.entities: + print("Entity '{}' has been mentioned '{}' time(s)".format( + entity.name, len(entity.matches) )) + if entity.data_source == "Wikipedia": + entity_to_url[entity.name] = entity.url + # [END recognize_linked_entities_async] + + print("\nNow let's see all of the Wikipedia articles we've extracted from our research documents") + for entity, url in entity_to_url.items(): + print("Link to Wikipedia article for '{}': {}".format( + entity, url + )) async def main(): - sample = RecognizeLinkedEntitiesSampleAsync() - await sample.recognize_linked_entities_async() + await sample_recognize_linked_entities_async() if __name__ == '__main__': loop = asyncio.get_event_loop() diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_recognize_pii_entities_async.py b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_recognize_pii_entities_async.py index c6619f8a891f..d7d2d2b0dbc0 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_recognize_pii_entities_async.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/async_samples/sample_recognize_pii_entities_async.py @@ -28,69 +28,66 @@ import asyncio -class RecognizePiiEntitiesSampleAsync(object): - - async def recognize_pii_entities_async(self): - print( - "In this sample we will be going through our customer's loan payment information and redacting " - "all PII (personally identifable information) before storing this information on our public website. " - "I'm also looking to explicitly extract the SSN information, so I can update my database with SSNs for " - "our customers" - ) - # [START recognize_pii_entities_async] - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics.aio import TextAnalyticsClient - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - text_analytics_client = TextAnalyticsClient( - endpoint=endpoint, credential=AzureKeyCredential(key) - ) - documents = [ - """Parker Doe has repaid all of their loans as of 2020-04-25. - Their SSN is 859-98-0987. To contact them, use their phone number - 555-555-5555. They are originally from Brazil and have Brazilian CPF number 998.214.865-68""" - ] - - async with text_analytics_client: - result = await text_analytics_client.recognize_pii_entities(documents) - - docs = [doc for doc in result if not doc.is_error] - - print( - "Let's compare the original document with the documents after redaction. " - "I also want to comb through all of the entities that got redacted" - ) - for idx, doc in enumerate(docs): - print("Document text: {}".format(documents[idx])) - print("Redacted document text: {}".format(doc.redacted_text)) - for entity in doc.entities: - print("...Entity '{}' with category '{}' got redacted".format( - entity.text, entity.category - )) - # [END recognize_pii_entities_async] - print("All of the information that I expect to be redacted is!") - - print( - "Now I want to explicitly extract SSN information to add to my user SSN database. " - "I also want to be fairly confident that what I'm storing is an SSN, so let's also " - "ensure that we're > 60% positive the entity is a SSN" - ) - ssns = [] - for doc in docs: - for entity in doc.entities: - if entity.category == 'USSocialSecurityNumber' and entity.confidence_score >= 0.6: - ssns.append(entity.text) - - print("We have extracted the following SSNs as well: '{}'".format( - "', '".join(ssns) - )) +async def sample_recognize_pii_entities_async(): + print( + "In this sample we will be going through our customer's loan payment information and redacting " + "all PII (personally identifable information) before storing this information on our public website. " + "I'm also looking to explicitly extract the SSN information, so I can update my database with SSNs for " + "our customers" + ) + # [START recognize_pii_entities_async] + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics.aio import TextAnalyticsClient + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient( + endpoint=endpoint, credential=AzureKeyCredential(key) + ) + documents = [ + """Parker Doe has repaid all of their loans as of 2020-04-25. + Their SSN is 859-98-0987. To contact them, use their phone number + 555-555-5555. They are originally from Brazil and have Brazilian CPF number 998.214.865-68""" + ] + + async with text_analytics_client: + result = await text_analytics_client.recognize_pii_entities(documents) + + docs = [doc for doc in result if not doc.is_error] + + print( + "Let's compare the original document with the documents after redaction. " + "I also want to comb through all of the entities that got redacted" + ) + for idx, doc in enumerate(docs): + print("Document text: {}".format(documents[idx])) + print("Redacted document text: {}".format(doc.redacted_text)) + for entity in doc.entities: + print("...Entity '{}' with category '{}' got redacted".format( + entity.text, entity.category + )) + # [END recognize_pii_entities_async] + print("All of the information that I expect to be redacted is!") + + print( + "Now I want to explicitly extract SSN information to add to my user SSN database. " + "I also want to be fairly confident that what I'm storing is an SSN, so let's also " + "ensure that we're > 60% positive the entity is a SSN" + ) + ssns = [] + for doc in docs: + for entity in doc.entities: + if entity.category == 'USSocialSecurityNumber' and entity.confidence_score >= 0.6: + ssns.append(entity.text) + + print("We have extracted the following SSNs as well: '{}'".format( + "', '".join(ssns) + )) async def main(): - sample = RecognizePiiEntitiesSampleAsync() - await sample.recognize_pii_entities_async() + await sample_recognize_pii_entities_async() if __name__ == '__main__': loop = asyncio.get_event_loop() diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_alternative_document_input.py b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_alternative_document_input.py index e1f569600aa4..1131c77d3d6e 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_alternative_document_input.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_alternative_document_input.py @@ -26,38 +26,36 @@ _LOGGER = logging.getLogger(__name__) -class AlternativeDocumentInputSample(object): - def alternative_document_input(self): - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics import TextAnalyticsClient +def sample_alternative_document_input(): + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics import TextAnalyticsClient - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) + text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) - documents = [ - {"id": "0", "country_hint": "US", "text": "I had the best day of my life. I decided to go sky-diving and it made me appreciate my whole life so much more. I developed a deep-connection with my instructor as well."}, - {"id": "1", "country_hint": "GB", - "text": "This was a waste of my time. The speaker put me to sleep."}, - {"id": "2", "country_hint": "MX", "text": "No tengo dinero ni nada que dar..."}, - {"id": "3", "country_hint": "FR", - "text": "L'hôtel n'était pas très confortable. L'éclairage était trop sombre."} - ] + documents = [ + {"id": "0", "country_hint": "US", "text": "I had the best day of my life. I decided to go sky-diving and it made me appreciate my whole life so much more. I developed a deep-connection with my instructor as well."}, + {"id": "1", "country_hint": "GB", + "text": "This was a waste of my time. The speaker put me to sleep."}, + {"id": "2", "country_hint": "MX", "text": "No tengo dinero ni nada que dar..."}, + {"id": "3", "country_hint": "FR", + "text": "L'hôtel n'était pas très confortable. L'éclairage était trop sombre."} + ] - result = text_analytics_client.detect_language(documents) + result = text_analytics_client.detect_language(documents) - for idx, doc in enumerate(result): - if not doc.is_error: - print("Document text: {}".format(documents[idx])) - print("Language detected: {}".format(doc.primary_language.name)) - print("ISO6391 name: {}".format(doc.primary_language.iso6391_name)) - print("Confidence score: {}\n".format(doc.primary_language.confidence_score)) - if doc.is_error: - print(doc.id, doc.error) + for idx, doc in enumerate(result): + if not doc.is_error: + print("Document text: {}".format(documents[idx])) + print("Language detected: {}".format(doc.primary_language.name)) + print("ISO6391 name: {}".format(doc.primary_language.iso6391_name)) + print("Confidence score: {}\n".format(doc.primary_language.confidence_score)) + if doc.is_error: + print(doc.id, doc.error) if __name__ == '__main__': - sample = AlternativeDocumentInputSample() - sample.alternative_document_input() + sample_alternative_document_input() diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_actions.py b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_actions.py index 16f431efb750..178381270a3c 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_actions.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_actions.py @@ -27,127 +27,124 @@ import os -class AnalyzeSample(object): - - def analyze(self): - # [START analyze] - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics import ( - TextAnalyticsClient, - RecognizeEntitiesAction, - RecognizeLinkedEntitiesAction, - RecognizePiiEntitiesAction, - ExtractKeyPhrasesAction, - AnalyzeSentimentAction, - ) - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - text_analytics_client = TextAnalyticsClient( - endpoint=endpoint, - credential=AzureKeyCredential(key), - ) - - documents = [ - '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!'\ - ] - - poller = text_analytics_client.begin_analyze_actions( - documents, - display_name="Sample Text Analysis", - actions=[ - RecognizeEntitiesAction(), - RecognizePiiEntitiesAction(), - ExtractKeyPhrasesAction(), - RecognizeLinkedEntitiesAction(), - AnalyzeSentimentAction() - ], - ) - - document_results = poller.result() - for doc, action_results in zip(documents, document_results): - print("\nDocument text: {}".format(doc)) - recognize_entities_result = action_results[0] - print("...Results of Recognize Entities Action:") - if recognize_entities_result.is_error: - print("...Is an error with code '{}' and message '{}'".format( - recognize_entities_result.code, recognize_entities_result.message - )) - else: - for entity in recognize_entities_result.entities: - print("......Entity: {}".format(entity.text)) - print(".........Category: {}".format(entity.category)) - print(".........Confidence Score: {}".format(entity.confidence_score)) - print(".........Offset: {}".format(entity.offset)) - - 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("......Entity: {}".format(entity.text)) - print(".........Category: {}".format(entity.category)) - print(".........Confidence Score: {}".format(entity.confidence_score)) - - extract_key_phrases_result = action_results[2] - print("...Results of Extract Key Phrases action:") - if extract_key_phrases_result.is_error: - print("...Is an error with code '{}' and message '{}'".format( - extract_key_phrases_result.code, extract_key_phrases_result.message - )) - else: - print("......Key Phrases: {}".format(extract_key_phrases_result.key_phrases)) - - recognize_linked_entities_result = action_results[3] - print("...Results of Recognize Linked Entities action:") - if recognize_linked_entities_result.is_error: - print("...Is an error with code '{}' and message '{}'".format( - recognize_linked_entities_result.code, recognize_linked_entities_result.message - )) - else: - for linked_entity in recognize_linked_entities_result.entities: - print("......Entity name: {}".format(linked_entity.name)) - print(".........Data source: {}".format(linked_entity.data_source)) - print(".........Data source language: {}".format(linked_entity.language)) - print(".........Data source entity ID: {}".format(linked_entity.data_source_entity_id)) - print(".........Data source URL: {}".format(linked_entity.url)) - print(".........Document matches:") - for match in linked_entity.matches: - print("............Match text: {}".format(match.text)) - print("............Confidence Score: {}".format(match.confidence_score)) - print("............Offset: {}".format(match.offset)) - print("............Length: {}".format(match.length)) - - analyze_sentiment_result = action_results[4] - print("...Results of Analyze Sentiment action:") - if analyze_sentiment_result.is_error: - print("...Is an error with code '{}' and message '{}'".format( - analyze_sentiment_result.code, analyze_sentiment_result.message - )) - else: - print("......Overall sentiment: {}".format(analyze_sentiment_result.sentiment)) - print("......Scores: positive={}; neutral={}; negative={} \n".format( - analyze_sentiment_result.confidence_scores.positive, - analyze_sentiment_result.confidence_scores.neutral, - analyze_sentiment_result.confidence_scores.negative, - )) - print("------------------------------------------") - - # [END analyze] +def sample_analyze_actions(): + # [START analyze] + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics import ( + TextAnalyticsClient, + RecognizeEntitiesAction, + RecognizeLinkedEntitiesAction, + RecognizePiiEntitiesAction, + ExtractKeyPhrasesAction, + AnalyzeSentimentAction, + ) + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient( + endpoint=endpoint, + credential=AzureKeyCredential(key), + ) + + documents = [ + '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!'\ + ] + + poller = text_analytics_client.begin_analyze_actions( + documents, + display_name="Sample Text Analysis", + actions=[ + RecognizeEntitiesAction(), + RecognizePiiEntitiesAction(), + ExtractKeyPhrasesAction(), + RecognizeLinkedEntitiesAction(), + AnalyzeSentimentAction() + ], + ) + + document_results = poller.result() + for doc, action_results in zip(documents, document_results): + print("\nDocument text: {}".format(doc)) + recognize_entities_result = action_results[0] + print("...Results of Recognize Entities Action:") + if recognize_entities_result.is_error: + print("...Is an error with code '{}' and message '{}'".format( + recognize_entities_result.code, recognize_entities_result.message + )) + else: + for entity in recognize_entities_result.entities: + print("......Entity: {}".format(entity.text)) + print(".........Category: {}".format(entity.category)) + print(".........Confidence Score: {}".format(entity.confidence_score)) + print(".........Offset: {}".format(entity.offset)) + + 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("......Entity: {}".format(entity.text)) + print(".........Category: {}".format(entity.category)) + print(".........Confidence Score: {}".format(entity.confidence_score)) + + extract_key_phrases_result = action_results[2] + print("...Results of Extract Key Phrases action:") + if extract_key_phrases_result.is_error: + print("...Is an error with code '{}' and message '{}'".format( + extract_key_phrases_result.code, extract_key_phrases_result.message + )) + else: + print("......Key Phrases: {}".format(extract_key_phrases_result.key_phrases)) + + recognize_linked_entities_result = action_results[3] + print("...Results of Recognize Linked Entities action:") + if recognize_linked_entities_result.is_error: + print("...Is an error with code '{}' and message '{}'".format( + recognize_linked_entities_result.code, recognize_linked_entities_result.message + )) + else: + for linked_entity in recognize_linked_entities_result.entities: + print("......Entity name: {}".format(linked_entity.name)) + print(".........Data source: {}".format(linked_entity.data_source)) + print(".........Data source language: {}".format(linked_entity.language)) + print(".........Data source entity ID: {}".format(linked_entity.data_source_entity_id)) + print(".........Data source URL: {}".format(linked_entity.url)) + print(".........Document matches:") + for match in linked_entity.matches: + print("............Match text: {}".format(match.text)) + print("............Confidence Score: {}".format(match.confidence_score)) + print("............Offset: {}".format(match.offset)) + print("............Length: {}".format(match.length)) + + analyze_sentiment_result = action_results[4] + print("...Results of Analyze Sentiment action:") + if analyze_sentiment_result.is_error: + print("...Is an error with code '{}' and message '{}'".format( + analyze_sentiment_result.code, analyze_sentiment_result.message + )) + else: + print("......Overall sentiment: {}".format(analyze_sentiment_result.sentiment)) + print("......Scores: positive={}; neutral={}; negative={} \n".format( + analyze_sentiment_result.confidence_scores.positive, + analyze_sentiment_result.confidence_scores.neutral, + analyze_sentiment_result.confidence_scores.negative, + )) + print("------------------------------------------") + + # [END analyze] if __name__ == "__main__": - sample = AnalyzeSample() - sample.analyze() + sample_analyze_actions() diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_healthcare_entities.py b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_healthcare_entities.py index 452ba2007acc..2910118097c2 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_healthcare_entities.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_healthcare_entities.py @@ -25,109 +25,105 @@ """ - -class AnalyzeHealthcareEntitiesSample(object): - - def analyze_healthcare_entities(self): - - print( - "In this sample we will be combing through the prescriptions our pharmacy has fulfilled " - "so we can catalog how much inventory we have" - ) - print( - "We start out with a list of prescription documents." - ) - - # [START analyze_healthcare_entities] - import os - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics import TextAnalyticsClient, HealthcareEntityRelationType, HealthcareEntityRelationRoleType - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_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_healthcare_entities(documents) - result = poller.result() - - docs = [doc for doc in result if not doc.is_error] - - print("Let's first visualize the outputted healthcare result:") - for idx, doc in enumerate(docs): - for entity in doc.entities: - print("Entity: {}".format(entity.text)) - print("...Normalized Text: {}".format(entity.normalized_text)) - print("...Category: {}".format(entity.category)) - print("...Subcategory: {}".format(entity.subcategory)) - print("...Offset: {}".format(entity.offset)) - print("...Confidence score: {}".format(entity.confidence_score)) - if entity.data_sources is not None: - print("...Data Sources:") - for data_source in entity.data_sources: - print("......Entity ID: {}".format(data_source.entity_id)) - print("......Name: {}".format(data_source.name)) - if entity.assertion is not None: - print("...Assertion:") - print("......Conditionality: {}".format(entity.assertion.conditionality)) - print("......Certainty: {}".format(entity.assertion.certainty)) - print("......Association: {}".format(entity.assertion.association)) - for relation in doc.entity_relations: - print("Relation of type: {} has the following roles".format(relation.relation_type)) - for role in relation.roles: - print("...Role '{}' with entity '{}'".format(role.name, role.entity.text)) - print("------------------------------------------") - - print("Now, let's get all of medication dosage relations from the documents") - dosage_of_medication_relations = [ - entity_relation - for doc in docs - for entity_relation in doc.entity_relations if entity_relation.relation_type == HealthcareEntityRelationType.DOSAGE_OF_MEDICATION - ] - # [END analyze_healthcare_entities] - - print( - "Now, I will create a dictionary of medication to total dosage. " - "I will use a regex to extract the dosage amount. For simplicity sake, I will assume " - "all dosages are represented with numbers and have mg unit." - ) - import re - from collections import defaultdict - - medication_to_dosage = defaultdict(int) - - for relation in dosage_of_medication_relations: - # The DosageOfMedication relation should only contain the dosage and medication roles - - dosage_role = next(iter(filter(lambda x: x.name == HealthcareEntityRelationRoleType.DOSAGE, relation.roles))) - medication_role = next(iter(filter(lambda x: x.name == HealthcareEntityRelationRoleType.MEDICATION, relation.roles))) - - try: - dosage_value = int(re.findall(r"\d+", dosage_role.entity.text)[0]) # we find the numbers in the dosage - medication_to_dosage[medication_role.entity.text] += dosage_value - except StopIteration: - # Error handling for if there's no dosage in numbers. - pass - - for medication, dosage in medication_to_dosage.items(): - print("We have fulfilled '{}' total mg of '{}'".format( - dosage, medication - )) +def sample_analyze_healthcare_entities(): + + print( + "In this sample we will be combing through the prescriptions our pharmacy has fulfilled " + "so we can catalog how much inventory we have" + ) + print( + "We start out with a list of prescription documents." + ) + + # [START analyze_healthcare_entities] + import os + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics import TextAnalyticsClient, HealthcareEntityRelationType, HealthcareEntityRelationRoleType + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_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_healthcare_entities(documents) + result = poller.result() + + docs = [doc for doc in result if not doc.is_error] + + print("Let's first visualize the outputted healthcare result:") + for idx, doc in enumerate(docs): + for entity in doc.entities: + print("Entity: {}".format(entity.text)) + print("...Normalized Text: {}".format(entity.normalized_text)) + print("...Category: {}".format(entity.category)) + print("...Subcategory: {}".format(entity.subcategory)) + print("...Offset: {}".format(entity.offset)) + print("...Confidence score: {}".format(entity.confidence_score)) + if entity.data_sources is not None: + print("...Data Sources:") + for data_source in entity.data_sources: + print("......Entity ID: {}".format(data_source.entity_id)) + print("......Name: {}".format(data_source.name)) + if entity.assertion is not None: + print("...Assertion:") + print("......Conditionality: {}".format(entity.assertion.conditionality)) + print("......Certainty: {}".format(entity.assertion.certainty)) + print("......Association: {}".format(entity.assertion.association)) + for relation in doc.entity_relations: + print("Relation of type: {} has the following roles".format(relation.relation_type)) + for role in relation.roles: + print("...Role '{}' with entity '{}'".format(role.name, role.entity.text)) + print("------------------------------------------") + + print("Now, let's get all of medication dosage relations from the documents") + dosage_of_medication_relations = [ + entity_relation + for doc in docs + for entity_relation in doc.entity_relations if entity_relation.relation_type == HealthcareEntityRelationType.DOSAGE_OF_MEDICATION + ] + # [END analyze_healthcare_entities] + + print( + "Now, I will create a dictionary of medication to total dosage. " + "I will use a regex to extract the dosage amount. For simplicity sake, I will assume " + "all dosages are represented with numbers and have mg unit." + ) + import re + from collections import defaultdict + + medication_to_dosage = defaultdict(int) + + for relation in dosage_of_medication_relations: + # The DosageOfMedication relation should only contain the dosage and medication roles + + dosage_role = next(iter(filter(lambda x: x.name == HealthcareEntityRelationRoleType.DOSAGE, relation.roles))) + medication_role = next(iter(filter(lambda x: x.name == HealthcareEntityRelationRoleType.MEDICATION, relation.roles))) + + try: + dosage_value = int(re.findall(r"\d+", dosage_role.entity.text)[0]) # we find the numbers in the dosage + medication_to_dosage[medication_role.entity.text] += dosage_value + except StopIteration: + # Error handling for if there's no dosage in numbers. + pass + + for medication, dosage in medication_to_dosage.items(): + print("We have fulfilled '{}' total mg of '{}'".format( + dosage, medication + )) if __name__ == "__main__": - sample = AnalyzeHealthcareEntitiesSample() - sample.analyze_healthcare_entities() + sample_analyze_healthcare_entities() diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_healthcare_entities_with_cancellation.py b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_healthcare_entities_with_cancellation.py index 850331275e2a..67faf378a1e0 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_healthcare_entities_with_cancellation.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_healthcare_entities_with_cancellation.py @@ -25,55 +25,52 @@ from azure.core.exceptions import HttpResponseError -class AnalyzeHealthcareEntitiesWithCancellationSample(object): - - def analyze_healthcare_entities_with_cancellation(self): - # [START analyze_healthcare_entities_with_cancellation] - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics import TextAnalyticsClient - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - text_analytics_client = TextAnalyticsClient( - endpoint=endpoint, - credential=AzureKeyCredential(key), - ) - - documents = [ - "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." - ] - - poller = text_analytics_client.begin_analyze_healthcare_entities(documents) - - try: - cancellation_poller = poller.cancel() - cancellation_poller.wait() - - except HttpResponseError as e: - # If the operation has already reached a terminal state it cannot be cancelled. - print(e) - - else: - print("Healthcare entities analysis was successfully cancelled.") - - # [END analyze_healthcare_entities_with_cancellation] +def sample_analyze_healthcare_entities_with_cancellation(): + # [START analyze_healthcare_entities_with_cancellation] + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics import TextAnalyticsClient + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient( + endpoint=endpoint, + credential=AzureKeyCredential(key), + ) + + documents = [ + "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." + ] + + poller = text_analytics_client.begin_analyze_healthcare_entities(documents) + + try: + cancellation_poller = poller.cancel() + cancellation_poller.wait() + + except HttpResponseError as e: + # If the operation has already reached a terminal state it cannot be cancelled. + print(e) + + else: + print("Healthcare entities analysis was successfully cancelled.") + + # [END analyze_healthcare_entities_with_cancellation] if __name__ == "__main__": - sample = AnalyzeHealthcareEntitiesWithCancellationSample() - sample.analyze_healthcare_entities_with_cancellation() + sample_analyze_healthcare_entities_with_cancellation() diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_sentiment.py b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_sentiment.py index 72440024bf70..5671e2c911ec 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_sentiment.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_sentiment.py @@ -28,84 +28,82 @@ import os -class AnalyzeSentimentSample(object): - - def analyze_sentiment(self): - print( - "In this sample we will be combing through reviews customers have left about their" - "experience using our skydiving company, Contoso." - ) - print( - "We start out with a list of reviews. Let us extract the reviews we are sure are " - "positive, so we can display them on our website and get even more customers!" - ) - - # [START analyze_sentiment] - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics import TextAnalyticsClient - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) - - documents = [ - """I had the best day of my life. I decided to go sky-diving and it made me appreciate my whole life so much more. - I developed a deep-connection with my instructor as well, and I feel as if I've made a life-long friend in her.""", - """This was a waste of my time. All of the views on this drop are extremely boring, all I saw was grass. 0/10 would - not recommend to any divers, even first timers.""", - """This was pretty good! The sights were ok, and I had fun with my instructors! Can't complain too much about my experience""", - """I only have one word for my experience: WOW!!! I can't believe I have had such a wonderful skydiving company right - in my backyard this whole time! I will definitely be a repeat customer, and I want to take my grandmother skydiving too, - I know she'll love it!""" - ] - - - result = text_analytics_client.analyze_sentiment(documents, show_opinion_mining=True) - docs = [doc for doc in result if not doc.is_error] - - print("Let's visualize the sentiment of each of these documents") - for idx, doc in enumerate(docs): - print("Document text: {}".format(documents[idx])) - print("Overall sentiment: {}".format(doc.sentiment)) - # [END analyze_sentiment] - - print("Now, let us extract all of the positive reviews") - positive_reviews = [doc for doc in docs if doc.sentiment == 'positive'] - - print("We want to be very confident that our reviews are positive since we'll be posting them on our website.") - print("We're going to confirm our chosen reviews are positive using two different tests") - - print( - "First, we are going to check how confident the sentiment analysis model is that a document is positive. " - "Let's go with a 90% confidence." - ) - positive_reviews = [ - review for review in positive_reviews - if review.confidence_scores.positive >= 0.9 - ] - - print( - "Finally, we also want to make sure every sentence is positive so we only showcase our best selves!" - ) - positive_reviews_final = [] - for idx, review in enumerate(positive_reviews): - print("Looking at positive review #{}".format(idx + 1)) - any_sentence_not_positive = False - for sentence in review.sentences: - print("...Sentence '{}' has sentiment '{}' with confidence scores '{}'".format( - sentence.text, - sentence.sentiment, - sentence.confidence_scores - ) +def sample_analyze_sentiment(): + print( + "In this sample we will be combing through reviews customers have left about their" + "experience using our skydiving company, Contoso." + ) + print( + "We start out with a list of reviews. Let us extract the reviews we are sure are " + "positive, so we can display them on our website and get even more customers!" + ) + + # [START analyze_sentiment] + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics import TextAnalyticsClient + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) + + documents = [ + """I had the best day of my life. I decided to go sky-diving and it made me appreciate my whole life so much more. + I developed a deep-connection with my instructor as well, and I feel as if I've made a life-long friend in her.""", + """This was a waste of my time. All of the views on this drop are extremely boring, all I saw was grass. 0/10 would + not recommend to any divers, even first timers.""", + """This was pretty good! The sights were ok, and I had fun with my instructors! Can't complain too much about my experience""", + """I only have one word for my experience: WOW!!! I can't believe I have had such a wonderful skydiving company right + in my backyard this whole time! I will definitely be a repeat customer, and I want to take my grandmother skydiving too, + I know she'll love it!""" + ] + + + result = text_analytics_client.analyze_sentiment(documents, show_opinion_mining=True) + docs = [doc for doc in result if not doc.is_error] + + print("Let's visualize the sentiment of each of these documents") + for idx, doc in enumerate(docs): + print("Document text: {}".format(documents[idx])) + print("Overall sentiment: {}".format(doc.sentiment)) + # [END analyze_sentiment] + + print("Now, let us extract all of the positive reviews") + positive_reviews = [doc for doc in docs if doc.sentiment == 'positive'] + + print("We want to be very confident that our reviews are positive since we'll be posting them on our website.") + print("We're going to confirm our chosen reviews are positive using two different tests") + + print( + "First, we are going to check how confident the sentiment analysis model is that a document is positive. " + "Let's go with a 90% confidence." + ) + positive_reviews = [ + review for review in positive_reviews + if review.confidence_scores.positive >= 0.9 + ] + + print( + "Finally, we also want to make sure every sentence is positive so we only showcase our best selves!" + ) + positive_reviews_final = [] + for idx, review in enumerate(positive_reviews): + print("Looking at positive review #{}".format(idx + 1)) + any_sentence_not_positive = False + for sentence in review.sentences: + print("...Sentence '{}' has sentiment '{}' with confidence scores '{}'".format( + sentence.text, + sentence.sentiment, + sentence.confidence_scores ) - if sentence.sentiment != 'positive': - any_sentence_not_positive = True - if not any_sentence_not_positive: - positive_reviews_final.append(review) + ) + if sentence.sentiment != 'positive': + any_sentence_not_positive = True + if not any_sentence_not_positive: + positive_reviews_final.append(review) + + print("We now have the final list of positive reviews we are going to display on our website!") - print("We now have the final list of positive reviews we are going to display on our website!") if __name__ == '__main__': - sample = AnalyzeSentimentSample() - sample.analyze_sentiment() + sample_analyze_sentiment() diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_sentiment_with_opinion_mining.py b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_sentiment_with_opinion_mining.py index 634d45125993..571afbc56cc6 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_sentiment_with_opinion_mining.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_analyze_sentiment_with_opinion_mining.py @@ -47,90 +47,88 @@ import os -class AnalyzeSentimentWithOpinionMiningSample(object): - def sample_analyze_sentiment_with_opinion_mining(self): - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics import TextAnalyticsClient - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - text_analytics_client = TextAnalyticsClient( - endpoint=endpoint, - credential=AzureKeyCredential(key) - ) - - print("In this sample we will be a hotel owner going through reviews of their hotel to find complaints.") - - print( - "I first found a handful of reviews for my hotel. Let's see what we have to improve." - ) - - documents = [ - """ - The food and service were unacceptable, but the concierge were nice. - After talking to them about the quality of the food and the process to get room service they refunded - the money we spent at the restaurant and gave us a voucher for near by restaurants. - """, - """ - The rooms were beautiful. The AC was good and quiet, which was key for us as outside it was 100F and our baby - was getting uncomfortable because of the heat. The breakfast was good too with good options and good servicing times. - The thing we didn't like was that the toilet in our bathroom was smelly. It could have been that the toilet was broken before we arrived. - Either way it was very uncomfortable. Once we notified the staff, they came and cleaned it and left candles. - """, - """ - Nice rooms! I had a great unobstructed view of the Microsoft campus but bathrooms were old and the toilet was dirty when we arrived. - It was close to bus stops and groceries stores. If you want to be close to campus I will recommend it, otherwise, might be better to stay in a cleaner one - """ - ] - - result = text_analytics_client.analyze_sentiment(documents, show_opinion_mining=True) - doc_result = [doc for doc in result if not doc.is_error] - - print("\nLet's first see the general sentiment of each of these reviews") - positive_reviews = [doc for doc in doc_result if doc.sentiment == "positive"] - mixed_reviews = [doc for doc in doc_result if doc.sentiment == "mixed"] - negative_reviews = [doc for doc in doc_result if doc.sentiment == "negative"] - print("...We have {} positive reviews, {} mixed reviews, and {} negative reviews. ".format( - len(positive_reviews), len(mixed_reviews), len(negative_reviews) +def sample_analyze_sentiment_with_opinion_mining(): + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics import TextAnalyticsClient + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient( + endpoint=endpoint, + credential=AzureKeyCredential(key) + ) + + print("In this sample we will be a hotel owner going through reviews of their hotel to find complaints.") + + print( + "I first found a handful of reviews for my hotel. Let's see what we have to improve." + ) + + documents = [ + """ + The food and service were unacceptable, but the concierge were nice. + After talking to them about the quality of the food and the process to get room service they refunded + the money we spent at the restaurant and gave us a voucher for near by restaurants. + """, + """ + The rooms were beautiful. The AC was good and quiet, which was key for us as outside it was 100F and our baby + was getting uncomfortable because of the heat. The breakfast was good too with good options and good servicing times. + The thing we didn't like was that the toilet in our bathroom was smelly. It could have been that the toilet was broken before we arrived. + Either way it was very uncomfortable. Once we notified the staff, they came and cleaned it and left candles. + """, + """ + Nice rooms! I had a great unobstructed view of the Microsoft campus but bathrooms were old and the toilet was dirty when we arrived. + It was close to bus stops and groceries stores. If you want to be close to campus I will recommend it, otherwise, might be better to stay in a cleaner one + """ + ] + + result = text_analytics_client.analyze_sentiment(documents, show_opinion_mining=True) + doc_result = [doc for doc in result if not doc.is_error] + + print("\nLet's first see the general sentiment of each of these reviews") + positive_reviews = [doc for doc in doc_result if doc.sentiment == "positive"] + mixed_reviews = [doc for doc in doc_result if doc.sentiment == "mixed"] + negative_reviews = [doc for doc in doc_result if doc.sentiment == "negative"] + print("...We have {} positive reviews, {} mixed reviews, and {} negative reviews. ".format( + len(positive_reviews), len(mixed_reviews), len(negative_reviews) + )) + print( + "\nSince these reviews seem so mixed, and since I'm interested in finding exactly what it is about my hotel that should be improved, " + "let's find the complaints users have about individual aspects of this hotel" + ) + + print( + "\nIn order to do that, I'm going to extract targets of a negative sentiment. " + "I'm going to map each of these targets to the mined opinion object we get back to aggregate the reviews by target. " + ) + target_to_complaints = {} + + for document in doc_result: + for sentence in document.sentences: + for mined_opinion in sentence.mined_opinions: + target = mined_opinion.target + if target.sentiment == 'negative': + target_to_complaints.setdefault(target.text, []) + target_to_complaints[target.text].append(mined_opinion) + + print("\nLet's now go through the aspects of our hotel people have complained about and see what users have specifically said") + + for target, complaints in target_to_complaints.items(): + print("Users have made {} complaint(s) about '{}', specifically saying that it's '{}'".format( + len(complaints), + target, + "', '".join( + [assessment.text for complaint in complaints for assessment in complaint.assessments] + ) )) - print( - "\nSince these reviews seem so mixed, and since I'm interested in finding exactly what it is about my hotel that should be improved, " - "let's find the complaints users have about individual aspects of this hotel" - ) - - print( - "\nIn order to do that, I'm going to extract targets of a negative sentiment. " - "I'm going to map each of these targets to the mined opinion object we get back to aggregate the reviews by target. " - ) - target_to_complaints = {} - - for document in doc_result: - for sentence in document.sentences: - for mined_opinion in sentence.mined_opinions: - target = mined_opinion.target - if target.sentiment == 'negative': - target_to_complaints.setdefault(target.text, []) - target_to_complaints[target.text].append(mined_opinion) - - print("\nLet's now go through the aspects of our hotel people have complained about and see what users have specifically said") - - for target, complaints in target_to_complaints.items(): - print("Users have made {} complaint(s) about '{}', specifically saying that it's '{}'".format( - len(complaints), - target, - "', '".join( - [assessment.text for complaint in complaints for assessment in complaint.assessments] - ) - )) - - - print( - "\n\nLooking at the breakdown, I can see what aspects of my hotel need improvement, and based off of both the number and " - "content of the complaints users have made about my toilets, I need to get that fixed ASAP." - ) + + + print( + "\n\nLooking at the breakdown, I can see what aspects of my hotel need improvement, and based off of both the number and " + "content of the complaints users have made about my toilets, I need to get that fixed ASAP." + ) if __name__ == '__main__': - sample = AnalyzeSentimentWithOpinionMiningSample() - sample.sample_analyze_sentiment_with_opinion_mining() + sample_analyze_sentiment_with_opinion_mining() diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_authentication.py b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_authentication.py index b369ff0ecc6c..58f2f1f62807 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_authentication.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_authentication.py @@ -33,58 +33,55 @@ import os -class AuthenticationSample(object): - - def authentication_with_api_key_credential(self): - print("\n.. authentication_with_api_key_credential") - # [START create_ta_client_with_key] - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics import TextAnalyticsClient - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - text_analytics_client = TextAnalyticsClient(endpoint, AzureKeyCredential(key)) - # [END create_ta_client_with_key] - - doc = [ - """ - I need to take my cat to the veterinarian. She's been coughing for a while and I thought it was just a hairball, - but now I'm now worried it might be something else. She's still very healthy so I'm not too worried though. - """ - ] - result = text_analytics_client.detect_language(doc) - - print("Language detected: {}".format(result[0].primary_language.name)) - print("Confidence score: {}".format(result[0].primary_language.confidence_score)) - - def authentication_with_azure_active_directory(self): - """DefaultAzureCredential will use the values from these environment - variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET +def sample_authentication_with_api_key_credential(): + print("\n.. authentication_with_api_key_credential") + # [START create_ta_client_with_key] + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics import TextAnalyticsClient + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient(endpoint, AzureKeyCredential(key)) + # [END create_ta_client_with_key] + + doc = [ """ - print("\n.. authentication_with_azure_active_directory") - # [START create_ta_client_with_aad] - from azure.ai.textanalytics import TextAnalyticsClient - from azure.identity import DefaultAzureCredential + I need to take my cat to the veterinarian. She's been coughing for a while and I thought it was just a hairball, + but now I'm now worried it might be something else. She's still very healthy so I'm not too worried though. + """ + ] + result = text_analytics_client.detect_language(doc) + + print("Language detected: {}".format(result[0].primary_language.name)) + print("Confidence score: {}".format(result[0].primary_language.confidence_score)) + +def sample_authentication_with_azure_active_directory(): + """DefaultAzureCredential will use the values from these environment + variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET + """ + print("\n.. authentication_with_azure_active_directory") + # [START create_ta_client_with_aad] + from azure.ai.textanalytics import TextAnalyticsClient + from azure.identity import DefaultAzureCredential - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - credential = DefaultAzureCredential() + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + credential = DefaultAzureCredential() - text_analytics_client = TextAnalyticsClient(endpoint, credential=credential) - # [END create_ta_client_with_aad] + text_analytics_client = TextAnalyticsClient(endpoint, credential=credential) + # [END create_ta_client_with_aad] - doc = [ - """ - I need to take my cat to the veterinarian. She's been coughing for a while and I thought it was just a hairball, - but now I'm now worried it might be something else. She's still very healthy so I'm not too worried though. - """ - ] - result = text_analytics_client.detect_language(doc) + doc = [ + """ + I need to take my cat to the veterinarian. She's been coughing for a while and I thought it was just a hairball, + but now I'm now worried it might be something else. She's still very healthy so I'm not too worried though. + """ + ] + result = text_analytics_client.detect_language(doc) - print("Language detected: {}".format(result[0].primary_language.name)) - print("Confidence score: {}".format(result[0].primary_language.confidence_score)) + print("Language detected: {}".format(result[0].primary_language.name)) + print("Confidence score: {}".format(result[0].primary_language.confidence_score)) if __name__ == '__main__': - sample = AuthenticationSample() - sample.authentication_with_api_key_credential() - sample.authentication_with_azure_active_directory() + sample_authentication_with_api_key_credential() + sample_authentication_with_azure_active_directory() diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_detect_language.py b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_detect_language.py index d0e006c40f1e..cc181dd6a846 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_detect_language.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_detect_language.py @@ -28,55 +28,52 @@ import os -class DetectLanguageSample(object): - - def detect_language(self): - print( - "In this sample we own a hotel with customers from all around the globe. We want to eventually " - "translate these reviews into English so our manager can read them. However, we first need to know which language " - "they are in for more accurate translation. This is the step we will be covering in this sample\n" - ) - # [START detect_language] - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics import TextAnalyticsClient - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) - documents = [ - """ - The concierge Paulette was extremely helpful. Sadly when we arrived the elevator was broken, but with Paulette's help we barely noticed this inconvenience. - She arranged for our baggage to be brought up to our room with no extra charge and gave us a free meal to refurbish all of the calories we lost from - walking up the stairs :). Can't say enough good things about my experience! - """, - """ - 最近由于工作压力太大,我们决定去富酒店度假。那儿的温泉实在太舒服了,我跟我丈夫都完全恢复了工作前的青春精神!加油! - """ - ] - - result = text_analytics_client.detect_language(documents) - reviewed_docs = [doc for doc in result if not doc.is_error] - - print("Let's see what language each review is in!") - - for idx, doc in enumerate(reviewed_docs): - print("Review #{} is in '{}', which has ISO639-1 name '{}'\n".format( - idx, doc.primary_language.name, doc.primary_language.iso6391_name - )) - if doc.is_error: - print(doc.id, doc.error) - # [END detect_language] - print( - "When actually storing the reviews, we want to map the review to their ISO639-1 name " - "so everything is more standardized" - ) - - review_to_language = {} - for idx, doc in enumerate(reviewed_docs): - review_to_language[documents[idx]] = doc.primary_language.iso6391_name +def sample_detect_language(): + print( + "In this sample we own a hotel with customers from all around the globe. We want to eventually " + "translate these reviews into English so our manager can read them. However, we first need to know which language " + "they are in for more accurate translation. This is the step we will be covering in this sample\n" + ) + # [START detect_language] + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics import TextAnalyticsClient + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) + documents = [ + """ + The concierge Paulette was extremely helpful. Sadly when we arrived the elevator was broken, but with Paulette's help we barely noticed this inconvenience. + She arranged for our baggage to be brought up to our room with no extra charge and gave us a free meal to refurbish all of the calories we lost from + walking up the stairs :). Can't say enough good things about my experience! + """, + """ + 最近由于工作压力太大,我们决定去富酒店度假。那儿的温泉实在太舒服了,我跟我丈夫都完全恢复了工作前的青春精神!加油! + """ + ] + + result = text_analytics_client.detect_language(documents) + reviewed_docs = [doc for doc in result if not doc.is_error] + + print("Let's see what language each review is in!") + + for idx, doc in enumerate(reviewed_docs): + print("Review #{} is in '{}', which has ISO639-1 name '{}'\n".format( + idx, doc.primary_language.name, doc.primary_language.iso6391_name + )) + if doc.is_error: + print(doc.id, doc.error) + # [END detect_language] + print( + "When actually storing the reviews, we want to map the review to their ISO639-1 name " + "so everything is more standardized" + ) + + review_to_language = {} + for idx, doc in enumerate(reviewed_docs): + review_to_language[documents[idx]] = doc.primary_language.iso6391_name if __name__ == '__main__': - sample = DetectLanguageSample() - sample.detect_language() + sample_detect_language() diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_extract_key_phrases.py b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_extract_key_phrases.py index 58fd2d15b728..c83e096373e9 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_extract_key_phrases.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_extract_key_phrases.py @@ -26,55 +26,52 @@ import os -class ExtractKeyPhrasesSample(object): - - def extract_key_phrases(self): - print( - "In this sample, we want to find the articles that mention Microsoft to read." - ) - articles_that_mention_microsoft = [] - # [START extract_key_phrases] - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics import TextAnalyticsClient - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) - articles = [ - """ - Washington, D.C. Autumn in DC is a uniquely beautiful season. The leaves fall from the trees - in a city chockful of forrests, leaving yellow leaves on the ground and a clearer view of the - blue sky above... - """, - """ - Redmond, WA. In the past few days, Microsoft has decided to further postpone the start date of - its United States workers, due to the pandemic that rages with no end in sight... - """, - """ - Redmond, WA. Employees at Microsoft can be excited about the new coffee shop that will open on campus - once workers no longer have to work remotely... - """ - ] - - result = text_analytics_client.extract_key_phrases(articles) - for idx, doc in enumerate(result): - if not doc.is_error: - print("Key phrases in article #{}: {}".format( - idx + 1, - ", ".join(doc.key_phrases) - )) - # [END extract_key_phrases] - if "Microsoft" in doc.key_phrases: - articles_that_mention_microsoft.append(str(idx + 1)) - - print( - "The articles that mention Microsoft are articles number: {}. Those are the ones I'm interested in reading.".format( - ", ".join(articles_that_mention_microsoft) - ) +def sample_extract_key_phrases(): + print( + "In this sample, we want to find the articles that mention Microsoft to read." + ) + articles_that_mention_microsoft = [] + # [START extract_key_phrases] + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics import TextAnalyticsClient + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) + articles = [ + """ + Washington, D.C. Autumn in DC is a uniquely beautiful season. The leaves fall from the trees + in a city chockful of forrests, leaving yellow leaves on the ground and a clearer view of the + blue sky above... + """, + """ + Redmond, WA. In the past few days, Microsoft has decided to further postpone the start date of + its United States workers, due to the pandemic that rages with no end in sight... + """, + """ + Redmond, WA. Employees at Microsoft can be excited about the new coffee shop that will open on campus + once workers no longer have to work remotely... + """ + ] + + result = text_analytics_client.extract_key_phrases(articles) + for idx, doc in enumerate(result): + if not doc.is_error: + print("Key phrases in article #{}: {}".format( + idx + 1, + ", ".join(doc.key_phrases) + )) + # [END extract_key_phrases] + if "Microsoft" in doc.key_phrases: + articles_that_mention_microsoft.append(str(idx + 1)) + + print( + "The articles that mention Microsoft are articles number: {}. Those are the ones I'm interested in reading.".format( + ", ".join(articles_that_mention_microsoft) ) + ) if __name__ == '__main__': - sample = ExtractKeyPhrasesSample() - sample.extract_key_phrases() + sample_extract_key_phrases() diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_get_detailed_diagnostics_information.py b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_get_detailed_diagnostics_information.py index a67e879a84ad..a880d56b658a 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_get_detailed_diagnostics_information.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_get_detailed_diagnostics_information.py @@ -27,50 +27,48 @@ _LOGGER = logging.getLogger(__name__) -class GetDetailedDiagnosticsInformationSample(object): - - def get_detailed_diagnostics_information(self): - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics import TextAnalyticsClient - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - # This client will log detailed information about its HTTP sessions, at DEBUG level - text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key), logging_enable=True) - - documents = [ - """I had the best day of my life. I decided to go sky-diving and it made me appreciate my whole life so much more. - I developed a deep-connection with my instructor as well. - """, - "This was a waste of my time. The speaker put me to sleep.", - "No tengo dinero ni nada que dar...", - "L'hôtel n'était pas très confortable. L'éclairage était trop sombre." - ] - - json_responses = [] - - def callback(resp): - _LOGGER.debug("document_count: {}".format(resp.statistics["document_count"])) - _LOGGER.debug("valid_document_count: {}".format(resp.statistics["valid_document_count"])) - _LOGGER.debug("erroneous_document_count: {}".format(resp.statistics["erroneous_document_count"])) - _LOGGER.debug("transaction_count: {}".format(resp.statistics["transaction_count"])) - _LOGGER.debug("model_version: {}".format(resp.model_version)) - json_response = json.dumps(resp.raw_response) - json_responses.append(json_response) - - result = text_analytics_client.extract_key_phrases( - documents, - show_stats=True, - model_version="latest", - raw_response_hook=callback - ) - for doc in result: - _LOGGER.warning("Doc with id {} has these warnings: {}".format(doc.id, doc.warnings)) - - _LOGGER.debug("json response: {}".format(json_responses[0])) + +def sample_get_detailed_diagnostics_information(): + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics import TextAnalyticsClient + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + # This client will log detailed information about its HTTP sessions, at DEBUG level + text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key), logging_enable=True) + + documents = [ + """I had the best day of my life. I decided to go sky-diving and it made me appreciate my whole life so much more. + I developed a deep-connection with my instructor as well. + """, + "This was a waste of my time. The speaker put me to sleep.", + "No tengo dinero ni nada que dar...", + "L'hôtel n'était pas très confortable. L'éclairage était trop sombre." + ] + + json_responses = [] + + def callback(resp): + _LOGGER.debug("document_count: {}".format(resp.statistics["document_count"])) + _LOGGER.debug("valid_document_count: {}".format(resp.statistics["valid_document_count"])) + _LOGGER.debug("erroneous_document_count: {}".format(resp.statistics["erroneous_document_count"])) + _LOGGER.debug("transaction_count: {}".format(resp.statistics["transaction_count"])) + _LOGGER.debug("model_version: {}".format(resp.model_version)) + json_response = json.dumps(resp.raw_response) + json_responses.append(json_response) + + result = text_analytics_client.extract_key_phrases( + documents, + show_stats=True, + model_version="latest", + raw_response_hook=callback + ) + for doc in result: + _LOGGER.warning("Doc with id {} has these warnings: {}".format(doc.id, doc.warnings)) + + _LOGGER.debug("json response: {}".format(json_responses[0])) if __name__ == '__main__': - sample = GetDetailedDiagnosticsInformationSample() - sample.get_detailed_diagnostics_information() + sample_get_detailed_diagnostics_information() diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_recognize_entities.py b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_recognize_entities.py index ad43a5a025c8..4c087489c069 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_recognize_entities.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_recognize_entities.py @@ -25,50 +25,48 @@ import os -class RecognizeEntitiesSample(object): - def recognize_entities(self): +def sample_recognize_entities(): + print( + "In this sample, we are a catering business, and we're looking to sort the reviews " + "for our organization based off of the organization that hired us for catering" + ) + organization_to_reviews = {} + # [START recognize_entities] + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics import TextAnalyticsClient + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) + reviews = [ + """I work for Foo Company, and we hired Contoso for our annual founding ceremony. The food + was amazing and we all can't say enough good words about the quality and the level of service.""", + """We at the Foo Company re-hired Contoso after all of our past successes with the company. + Though the food was still great, I feel there has been a quality drop since their last time + catering for us. Is anyone else running into the same problem?""", + """Bar Company is over the moon about the service we received from Contoso, the best sliders ever!!!!""" + ] + + result = text_analytics_client.recognize_entities(reviews) + result = [review for review in result if not review.is_error] + + for idx, review in enumerate(result): + for entity in review.entities: + print("Entity '{}' has category '{}'".format(entity.text, entity.category)) + # [END recognize_entities] + if entity.category == 'Organization': + organization_to_reviews.setdefault(entity.text, []) + organization_to_reviews[entity.text].append(reviews[idx]) + + for organization, reviews in organization_to_reviews.items(): print( - "In this sample, we are a catering business, and we're looking to sort the reviews " - "for our organization based off of the organization that hired us for catering" - ) - organization_to_reviews = {} - # [START recognize_entities] - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics import TextAnalyticsClient - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) - reviews = [ - """I work for Foo Company, and we hired Contoso for our annual founding ceremony. The food - was amazing and we all can't say enough good words about the quality and the level of service.""", - """We at the Foo Company re-hired Contoso after all of our past successes with the company. - Though the food was still great, I feel there has been a quality drop since their last time - catering for us. Is anyone else running into the same problem?""", - """Bar Company is over the moon about the service we received from Contoso, the best sliders ever!!!!""" - ] - - result = text_analytics_client.recognize_entities(reviews) - result = [review for review in result if not review.is_error] - - for idx, review in enumerate(result): - for entity in review.entities: - print("Entity '{}' has category '{}'".format(entity.text, entity.category)) - # [END recognize_entities] - if entity.category == 'Organization': - organization_to_reviews.setdefault(entity.text, []) - organization_to_reviews[entity.text].append(reviews[idx]) - - for organization, reviews in organization_to_reviews.items(): - print( - "\n\nOrganization '{}' has left us the following review(s): {}".format( - organization, "\n\n".join(reviews) - ) + "\n\nOrganization '{}' has left us the following review(s): {}".format( + organization, "\n\n".join(reviews) ) + ) if __name__ == '__main__': - sample = RecognizeEntitiesSample() - sample.recognize_entities() + sample_recognize_entities() diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_recognize_linked_entities.py b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_recognize_linked_entities.py index 20a6aeb05a57..ee870dfa3496 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_recognize_linked_entities.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_recognize_linked_entities.py @@ -29,56 +29,54 @@ import os -class RecognizeLinkedEntitiesSample(object): - - def recognize_linked_entities(self): - print( - "In this sample, we are students conducting research for a class project. We will extract " - "links to Wikipedia articles for all entities listed in our research documents, so we have " - "all of the necessary information for research purposes." - ) - # [START recognize_linked_entities] - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics import TextAnalyticsClient - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) - documents = [ - """ - Microsoft was founded by Bill Gates with some friends he met at Harvard. One of his friends, - Steve Ballmer, eventually became CEO after Bill Gates as well. Steve Ballmer eventually stepped - down as CEO of Microsoft, and was succeeded by Satya Nadella. - Microsoft originally moved its headquarters to Bellevue, Wahsington in Januaray 1979, but is now - headquartered in Redmond. - """ - ] - - result = text_analytics_client.recognize_linked_entities(documents) - docs = [doc for doc in result if not doc.is_error] - - print( - "Let's map each entity to it's Wikipedia article. I also want to see how many times each " - "entity is mentioned in a document\n\n" - ) - entity_to_url = {} - for doc in docs: - for entity in doc.entities: - print("Entity '{}' has been mentioned '{}' time(s)".format( - entity.name, len(entity.matches) - )) - if entity.data_source == "Wikipedia": - entity_to_url[entity.name] = entity.url - # [END recognize_linked_entities] - - print("\nNow let's see all of the Wikipedia articles we've extracted from our research documents") - for entity, url in entity_to_url.items(): - print("Link to Wikipedia article for '{}': {}".format( - entity, url + +def sample_recognize_linked_entities(): + print( + "In this sample, we are students conducting research for a class project. We will extract " + "links to Wikipedia articles for all entities listed in our research documents, so we have " + "all of the necessary information for research purposes." + ) + # [START recognize_linked_entities] + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics import TextAnalyticsClient + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=AzureKeyCredential(key)) + documents = [ + """ + Microsoft was founded by Bill Gates with some friends he met at Harvard. One of his friends, + Steve Ballmer, eventually became CEO after Bill Gates as well. Steve Ballmer eventually stepped + down as CEO of Microsoft, and was succeeded by Satya Nadella. + Microsoft originally moved its headquarters to Bellevue, Wahsington in Januaray 1979, but is now + headquartered in Redmond. + """ + ] + + result = text_analytics_client.recognize_linked_entities(documents) + docs = [doc for doc in result if not doc.is_error] + + print( + "Let's map each entity to it's Wikipedia article. I also want to see how many times each " + "entity is mentioned in a document\n\n" + ) + entity_to_url = {} + for doc in docs: + for entity in doc.entities: + print("Entity '{}' has been mentioned '{}' time(s)".format( + entity.name, len(entity.matches) )) + if entity.data_source == "Wikipedia": + entity_to_url[entity.name] = entity.url + # [END recognize_linked_entities] + + print("\nNow let's see all of the Wikipedia articles we've extracted from our research documents") + for entity, url in entity_to_url.items(): + print("Link to Wikipedia article for '{}': {}".format( + entity, url + )) if __name__ == '__main__': - sample = RecognizeLinkedEntitiesSample() - sample.recognize_linked_entities() + sample_recognize_linked_entities() diff --git a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_recognize_pii_entities.py b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_recognize_pii_entities.py index 2fc5a1825656..b25d351a6795 100644 --- a/sdk/textanalytics/azure-ai-textanalytics/samples/sample_recognize_pii_entities.py +++ b/sdk/textanalytics/azure-ai-textanalytics/samples/sample_recognize_pii_entities.py @@ -27,65 +27,62 @@ import os -class RecognizePiiEntitiesSample(object): - - def recognize_pii_entities(self): - print( - "In this sample we will be going through our customer's loan payment information and redacting " - "all PII (personally identifable information) before storing this information on our public website. " - "I'm also looking to explicitly extract the SSN information, so I can update my database with SSNs for " - "our customers" - ) - # [START recognize_pii_entities] - from azure.core.credentials import AzureKeyCredential - from azure.ai.textanalytics import TextAnalyticsClient - - endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] - key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] - - text_analytics_client = TextAnalyticsClient( - endpoint=endpoint, credential=AzureKeyCredential(key) - ) - documents = [ - """Parker Doe has repaid all of their loans as of 2020-04-25. - Their SSN is 859-98-0987. To contact them, use their phone number - 555-555-5555. They are originally from Brazil and have Brazilian CPF number 998.214.865-68""" - ] - - result = text_analytics_client.recognize_pii_entities(documents) - docs = [doc for doc in result if not doc.is_error] - - print( - "Let's compare the original document with the documents after redaction. " - "I also want to comb through all of the entities that got redacted" - ) - for idx, doc in enumerate(docs): - print("Document text: {}".format(documents[idx])) - print("Redacted document text: {}".format(doc.redacted_text)) - for entity in doc.entities: - print("...Entity '{}' with category '{}' got redacted".format( - entity.text, entity.category - )) - - # [END recognize_pii_entities] - print("All of the information that I expect to be redacted is!") - - print( - "Now I want to explicitly extract SSN information to add to my user SSN database. " - "I also want to be fairly confident that what I'm storing is an SSN, so let's also " - "ensure that we're > 60% positive the entity is a SSN" - ) - ssns = [] - for doc in docs: - for entity in doc.entities: - if entity.category == 'USSocialSecurityNumber' and entity.confidence_score >= 0.6: - ssns.append(entity.text) - - print("We have extracted the following SSNs as well: '{}'".format( - "', '".join(ssns) - )) +def sample_recognize_pii_entities(): + print( + "In this sample we will be going through our customer's loan payment information and redacting " + "all PII (personally identifable information) before storing this information on our public website. " + "I'm also looking to explicitly extract the SSN information, so I can update my database with SSNs for " + "our customers" + ) + # [START recognize_pii_entities] + from azure.core.credentials import AzureKeyCredential + from azure.ai.textanalytics import TextAnalyticsClient + + endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] + key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] + + text_analytics_client = TextAnalyticsClient( + endpoint=endpoint, credential=AzureKeyCredential(key) + ) + documents = [ + """Parker Doe has repaid all of their loans as of 2020-04-25. + Their SSN is 859-98-0987. To contact them, use their phone number + 555-555-5555. They are originally from Brazil and have Brazilian CPF number 998.214.865-68""" + ] + + result = text_analytics_client.recognize_pii_entities(documents) + docs = [doc for doc in result if not doc.is_error] + + print( + "Let's compare the original document with the documents after redaction. " + "I also want to comb through all of the entities that got redacted" + ) + for idx, doc in enumerate(docs): + print("Document text: {}".format(documents[idx])) + print("Redacted document text: {}".format(doc.redacted_text)) + for entity in doc.entities: + print("...Entity '{}' with category '{}' got redacted".format( + entity.text, entity.category + )) + + # [END recognize_pii_entities] + print("All of the information that I expect to be redacted is!") + + print( + "Now I want to explicitly extract SSN information to add to my user SSN database. " + "I also want to be fairly confident that what I'm storing is an SSN, so let's also " + "ensure that we're > 60% positive the entity is a SSN" + ) + ssns = [] + for doc in docs: + for entity in doc.entities: + if entity.category == 'USSocialSecurityNumber' and entity.confidence_score >= 0.6: + ssns.append(entity.text) + + print("We have extracted the following SSNs as well: '{}'".format( + "', '".join(ssns) + )) if __name__ == '__main__': - sample = RecognizePiiEntitiesSample() - sample.recognize_pii_entities() + sample_recognize_pii_entities()