Next-Word Predictor

Ranjeet Kumar
August 2026

Data Science Capstone — Johns Hopkins University / SwiftKey

Typing, with the next word already suggested.

The problem

Typing on a phone is slow. Every keystroke saved is time returned to the user — which is why every major mobile keyboard predicts the next word.

What was built

  • A statistical language model trained on 550 MB of English blogs, news and tweets — 4.3 million lines, 102 million words.
  • A web app that takes any phrase and returns the most likely next word in about 11 milliseconds.

The engineering constraint

The raw corpus cannot ship. The finished model must fit in the memory of a free shinyapps.io instance and respond fast enough to feel instant — so the real work was making it small, not just accurate.

The algorithm

N-gram counting with stupid backoff.

  1. Learn. Count every 2-, 3- and 4-word sequence in the training text and record which word followed each one.
  2. Shrink. Discard sequences seen only once, keep only the top 5 candidates per phrase, and store words as integers rather than text. This is what makes the model deployable.
  3. Predict. Match the last three words typed. No match? Back off to the last two, then the last one, then to the most common words in English.
  4. Rank. Each backoff step is penalised by a factor of 0.4, so a longer, more specific match always wins.

Why stupid backoff: it needs no expensive normalisation, and for ranking the top few candidates it performs comparably to full Kneser-Ney smoothing at a fraction of the cost. A prediction is always returned — the unigram fallback guarantees it, even for words the model has never seen.

The app

Use it in one step: type a phrase. The prediction appears as you type.

| Element | What it does | |—|—| | Text box | Accepts any phrase, any length | | Big blue word | The single most likely next word | | Chips below | Alternative suggestions | | Table | Score, and which n-gram produced the answer | | Slider | Show 1–5 candidates | | Example links | One click to try a sample phrase |

No submit step is required, though a Predict button is provided. Punctuation, capitalisation, URLs and hashtags are handled automatically, and profanity is filtered from the training data so it is never suggested.

Results

| Measure | Result | |—|—| | Training data | 35% of the corpus — 1.5 M lines | | Model in memory | 63 MB (7 MB on disk) | | Median response time | 11 ms (95th pct: 17 ms) | | Top-1 accuracy | 14.4% | | Top-3 accuracy | 21.7% |

Measured on 2,000 phrases the model never saw in training. For context, a random guess from a 300,000-word vocabulary is right about once in 300,000 attempts — this is right roughly once every five keystrokes, in the time between keypresses.

Where it goes next: learning from each user's own typing, and swapping counts for a small neural model where memory allows.

Try it

https://ranjeetk25.shinyapps.io/capstone_app/

A model trained on a hundred million words, answering in the time it takes to press the next key.