Sentiment Analysis in Python: VADER vs TF-IDF vs RoBERTa vs Google NL API (Notebook)
There are four families of sentiment analysis method, and they disagree more often than you’d expect. This free Colab notebook runs each one on the same text, measures how well each does, and then applies the best one to an SEO job: checking how the results for your branded queries feel.
Who it’s for
Marketers, SEOs and analysts choosing a sentiment approach for reviews, social posts or SERP monitoring, who want evidence before they commit to a tool or an API. It also works as a hands-on introduction if you have read about these methods but never seen them run side by side on the same data.
How to use it
- Lexicon-based. Score text with the VADER and AFINN word dictionaries.
- Classic machine learning. Train a TF-IDF + logistic regression classifier on about 45,000 labelled tweets (TweetEval).
- Deep learning. Label the same text with a transformer fine-tuned for sentiment (
twitter-roberta-base-sentiment-latest), with a confidence score. - Benchmark. Accuracy and macro-F1 for every method on the same 2,000-tweet test sample, plus a chart.
- Document vs sentence level. See how a mixed review breaks down sentence by sentence.
- Google Cloud Natural Language API (optional). Score and magnitude from API v1 and v2 side by side.
- Brand SERP sentiment. Upload titles ranking for your branded queries (keyword, position, title, URL) and get a bubble chart by position, a list of negative titles and a domain breakdown.
- Export the results as CSVs.
What the benchmark showed
On the test run (22 October 2026), the lexicon methods scored around 0.52 to 0.53 accuracy, TF-IDF with logistic regression 0.60, and RoBERTa 0.71. The transformer was fine-tuned on TweetEval’s training data, so it has a home advantage here. Test on a labelled sample of your own text before you choose.
Costs and limits
- Sections 1 to 5 and 7 are free and need no API key. A GPU runtime speeds up the transformer but isn’t required.
- The Natural Language API section needs a Google Cloud project with billing enabled; the first 5,000 units a month are free.
- The sample SERP titles are for a fictional brand. Replace them with your own export.
- Sentiment models struggle with sarcasm, domain jargon and mixed opinions, which is why the sentence-level section is there.
For the theory behind each method, read Sentiment Analysis: A Deep Dive Into the Theory, Methods, and Applications. Prefer no code? Try the no-code sentiment template for Google Sheets, or learn the methods in the Introduction to Machine Learning for SEO course.
What’s included
- Lexicon (VADER, AFINN), TF-IDF + logistic regression, and a RoBERTa transformer on the same text
- Accuracy and macro-F1 benchmark on 2,000 TweetEval test tweets, with a chart
- Document vs sentence-level sentiment on a mixed review
- Optional Google Cloud Natural Language API v1 and v2 side by side
- Brand SERP sentiment: bubble chart by position, negative titles and domain breakdown
Where this resource is used on MLforSEO
Walkthroughs on the blog
More customer insights resources
- Coding Script or NotebookCustomer Segmentation with Machine Learning Using Three Clustering Approaches
- No-Code TemplateIBM Watson NLU Sentiment and Emotion Analysis Template for Google Sheets with Multi-Dimensional Affect Detection
- No-Code TemplateSentiment Analysis No-code Template with Google Cloud Natural Language API
- No-Code TemplateGoogle Sheets Content Moderation Template with Google Cloud Natural Language API for Toxic Content Detection
- Coding Script or NotebookComprehensive Customer Review Semantic Analysis with Google Cloud NLP & IBM Watson NLU
- Visualization TemplateCustomer Reviews Semantic Analysis: Looker Studio Template
See all 9 customer insights resources →See all 7 ML foundations resources →See all 42 coding script or notebook resources →
