Top Description Inners Constructors Methods
de.undercouch.citeproc.helper

public Class Levenshtein

extends Object
Class Inheritance
Imports
org.apache.commons.lang3.StringUtils, org.apache.commons.text.similarity.LevenshteinDistance, java.util.ArrayList, .Collection, .Collections, .LinkedHashSet, .LinkedList, .List

Uses LevenshteinDistance#apply(CharSequence, CharSequence) to calculate the edit distance between two strings. Provides useful helper methods to traverse a set of strings and select the most similar ones to a given input string.
Author
Michel Kraemer

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static <
the type of the strings in the given collection
T extends CharSequence
>
T

Returns:

the string with the lowest Levenshtein distance
findMinimum
(Collection<T>
the collection to search
ss
,
CharSequence
the second string
t
)

Searches the given collection of strings and returns the string that has the lowest Levenshtein distance to a given second string t.

public static <
the type of the strings in the given collection
T extends CharSequence
>
Collection<T>

Returns:

the strings with the lowest Levenshtein distance
findMinimum
(Collection<T>
the collection to search
ss
,
CharSequence
the string to compare to
t
,
int
the maximum number of strings to return
n
,
int
a threshold for individual item distances. Only items with a distance below this threshold will be included in the result.
threshold
)

Searches the given collection of strings and returns a collection of at most n strings that have the lowest Levenshtein distance to a given string t.

public static <
the type of the strings in the given collection
T extends CharSequence
>
Collection<T>

Returns:

a collection with similar strings
findSimilar
(Collection<T>
the collection to search
ss
,
CharSequence
the string to compare to
t
)

Searches the given collection of strings and returns a collection of strings similar to a given string t.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Levenshteinback to summary
public Levenshtein()

Method Detail

findMinimumback to summary
public static <T extends CharSequence> T findMinimum(Collection<T> ss, CharSequence t)

Searches the given collection of strings and returns the string that has the lowest Levenshtein distance to a given second string t. If the collection contains multiple strings with the same distance to t only the first one will be returned.

Parameters
<T>
the type of the strings in the given collection
ss:Collection<T>

the collection to search

t:CharSequence

the second string

Returns:T

the string with the lowest Levenshtein distance

findMinimumback to summary
public static <T extends CharSequence> Collection<T> findMinimum(Collection<T> ss, CharSequence t, int n, int threshold)

Searches the given collection of strings and returns a collection of at most n strings that have the lowest Levenshtein distance to a given string t. The returned collection will be sorted according to the distance with the string with the lowest distance at the first position.

Parameters
<T>
the type of the strings in the given collection
ss:Collection<T>

the collection to search

t:CharSequence

the string to compare to

n:int

the maximum number of strings to return

threshold:int

a threshold for individual item distances. Only items with a distance below this threshold will be included in the result.

Returns:Collection<T>

the strings with the lowest Levenshtein distance

findSimilarback to summary
public static <T extends CharSequence> Collection<T> findSimilar(Collection<T> ss, CharSequence t)

Searches the given collection of strings and returns a collection of strings similar to a given string t. Uses reasonable default values for human-readable strings. The returned collection will be sorted according to their similarity with the string with the best match at the first position.

Parameters
<T>
the type of the strings in the given collection
ss:Collection<T>

the collection to search

t:CharSequence

the string to compare to

Returns:Collection<T>

a collection with similar strings

de.undercouch.citeproc.helper back to summary

private Class Levenshtein.Item<T>

extends Object
implements Comparable<Levenshtein.Item<T>>
Class Inheritance
All Implemented Interfaces
java.lang.Comparable

Field Summary

Modifier and TypeField and Description
private final int
private final T

Constructor Summary

AccessConstructor and Description
public
Item(T str, int distance)

Method Summary

Modifier and TypeMethod and Description
public int
compareTo(Levenshtein.Item<T> o)

Implements java.lang.Comparable.compareTo.

Compares this object with the specified object for order.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

distanceback to summary
private final int distance
strback to summary
private final T str

Constructor Detail

Itemback to summary
public Item(T str, int distance)

Method Detail

compareToback to summary
public int compareTo(Levenshtein.Item<T> o)

Implements java.lang.Comparable.compareTo.

Doc from java.lang.Comparable.compareTo.

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

The implementor must ensure signum(x.compareTo(y)) == -signum(y.compareTo(x)) for all x and y. (This implies that x.compareTo(y) must throw an exception if and only if y.compareTo(x) throws an exception.)

The implementor must also ensure that the relation is transitive: (x.compareTo(y) > 0 && y.compareTo(z) > 0) implies x.compareTo(z) > 0.

Finally, the implementor must ensure that x.compareTo(y)==0 implies that signum(x.compareTo(z)) == signum(y.compareTo(z)), for all z.

Annotations
@Override