November 13, 2026

The goal

A simple text-prediction product

  • Accepts a multi-word phrase as input.
  • Predicts one likely next word.
  • Shows alternative suggestions.
  • Uses English text from blogs, news and Twitter.

Data used in this demonstration

  • 2,000 lines from each source file.
  • 6,000 text lines in total.
  • A compact sample corpus for demonstrating the workflow.

Data and preparation

Three text sources

Source Lines Tokens*
Blogs 2,000 82,071
News 2,000 68,113
Twitter 2,000 25,100

*Token counts are based on alphabetic words and contractions in the supplied sample files.

Preparation

  • Convert text to lowercase.
  • Extract words and common contractions.
  • Preserve word order within each line.
  • Count observed word sequences and their following words.

Prediction algorithm

Backoff n-gram approach

  1. Check the last three words of the phrase (four-gram context).
  2. If unseen, check the last two words (trigram context).
  3. If unseen, check the last word (bigram context).
  4. If no context is found, use the most frequent words overall.

Why this approach?

  • Lightweight and fast to run.
  • Uses observed sequences from the corpus.
  • Provides a sensible fallback for unseen phrases.

Shiny application

How to use the app

  • Type a phrase into the text box.
  • Select Predict next word.
  • Read the suggested next word.
  • Review up to four additional likely words.

Application components

  • Text input and prediction button.
  • Trained n-gram model loaded at startup.
  • Main prediction display and ranked suggestions table.

Evaluation and next steps

Basic testing plan

  • Try at least five phrases from news or Twitter-style text.
  • Confirm the app returns one next-word prediction for each phrase.
  • Check that alternatives are displayed when matching contexts exist.

Limitations and improvements

  • The model is trained on 6,000 sample lines, not the full corpus.
  • Rare phrases may fall back to shorter contexts or unigram frequency.
  • Future work: use the full dataset, optimize memory and speed, and evaluate prediction quality on held-out text.

Deliverables: Shiny app (app.R) and this five-slide presentation.