CompareDistance.java
1.32 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.objecteye.utils;
import org.springframework.stereotype.Component;
@Component
public class CompareDistance {
/**
* 求两个double[]数组的欧式距离
*
* @param vector1 数组1
* @param distance 归一化之后的距离
* @param vector2 数组2
* @param distance1 归一化之后的距离
* @return 距离
*/
public double simDistance(double[] vector1, double distance, double[] vector2, double distance1) {
double score = 0;
for (int i = 0; i < vector1.length; i++) {
score += vector1[i] * vector2[i];
}
score = score / (distance * distance1);
return score < 0 ? 0 : score;
}
/**
* 数组归一化之后的距离
*
* @param vector1 数组
* @return 归一化之后的距离
*/
public double getDistance(double[] vector1) {
double distance = 0;
double maxa = 0;
for (int j = 0; j < vector1.length; j++) {
if (vector1[j] > maxa) {
maxa = vector1[j];
}
}
for (int j = 0; j < vector1.length; j++) {
vector1[j] = (vector1[j] / maxa);
distance += vector1[j] * vector1[j];
// distance += (vector1[j] / maxa) * (vector1[j] / maxa);
}
return Math.sqrt(distance);
}
}