These examples demonstrate how to perform sentiment analysis of food reviews in a TeradataML DataFrame. Refer to TextAnalyticsAI Example Setup for the prerequisite steps.
obj_aws.analyze_sentiment(column="reviews",data=data,accumulate='reviews')
Output:
| reviews | Sentiment |
|---|---|
| The food was excellent but it arrived a bit late | positive |
| Todays food delivery was quicker than yesterdays Appreciated it | positive |
| Both the food and the delivery service were topnotch. | positive |
| The delivery was prompt but the food was spilled and the portion size was small. | negative |
| The food was average and the delivery person was rude | negative |
obj_azure.analyze_sentiment(column="reviews",data=data,accumulate='reviews')
Output:
| reviews | Sentiment |
|---|---|
| Both the food and the delivery service were topnotch. | 'positive' |
| The delivery was prompt but the food was spilled and the portion size was small. | Negative |
| Todays food delivery was quicker than yesterdays Appreciated it | positive |
| The food was excellent but it arrived a bit late | positive |
| The food was average and the delivery person was rude | negative |
obj_gcp.analyze_sentiment(column="reviews",data=data,accumulate='reviews')
Output:
| reviews | Sentiment |
|---|---|
| The food was excellent but it arrived a bit late | positive |
| Todays food delivery was quicker than yesterdays Appreciated it | positive |
| Both the food and the delivery service were topnotch. | positive |
| The delivery was prompt but the food was spilled and the portion size was small. | negative |
| The food was average and the delivery person was rude | negative |
Example 1: Analyze sentiment of food reviews in the 'reviews' column of a teradataml DataFrame using Hugging Face model 'distilbert-base-uncased-emotion'
Reviews are passed as a column name along with the teradataml DataFrame.
model_name = 'bhadresh-savani/distilbert-base-uncased-emotion'
model_args = {'transformer_class': 'AutoModelForSequenceClassification',
'task' : 'text-classification'}
llm = TeradataAI(api_type = "hugging_face",
model_name = model_name,
model_args = model_args)
Create a TextAnalyticsAI object.
obj = TextAnalyticsAI(llm=llm) obj.analyze_sentiment(column='reviews', data=df_reviews, delimiter="#")
Output:
| text | label |
|---|---|
| Todays food delivery was quicker than yesterdays Appreciated it | joy |
| The food was excellent but it arrived a bit late | joy |
| The food was average and the delivery person was rude | anger |
| Both the food and the delivery service were topnotch. | anger |
| The delivery was prompt but the food was spilled and the portion size was small. | anger |
Example 2: Extend Example 1 using "output_labels" to format the output
obj.analyze_sentiment(column ='reviews',
data = df_reviews,
output_labels = {'label': str, 'score': float},
delimiter = "#")
Output:
| text | label | score |
|---|---|---|
| Todays food delivery was quicker than yesterdays Appreciated it | joy | 0.9902466535568237 |
| The food was excellent but it arrived a bit late | joy | 0.9982774257659912 |
| The food was average and the delivery person was rude | anger | 0.9979689717292786 |
| Both the food and the delivery service were topnotch. | anger | 0.9234364032745361 |
| The delivery was prompt but the food was spilled and the portion size was small. | anger | 0.9261348247528076 |
Example 3: Extend Example 1 to use user defined script for inferencing
base_dir = os.path.dirname(teradatagenai.__file__)
sentiment_analyze_script = os.path.join(base_dir, 'example-data',
'analyze_sentiment.py')
obj.analyze_sentiment(column ='reviews',
data = df_reviews,
script = sentiment_analyze_script,
delimiter = "#")
Output:
| text | Sentiment |
|---|---|
| The delivery was prompt but the food was spilled and the portion size was small. | anger |
| The food was average and the delivery person was rude | anger |
| The food was excellent but it arrived a bit late | joy |
| Todays food delivery was quicker than yesterdays Appreciated it | joy |
| Both the food and the delivery service were topnotch. | anger |
obj_nim.analyze_sentiment(column="reviews",data=data,accumulate='reviews')
Output
| reviews | Sentiment |
|---|---|
| The food was excellent but it arrived a bit late | positive |
| Todays food delivery was quicker than yesterdays Appreciated it | positive |
| Both the food and the delivery service were topnotch. | positive |
| The food was average and the delivery person was rude | negative |
| The delivery was prompt but the food was spilled and the portion size was small. | negative |