Sentiment Analysis in C#: Azure AI Language or LLMs

This blog is presented as a part of C# Advent 2025. Follow the link to check out the rest of the excellent C# and .NET content coming out in 2 blogs per day between December 1 and 25.

Sentiment analysis has been a cornerstone of Natural Language Processing (NLP) for years, helping businesses understand customer feedback, employee satisfaction, and market trends. As a C# developer, you’ve had access to powerful sentiment analysis tools through Azure AI services. But with the rise of Large Language Models (LLMs), the landscape is shifting.

In this post, we’ll explore both approaches using the example of an employee feedback management platform.

The Traditional Approach: Azure AI Language Service

Microsoft’s Azure AI Language service has been the go-to solution for sentiment analysis in the .NET ecosystem. It provides:

  • Sentiment labels: positive, negative, neutral, and mixed
  • Confidence scores: Values between 0-1 for each sentiment category
  • Opinion Mining: Aspect-based sentiment analysis that links sentiments to specific targets (nouns) and assessments (adjectives)

Implementation with Azure.AI.TextAnalytics

Here’s how you implement traditional sentiment analysis using the Azure.AI.TextAnalytics NuGet package

using Azure;
using Azure.AI.TextAnalytics;

public class SentimentAnalyzer : ISentimentAnalyzer
{
    private readonly TextAnalyticsClient _client;

    public SentimentAnalyzer(AzureLanguageOptions options)
    {
        _client = new TextAnalyticsClient(
            new Uri(options.Endpoint),
            new AzureKeyCredential(options.ApiKey)
        );
    }

    public async Task<SentimentAnalysisResult> AnalyzeTranscriptAsync(
        string transcript,
        CancellationToken cancellationToken = default)
    {
        // Enable Opinion Mining for aspect-based sentiment
        var options = new AnalyzeSentimentOptions
        {
            IncludeOpinionMining = true
        };

        var response = await _client.AnalyzeSentimentAsync(
            transcript,
            options: options,
            cancellationToken: cancellationToken
        );

        DocumentSentiment documentSentiment = response.Value;

        // Extract overall sentiment with confidence scores
        var overallSentiment = new OverallSentiment
        {
            Sentiment = MapSentiment(documentSentiment.Sentiment),
            PositiveConfidence = documentSentiment.ConfidenceScores.Positive,
            NegativeConfidence = documentSentiment.ConfidenceScores.Negative,
            NeutralConfidence = documentSentiment.ConfidenceScores.Neutral
        };

        // Extract topic-level sentiments from opinion mining
        var topics = ExtractTopicSentiments(documentSentiment);

        return new SentimentAnalysisResult
        {
            OverallSentiment = overallSentiment,
            Topics = topics,
            TranscriptText = transcript
        };
    }

    private static List<TopicSentiment> ExtractTopicSentiments(
        DocumentSentiment documentSentiment)
    {
        var topics = new List<TopicSentiment>();

        foreach (var sentence in documentSentiment.Sentences)
        {
            foreach (var opinion in sentence.Opinions)
            {
                var target = opinion.Target;
                topics.Add(new TopicSentiment
                {
                    Topic = target.Text,
                    Sentiment = MapSentiment(target.Sentiment),
                    PositiveConfidence = target.ConfidenceScores.Positive,
                    NegativeConfidence = target.ConfidenceScores.Negative,
                    TextContext = sentence.Text
                });
            }
        }
        return topics;
    }
}

This approach works well for straightforward text analysis, as documented in the Azure AI Language quickstart

However, when dealing with complex real-world scenarios like employee feedback transcripts, traditional methods reveal their limitations:

  1. Sentiment Dilution: Multi-speaker conversations mix interviewer questions with employee responses, diluting the actual sentiment being measured.
  2. Topic Contamination: Agent prompts like “How is your manager?” can skew topic extraction.
  3. Context Loss: Short answers like “Not good” lose meaning without the question context.
  4. Figurative Language: Sarcasm, implied meaning, and nuanced expressions are often misclassified.

As Classification of natural language text with generative AI in Microsoft Fabric notes:

“Traditional methods such as rule-based chunking and sentiment analysis often miss the nuances of language, such as figurative speech and implied meaning.”

The LLM Revolution: Azure OpenAI

This is where Large Language Models change the game. According to Microsoft’s Classification of natural language text with generative AI in Microsoft Fabric :

“Generative AI and Large Language Models (LLMs) change this dynamic by enabling large-scale, sophisticated interpretation of text. They can capture figurative language, implications, connotations, and creative expressions, leading to deeper insights and more consistent classification across large volumes of text.”

Implementation with Azure OpenAI

Here’s the LLM-based approach using Azure OpenAI with structured outputs:

using Azure;
using Azure.AI.OpenAI;
using Microsoft.Extensions.AI;
using OpenAI.Chat;

public class LLMSentimentAnalyzer : ISentimentAnalyzer
{
    private readonly ChatClient _chatClient;

    public LLMSentimentAnalyzer(AzureOpenAIOptions options)
    {
        var azureClient = new AzureOpenAIClient(
            new Uri(options.Endpoint),
            new AzureKeyCredential(options.ApiKey)
        );
        _chatClient = azureClient.GetChatClient(options.DeploymentName);
    }

    public async Task<SentimentAnalysisResult> AnalyzeTranscriptAsync(
        string transcript,
        CancellationToken cancellationToken = default)
    {
        var systemPrompt = """
            You are a sentiment analysis expert. Analyze the employee feedback transcript
            and extract sentiment information.

            Rules:
            1. Analyze the EMPLOYEE's sentiment only (ignore interviewer/agent)
            2. Overall sentiment can be Positive, Negative, Neutral, or Mixed
            3. Extract key topics mentioned: supervisor, commute, compensation, culture, etc.
            4. PAY SPECIAL ATTENTION to mental health indicators: stress, burnout,
               work-life balance, anxiety, feeling overwhelmed
            5. Confidence scores must sum to 1.0
            6. textContext should be a direct quote from the transcript
            """;

        // Generate JSON schema for structured outputs
        var schema = AIJsonUtilities.CreateJsonSchema(typeof(LLMSentimentResponse));

        var options = new ChatCompletionOptions
        {
            Temperature = 0f,
            ResponseFormat = ChatResponseFormat.CreateJsonSchemaFormat(
                "sentiment_analysis_result",
                BinaryData.FromString(schema.ToString())
            )
        };

        var completion = await _chatClient.CompleteChatAsync(
            [
                new SystemChatMessage(systemPrompt),
                new UserChatMessage(transcript)
            ],
            options,
            cancellationToken
        );

        var jsonResponse = completion.Value.Content[0].Text;
        return ParseLLMResponse(jsonResponse, transcript);
    }
}

The Structured Output Advantage

A key feature enabling reliable LLM-based sentiment analysis is structured outputs:

“Structured outputs make a model follow a JSON Schema definition that you provide as part of your inference API call… Structured outputs are recommended for function calling, extracting structured data, and building complex multi-step workflows.”

This ensures the LLM returns data in exactly the format your application expects:

private class LLMSentimentResponse
{
    public required LLMOverallSentiment OverallSentiment { get; set; }
    public required List<LLMTopicSentiment> Topics { get; set; }
}

private class LLMOverallSentiment
{
    public required string Sentiment { get; set; }
    public required double PositiveConfidence { get; set; }
    public required double NegativeConfidence { get; set; }
    public required double NeutralConfidence { get; set; }
}

Hybrid Approach: LLM Preprocessing + Traditional Analysis

For scenarios where you want the best of both worlds, you can use LLMs to preprocess text before sending it to Azure AI Language:

public class LlmTranscriptPreprocessor : ITranscriptPreprocessor
{
    private readonly ChatClient _chatClient;

    public async Task<string> PreprocessAsync(
        string multiSpeakerTranscript,
        CancellationToken cancellationToken = default)
    {
        var systemPrompt = """
            Transform this multi-speaker employee feedback transcript into a
            first-person narrative from the employee's perspective only.

            Rules:
            1. Include ONLY the employee's statements
            2. Remove all interviewer questions
            3. Resolve pronouns using context (e.g., "she" -> "my manager")
            4. Expand short answers using question context
               (Q: "How was compensation?" A: "Not good" ->
                "I was not satisfied with the compensation")
            5. Maintain original sentiment and meaning
            """;

        var options = new ChatCompletionOptions { Temperature = 0f };

        var completion = await _chatClient.CompleteChatAsync(
            [
                new SystemChatMessage(systemPrompt),
                new UserChatMessage(multiSpeakerTranscript)
            ],
            options,
            cancellationToken
        );

        return completion.Value.Content[0].Text ?? string.Empty;
    }
}

Comparison: When to Use Each Approach

Aspect Azure AI LanguageAzure OpenAI LLM
Setup ComplexityLow – API key and endpointMedium – Deployment needed
CostPer-document pricingPer-token pricing
Customization Limited to model versionsFully customizable via prompts
Multi-speaker textStruggles with mixed speakersExcels with proper prompting
Figurative languageOften misclassifiedBetter understanding
Domain specificityGeneric modelCustom prompts for your domain
Latency~100-200ms~500ms-2s
Structured outputFixed schemaFlexible JSON schema

Design Pattern: Strategy Pattern for Flexibility

Notice how both implementations share the same interface:

public interface ISentimentAnalyzer
{
    Task<SentimentAnalysisResult> AnalyzeTranscriptAsync(
        string transcript,
        CancellationToken cancellationToken = default);
}

This allows you to swap implementations based on your needs:

// In your DI configuration
services.AddScoped<ISentimentAnalyzer>(sp =>
{
    var config = sp.GetRequiredService<IConfiguration>();
    var useLlm = config.GetValue<bool>("UseLlmSentimentAnalysis");

    return useLlm
        ? new LLMSentimentAnalyzer(azureOpenAIOptions)
        : new SentimentAnalyzer(azureLanguageOptions);
});

Conclusion

The shift from traditional NLP services to LLMs isn’t about one being “better” than the other—it’s about choosing the right tool for your specific use case:

  • Use Azure AI Language when you need quick, cost-effective sentiment analysis for simple text
  • Use Azure OpenAI when dealing with complex, nuanced text that requires domain-specific understanding
  • Use a hybrid approach when you want traditional service benefits with LLM preprocessing

As Natural language processing technology states:

“Language models enhance natural language processing by providing advanced text generation and understanding capabilities… They serve as powerful tools within the broader natural language processing domain by enabling more sophisticated language processing.”

The code examples in this post illustrate how a real employee feedback management system can benefit from LLM-based analysis to significantly improved the accuracy of employee sentiment detection, particularly for sensitive topics like mental health and work-life balance.

If you’d like help integrating sentiment analysis or other AI-driven technologies into your .NET applications, reach out to the experts at Trailhead.

References

Azure AI Language – Sentiment Analysis Overview
Azure AI Language – Quickstart C#
Azure OpenAI Structured Outputs
Text Classification with Generative AI
Natural Language Processing Technology Choices
How Generative AI and LLMs Work
Azure.AI.TextAnalytics NuGet Package

Picture of Rodrigo Juarez

Rodrigo Juarez

Rodrigo Juarez has over 25 years of experience in software development, specializing in Microsoft technologies. His mission is to solve complex problems for his clients, with a focus on results. He achieves this by applying the appropriate technology and using best practices to ensure the delivery of high-quality solutions. Over the past 6 years, Rodrigo has honed his skills in back-end development and in creating mobile applications, initially using Xamarin and now transitioning to .NET MAUI. He is a Lead Mobile Developer at Trailhead Technology Partners and the co-author, along with Jesse Liberty, of the book ".NET MAUI for C# Developers"

Free Consultation

Sign up for a FREE consultation with one of Trailhead's experts.

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

Related Blog Posts

We hope you’ve found this to be helpful and are walking away with some new, useful insights. If you want to learn more, here are a couple of related articles that others also usually find to be interesting:

Our Gear Is Packed and We're Excited to Explore With You

Ready to come with us? 

Together, we can map your company’s software journey and start down the right trails. If you’re set to take the first step, simply fill out our contact form. We’ll be in touch quickly – and you’ll have a partner who is ready to help your company take the next step on its software journey. 

We can’t wait to hear from you! 

Main Contact

This field is for validation purposes and should be left unchanged.

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the form below. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Montage Portal

Montage Furniture Services provides furniture protection plans and claims processing services to a wide selection of furniture retailers and consumers.

Project Background

Montage was looking to build a new web portal for both Retailers and Consumers, which would integrate with Dynamics CRM and other legacy systems. The portal needed to be multi tenant and support branding and configuration for different Retailers. Trailhead architected the new Montage Platform, including the Portal and all of it’s back end integrations, did the UI/UX and then delivered the new system, along with enhancements to DevOps and processes.

Logistics

We’ve logged countless miles exploring the tech world. In doing so, we gained the experience that enables us to deliver your unique software and systems architecture needs. Our team of seasoned tech vets can provide you with:

Custom App and Software Development

We collaborate with you throughout the entire process because your customized tech should fit your needs, not just those of other clients.

Cloud and Mobile Applications

The modern world demands versatile technology, and this is exactly what your mobile and cloud-based apps will give you.

User Experience and Interface (UX/UI) Design

We want your end users to have optimal experiences with tech that is highly intuitive and responsive.

DevOps

This combination of Agile software development and IT operations provides you with high-quality software at reduced cost, time, and risk.

Trailhead stepped into a challenging project – building our new web architecture and redeveloping our portals at the same time the business was migrating from a legacy system to our new CRM solution. They were able to not only significantly improve our web development architecture but our development and deployment processes as well as the functionality and performance of our portals. The feedback from customers has been overwhelmingly positive. Trailhead has proven themselves to be a valuable partner.

– BOB DOERKSEN, Vice President of Technology Services
at Montage Furniture Services

Technologies Used

When you hit the trails, it is essential to bring appropriate gear. The same holds true for your digital technology needs. That’s why Trailhead builds custom solutions on trusted platforms like .NET, Angular, React, and Xamarin.

Expertise

We partner with businesses who need intuitive custom software, responsive mobile applications, and advanced cloud technologies. And our extensive experience in the tech field allows us to help you map out the right path for all your digital technology needs.

  • Project Management
  • Architecture
  • Web App Development
  • Cloud Development
  • DevOps
  • Process Improvements
  • Legacy System Integration
  • UI Design
  • Manual QA
  • Back end/API/Database development

We partner with businesses who need intuitive custom software, responsive mobile applications, and advanced cloud technologies. And our extensive experience in the tech field allows us to help you map out the right path for all your digital technology needs.

Our Gear Is Packed and We're Excited to Explore with You

Ready to come with us? 

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the contact form. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Thank you for reaching out.

You’ll be getting an email from our team shortly. If you need immediate assistance, please call (616) 371-1037.