0

I am trying to fetch similar words in a string. Suppose I have got a set of 5 keywords.

cats, dogs, animals, food, water

And I have a string like this

"Cat's an animal but different from a dog. Food's not same for both of them. But they both drink water."

If I try to use strpos in this case, I may find only one similar word "water" but in fact there are others too. What can help me achieve to detect all keywords in the sentence mentioned above?

SanketR
  • 1,182
  • 14
  • 35
  • What is your expected output? Can you show your attempted code? – anubhava Dec 02 '15 at 16:58
  • Could you paste your existing code? It is difficult to see what your problem is without seeing how youre trying to accomplish this. It could be a case insensitive issue, or perhaps an issue with the arguments passed to `strpos()`. – Todd Dec 02 '15 at 17:10

2 Answers2

0

Its possible you have an issue with case sensitivity. Try using the stripos() function. It could also be an issue with pluralization, or contractions, in which case a regular expression might make this easier.

Todd
  • 2,824
  • 2
  • 29
  • 39
0

For using regex -
This might mitigate contractions and boundary punctuation and such.
A more elaborate technique would be needed if detecting similarities
down to the character level.

(?i)(?<!\S)(?:cat(?:'?s)?|dog(?:'?s)?|animal(?:'?s)?|food|water)(?:(?=\p{P})|(?!\S))

Formatted:

 (?i)
 (?<! \S )
 (?:
      cat
      (?: '?s )?
   |  dog
      (?: '?s )?
   |  animal
      (?: '?s )?
   |  food
   |  water
 )
 (?:
      (?= \p{P} )
   |  (?! \S )
 )