Sentiment Analysis

Using Hugging Face 🤗 Pipeline

Jan Kirenz

Python setup

from transformers import pipeline

Intuition

  • Text classification: assigning a label or class to a given text

  • Sentiment analysis is the automated process of tagging data according to their sentiment:

    • Positive, negative and neutral

Use Cases

  • Sentiment Analysis on Customer Reviews

  • You can track the sentiments of your customers from the product reviews using sentiment analysis models.

  • This can help understand churn and retention by grouping reviews by sentiment, to later analyze the text and make strategic decisions based on this knowledge.

Pipeline

Pipeline example with default model

sentiment_pipeline = pipeline("sentiment-analysis")

data = ["I love you",
        "I hate you"]

sentiment_pipeline(data)
  • Output:
[{'label': 'POSITIVE', 'score': 0.9998656511306763},
 {'label': 'NEGATIVE', 'score': 0.9991129040718079}]

Pipeline example with specific model

specific_model = pipeline(
    model="finiteautomata/bertweet-base-sentiment-analysis")
specific_model(data)
  • Output:
[{'label': 'POS', 'score': 0.9916695356369019},
 {'label': 'NEG', 'score': 0.9806600213050842}]

Available models

  • Sentiment analysis models on the Hub

  • Twitter-roberta-base-sentiment is a roBERTa model trained on ~58M tweets and fine-tuned

  • Bert-base-multilingual-uncased-sentiment is a model fine-tuned for sentiment analysis on product reviews in six languages: English, Dutch, German, French, Spanish and Italian.

  • Distilbert-base-uncased-emotion is a model fine-tuned for detecting emotions in texts, including sadness, joy, love, anger, fear and surprise.

Resources