library(stringdist)
## Loading required package: parallel
# Searching in HHGTTG
search.text= "Far out in the uncharted backwaters of the unfashionable  end  of the  western  spiral  arm  of  the Galaxy lies a small unregarded yellow sun. Orbiting this at a distance of roughly ninety-two  million  miles is  an  utterly insignificant little blue green planet whose apedescended life forms are so amazingly primitive that  they  still think digital watches are a pretty neat idea. This planet has - or rather had - a problem, which was this: most of  the  people  on  it were unhappy for pretty much of the time. Many solutions were suggested for this problem, but most of these were  largely  concerned with the movements of small green pieces of paper, which is odd because on the whole it wasn't  the  small green pieces of paper that were unhappy. And so the problem remained; lots of the people  were  mean,  and most of them were miserable, even the ones with digital watches. Many were increasingly of the opinion that they'd all made a  big mistake  in  coming  down  from the trees in the first place. And some said that even the trees had been a bad move,  and  that  no one should ever have left the oceans."

# Search string
search.string= unlist(strsplit(search.text, " "))

# Find string
find.string= c("bar", "and", "fashion", "san", "billion")

# Function to get strings matching in search string
string.match.search.hits= function(search.string, find.string, max.dist) {
  smatrix= stringdistmatrix(a= find.string, b= search.string, useBytes= F, method= "jw", ncores= 2)
  idx= which(smatrix<= max.dist, arr.ind=T)
  search.hits= search.string[idx[,2]]
  unique(search.hits)
}

# Function to get strings matching in find string
string.match.find.hits= function(search.string, find.string, max.dist) {
  smatrix= stringdistmatrix(a= find.string, b= search.string, useBytes= F, method= "jw", ncores= 2)
  idx= which(smatrix<= max.dist, arr.ind=T)
  find.hits= find.string[idx[,1]]
  unique(find.hits)
}


# Perfect match
string.match.find.hits(search.string, find.string, max.dist= 0)
## [1] "and"
string.match.search.hits(search.string, find.string, max.dist= 0)
## [1] "and"
# Close match
string.match.find.hits(search.string, find.string, max.dist= 0.1)
## [1] "billion" "and"
string.match.search.hits(search.string, find.string, max.dist= 0.1)
## [1] "million" "and"
# Fuzzy Match
string.match.find.hits(search.string, find.string, max.dist= 0.3)
## [1] "bar"     "fashion" "and"     "san"     "billion"
string.match.search.hits(search.string, find.string, max.dist= 0.3)
##  [1] "Far"           "unfashionable" "end"           "a"            
##  [5] "sun."          "distance"      "million"       "an"           
##  [9] "Many"          "wasn't"        "And"           "and"          
## [13] "opinion"       "made"          "said"          "bad"