Logo

MonoCalc

/

N-Gram Generator

Text
0 characters — drop a .txt file here or paste below

Gram settings

Drag or type a value between 1 and 10
Comma separated; applied on top of the built-in list
How word grams are joined in the table

Result filters

Hide rarer grams
0 shows every gram
Decimals on percentages
Ctrl/Cmd + K to focus

How the window slides

n = 2
the
quick
brown
fox
jumps
over
the
lazy

Each highlighted run of 2 words is one n-gram. The window advances a single position at a time, so a text of T words yields T − 2 + 1 grams per segment.

Enter some text to generate n-grams

Paste a paragraph, load a sample or drop a .txt file. Every gram, count and percentage is computed in your browser.

About This Tool

N-Gram Generator – Word and Character Sequence Frequency Analysis

An n-gram is a contiguous run of n items drawn from a stream of text. Set n = 1 and you have unigrams, the individual words or letters. Set n = 2 and you have bigrams — adjacent pairs such as machine learning. Set n = 3 and you have trigrams. This n-gram generator slides a window of your chosen width across any text and reports every gram it finds together with its count, its share of the total, its rank and the position of its first appearance.

How the generator builds n-grams

The text is first cut into segments. By default a segment ends at a line break and at a sentence terminator, so lazy dog. The quick never produces the nonsense bigram dog the. Each segment is then lowercased and stripped of punctuation according to your settings and split into tokens: words separated by whitespace in word mode, or individual code points in character mode — Array.from is used so that emoji and other astral-plane characters count as one character rather than two surrogate halves.

A window of width n then advances one position at a time across each segment and every window it captures is tallied. A segment of T tokens therefore yields T − n + 1 grams, and the frequency percentage of a gram is its count divided by the grand total across all segments. Turning on cross sentence boundaries merges those segments, which raises the total and shifts every percentage — the counts did not change, the denominator did.

Word n-grams versus character n-grams

Word n-grams capture phrasing. They are what SEO analysts use to find the repeated two- and three-word phrases a page actually emphasises, what editors use to catch crutch phrases like at the end of the day, and what autocomplete and predictive keyboards use to guess the next word. Character n-grams capture spelling and morphology instead. Character bigram and trigram frequencies are the classical entry point for breaking a substitution cipher, they drive language identification for short strings, and character trigram profiles are a standard fuzzy-matching technique for deduplicating names and addresses.

Reading the summary statistics

The type-token ratio is unique grams divided by total grams. A value close to 1 means almost nothing repeats; a value close to 0 means heavy repetition. The repetition score is simply (1 − TTR) × 100, rescaled so a higher number means a more repetitive text. Hapax legomenacounts grams that occur exactly once — in natural language this is typically about half the vocabulary, a direct consequence of Zipf's law, under which frequency falls off roughly as the inverse of rank. Because all three figures depend strongly on document length, only compare them across passages of similar size.

Data sparsity is the real limit on n
Raise n and the number of possible grams explodes while the number you actually observe barely changes. By n = 4 nearly every gram in an ordinary document occurs exactly once, the type-token ratio approaches 1 and the counts stop carrying information. The comparison view plots this collapse directly. For most texts the useful structure lives at n between 1 and 3.

Stop words, padding and Markov probabilities

Removing stop words before building word grams strips out the, of, and and their kin. That is the right move for keyword research, where function words otherwise dominate the top of the table, and it produces a keyword density view of the phrases a search engine would associate with the page. It is the wrong move for linguistic work: function words carry most of the grammatical signal, and removing them creates phrases that never actually occurred.

Enabling padding wraps each segment in <s> and </s> boundary tokens so that sentence-initial and sentence-final context is modelled explicitly — the convention used when training a language model. With padding and n ≥ 2 the Markov table becomes available, converting the counts into conditional probabilities P(next | context) = count(context + next) ÷ Σ count(context + *), ready to seed a Markov chain text generator.

Counts are not probabilities
A gram absent from your text has an observed probability of zero, which is almost never true of the language as a whole. Real language models apply smoothing — Laplace, Good-Turing or Kneser-Ney — to reserve probability mass for unseen grams. Treat the figures here as observed frequencies for this specific text, not as estimates for the language.

Practical uses

Content teams run an article at n = 2 and n = 3 with stop words removed and a minimum frequency of 2 to surface the long-tail phrases the page genuinely repeats. Students generate training data for a bigram language model and inspect the distribution. Puzzle solvers switch to character mode and turn punctuation stripping off for cipher frequency analysis. Editors sort by frequency to find the phrase they have leaned on eight times in two pages. Everything runs entirely in your browser, so a draft manuscript or a confidential document is never uploaded anywhere.

Frequently Asked Questions

Is the N-Gram Generator free?

Yes, N-Gram Generator is totally free :)

Can I use the N-Gram Generator offline?

Yes, you can install the webapp as PWA.

Is it safe to use N-Gram Generator?

Yes, any data related to N-Gram Generator only stored in your browser (if storage required). You can simply clear browser cache to clear all the stored data. We do not store any data on server.

How does the N-Gram Generator work?

Your text is first cut into segments — by default at line breaks and at sentence terminators — so that a gram never spans a full stop. Each segment is then lowercased and stripped of punctuation according to your settings and split into tokens: whitespace-separated words in Word mode, or individual code points in Character mode. A window of width n slides one position at a time across each segment, and every window it captures is tallied in a map. The frequency percentage is that tally divided by the total number of windows, which is why turning boundary crossing on changes both the totals and the percentages.

What is the difference between a unigram, a bigram and a trigram?

They are simply n-grams with n set to 1, 2 and 3. A unigram is a single item, so with n = 1 the tool becomes a plain word- or letter-frequency counter. A bigram is a pair of adjacent items and a trigram is a run of three. Bigrams and trigrams capture short-range context — they are what let a model know that "New York" is a unit rather than two unrelated words — which is why they dominate autocomplete, spelling correction and keyword-phrase research.

Why does the number of unique n-grams collapse as I raise n?

This is data sparsity, and it is the central practical limit on n-gram models. A text with 500 words yields 499 bigrams, but the number of possible bigrams over a 200-word vocabulary is 40 000, so almost every bigram you observe appears exactly once. Raise n to 4 or 5 and effectively every gram becomes unique, the type-token ratio approaches 1, and the counts stop being informative. For most documents n between 1 and 3 is where the interesting structure lives; larger n needs a much larger corpus.

When should I remove stop words or turn off punctuation stripping?

Remove stop words for SEO and topic work, where "of the" and "in a" crowd out the phrases you actually care about. Keep them for anything linguistic — Markov chains, style analysis, authorship attribution — because function words carry most of the grammatical signal and removing them creates phrases that never occurred in the text. Punctuation stripping is the right default for phrase counting, but switch it off for cipher work and character-level analysis where every symbol matters.

What do the type-token ratio and hapax legomena figures tell me?

The type-token ratio is unique grams divided by total grams, a standard lexical-diversity measure: values near 1 mean almost nothing repeats, values near 0 mean heavy repetition. Hapax legomena counts the grams that occur exactly once, and in natural language it is typically around half of the vocabulary — a consequence of Zipf's law, where frequency falls off roughly as the inverse of rank. Both figures depend strongly on text length, so only compare them between passages of similar size.

Is my text sent to a server?

No. Tokenising, counting, the Markov probabilities and every export run entirely in your browser, so a draft manuscript, a client document or a puzzle ciphertext never leaves your machine. Your settings are kept in your own browser's local storage so the page opens where you left it, and share links carry the settings plus a short excerpt only when you choose to create one.