-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.R
158 lines (131 loc) · 4.82 KB
/
app.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
################################################################################
# app.R
#
# This is a Shiny web application showcasing Yet Another Predictive Text
# Model (YAP™).
#
# https://sweitzen.shinyapps.io/yap-tm/
#
# Requires:
# predictNext.R
# tokenizer.R
# www/dts_pruned_8.rda (training ngrams, pruned to count > 8)
# www/style.css
library(shiny)
library(stringr)
source("predictNext.R")
# Load training Ngrams
load("www/dts_pruned_8.rda")
################################################################################
# Define UI for application
ui <- fluidPage(
theme="style.css",
# Application title
titlePanel(HTML("<h1>Yet Another Predictive Text Model (YAP™)</h1>"),
windowTitle="YAP TM"),
# Text instructions
HTML(paste0(
"Enter text in the input box below, as though you were using a mobile ",
"messaging app, and YAP™ will attempt to predict your next ",
"word. If the last character entered is a space, YAP™ will know ",
"the previous word is complete. If the last character entered is not ",
"a space, YAP™ will assume you are still typing the next word, ",
"and will offer predictions that start with the last word or word ",
"fragment you have typed.<br><hr>"
)),
# Sidebar with a text input
sidebarLayout(
sidebarPanel(
width=5,
wellPanel(
textInput(
"X",
"Enter text below:"
)
),
wellPanel(
HTML("<b>Prediction:</b>"),
htmlOutput("next_word")
),
# Built with Shiny by RStudio
h5("Built with",
a(href="https://shiny.rstudio.com",
img(src="http://www.rstudio.com/wp-content/uploads/2014/04/shiny.png",
height="30px")),
"by",
a(href="https://www.rstudio.com",
img(src="http://www.rstudio.com/wp-content/uploads/2014/07/RStudio-Logo-Blue-Gray.png",
height="30px")),
"."
)
),
# Show best prediction and table of top five predictions
mainPanel(
width=7,
tableOutput("predictions")
)
),
HTML("<hr>"),
HTML(paste0(
"The YAP™ app uses a 5-gram language model to predict the next ",
"word from user input, ranking the predictions using a method called ",
"\"Stupid Backoff\", as described in ",
"<a href=\"http://www.aclweb.org/anthology/D07-1090.pdf\">'Large ",
"Language Models in Machine Translation'</a> by T. Brants et al, in ",
"EMNLP/CoNLL 2007.<p><br>"
)),
HTML(paste0(
"YAP™ was implemented as the capstone project for the Johns ",
"Hopkins University Coursera ",
"<a href=\"https://www.coursera.org/specializations/jhu-data-science\">",
"Data Science Specialization</a>.<p><br>",
"The R code underlying this app can be found on ",
"<a href=\"https://github.com/sweitzen/yap-tm\">GitHub</a>.<br>",
"Visit the author on ",
"<a href=\"https://www.linkedin.com/in/sweitzen/\">LinkedIn</a>."
))
)
################################################################################
# Define server logic
server <- function(input, output) {
# Reactive expression to get predictions
predictions <- reactive({
req(input$X)
out <- predictNext(input$X, dts)
names(out) <- c("Score", "Prediction")
return(out[, c("Prediction", "Score")])
})
input_display <- reactive({
req(input$X)
# If the input ends with a space, the last word is complete
# If it doesn't end with a space, the last word may not be complete
if (str_sub(input$X, start= -1) == " ") {
out <- input$X
} else {
if (str_count(input$X, "\\S+") == 1) {
out <- ""
} else {
# Split beginning fragment of next word off of input
out <-paste(word(input$X, 1:(str_count(input$X, "\\S+") - 1)),
collapse=" ")
}
}
return(out)
})
# Output the top prediction appended to the input
output$next_word <- renderUI((
HTML(paste0(
input_display(),
" <i>",
predictions()$Prediction[1],
"</i>"
))
))
# Output the top five predictions with their SBO score
output$predictions <- renderTable(
{predictions()},
digits=4
)
}
# Run the application
shinyApp(ui = ui, server = server)