title: Replace words/phrases within longer strings if they are found in lookup table
body: <p>I have a data frame of sentences and a data frame of key words and their synonyms. I would like to look through each row of the sentences and replace any synonyms found with the appropriate key word. I've been struggling with this thing for the last couple of days without much luck. So any advice you can provide would be much appreciated!</p>
  
  <p>Sample data:</p>
  
  <pre><code>sentences &lt;- data.frame( ID = c( "1", "2", "3", "4"),
                           text = c("the kitten in the hat",
                                    "a dog with a bone",
                                    "this is a category",
                                    "their cat has no hat"),
                           stringsAsFactors=FALSE)
  
  lookup &lt;- data.frame( key = c("cat", "a", "has"),
                         synonym = c("kitten", "the", "with"),
                         stringsAsFactors=FALSE)
  </code></pre>
  
  <p>I'd like to get the data back as a data frame much like the original "sentences" only with the synonyms replaced. For example:</p>
  
  <pre><code>ID        text
  1        a cat in a hat
  2        a dog has a bone
  3        this is a category
  4        their cat has no hat
  </code></pre>
  
  <p>The actual data consists of 2016 sentences of between 200-500 words each. The lookup table contains about 200,000 rows of words and phrases. I've figured out how to replace individual words and phrases without much trouble, but I can't figure out how to do it with a lookup table.</p>
  
  <p>One other note that's causing me grief: I need to match exact words/phrases including special characters. For example "adison's disease" should match "adison's disease" but not "adisons disease". "cotton-roll" should match "cotton-roll" but it should not match "cottonroll" or "cotton roll". </p>
  
  <p>I'm using 
  R version 3.6.2 (2019-12-12)
  Platform: x86_64-w64-mingw32/x64 (64-bit)
  Running under: Windows 10 x64 (build 18362)</p>