-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimilarityCalculatorCosineImpl.java
More file actions
30 lines (26 loc) · 1.12 KB
/
SimilarityCalculatorCosineImpl.java
File metadata and controls
30 lines (26 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package ie.atu.sw;
/**
* Implementation of the SimilarityCalculator interface for calculate
* correlation between two vectors using Cosine Distance
*/
public class SimilarityCalculatorCosineImpl implements SimilarityCalculator {
/**
* Calculates the Cosine Distance between two vector
*
* @param vectorA first word vector
* @param vectorB second word vector
* @return Cosine distance ( between 1 (identical and -1 weaker similarity)
*/
@Override
public double calculateSimilarity(double[] vectorA, double[] vectorB) { // Big O = O(n)
double dotProduct = 0.0; // Big O = O(1)
double squaredA = 0.0; // Big O = O(1)
double squaredB = 0.0; // Big O = O(1)
for (int i = 0; i < vectorA.length; i++) { // Big O = O(n)
dotProduct += vectorA[i] * vectorB[i]; // Big O = O(1)
squaredA += vectorA[i] * vectorA[i]; // Big O = O(1)
squaredB += vectorB[i] * vectorB[i]; // Big O = O(1)
}
return dotProduct / (Math.sqrt(squaredA) * Math.sqrt(squaredB)); // Big O = O(1)
}
}