lib/fuzzy_match.rb in fuzzy_match-1.1.1 vs lib/fuzzy_match.rb in fuzzy_match-1.2.1

- old
+ new

@@ -3,22 +3,25 @@ if ::ActiveSupport::VERSION::MAJOR >= 3 require 'active_support/core_ext' end require 'to_regexp' +require 'fuzzy_match/normalizer' +require 'fuzzy_match/stop_word' +require 'fuzzy_match/blocking' +require 'fuzzy_match/identity' +require 'fuzzy_match/result' +require 'fuzzy_match/wrapper' +require 'fuzzy_match/similarity' +require 'fuzzy_match/score' + +if defined?(::ActiveRecord) + require 'fuzzy_match/cached_result' +end + # See the README for more information. class FuzzyMatch - autoload :Tightener, 'fuzzy_match/tightener' - autoload :StopWord, 'fuzzy_match/stop_word' - autoload :Blocking, 'fuzzy_match/blocking' - autoload :Identity, 'fuzzy_match/identity' - autoload :Result, 'fuzzy_match/result' - autoload :Wrapper, 'fuzzy_match/wrapper' - autoload :Similarity, 'fuzzy_match/similarity' - autoload :Score, 'fuzzy_match/score' - autoload :CachedResult, 'fuzzy_match/cached_result' - DEFAULT_OPTIONS = { :first_blocking_decides => false, :must_match_blocking => false, :must_match_at_least_one_word => false, :gather_last_result => false, @@ -26,37 +29,36 @@ } attr_reader :haystack attr_reader :blockings attr_reader :identities - attr_reader :tighteners + attr_reader :normalizers attr_reader :stop_words attr_reader :read attr_reader :default_options # haystack - a bunch of records that will compete to see who best matches the needle # - # rules (can only be specified at initialization or by using a setter) - # * tighteners: regexps (see readme) - # * identities: regexps - # * blockings: regexps - # * stop_words: regexps - # * read: how to interpret each entry in the 'haystack', either a Proc or a symbol + # Rules (can only be specified at initialization or by using a setter) + # * :<tt>normalizers</tt> - regexps (see README) + # * :<tt>identities</tt> - regexps + # * :<tt>blockings</tt> - regexps + # * :<tt>stop_words</tt> - regexps # - # options (can be specified at initialization or when calling #find) - # * first_blocking_decides - # * must_match_blocking - # * must_match_at_least_one_word - # * gather_last_result - # * find_all + # Options (can be specified at initialization or when calling #find) + # * :<tt>read</tt> - how to interpret each record in the 'haystack', either a Proc or a symbol + # * :<tt>must_match_blocking</tt> - don't return a match unless the needle fits into one of the blockings you specified + # * :<tt>must_match_at_least_one_word</tt> - don't return a match unless the needle shares at least one word with the match + # * :<tt>first_blocking_decides</tt> - force records into the first blocking they match, rather than choosing a blocking that will give them a higher score + # * :<tt>gather_last_result</tt> - enable <tt>last_result</tt> def initialize(competitors, options_and_rules = {}) options_and_rules = options_and_rules.symbolize_keys # rules self.blockings = options_and_rules.delete(:blockings) || [] self.identities = options_and_rules.delete(:identities) || [] - self.tighteners = options_and_rules.delete(:tighteners) || [] + self.normalizers = options_and_rules.delete(:normalizers) || options_and_rules.delete(:tighteners) || [] self.stop_words = options_and_rules.delete(:stop_words) || [] @read = options_and_rules.delete(:read) || options_and_rules.delete(:haystack_reader) # options @default_options = options_and_rules.reverse_merge(DEFAULT_OPTIONS).freeze @@ -71,12 +73,12 @@ def identities=(ary) @identities = ary.map { |regexp_or_str| Identity.new regexp_or_str } end - def tighteners=(ary) - @tighteners = ary.map { |regexp_or_str| Tightener.new regexp_or_str } + def normalizers=(ary) + @normalizers = ary.map { |regexp_or_str| Normalizer.new regexp_or_str } end def stop_words=(ary) @stop_words = ary.map { |regexp_or_str| StopWord.new regexp_or_str } end @@ -93,22 +95,19 @@ options = options.symbolize_keys.merge(:find_all => true) find needle, options end def find(needle, options = {}) - raise ::RuntimeError, "[fuzzy_match] Dictionary has already been freed, can't perform more finds" if freed? - options = options.symbolize_keys.reverse_merge default_options gather_last_result = options[:gather_last_result] is_find_all = options[:find_all] first_blocking_decides = options[:first_blocking_decides] must_match_blocking = options[:must_match_blocking] must_match_at_least_one_word = options[:must_match_at_least_one_word] if gather_last_result - free_last_result @last_result = Result.new last_result.read = read last_result.haystack = haystack last_result.options = options last_result.timeline << <<-EOS @@ -116,11 +115,11 @@ \tOptions: #{options.inspect} EOS end if gather_last_result - last_result.tighteners = tighteners + last_result.normalizers = normalizers last_result.identities = identities last_result.blockings = blockings last_result.stop_words = stop_words end @@ -261,23 +260,9 @@ def explain(needle, options = {}) find needle, options.merge(:gather_last_result => true) last_result.explain end - def freed? - @freed == true - end - + # DEPRECATED - doesn't do anything def free - free_last_result - @haystack.try :clear - @haystack = nil - ensure - @freed = true - end - - private - - def free_last_result - @last_result = nil end end