Skip to content
Coding Script or NotebookGoogle ColabFree

Sentiment Analysis in Python: VADER vs TF-IDF vs RoBERTa vs Google NL API (Notebook)

ForCustomer insightsML foundations

By Lazarina Stoy· Updated 24 Oct 2026

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

  1. Lexicon-based. Score text with the VADER and AFINN word dictionaries.
  2. Classic machine learning. Train a TF-IDF + logistic regression classifier on about 45,000 labelled tweets (TweetEval).
  3. Deep learning. Label the same text with a transformer fine-tuned for sentiment (twitter-roberta-base-sentiment-latest), with a confidence score.
  4. Benchmark. Accuracy and macro-F1 for every method on the same 2,000-tweet test sample, plus a chart.
  5. Document vs sentence level. See how a mixed review breaks down sentence by sentence.
  6. Google Cloud Natural Language API (optional). Score and magnitude from API v1 and v2 side by side.
  7. 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.
  8. 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