View Javadoc
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  import org.apache.commons.lang3.Validate;
20  
21  /**
22   * This stores a {@link SimilarityScore} implementation and a {@link CharSequence} "left" string.
23   * The {@link #apply(CharSequence right)} method accepts the "right" string and invokes the
24   * comparison function for the pair of strings.
25   *
26   * <p>
27   * The following is an example which finds the most similar string:
28   * </p>
29   * <pre>
30   * SimilarityScore&lt;Integer&gt; similarityScore = new LevenshteinDistance();
31   * String target = "Apache";
32   * SimilarityScoreFrom&lt;Integer&gt; similarityScoreFrom =
33   *     new SimilarityScoreFrom&lt;Integer&gt;(similarityScore, target);
34   * String mostSimilar = null;
35   * Integer shortestDistance = null;
36   *
37   * for (String test : new String[] { "Appaloosa", "a patchy", "apple" }) {
38   *     Integer distance = similarityScoreFrom.apply(test);
39   *     if (shortestDistance == null || distance &lt; shortestDistance) {
40   *         shortestDistance = distance;
41   *         mostSimilar = test;
42   *     }
43   * }
44   *
45   * System.out.println("The string most similar to \"" + target + "\" "
46   *     + "is \"" + mostSimilar + "\" because "
47   *     + "its distance is only " + shortestDistance + ".");
48   * </pre>
49   *
50   * @param <R> This is the type of similarity score used by the SimilarityScore function.
51   * @since 1.0
52   */
53  public class SimilarityScoreFrom<R> {
54  
55      /**
56       * Similarity score.
57       */
58      private final SimilarityScore<R> similarityScore;
59  
60      /**
61       * Left parameter used in distance function.
62       */
63      private final CharSequence left;
64  
65      /**
66       * This accepts the similarity score implementation and the "left" string.
67       *
68       * @param similarityScore This may not be null.
69       * @param left This may be null here,
70       *             but the SimilarityScore#compare(CharSequence left, CharSequence right)
71       *             implementation may not accept nulls.
72       */
73      public SimilarityScoreFrom(final SimilarityScore<R> similarityScore, final CharSequence left) {
74          Validate.isTrue(similarityScore != null, "The edit distance may not be null.");
75  
76          this.similarityScore = similarityScore;
77          this.left = left;
78      }
79  
80      /**
81       * This compares "left" field against the "right" parameter
82       * using the "similarity score" implementation.
83       *
84       * @param right the second CharSequence
85       * @return The similarity score between two CharSequences
86       */
87      public R apply(final CharSequence right) {
88          return similarityScore.apply(left, right);
89      }
90  
91      /**
92       * Gets the left parameter.
93       *
94       * @return The left parameter
95       */
96      public CharSequence getLeft() {
97          return left;
98      }
99  
100     /**
101      * Gets the edit distance.
102      *
103      * @return The edit distance
104      */
105     public SimilarityScore<R> getSimilarityScore() {
106         return similarityScore;
107     }
108 
109 }