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.
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
}
server.Rsource("predict.R")
shinyServer(function(input, output) {
observeEvent(input$predictBtn, {
output$prediction <- renderText({
predict_next_word(input$inputText)
})
})
})
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.
Here’s a checklist:
Include a README with: