Concepts / Text Classification with Movie Reviews

Text Classification with Movie Reviews

A model can only learn from numbers, so the representation chosen for text decides which information the model can use, and the source emphasizes that the representation of the data is key in NLP applications.

  • Programming

Why a Model Cannot Read a Review

Imagine you are asked to build a filter that decides whether a movie review is positive or negative. The learning algorithm you plan to use only accepts numbers. It has no direct way to take in a sentence such as 'Great film, I loved the ending.' Before any learning can happen, something has to turn that sentence into numbers. That step is called data representation, and the choice you make at this step quietly decides what the model is allowed to know.

What do you think happens?

Do the reviews 'Boring plot, great ending' and 'Great ending, boring plot' give a bag-of-words representation the same counts? Make your guess before reading on.

  • Yes, the counts are identical

  • No, the word order makes the counts different

Reveal answer
Answer:

Yes, the counts are identical.

A bag-of-words representation records each token and how many times it appears, and it discards word order. Both reviews contain the tokens boring, plot, great and ending exactly once each, so the bag is the same for both, even though the sentences are different.

The Representation Sits Between Text and Model

A model can only learn from numbers, so the representation chosen for text determines which information the model can use. The source emphasizes this directly: as is often the case in machine learning, the representation of the data is key in NLP applications. The same review can be turned into different sets of numbers, and a model that learns from one set may succeed or fail because of the representation rather than because of the learning algorithm itself.

It helps to see the whole path from raw text to a prediction as a pipeline. Each stage hands a transformed version of the review to the next stage, and information that is dropped at an early stage can never be recovered by a later one. The diagram below traces that path for a bag-of-words representation.

raw review textoriginal sentencelowercase and strippunctuationnormalize the textsplit into tokensunits to be countedcount each distincttokenword order discarded herebag of countsnumbers the model receivesmodellearns from the counts
At which step does the word order of the review disappear, so that the model can no longer use it?

Tracing One Review Token by Token

A bag-of-words representation starts by splitting text into tokens, which are the units the representation counts. For this article, a token is a lowercase word with punctuation removed. The representation then records each distinct token and how many times it appears. The result is a bag: a collection of words with counts, and nothing that says where each word sat in the sentence.

Worked example

Counting the tokens of one review

Build a bag-of-words representation for the review 'Boring plot, boring acting, great ending'.

  1. 1
    Lowercase the text

    Every letter becomes lowercase, so 'Boring' and 'boring' are treated as the same token.

  2. 2
    Remove the punctuation

    The commas are removed, leaving the words only.

  3. 3
    Split on spaces

    Splitting produces six tokens in this order: boring, plot, boring, acting, great, ending.

  4. 4
    Count each distinct token

    The token boring appears twice. The tokens plot, acting, great and ending each appear once.

  5. 5
    Hand over the bag

    The model receives the counts, not the sentence. The sequence boring, plot, boring is no longer visible to it.

Result

boring: 2, plot: 1, acting: 1, great: 1, ending: 1

boringcount 2plotcount 1actingcount 1greatcount 1endingcount 1
Which count does each distinct token receive after the review is tokenized and counted?

Notice that the six tokens collapse into five entries. The repeated token boring is recorded once with a count of 2. This is the core move of the representation: distinct words become keys, and their frequencies become the values the model sees.

What the Bag Keeps and What It Discards

A bag-of-words representation keeps which tokens occur and how often. It does not keep the order of the tokens. This is the trade-off behind its simplicity. Two texts with the same words in different orders end up with identical counts, so the representation cannot tell them apart.

token identitywhich words occurword ordersequence of tokenstoken counthow often each occursgrammarsentence structure
Which parts of the review survive conversion to counts, and which are lost?

Whether that loss matters depends on the task. The source describes the bag-of-words approach as a simple and powerful solution for text classification tasks such as spam and fraud detection and sentiment analysis. For many of these problems, a strong signal such as repeated words like boring may be enough to separate one class from another, even though the arrangement of the words has been thrown away.

ReviewSentence orderBag-of-words counts
Boring plot, great endingboring, plot, great, endingboring 1, plot 1, great 1, ending 1
Great ending, boring plotgreat, ending, boring, plotboring 1, plot 1, great 1, ending 1
Boring plot, boring acting, great endingboring, plot, boring, acting, great, endingboring 2, plot 1, acting 1, great 1, ending 1

Different sentence orders can produce identical bags, while repetition changes the counts.

Reading Bigrams as Local Word Order

An n-gram is a run of n consecutive tokens. A bigram, with n equal to 2, keeps a small piece of local order that a single-token count throws away. You can picture a bigram as a window of width two that slides across the token list one position at a time, recording each pair it covers.

boring plotwindow 1plot boringwindow 2boring actingwindow 3acting greatwindow 4great endingwindow 5slide one tokenslide one tokenslide one tokenslide one token
How does a window of two adjacent tokens move across the review, and which adjacent pairs does it record?

For the review 'Boring plot, boring acting, great ending', the window produces five bigrams. Two of them, 'boring plot' and 'boring acting', show that boring is now recorded next to other words. A single-token bag only knows that boring appeared twice; a bigram list also records what boring was next to. The source notes that inspecting the tokens and n-grams that are extracted can give powerful insights into the modeling process.

In the real world

Consider a review containing the phrase 'not good'. A single-token bag records the token not and the token good, each with a count, and nothing ties them together. A bigram 'not good' keeps the adjacency, which is the kind of local pattern a sentiment task may need. This example is an illustration of the idea rather than a result from the source.

Checking Whether Tokens Match Expectations

The source makes a point that goes beyond choosing a representation: you can introspect the extracted pieces directly. In text processing, it is often possible to introspect models in a meaningful way, and this holds for supervised tasks, where labels guide learning, and for unsupervised tasks, where labels are absent. The practical advice is to take full advantage of this ability when using NLP-based methods.

  1. 1
    Write down the raw text you believe the model sees, then list the tokens your preprocessing actually produces.
  2. 2
    Compare the two lists and look for words that were dropped, merged, or split in an unexpected way.
  3. 3
    Extract the bigrams and read them as a list, checking that adjacent pairs are the ones you intended.
  4. 4
    Ask whether the cue a task needs, such as a repeated negative word or a phrase like not good, appears among the tokens or n-grams.
  5. 5
    If the cue is missing, change the representation before changing the model.
Before

tokens: boring, plot, boring, acting, great, ending

Unigram counts only: the adjacency of boring to other words is not recorded.

After

bigrams: boring plot, plot boring, boring acting, acting great, great ending

Bigrams record adjacency, so boring is now linked to plot and to acting.

Mistakes Learners Make with Representations

  • Common mistake

    Assuming the model sees the sentence

    What it looks like

    Believing a classifier reads 'Great film, I loved the ending' as written.

    Why it fails

    The learning algorithm accepts only numbers, so the text must first be converted.

    Do this instead

    Think of the representation as the only thing the model sees, and inspect what it contains.

  • Common mistake

    Treating word order as preserved

    What it looks like

    Expecting 'Boring plot, great ending' and 'Great ending, boring plot' to be told apart by a bag of words.

    Why it fails

    A bag of words keeps token identity and counts, and discards word order, so both reviews produce the same counts.

    Do this instead

    Use bigrams or another representation if adjacent word order is needed by the task.

  • Common mistake

    Skipping inspection of extracted tokens

    What it looks like

    Training a model without ever printing the tokens or n-grams it was given.

    Why it fails

    The extracted pieces often differ from what you assumed when you wrote the preprocessing code.

    Do this instead

    List the tokens and a sample of n-grams and compare them with the text before modeling.

  • Common mistake

    Counting punctuation or capital letters as separate tokens when the definition removes them

Practice: Count the Tokens Yourself

EASY

Lowercase and tokenize the review 'Great film, great acting, boring ending'. Write down the count for each distinct token, then list its first two bigrams. Finally, state one piece of information the bag of counts has lost.

Hints
  • Remove the comma and split on spaces to get the token list first.
  • The token great appears twice in this review.
  • The bigrams are the adjacent pairs, so start with the first two tokens together.

Key Takeaways

  • A model can only learn from numbers, so the representation chosen for text decides what information the model can use.
  • A bag-of-words representation splits text into lowercase tokens with punctuation removed, then records each distinct token and its count.
  • The bag keeps token identity and frequency, and discards word order and grammar, which is why different sentences can produce identical counts.
  • Bigrams keep a small piece of local order, and inspecting the tokens and n-grams lets you check whether the representation holds the cues a task needs.

Related concepts

More concepts starting with T