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.Map;
21  import java.util.Set;
22  
23  /**
24   * Measures the Cosine similarity of two vectors of an inner product space and
25   * compares the angle between them.
26   *
27   * <p>
28   * For further explanation about the Cosine Similarity, refer to
29   * http://en.wikipedia.org/wiki/Cosine_similarity.
30   * </p>
31   *
32   * @since 1.0
33   */
34  public class CosineSimilarity {
35  
36      /**
37       * Calculates the cosine similarity for two given vectors.
38       *
39       * @param leftVector left vector
40       * @param rightVector right vector
41       * @return cosine similarity between the two vectors
42       */
43      public Double cosineSimilarity(final Map<CharSequence, Integer> leftVector, final Map<CharSequence, Integer> rightVector) {
44          if (leftVector == null || rightVector == null) {
45              throw new IllegalArgumentException("Vectors must not be null");
46          }
47  
48          final Set<CharSequence> intersection = getIntersection(leftVector, rightVector);
49  
50          final double dotProduct = dot(leftVector, rightVector, intersection);
51          double d1 = 0.0d;
52          for (final Integer value : leftVector.values()) {
53              d1 += Math.pow(value, 2);
54          }
55          double d2 = 0.0d;
56          for (final Integer value : rightVector.values()) {
57              d2 += Math.pow(value, 2);
58          }
59          double cosineSimilarity;
60          if (d1 <= 0.0 || d2 <= 0.0) {
61              cosineSimilarity = 0.0;
62          } else {
63              cosineSimilarity = (double) (dotProduct / (double) (Math.sqrt(d1) * Math.sqrt(d2)));
64          }
65          return cosineSimilarity;
66      }
67  
68      /**
69       * Returns a set with strings common to the two given maps.
70       *
71       * @param leftVector left vector map
72       * @param rightVector right vector map
73       * @return common strings
74       */
75      private Set<CharSequence> getIntersection(final Map<CharSequence, Integer> leftVector,
76              final Map<CharSequence, Integer> rightVector) {
77          final Set<CharSequence> intersection = new HashSet<>(leftVector.keySet());
78          intersection.retainAll(rightVector.keySet());
79          return intersection;
80      }
81  
82      /**
83       * Computes the dot product of two vectors. It ignores remaining elements. It means
84       * that if a vector is longer than other, then a smaller part of it will be used to compute
85       * the dot product.
86       *
87       * @param leftVector left vector
88       * @param rightVector right vector
89       * @param intersection common elements
90       * @return the dot product
91       */
92      private double dot(final Map<CharSequence, Integer> leftVector, final Map<CharSequence, Integer> rightVector,
93              final Set<CharSequence> intersection) {
94          long dotProduct = 0;
95          for (final CharSequence key : intersection) {
96              dotProduct += leftVector.get(key) * rightVector.get(key);
97          }
98          return dotProduct;
99      }
100 
101 }