Module: StringMetric::Levenshtein

Defined in:
lib/string_metric/levenshtein.rb,
lib/string_metric/levenshtein/recursive.rb,
lib/string_metric/levenshtein/experiment.rb,
lib/string_metric/levenshtein/iterative_with_full_matrix.rb,
lib/string_metric/levenshtein/iterative_with_two_matrix_rows.rb,
lib/string_metric/levenshtein/iterative_with_two_matrix_rows_optimized.rb

Overview

Levenshtein Distance implementation

Defined Under Namespace

Classes: Experiment, IterativeWithFullMatrix, IterativeWithTwoMatrixRows, IterativeWithTwoMatrixRowsOptimized, Recursive

Constant Summary collapse

STRATEGIES =
{
  experiment:   Experiment,
  full_matrix:  IterativeWithFullMatrix,
  recursive:    Recursive,
  two_matrix_rows:    IterativeWithTwoMatrixRows,
  two_matrix_rows_v2: IterativeWithTwoMatrixRowsOptimized
}

Class Method Summary collapse

Class Method Details

.default_strategyObject

Currently the default strategy is set to IterativeWithTwoMatrixRows



51
52
53
54
55
56
57
# File 'lib/string_metric/levenshtein.rb', line 51

def default_strategy
  if RUBY_ENGINE == "ruby"
    pick_strategy(:two_matrix_rows_v2)
  else
    pick_strategy(:two_matrix_rows)
  end
end

.distance(from, to, options = {}) ⇒ Fixnum, Float

Levenshtein Distance of two strings

Parameters:

  • from (String)

    the first string

  • to (String)

    the second string

  • options (Hash) (defaults to: {})

    options

Options Hash (options):

  • :max_distance (Fixnum, Float)

    If this option is passed then levenstein distance is trimmed to this value (if greater)

  • :insertion_cost (Fixnum, Float)

    If this option is passed then new insertion cost is taken into account (by default is 1)

  • :deletion_cost (Fixnum, Float)

    If this option is passed then new deletion cost is taken into account (by default is 1)

  • :substitution_cost (Fixnum, Float)

    If this option is passed then new substitution cost is taken into account (be default is 1)

  • :strategy (Symbol)

    The desired strategy for Levenshtein distance. Supported strategies are :recursive, :two_matrix_rows, :full_matrix, :two_matrix_rows_v2 and :experiment. The default strategy is :two_matrix_rows_v2 for MRI and :two_matrix_rows for other platforms. One should not depend on :experiment strategy.

Returns:

  • (Fixnum, Float)

    the Levenshtein Distance



42
43
44
45
46
47
# File 'lib/string_metric/levenshtein.rb', line 42

def distance(from, to, options = {})
  strategy = pick_strategy(options[:strategy]) || Levenshtein.default_strategy
  args = [from, to, options]

  strategy.distance(*args)
end