0Pricing
NLP Academy · Lesson

Bigrams and Trigrams Explained

Sliding windows over tokens.

Bigrams and Trigrams Explained is a free NLP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the NLP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Naming the N-Grams

The n in n-gram is just a number. Pick n and you decide how many neighboring words each feature bundles together.

Meet the Bigram

A bigram is an n-gram of size two, every adjacent pair of words. It is the most common upgrade over plain single words. 👍

Bigrams in Action

From four words you get three overlapping pairs. Notice how each bigram shares one word with the next one.

words = "i love clean code".split()
bigrams = list(zip(words, words[1:]))
print(bigrams)

Meet the Trigram

A trigram takes three words at a time. It captures even more context, like is not good as one unit.

words = "this is not good".split()
trigrams = list(zip(words, words[1:], words[2:]))
print(trigrams)

How Many Do You Get?

For a sentence of N words, you get N minus n plus one n-grams. So 4 words give 3 bigrams and 2 trigrams.

The Window Slides

Each n-gram comes from a small window moving one word forward. Overlap is the point, since shared words stitch context together.

A Reusable Helper

You can build any n-gram with one tidy function using zip. Change the value of n and the same code handles bigrams or trigrams.

def ngrams(words, n):
    return list(zip(*[words[i:] for i in range(n)]))

Bigger N, More Context

Higher values of n hold longer phrases, so they capture context more precisely. That power comes with a cost you will meet soon.

Bigger N, Rarer Grams

The longer the run, the less often that exact sequence repeats. Large n-grams become rare, appearing in few documents.

Why Bigrams Are Popular

Bigrams hit a sweet spot: enough context to catch not bad, common enough to repeat across texts. That balance makes them a reliable default.

Mixing Sizes

You rarely use one size alone. Most pipelines combine unigrams and bigrams so the model sees both single words and their key pairs.

Quick Check

How many bigrams come from the sentence text is just data, which has four words?

Recap: Bigrams and Trigrams

A bigram joins two words, a trigram joins three. Bigger n holds more context but appears less often. Next you will let scikit-learn build them. ⚙️

Frequently asked questions

Is the “Bigrams and Trigrams Explained” lesson free?

Yes — the full text of “Bigrams and Trigrams Explained” is free to read here on the web, and the NLP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the NLP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Bigrams and Trigrams Explained”?

Sliding windows over tokens. You practise NLP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start NLP Academy?

No prior experience is required. NLP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Bigrams and Trigrams Explained” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this NLP Academy lesson?

Yes. Every NLP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Single Words Lose Meaning
  2. Bigrams and Trigrams Explained
  3. N-Gram Features in scikit-learn
  4. Choosing the Right N-Gram Range
← Back to NLP Academy