-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimilarityCalculatorManhattanImpl.java
More file actions
32 lines (27 loc) · 1.15 KB
/
SimilarityCalculatorManhattanImpl.java
File metadata and controls
32 lines (27 loc) · 1.15 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
31
32
package ie.atu.sw;
/**
* Implementation of the SimilarityCalculator interface for calculate
* correlation between two vectors using Manhattan algorithm
*/
public class SimilarityCalculatorManhattanImpl implements SimilarityCalculator {
/**
* Calculates the Manhattan distance between two vector
*
* @param vectorA first word vector
* @param vectorB second word vector
* @return Manhattan distance
* @throws IllegalArgumentException occurs if vector have different length
*/
@Override
public double calculateSimilarity(double[] vectorA, double[] vectorB) {
// Big O = O(n) n. of words in cycle for
if (vectorA.length != vectorB.length) {
throw new IllegalArgumentException("Ops .. Vector must have same lenght!");
}
double manhattanDistance = 0.0;
for (int i = 0; i < vectorA.length; i++) { // Big O = O(n)
manhattanDistance += Math.abs(vectorA[i] - vectorB[i]); // Big O = O(1)
}
return 1.0 / (1.0 + manhattanDistance); //inverted because this calculation has best score negative value
}
}