VibeCheck: Sentiment Analysis in .NET

VibeCheck: Sentiment Analysis in .NET

Using Azure Cognitive Services

·

3 min read

With countless feedback channels available today, users freely share their opinions and feedback.

This makes it crucial for businesses to understand user opinions and emotional tone in text such as feedback, social media posts, or customer reviews. Doing so can lead to improved customer satisfaction, enhanced decision making, and automation of moderation or escalation.

Now that we’ve established the need, let’s build a customer portal support application that allows users to submit feedback after a support interaction. Once the user submits their feedback, we need to analyse the sentiment of feedback to prioritize responses and notify stakeholders of highly negative feedback.

To achieve this, we will integrate Azure Cognitive Services’ Text Analytics API into a .NET application to detect and process sentiment from feedback. Normally, this application would be implemented on the web, possibly as a user feedback form, but to keep things simple, we will build a console application for this example.

So, let’s begin.

VibeCheck : Sentiment Analysis in .NET

First thing is first. We need to sign in to Azure Portal, create an Azure Cognitive Services resource and note the API Key and endpoint URL. I am going to create Environment Variables in Windows for those values.

💡
If you add these variables as System variables, you will need to restart your computer. If you add them as User Variables, then you may need to restart Visual Studio.

After that we need to create a console application and add the following NuGet Package:

dotnet add package Azure.AI.TextAnalytics

And then add the following code in your Program.cs.

var endpoint = Environment.GetEnvironmentVariable("AzureCognitiveEndpoint");
var apiKey = Environment.GetEnvironmentVariable("AzureCognitiveServiceKey");

if (string.IsNullOrEmpty(endpoint) || string.IsNullOrEmpty(apiKey))
{
    Console.WriteLine("Environment variables 'AzureCognitiveEndpoint' and 'AzureCognitiveServiceKey' must be set.");
    return;
}

// Create Text Analytics client
var client = new TextAnalyticsClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

Console.WriteLine("Enter feedback text:");
var feedback = Console.ReadLine();

if (!string.IsNullOrEmpty(feedback))
{
    var sentiment = AnalyzeSentiment(client, feedback);
    Console.WriteLine($"Sentiment: {sentiment.Sentiment}");
    Console.WriteLine($"Positive Score: {sentiment.ConfidenceScores.Positive}");
    Console.WriteLine($"Neutral Score: {sentiment.ConfidenceScores.Neutral}");
    Console.WriteLine($"Negative Score: {sentiment.ConfidenceScores.Negative}");

    if (sentiment.ConfidenceScores.Positive > 0.75)
    {
        Console.WriteLine("Thank you for your positive feedback!");
        Console.WriteLine($"Feedback: {feedback}");
    }
    else if (sentiment.ConfidenceScores.Negative > 0.75)
    {
        Console.WriteLine("ALERT: Negative feedback detected. Notifying support team...");
        NotifySupportTeam(feedback);
    }
    else
    {
        Console.WriteLine("Feedback is neutral or mixed.");
        Console.WriteLine($"Feedback: {feedback}");
    }
}
else
{
    Console.WriteLine("Feedback cannot be empty!");
}

return;

static DocumentSentiment AnalyzeSentiment(TextAnalyticsClient client, string feedback)
{
    // Analyze sentiment using Azure Cognitive Services
    var response = client.AnalyzeSentiment(feedback);
    return response.Value;
}

That’s it! We’re done - how easy was that?

Basically, we get our API Key and endpoint and create a new TextAnalyticsClient. We then send the feedback to the service and get the results back. Here is how it looks:

The best part is, you get multi-language support out of the box. Check this out for German and Spanish. The English phrase is “The support was fantastic! I was very happy!”:

German

Spanish

Conclusion

Sentiment analysis might sound complicated, but Azure AI Services makes it simple. By hiding the complexity, we were able to focus on solving the problem at hand: analyzing feedback to improve customer satisfaction and decision-making.

This kind of solution is not just limited to customer support portals. Industries like insurance can greatly benefit from sentiment analysis. For instance, insurers can analyze customer feedback to detect dissatisfaction early, prioritize claims escalations, or identify trends in policyholder sentiment. This can enhance the customer experience and streamline operations, making sentiment analysis a valuable tool across various sectors.

This simple application is a starting point. From here, you could scale it to process feedback in real-time, integrate it with CRM systems, or add analytics dashboards to track sentiment trends over time.

You can find the source code here: https://github.com/tjgokken/VibeCheck