# Learn how to perform opinion mining with Azure Cognitive Services

# Dive deeper into sentiment analysis

The Azure Cognitive Services Text Analytics API (opens new window) provides the sentiment analysis feature. You can use this to analyze if a sentence is positive, negative, or neutral. And to get a more advanced analysis, you can use the opinion mining feature of sentiment analysis (opens new window), which analyzes a sentence on a deeper level and can identify multiple different sentiments in a sentence. For instance, the sentence "I love spicy food, but I hate how it makes me feel", is both positive and negative. Opinion mining can detect those sentiments.

In this post, we'll use the opinion mining feature (opens new window) of the Cognitive Services Text Analytics API (opens new window).

# Prerequisites

If you want to follow along, you'll need the following:

# Use the Sentiment Analysis feature of the Text Analytics API

To use the opinion mining feature, we need a Cognitive Services Text Analytics API, which we'll create in the Azure portal.

  1. Click the Create a resource button (the plus-sign in the top left corner)
  2. Search for Text Analytics, select the "Text Analytics" result and click Create
    1. This brings you to the Create Text Analytics blade
    2. Select a Resource Group
    3. Fill in a Name for the service
    4. Pick a Pricing tier
    5. Click Review + create and Create after that to create the service

(Create Text Analytics in the Azure portal)

When the Text Analytics Cognitive Service is created, navigate to it in the Azure portal. In the Keys and Endpoint blade, copy the Access key and the Endpoint for the service. We'll need those later.

(Keys and Endpoints blade in the Azure portal)

Let's use the Text Analytics service in an application that we'll create with Visual Studio.

  1. Open Visual Studio
  2. Create a new console application by navigating to File > New > Project and selecting Console Application
  3. The first thing that we need to do, is to reference a NuGet package to work with the Text Analytics service. Right-click the project file and select Manage NuGet Packages
  4. Find the package Azure.AI.TextAnalytics and install it

(Azure.AI.TextAnalytics NuGet package in Visual Studio)

  1. Next, create the code in the Program.cs file. The file should look like this:
using System;
using System.Collections.Generic;
using Azure;
using Azure.AI.TextAnalytics;

namespace OpinionMining
{
    class Program
    {
        private static readonly AzureKeyCredential credentials = new AzureKeyCredential("Access Key");
        private static readonly Uri endpoint = new Uri("Service Endpoint");
        static void Main(string[] args)
        {
            var client = new TextAnalyticsClient(endpoint, credentials);
   
            SentimentAnalysisWithOpinionMiningExample(client);

            Console.WriteLine("Press any key to exit the app");
            Console.ReadLine();
        }

        static void SentimentAnalysisWithOpinionMiningExample(TextAnalyticsClient client)
        {
            var documents = new List<string>
            {
                "The food and service were unacceptable, but the concierge were nice."
            };

            AnalyzeSentimentResultCollection reviews = client.AnalyzeSentimentBatch(documents, options: new AnalyzeSentimentOptions()
            {
                IncludeOpinionMining = true
            });

            foreach (AnalyzeSentimentResult review in reviews)
            {
                Console.WriteLine($"Document sentiment: {review.DocumentSentiment.Sentiment}\n");
                Console.WriteLine($"\tPositive score: {review.DocumentSentiment.ConfidenceScores.Positive:0.00}");
                Console.WriteLine($"\tNegative score: {review.DocumentSentiment.ConfidenceScores.Negative:0.00}");
                Console.WriteLine($"\tNeutral score: {review.DocumentSentiment.ConfidenceScores.Neutral:0.00}\n");
                foreach (SentenceSentiment sentence in review.DocumentSentiment.Sentences)
                {
                    Console.WriteLine($"\tText: \"{sentence.Text}\"");
                    Console.WriteLine($"\tSentence sentiment: {sentence.Sentiment}");
                    Console.WriteLine($"\tSentence positive score: {sentence.ConfidenceScores.Positive:0.00}");
                    Console.WriteLine($"\tSentence negative score: {sentence.ConfidenceScores.Negative:0.00}");
                    Console.WriteLine($"\tSentence neutral score: {sentence.ConfidenceScores.Neutral:0.00}\n");

                    foreach (MinedOpinion minedOpinion in sentence.MinedOpinions)
                    {
                        Console.WriteLine($"\tAspect: {minedOpinion.Aspect.Text}, Value: {minedOpinion.Aspect.Sentiment}");
                        Console.WriteLine($"\tAspect positive score: {minedOpinion.Aspect.ConfidenceScores.Positive:0.00}");
                        Console.WriteLine($"\tAspect negative score: {minedOpinion.Aspect.ConfidenceScores.Negative:0.00}");
                        foreach (OpinionSentiment opinion in minedOpinion.Opinions)
                        {
                            Console.WriteLine($"\t\tRelated Opinion: {opinion.Text}, Value: {opinion.Sentiment}");
                            Console.WriteLine($"\t\tRelated Opinion positive score: {opinion.ConfidenceScores.Positive:0.00}");
                            Console.WriteLine($"\t\tRelated Opinion negative score: {opinion.ConfidenceScores.Negative:0.00}");
                        }
                    }
                }
                Console.WriteLine($"\n");
            }
        }
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

This code connects to the Text Analytics API using the access key and the endpoint URL. Once connected, it calls the AnalyzeSentimentBatch method with the "IncludeOpinionMining = true" parameter. This lets the Text Analytics service use the opinion mining feature to analyze the sentence

  1. When you run the program, the code loops through the results and writes the MinedOpinions on the screen for the sentence "The food and service were unacceptable, but the concierge were nice.". This results in the image below, which shows that the sentence contains both positive and negative sentiments. It even figured out the subject of the sentiments (which it categorizes as Aspects) as food, service and concierge.

(Result of opinion mining with the Text Analytics API)

# Conclusion

The Opinion Mining feature of the Azure Cognitive Service Text Analytics API (opens new window) can analyze multiple sentiments within a sentence. Go and check it out!