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

Your Name
九月 30, 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 (4,269,678 lines). A random sample of 5.0% of lines (5,001,014 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: 383,036 n-grams, 25 MB 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) 5,000
Top-1 accuracy 15.8%
Top-3 accuracy 25.2%
Top-5 accuracy 30.7%
Average time per prediction 3.7 ms
Model size in memory 25 MB

“Top-3” is what a phone keyboard shows: the right word is in the suggestion bar for 25.2% 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.