2026-11-15

The Problem and the App

  • Goal: predict the next word as someone types
  • Built a Shiny app: enter text, click Predict next word, and get the top suggestion plus alternatives
  • Trained on a 20% sample of the text corpus
  • Try it: https://aj21122112.shinyapps.io/next-word/

Data and Preprocessing

  • Source: blog, news, and Twitter text files
  • Sampled 20% of lines, split into sentences
  • 80/20 train/test split, with 10% of train held out for validation
  • Built unigram, bigram, trigram, and 4-gram count tables
  • Dropped singleton 3-grams and 4-grams to save memory

The Model

  • Candidates: top 30 next words from each n-gram order, plus unigram fallbacks
  • Features: n-gram probabilities and counts, unigram probability, and word-association (PMI) with content words in the last 25 tokens
  • Reranker: a logistic regression trained on 5,000 validation prefixes scores the candidates
  • Top-scoring candidate is shown as the prediction

Performance

Metric Accuracy
Top-1 0.1690
Top-3 0.2625
Top-5 0.3170
  • Evaluated on 2,000 held-out 4-grams from the test set

Using the App

  1. Type a phrase in the text box
  2. Choose how many suggestions to show
  3. Click Predict next word
  4. Read the top prediction in large text, with the other suggestions in the table

Code: https://github.com/aj21122112/Data_sci_final