Friday, January 24, 2025

Azure AI services - Azure AI Language - Exploring Azure Text Analytics for Health Using Client Library and API

Azure AI services - Azure AI Language - Exploring Azure Text Analytics for Health Using Client Library and API:


using Azure;
using Azure.AI.TextAnalytics;
 
public class Program
{
    public static async Task Main(string[] args)
    {
        var LANGUAGE_KEY = "BbVzkY9TYPTvBTRXQ99BAACYeBjFXJ3w3AAAaACOGG9CD";
        var LANGUAGE_ENDPOINT = "https://language1.cognitiveservices.azure.com/";
        AzureKeyCredential credentials = new(LANGUAGE_KEY);
        Uri endpoint = new(LANGUAGE_ENDPOINT);
        var client = new TextAnalyticsClient(endpoint, credentials);
        await healthExample(client);
        Console.ReadKey();
    }
 
    private static async Task healthExample(TextAnalyticsClient client)
    {
        string document = "Prescribed 100mg ibuprofen, taken twice daily.";
        List<string> batchInput = new List<string>()
            {
                document
            };
        AnalyzeHealthcareEntitiesOperation healthOperation = await client.StartAnalyzeHealthcareEntitiesAsync(batchInput);
        await healthOperation.WaitForCompletionAsync();
        await foreach (AnalyzeHealthcareEntitiesResultCollection documentsInPage in healthOperation.Value)
        {
            foreach (AnalyzeHealthcareEntitiesResult entitiesInDoc in documentsInPage)
            {
                if (!entitiesInDoc.HasError)
                {
                    foreach (var entity in entitiesInDoc.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}");
                    }
                    foreach (HealthcareEntityRelation relations in entitiesInDoc.EntityRelations)
                    {
                        Console.WriteLine($"    Relation: {relations.RelationType}");
                        Console.WriteLine($"    For this relation there are {relations.Roles.Count} 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}");
                        }
                    }
                }
                else
                {
                    Console.WriteLine($"  Document error code: {entitiesInDoc.Error.ErrorCode}.");
                    Console.WriteLine($"  Message: {entitiesInDoc.Error.Message}");
                }
            }
        }
    }
}

OutPut:



No comments:

Post a Comment

Featured Post

Azure AI services - Recognize and synthesize speech

Azure AI services - Recognize and synthesize speech: 1. Create 'Speech service' in Azure C# Code: using System; using System.Threa...

Popular posts