SimilarityScoreFrom.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.text.similarity;

  18. import org.apache.commons.lang3.Validate;

  19. /**
  20.  * This stores a {@link SimilarityScore} implementation and a {@link CharSequence} "left" string.
  21.  * The {@link #apply(CharSequence right)} method accepts the "right" string and invokes the
  22.  * comparison function for the pair of strings.
  23.  *
  24.  * <p>
  25.  * The following is an example which finds the most similar string:
  26.  * </p>
  27.  * <pre>
  28.  * SimilarityScore&lt;Integer&gt; similarityScore = new LevenshteinDistance();
  29.  * String target = "Apache";
  30.  * SimilarityScoreFrom&lt;Integer&gt; similarityScoreFrom =
  31.  *     new SimilarityScoreFrom&lt;Integer&gt;(similarityScore, target);
  32.  * String mostSimilar = null;
  33.  * Integer shortestDistance = null;
  34.  *
  35.  * for (String test : new String[] { "Appaloosa", "a patchy", "apple" }) {
  36.  *     Integer distance = similarityScoreFrom.apply(test);
  37.  *     if (shortestDistance == null || distance &lt; shortestDistance) {
  38.  *         shortestDistance = distance;
  39.  *         mostSimilar = test;
  40.  *     }
  41.  * }
  42.  *
  43.  * System.out.println("The string most similar to \"" + target + "\" "
  44.  *     + "is \"" + mostSimilar + "\" because "
  45.  *     + "its distance is only " + shortestDistance + ".");
  46.  * </pre>
  47.  *
  48.  * @param <R> This is the type of similarity score used by the SimilarityScore function.
  49.  * @since 1.0
  50.  */
  51. public class SimilarityScoreFrom<R> {

  52.     /**
  53.      * Similarity score.
  54.      */
  55.     private final SimilarityScore<R> similarityScore;

  56.     /**
  57.      * Left parameter used in distance function.
  58.      */
  59.     private final CharSequence left;

  60.     /**
  61.      * This accepts the similarity score implementation and the "left" string.
  62.      *
  63.      * @param similarityScore This may not be null.
  64.      * @param left This may be null here,
  65.      *             but the SimilarityScore#compare(CharSequence left, CharSequence right)
  66.      *             implementation may not accept nulls.
  67.      */
  68.     public SimilarityScoreFrom(final SimilarityScore<R> similarityScore, final CharSequence left) {
  69.         Validate.isTrue(similarityScore != null, "The edit distance may not be null.");

  70.         this.similarityScore = similarityScore;
  71.         this.left = left;
  72.     }

  73.     /**
  74.      * This compares "left" field against the "right" parameter
  75.      * using the "similarity score" implementation.
  76.      *
  77.      * @param right the second CharSequence
  78.      * @return The similarity score between two CharSequences
  79.      */
  80.     public R apply(final CharSequence right) {
  81.         return similarityScore.apply(left, right);
  82.     }

  83.     /**
  84.      * Gets the left parameter.
  85.      *
  86.      * @return The left parameter
  87.      */
  88.     public CharSequence getLeft() {
  89.         return left;
  90.     }

  91.     /**
  92.      * Gets the edit distance.
  93.      *
  94.      * @return The edit distance
  95.      */
  96.     public SimilarityScore<R> getSimilarityScore() {
  97.         return similarityScore;
  98.     }

  99. }