Exercise - Call the cloud service

Completed

In these exercises, you'll set up the resources to use Text Analytics for health and the steps to create a .NET application.

Exercise - Set up resources to use Text Analytics for health

In this exercise, you'll create an Azure subscription and language resource, and then you'll copy the key and endpoint of the language resource.

Task - Create an Azure subscription and resource group

If you already have an Azure subscription, skip to the next task.

  1. Go to Discover AI services with an Azure free account and Sign in.

  2. Select Start free.

  3. Enter the details in the form and then select Next.

  4. Provide payment information and then select Sign up.

  5. After creating the Azure subscription, select Resource groups.

    Screenshot showing the Resource groups link in Azure services.

  6. Select Create.

    Screenshot showing the Create new resource button.

  7. Select the subscription that you created, enter DemoLanguageGroup in the Resource group field, select your Region, and then select Review + create.

    Screenshot showing the Create a resource group dialog, showing information filled in.

  8. Select Create.

Task - Create a language resource

If you've already created a language resource, skip to the next task.

  1. Go to Language Studio in Azure Cognitive Services and sign in.

  2. Select your Azure directory and Azure subscription and then select Create a new language resource.

    Screenshot showing the Choose a language resource dialog, showing the Create a new language resource option.

  3. Select your Azure subscription, select the Azure resource group that you created, enter DemoLanguageResourceFL (replace FL with your initials) for the Azure resource name, select your Location, select S for Pricing Tier, and then select Done.

    Screenshot showing the Create a new language resource dialog, showing information filled in.

  4. After you've created the language resource, select Done.

Task - Copy the key and endpoint

In this task, you'll copy the key and endpoint by following these steps:

  1. Go to the Microsoft Azure portal and sign in.

  2. Locate and open the language resource that you created.

    Screenshot showing the language resources.

  3. Select Keys and Endpoint.

  4. Copy Key 1 to the clipboard.

    Screenshot showing the Copy to clipboard button next to Key 1.

  5. Save the key that you copied on a notepad. You'll need this key in subsequent tasks.

  6. Copy the Endpoint URL.

  7. Save the URL on a notepad. You'll need this URL in the subsequent steps.

Exercise - Create a .NET application

In this exercise, you'll create a console application that analyzes documents by using Azure AI Text Analytics.

Task - Create an application

In this first task, you'll create an application:

  1. Start Visual Studio 2022.

  2. Select File > New > Project.

  3. Select Console App and then select Next.

  4. Enter LanguageApp for the Project name and then select Next.

  5. Select the Do not use top-level statements checkbox and then select Create.

    Screenshot showing the create new console app wizard.

  6. Right-click the project and then select Manage NuGet Packages.

  7. Select the Browse tab, search for and select Azure.AI.TextAnalytics, select the stable version, and then select Install.

    Screenshot showing the install NuGet package button.

  8. Close the NuGet Package Manager tab.

  9. Open the Program.cs page and then add the following using statements.

    using Azure;
    using System;
    using Azure.AI.TextAnalytics;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    
  10. Add the following code inside the Program class. Replace KEY with the key that you copied from Azure and then replace the ENDPOINT URL with the one that you copied from Azure.

    private static readonly AzureKeyCredential _credentials = new AzureKeyCredential("KEY");
    private static readonly Uri _endPoint = new Uri("ENDPOINT");
    

    Screenshot showing a key and endpoint in C# code.

  11. Add the following method to the Program class.

    static async Task ProcessDocuments(TextAnalyticsClient client, List<string> batchInput)
    {
        AnalyzeHealthcareEntitiesOperation healthOperation = await client.StartAnalyzeHealthcareEntitiesAsync(batchInput);
        await healthOperation.WaitForCompletionAsync();
    
        await foreach (AnalyzeHealthcareEntitiesResultCollection resultDocs in healthOperation.Value)
        {
            Console.WriteLine($"Results of Azure Text Analytics for health async model, version: "{resultDocs.ModelVersion}"");
            Console.WriteLine("");
    
            foreach (AnalyzeHealthcareEntitiesResult resultDoc in resultDocs)
               ProcessDocumentResult(resultDoc);
        }
    }
    
  12. Add the following method to the Program class. This method checks if the document had errors; if not, the method will display all identified entities and relations that have been discovered between those entities.

    private static void ProcessDocumentResult(AnalyzeHealthcareEntitiesResult resultDoc)
    {
    }
    
  13. Add the following if statement to the ProcessDocumentResult method. The if statement checks for errors and shows an error message if errors have occurred.

    if (!resultDoc.HasError)
    {
    
    }
    else
    {
        Console.WriteLine("  Error!");
        Console.WriteLine($"  Document error code: {resultDoc.Error.ErrorCode}.");
        Console.WriteLine($"  Message: {resultDoc.Error.Message}");
    }
    Console.WriteLine("");
    
  14. Add the following for-each statement inside the if statement to show all recognized healthcare entities.

    foreach (var entity in resultDoc.Entities)
    {
        // view recognized healthcare entities
        Console.WriteLine($"  Entity: {entity.Text}");
        Console.WriteLine($"  Category: {entity.Category}");
        Console.WriteLine($"  Offset: {entity.Offset}");
        Console.WriteLine($"  Length: {entity.Length}");
        Console.WriteLine($"  NormalizedText: {entity.NormalizedText}");
    }
    Console.WriteLine($"  Found {resultDoc.EntityRelations.Count} relations in the current document:");
    Console.WriteLine("");
    
  15. Add the following for-each statement inside the if statement to show all recognized healthcare relations.

    // view recognized healthcare relations
    foreach (HealthcareEntityRelation relations in resultDoc.EntityRelations)
    {
        Console.WriteLine($"    Relation: {relations.RelationType}");
        Console.WriteLine($"    For this relation there are {relations.Roles.Count} roles");
    
        // view relation roles
        foreach (HealthcareEntityRelationRole role in relations.Roles)
        {
            Console.WriteLine($"      Role Name: {role.Name}");
    
            Console.WriteLine($"      Associated Entity Text: {role.Entity.Text}");
            Console.WriteLine($"      Associated Entity Category: {role.Entity.Category}");
            Console.WriteLine("");
        }
        Console.WriteLine("");
    }
    

    The if statement will resemble the following image.

    Screenshot showing the code inside the if statement.

  16. Create a Text Analytics client by using your key and endpoint URL. Add the following code inside the Main method.

    var client = new TextAnalyticsClient(_endPoint, _credentials);

  17. Create the string that you want to analyze. Add the following code inside the Main method.

    List<string> batchInput = new List<string>()
    {
        "57 year old male, presents with angina occurring nightly after meal time"
    };
    
  18. Call the process documents method by passing the client and the text to analyze. Add the following code inside the Main method.

    await ProcessDocuments(client, batchInput);

  19. Make the Main method async that returns Task.

    The Main method should now resemble the following image.

    Screenshot showing the Main method.

  20. Run the application.

  21. The output should resemble the following image.

    Screenshot showing the console application output.

  22. Stop the application.

  23. Add another document to the patchInput of "Prescribed 100-mg ibuprofen, taken twice daily."

    Screenshot showing the updated patch-input documents.

  24. Run the application again.

  25. The output will resemble the following image.

    Screenshot showing the new console application output.

  26. Stop the application.

  27. Try providing your own documents for analysis and then review the application output.