N-gram Text Generator
The generator never learns grammar or meaning. It counts which words follow which word sequences in your text, then writes new text by repeatedly picking a plausible next word. The sequence length n is how many previous words it looks at.
Phase 1: build the model
flowchart LR A["Split the text into words"] --> B["Slide a window of n words across the text"] B --> C["Window = key, the word right after it = follower"] C --> D["Append the follower to that key's list"]
With n = 2, the text the cat sat on the cat ran produces:
| Key (2 words) | Followers seen |
|---|---|
| the cat | sat, ran |
| cat sat | on |
| sat on | the |
| on the | cat |
Phase 2: generate text
flowchart TD
A["Pick a starting key, preferring one that begins with a capital letter"] --> B["Look up the followers of the current key"]
B --> C{"Any followers?"}
C -- yes --> D["Pick one at random"]
D --> E["Append it to the output"]
E --> F["New key = the last n words of the output"]
C -- no --> G["Dead end: jump to a random key"]
G --> B
F --> H{"Reached the requested length?"}
H -- no --> B
H -- yes --> I["Show the text"]
Why it works
- Repeats act as weights. A follower appears in the list once per occurrence, so a word that followed a key five times is five times as likely as one that followed it once.
- Larger n copies more. At n = 1 the output is loose and often nonsense. At high n most keys have a single follower, and the output quotes the source.
- Dead ends happen. A key at the very end of the text has no follower, which is why the generator jumps to a random key instead of stopping. A jump uses up one of the requested steps without adding a word, and the starting key's n words come first, so the output length is only approximately the number requested.