def sample_extractive_summarization(): from azure.core.credentials import AzureKeyCredential from azure.ai.textanalytics import ( TextAnalyticsClient, ExtractSummaryAction ) endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] text_analytics_client = TextAnalyticsClient( endpoint=endpoint, credential=AzureKeyCredential(key), ) document = [ "At Microsoft, we have been on a quest to advance AI beyond existing techniques, by taking a more holistic, " "human-centric approach to learning and understanding. As Chief Technology Officer of Azure AI Cognitive " "Services, I have been working with a team of amazing scientists and engineers to turn this quest into a " "reality. In my role, I enjoy a unique perspective in viewing the relationship among three attributes of " "human cognition: monolingual text (X), audio or visual sensory signals, (Y) and multilingual (Z). At the " "intersection of all three, there's magic-what we call XYZ-code as illustrated in Figure 1-a joint " "representation to create more powerful AI that can speak, hear, see, and understand humans better. " "We believe XYZ-code will enable us to fulfill our long-term vision: cross-domain transfer learning, " "spanning modalities and languages. The goal is to have pretrained models that can jointly learn " "representations to support a broad range of downstream AI tasks, much in the way humans do today. " "Over the past five years, we have achieved human performance on benchmarks in conversational speech " "recognition, machine translation, conversational question answering, machine reading comprehension, " "and image captioning. These five breakthroughs provided us with strong signals toward our more ambitious " "aspiration to produce a leap in AI capabilities, achieving multisensory and multilingual learning that " "is closer in line with how humans learn and understand. I believe the joint XYZ-code is a foundational " "component of this aspiration, if grounded with external knowledge sources in the downstream AI tasks." ] poller = text_analytics_client.begin_analyze_actions( document, actions=[ ExtractSummaryAction(), ], ) document_results = poller.result() for result in document_results: extract_summary_result = result[0] # first document, first result if extract_summary_result.is_error: print("...Is an error with code '{}' and message '{}'".format( extract_summary_result.code, extract_summary_result.message )) else: print("Summary extracted: \n{}".format( " ".join([sentence.text for sentence in extract_summary_result.sentences])) )
def sample_classify_document_multi_categories(): from azure.core.credentials import AzureKeyCredential from azure.ai.textanalytics import (TextAnalyticsClient, MultiCategoryClassifyAction) endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] project_name = os.environ["MULTI_CATEGORY_CLASSIFY_PROJECT_NAME"] deployed_model_name = os.environ["MULTI_CATEGORY_CLASSIFY_DEPLOYMENT_NAME"] path_to_sample_document = os.path.abspath( os.path.join( os.path.abspath(__file__), "..", "./text_samples/custom_classify_sample.txt", )) text_analytics_client = TextAnalyticsClient( endpoint=endpoint, credential=AzureKeyCredential(key), ) with open(path_to_sample_document, "r") as fd: document = [fd.read()] poller = text_analytics_client.begin_analyze_actions( document, actions=[ MultiCategoryClassifyAction(project_name=project_name, deployment_name=deployed_model_name), ], ) document_results = poller.result() for doc, classification_results in zip(document, document_results): for classification_result in classification_results: if not classification_result.is_error: classifications = classification_result.classifications print( "\nThe movie plot '{}' was classified as the following genres:\n" .format(doc)) for classification in classifications: print("'{}' with confidence score {}.".format( classification.category, classification.confidence_score)) else: print( "Movie plot '{}' has an error with code '{}' and message '{}'" .format(doc, classification_result.code, classification_result.message))
def sample_recognize_custom_entities(): from azure.core.credentials import AzureKeyCredential from azure.ai.textanalytics import ( TextAnalyticsClient, RecognizeCustomEntitiesAction, ) endpoint = os.environ["AZURE_TEXT_ANALYTICS_ENDPOINT"] key = os.environ["AZURE_TEXT_ANALYTICS_KEY"] project_name = os.environ["CUSTOM_ENTITIES_PROJECT_NAME"] deployed_model_name = os.environ["CUSTOM_ENTITIES_DEPLOYMENT_NAME"] path_to_sample_document = os.path.abspath( os.path.join( os.path.abspath(__file__), "..", "./text_samples/custom_entities_sample.txt", )) text_analytics_client = TextAnalyticsClient( endpoint=endpoint, credential=AzureKeyCredential(key), ) with open(path_to_sample_document) as fd: document = [fd.read()] poller = text_analytics_client.begin_analyze_actions( document, actions=[ RecognizeCustomEntitiesAction(project_name=project_name, deployment_name=deployed_model_name), ], ) document_results = poller.result() for result in document_results: custom_entities_result = result[0] # first document, first result if not custom_entities_result.is_error: for entity in custom_entities_result.entities: print( "Entity '{}' has category '{}' with confidence score of '{}'" .format(entity.text, entity.category, entity.confidence_score)) else: print("...Is an error with code '{}' and message '{}'".format( custom_entities_result.code, custom_entities_result.message))
def sample_model_version(): print("--------------Choosing model_version sample--------------") from azure.core.credentials import AzureKeyCredential from azure.ai.textanalytics import TextAnalyticsClient, RecognizeEntitiesAction 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 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." ] print("\nSetting model_version='latest' with recognize_entities") result = text_analytics_client.recognize_entities(documents, model_version="latest") result = [review for review in result if not review.is_error] print("...Results of Recognize Entities:") for review in result: for entity in review.entities: print( f"......Entity '{entity.text}' has category '{entity.category}'" ) print( "\nSetting model_version='latest' with recognize entities action in begin_analyze_actions" ) poller = text_analytics_client.begin_analyze_actions( documents, actions=[RecognizeEntitiesAction(model_version="latest")]) print("...Results of Recognize Entities Action:") document_results = poller.result() for action_results in document_results: recognize_entities_result = action_results[0] 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( f"......Entity '{entity.text}' has category '{entity.category}'" )
def analyze(self): # [START analyze] from azure.core.credentials import AzureKeyCredential from azure.ai.textanalytics import ( TextAnalyticsClient, RecognizeEntitiesAction, RecognizeLinkedEntitiesAction, RecognizePiiEntitiesAction, ExtractKeyPhrasesAction, AnalyzeSentimentAction, PiiEntityDomainType, ) 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 [email protected]! \ 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(domain_filter=PiiEntityDomainType. PROTECTED_HEALTH_INFORMATION), ExtractKeyPhrasesAction(), RecognizeLinkedEntitiesAction(), AnalyzeSentimentAction() ], ) result = poller.result() action_results = [ action_result for action_result in list(result) if not action_result.is_error ] first_action_result = action_results[0] print("Results of Entities Recognition action:") docs = [ doc for doc in first_action_result.document_results if not doc.is_error ] for idx, doc in enumerate(docs): print("\nDocument text: {}".format(documents[idx])) for entity in doc.entities: print("Entity: {}".format(entity.text)) print("...Category: {}".format(entity.category)) print("...Confidence Score: {}".format( entity.confidence_score)) print("...Offset: {}".format(entity.offset)) print("...Length: {}".format(entity.length)) print("------------------------------------------") second_action_result = action_results[1] print("Results of PII Entities Recognition action:") docs = [ doc for doc in second_action_result.document_results if not doc.is_error ] for idx, doc in enumerate(docs): print("Document text: {}".format(documents[idx])) print("Document text with redactions: {}".format( doc.redacted_text)) for entity in doc.entities: print("Entity: {}".format(entity.text)) print("...Category: {}".format(entity.category)) print("...Confidence Score: {}\n".format( entity.confidence_score)) print("...Offset: {}".format(entity.offset)) print("...Length: {}".format(entity.length)) print("------------------------------------------") third_action_result = action_results[2] print("Results of Key Phrase Extraction action:") docs = [ doc for doc in third_action_result.document_results if not doc.is_error ] for idx, doc in enumerate(docs): print("Document text: {}\n".format(documents[idx])) print("Key Phrases: {}\n".format(doc.key_phrases)) print("------------------------------------------") fourth_action_result = action_results[3] print("Results of Linked Entities Recognition action:") docs = [ doc for doc in fourth_action_result.document_results if not doc.is_error ] for idx, doc in enumerate(docs): print("Document text: {}\n".format(documents[idx])) for linked_entity in doc.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)) print("------------------------------------------") fifth_action_result = action_results[4] print("Results of Sentiment Analysis action:") docs = [ doc for doc in fifth_action_result.document_results if not doc.is_error ] for doc in docs: print("Overall sentiment: {}".format(doc.sentiment)) print("Scores: positive={}; neutral={}; negative={} \n".format( doc.confidence_scores.positive, doc.confidence_scores.neutral, doc.confidence_scores.negative, )) print("------------------------------------------")
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 [email protected]! ' '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(f"\nDocument text: {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(f"......Entity: {entity.text}") print(f".........Category: {entity.category}") print(f".........Confidence Score: {entity.confidence_score}") print(f".........Offset: {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(f"......Entity: {entity.text}") print(f".........Category: {entity.category}") print(f".........Confidence Score: {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( f"......Key Phrases: {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(f"......Entity name: {linked_entity.name}") print(f".........Data source: {linked_entity.data_source}") print( f".........Data source language: {linked_entity.language}") print( f".........Data source entity ID: {linked_entity.data_source_entity_id}" ) print(f".........Data source URL: {linked_entity.url}") print(".........Document matches:") for match in linked_entity.matches: print(f"............Match text: {match.text}") print( f"............Confidence Score: {match.confidence_score}" ) print(f"............Offset: {match.offset}") print(f"............Length: {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( f"......Overall sentiment: {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("------------------------------------------")