Last active
May 31, 2021 06:58
-
-
Save pakjiddat/43c61c54b645e5bd0096d6fd75e58127 to your computer and use it in GitHub Desktop.
Shiny app for predicting words - server code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# This is the server logic of the word-predictor application. You can run the | |
# application by clicking 'Run App' above. | |
# | |
# Find out more about building applications with Shiny here: | |
# | |
# http://shiny.rstudio.com/ | |
# | |
library(shiny) | |
library(ggplot2) | |
library(wordpredictor) | |
# Define server logic | |
shinyServer(function(input, output) { | |
# The ModelEvaluator object is created | |
mp <- ModelPredictor$new("./def-model.RDS") | |
# The predicted word information | |
p <- NULL | |
# The next word is predicted | |
output$next_word <- renderUI({ | |
# If the user entered some text | |
if (trimws(input$ngram) != "") { | |
# The text entered by the user is split on space | |
w <- trimws(input$ngram) | |
# The next word is predicted | |
p <- mp$predict_word(w, 10) | |
# If the next word was not found | |
if (!p$found) { | |
# The next word and next word is set to an information | |
# message | |
nw <- span("Not Found", style = "color:red") | |
# The next word probability is set to an information | |
# message | |
nwp <- span("N.A", style = "color:red") | |
# The plot is set to empty | |
output$next_word_plot <- renderPlot({}) | |
# The predicted next word | |
nw <- tags$div("Predicted Word: ", tags$strong(nw)) | |
# The predicted next word probability | |
nwp <- tags$div("Word Probability: ", tags$strong(nwp)) | |
# The next word probability is updated | |
output$word_prob <- renderUI(nwp) | |
} | |
else { | |
# The next word | |
nw <- p$words[[1]] | |
# The next word probability | |
nwp <- p$probs[[1]] | |
# The plot is updated | |
output$next_word_plot <- renderPlot({ | |
# A data frame containing the data to plot | |
df <- data.frame("word" = p$words, "prob" = p$probs) | |
# The data frame is sorted in descending order | |
df <- (df[order(df$prob, decreasing = T),]) | |
# The words and their probabilities are plotted | |
g <- ggplot(data = df, aes(x = reorder(word, prob), y = prob)) + | |
geom_bar(stat = "identity", fill = "red") + | |
ggtitle("Predicted words and their probabilities") + | |
ylab("Probability") + | |
xlab("Word") | |
return(g) | |
}) | |
# The predicted next word | |
nw <- tags$div("Predicted Word: ", tags$strong(nw)) | |
# The predicted next word probability | |
nwp <- tags$div("Word Probability: ", tags$strong(nwp)) | |
# The next word probability is updated | |
output$word_prob <- renderUI(nwp) | |
} | |
} | |
else { | |
# The next word is set to "" | |
nw <- tags$span() | |
# The next word probability text is set to "" | |
output$word_prob <- renderUI(tags$span()) | |
# The plot is set to empty | |
output$next_word_plot <- renderPlot({}) | |
} | |
return(nw) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment