Main Working Code
Here, you’ll be able to observe the most critical part of the code in action. This algorithm is responsible for extracting the last word from the sentence to predict, locating the first word of the bigram, and utilizing the second one.
Below is the main code:
word_to_start_with <- input$name
last_word <- str_extract(word_to_start_with, "\\b\\w+\\b$")
result <- newsBigrams %>%
dplyr::filter(str_detect(bigram, paste0("^", last_word, "\\b"))) %>%
mutate(second_word = str_extract(bigram, "\\b\\w+\\b")) %>%
arrange(line) %>%
slice(1) %>%
pull(bigram)
return(result)