Next Word Prediction

Next Word Prediction: Overview

What is it?

  • Goal: Predict the next word based on user input
  • Method: N-gram language model with Stupid Backoff
  • Technology: R, Shiny, NLP

Why it matters

  • Text messaging: Predictive text
  • Email composition: Auto-completion
  • Accessibility: Help for users with disabilities

Data source

  • SwiftKey en_US dataset (blogs, news, Twitter)
  • ~4.2 million lines, ~100 million words

How It Works: The Algorithm

Prediction Strategy (Stupid Backoff)

Step 1: User types a phrase (e.g., “I want to”)

Step 2: Preprocess text

  • Convert to lowercase
  • Remove punctuation
  • Remove extra spaces

Step 3: Predict using N-gram model

  • Try 3-gram (last 2 words) → Return if found
  • Backoff to 2-gram (last 1 word) → Return if found
  • Final fallback → Most common word

Example

  • Input: “I want to”
  • Predict: “go”, “eat”, “see”, “be”, etc.
  • Model selects highest probability word

The Shiny Application

Features

  • Simple Interface: Clean, intuitive design
  • Input: Text box for typing phrases
  • Prediction: Click “Predict” button
  • Output: Displays the most likely next word
  • Examples: Click to auto-fill test phrases

How to Use

  1. Type a phrase (e.g., “I love to”)
  2. Click “Predict Next Word”
  3. The app shows the predicted word

Try It!

https://tuenxu666.shinyapps.io/app904/

Data and Training

Dataset Summary

Dataset Lines Max Line Length
Blogs 899,288 40,833
News 1,010,242 11,385
Twitter 2,360,148 140

Model Training Process

  1. Clean text: Lowercase, remove punctuation
  2. Build N-grams: 1-gram, 2-gram, 3-gram models
  3. Store frequencies: Data frames for fast lookup
  4. Optimize: Lightweight model for Shiny deployment

Key Finding

  • “love” appears ~4x more than “hate” in Twitter data

Conclusion and Next Steps

Summary

  • ✅ Built a working next-word prediction model
  • ✅ Deployed as interactive Shiny application
  • ✅ Created presentation for stakeholders

Achievements

  • Accurate predictions for common phrases
  • Fast response time (< 1 second)
  • Intuitive user interface

Future Improvements

  • Add 4-gram and 5-gram models
  • Add confidence scores to predictions
  • Support multiple languages
  • Implement Kneser-Ney smoothing

Thank You!

Try the app: https://tuenxu666.shinyapps.io/app904/

Enxu Lu | 2026-09-05