Class: FuzzyMatch
- Inherits:
-
Object
- Object
- FuzzyMatch
- Defined in:
- lib/fuzzy_match.rb,
lib/fuzzy_match/rule.rb,
lib/fuzzy_match/score.rb,
lib/fuzzy_match/result.rb,
lib/fuzzy_match/version.rb,
lib/fuzzy_match/wrapper.rb,
lib/fuzzy_match/similarity.rb,
lib/fuzzy_match/score/amatch.rb,
lib/fuzzy_match/cached_result.rb,
lib/fuzzy_match/rule/grouping.rb,
lib/fuzzy_match/rule/identity.rb,
lib/fuzzy_match/rule/stop_word.rb,
lib/fuzzy_match/rule/normalizer.rb,
lib/fuzzy_match/score/pure_ruby.rb
Overview
See the README for more information.
Defined Under Namespace
Classes: CachedResult, Result, Rule, Score, Similarity, Wrapper
Constant Summary collapse
- DEFAULT_ENGINE =
:pure_ruby
- DEFAULT_OPTIONS =
{ :first_grouping_decides => false, :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, :find_best => false, }
- VERSION =
'1.5.0'
Instance Attribute Summary collapse
-
#default_options ⇒ Object
readonly
Returns the value of attribute default_options.
-
#groupings ⇒ Object
Returns the value of attribute groupings.
-
#haystack ⇒ Object
Returns the value of attribute haystack.
-
#identities ⇒ Object
Returns the value of attribute identities.
-
#normalizers ⇒ Object
Returns the value of attribute normalizers.
-
#read ⇒ Object
Returns the value of attribute read.
-
#stop_words ⇒ Object
Returns the value of attribute stop_words.
Class Method Summary collapse
Instance Method Summary collapse
-
#explain(needle, options = {}) ⇒ Object
Explain is like mysql’s EXPLAIN command.
- #find(needle, options = {}) ⇒ Object
- #find_all(needle, options = {}) ⇒ Object
- #find_all_with_score(needle, options = {}) ⇒ Object
- #find_best(needle, options = {}) ⇒ Object
-
#free ⇒ Object
DEPRECATED.
-
#initialize(competitors, options_and_rules = {}) ⇒ FuzzyMatch
constructor
haystack - a bunch of records that will compete to see who best matches the needle.
- #last_result ⇒ Object
Constructor Details
#initialize(competitors, options_and_rules = {}) ⇒ FuzzyMatch
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)
-
:
normalizers
- regexps (see README) -
:
identities
- regexps -
:
groupings
- regexps -
:
stop_words
- regexps -
:
read
- how to interpret each record in the ‘haystack’, either a Proc or a symbol
Options (can be specified at initialization or when calling #find)
-
:
must_match_grouping
- don’t return a match unless the needle fits into one of the groupings you specified -
:
must_match_at_least_one_word
- don’t return a match unless the needle shares at least one word with the match -
:
first_grouping_decides
- force records into the first grouping they match, rather than choosing a grouping that will give them a higher score -
:
gather_last_result
- enablelast_result
-
:
threshold
- set a score threshold below which not to return results (not generally recommended - please test the results of setting a threshold thoroughly - one set of results and their scores probably won’t be enough to determine the appropriate number). Only checked against the Pair Distance score and ignored when one string or the other is of length 1.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/fuzzy_match.rb', line 72 def initialize(competitors, = {}) = .dup # rules self.groupings = .delete(:groupings) || .delete(:blockings) || [] self.identities = .delete(:identities) || [] self.normalizers = .delete(:normalizers) || .delete(:tighteners) || [] self.stop_words = .delete(:stop_words) || [] @read = .delete(:read) || .delete(:haystack_reader) # options if deprecated = .delete(:first_blocking_decides) [:first_grouping_decides] = deprecated end if deprecated = .delete(:must_match_blocking) [:must_match_grouping] = deprecated end @default_options = DEFAULT_OPTIONS.merge().freeze # do this last self.haystack = competitors end |
Instance Attribute Details
#default_options ⇒ Object (readonly)
Returns the value of attribute default_options.
55 56 57 |
# File 'lib/fuzzy_match.rb', line 55 def @default_options end |
#groupings ⇒ Object
Returns the value of attribute groupings.
50 51 52 |
# File 'lib/fuzzy_match.rb', line 50 def groupings @groupings end |
#haystack ⇒ Object
Returns the value of attribute haystack.
49 50 51 |
# File 'lib/fuzzy_match.rb', line 49 def haystack @haystack end |
#identities ⇒ Object
Returns the value of attribute identities.
51 52 53 |
# File 'lib/fuzzy_match.rb', line 51 def identities @identities end |
#normalizers ⇒ Object
Returns the value of attribute normalizers.
52 53 54 |
# File 'lib/fuzzy_match.rb', line 52 def normalizers @normalizers end |
#read ⇒ Object
Returns the value of attribute read.
54 55 56 |
# File 'lib/fuzzy_match.rb', line 54 def read @read end |
#stop_words ⇒ Object
Returns the value of attribute stop_words.
53 54 55 |
# File 'lib/fuzzy_match.rb', line 53 def stop_words @stop_words end |
Class Method Details
.engine ⇒ Object
14 15 16 |
# File 'lib/fuzzy_match.rb', line 14 def engine @engine end |
.engine=(alt_engine) ⇒ Object
18 19 20 |
# File 'lib/fuzzy_match.rb', line 18 def engine=(alt_engine) @engine = alt_engine end |
Instance Method Details
#explain(needle, options = {}) ⇒ Object
Explain is like mysql’s EXPLAIN command. You give it a needle and it tells you about how it was located (successfully or not) in the haystack.
d = FuzzyMatch.new ['737', '747', '757' ]
d.explain 'boeing 737-100'
338 339 340 341 |
# File 'lib/fuzzy_match.rb', line 338 def explain(needle, = {}) find needle, .merge(:gather_last_result => true) last_result.explain end |
#find(needle, options = {}) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/fuzzy_match.rb', line 134 def find(needle, = {}) = .merge threshold = [:threshold] gather_last_result = [:gather_last_result] is_find_all_with_score = [:find_all_with_score] is_find_best = [:find_best] is_find_all = [:find_all] || is_find_all_with_score || is_find_best first_grouping_decides = [:first_grouping_decides] must_match_grouping = [:must_match_grouping] must_match_at_least_one_word = [:must_match_at_least_one_word] if gather_last_result @last_result = Result.new last_result.read = read last_result.haystack = haystack last_result. = last_result.timeline << <<-EOS Options were set, either by you or by falling back to defaults. \tOptions: #{.inspect} EOS end if gather_last_result last_result.normalizers = normalizers last_result.identities = identities last_result.groupings = groupings last_result.stop_words = stop_words end needle = Wrapper.new self, needle, true if gather_last_result last_result.needle = needle last_result.timeline << <<-EOS The needle's #{needle.variants.length} variants were enumerated. \tVariants: #{needle.variants.map(&:inspect).join(', ')} EOS end if must_match_grouping and groupings.any? and groupings.none? { |grouping| grouping.match? needle } if gather_last_result last_result.timeline << <<-EOS The needle didn't match any of the #{groupings.length} grouping, which was a requirement. \tGroupings (first 3): #{groupings[0,3].map(&:inspect).join(', ')} EOS end if is_find_all return [] else return nil end end if must_match_at_least_one_word passed_word_requirement = haystack.select do |straw| (needle.words & straw.words).any? end if gather_last_result last_result.timeline << <<-EOS Since :must_match_at_least_one_word => true, the competition was reduced to records sharing at least one word with the needle. \tNeedle words: #{needle.words.map(&:inspect).join(', ')} \tPassed (first 3): #{passed_word_requirement[0,3].map(&:render).map(&:inspect).join(', ')} \tFailed (first 3): #{(haystack-passed_word_requirement)[0,3].map(&:render).map(&:inspect).join(', ')} EOS end else passed_word_requirement = haystack end if groupings.any? joint = passed_word_requirement.select do |straw| if first_grouping_decides if first_grouping = groupings.detect { |grouping| grouping.match? needle } first_grouping.join? needle, straw end else groupings.any? { |grouping| grouping.join? needle, straw } end end if gather_last_result last_result.timeline << <<-EOS Since there were groupings, the competition was reduced to records in the same group as the needle. \tGroupings (first 3): #{groupings[0,3].map(&:inspect).join(', ')} \tPassed (first 3): #{joint[0,3].map(&:render).map(&:inspect).join(', ')} \tFailed (first 3): #{(passed_word_requirement-joint)[0,3].map(&:render).map(&:inspect).join(', ')} EOS end else joint = passed_word_requirement.dup end if joint.none? if must_match_grouping if gather_last_result last_result.timeline << <<-EOS Since :must_match_at_least_one_word => true and none of the competition was in the same group as the needle, the search stopped. EOS end if is_find_all return [] else return nil end else joint = passed_word_requirement.dup end end if identities.any? possibly_identical = joint.select do |straw| identities.all? do |identity| answer = identity.identical? needle, straw answer.nil? or answer == true end end if gather_last_result last_result.timeline << <<-EOS Since there were identities, the competition was reduced to records that might be identical to the needle (in other words, are not certainly different) \tIdentities (first 10 of #{identities.length}): #{identities[0,9].map(&:inspect).join(', ')} \tPassed (first 10 of #{possibly_identical.length}): #{possibly_identical[0,9].map(&:render).map(&:inspect).join(', ')} \tFailed (first 10 of #{(joint-possibly_identical).length}): #{(joint-possibly_identical)[0,9].map(&:render).map(&:inspect).join(', ')} EOS end else possibly_identical = joint.dup end similarities = possibly_identical.map { |straw| needle.similarity straw }.sort.reverse if gather_last_result last_result.timeline << <<-EOS The competition was sorted in order of similarity to the needle. \tSimilar (first 10 of #{similarities.length}): #{similarities[0,9].map { |s| "#{s.wrapper2.render.inspect} (#{[s.best_score.dices_coefficient_similar, s.best_score.levenshtein_similar].map { |v| '%0.5f' % v }.join('/')})" }.join(', ')} EOS end if is_find_all_with_score 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 if is_find_all memo = [] similarities.each do |similarity| if similarity.satisfy?(needle, threshold) memo << similarity.wrapper2.record end end return memo end best_similarity = similarities.first winner = nil if best_similarity and best_similarity.satisfy?(needle, threshold) winner = best_similarity.wrapper2.record if gather_last_result last_result.winner = winner last_result.score = best_similarity.best_score.dices_coefficient_similar last_result.timeline << <<-EOS A winner was determined because the Dice's Coefficient similarity (#{best_similarity.best_score.dices_coefficient_similar}) is greater than zero or because it shared a word with the needle. EOS end elsif gather_last_result best_similarity_record = if best_similarity and best_similarity.wrapper2 best_similarity.wrapper2.record end last_result.timeline << <<-EOS No winner assigned because the score of the best similarity (#{best_similarity_record.inspect}) was zero and it didn't match any words with the needle (#{needle.inspect}). EOS end winner end |
#find_all(needle, options = {}) ⇒ Object
119 120 121 122 |
# File 'lib/fuzzy_match.rb', line 119 def find_all(needle, = {}) = .merge(:find_all => true) find needle, end |
#find_all_with_score(needle, options = {}) ⇒ Object
129 130 131 132 |
# File 'lib/fuzzy_match.rb', line 129 def find_all_with_score(needle, = {}) = .merge(:find_all_with_score => true) find needle, end |
#find_best(needle, options = {}) ⇒ Object
124 125 126 127 |
# File 'lib/fuzzy_match.rb', line 124 def find_best(needle, = {}) = .merge(:find_best => true) find needle, end |
#free ⇒ Object
DEPRECATED
344 345 |
# File 'lib/fuzzy_match.rb', line 344 def free end |
#last_result ⇒ Object
115 116 117 |
# File 'lib/fuzzy_match.rb', line 115 def last_result @last_result || raise(::RuntimeError, "[fuzzy_match] You can't access the last result until you've run a find with :gather_last_result => true") end |