ng3 <- data.frame(p = c("for the", "going to"), w = c("first", "be"))
ng2 <- data.frame(p = c("the", "to"), w = c("first", "the"))
predict_demo <- function(phrase) {
words <- unlist(strsplit(tolower(trimws(phrase)), " +"))
n <- length(words)
if (n >= 2 && paste(words[(n-1):n], collapse=" ") %in% ng3$p)
return(ng3$w[ng3$p == paste(words[(n-1):n], collapse=" ")])
if (n >= 1 && words[n] %in% ng2$p)
return(ng2$w[ng2$p == words[n]])
return("the")
}
predict_demo("Thanks for the")[1] "first"