Using Customer Reviews To Know Product's Performance In Market - Azure Sentiment Analysis

Today I'll be mentioning one of the useful functions of Azure Text Analytics - Sentiment Analysis. Azure text analytics is a cloud-based offering from Microsoft and it provides Natural Language Processing over raw text. 

USECASE DESCRIBED

In this article, I will explain how to use customer-provided product reviews to understand the market insight and how one can take a call on manufacturing the products in the future. Here is the pictorial representation of this use case.

Azure Sentiment Analysis

Here are the high-level steps of how we can achieve this entire flow,

Step 1

This entire process starts with the data collection part and for this, I'm using a CSV file with customer-provided reviews. Here is the gist of it,

Azure Sentiment Analysis

Step 2

Once data is collected, we need to import the data and for that, I'm using Jupyter Notebook inside Visual Studio Code. Here is the Python code to read and extract data from CSV file,

import csv
feedbacks = []
counter = 0
with open('Feedback.csv', mode='r', encoding='utf8') as csv_file:
    reader = csv.DictReader(csv_file)
    for row in reader:
        counter+=1
        if (counter <= 9):
            feedbacks.append(row['reviews.title'] + '.')

Step 3

Next, we need to create a Text Analytics resource in Azure to get a key and an endpoint. This can be done by log onto the Azure portal and search for Text Analytics,

Azure Sentiment Analysis

Click on the above button and then click on Create. It will open up a new page as shown below, wherein you need to furnish all the basic details,

Azure Sentiment Analysis

Clicking on Review + create will create a new Text Analytics resource for you.

Step 4

Grab key and endpoint from the Azure portal and create 2 variables to store them.

Azure Sentiment Analysis

key = "TEXT_ANALYTICS_KEY"
endPoint = "TEXT_ANALYTICS_ENDPOINT"

Step 5

Next is to install the required Python module. In VS Code, open a new terminal and install the below module using Pip,

pip install azure.ai.textanalytics

Step 6

Import the modules and create client objects as shown below,

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

client = TextAnalyticsClient(endpoint=endPoint, credential=AzureKeyCredential(key))
response = client.analyze_sentiment(documents=feedbacks)
review = type('', (), {'positive':0, 'negative':0, 'neutral':0})()
for idx, sentence in enumerate(response):
    print("Sentence {}: {}".format(idx+1, sentence.sentiment))
    if(sentence.sentiment == "positive"):
        review.positive = review.positive + 1
    elif (sentence.sentiment == "negative"):
        review.negative = review.negative + 1
    else:
        review.neutral = review.neutral + 1

At this point, if you will run the code, you will get the results from sentiment analysis. 

Step 7

Now, it's time to plot the analysis results. This can be done by using MatplotLib. If VS Code is not detecting it, then you can install it using Pip (pip install matplotlib).

Here is the code to plot the results,

import matplotlib.pyplot as plot
figure = plot.figure()
ax = figure.add_axes([0,0,1,1])
x_values = ['Positive', 'Negative', 'Neutral']
y_values = [review.positive, review.negative, review.neutral]
ax.bar(x_values, y_values)

Step 8

If everything went well so far, then on executing the application, you will see similar output as shown below,

Azure Sentiment Analysis

Conclusion and Takeaway

Looking at the above chart, the manufacturer can take a call and decide, whether he needs to increase the production or slow down the production and understand the customer's pain points.

Hope you enjoyed reading this article. There may be a few steps, which I didn't explain here. So, in case, if you got stuck at any point while reading this, I would recommend you to watch out for my video demonstrating end-to-end flow on my channel.

Happy learning!


Similar Articles