lib/fuzzy_match.rb in fuzzy_match-1.4.1 vs lib/fuzzy_match.rb in fuzzy_match-1.5.0
- old
+ new
@@ -1,7 +1,5 @@
-require 'to_regexp'
-
require 'fuzzy_match/rule'
require 'fuzzy_match/rule/normalizer'
require 'fuzzy_match/rule/stop_word'
require 'fuzzy_match/rule/grouping'
require 'fuzzy_match/rule/identity'
@@ -40,11 +38,12 @@
:must_match_grouping => false,
:must_match_at_least_one_word => false,
:gather_last_result => false,
:find_all => false,
:find_all_with_score => false,
- :threshold => 0
+ :threshold => 0,
+ :find_best => false,
}
self.engine = DEFAULT_ENGINE
attr_reader :haystack
@@ -92,23 +91,23 @@
# do this last
self.haystack = competitors
end
def groupings=(ary)
- @groupings = ary.map { |regexp_or_str| Rule::Grouping.new regexp_or_str }
+ @groupings = ary.map { |regexp| Rule::Grouping.new regexp }
end
def identities=(ary)
- @identities = ary.map { |regexp_or_str| Rule::Identity.new regexp_or_str }
+ @identities = ary.map { |regexp| Rule::Identity.new regexp }
end
def normalizers=(ary)
- @normalizers = ary.map { |regexp_or_str| Rule::Normalizer.new regexp_or_str }
+ @normalizers = ary.map { |regexp| Rule::Normalizer.new regexp }
end
def stop_words=(ary)
- @stop_words = ary.map { |regexp_or_str| Rule::StopWord.new regexp_or_str }
+ @stop_words = ary.map { |regexp| Rule::StopWord.new regexp }
end
def haystack=(ary)
@haystack = ary.map { |competitor| Wrapper.new self, competitor }
end
@@ -120,10 +119,15 @@
def find_all(needle, options = {})
options = options.merge(:find_all => true)
find needle, options
end
+ def find_best(needle, options = {})
+ options = options.merge(:find_best => true)
+ find needle, options
+ end
+
def find_all_with_score(needle, options = {})
options = options.merge(:find_all_with_score => true)
find needle, options
end
@@ -131,11 +135,12 @@
options = default_options.merge options
threshold = options[:threshold]
gather_last_result = options[:gather_last_result]
is_find_all_with_score = options[:find_all_with_score]
- is_find_all = options[:find_all] || is_find_all_with_score
+ is_find_best = options[:find_best]
+ is_find_all = options[:find_all] || is_find_all_with_score || is_find_best
first_grouping_decides = options[:first_grouping_decides]
must_match_grouping = options[:must_match_grouping]
must_match_at_least_one_word = options[:must_match_at_least_one_word]
if gather_last_result
@@ -268,9 +273,26 @@
memo = []
similarities.each do |similarity|
if similarity.satisfy?(needle, threshold)
bs = similarity.best_score
memo << [similarity.wrapper2.record, bs.dices_coefficient_similar, bs.levenshtein_similar]
+ end
+ end
+ return memo
+ end
+
+ if is_find_best
+ memo = []
+ best_bs = nil
+ similarities.each do |similarity|
+ if similarity.satisfy?(needle, threshold)
+ bs = similarity.best_score
+ best_bs ||= bs
+ if bs >= best_bs
+ memo << similarity.wrapper2.record
+ else
+ break
+ end
end
end
return memo
end