public class LevenshteinDistance extends Object implements EditDistance<Integer>
This is the number of changes needed to change one sequence into another, where each change is a single character modification (deletion, insertion or substitution).
This code has been adapted from Apache Commons Lang 3.3.
Constructor and Description |
---|
LevenshteinDistance()
This returns the default instance that uses a version
of the algorithm that does not use a threshold parameter.
|
LevenshteinDistance(Integer threshold)
If the threshold is not null, distance calculations will be limited to a maximum length.
|
Modifier and Type | Method and Description |
---|---|
Integer |
apply(CharSequence left,
CharSequence right)
Find the Levenshtein distance between two Strings.
|
static LevenshteinDistance |
getDefaultInstance()
Gets the default instance.
|
Integer |
getThreshold()
Gets the distance threshold.
|
public LevenshteinDistance()
This returns the default instance that uses a version of the algorithm that does not use a threshold parameter.
getDefaultInstance()
public LevenshteinDistance(Integer threshold)
If the threshold is not null, distance calculations will be limited to a maximum length. If the threshold is null, the unlimited version of the algorithm will be used.
threshold
- If this is null then distances calculations will not be limited.
This may not be negative.public Integer apply(CharSequence left, CharSequence right)
Find the Levenshtein distance between two Strings.
A higher score indicates a greater distance.
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.apply(null, *) = IllegalArgumentException distance.apply(*, null) = IllegalArgumentException distance.apply("","") = 0 distance.apply("","a") = 1 distance.apply("aaapppp", "") = 7 distance.apply("frog", "fog") = 1 distance.apply("fly", "ant") = 3 distance.apply("elephant", "hippo") = 7 distance.apply("hippo", "elephant") = 7 distance.apply("hippo", "zzzzzzzz") = 8 distance.apply("hello", "hallo") = 1
apply
in interface EditDistance<Integer>
apply
in interface SimilarityScore<Integer>
left
- the first string, must not be nullright
- the second string, must not be nullIllegalArgumentException
- if either String input null
public static LevenshteinDistance getDefaultInstance()
public Integer getThreshold()
Copyright © 2014–2017 The Apache Software Foundation. All rights reserved.