EditDistance implementation and a CharSequence "left" string.
The apply(CharSequence right) method accepts the "right" string and invokes the
comparison function for the pair of strings.
The following is an example which finds the most similar string:
EditDistance<Integer> editDistance = new LevenshteinDistance();
String target = "Apache";
EditDistanceFrom<Integer> editDistanceFrom =
new EditDistanceFrom<Integer>(editDistance, target);
String mostSimilar = null;
Integer shortestDistance = null;
for (String test : new String[] { "Appaloosa", "a patchy", "apple" }) {
Integer distance = editDistanceFrom.apply(test);
if (shortestDistance == null || distance < shortestDistance) {
shortestDistance = distance;
mostSimilar = test;
}
}
System.out.println("The string most similar to \"" + target + "\" "
+ "is \"" + mostSimilar + "\" because "
+ "its distance is only " + shortestDistance + ".");
| Modifier and Type | Field and Description |
|---|---|
| private final EditDistance | editDistance
Edit distance. |
| private final CharSequence | left
Left parameter used in distance function. |
| Access | Constructor and Description |
|---|---|
| public | EditDistanceFrom(final EditDistance<R>
This may not be null. editDistance, final CharSequence This may be null here,
but the EditDistance#compare(CharSequence left, CharSequence right)
implementation may not accept nulls. left)Constructs the edit distance implementation and the "left" string. |
| Modifier and Type | Method and Description |
|---|---|
| public R | Returns: The similarity score between two CharSequences.the second CharSequence. right)Compares "left" field against the "right" parameter using the "edit distance" implementation. |
| public EditDistance | |
| public CharSequence |
| editDistance | back to summary |
|---|---|
| private final EditDistance<R> editDistance Edit distance. | |
| left | back to summary |
|---|---|
| private final CharSequence left Left parameter used in distance function. | |
| EditDistanceFrom | back to summary |
|---|---|
| public EditDistanceFrom(final EditDistance<R> editDistance, final CharSequence left) Constructs the edit distance implementation and the "left" string.
| |
| apply | back to summary |
|---|---|
| public R apply(final CharSequence right) Compares "left" field against the "right" parameter using the "edit distance" implementation.
| |
| getEditDistance | back to summary |
|---|---|
| public EditDistance Gets the edit distance.
| |
| getLeft | back to summary |
|---|---|
| public CharSequence getLeft() Gets the left parameter.
| |