|

|  How to Access Microsoft Cognitive Services Text Analytics API in C#

How to Access Microsoft Cognitive Services Text Analytics API in C#

October 31, 2024

Learn how to seamlessly integrate Microsoft Cognitive Services Text Analytics API in C# to enhance your app's capabilities with natural language processing features.

How to Access Microsoft Cognitive Services Text Analytics API in C#

 

Initialize the Project and Install the Required Package

 

  • Create a new .NET project in Visual Studio by selecting "Console App" from the template list.
  •  

  • Go to the NuGet Package Manager Console (or use the NuGet Package Manager GUI) and install the `Azure.AI.TextAnalytics` library to facilitate calls to the Microsoft Cognitive Services Text Analytics API.
Install-Package Azure.AI.TextAnalytics

 

Set Up the Azure Text Analytics Client

 

  • Import the necessary namespaces in your C# program to access Azure Text Analytics.
  •  

  • Create a method to initialize the `TextAnalyticsClient` using the endpoint URL and API key provided by your Azure portal.
using Azure;
using Azure.AI.TextAnalytics;

public class TextAnalyticsService
{
    private readonly TextAnalyticsClient _client;

    public TextAnalyticsService(string endpoint, string apiKey)
    {
        var credentials = new AzureKeyCredential(apiKey);
        _client = new TextAnalyticsClient(new Uri(endpoint), credentials);
    }
}

 

Analyze Sentiment

 

  • Implement a method within your C# class to analyze the sentiment of a given text string. The method should handle potential exceptions that could be thrown during the API call.
public string AnalyzeSentiment(string text)
{
    try
    {
        DocumentSentiment documentSentiment = _client.AnalyzeSentiment(text);
        return documentSentiment.Sentiment.ToString();
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine($"Error processing the sentiment analysis: {e.Message}");
        return "Error";
    }
}

 

Extract Key Phrases

 

  • Add functionality to extract key phrases from text input, providing insights into the most significant terms within the text.
public IList<string> ExtractKeyPhrases(string text)
{
    try
    {
        KeyPhraseCollection keyPhrases = _client.ExtractKeyPhrases(text);

        return keyPhrases.ToList();
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine($"Error extracting key phrases: {e.Message}");
        return new List<string>();
    }
}

 

Detect Language

 

  • Implement a method to detect the language of a given input text, which can be useful for multilingual applications.
public string DetectLanguage(string text)
{
    try
    {
        DetectedLanguage detectedLanguage = _client.DetectLanguage(text);
        return detectedLanguage.Name;
    }
    catch (RequestFailedException e)
    {
        Console.WriteLine($"Error detecting language: {e.Message}");
        return "Error";
    }
}

 

Execute the Main Program Logic

 

  • In your `Main` method, instantiate the `TextAnalyticsService` class with the endpoint and API key. Call the implemented methods to demonstrate their functionality.
public class Program
{
    private static void Main(string[] args)
    {
        string endpoint = "<Your-Endpoint-URL>";
        string apiKey = "<Your-API-Key>";

        var service = new TextAnalyticsService(endpoint, apiKey);

        string textToAnalyze = "Microsoft Cognitive Services provides powerful AI tools.";

        string sentiment = service.AnalyzeSentiment(textToAnalyze);
        Console.WriteLine($"Sentiment: {sentiment}");

        IList<string> keyPhrases = service.ExtractKeyPhrases(textToAnalyze);
        Console.WriteLine("Key Phrases: " + string.Join(", ", keyPhrases));

        string language = service.DetectLanguage(textToAnalyze);
        Console.WriteLine($"Language: {language}");
    }
}

 

Incorporate these steps into your .NET application, and make appropriate adjustments to handle additional edge cases or customize functionalities according to project requirements.