Understanding Customer Reviews using Snowflake Cortex
Overview
Understanding customer feedback is critical for businesses, but analyzing large volumes of unstructured text can be challenging. In this solution, you will use Cortex AISQL to systematically gain insights from unstructured customer feedback.
This solution is one of the templates inside Snowflake, that allows you to leverage multiple AISQL functions to answer different use case questions upon customer reviews. You can access the template from within Snowflake by clicking the “Open in Snowflake” button above, or you can follow the instructions below to execute this code on your own.
Tasty Bytes is a global e-commerce company selling different merchandise. They collect customer reviews to gain insights into how their products are performing.
The following code can be copied into a notebook to execute. It leverages multiple AISQL functions to answer different use case questions upon customer reviews.
Step 1: Set up your environment and data
Let's begin by running the query below. It sets the proper context for this session. It also creates and populates two tables, PRODUCT_REVIEWS and PRODUCT_CATALOG, with sample data for our analysis.
Create a Project > Notebook and add the following SQL code and add the cell name: IMPORT_DATA_SQL
Now add this code to another SQL cell and name it: CHECK_DATA_SQL
Step 2: Correlate sentiment with ratings
As a first step, let's perform a quick sanity check. We'll use the SNOWFLAKE.CORTEX.SENTIMENT function to score the sentiment of each review. We can then check if it correlates with the user-provided star rating to see if they align.
Add the following code to another SQL cell and name it: SENTIMENT_CHECK_SQL
Step 3: Find top issues in a category
Now, let's dig deeper. Suppose you want to know what the biggest complaints are for 'Electronics'. You can focus on the ones with negative sentiments, and use AI_AGG to analyze all relevant reviews and aggregate the common themes into a single summary.
Add the following code to another SQL cell and name it: AGG_TOP_ISSUES_SQL
-- The text may not display fully in the SQL cell. Please hover or double-click on the SQL cell to view the full text. Print the result to a dataframe for easier reading using the following code in a Python cell, name it: DISPLAY_TOP_ISSUES_PY
Step 4: Identify the most common issues
To answer this question, we start with filtering to Clothing category. Another way to identify comments that mentioned product issue is to leverage our latest AI_FILTER to conduct filtering using natural language.
The next step we use the AI_AGG function to get a list of all product issues mentioned.
Create a SQL cell to add the following code and name it: COMMON_ISSUE_SQL
Now, we'll print the result to a dataframe for easier reading. Use the following Python code and name the cell:
Step 5: Productionalize the pipeline
With the issues suggested through the AI_AGG function pipeline above, we can now leverage AI_CLASSIFY to turn into continuous data pipeline to keep classify the reviews.
Paste the following code in a SQL cell and name it CLASSIFY_SQL
Step 6: Generate responses to customer complaints
Finally, let's close the loop. You can use AI_COMPLETE to help your support team draft empathetic and relevant responses to negative reviews, improving customer satisfaction at scale.
Create another SQL Cell and name it: GENERATE_SQL. Add the following code to it:
- End-to-End Workflow: You can chain Cortex AI functions together (SENTIMENT -> AI_AGG -> AI_CLASSIFY -> AI_COMPLETE) to build a powerful analysis pipeline entirely within Snowflake.
- Insight from Unstructured Data: You don't need complex data science tools to extract valuable insights from text. All of this was done with familiar SQL.
- Automate and Scale: By identifying common issues and creating classifiers, you can automate the process of tracking feedback and responding to customers more efficiently.
Code Example
classified_reviews AS ( SELECT review_id, review_text, AI_CLASSIFY( review_text, [ 'Sizing issue', 'Color issue', 'Fabric quality issue', 'Washing problem', 'Pricing issue' ] ) as classification FROM clothing_issue_reviews ) classified_reviews AS ( SELECT review_id, review_text, AI_CLASSIFY( review_text, [ 'Sizing issue', 'Color issue', 'Fabric quality issue', 'Washing problem', 'Pricing issue' ] ) as classification FROM clothing_issue_reviews ) classified_reviews AS ( SELECT review_id, review_text, AI_CLASSIFY( review_text, [ 'Sizing issue', 'Color issue', 'Fabric quality issue', 'Washing problem', 'Pricing issue' ] ) as classification FROM clothing_issue_reviews ) classified_reviews AS ( SELECT review_id, review_text, AI_CLASSIFY( review_text, [ 'Sizing issue', 'Color issue', 'Fabric quality issue', 'Washing problem', 'Pricing issue' ] ) as classification FROM clothing_issue_reviews ) classified_reviews AS ( SELECT review_id, review_text, AI_CLASSIFY( review_text, [ 'Sizing issue', 'Color issue', 'Fabric quality issue', 'Washing problem', 'Pricing issue' ] ) as classification FROM clothing_issue_reviews )
Get Started
This content is provided as is, and is not maintained on an ongoing basis. It may be out of date with current Snowflake instances