class Amatch::Levenshtein

The Levenshtein edit distance is defined as the minimal costs involved to transform one string into another by using three elementary operations: deletion, insertion and substitution of a character. To transform “water” into “wine”, for instance, you have to substitute “a” -> “i”: “witer”, “t” -> “n”: “winer” and delete “r”: “wine”. The edit distance between “water” and “wine” is 3, because you have to apply three operations. The edit distance between “wine” and “wine” is 0 of course: no operation is necessary for the transformation – they're already the same string. It's easy to see that more similar strings have smaller edit distances than strings that differ a lot.

Public Class Methods

new(pattern) click to toggle source

Creates a new Amatch::Levenshtein instance from pattern.

static VALUE rb_Levenshtein_initialize(VALUE self, VALUE pattern)
{
    GET_STRUCT(General)
    General_pattern_set(amatch, pattern);
    return self;
}

Public Instance Methods

match(strings) → results click to toggle source

Uses this Amatch::Levenshtein instance to match #pattern against strings. It returns the number operations, the Sellers distance. strings has to be either a String or an Array of Strings. The returned results is either a Float or an Array of Floats respectively.

static VALUE rb_Levenshtein_match(VALUE self, VALUE strings)
{                                                                            
    GET_STRUCT(General)
    return General_iterate_strings(amatch, strings, Levenshtein_match);
}
pattern → pattern string

Returns the current pattern string of this instance.

pattern=(pattern)

Sets the current pattern string of this instance to pattern.

similar(strings) → results click to toggle source

Uses this Amatch::Levenshtein instance to match #pattern against strings, and compute a Levenshtein distance metric number between 0.0 for very unsimilar strings and 1.0 for an exact match. strings has to be either a String or an Array of Strings. The returned results is either a Fixnum or an Array of Fixnums respectively.

static VALUE rb_Levenshtein_similar(VALUE self, VALUE strings)
{                                                                            
    GET_STRUCT(General)
    return General_iterate_strings(amatch, strings, Levenshtein_similar);
}