Class EditDistanceFrom<R>

  • Type Parameters:
    R - This is the type of similarity score used by the EditDistance function.

    public class EditDistanceFrom<R>
    extends java.lang.Object

    This stores a 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 + ".");
     
    Since:
    1.0
    • Constructor Summary

      Constructors 
      Constructor Description
      EditDistanceFrom​(EditDistance<R> editDistance, java.lang.CharSequence left)
      This accepts the edit distance implementation and the "left" string.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      R apply​(java.lang.CharSequence right)
      This compares "left" field against the "right" parameter using the "edit distance" implementation.
      EditDistance<R> getEditDistance()
      Gets the edit distance.
      java.lang.CharSequence getLeft()
      Gets the left parameter.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • EditDistanceFrom

        public EditDistanceFrom​(EditDistance<R> editDistance,
                                java.lang.CharSequence left)

        This accepts the edit distance implementation and the "left" string.

        Parameters:
        editDistance - This may not be null.
        left - This may be null here, but the EditDistance#compare(CharSequence left, CharSequence right) implementation may not accept nulls.
    • Method Detail

      • apply

        public R apply​(java.lang.CharSequence right)

        This compares "left" field against the "right" parameter using the "edit distance" implementation.

        Parameters:
        right - the second CharSequence
        Returns:
        The similarity score between two CharSequences
      • getLeft

        public java.lang.CharSequence getLeft()
        Gets the left parameter.
        Returns:
        The left parameter
      • getEditDistance

        public EditDistance<R> getEditDistance()
        Gets the edit distance.
        Returns:
        The edit distance