liveBook · Manning

Sentiment classifier

As explained in Starting Data Analytics with Generative AI and Python

A sentiment classifier is a model that assigns text to sentiment categories such as positive, negative, or neutral.

A sentiment classifier assigns text to categories such as positive, negative, or neutral. For customer reviews, it can help identify positive feedback, but its usefulness depends on language, domain, and validation. section 6.2.5 section 6.2.6

Treat classifier results as evidence to inspect rather than unquestionable labels:

  • Check domain mismatch when training data, such as financial news, differs from customer reviews. section 6.2.5
  • Watch for sarcasm, cultural expressions, regional Portuguese, inherited bias, computing demands, and privacy concerns. section 6.2.5
  • Compare classifications with raw reviews and monitor errors through a human-in-the-loop process. section 6.2.5

Portuguese customer reviews

A FinBERT-PT-BR pipeline can process a review column, return a label and confidence score, and filter records assumed to have the positive label. Because LABEL_1 may not universally mean positive, verify the model’s labels before filtering. section 6.2.5

Translation before classification

Another workflow translates Portuguese reviews with facebook/m2m100_418M, then applies distilbert-base-uncased-finetuned-sst-2-english. On 500 reviews, this approach achieved reported sensitivity of 0.86 and specificity of 0.89, though processing took time. section 6.2.6

Listing 6.2 Extracting positive reviews with the help of a suboptimal model
import pandas as pd
from transformers import pipeline

# Assuming df is your DataFrame and it has a column named 
'review_comment_message'
# Load the sentiment analysis pipeline with the FinBERT-PT-BR model
classifier = pipeline("sentiment-analysis", 
model="lucas-leme/FinBERT-PT-BR")
def get_sentiment(review):
    try:
        result = classifier(review)[0]
        return result['label'], result['score']
    except Exception as e:
        print(f"Error processing review: {e}")
        return None, None
# Apply the sentiment analysis to each review
df['sentiment'], df['score'] = 
zip(*df['review_comment_message'].map(get_sentiment))
# Filter the DataFrame to only include positive reviews
positive_reviews_df = df[df['sentiment'] == 'LABEL_1']   
# Assuming 'LABEL_1' is positive; adjust label as necessary based on model
output
# Now positive_reviews_df contains only the positive reviews

Inspect the pandas and Transformers workflow for loading FinBERT-PT-BR, handling errors, recording scores, and filtering positive labels. section 6.2.5

Listing 6.3 Sentiment analysis on translated inputs
import pandas as pd
from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer, 
pipeline
[...]
# Initialize the M2M100 tokenizer and model for translation
tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M")
model = 
M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M")
# Initialize the sentiment analysis pipeline
sentiment_pipeline = pipeline('sentiment-analysis', 
model='distilbert-base-uncased-finetuned-sst-2-english'
)
def translate_review(review):
    # Specify the source and target language
    tokenizer.src_lang = "pt"
    encoded_pt = tokenizer(review, return_tensors="pt")
    generated_tokens = model.generate(**encoded_pt, 
    forced_bos_token_id=tokenizer.get_lang_id("en"))
    translated_review = tokenizer.decode(generated_tokens[0], 
    skip_special_tokens=True)
    return translated_review
def analyze_sentiment(review):
    result = sentiment_pipeline(review)[0]
    return result['label'], result['score']
# Translate reviews from Portuguese to English
df['translated_review'] = 
df['review_comment_message'].apply(translate_review)
# Apply sentiment analysis to the translated reviews
df['sentiment'], df['score'] = 
zip(*df['translated_review'].apply(analyze_sentiment))
# Filter the DataFrame to only include reviews with positive sentiment
positive_reviews_df = df[df['sentiment'] == 'POSITIVE']
# positive_reviews_df now contains only the positive reviews, translated 
into English

Inspect the translation-and-classification workflow using M2M100 and an English sentiment pipeline. section 6.2.6

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Demonstrates model pipelines, confidence scores, label filtering, translation, evaluation, and limitations without developing classifier internals.
Examples
Uses Portuguese customer reviews, FinBERT-PT-BR, M2M100 translation, and an English SST-2 classifier.
What it assumes
Assumes a review dataframe and basic Python workflow, while demonstrating classification and translation from implementation examples.
What you can do after
Apply it via Transformers pipelines to label reviews, inspect confidence scores, filter positives, and compare classification performance.
How it connects
Connects sentiment analysis with customer reviews, machine translation, human-in-the-loop monitoring, and model evaluation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Starting Data Analytics with Generative AI and Python. The section links point to the parts of the book it draws on.