Sentiment analysis is the process of identifying the sentiment expressed in a piece of text, such as a tweet, customer review, or news article. With the help of Azure Text Analytics, an AI-powered service from Microsoft, it’s possible to perform sentiment analysis on large volumes of text data quickly and easily. In this article, we will explore how to use Azure Text Analytics for sentiment analysis with a real-life project.
Real-Life Project: Analyzing Customer Reviews For our real-life project, we will be analyzing customer reviews for a restaurant. The restaurant has recently launched a new menu, and they want to know how customers are responding to the new dishes. By performing sentiment analysis on the customer reviews, we can identify any negative feedback and take corrective action to improve the customer experience.
– Step 1: Collect Customer Reviews The first step is to collect customer reviews. We can do this by using a web scraping tool or by manually copying and pasting the reviews into a text file. For our project, we will be using a sample dataset of customer reviews for a restaurant.
– Step 2: Prepare the Data Once we have collected the customer reviews, we need to prepare the data for analysis. We can do this by cleaning the data, removing any irrelevant information, and converting the data into a format that can be analyzed by Azure Text Analytics. For our project, we will be using Python to clean and prepare the data.
import pandas as pd
import re
# Load the data into a pandas dataframe
df = pd.read_csv('customer_reviews.csv')
# Clean the text data
def clean_text(text):
text = re.sub('[^a-zA-Z0-9\n\.]', ' ', text) # Remove special characters
text = text.lower() # Convert to lowercase
text = text.strip() # Remove leading/trailing spaces
text = re.sub(' +', ' ', text) # Remove extra spaces
return text
df['text'] = df['text'].apply(clean_text)
# Save the cleaned data to a new file
df.to_csv('cleaned_customer_reviews.csv', index=False)
This code loads the customer reviews into a pandas dataframe, cleans the text data using regular expressions, and saves the cleaned data to a new file called cleaned_customer_reviews.csv
.
– Step 3: Perform Sentiment Analysis with Azure Text Analytics Now that we have prepared the data, we can perform sentiment analysis with Azure Text Analytics. To do this, we need to create an Azure Text Analytics resource and obtain the API key and endpoint. We can then use the Python SDK for Azure Text Analytics to perform sentiment analysis on the customer reviews.
import os
from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential
import pandas as pd
# Load the data into a pandas dataframe
df = pd.read_csv('cleaned_customer_reviews.csv')
# Authenticate with Azure Text Analytics
endpoint = "https://<your-text-analytics-endpoint>.cognitiveservices.azure.com/"
key = "<your-text-analytics-api-key>"
credential = AzureKeyCredential(key)
# Create a TextAnalyticsClient
client = TextAnalyticsClient(endpoint=endpoint, credential=credential)
# Perform sentiment analysis on each review
results = []
for text in df['text']:
result = client.analyze_sentiment(documents=)[0]
sentiment = result.sentiment
confidence_scores = result.confidence_scores
results.append({'text': text, 'sentiment': sentiment, 'positive': confidence_scores.positive, 'negative': confidence_scores.negative, 'neutral': confidence_scores.neutral})
# Save the results to a new dataframe
results_df = pd.DataFrame(results)
# Save the results to a CSV file
results_df.to_csv('sentiment_analysis_results.csv', index=False)
his code first loads the cleaned customer reviews into a pandas dataframe. We then authenticate with Azure Text Analytics by specifying the endpoint and API key. We create a TextAnalyticsClient object and use the analyze_sentiment
method to perform sentiment analysis on each customer review. The results of the sentiment analysis are then saved to a new dataframe and to a CSV file.
In the next step, we will analyze the results of the sentiment analysis to identify any negative feedback.
– Step 4: Analyze the Results Now that we have performed sentiment analysis on the customer reviews, we can analyze the results to identify any negative feedback. We can do this by filtering the results to show only the reviews with a negative sentiment score.
# Load the results into a pandas dataframe
results_df = pd.read_csv('sentiment_analysis_results.csv')
# Filter the results to show only negative reviews
negative_reviews = results_df[results_df['sentiment'] == 'negative']
# Print the negative reviews
for index, row in negative_reviews.iterrows():
print(row['text'])
print('Sentiment: ' + row['sentiment'])
print('Positive: ' + str(row['positive']))
print('Negative: ' + str(row['negative']))
print('Neutral: ' + str(row['neutral']))
print()
This code loads the sentiment analysis results into a pandas dataframe and filters the results to show only the reviews with a negative sentiment score. We then print the negative reviews along with the sentiment score and confidence scores.
In our real-life project, we can use this information to identify the specific issues that customers are experiencing with the new menu items. For example, we may notice that several customers are complaining about the portion sizes or the level of spiciness in a particular dish. We can then take corrective action to address these issues and improve the customer experience.
In this article, we have explored how to perform sentiment analysis with Azure Text Analytics with a real-life project. By analyzing customer reviews for a restaurant, we were able to identify any negative feedback and take corrective action to improve the customer experience. Azure Text Analytics is a powerful tool that can be used for a wide range of applications, including social media monitoring, market research, and customer feedback analysis.
Contact us for more information or visit our blog.