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.
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.
- Tokenise. In word mode the text is optionally lowercased, optionally stripped of punctuation (intra-word apostrophes and hyphens are kept, so
don'tandwell-beingsurvive), 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'schar_wbintent. - Slide the window. For a sequence of length
Tand window sizen, the tool emits the items at positionsi … i+n−1for every startifrom 0 to T−n. The number of n-grams in a segment is thereforemax(0, T − n + 1). - 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.
- Optional sentence padding. When enabled (word mode only), the text is first split into sentences on
. ! ?boundaries, and each sentence is padded withn−1leading<s>markers and one trailing</s>marker. This mirrors NLTK'spad_left/pad_rightpreprocessing 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
Frequently asked questions
Sources & references
- Jurafsky & Martin — Speech and Language Processing (3rd ed. draft), Ch. 3: N-gram Language Models
- NLTK — nltk.util.ngrams and sentence padding (pad_left / pad_right)
- scikit-learn — CountVectorizer (char / char_wb analyzers, ngram_range)
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
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.