September 24th, 2020

Opinion Mining with the Text Analytics Client Libraries

Isabella Cai
SDE II

With v5.1.0, the new Text Analytics client library now supports opinion mining. In this post, we’ll introduce you to the opinion mining concept and then jump into some code.

Introducing Opinion Mining

You can think of opinion mining as a more granular sentiment analysis, diving even deeper into the individual opinions that shape the overall sentiment. More formally, it provides in-depth analysis of opinions about aspects (i.e. attributes) of a product or a service, and is also referred to as Aspect-Based Sentiment Analysis.

Since this is a concept most clearly explained with examples, let’s consider a review (taken directly from the Text Analytics service documentation): “The room was great, but the staff was unfriendly”. Returning an overall sentiment for this review doesn’t capture the two distinct opinions of aspects of the hotel, so this is a perfect review to dive deeper into. By using opinion mining, we get that while the “room” is “great”, the “staff” is “unfriendly”. Here is a visual breakdown (also taken directly from the service documentation) of the information opinion mining will extract from this review:

Aspect Opinion Sentiment
room great positive
staff unfriendly negative

Let’s consider a scenario of a hotel manager looking to improve their hotel. This extra layer of opinion mining when conducting sentiment analysis on customer reviews allows them to determine that they should allocate extra resources to train their staff members.

Other scenarios where opinion mining is useful that aren’t shown in this blog include:

  • Creating word clouds from the analyzed opinions
  • Issue automation. For example, routing complaints to different teams based on the aspects that show up in the issues.
  • Analyzing social media streams

Opinion Mining with the new Text Analytics client library

Let’s expand on the previous example with some code! We’ll continue looking into the hotel manager scenario, where we’ll focus on identifying complaints in reviews (special thanks to Mari for this usage scenario).

I will use Python in this post, but if you would like a demonstration in a different language, please refer to the following samples in the supported languages for: .NET, JavaScript/TypeScript, and Java.

First, here are the reviews the hotel manager has scraped. Just analyzing each review on a sentence level isn’t enough: different aspects of the hotel have different opinions and sentiments associated with them, and returning an overall sentiment for the review won’t tell them what they should change about their hotel.

reviews = [
    "The room was great, but the staff was unfriendly",
    "The rooms were beautiful. The AC was good and quiet.",
    "The breakfast was good, but the toilet was smelly.",
    "Loved this hotel - good breakfast - nice shuttle service - clean rooms.",
    "I had a great unobstructed view of the Microsoft campus.",
    "Nice rooms but bathrooms were old and the toilet was dirty when we arrived.",
    "The toilet was broken."
]

Run the following pip command to install the library:

pip install azure-ai-textanalytics --pre

To use our Text Analytics client library, the hotel manager first needs to create a service client for their network calls. See the client library’s documentation on how to create and authenticate a client for more information.

from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient

credential = AzureKeyCredential("<api_key>")
client = TextAnalyticsClient(endpoint="<endpoint>", credential=credential)

Now it is time to conduct opinion mining on these reviews! Since opinion mining is a form of sentiment analysis, the hotel manager needs to call analyze_sentiment. To let the service know that they also want opinion mining to be done on top of the analysis, they also need to pass show_opinion_mining=True to the call.

analyzed_reviews = client.analyze_sentiment(reviews, show_opinion_mining=True)

Now that the hotel manager has used our client library to analyze their reviews with opinion mining, they need to extract the information useful for their scenario. Since they’re looking to improve the experience of their hotel, they are most interested in the content and frequency of complaints.

complaints = {}
for review in analyzed_reviews:
    for sentence in review.sentences:
        for mined_opinion in sentence.mined_opinions:
            if mined_opinion.aspect.sentiment == "negative":
                aspect_text = mined_opinion.aspect.text
                opinions = [opinion.text for opinion in mined_opinion.opinions]
                if aspect_text in complaints:
                    complaints[aspect_text].extend(opinions)
                else:
                    complaints[aspect_text] = opinions

With these complaints, they can see what aspects of their hotel people feel negatively about, and how many people agree.

for complaint in complaints:
    opinions = complaints[complaint]
    print(f"There have been '{len(opinions)}' complaint(s) about '{complaint}'")
    joined_opinions = "', '".join(opinions)
    print(f"...The specific complaints are: '{joined_opinions}'")

Output:

There have been '1' complaint(s) about 'staff'
...The specific complaints are: 'unfriendly'
There have been '3' complaint(s) about 'toilet'
...The specific complaints are: 'smelly', 'dirty', 'broken'
There have been '1' complaint(s) about 'bathrooms'
...The specific complaints are: 'old'

Thanks to the power of opinion mining, the hotel manager can see the above aspects of their hotel need to be improved, and that they especially need to resolve their toilet situation.

Further Documentation

If you’re interested in getting started with our Text Analytics client libraries, here are the links for each language we support

Azure SDK Blog Contributions

Thank you for reading this Azure SDK blog post! We hope that you learned something new and welcome you to share this post. We are open to Azure SDK blog contributions. Please contact us at azsdkblog@microsoft.com with your topic and we’ll get you setup as a guest blogger.

Azure SDK Links

Author

Hi my name is Isabella and I work on Python SDKs for Azure

0 comments

Discussion are closed.