Sri :
library(gsubfn)
a <-c("Whats your","Whats your name", "name", "fro")
b <- c("What is yours","what is your name","names","froth")
replacements <- setNames(as.list(b), a)
input_string = "fro Whats your name and Where're name you from to and fro I Whats your"
gsubfn(paste(paste0("\\w*", names(replacements), "\\w*"), collapse = "|"), replacements, input_string)
, , , :
input_string = "Whats your name and Where're you from"
matching <- data.frame(from_word=c("Whats your name", "name", "fro", "Where're", "Whats"),
to_word=c("what is your name","names","froth", "where are", "Whatsup"))
library(gsubfn)
matching$from_word <- as.character(matching$from_word)
matching$to_word <- as.character(matching$to_word)
test <- unlist(str_split(input_string, " "))
test2 <- sapply(paste0("\\b", test, "\\b"), grepl, matching$from_word)
rownames(test2) <- matching$from_word
test3 <- test2[order(rowSums(test2), decreasing = TRUE),]
test3[apply(test3, 2, cumsum) > 1] <- FALSE
replacements <- setNames(as.list(as.character(matching[,2])[order(rowSums(test2), decreasing = TRUE)][rowSums(test3) >= 1]),
as.character(matching[,1])[order(rowSums(test2), decreasing = TRUE)][rowSums(test3) >= 1])
gsubfn(paste(as.character(matching[,1])[order(rowSums(test2), decreasing = TRUE)][rowSums(test3) >= 1], collapse = "|"),
replacements,input_string)