@@ -506,10 +506,7 @@ from azure.core.credentials import AzureKeyCredential
506506from azure.ai.textanalytics import (
507507 TextAnalyticsClient,
508508 RecognizeEntitiesAction,
509- RecognizePiiEntitiesAction,
510- ExtractKeyPhrasesAction,
511- RecognizeLinkedEntitiesAction,
512- AnalyzeSentimentAction
509+ AnalyzeSentimentAction,
513510)
514511
515512credential = AzureKeyCredential(" <api_key>" )
@@ -524,81 +521,39 @@ poller = text_analytics_client.begin_analyze_actions(
524521 display_name = " Sample Text Analysis" ,
525522 actions = [
526523 RecognizeEntitiesAction(),
527- RecognizePiiEntitiesAction(),
528- ExtractKeyPhrasesAction(),
529- RecognizeLinkedEntitiesAction(),
530524 AnalyzeSentimentAction()
531525 ]
532526)
533527
534528# returns multiple actions results in the same order as the inputted actions
535- result = poller.result()
536-
537- first_action_result = next (result)
538- print (" Results of Entities Recognition action:" )
539- docs = [doc for doc in first_action_result.document_results if not doc.is_error]
540-
541- for idx, doc in enumerate (docs):
542- print (" \n Document text: {} " .format(documents[idx]))
543- for entity in doc.entities:
544- print (" Entity: {} " .format(entity.text))
545- print (" ...Category: {} " .format(entity.category))
546- print (" ...Confidence Score: {} " .format(entity.confidence_score))
547- print (" ...Offset: {} " .format(entity.offset))
548- print (" ------------------------------------------" )
549-
550- second_action_result = next (result)
551- print (" Results of PII Entities Recognition action:" )
552- docs = [doc for doc in second_action_result.document_results if not doc.is_error]
553-
554- for idx, doc in enumerate (docs):
555- print (" Document text: {} " .format(documents[idx]))
556- for entity in doc.entities:
557- print (" Entity: {} " .format(entity.text))
558- print (" Category: {} " .format(entity.category))
559- print (" Confidence Score: {} \n " .format(entity.confidence_score))
560- print (" ------------------------------------------" )
561-
562- third_action_result = next (result)
563- print (" Results of Key Phrase Extraction action:" )
564- docs = [doc for doc in third_action_result.document_results if not doc.is_error]
565-
566- for idx, doc in enumerate (docs):
567- print (" Document text: {} \n " .format(documents[idx]))
568- print (" Key Phrases: {} \n " .format(doc.key_phrases))
569- print (" ------------------------------------------" )
570-
571- fourth_action_result = next (result)
572- print (" Results of Linked Entities Recognition action:" )
573- docs = [doc for doc in fourth_action_result.document_results if not doc.is_error]
574-
575- for idx, doc in enumerate (docs):
576- print (" Document text: {} \n " .format(documents[idx]))
577- for linked_entity in doc.entities:
578- print (" Entity name: {} " .format(linked_entity.name))
579- print (" ...Data source: {} " .format(linked_entity.data_source))
580- print (" ...Data source language: {} " .format(linked_entity.language))
581- print (" ...Data source entity ID: {} " .format(linked_entity.data_source_entity_id))
582- print (" ...Data source URL: {} " .format(linked_entity.url))
583- print (" ...Document matches:" )
584- for match in linked_entity.matches:
585- print (" ......Match text: {} " .format(match.text))
586- print (" .........Confidence Score: {} " .format(match.confidence_score))
587- print (" .........Offset: {} " .format(match.offset))
588- print (" .........Length: {} " .format(match.length))
589- print (" ------------------------------------------" )
590-
591- fifth_action_result = next (result)
592- print (" Results of Sentiment Analysis action:" )
593- docs = [doc for doc in fifth_action_result.document_results if not doc.is_error]
594-
595- for doc in docs:
596- print (" Overall sentiment: {} " .format(doc.sentiment))
597- print (" Scores: positive={} ; neutral={} ; negative={} \n " .format(
598- doc.confidence_scores.positive,
599- doc.confidence_scores.neutral,
600- doc.confidence_scores.negative,
601- ))
529+ document_results = poller.result()
530+ for doc, action_results in zip (documents, document_results):
531+ recognize_entities_result, analyze_sentiment_result = action_results
532+ print (" \n Document text: {} " .format(doc))
533+ print (" ...Results of Recognize Entities Action:" )
534+ if recognize_entities_result.is_error:
535+ print (" ......Is an error with code '{} ' and message '{} '" .format(
536+ recognize_entities_result.code, recognize_entities_result.message
537+ ))
538+ else :
539+ for entity in recognize_entities_result.entities:
540+ print (" ......Entity: {} " .format(entity.text))
541+ print (" .........Category: {} " .format(entity.category))
542+ print (" .........Confidence Score: {} " .format(entity.confidence_score))
543+ print (" .........Offset: {} " .format(entity.offset))
544+
545+ print (" ...Results of Analyze Sentiment action:" )
546+ if analyze_sentiment_result.is_error:
547+ print (" ......Is an error with code '{} ' and message '{} '" .format(
548+ analyze_sentiment_result.code, analyze_sentiment_result.message
549+ ))
550+ else :
551+ print (" ......Overall sentiment: {} " .format(analyze_sentiment_result.sentiment))
552+ print (" ......Scores: positive={} ; neutral={} ; negative={} \n " .format(
553+ analyze_sentiment_result.confidence_scores.positive,
554+ analyze_sentiment_result.confidence_scores.neutral,
555+ analyze_sentiment_result.confidence_scores.negative,
556+ ))
602557 print (" ------------------------------------------" )
603558```
604559
0 commit comments