18 July 2026

Introduction

This app predicts the next word a user is likely to type, given a partial phrase in English — the same problem SwiftKey/Google Keyboard solve for mobile typing.

  • Input: a phrase (e.g. “I love to”)
  • Output: the single most probable next word, plus 5 ranked candidates
  • Built with R Shiny, deployed on shinyapps.io
  • Trained on a sampled corpus of blogs, news, and Twitter text

The Algorithm

N-gram language model with Stupid Backoff

  1. Clean and tokenize the training corpus; build frequency tables of 2-grams, 3-grams, and 4-grams.
  2. Given an input phrase, take its last 3 words as context and look for a matching 4-gram.
  3. If no match, back off to the last 2 words (3-gram table), then to the last word (2-gram table), applying a decreasing weight (0.4, then 0.16) at each backoff step — the standard Stupid Backoff scheme (Brants et al., 2007).
  4. If nothing matches, fall back to the most frequent English words.
  5. Return the top-scoring candidate as the prediction.

This avoids expensive smoothing (Kneser-Ney, Good-Turing) while still giving strong practical accuracy and near-instant response time.

How the App Works

  1. Type any English phrase into the text box, leaving off the last word (e.g. “Thank you so much for the birthday”).
  2. Click Submit.
  3. The model computes the prediction (typically < 1 second) and displays:
    • The single best next word, in large text
    • A table of the top 5 candidate words with their scores

The n-gram tables are pre-built when the app starts, so runtime lookups are just fast data-frame filters — no retraining happens per request.

Using the App / Experience

  • The interface is deliberately minimal: one input box, one button, one clear answer — no clutter.
  • Predictions for common English phrases (news/social-media style) return sensible, fluent completions.
  • Rare or very short phrases fall back gracefully to common English words instead of erroring out.
  • Response time stays well under a second because scoring is a table lookup, not live model training.

Why This Approach / Wrap-up

  • Novelty: combines a lightweight, explainable Stupid Backoff model with a clean single-purpose UI — fast enough for real-time typing assistance, unlike heavier neural approaches that are impractical to serve for free on shinyapps.io.
  • Practicality: the whole pipeline (clean → tokenize → n-gram tables → backoff scoring) is transparent and easy to extend with a larger corpus or higher-order n-grams.
  • Takeaway: a small, well-engineered NLP product that balances accuracy, latency, and simplicity — the kind of trade-off a production data science team has to make daily.

Try it: (Shiny app link)  |  Code: (GitHub repo link)