class FuzzyWuzzy

Public Class Methods

new(value, other_value) click to toggle source
# File lib/match-mate/utils/fuzzy_wuzzy.rb, line 4
def initialize(value, other_value)
  @values = [value].flatten.map(&:to_s)
  @other_values = [other_value].flatten.map(&:to_s)
  PyCall.exec import
end

Public Instance Methods

partial_ratio() click to toggle source

Compares partial string similarity

# File lib/match-mate/utils/fuzzy_wuzzy.rb, line 21
def partial_ratio
  run_command :partial_ratio
end
ratio() click to toggle source

Compares the entire string similarity, in order.

# File lib/match-mate/utils/fuzzy_wuzzy.rb, line 26
def ratio
  run_command :ratio
end
token_set_ratio() click to toggle source

Compares the entire string similarity, ignores word order and duplicated words

# File lib/match-mate/utils/fuzzy_wuzzy.rb, line 11
def token_set_ratio
  run_command :token_set_ratio
end
token_sort_ratio() click to toggle source

Compares the entire string similarity, ignores word order

# File lib/match-mate/utils/fuzzy_wuzzy.rb, line 16
def token_sort_ratio
  run_command :token_sort_ratio
end

Private Instance Methods

combinations() click to toggle source
# File lib/match-mate/utils/fuzzy_wuzzy.rb, line 52
def combinations
  @values.product @other_values
end
command(method, value, other_value) click to toggle source
# File lib/match-mate/utils/fuzzy_wuzzy.rb, line 66
  def command(method, value, other_value)
    <<~PYTHON
      fuzz.#{method}("#{value}", "#{other_value}")
    PYTHON
  end
import() click to toggle source
# File lib/match-mate/utils/fuzzy_wuzzy.rb, line 56
  def import
    <<~PYTHON
      from fuzzywuzzy import fuzz
    PYTHON
  end
normalize(value) click to toggle source
# File lib/match-mate/utils/fuzzy_wuzzy.rb, line 62
def normalize(value)
  value.strip.squeeze(' ').downcase
end
run_command(method) click to toggle source
# File lib/match-mate/utils/fuzzy_wuzzy.rb, line 32
def run_command(method)
  scores = []
  combinations.each do |value, other_value|
    score = score_for method, value, other_value
    scores << score
    break if score == 100
  end
  scores.max
end
score_for(method, value, other_value) click to toggle source
# File lib/match-mate/utils/fuzzy_wuzzy.rb, line 42
def score_for(method, value, other_value)
  value = normalize(value)
  other_value = normalize(other_value)
  if value == other_value
    100
  else
    PyCall.eval command(method, value, other_value)
  end
end