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    *      https://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       * The singleton instance.
36       */
37      static final JaccardSimilarity INSTANCE = new JaccardSimilarity();
38  
39      /**
40       * Creates a new instance.
41       */
42      public JaccardSimilarity() {
43          // empty
44      }
45  
46      /**
47       * Computes the Jaccard Similarity of two set character sequence passed as input.
48       *
49       * @param left  first input sequence.
50       * @param right second input sequence.
51       * @return The Jaccard Similarity of two set character sequence passed as input.
52       * @throws IllegalArgumentException if either String input {@code null}.
53       */
54      @Override
55      public Double apply(final CharSequence left, final CharSequence right) {
56          return apply(SimilarityInput.input(left), SimilarityInput.input(right));
57      }
58  
59      /**
60       * Computes the Jaccard Similarity of two character sequences passed as input. Does the calculation by identifying the union (characters in at least one of
61       * the two sets) of the two sets and intersection (characters which are present in set one which are present in set two)
62       *
63       * @param <E>   The type of similarity score unit.
64       * @param left  first input sequence.
65       * @param right second input sequence.
66       * @return The Jaccard Similarity of two set character sequence passed as input.
67       * @since 1.13.0
68       */
69      public <E> Double apply(final SimilarityInput<E> left, final SimilarityInput<E> right) {
70          if (left == null || right == null) {
71              throw new IllegalArgumentException("Input cannot be null");
72          }
73          final int leftLength = left.length();
74          final int rightLength = right.length();
75          if (leftLength == 0 && rightLength == 0) {
76              return 1d;
77          }
78          if (leftLength == 0 || rightLength == 0) {
79              return 0d;
80          }
81          final Set<E> leftSet = new HashSet<>();
82          for (int i = 0; i < leftLength; i++) {
83              leftSet.add(left.at(i));
84          }
85          final Set<E> rightSet = new HashSet<>();
86          for (int i = 0; i < rightLength; i++) {
87              rightSet.add(right.at(i));
88          }
89          final Set<E> unionSet = new HashSet<>(leftSet);
90          unionSet.addAll(rightSet);
91          final int intersectionSize = leftSet.size() + rightSet.size() - unionSet.size();
92          return 1.0d * intersectionSize / unionSet.size();
93      }
94  }