smile.math.distance
Class EditDistance
- java.lang.Object
-
- smile.math.distance.EditDistance
-
- All Implemented Interfaces:
- java.io.Serializable, Distance<java.lang.String>, Metric<java.lang.String>
public class EditDistance extends java.lang.Object implements Metric<java.lang.String>, java.io.Serializable
The Edit distance between two strings is a metric for measuring the amount of difference between two sequences. The Levenshtein distance between two strings is given by the minimum number of operations needed to transform one string into the other, where an operation is an insertion, deletion, or substitution of a single character. A generalization of the Levenshtein distance (Damerau-Levenshtein distance) allows the transposition of two characters as an operation.Given two strings x and y of length m and n (suppose n ≥ m), this implementation takes O(ne) time and O(mn) space by an extended Ukkonen's algorithm in case of unit cost, where e is the edit distance between x and y. Thus this algorithm is output sensitive. The smaller the distance, the faster it runs.
For weighted cost, this implements the regular dynamic programming algorithm, which takes O(mn) time and O(m) space.
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor and Description EditDistance(double[][] weight)Constructor.EditDistance(double[][] weight, double radius)Constructor.EditDistance(int maxStringLength)Constructor.EditDistance(int maxStringLength, boolean damerau)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description doubled(char[] x, char[] y)Edit distance between two strings.doubled(java.lang.String x, java.lang.String y)Edit distance between two strings.static intdamerau(char[] x, char[] y)Damerau-Levenshtein distance between two strings allows insertion, deletion, substitution, or transposition of characters.static intdamerau(java.lang.String x, java.lang.String y)Damerau-Levenshtein distance between two strings allows insertion, deletion, substitution, or transposition of characters.static intlevenshtein(char[] x, char[] y)Levenshtein distance between two strings allows insertion, deletion, or substitution of characters.static intlevenshtein(java.lang.String x, java.lang.String y)Levenshtein distance between two strings allows insertion, deletion, or substitution of characters.java.lang.StringtoString()
-
-
-
Constructor Detail
-
EditDistance
public EditDistance(double[][] weight)
Constructor. Weighted Levenshtein distance without path constraints. Only insertion, deletion, and substitution operations are supported.
-
EditDistance
public EditDistance(double[][] weight, double radius)Constructor. Weighted Levenshtein distance with Sakoe-Chiba band, which improve computational cost. Only insertion, deletion, and substitution operations are supported.- Parameters:
radius- the window width of Sakoe-Chiba band in terms of percentage of sequence length.
-
EditDistance
public EditDistance(int maxStringLength)
Constructor. Unit cost edit distance.- Parameters:
maxStringLength- the maximum length of strings that will be feed to this algorithm.
-
EditDistance
public EditDistance(int maxStringLength, boolean damerau)Constructor. Damerau-Levenshtein distance.- Parameters:
maxStringLength- the maximum length of strings that will be feed to this algorithm.damerau- if true, calculate Damerau-Levenshtein distance instead of plain Levenshtein distance.
-
-
Method Detail
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
d
public double d(java.lang.String x, java.lang.String y)Edit distance between two strings. O(mn) time and O(n) space for weighted edit distance. O(ne) time and O(mn) space for unit cost edit distance. For weighted edit distance, this method is multi-thread safe. However, it is NOT multi-thread safe for unit cost edit distance.
-
d
public double d(char[] x, char[] y)Edit distance between two strings. O(mn) time and O(n) space for weighted edit distance. O(ne) time and O(mn) space for unit cost edit distance. For weighted edit distance, this method is multi-thread safe. However, it is NOT multi-thread safe for unit cost edit distance.
-
levenshtein
public static int levenshtein(java.lang.String x, java.lang.String y)Levenshtein distance between two strings allows insertion, deletion, or substitution of characters. O(mn) time and O(n) space. Multi-thread safe.
-
levenshtein
public static int levenshtein(char[] x, char[] y)Levenshtein distance between two strings allows insertion, deletion, or substitution of characters. O(mn) time and O(n) space. Multi-thread safe.
-
damerau
public static int damerau(java.lang.String x, java.lang.String y)Damerau-Levenshtein distance between two strings allows insertion, deletion, substitution, or transposition of characters. O(mn) time and O(n) space. Multi-thread safe.
-
damerau
public static int damerau(char[] x, char[] y)Damerau-Levenshtein distance between two strings allows insertion, deletion, substitution, or transposition of characters. O(mn) time and O(n) space. Multi-thread safe.
-
-
DataMelt 3.0 © DataMelt by jWork.ORG