Exercise - Call the cloud service
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.
Go to Discover AI services with an Azure free account and Sign in.
Select Start free.
Enter the details in the form and then select Next.
Provide payment information and then select Sign up.
After creating the Azure subscription, select Resource groups.
Select Create.
Select the subscription that you created, enter DemoLanguageGroup in the Resource group field, select your Region, and then select Review + create.
Select Create.
Task - Create a language resource
If you've already created a language resource, skip to the next task.
Go to Language Studio in Azure Cognitive Services and sign in.
Select your Azure directory and Azure subscription and then select Create a new language resource.
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.
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:
Go to the Microsoft Azure portal and sign in.
Locate and open the language resource that you created.
Select Keys and Endpoint.
Copy Key 1 to the clipboard.
Save the key that you copied on a notepad. You'll need this key in subsequent tasks.
Copy the Endpoint URL.
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:
Start Visual Studio 2022.
Select File > New > Project.
Select Console App and then select Next.
Enter LanguageApp for the Project name and then select Next.
Select the Do not use top-level statements checkbox and then select Create.
Right-click the project and then select Manage NuGet Packages.
Select the Browse tab, search for and select Azure.AI.TextAnalytics, select the stable version, and then select Install.
Close the NuGet Package Manager tab.
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;
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");
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); } }
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) { }
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("");
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("");
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.
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);
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" };
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);
Make the Main method async that returns Task.
The Main method should now resemble the following image.
Run the application.
The output should resemble the following image.
Stop the application.
Add another document to the patchInput of "Prescribed 100-mg ibuprofen, taken twice daily."
Run the application again.
The output will resemble the following image.
Stop the application.
Try providing your own documents for analysis and then review the application output.