Created
June 22, 2012 21:14
-
-
Save codatory/2975241 to your computer and use it in GitHub Desktop.
Fuzzy Matching Library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FuzzyMatch | |
class Matcher | |
require 'singleton' | |
require 'fuzzystringmatch' | |
include Singleton | |
JW = FuzzyStringMatch::JaroWinkler.create | |
def self.get_score(str1,str2) | |
JW.getDistance(str1,str2) | |
end | |
end | |
class List | |
require 'set' | |
def initialize(options={}) | |
@options = { | |
:filter => //, | |
:score => 0.8, | |
:downcase => true, | |
:ascii_only => true, | |
:fix_spaces => true | |
}.merge!(options) | |
@data = SortedSet.new | |
end | |
def to_a | |
@data.to_a | |
end | |
def <<(item) | |
item.downcase! if @options[:downcase] | |
item.gsub!(/[^a-z0-9 ]/,'') if @options[:ascii_only] | |
item.gsub!(/\s{2,}/, '') if @options[:fix_spaces] | |
item.gsub!(@options[:filter], '') | |
@data << item | |
end | |
def search(string) | |
@data.to_a.select do |candidate| | |
Matcher.get_score(string,candidate) > @options[:score] | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment