induwara.lk
induwara.lkText · NLP

N-gram Generator — Word & Character n-grams

Paste any text and get its word or character n-grams — unigrams, bigrams, trigrams, up to 5-grams — with a ranked frequency table you can copy or download. It runs entirely in your browser, so nothing is uploaded. A handy aid for NLP students and SEO writers alike.

By Induwara AshinsanaUpdated Jul 13, 2026
N-gram Generator
Everything runs on your device — your text is never uploaded.0 / 50,000 chars
n-gram size
Unit
Examples
Total bigrams
Distinct n-grams
Tokens used
Segments
Enter some text above to see its n-grams.

Runs entirely in your browser — no upload, no key, nothing stored. Word n-grams are joined by a space; character n-grams are formed within each word (never across the gap between words). Method follows Jurafsky & Martin §3 and scikit-learn's CountVectorizer. Up to 50,000 characters.

How it works

An n-gram is a contiguous run of nitems pulled from a sequence. This tool builds n-grams two ways — over words or over characters — using the classical sliding-window construction described in Jurafsky & Martin's Speech and Language Processing, §3 "N-gram Language Models". Every step is a deterministic string operation, so the output is fully reproducible and never leaves your device.

  1. Tokenise. In word mode the text is optionally lowercased, optionally stripped of punctuation (intra-word apostrophes and hyphens are kept, so don't and well-being survive), then split on whitespace. In character mode each word's letters become the sequence, and n-grams are formed within a word so they never straddle the gap between two words — matching scikit-learn's char_wb intent.
  2. Slide the window. For a sequence of length T and window size n, the tool emits the items at positions i … i+n−1 for every start i from 0 to T−n. The number of n-grams in a segment is therefore max(0, T − n + 1).
  3. Count. Identical n-grams are tallied into a frequency map. Word n-grams are shown joined by a single space; character n-grams are concatenated. The table is sorted by count descending, with ties broken alphabetically so the same text always produces the same order.
  4. Optional sentence padding. When enabled (word mode only), the text is first split into sentences on . ! ? boundaries, and each sentence is padded with n−1 leading <s> markers and one trailing </s> marker. This mirrors NLTK's pad_left/pad_right preprocessing and is what language-model training uses to learn sentence starts and ends.

Every count is independently cross-checked: after the sliding window builds the list, the total is re-derived straight from the closed-form Σ max(0, T − n + 1) over each segment. When the two agree — they always should — the badge in the tool reads "count verified". What this tool deliberately does not do is estimate probabilities or apply smoothing (Laplace, Kneser-Ney): it is an n-gram extractor, not a language model, so the numbers you see are exact observed counts, nothing modelled.

Worked examples

Word bigrams (n = 2)

“The cat sat on the mat.” · lowercase · strip punctuation

  1. Tokens (6): the, cat, sat, on, the, mat
  2. Count = T − n + 1 = 6 − 2 + 1 = 5 bigrams
  3. Ordered: the cat · cat sat · sat on · on the · the mat
  4. Frequencies: each appears once → 5 total, 5 distinct

Character trigrams (n = 3)

“banana” · character mode · lowercase

  1. Characters (6): b a n a n a
  2. Count = 6 − 3 + 1 = 4 trigrams
  3. Ordered: ban · ana · nan · ana
  4. Frequencies: ana → 2, ban → 1, nan → 1 (4 total, 3 distinct)

Text shorter than n (edge case)

“hello world” · word mode · n = 3

  1. Tokens (2): hello, world
  2. Count = max(0, 2 − 3 + 1) = max(0, 0) = 0
  3. Result: empty table with a clear “fewer than n” notice
  4. No error, no silent NaN — just zero 3-grams

Frequently asked questions

Sources & references

The construction and the worked examples on this page were last reconciled against these sources on 2026-07-13. The definitions are standard and stable; the page is reviewed if the reference implementations change their default behaviour.

Related tools

Rate this tool
Be the first to rate

Comments & feedback

Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.

Found a bug, edge case, or want to suggest an improvement?

Email me at [email protected] — most fixes ship within 24 hours.