Sentiment analysis using Google sheets and Snowflake Cortex
Learn how to load user review data from Google Sheets intoS nowflake Cortex based vector store, and perform sentiment analysis using Snowflake Cortex's sentiment function.
Join our newsletter to get all the insights on the data stack
Should you build or buy your data pipelines?
Download our free guide and discover the best approach for your needs, whether it's building your ELT solution in-house or opting for Airbyte Open Source or Airbyte Cloud.
Run Sentiment analysis from Snowflake Cortex Airbyte Destination Records
from snowflake import connector
from IPython.display import display, HTML
def get_db_connection():
return connector.connect(
account=userdata.get("SNOWFLAKE_HOST"),
role=userdata.get("SNOWFLAKE_ROLE"),
warehouse=userdata.get("SNOWFLAKE_WAREHOUSE"),
database=userdata.get("SNOWFLAKE_DATABASE"),
schema=userdata.get("SNOWFLAKE_SCHEMA"),
user=userdata.get("SNOWFLAKE_USERNAME"),
password=userdata.get("SNOWFLAKE_PASSWORD"),
)
def get_sentiment_analysis_from_snowflake(table_name):
conn = get_db_connection()
cur = conn.cursor()
query = f"""
SELECT
DOCUMENT_CONTENT,
CASE
WHEN SENTIMENT_SCORE > 0 THEN 'Positive'
WHEN SENTIMENT_SCORE < 0 THEN 'Negative'
ELSE 'Neutral'
END AS SENTIMENT
FROM (
SELECT
SNOWFLAKE.CORTEX.SENTIMENT(DOCUMENT_CONTENT) AS SENTIMENT_SCORE,
DOCUMENT_CONTENT
FROM {table_name}
LIMIT 5
);
"""
cur.execute(query)
result = cur.fetchall()
col_names = [desc[0] for desc in cur.description]
df = pd.DataFrame(result,columns=col_names)
return df
response = get_sentiment_analysis_from_snowflake("amazon_reviews")
display(HTML(response.to_html(index=False)))
DOCUMENT_CONTENT SENTIMENT
"reviewerName: 0mie\noverall: 5\nreviewText: Purchased this for my device, it worked as advertised. You can never have too much phone memory, since I download a lot of stuff this was a no brainer for me.\nreviewTime: 2013-10-25\nday_diff: 409\nhelpful_yes: 0\nhelpful_no: 0\ntotal_vote: 0\nscore_pos_neg_diff: 0\nscore_average_rating: 0\nwilson_lower_bound: 0" Negative
"reviewerName: 1K3\noverall: 4\nreviewText: it works as expected. I should have sprung for the higher capacity. I think its made a bit cheesier than the earlier versions; the paint looks not as clean as before\nreviewTime: 2012-12-23\nday_diff: 715\nhelpful_yes: 0\nhelpful_no: 0\ntotal_vote: 0\nscore_pos_neg_diff: 0\nscore_average_rating: 0\nwilson_lower_bound: 0" Negative
"reviewerName: 1m2\noverall: 5\nreviewText: This think has worked out great.Had a diff. bran 64gb card and if went south after 3 months.This one has held up pretty well since I had my S3, now on my Note3.*** update 3/21/14I've had this for a few months and have had ZERO issue's since it was transferred from my S3 to my Note3 and into a note2. This card is reliable and solid!Cheers!\nreviewTime: 2013-11-21\nday_diff: 382\nhelpful_yes: 0\nhelpful_no: 0\ntotal_vote: 0\nscore_pos_neg_diff: 0\nscore_average_rating: 0\nwilson_lower_bound: 0" Positive
"reviewerName: 2&1/2Men\noverall: 5\nreviewText: Bought it with Retail Packaging, arrived legit, in a orange envelope, english version not asian like the picture shows. arrived quickly, bought a 32 and 16 both retail packaging for my htc one sv and Lg Optimus, both cards in working order, probably best price you'll get for a nice sd card\nreviewTime: 2013-07-13\nday_diff: 513\nhelpful_yes: 0\nhelpful_no: 0\ntotal_vote: 0\nscore_pos_neg_diff: 0\nscore_average_rating: 0\nwilson_lower_bound: 0" Negative
"reviewerName: 2Cents!\noverall: 5\nreviewText: It's mini storage. It doesn't do anything else and it's not supposed to. I purchased it to add additional storage to my Microsoft Surface Pro tablet which only come in 64 and 128 GB. It does what it's supposed to and SanDisk has a long standing reputation that speaks for itself.\nreviewTime: 2013-04-29\nday_diff: 588\nhelpful_yes: 0\nhelpful_no: 0\ntotal_vote: 0\nscore_pos_neg_diff: 0\nscore_average_rating: 0\nwilson_lower_bound: 0" Negative
Should you build or buy your data pipelines?
Download our free guide and discover the best approach for your needs, whether it's building your ELT solution in-house or opting for Airbyte Open Source or Airbyte Cloud.