Text N-gram Generator

Generates all contiguous N-word sequences, or n-grams, from your text for a configurable N, one n-gram per output line, a generalization of fixed unigram and bigram extraction to any window size for language modeling and text analysis. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-23

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.

Features

Advantages

  • Supports any N, not just the fixed unigram or bigram case.
  • Produces overlapping n-grams, the standard convention for n-gram analysis.

Limitations

  • Requires at least N words of input.
  • Splits strictly on whitespace, so punctuation stays attached to adjacent words.

Examples

3-grams of a short sentence

Input

the quick brown fox jumps

Output

the quick brown
quick brown fox
brown fox jumps

With N=3, three overlapping 3-word windows are produced from the five-word sentence.

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.

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.

References

Frequently Asked Questions