Overview
Introduction
N-gram extraction is a foundational step in language modeling and text analysis, and generalizing it beyond the fixed unigram/bigram case usually means writing a small script.
It runs entirely client-side, so nothing you paste is ever uploaded to a server.
What Is Text N-gram Generator?
A word n-gram generator that lists every contiguous sequence of N words from the input, one n-gram per line, for any N you choose.
It runs entirely client-side as part of this site's String Tools collection, so nothing you paste is ever uploaded to a server.
How Text N-gram Generator Works
The input is split into words on whitespace, and a sliding window of size N moves one word at a time across the list, joining each window's words into a single n-gram line.
The transformation happens synchronously in JavaScript the moment you type, with no network round trip involved.
When To Use Text N-gram Generator
Use it as a building block for an n-gram language model, a text similarity comparison, or to inspect the word-sequence structure of a passage at any window size.
It's a fast way to get the answer without opening a code editor, a REPL, or writing a one-off script just to check.
Often used alongside String Bigram Generator and String Unigram Generator.
Features
Advantages
- Supports any N, not just the fixed unigram or bigram case.
- Produces overlapping n-grams, the standard convention for n-gram analysis.
- Yields exactly the word count minus N plus one n-grams, so the output size is predictable from the word count and window size alone.
Limitations
- Requires at least N words of input.
- Splits strictly on whitespace, so punctuation stays attached to adjacent words.
Examples
Best Practices & Notes
Best Practices
- Use a larger N to capture more context per n-gram, at the cost of fewer total n-grams from the same text.
- Normalize case and strip punctuation first, so the same phrase occurring twice produces identical n-grams rather than near-duplicates.
- Keep N below the word count, since a window larger than the text produces no n-grams at all.
Developer Notes
The core loop is `for (let i = 0; i <= words.length - n; i++) ngrams.push(words.slice(i, i + n).join(" "))`, producing exactly `words.length - n + 1` overlapping n-grams for an input of `words.length` words.
Text N-gram Generator Use Cases
- Building input for an n-gram language model
- Comparing two texts' n-gram overlap for similarity scoring
- Inspecting a passage's word-sequence structure at a chosen window size
Common Mistakes
- Passing text with fewer words than N, which can't form any n-gram.
- Expecting punctuation to be stripped from words automatically; it stays attached.
Tips
- Try N=2 or N=3 first for readable output; larger N values produce longer, more specific n-grams with fewer repeats.
- Consecutive n-grams overlap by N minus one words, which is why even a short passage still yields a useful number of them.