Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions macros/udfs/sentiment_analysis.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{% macro create_udf_sentiment_analysis() %}

create or replace function {{target.schema}}.udf_sentiment_analysis(text STRING, output INTEGER)
returns STRING
Comment on lines +3 to +4

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your arguments need revision
text STRING, model INTEGER or STRING [more on this later], output VARIANT (if you still want to differentiate between label and score)

and returns STRING, you have to make sure L32 has to cast as a string.

language PYTHON
runtime_version = 3.8
packages = ('pytorch','transformers','gensim')
handler = 'sentiment_analysis_py'
as

$$

from transformers import pipeline
from gensim.parsing.preprocessing import remove_stopwords
import string

model = "cardiffnlp/twitter-roberta-base-sentiment-latest"
-- model = 'nlptown/bert-base-multilingual-uncased-sentiment'

def sentiment_analysis(text, model_id, output='score'):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your function name (sentiment_analysis) and your handler on L8 have to be equivalent. you also should have the equivalent # of arguments here and in your UDF call.


-- text pre-processing
text = str(remove_stopwords(text).translate(str.maketrans('', '', string.punctuation)))

-- Classifier
classifier = pipeline("sentiment-analysis",model=model_id)
results = classifier(text)

if output=='label':
return results[0].get('label')
else:
return results[0].get('score')

$$

{% endmacro %}
31 changes: 31 additions & 0 deletions macros/udfs/sentiment_analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
version: 2

macros:
- name: create_udf_sentiment_analysis
description: "
This macro iterates through a piece of text to return the overall
sentiment of that text.
First, the macro pre-processes the text removing unnecessary punctuation
and stopwords to help increase the accuracy of the model. Subsequently, using
the transformers library it applies a sentiment analysis pipeline based on a
pre-trained model that will return either a score or a label for the text.

Recommendation is to use the following popular models:
1. cardiffnlp/twitter-roberta-base-sentiment-latest:
(https://huggingface.co/cardiffnlp/twitter-roberta-base-sentiment-latest)
This model is trained on 124M tweets from January 2018 to December 2021,
and is finetuned for sentiment analysis.
It outputs a label - Neutral, Positive or Negative - and a score ranging
from 0 to 1 - 0 being the most negative and 1, the most positive.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you've mis-interpreted score here. Score is more akin to the confidence in the assessment.

image

image

I actually have mixed results with nlptown model as well - the same positive text in the screenshots only yield 1 star with a score of 0.76.

2. nlptown/bert-base-multilingual-uncased-sentiment:
(https://huggingface.co/nlptown/bert-base-multilingual-uncased-sentiment)
This model is fine-tuned for sentiment analysis on product reviews in six
languages: English, Dutch, German, French, Spanish and Italian. It outputs
a label - 1 to 5 stars - and a score ranging from 0 to 1 - 0 being the
most negative and 1, the most positive.

Macro returns a STRING data type. If 'score' is used as an output, then it will
have to be cast to FLOAT data type.
"
docs:
show: false