1dolinski
1dolinski

Reputation: 549

Pattern Matching with Ruby

Im looking to match two strings and return a score, I don't care about order, just the percentage of character matches.. I was looking at the 'string_score' gem but it cares about order.. as can seen by my test ... any suggestions on a better approach?

require 'string_score/ext/string'

"JAMES DOWNY".downcase.score("Downy, James".downcase)

#=> 0

"JAMES DOWNY".downcase.score("James Downy".downcase)
#=> 1


"JAMES DOWNY".downcase.score("James, Downy".downcase)

# w the comma
#=> 0

Upvotes: 0

Views: 888

Answers (1)

1dolinski
1dolinski

Reputation: 549

Ok so I looked around, specifically at the "string_score", "scorer", and "text" gems. Those weren't cutting it for this particular question. I decided to go with the "amatch" gem.

This was my made up code for checking the consistency between two strings.

require 'rubygems'
require 'scorer'
require 'amatch'

include Amatch

class String
    def order_downcase
        self.chars.sort_by(&:downcase).join.downcase
    end
end

def goodmatch?(check_string,base_string)
    x = JaroWinkler.new(basestring.order_downcase)
    x.match(check_string.order_downcase)
end

It's still not perfect though. For example if I do goodmatch?("one","two") it returns 0.0. I believe this is too low, it should be around 10% or something because of the "o" in both words. I will give the answer and an up vote to someone who can better this solution.

Upvotes: 1

Related Questions