Next Word Predictor: Faster Typing with a Lightweight n-gram Model

九月 26, 2026

The opportunity: people type billions of words a day on phones. Suggesting the next word saves keystrokes and reduces errors.

The product: a web app that predicts the next word of any phrase, in milliseconds, from a model small enough to run on a free server.

Try it: https://YOUR-ACCOUNT.shinyapps.io/next-word/

The Data and the Model

  • Corpus: SwiftKey English blogs, news and Twitter ([not measured] lines). A random sample of [not measured] of lines ([not measured] words) was used for training; a disjoint set was held out for testing.
  • Cleaning: lower-case; remove URLs, hashtags, numbers and punctuation; split into sentences so n-grams never cross a sentence boundary.
  • Counting: 2-, 3- and 4-grams with their conditional probability P(word | previous words).
  • Pruning: drop n-grams seen once, keep only the 5 best continuations per context, and never suggest profanity. Result: [not measured] n-grams, [not measured] in memory.

The Algorithm: Stupid Backoff

For input “… thanks for the”:

  1. Look up the last 3 words (“thanks for the”) in the 4-gram table. Seen continuations are scored by their probability.
  2. For words not yet found, back off to the last 2 words, then 1, multiplying the score by 0.4 per step.
  3. If nothing matches, fall back to the most frequent words.

Stupid Backoff (Brants et al., 2007) needs no expensive smoothing, scales to web-sized data and each prediction is just 3 keyed look-ups.

Performance on Held-out Text

Metric Value
Test cases (unseen sentences) [not measured]
Top-1 accuracy [not measured]
Top-3 accuracy [not measured]
Top-5 accuracy [not measured]
Average time per prediction [not measured]
Model size in memory [not measured]

“Top-3” is what a phone keyboard shows: the right word is in the suggestion bar for [not measured] of words typed.

The App and Next Steps

Using the app (https://YOUR-ACCOUNT.shinyapps.io/next-word/)

  • Type a phrase; the most likely next word appears instantly.
  • Click any of the 5 suggestions to append it and keep going.
  • Tick “Show scores” to see which n-gram order produced each suggestion.

Next steps

  • Train on the full corpus and add 5-grams (accuracy vs. memory trade-off).
  • Personalise with the user's own typing history.
  • Add sentence-start and topic context for smarter suggestions.