public class LevenshteinDistance
extends java.lang.Object
implements com.google.common.base.Function<java.lang.String,java.lang.Integer>
Modifier and Type | Field and Description |
---|---|
static java.util.regex.Pattern |
STANDARD_CHARS |
Constructor and Description |
---|
LevenshteinDistance(java.lang.String baseString,
boolean caseSensitive)
Create a new instance.
|
LevenshteinDistance(java.lang.String baseString,
boolean caseSensitive,
java.util.regex.Pattern replacePattern)
Create a new instance.
|
Modifier and Type | Method and Description |
---|---|
java.lang.Integer |
apply(java.lang.String input) |
static int |
distance(java.lang.String s,
java.lang.String t)
Find the Levenshtein distance between two Strings.
|
public LevenshteinDistance(java.lang.String baseString, boolean caseSensitive)
baseString
- the string to compare tocaseSensitive
- true to make case sensitive comparisonspublic LevenshteinDistance(java.lang.String baseString, boolean caseSensitive, @Nullable java.util.regex.Pattern replacePattern)
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)@Nullable public java.lang.Integer apply(java.lang.String input)
apply
in interface java.util.function.Function<java.lang.String,java.lang.Integer>
public static int distance(java.lang.String s, java.lang.String t)
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.htm
distance(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
s
- the first String, must not be nullt
- the second String, must not be nulljava.lang.IllegalArgumentException
- if either String input null