July 2026

The Problem & The Data

Goal: Predict the next word a user will type, in real time.

Training Data: HC Corpora (Coursera / SwiftKey)

  • ~4 million lines of English text
  • Sources: Twitter, News articles, Blog posts
  • Sampled 5% per source → 213,000 lines for training

Why N-grams?

  • No external APIs or large language models needed
  • Runs entirely offline inside a Shiny app
  • Fast lookup — predictions return in milliseconds

The Algorithm: Stupid Backoff

N-gram model with Stupid Backoff (Brants et al., 2007)

User types: *“happy new ___“*

  1. 4-gram lookup — search last 3 words in quadgram table
  2. 3-gram lookup — search “happy new” → returns year
  3. 2-gram lookup — search last word only (fallback)
  4. Unigram fallback — most frequent word in corpus

Training pipeline:

  • Lowercased, stripped punctuation, removed numbers
  • Built frequency tables for 2-, 3-, and 4-grams
  • Stored as named lookup vectors → saved as 42 MB .rds file

The App: How It Works

Live app: https://rameez-captone-project.shinyapps.io/NextWordPrediction/

Instructions:

  1. Type a phrase (2+ words) into the text box
  2. Press Predict
  3. The predicted next word appears instantly below

Example predictions:

Phrase entered Predicted word
I want to go to
the weather is changing
happy new year
I love you so
let me know if

Performance & Design Decisions

Model size vs. accuracy tradeoff

Sample Rate Model Size Coverage
5% (current) 42 MB Good for common phrases
10% ~80 MB Better for rare phrases
100% ~600 MB Too large for shinyapps.io

Prediction speed: < 5ms per query (hash table lookup)

What works well: Common phrases from news & social media

Limitations: Rare phrases may fall back to bigram; no context beyond 3 words

Summary

What was built:

  • N-gram language model trained on 213,000 lines of English text
  • Stupid Backoff algorithm for robust next-word prediction
  • Deployed Shiny app accessible from any browser

Technologies used: R, Shiny, stringr, dplyr, rsconnect

Links:

Thank you!