Class: Eco::Data::FuzzyMatch::Results

Inherits:
Struct
  • Object
show all
Includes:
Enumerable
Defined in:
lib/eco/data/fuzzy_match/results.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#needleObject

Returns the value of attribute needle

Returns:

  • (Object)

    the current value of needle



4
5
6
# File 'lib/eco/data/fuzzy_match/results.rb', line 4

def needle
  @needle
end

#raw_resultsObject

Returns the value of attribute raw_results

Returns:

  • (Object)

    the current value of raw_results



4
5
6
# File 'lib/eco/data/fuzzy_match/results.rb', line 4

def raw_results
  @raw_results
end

#thresholdObject

Returns the value of attribute threshold.



7
8
9
# File 'lib/eco/data/fuzzy_match/results.rb', line 7

def threshold
  @threshold
end

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



4
5
6
# File 'lib/eco/data/fuzzy_match/results.rb', line 4

def value
  @value
end

Instance Method Details

#each(&block) ⇒ Object



13
14
15
16
# File 'lib/eco/data/fuzzy_match/results.rb', line 13

def each(&block)
  return to_enum(:each) unless block
  raw_results.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/eco/data/fuzzy_match/results.rb', line 9

def empty?
  count < 1
end

#merge(res) ⇒ Object

Merges the results of both Results object



19
20
21
22
23
24
# File 'lib/eco/data/fuzzy_match/results.rb', line 19

def merge(res)
  unless self.needle == res.needle
    raise "To merge 2 Results, needle should be the same ('#{value}'). Given '#{res.value}'"
  end
  self.class.new(needle, value, raw_results.concat(res.raw_results))
end

#orderObject



49
50
51
# File 'lib/eco/data/fuzzy_match/results.rb', line 49

def order
  @order ||= [:words_ngrams, :dice]
end

#order=(values) ⇒ Object

Parameters:

  • values (Array<Symbol>)

    the algorithms' results it should be ordered by

    • Possible values: :dice, :levenshtein, :jaro_winkler, :ngrams, :words_ngrams, :chars_position, :average


43
44
45
46
47
# File 'lib/eco/data/fuzzy_match/results.rb', line 43

def order=(values)
  @order = [values].flatten.compact.tap do |o|
    raw_results.each {|r| r.order = o}
  end
end


57
58
59
60
61
62
63
# File 'lib/eco/data/fuzzy_match/results.rb', line 57

def print
  msg = results.map do |result|
    result.print
  end.join("\n  ")

  puts "'#{value}':\n  " + msg
end

#relevant_results(**options) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/eco/data/fuzzy_match/results.rb', line 30

def relevant_results(**options)
  options = {order: order, threshold: threshold || 0.5}.merge(options)
  raw_results.select do |result|
    result.all_threshold?(options[:order], options[:threshold])
  end.yield_self do |filtered|
    self.class.new(needle, value, filtered).tap do |results|
      results.order = options[:order]
    end
  end
end

#resultsObject



53
54
55
# File 'lib/eco/data/fuzzy_match/results.rb', line 53

def results
  raw_results.sort
end

#results_with_false_positivesObject



26
27
28
# File 'lib/eco/data/fuzzy_match/results.rb', line 26

def results_with_false_positives
  relevant_results(order: :jaro_winkler, threshold: 0.5)
end