← All projects

Case study · NLP

  • NLP
  • Sentiment classification
  • GloVe & BERT embeddings

50,000 reviews. What are they really saying?

A positive/negative label is where most sentiment projects stop. I took the IMDB 50K movie reviews five layers further: how a review feels, how it's written, which part of the film it's about, and which other reviews mean the same thing. Two layers worked. One failed loudly, and that failure is the most useful part.

49,582reviews after removing 418 duplicates
88.3%test accuracy, Logistic Regression on TF-IDF
5lenses on every review

Step 01 · Clean the text

50,000 reviews in. 49,582 worth keeping.

01 · aCheck before you trust

No missing values, but 418 reviews appeared twice. Left in, the same review could land in both the training and the test set, and the model would be graded on answers it had already seen. After dropping them, the classes are almost exactly even, so plain accuracy is a fair score.

50,000raw reviews, 2 columns: text and label
418exact duplicates removed
24,884positive reviews left
24,698negative reviews left: a 50.2 / 49.8 split

01 · bFive steps, live

From messy HTML to the words that matter.

IMDB reviews arrive with <br /> tags, punctuation and filler words. These are the notebook's exact cleaning steps, running in your browser on three real reviews from the dataset. Edit the text or paste your own and watch each step change it.

runs in your browsersteps 1–4 match the notebook exactly · step 5 approximates WordNet

    The bar shows the share of tokens left after stopwords. Two quirks the notebook inherits: punctuation is removed before stopwords, so “don't” becomes “dont” and slips past the stopword list; and NLTK's list contains “not”, so negation is thrown away.

    Step 02 · Positive or negative

    Two baselines. One clear winner.

    Each review becomes a TF-IDF vector over the 5,000 most useful words: a word scores high when it's frequent in this review but rare across all reviews. Two classifiers learned from 39,665 reviews and were scored once on 9,917 they had never seen.

    Sort:
      test accuracy (80–95% scale)training accuracy

      Logistic Regression on the 9,917 test reviews

      Negative · precision0.89
      Negative · recall0.87
      Positive · precision0.87
      Positive · recall0.90

      Balanced across both classes: it isn't buying accuracy by favouring one label.

      Why Logistic Regression won

      Naive Bayes assumes every word is independent evidence, so “not” and “good” each vote alone. Logistic Regression learns one weight per word jointly and can discount words that usually travel together. The cost: a slightly wider train–test gap (2.6 points against NB's 0.9), the first sign it fits the training reviews more tightly. Raising the 5,000-feature cap would be the place to watch that gap grow.

      Step 03 · How does it feel? The hard part

      Five emotions. One kept winning.

      No emotion labels exist for these reviews, so I tried it without training: turn every review into the average of its GloVe word vectors (100 numbers each), turn each emotion into a vector too, and give every review the emotion it points closest to. Three attempts. Watch where the reviews go.

      1 dot = 100 reviews; any emotion with at least one review gets a dot, so the small piles are drawn larger than they are. Exact counts are above each label.

      Share of reviews given one emotion

      >99.9%

      in two of three attempts, and 99.4% in the other. Sadness and disgust were never assigned, not once.

      Why it can't work this way

      Average a few hundred word vectors and the details cancel out: every review drifts toward the same generic centre of the embedding space. The method then only asks which emotion vector sits nearest that centre, and the answer is the same for almost every review. Changing the emotion words moved the winner from surprise to joy; it never split the pile. The notebook's first draft called the result “not close to even”. The honest version: this approach didn't measure emotion at all.

      • Use a real emotion lexiconNRC tags 14,000 words with emotions, so a review can score on several at once instead of one argmax.
      • Compare against the centreSubtract the average review vector first, so each review's distinct direction is what gets matched.
      • Label a few hundredWithout labels this could only be eyeballed. A small hand-labelled set would turn it into a measurable task.

      Step 04 · How it's written, what it's about

      Happy reviewers don't shout more.

      04 · aWriting style

      I tagged every word's part of speech in 10,000 reviews and measured eight style signals. Before you look: which reviews do you think use more exclamation marks?

      Average per review · positive vs negative

      PositiveNegative
      0.266Noun share0.259
      0.143Verb share0.151
      0.078Adjective share0.076
      0.056Adverb share0.060
      0.845Unique-word ratio0.852
      5.91Word length5.83
      21.6Sentence length20.5
      0.96Exclamation marks1.05

      Each pair is scaled to its larger value. The bars are nearly mirror images: style barely separates sentiment. I expected positive reviews to be more expressive, and the data said no.

      04 · bAspect sentiment

      Everyone talks plot. Music gets the warmest words.

      A review can love the acting and hate the script. For five aspects, I kept only the sentences that mention one of its keywords and scored them with VADER (−1 very negative, +1 very positive), across all 49,582 reviews.

      AspectMentioned inAverage tone when mentioned (0 to +0.3)
      Story54%+0.10
      Acting53%+0.20
      Direction23%+0.15
      Visuals20%+0.19
      Music19%+0.24

      Story is mentioned most but scored coolest: plot is where reviewers complain. Music is mentioned least and scored warmest.

      The keyword rule is simple, so its blind spots are easy to see. Try the examples, or type your own: it runs the notebook's exact rule.

      runs in your browsersentence split + substring keyword match, as in the notebook
        What this means for the numbers above

        “History” counts as story and “broadcast” as acting, so the two biggest mention rates are probably a little inflated. “Beautifully shot” mentions no visuals keyword and is missed. Matching whole words and widening the lists would fix both.

        Step 05 · Meaning, not words

        Same review. Two different neighbours.

        05 · aBERT vs TF-IDF

        GloVe gives “bright” one vector whether it's a bright performance or a bright screen. BERT reads the whole review at once, so its vector depends on context. I embedded 500 reviews with bert-base-uncased (averaging token vectors, padding masked out) and asked each method for the closest match to one review.

        0.909cosine similarity

        The review
        “Soul Plane” is a horrible attempt at comedy that only should appeal people with thick skulls, bloodshot eyes and furry pawns. The plot is not only incoherent but also non-existent…
        BERT's match
        Candidate A
        Reba is, without a doubt, one of the worst “comedy” series ever. I wonder how come there are people writing good comments on “Reba”…
        TF-IDF's match
        Candidate B
        If it is true that the movie only cost 150K to make, it explains a lot. But, it doesn't explain why it has no real plot…

        The two scores aren't on one scale: averaged BERT vectors all sit fairly close together, so BERT similarities run high in general. What matters is which neighbour each method chose.

        05 · bWhy word counts aren't enough

        “Not good” and “good” look identical.

        Bag-of-words methods compare which words two texts share. After the cleaning in step 01 (stopwords include “not”), see how that goes wrong. This is a live word-count cosine similarity: 1.00 means identical word bags, 0 means no words in common.

        runs in your browserword counts after cleaning · no BERT here
        0.00similarity
        The takeaway

        Word overlap can't see negation, word order or synonyms. That's the gap contextual models like BERT close, and why Logistic Regression's 88.3% is a strong baseline, not a ceiling.

        The lesson

        A method that fails loudly is still a finding.

        The classifier worked, the aspect and similarity layers mostly worked, and the emotion layer didn't. Saying exactly how it failed, and why, is worth more than a chart that looks balanced.

        Python · pandas · NLTK · scikit-learn · gensim (GloVe) · PyTorch · Hugging Face Transformers (BERT). IMDB Dataset of 50K Movie Reviews (Maas et al., 2011).