🎯Automated Customer Review Sentiment Analysis for Businesses

Merwan Chinta
CodeNx
Published in
12 min readNov 13, 2023

🌟Objective

HappyRetail.com, an emerging online e-commerce platform, understanding the increasing volume of customer feedback has become a significant challenge. Their goal is to efficiently and accurately analyze customer feedback from their site. The system that would be built should extract crucial insights from customer reviews, enhancing the understanding of customer needs. Our objective is to categorize feedback, thereby refining customer service, informing product development, and shaping business strategies based on authentic customer feedback.

Design

In the digital era, customer feedback is invaluable for businesses. Online reviews and social media comments offer a wealth of insights. The key to unlocking this potential lies in Automated Customer Review Sentiment Analysis, leveraging Artificial Intelligence (AI). By using AI technologies, businesses can swiftly and accurately process vast amounts of text data, categorizing reviews into positive, negative, or neutral sentiments. This automation exceeds manual review capabilities, providing a rapid and precise grasp of customer opinions.

Implementing this technology streamlines customer feedback analysis and turns it into actionable insights. This approach aids in informed decision-making, elevates customer experiences, and ensures a swift response to market trends, making it an essential tool for businesses focused on customer-centric growth. 🚀📈💬

The design revolves around leveraging Azure Cognitive Services, specifically the Text Analytics API, to process and analyze customer reviews. This involves collecting reviews from various sources, preprocessing the data for consistency, applying sentiment analysis, and then visualizing the results for easy interpretation and actionable insights. The system should be designed to be automated, scalable, and integrate-able with existing business processes at HappyRetail.com.

Leverage Azure Cognitive Services, the Text Analytics, to process and analyze customer reviews/feedback automatically
Image source: Created by Author

Efficient Data Collection: The Foundation of AI-Powered Customer Feedback Analysis

In the world of customer feedback analysis powered by artificial intelligence (AI), one of the fundamental pillars is the efficient collection of data. This pivotal step forms the bedrock upon which businesses can build their understanding of customer sentiments and preferences. We’ll delve into the intricacies of data collection, highlighting the unique steps and considerations that can make or break the success of your AI-driven analysis.

Identifying Data Sources: The first step is to identify all potential sources of customer feedback. For HappyRetail.com, this includes their website, social media platforms, email surveys, and any other online forums or review sites where customers might leave feedback.

Automated Data Aggregation: To handle the volume of data efficiently, we need to set up automated systems for aggregating feedback. This could involve web scraping tools, APIs provided by review sites, or integrations with social media platforms. Azure Logic Apps can be particularly useful here, offering capabilities to automate workflows and data collection from various sources.

Data Formatting and Standardization: Once collected, the data must be formatted and standardized to ensure consistency. This means converting all feedback into a uniform format, which could involve tasks like removing HTML tags from web-scraped data, standardizing date formats, and ensuring text is in a readable and analyzable state.

Data Quality: Quality control checks are essential to ensure the reliability of the data. This includes filtering out spam, duplicate entries, and irrelevant content. Optionally, we can use algorithms to identify and remove such anomalies to maintain the integrity of the dataset.

Compliance and Privacy Considerations: While collecting data, it’s crucial to comply with data protection laws and regulations like GDPR or CCPA. This means ensuring customer data is anonymized or de-identified where necessary and that the data collection processes adhere to legal standards.

Initial Data Storage: The collected data needs to be stored in a secure and accessible location before processing. Azure Data Lake or Azure Blob Storage can be ideal for this purpose, offering scalable and secure storage solutions.

Preparing for Data Processing: Finally, the collected data should be prepared for the next stage, which is processing and analysis. This preparation involves tagging or categorizing data based on its source or content type, making it easier to process in subsequent steps.

Data Preprocessing: Harnessing Azure Databricks

Data preprocessing is a critical phase, where the raw customer feedback data is transformed into a clean, standardized format suitable for analysis. Azure Databricks plays a pivotal role in this process, offering a powerful and collaborative environment for data processing. Key Steps in Data Preprocessing:

Data Cleaning

Data Transformation

Data Normalization

Handling Missing Values

Feature Engineering

For simplicity and understanding, let’s take a sneak peek at sample subset of data and how it looks:

CustomerId,Review
123,"Loved the product, fast delivery!"
456,"Not satisfied with the product quality"
789,"Great service, will buy again"
101,"Delayed shipment, but good customer support"
112,"Product is decent for the price"

The below code will illustrate how to perform basic preprocessing tasks on a hypothetical customer feedback dataset. The code is a standard Spark script, Azure Databricks acts as the powerful and optimized platform where this script is executed, providing enhanced capabilities for managing and processing big data.

from pyspark.sql import SparkSession
from pyspark.sql.functions import col, lower, regexp_replace, size, split

# Azure Blob Storage account information
storage_account_name = "your_storage_account_name"
storage_account_access_key = "your_storage_account_access_key"
container_name = "your_container_name"
blob_file = "path/in/container/customer_feedback.csv"

# Spark session with Azure Blob Storage configuration
spark = SparkSession.builder \
.appName("CustomerFeedbackAnalysis") \
.config("fs.azure.account.key." + storage_account_name + ".blob.core.windows.net", storage_account_access_key) \
.getOrCreate()

# Define the Blob Storage URL
blob_storage_url = f"wasbs://{container_name}@{storage_account_name}.blob.core.windows.net/{blob_file}"

# Load the dataset from Azure Blob Storage
df = spark.read.csv(blob_storage_url, header=True, inferSchema=True)

# Display the first few records for overview
df.show()

# Data Cleaning
# Remove duplicate entries
df = df.dropDuplicates()

# Handle missing values - here we're dropping rows with missing 'Review' field
df = df.na.drop(subset=["Review"])

# Data Transformation
# Convert text to lowercase
df = df.withColumn("Review", lower(col("Review")))

# Remove punctuation and special characters from the reviews
df = df.withColumn("Review", regexp_replace(col("Review"), "[^a-zA-Z0-9 ]", ""))

# Feature Engineering
# For simplicity, let's count the number of words in each review
df = df.withColumn("WordCount", size(split(col("Review"), " ")))

# Show transformed data
df.show()

# Define the output path in Blob Storage
output_blob_file = "path/in/container/processed_customer_feedback.csv"

# Save the processed data back to Azure Blob Storage
output_blob_storage_url = f"wasbs://{container_name}@{storage_account_name}.blob.core.windows.net/{output_blob_file}"
df.write.csv(output_blob_storage_url, header=True)

# Stop the Spark session
spark.stop()

Azure Databricks provides a managed Spark service with enhanced features and an optimized environment for big data processing and analytics.

Here’s how it fits into the context of the code:

Managed Spark Environment: Azure Databricks offers a fully managed Apache Spark environment. The code snippet is a Spark script, and Azure Databricks is the platform where this script would be executed. Databricks manages the underlying Spark infrastructure, allowing us to focus on the data processing without worrying about the operational aspects of Spark cluster management.

Integration with Azure Services: Databricks seamlessly integrates with Azure services, including Azure Blob Storage. The configuration lines in the script that set up the connection to Azure Blob Storage (fs.azure.account.key...) are an example of this integration. Databricks makes it easier to connect to and interact with data stored in Azure Blob Storage.

Collaborative Workspace: Azure Databricks provides a collaborative workspace that can be used by data engineers, data scientists, and analysts. The code for data preprocessing can be written, executed, and shared within this workspace.

Optimized Performance: Databricks optimizes the performance of Spark jobs. The platform includes optimizations for both compute and IO which can make your Spark jobs run faster and more efficiently. This is particularly beneficial when dealing with large datasets like customer feedback data.

Scalability and Flexibility: The Databricks environment can automatically scale depending on the workload. This means that for processing large volumes of data (like in sentiment analysis tasks), it can dynamically allocate more resources to handle the load, and then scale down when the heavy lifting is done, optimizing cost and performance.

Data Processing Capabilities: The actual data processing tasks such as cleaning, transformation, and feature engineering (as shown in the code) are performed using Spark’s capabilities. Databricks enhances these capabilities by providing a more user-friendly interface and additional tools and libraries to simplify the process.

With this, we now pre-processed the data using Azure Databricks environment/Spark script and the transformed data is stored in Azure Blobs. Now we will use text analysis to analyze the sentiments in customer feedback.

More on Azure Blobs can be found in one of my article:

Sentiment Analysis: Azure Cognitive Services

We will use Azure Cognitive Services for text analysis and sentiment analysis. To trigger text analysis as soon as data is inserted or updated in Azure Blob Storage, we can configure Azure Functions along with Azure Event Grid or Azure Logic Apps. Here’s an overview of how this can be set up.

Azure Event Grid and Azure Function

Azure Event Grid can monitor our Azure Blob Storage for any new blobs or updated blobs. Once a new or updated blob is detected, it can trigger an Azure Function.

  1. Event Subscription: Set up an event subscription in Azure Event Grid that watches for the ‘BlobCreated’ or ‘BlobUpdated’ events in your Azure Blob Storage container.
  2. Azure Function: Create an Azure Function with a Blob trigger. This function is triggered whenever a new file is added to or updated in the specified Blob Storage container. The function can then process this file.
  3. Text Analysis in .NET: Inside the Azure Function, use .NET code to call Azure Cognitive Services for text analysis. The Function can read the content of the blob, perform the analysis, and then store the results back in Azure Blob Storage or another data store as required.

Using Azure Logic Apps and Azure Function

Alternatively, Azure Logic Apps can be used to create a workflow that triggers when a new blob is added:

  1. Logic App Trigger: Create a Logic App with a trigger that listens for new blobs in your Azure Blob Storage.
  2. Calling Azure Function: The Logic App can then call an Azure Function that performs the text analysis. This function can be written in .NET and utilize Azure Cognitive Services.
  3. Process and Store Results: After the text analysis is done, the Azure Function can store the results back in Azure Blob Storage or another destination.

Sample code for Azure Function for Text Analysis

using System;
using System.Globalization;
using System.IO;
using System.Linq;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
using Azure.AI.TextAnalytics;
using Azure;
using Azure.Storage.Blobs;
using CsvHelper;

public static class TextAnalysisFunction
{
[FunctionName("TextAnalysisFunction")]
public static void Run([BlobTrigger("your-container/{name}", Connection = "AzureWebJobsStorage")] Stream myBlob, string name, ILogger log)
{
log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");

// Initialize Text Analytics client
var credentials = new AzureKeyCredential("YourCognitiveServicesKey");
var textAnalyticsClient = new TextAnalyticsClient(new Uri("YourCognitiveServicesEndpoint"), credentials);

// Create a BlobServiceClient
BlobServiceClient blobServiceClient = new BlobServiceClient("YourAzureBlobStorageConnectionString");

// Get the container and blob reference
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("YourContainerName");
BlobClient blobClient = containerClient.GetBlobClient("processed_customer_feedback.csv");

// Download the blob to a stream
using var memoryStream = new MemoryStream();
blobClient.DownloadTo(memoryStream);
memoryStream.Position = 0;

// Read the CSV data
using var reader = new StreamReader(memoryStream);
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
var records = csv.GetRecords<CustomerFeedback>().ToList();

// Process and analyze each review for sentiment
foreach (var record in records)
{
// Call the Text Analytics API to analyze sentiment
DocumentSentiment documentSentiment = textAnalyticsClient.AnalyzeSentiment(record.Review);
Console.WriteLine($"Customer {record.CustomerId} review sentiment: {documentSentiment.Sentiment}");

// Additional processing and storing results
}
}
}


public class CustomerFeedback
{
public int CustomerId { get; set; }
public string Review { get; set; }
}

The Run method in TextAnalysisFunction is the entry point of this Azure Function. It is triggered when a new blob is added to the specified container.

In this setup, whenever new data is preprocessed and stored in Azure Blob Storage by your Spark script in Azure Databricks, the Azure Function is automatically triggered to perform text analysis, making the entire process efficient and automated.

Data Storage and Management: Cosmos DB

After processing and analyzing the customer feedback data using Azure Databricks and Azure Functions, the next crucial step is Data Storage and Management. This phase involves storing the processed data, including the sentiment analysis results, in a way that is secure, accessible, and conducive to further analysis and reporting. Azure provides several options for this, each with its own set of features and benefits.

Azure Cosmos DB is a good choice for semi-structured data, providing high throughput and low-latency data access, suitable for scalable applications.

using System;
using System.Globalization;
using System.IO;
using System.Linq;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Azure.AI.TextAnalytics;
using Azure;
using Azure.Storage.Blobs;
using CsvHelper;
using Microsoft.Azure.Cosmos;
using Newtonsoft.Json;
using System.Threading.Tasks;

public class CustomerFeedback
{
public int CustomerId { get; set; }
public string Review { get; set; }
public string Sentiment { get; set; } // Added to store sentiment result
}

public static class TextAnalysisFunction
{
private static readonly string cognitiveServicesKey = "YourCognitiveServicesKey";
private static readonly string cognitiveServicesEndpoint = "YourCognitiveServicesEndpoint";
private static readonly string cosmosDbEndpoint = "Your-CosmosDB-Endpoint";
private static readonly string cosmosDbKey = "Your-CosmosDB-Key";
private static CosmosClient cosmosClient = new CosmosClient(cosmosDbEndpoint, cosmosDbKey);

[FunctionName("TextAnalysisFunction")]
public static async Task Run([BlobTrigger("your-container/{name}", Connection = "AzureWebJobsStorage")] Stream myBlob, string name, ILogger log)
{
// Initialize Text Analytics client
var credentials = new AzureKeyCredential(cognitiveServicesKey);
var textAnalyticsClient = new TextAnalyticsClient(new Uri(cognitiveServicesEndpoint), credentials);

// Read the CSV data
using var reader = new StreamReader(myBlob);
using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
var records = csv.GetRecords<CustomerFeedback>().ToList();

// Cosmos DB Container
var container = cosmosClient.GetContainer(databaseId, containerId);

// Process and analyze each review for sentiment
foreach (var record in records)
{
try
{
// Analyze sentiment
DocumentSentiment documentSentiment = textAnalyticsClient.AnalyzeSentiment(record.Review);
log.LogInformation($"Customer {record.CustomerId} review sentiment: {documentSentiment.Sentiment}");

// Add sentiment to record and store in Cosmos DB
record.Sentiment = documentSentiment.Sentiment.ToString();
await container.CreateItemAsync(record, new PartitionKey(record.CustomerId.ToString()));
}
catch (Exception ex)
{
log.LogError($"Error processing record: {ex.Message}");
}
}
}
}

Key Modifications and Additions:

  • The CustomerFeedback class has been updated to include a Sentiment property.
  • The Cosmos DB client is initialized with your account’s endpoint URI and primary key.
  • After sentiment analysis is performed on each record, the sentiment result is added to the CustomerFeedback object.
  • Each updated CustomerFeedback object is then asynchronously stored in the specified Cosmos DB container.
[BlobTrigger("your-container/{name}", Connection = "AzureWebJobsStorage")] Stream myBlob, string name
  • “your-container/{name}” is the path within your Blob Storage where the function listens for changes. "your-container" should be replaced with the actual name of your container in Azure Blob Storage. The {name} is a placeholder that gets replaced by the actual name of the blob that caused the trigger.

Replace the placeholder values for Azure Services with your actual configuration details.

Leveraging Azure Power BI: For Sentiment Analysis Insights

After the sentiment analysis of customer feedback is successfully performed and stored in Azure Cosmos DB, the next vital step for is to visualize and report these outcomes. Utilizing Azure Power BI for this task is a game-changer, offering powerful tools to create insightful dashboards and interactive reports. These visualizations play a key role in transforming raw data into actionable business intelligence, highlighting key sentiment trends and patterns that are crucial for strategic decision-making.

Key Features of Azure Power BI for Sentiment Analysis Reporting

  1. Data Integration: Seamlessly connect Power BI to Azure Cosmos DB, ensuring real-time data flow and up-to-date reporting.
  2. Interactive Dashboards: We create comprehensive dashboards that provide an at-a-glance overview of customer sentiment, with the capability to drill down into specific aspects for detailed analysis. We can also utilize various visualization tools like charts, graphs, and heat maps to represent sentiment data effectively.
  3. Trend Analysis and Pattern Identification: Use time series analyses to track sentiment trends over time, helping in identifying patterns related to product launches, marketing campaigns, or other business activities. Compare sentiment across different product categories, regions, or customer demographics.
  4. Custom Reports: Develop tailored reports focusing on specific areas of interest, such as product feedback, customer service experiences, or overall brand perception. Enable scheduled or on-demand report generation for different stakeholders.
  5. User Accessibility and Collaboration: Share dashboards and reports with team members and stakeholders through Power BI’s web-based service, ensuring that everyone has access to the latest insights. Foster a data-driven culture by enabling collaborative decision-making based on shared insights.

HappyRetail.com can effectively harness the power of sentiment analysis, turning customer feedback into a strategic asset that drives their business growth and customer satisfaction.

Conclusion

Our journey with HappyRetail.com to implement an AI-driven sentiment analysis using Azure’s suite of tools represents a significant stride in harnessing technology for deep customer insights. From collecting and preprocessing data in Azure Databricks to performing sentiment analysis with Azure Cognitive Services, and finally visualizing the results in Azure Power BI, each step has been crucial in transforming customer feedback into actionable business intelligence. This innovative approach not only enhances the understanding of customer sentiment but also empowers strategic decision-making and fosters a data-driven business culture.

Ultimately, this integration of AI and cloud technologies positions HappyRetail.com at the forefront of customer-centric innovation, setting a benchmark for leveraging technology to drive business success and customer satisfaction.

I trust this information has been valuable to you. 🌟 Wishing you an enjoyable and enriching learning journey!

📚 For more insights like these, feel free to follow 👉 Merwan Chinta

--

--

Merwan Chinta
CodeNx

🚧 Roadblock Eliminator & Learning Advocate 🖥️ Software Architect 🚀 Efficiency & Performance Guide 🌐 Cloud Tech Specialist