Reputation: 93
So I've been trying to find a way to deal with this problem. I have 2 strings. For example:
Drinkable Yoghurt Is Good for your health
and
Drink Yog is good if you do not exceed
So what I want is tho complement the incomplete words and include the words that are not in both strings. THE ORDER OF THE TERMS IS NOT IMPORTANT NOR THE CASES. So my result could be:
Drinkable Yoghurt Is Good for your health if do not exceed
I just want to complement the strings. Any idea? Thank you.
Upvotes: 2
Views: 129
Reputation: 2589
foo <- "Drinkable Yoghurt Is Good for your health"
bar <- "Drink Yog is good if you do not exceed"
foo_vec <- unlist(strsplit(foo, " "))
bar_vec <- unlist(strsplit(bar, " "))
Find the words in foo
that are not contained inside words in bar
, and vice versa, ignoring case.
foo_vec2 <- foo_vec[!apply(sapply(foo_vec, function(x) grepl(x, bar_vec, ignore.case = TRUE)), 2, any)]
bar_vec2 <- bar_vec[!apply(sapply(bar_vec, function(x) grepl(x, foo_vec, ignore.case = TRUE)), 2, any)]
Find the words in both, ignoring case.
both <- intersect(tolower(foo_vec), tolower(bar_vec))
Put everything together.
paste(c(both, foo_vec2, bar_vec2), collapse=" ")
#> [1] "is good Drinkable Yoghurt for your health if do not exceed"
Upvotes: 1