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
17
# 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



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

def merge(res)
  unless 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



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

def order
  @order ||= %i[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


46
47
48
49
50
# File 'lib/eco/data/fuzzy_match/results.rb', line 46

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


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

def print
  msg = results.map(&:print).join("\n  ")

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

#relevant_results(**options) ⇒ Object



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

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.then do |filtered|
    self.class.new(needle, value, filtered).tap do |results|
      results.order = options[:order]
    end
  end
end

#resultsObject



56
57
58
# File 'lib/eco/data/fuzzy_match/results.rb', line 56

def results
  raw_results.sort
end

#results_with_false_positivesObject



28
29
30
# File 'lib/eco/data/fuzzy_match/results.rb', line 28

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