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

  53.     /**
  54.      * Similarity score.
  55.      */
  56.     private final SimilarityScore<R> similarityScore;
  57.     /**
  58.      * Left parameter used in distance function.
  59.      */
  60.     private final CharSequence left;

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

  73.         this.similarityScore = similarityScore;
  74.         this.left = left;
  75.     }

  76.     /**
  77.      * <p>
  78.      * This compares "left" field against the "right" parameter
  79.      * using the "similarity score" implementation.
  80.      * </p>
  81.      *
  82.      * @param right the second CharSequence
  83.      * @return the similarity score between two CharSequences
  84.      */
  85.     public R apply(final CharSequence right) {
  86.         return similarityScore.apply(left, right);
  87.     }

  88.     /**
  89.      * Gets the left parameter.
  90.      *
  91.      * @return the left parameter
  92.      */
  93.     public CharSequence getLeft() {
  94.         return left;
  95.     }

  96.     /**
  97.      * Gets the edit distance.
  98.      *
  99.      * @return the edit distance
  100.      */
  101.     public SimilarityScore<R> getSimilarityScore() {
  102.         return similarityScore;
  103.     }

  104. }