Class LevenshteinDistance
java.lang.Object
com.sk89q.worldedit.util.function.LevenshteinDistance
- All Implemented Interfaces:
com.google.common.base.Function<String,
,Integer> Function<String,
Integer>
public class LevenshteinDistance
extends Object
implements com.google.common.base.Function<String,Integer>
Provides a Levenshtein distance between a given string and each string
that this function is applied to.
-
Field Summary
-
Constructor Summary
ConstructorDescriptionLevenshteinDistance
(String baseString, boolean caseSensitive) Create a new instance.LevenshteinDistance
(String baseString, boolean caseSensitive, Pattern replacePattern) Create a new instance. -
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.google.common.base.Function
equals
-
Field Details
-
STANDARD_CHARS
-
-
Constructor Details
-
LevenshteinDistance
Create a new instance.- Parameters:
baseString
- the string to compare tocaseSensitive
- true to make case sensitive comparisons
-
LevenshteinDistance
public LevenshteinDistance(String baseString, boolean caseSensitive, @Nullable Pattern replacePattern) Create a new instance.- Parameters:
baseString
- the string to compare tocaseSensitive
- true to make case sensitive comparisonsreplacePattern
- pattern to match characters to be removed in both the input and test strings (may be null)
-
-
Method Details
-
apply
-
distance
Find the Levenshtein distance between two Strings.
This is the number of changes needed to change one String into another, where each change is a single character modification (deletion, insertion or substitution).
The previous implementation of the Levenshtein distance algorithm was from http://www.merriampark.com/ld.htm
Chas Emerick has written an implementation in Java, which avoids an OutOfMemoryError which can occur when my Java implementation is used with very large strings.
This implementation of the Levenshtein distance algorithm is from http://www.merriampark.com/ldjava.htmdistance(null, *) = IllegalArgumentException distance(*, null) = IllegalArgumentException distance("","") = 0 distance("","a") = 1 distance("aaapppp", "") = 7 distance("frog", "fog") = 1 distance("fly", "ant") = 3 distance("elephant", "hippo") = 7 distance("hippo", "elephant") = 7 distance("hippo", "zzzzzzzz") = 8 distance("hello", "hallo") = 1
- Parameters:
s
- the first String, must not be nullt
- the second String, must not be null- Returns:
- result distance
- Throws:
IllegalArgumentException
- if either String inputnull
-