Top Description Fields Constructors Methods
org.apache.commons.text.similarity

public Class CosineSimilarity

extends Object
Class Inheritance
Imports
java.util.HashSet, .Map, .Set

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 https://en.wikipedia.org/wiki/Cosine_similarity.

Instances of this class are immutable and are safe for use by multiple concurrent threads.

Since
1.0

Field Summary

Modifier and TypeField and Description
pack-priv static final CosineSimilarity
INSTANCE

The singleton instance.

Constructor Summary

AccessConstructor and Description
public
CosineSimilarity()

Construct a new instance.

Method Summary

Modifier and TypeMethod and Description
public Double

Returns:

cosine similarity between the two vectors.
cosineSimilarity
(final Map<CharSequence, Integer>
left vector.
leftVector
,
final Map<CharSequence, Integer>
right vector.
rightVector
)

Calculates the cosine similarity for two given vectors.

private double

Returns:

The dot product.
dot
(final Map<CharSequence, Integer>
left vector.
leftVector
,
final Map<CharSequence, Integer>
right vector.
rightVector
,
final Set<CharSequence>
common elements.
intersection
)

Computes the dot product of two vectors.

private Set<CharSequence>

Returns:

common strings.
getIntersection
(final Map<CharSequence, Integer>
left vector map.
leftVector
,
final Map<CharSequence, Integer>
right vector map.
rightVector
)

Returns a set with keys common to the two given maps.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INSTANCEback to summary
pack-priv static final CosineSimilarity INSTANCE

The singleton instance.

Constructor Detail

CosineSimilarityback to summary
public CosineSimilarity()

Construct a new instance.

Method Detail

cosineSimilarityback to summary
public Double cosineSimilarity(final Map<CharSequence, Integer> leftVector, final Map<CharSequence, Integer> rightVector)

Calculates the cosine similarity for two given vectors.

Parameters
leftVector:Map<CharSequence, Integer>

left vector.

rightVector:Map<CharSequence, Integer>

right vector.

Returns:Double

cosine similarity between the two vectors.

dotback to summary
private double dot(final Map<CharSequence, Integer> leftVector, final Map<CharSequence, Integer> rightVector, final 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:Map<CharSequence, Integer>

left vector.

rightVector:Map<CharSequence, Integer>

right vector.

intersection:Set<CharSequence>

common elements.

Returns:double

The dot product.

getIntersectionback to summary
private Set<CharSequence> getIntersection(final Map<CharSequence, Integer> leftVector, final Map<CharSequence, Integer> rightVector)

Returns a set with keys common to the two given maps.

Parameters
leftVector:Map<CharSequence, Integer>

left vector map.

rightVector:Map<CharSequence, Integer>

right vector map.

Returns:Set<CharSequence>

common strings.