0

I ve got an array_subscribed_players and I am checking if this array includes a string this way:

array_subscribed_players.include?(subscription.user.full_name_inversed.downcase.strip)

I am looking for a way to make this expression return true even if the string that I give does not match perfectly with the string contained in array_subscribed_players. Let's say if there are one or two different letters or if it has one extra or less letter.

sawa
  • 165,429
  • 45
  • 277
  • 381
David Geismar
  • 3,152
  • 6
  • 41
  • 80
  • 1
    Bob Jones & Rob Bones match? only two letters different but clearly not a typo. NLP is not possible with regex, but you can do a crude match to match if two letters are different, but you're going to get some pretty inaccurate results – Yule Sep 16 '15 at 09:49

3 Answers3

0

You might do best with a gem that does fuzzy searching, with such methods as trigrams, and then experimenting with what you consider to be a high enough score to count as a match.

Various gems are available: look for trigram, fuzzy match, etc.

David Aldridge
  • 51,479
  • 8
  • 68
  • 96
0

What you want is a fuzzy match algorithm. You can start using the solution suggested in this answer or one of the various fuzzy matching Ruby implementations.

Once you have the algorithm in place (packaged as a method), simply call that method on each item of the array rather than using the standard string equality.

Community
  • 1
  • 1
Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
0

You could utilize the gem similar text. It provides a % similar to compared to two strings. You'd have to build the logic for what % similarity you are looking for.

https://rubygems.org/gems/similar_text