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
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
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); }
Returns the current pattern string of this instance.
Sets the current pattern string of this instance to pattern
.
searches #pattern in
strings
and returns the edit distance (the sum of character
operations) as a Fixnum value, by greedy trimming prefixes or postfixes of
the match. 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_search(VALUE self, VALUE strings) { GET_STRUCT(General) return General_iterate_strings(amatch, strings, Levenshtein_search); }
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); }