🔧 PART 1: Designing the Shiny UI

Here’s a basic ui.R (or UI part of app.R):

library(shiny)

shinyUI(fluidPage(
  titlePanel("Next Word Predictor"),

  sidebarLayout(
    sidebarPanel(
      textInput("inputText", "Enter a phrase:", value = ""),
      actionButton("predictBtn", "Predict Next Word")
    ),

    mainPanel(
      h4("Predicted Next Word:"),
      verbatimTextOutput("prediction")
    )
  )
))

If you’re using a single app.R file, wrap this into the ui <- fluidPage(...) section.


🤖 PART 2: Writing the Prediction Function

Here’s a simple N-gram-based logic (you can extend it later with smoothing/backoff):

predict.R

# Load necessary libraries
library(stringr)
library(data.table)

# Load your n-gram data (assumed to be preprocessed)
# Example: bigrams, trigrams, quadgrams as data.tables with columns:
# "word1", "word2", ..., "next_word", "freq"

load("ngrams.RData")  # Load your n-gram frequency tables

clean_input <- function(text) {
  text <- tolower(text)
  text <- str_replace_all(text, "[^a-z\\s]", "")
  text <- str_squish(text)
  return(text)
}

predict_next_word <- function(input_text) {
  input_text <- clean_input(input_text)
  words <- str_split(input_text, " ")[[1]]
  len <- length(words)

  if (len >= 3) {
    match <- quadgrams[word1 == words[len-2] & word2 == words[len-1] & word3 == words[len]]
    if (nrow(match) > 0) return(match[which.max(freq)]$next_word)
  }
  if (len >= 2) {
    match <- trigrams[word1 == words[len-1] & word2 == words[len]]
    if (nrow(match) > 0) return(match[which.max(freq)]$next_word)
  }
  if (len >= 1) {
    match <- bigrams[word1 == words[len]]
    if (nrow(match) > 0) return(match[which.max(freq)]$next_word)
  }

  return("the")  # fallback word
}

🔁 PART 3: Integrating Server Logic

server.R

source("predict.R")

shinyServer(function(input, output) {
  observeEvent(input$predictBtn, {
    output$prediction <- renderText({
      predict_next_word(input$inputText)
    })
  })
})

🎯 PART 4: Creating the Slide Deck

Use R Markdown Presentation (.Rpres file). Sample content:

---  
title: "Next Word Predictor"  
author: "Your Name"  
date: "July 2025"  
output: ioslides_presentation  
---

## Problem Statement

Typing on small screens is slow. Can we build a smart, predictive typing tool using NLP to guess the next word?

---

## Our Approach

- Use cleaned Twitter/news data
- Tokenize and build N-gram frequency models
- Use a backoff strategy to predict next word

---

## App Demo

1. Enter a phrase
2. Click "Predict"
3. Output: one most likely next word

(Include a screenshot of your Shiny app)

---

## Algorithm Summary

- Clean input text
- Match 3, 2, or 1 previous words with quadgrams/trigrams/bigrams
- Return most frequent match
- Fallback: return "the"

---

## Future Work & Value

- Improve with deep learning (RNNs, transformers)
- Use in mobile keyboards, autocomplete
- Lightweight, fast, user-friendly

Would you invest in a mobile typing prediction tool?

You can preview and publish from RStudio via File > Publish to RPubs.


🧼 PART 5: Polishing for Submission

Here’s a checklist:

✅ Final Shiny App:

  • Deployed on shinyapps.io
  • Predicts words reliably
  • Handles invalid or empty input
  • Uses a loading spinner if needed (optional)

✅ Slide Deck:

  • No more than 5 slides
  • Clear, non-technical summary
  • Screenshots, value prop

✅ RPubs and Shiny URLs:

  • Save the URLs to submit

✅ Bonus (Optional):

  • Include a README with:

    • How the app works
    • How to run it locally
    • Dataset source and credits