module Edits::Levenshtein

Overview

Implementation of Levenshtein distance algorithm.

Determines distance between two string by counting edits, identifying:

Defined in:

edits/levenshtein.cr

Class Method Summary

Class Method Detail

def self.distance(str1, str2, max : Int32) #

Calculate the Levenshtein (edit) distance of two sequences, bounded by a maximum value. For low max values, this can have better performance.

Levenshtein.distance 'cloud', 'crayon'                # => 5
Levenshtein.distance_with_max 'cloud', 'crayon', 2    # => 2

[View source]
def self.distance(str1, str2) #

Calculate the Levenshtein (edit) distance of two sequences.

Note: a true distance metric, satisfies triangle inequality.

Levenshtein.distance('sand', 'hands') # => 2


[View source]
def self.most_similar(prototype, strings : Enumerable) #

Given a prototype string and an array of strings, determines which string is most similar to the prototype.

Levenshtein.most_similar('foo', strings) is functionally equivalent to strings.min_by { |s| Levenshtein.distance('foo', s) }, leveraging a max distance.


[View source]