title: minimum distance function between strings
body: <p>completely edited, big thanks shayaa for the advice!</p>
  
  <p>out of sentences in a matrix (read in from csv), there shall be words detected which are stored in lists (read in from txt). </p>
  
  <pre><code>sentences_list &lt;- matrix(c(
      "this screen is great", 
      "this camera is not bad", 
      "everything good but the camera is awesome",
      "everything bad but the camera is awesome",
      "battery is ok but the camera is awesome"), ncol = 1)
  
  word_list_one &lt;-list("screen", "camera", "battery")
  word_list_two &lt;-list("good", "great", "awesome")
  word_list_three &lt;-list("bad", "awful", "poor")
  word_list_four &lt;-list("not", "don't", "neither")
  
      one &lt;- apply(sentences_list, 2, function(x) {
          str_detect(x, paste(word_list_one, sep = '|', collapse = '|'))
      })
  
      two &lt;- apply(sentences_list, 2, function(x) {
        str_detect(x, paste(word_list_two, sep = '|', collapse = '|'))
      })
  
      three &lt;- apply(sentences_list, 2, function(x) {
        str_detect(x, paste(word_list_three, sep = '|', collapse = '|'))
      })
  
      four &lt;- apply(sentences_list, 2, function(x) {
        str_detect(x, paste(word_list_four, sep = '|', collapse = '|'))
      })
  </code></pre>
  
  <p>The following code can be taken for reviewing which words were matched. (the results are stored instead of directly displayed, because the number of results is counted in a certain way in the aftermath)</p>
  
  <pre><code>row=5
  
  print(sentences_list[row])
  c(str_extract(sentences_list[row], paste(word_list_one, sep = '|', collapse = '|')))
  c(str_extract(sentences_list[row], paste(word_list_two, sep = '|', collapse = '|')))
  c(str_extract(sentences_list[row], paste(word_list_three, sep = '|', collapse = '|')))
  c(str_extract(sentences_list[row], paste(word_list_four, sep = '|', collapse = '|')))
  </code></pre>
  
  <p>For <code>row=1</code> and <code>row=2</code> everything works as it should, but not for the following. This is because only the first match in a sentence from the <code>word_list_x</code> is returned. What I would rather like the code to do, is giving back the word for a <code>word_list_x</code> which is the closest in distance to the word found in another <code>word_list_</code>. </p>
  
  <p>so for <code>row=3</code>in <code>sentences_list</code> the result for the <code>word_list_two = "good"</code>, because it is found first. What should be the result is <code>word_list_two = "awesome"</code> because in the sentence of <code>row=3</code> it stands closer to the result found in <code>word_list_one = "camera"</code>.</p>
  
  <p>as for <code>row=4</code>in <code>sentences_list</code> the result for the <code>word_list_three = "bad"</code> and <code>word_list_two = "awesome"</code>. Because the result from <code>word_list_two</code>is closer in distance to the result found in <code>word_list_one = "camera"</code> only the result from <code>word_list_two = "awesome"</code> should be returned leaving <code>word_list_three = " "</code>blank.</p>
  
  <p>as for <code>row=5</code>in <code>sentences_list</code> the result for the <code>word_list_one = "battery"</code>, because it is found first. What should be the result is <code>word_list_one = "camera"</code> because in the sentence of <code>row=5</code> it stands closer to the result found in <code>word_list_two= "great"</code>.</p>
  
  <p>Obviously, as a novice, I am completely overstrained with the scale of that project and I am very grateful for any help you can provide,thank you very much!</p>