Sitemap

How to Build Text Analytics using Azure Cognitive Services and C#

5 min readApr 5, 2023

--

Are you looking to extract insights from large volumes of text data? Azure Cognitive Services offers a range of text analytics capabilities, including sentiment analysis and keyphrase extraction. In this tutorial, we will walk you through how to build a simple text analytics application using C# and Azure Cognitive Services.

Prerequisites

Before getting started, you will need the following:

  • An Azure subscription. You can create a free account here.
  • Visual Studio or Visual Studio Code with the .NET Core SDK installed.

Step 1: Create a Text Analytics Resource

To use Azure Cognitive Services, you will need to create a resource in the Azure portal. Follow these steps to create a Text Analytics resource:

  1. Log in to the Azure portal.
  2. Click on the “+ Create a resource” button in the top left corner.
  3. Search for “Text Analytics” and select the “Text Analytics” result.
  4. Click on the “Create” button.
  5. Provide a name for your resource, select your subscription and resource group, and choose a pricing tier.
  6. Review and accept the terms and conditions, and then click on the “Create” button.

Once the resource is created, note down the endpoint URL and the access key, as we will need these later in our code.

Step 2: Create a C# Console Application

Open Visual Studio or Visual Studio Code and create a new C# console application. We will use this application to connect to the Text Analytics API and perform text analytics on some sample data.

Step 3: Install the Azure Text Analytics SDK

To use the Azure Text Analytics API in our C# application, we will need to install the Azure Text Analytics SDK. Follow these steps to install the SDK:

  1. In Visual Studio, right-click on the project in the Solution Explorer and select “Manage NuGet Packages”.
  2. In the “Browse” tab, search for “Microsoft.Azure.CognitiveServices.Language.TextAnalytics”.
  3. Click on the “Install” button to install the package.

Step 4: Add Code to Analyze Text

Let’s now add some code to our C# application to connect to the Text Analytics API and perform text analytics on some sample data.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.CognitiveServices.Language.TextAnalytics;
using Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models;

class Program
{
static async Task Main(string[] args)
{
// Configure the Text Analytics client
var textAnalyticsClient = new TextAnalyticsClient(
new ApiKeyServiceClientCredentials("<your_api_key>"),
new System.Net.Http.DelegatingHandler[] { });
textAnalyticsClient.Endpoint = "https://<your_region>.api.cognitive.microsoft.com/";

// Define some sample text to analyze
var documents = new List<Input>();
documents.Add(new Input(id: "1", text: "I really enjoyed the movie. The acting was great!"));
documents.Add(new Input(id: "2", text: "The food at the restaurant was terrible."));
documents.Add(new Input(id: "3", text: "The service was slow, but the food was good."));

// Analyze the text
var sentimentResults = await textAnalyticsClient.SentimentAsync(
new MultiLanguageBatchInput(documents));
var keyPhraseResults = await textAnalyticsClient.KeyPhrasesAsync(
new MultiLanguageBatchInput(documents));
// Output the results
Console.WriteLine("Sentiment Analysis Results:");
foreach (var result in sentimentResults.Documents)
{
Console.WriteLine($"Document ID: {result.Id}");
Console.WriteLine($"Sentiment Score: {result.Score:0.00}");
}

Console.WriteLine("Key Phrase Extraction Results:");
foreach (var result in keyPhraseResults.Documents)
{
Console.WriteLine($"Document ID: {result.Id}");
Console.WriteLine($"Key Phrases: {string.Join(", ", result.KeyPhrases)}");
}

Console.ReadLine();
}

In the code above, we first configure the Text Analytics client by providing our API key and endpoint URL. We then define some sample text to analyze and use the client to perform sentiment analysis and key phrase extraction. Finally, we output the results to the console.

### Step 5: Run the Application

We are now ready to run our application! Press F5 to run the application in Visual Studio or run the following command in the terminal if you are using Visual Studio Code:


You should see the sentiment analysis and key phrase extraction results printed to the console.

Congratulations! You have just built a text analytics application using Azure Cognitive Services and C#. With the Text Analytics API, you can extract valuable insights from large volumes of text data to drive your business decisions.

Step 6: Customizing the Text Analytics API

The Text Analytics API provides various customization options that you can use to tailor the analysis to your specific needs. For example, you can specify the language of the input text, adjust the sentiment score scale, and configure the entity recognition behavior.

Let’s say we want to analyze some text in Spanish. We can easily modify our code to do so by specifying the language code in the request options:

// Configure the client
var client = new TextAnalyticsClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

// Define some sample text in Spanish
var spanishText = "Me encanta el chocolate y la música clásica.";

// Analyze the sentiment and key phrases of the Spanish text
var sentimentResults = await client.AnalyzeSentimentAsync(new [] { spanishText }, "es");
var keyPhraseResults = await client.ExtractKeyPhrasesAsync(new [] { spanishText }, "es");

In this example, we specified “es” as the language code for Spanish. The Text Analytics API supports several languages, including English, Spanish, French, German, Italian, Portuguese, Dutch, and Swedish.

Step 7: Integrating Text Analytics into Your Applications

Now that we have built a simple text analytics application, we can integrate the Text Analytics API into our own applications. For example, we can use the API to analyze customer feedback on a website or social media platform, monitor news articles for specific topics, or analyze product reviews to identify common themes and issues.

To integrate the Text Analytics API into your own applications, you will need to obtain an API key and endpoint URL from the Azure portal. You can then use one of the Azure Cognitive Services client libraries for your programming language of choice to call the API.

Conclusion

In this tutorial, we learned how to build a text analytics application using Azure Cognitive Services and C#. We used the Text Analytics API to perform sentiment analysis and key phrase extraction on some sample text and customized the API to analyze text in different languages. We also discussed how you can integrate the Text Analytics API into your own applications to extract valuable insights from text data.

Azure Cognitive Services provides a suite of APIs for various AI and machine learning tasks, including speech recognition, image analysis, and natural language processing. By leveraging these APIs, you can add powerful AI capabilities to your applications without having to build and train your own machine-learning models.

--

--

Anselm Fowel
Anselm Fowel

Written by Anselm Fowel

I am first a software engineer, having spent over 11 years within the Fintech and insurance space I have been able to manage great team into delivering top apps

No responses yet