Class CosineSimilarity
java.lang.Object
org.apache.commons.text.similarity.CosineSimilarity
Measures the Cosine similarity of two vectors of an inner product space and
compares the angle between them.
For further explanation about the Cosine Similarity, refer to http://en.wikipedia.org/wiki/Cosine_similarity.
- Since:
- 1.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncosineSimilarity
(Map<CharSequence, Integer> leftVector, Map<CharSequence, Integer> rightVector) Calculates the cosine similarity for two given vectors.private double
dot
(Map<CharSequence, Integer> leftVector, Map<CharSequence, Integer> rightVector, Set<CharSequence> intersection) Computes the dot product of two vectors.private Set<CharSequence>
getIntersection
(Map<CharSequence, Integer> leftVector, Map<CharSequence, Integer> rightVector) Returns a set with strings common to the two given maps.
-
Constructor Details
-
CosineSimilarity
public CosineSimilarity()
-
-
Method Details
-
cosineSimilarity
public Double cosineSimilarity(Map<CharSequence, Integer> leftVector, Map<CharSequence, Integer> rightVector) Calculates the cosine similarity for two given vectors.- Parameters:
leftVector
- left vectorrightVector
- right vector- Returns:
- cosine similarity between the two vectors
-
dot
private double dot(Map<CharSequence, Integer> leftVector, Map<CharSequence, Integer> rightVector, Set<CharSequence> intersection) Computes the dot product of two vectors. It ignores remaining elements. It means that if a vector is longer than other, then a smaller part of it will be used to compute the dot product.- Parameters:
leftVector
- left vectorrightVector
- right vectorintersection
- common elements- Returns:
- The dot product
-
getIntersection
private Set<CharSequence> getIntersection(Map<CharSequence, Integer> leftVector, Map<CharSequence, Integer> rightVector) Returns a set with strings common to the two given maps.- Parameters:
leftVector
- left vector maprightVector
- right vector map- Returns:
- common strings
-