Class: Amatch::Levenshtein

Inherits:
Object
  • Object
show all
Defined in:
ext/amatch_ext.c,
ext/amatch_ext.c

Overview

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.

Instance Method Summary collapse

Constructor Details

#initializeObject

Instance Method Details

#matchObject

#patternObject

call-seq: pattern -> pattern string

Returns the current pattern string of this instance.

#pattern=Object

call-seq: pattern=(pattern)

Sets the current pattern string of this instance to pattern.

#search(strings) ⇒ Object

searches Amatch::Levenshtein#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.



861
862
863
864
865
# File 'ext/amatch_ext.c', line 861

static VALUE rb_Levenshtein_search(VALUE self, VALUE strings)
{                                                                            
    GET_STRUCT(General)
    return General_iterate_strings(amatch, strings, Levenshtein_search);
}

#similar(strings) ⇒ Object

Uses this Amatch::Levenshtein instance to match Amatch::Levenshtein#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.



830
831
832
833
834
# File 'ext/amatch_ext.c', line 830

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