Top Description Fields Constructors Methods
org.apache.commons.text.similarity

public Class DamerauLevenshteinDistance

extends Object
implements EditDistance<Integer>
Class Inheritance
All Implemented Interfaces
org.apache.commons.text.similarity.EditDistance, org.apache.commons.text.similarity.SimilarityScore, org.apache.commons.text.similarity.ObjectSimilarityScore, java.util.function.BiFunction

An algorithm for measuring the difference between two character sequences using the Damerau-Levenshtein Distance.

This is the number of changes needed to change one sequence into another, where each change is a single character modification (deletion, insertion, substitution, or transposition of two adjacent characters).

Since
1.15.0
See Also
Damerau-Levenshtein Distance on Wikipedia

Field Summary

Modifier and TypeField and Description
private final Integer
threshold

Threshold.

Constructor Summary

AccessConstructor and Description
public
DamerauLevenshteinDistance()

Constructs a default instance that uses a version of the algorithm that does not use a threshold parameter.

public
DamerauLevenshteinDistance(final Integer
If this is null then distances calculations will not be limited. This may not be negative.
threshold
)

Constructs a new instance.

Method Summary

Modifier and TypeMethod and Description
public Integer

Returns:

result distance, or -1 if threshold is exceeded.
apply
(final CharSequence
the first input, must not be null.
left
,
final CharSequence
the second input, must not be null.
right
)

Implements org.apache.commons.text.similarity.SimilarityScore.apply.

Computes the Damerau-Levenshtein distance between two Strings.

public <
The type of similarity score unit.
E
>
Integer

Returns:

result distance, or -1 if threshold is exceeded.
apply
(final SimilarityInput<E>
the first input, must not be null.
left
,
final SimilarityInput<E>
the second input, must not be null.
right
)

Implements org.apache.commons.text.similarity.ObjectSimilarityScore.apply.

Computes the Damerau-Levenshtein distance between two inputs.

private static int

Returns:

The distance value, or -1 if distance is greater than threshold.
clampDistance
(final int
The distance value.
distance
,
final int
The threshold value.
threshold
)

Utility function to ensure distance is valid according to threshold.

public Integer

Returns:

The distance threshold.
getThreshold
()

Gets the distance threshold.

private static <E> int

Returns:

result distance, or -1 if distance exceeds threshold.
limitedCompare
(SimilarityInput<E>
the first SimilarityInput, must not be null.
left
,
SimilarityInput<E>
the second SimilarityInput, must not be null.
right
,
final int
the target threshold, must not be negative.
threshold
)

Finds the Damerau-Levenshtein distance between two CharSequences if it's less than or equal to a given threshold.

private static <E> int

Returns:

result distance.
unlimitedCompare
(SimilarityInput<E>
the first CharSequence, must not be null.
left
,
SimilarityInput<E>
the second CharSequence, must not be null.
right
)

Finds the Damerau-Levenshtein distance between two inputs using optimal string alignment.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

thresholdback to summary
private final Integer threshold

Threshold.

Constructor Detail

DamerauLevenshteinDistanceback to summary
public DamerauLevenshteinDistance()

Constructs a default instance that uses a version of the algorithm that does not use a threshold parameter.

DamerauLevenshteinDistanceback to summary
public DamerauLevenshteinDistance(final Integer threshold)

Constructs a new instance. 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.

Parameters
threshold:Integer

If this is null then distances calculations will not be limited. This may not be negative.

Method Detail

applyback to summary
public Integer apply(final CharSequence left, final CharSequence right)

Implements org.apache.commons.text.similarity.SimilarityScore.apply.

Computes the Damerau-Levenshtein distance between two Strings.

A higher score indicates a greater distance.

Parameters
left:CharSequence

the first input, must not be null.

right:CharSequence

the second input, must not be null.

Returns:Integer

result distance, or -1 if threshold is exceeded.

Annotations
@Override
Exceptions
IllegalArgumentException:
if either String input null.
applyback to summary
public <E> Integer apply(final SimilarityInput<E> left, final SimilarityInput<E> right)

Implements org.apache.commons.text.similarity.ObjectSimilarityScore.apply.

Computes the Damerau-Levenshtein distance between two inputs.

A higher score indicates a greater distance.

Parameters
<E>
The type of similarity score unit.
left:SimilarityInput<E>

the first input, must not be null.

right:SimilarityInput<E>

the second input, must not be null.

Returns:Integer

result distance, or -1 if threshold is exceeded.

Exceptions
IllegalArgumentException:
if either String input null.
Since
1.13.0
clampDistanceback to summary
private static int clampDistance(final int distance, final int threshold)

Utility function to ensure distance is valid according to threshold.

Parameters
distance:int

The distance value.

threshold:int

The threshold value.

Returns:int

The distance value, or -1 if distance is greater than threshold.

getThresholdback to summary
public Integer getThreshold()

Gets the distance threshold.

Returns:Integer

The distance threshold.

limitedCompareback to summary
private static <E> int limitedCompare(SimilarityInput<E> left, SimilarityInput<E> right, final int threshold)

Finds the Damerau-Levenshtein distance between two CharSequences if it's less than or equal to a given threshold.

Parameters
left:SimilarityInput<E>

the first SimilarityInput, must not be null.

right:SimilarityInput<E>

the second SimilarityInput, must not be null.

threshold:int

the target threshold, must not be negative.

Returns:int

result distance, or -1 if distance exceeds threshold.

unlimitedCompareback to summary
private static <E> int unlimitedCompare(SimilarityInput<E> left, SimilarityInput<E> right)

Finds the Damerau-Levenshtein distance between two inputs using optimal string alignment.

Parameters
left:SimilarityInput<E>

the first CharSequence, must not be null.

right:SimilarityInput<E>

the second CharSequence, must not be null.

Returns:int

result distance.

Exceptions
IllegalArgumentException:
if either CharSequence input is null.