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 java.util.HashSet;
20  import java.util.Set;
21  
22  /**
23   * Measures the Jaccard similarity (aka Jaccard index) of two sets of character sequence. Jaccard similarity is the size of the intersection divided by the size
24   * of the union of the two sets.
25   *
26   * <p>
27   * For further explanation about Jaccard Similarity, refer https://en.wikipedia.org/wiki/Jaccard_index
28   * </p>
29   *
30   * @since 1.0
31   */
32  public class JaccardSimilarity implements SimilarityScore<Double> {
33  
34      /**
35       * Singleton instance.
36       */
37      static final JaccardSimilarity INSTANCE = new JaccardSimilarity();
38  
39      /**
40       * Computes the Jaccard Similarity of two set character sequence passed as input.
41       *
42       * @param left  first input sequence.
43       * @param right second input sequence.
44       * @return index.
45       * @throws IllegalArgumentException if either String input {@code null}.
46       */
47      @Override
48      public Double apply(final CharSequence left, final CharSequence right) {
49          return apply(SimilarityInput.input(left), SimilarityInput.input(right));
50      }
51  
52      /**
53       * Computes the Jaccard Similarity of two character sequences passed as input. Does the calculation by identifying the union (characters in at least one of
54       * the two sets) of the two sets and intersection (characters which are present in set one which are present in set two)
55       *
56       * @param <E>   The type of similarity score unit.
57       * @param left  first input sequence.
58       * @param right second input sequence.
59       * @return index.
60       * @since 1.13.0
61       */
62      public <E> Double apply(final SimilarityInput<E> left, final SimilarityInput<E> right) {
63          if (left == null || right == null) {
64              throw new IllegalArgumentException("Input cannot be null");
65          }
66          final int leftLength = left.length();
67          final int rightLength = right.length();
68          if (leftLength == 0 && rightLength == 0) {
69              return 1d;
70          }
71          if (leftLength == 0 || rightLength == 0) {
72              return 0d;
73          }
74          final Set<E> leftSet = new HashSet<>();
75          for (int i = 0; i < leftLength; i++) {
76              leftSet.add(left.at(i));
77          }
78          final Set<E> rightSet = new HashSet<>();
79          for (int i = 0; i < rightLength; i++) {
80              rightSet.add(right.at(i));
81          }
82          final Set<E> unionSet = new HashSet<>(leftSet);
83          unionSet.addAll(rightSet);
84          final int intersectionSize = leftSet.size() + rightSet.size() - unionSet.size();
85          return 1.0d * intersectionSize / unionSet.size();
86      }
87  }