Overview

This exercise demonstrates how to analyze text using Azure AI Language, including language detection, sentiment analysis, key phrase extraction, and entity recognition.


Steps & Configuration Details

1. Provision an Azure AI Language Resource

  • Open Azure Portal (https://portal.azure.com) and sign in.
  • Select Create a resource → Search for Language Service → Click Create.
  • Configuration Items:
    • Subscription: Your Azure subscription.
    • Resource Group: Select or create a resource group.
    • Region: Choose any available region.
    • Name: Enter a unique name.
    • Pricing Tier: F0 (Free) or S (Standard).
    • Responsible AI Notice: Agree.

After provisioning, navigate to Keys and Endpoint in the Resource Management section.


2. Clone the Repository

  • Open Azure Cloud Shell in the Azure Portal.
  • Select PowerShell as the environment.
  • Run the following commands:
    rm -r mslearn-ai-language -f
    git clone https://github.com/microsoftlearning/mslearn-ai-language mslearn-ai-language
    cd mslearn-ai-language/Labfiles/01-analyze-text
    

3. Configure Your Application

  • Navigate to the correct folder:
    cd C-Sharp/text-analysis  # For C#
    cd Python/text-analysis   # For Python
    
  • Install dependencies:
    • C#:
      dotnet add package Azure.AI.TextAnalytics --version 5.3.0
      
    • Python:
      python -m venv labenv
      ./labenv/bin/Activate.ps1
      pip install -r requirements.txt azure-ai-textanalytics==5.3.0
      
  • Open the configuration file:
    • C#: appsettings.json
    • Python: .env
  • Update Configuration Values:
    • Azure AI Language Endpoint
    • API Key
  • Save the configuration file.

4. Implement Text Analysis

  • Open the code file:
    • C#: Program.cs
    • Python: text-analysis.py
  • Add references:
    • C#:
      using Azure;
      using Azure.AI.TextAnalytics;
      
    • Python:
      from azure.core.credentials import AzureKeyCredential
      from azure.ai.textanalytics import TextAnalyticsClient
      
  • Create the AI Language client:
    • C#:
      AzureKeyCredential credentials = new AzureKeyCredential(aiSvcKey);
      Uri endpoint = new Uri(aiSvcEndpoint);
      TextAnalyticsClient aiClient = new TextAnalyticsClient(endpoint, credentials);
      
    • Python:
      credential = AzureKeyCredential(ai_key)
      ai_client = TextAnalyticsClient(endpoint=ai_endpoint, credential=credential)
      

5. Detect Language

  • C#:
    DetectedLanguage detectedLanguage = aiClient.DetectLanguage(text);
    Console.WriteLine($"\nLanguage: {detectedLanguage.Name}");
    
  • Python:
    detectedLanguage = ai_client.detect_language(documents=[text])[0]
    print('\nLanguage: {}'.format(detectedLanguage.primary_language.name))
    

6. Evaluate Sentiment

  • C#:
    DocumentSentiment sentimentAnalysis = aiClient.AnalyzeSentiment(text);
    Console.WriteLine($"\nSentiment: {sentimentAnalysis.Sentiment}");
    
  • Python:
    sentimentAnalysis = ai_client.analyze_sentiment(documents=[text])[0]
    print("\nSentiment: {}".format(sentimentAnalysis.sentiment))
    

7. Extract Key Phrases

  • C#:
    KeyPhraseCollection phrases = aiClient.ExtractKeyPhrases(text);
    if (phrases.Count > 0) {
        Console.WriteLine("\nKey Phrases:");
        foreach (string phrase in phrases) {
            Console.WriteLine($"\t{phrase}");
        }
    }
    
  • Python:
    phrases = ai_client.extract_key_phrases(documents=[text])[0].key_phrases
    if len(phrases) > 0:
        print("\nKey Phrases:")
        for phrase in phrases:
            print('\t{}'.format(phrase))
    

8. Recognize Entities

  • C#:
    CategorizedEntityCollection entities = aiClient.RecognizeEntities(text);
    if (entities.Count > 0) {
        Console.WriteLine("\nEntities:");
        foreach (CategorizedEntity entity in entities) {
            Console.WriteLine($"\t{entity.Text} ({entity.Category})");
        }
    }
    
  • Python:
    entities = ai_client.recognize_entities(documents=[text])[0].entities
    if len(entities) > 0:
        print("\nEntities")
        for entity in entities:
            print('\t{} ({})'.format(entity.text, entity.category))
    

9. Extract Linked Entities

  • C#:
    LinkedEntityCollection linkedEntities = aiClient.RecognizeLinkedEntities(text);
    if (linkedEntities.Count > 0) {
        Console.WriteLine("\nLinks:");
        foreach (LinkedEntity linkedEntity in linkedEntities) {
            Console.WriteLine($"\t{linkedEntity.Name} ({linkedEntity.Url})");
        }
    }
    
  • Python:
    entities = ai_client.recognize_linked_entities(documents=[text])[0].entities
    if len(entities) > 0:
        print("\nLinks")
        for linked_entity in entities:
            print('\t{} ({})'.format(linked_entity.name, linked_entity.url))
    

10. Run Your Application

  • C#:
    dotnet run
    
  • Python:
    python text-analysis.py
    
  • Observe the output, which should display detected language, sentiment, key phrases, and entities.

11. Clean Up

  • Delete Azure resources to avoid unnecessary costs:

This summary captures the essential steps while highlighting all configuration items and code references required for analyzing text using Azure AI Language.

Related Posts