Class: Eco::Data::FuzzyMatch::Result

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

Constant Summary collapse

ALL_METHODS =
%i[
  dice levenshtein jaro_winkler
  ngrams words_ngrams chars_position
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#chars_positionObject

Returns the value of attribute chars_position

Returns:

  • (Object)

    the current value of chars_position



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

def chars_position
  @chars_position
end

#diceObject

Returns the value of attribute dice

Returns:

  • (Object)

    the current value of dice



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

def dice
  @dice
end

#jaro_winklerObject

Returns the value of attribute jaro_winkler

Returns:

  • (Object)

    the current value of jaro_winkler



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

def jaro_winkler
  @jaro_winkler
end

#levenshteinObject

Returns the value of attribute levenshtein

Returns:

  • (Object)

    the current value of levenshtein



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

def levenshtein
  @levenshtein
end

#matchObject

Returns the value of attribute match

Returns:

  • (Object)

    the current value of match



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

def match
  @match
end

#needle_valueObject

Returns the value of attribute needle_value

Returns:

  • (Object)

    the current value of needle_value



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

def needle_value
  @needle_value
end

#ngramsObject

Returns the value of attribute ngrams

Returns:

  • (Object)

    the current value of ngrams



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

def ngrams
  @ngrams
end

#pivotObject

Returns the value of attribute pivot.



20
21
22
# File 'lib/eco/data/fuzzy_match/result.rb', line 20

def pivot
  @pivot
end

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



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

def value
  @value
end

#words_ngramsObject

Returns the value of attribute words_ngrams

Returns:

  • (Object)

    the current value of words_ngrams



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

def words_ngrams
  @words_ngrams
end

Instance Method Details

#<=>(other) ⇒ Object



106
107
108
# File 'lib/eco/data/fuzzy_match/result.rb', line 106

def <=>(other)
  compare(other)
end

#all_threshold?(methods = order, threshold = 0.15) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
# File 'lib/eco/data/fuzzy_match/result.rb', line 78

def all_threshold?(methods = order, threshold = 0.15)
  return true unless threshold

  [methods].flatten.compact.all? {|method| threshold?(method, threshold)}
end

#any_threshold?(methods = order, threshold = 0.15) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
# File 'lib/eco/data/fuzzy_match/result.rb', line 84

def any_threshold?(methods = order, threshold = 0.15)
  return true unless threshold

  [methods].flatten.compact.any? {|method| threshold?(method, threshold)}
end

#averageObject



63
64
65
66
# File 'lib/eco/data/fuzzy_match/result.rb', line 63

def average
  values = [dice, levenshtein, jaro_winkler, ngrams, words_ngrams, chars_position]
  (values.inject(0.0, :+) / values.length).round(3)
end

#jaroObject



51
52
53
# File 'lib/eco/data/fuzzy_match/result.rb', line 51

def jaro
  jaro_winkler
end

#levObject

Shortcuts



47
48
49
# File 'lib/eco/data/fuzzy_match/result.rb', line 47

def lev
  levenshtein
end

#orderObject



102
103
104
# File 'lib/eco/data/fuzzy_match/result.rb', line 102

def order
  @order ||= %i[words_ngrams dice]
end

#order=(values) ⇒ Object



96
97
98
99
100
# File 'lib/eco/data/fuzzy_match/result.rb', line 96

def order=(values)
  @order = [values].flatten.compact.tap do |o|
    o << %i[words_ngrams dice] if o.empty?
  end
end

#posObject



59
60
61
# File 'lib/eco/data/fuzzy_match/result.rb', line 59

def pos
  chars_position
end

TODO: print in the order of order



69
70
71
72
73
74
75
76
# File 'lib/eco/data/fuzzy_match/result.rb', line 69

def print
  msg  = "(Dice: #{dice}) (Lev Dst: #{levenshtein}) "
  msg << "(Jaro: #{jaro_winkler}) "
  msg << "(Ngram: #{ngrams}) (WNgrams: #{words_ngrams}) "
  msg << "(C Pos: #{chars_position}) "
  msg << "(Avg: #{average}) "
  msg << "'#{value}'"
end

#threshold?(method = :dice, threshold = 0.15) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


90
91
92
93
94
# File 'lib/eco/data/fuzzy_match/result.rb', line 90

def threshold?(method = :dice, threshold = 0.15)
  raise ArgumentError, "Uknown method '#{method}'" unless respond_to?(method, true)

  send(method) >= threshold
end

#values_at(*keys) ⇒ Object



110
111
112
113
114
# File 'lib/eco/data/fuzzy_match/result.rb', line 110

def values_at(*keys)
  keys.map do |key|
    send(key) if respond_to?(key)
  end
end

#wngramsObject



55
56
57
# File 'lib/eco/data/fuzzy_match/result.rb', line 55

def wngrams
  words_ngrams
end