Next Word Prediction

Umesh Rijal
August 2026

An NLP-powered text prediction application

Johns Hopkins Data Science Capstone

Built with R, Quanteda and Shiny

Objective: Predict the most likely next word from a phrase entered by the user.

# The Problem

Why Next Word Prediction?

Typing assistance can make text entry faster and more convenient.

Project objective

  • Accept a phrase from the user
  • Analyze the preceding words
  • Predict the next word
  • Return the result through a Shiny application

Training data

The English SwiftKey corpus contains:

  • Blogs
  • News
  • Twitter

A 1% sample was used for this prototype.

# The Data

Building the Language Model

The sampled text was cleaned and converted into word sequences.

Four levels of context

Unigram — individual word frequency

Bigram — two-word sequences

Trigram — three-word sequences

4-gram — four-word sequences

The frequency models are saved as .RDS files and loaded by the Shiny application.

Why n-grams?

Longer sequences provide more context, while shorter sequences provide fallback when a phrase is uncommon.

# The Algorithm

Backoff Prediction Strategy

The application searches from the most specific context to the least specific:

4-gram
   ↓
3-gram
   ↓
2-gram
   ↓
Unigram

# From Prototype to Product

What We Built

The project demonstrates an end-to-end path from text data to an interactive prediction application.

Current capabilities

  • 1–4 gram language model
  • Backoff prediction strategy
  • Interactive Shiny interface
  • Deployed web application
  • Real-time single-word predictions

Next Steps

  • Measure prediction accuracy using held-out data
  • Increase the training dataset
  • Improve handling of punctuation and unseen phrases
  • Provide multiple candidate predictions
  • Optimize model speed and memory usage

Final Takeaway

A statistical NLP model can be transformed into a practical, user-facing application using R and Shiny.