Next Word Predictor

Chunlu (Lyn) Yang
Coursera Data Science Capstone

A lightweight Shiny app that predicts the next word from an English phrase.

Try the app: https://lyannn.shinyapps.io/next_word_predictor/

  • Input: a phrase with multiple words
  • Output: one predicted next word
  • Goal: fast, simple, and reliable enough for interactive use

How the Model Works

The app uses a reliability-aware n-gram language model.

  1. Clean the input text: lowercase, remove non-word symbols, trim spacing
  2. Use the last words as context
  3. Retrieve candidate words from trigram and bigram tables
  4. Add unigram fallback candidates so the app always returns a prediction
  5. Score candidates with reliability-aware interpolation

Key idea: Rare trigrams should not automatically override stronger lower-order evidence.

Model Exploration

I compared three prediction strategies on the same 10-item benchmark.

Model Description Multiple-choice accuracy
V1 Hard backoff: trigram → bigram → unigram 30%
V2 Reliability-aware trigram/bigram/unigram interpolation 40%
V3 V2 + pruned 4-gram evidence 40%

V2 improved accuracy without adding extra model storage.

Efficiency and Complexity

Model Avg. runtime per prediction Accuracy
V1 0.24023 sec 30%
V2 0.24045 sec 40%
V3 0.25329 sec 40%

4-gram exploration showed strong sparsity:

  • 95.5% of unique 4-grams occurred only once
  • Pruning singleton 4-grams retained only 4.46% of the original 4-gram features
  • Final deployment model was compressed to about 3.6 MB

Why This App Is Useful

The app is designed for quick, interactive next-word prediction.

How to use it:

  1. Open the Shiny app
  2. Type an English phrase, such as: Thank you so much for your
  3. Click Predict Next Word
  4. View the predicted word

The final model uses V2 because it provides the best balance of accuracy, speed, and simplicity.

App link: https://lyannn.shinyapps.io/next_word_predictor/