Top Description Constructors Methods
info.debatty.java.stringsimilarity

public Class Levenshtein

extends Object
implements MetricStringDistance
Class Inheritance
All Implemented Interfaces
info.debatty.java.stringsimilarity.interfaces.MetricStringDistance, info.debatty.java.stringsimilarity.interfaces.StringDistance, java.io.Serializable
Annotations
@Immutable
Imports
info.debatty.java.stringsimilarity.interfaces.MetricStringDistance, net.jcip.annotations.Immutable

The Levenshtein distance between two words is the minimum number of single-character edits (insertions, deletions or substitutions) required to change one string into the other.
Author
Thibault Debatty

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public final double
distance(final String s1, final String s2)

Implements info.debatty.java.stringsimilarity.interfaces.MetricStringDistance.distance.

Equivalent to distance(s1, s2, Integer.MAX_VALUE).

public final double

Returns:

The computed Levenshtein distance.
distance
(final String
The first string to compare.
s1
,
final String
The second string to compare.
s2
,
final int
The maximum result to compute before stopping. This means that the calculation can terminate early if you only care about strings with a certain similarity. Set this to Integer.MAX_VALUE if you want to run the calculation to completion in every case.
limit
)

The Levenshtein distance, or edit distance, between two words is the minimum number of single-character edits (insertions, deletions or substitutions) required to change one word into the other.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Levenshteinback to summary
public Levenshtein()

Method Detail

distanceback to summary
public final double distance(final String s1, final String s2)

Implements info.debatty.java.stringsimilarity.interfaces.MetricStringDistance.distance.

Equivalent to distance(s1, s2, Integer.MAX_VALUE).

distanceback to summary
public final double distance(final String s1, final String s2, final int limit)

The Levenshtein distance, or edit distance, between two words is the minimum number of single-character edits (insertions, deletions or substitutions) required to change one word into the other. http://en.wikipedia.org/wiki/Levenshtein_distance It is always at least the difference of the sizes of the two strings. It is at most the length of the longer string. It is zero if and only if the strings are equal. If the strings are the same size, the Hamming distance is an upper bound on the Levenshtein distance. The Levenshtein distance verifies the triangle inequality (the distance between two strings is no greater than the sum Levenshtein distances from a third string). Implementation uses dynamic programming (Wagner–Fischer algorithm), with only 2 rows of data. The space requirement is thus O(m) and the algorithm runs in O(mn).

Parameters
s1:String

The first string to compare.

s2:String

The second string to compare.

limit:int

The maximum result to compute before stopping. This means that the calculation can terminate early if you only care about strings with a certain similarity. Set this to Integer.MAX_VALUE if you want to run the calculation to completion in every case.

Returns:double

The computed Levenshtein distance.

Exceptions
NullPointerException:
if s1 or s2 is null.