1 package net.obsearch.example.doc;
2
3 import java.util.Arrays;
4
5 import net.obsearch.exception.OBException;
6 import net.obsearch.ob.OBFloat;
7
8
9
10
11
12
13
14 public class OBTanimoto extends AbstractDocument {
15
16 public OBTanimoto(){
17
18 }
19 public OBTanimoto(String data) throws OBException {
20 super(data);
21 }
22
23 public float distance(OBFloat object) throws OBException {
24
25 OBTanimoto other = (OBTanimoto)object;
26
27
28
29 int dot = 0;
30 int i1 = 0;
31 int i2 = 0;
32 final int max1 = super.ids.length;
33 final int max2 = other.ids.length;
34 while(i1 < max1 && i2 < max2){
35 if(ids[i1] < other.ids[i2]){
36
37 dot += counts[i1];
38 i1++;
39 }else if(ids[i1] > other.ids[i2]){
40
41 dot += other.counts[i2];
42 i2++;
43 }else{
44
45 dot += Math.abs(counts[i1] - other.counts[i2]);
46 i1++;
47 i2++;
48 }
49 }
50 while(i1 < max1){
51 dot += counts[i1];
52
53 i1++;
54 }
55 while(i2 < max2){
56 dot += other.counts[i2];
57
58 i2++;
59 }
60 assert ((float)dot) == dot;
61
62 return (float)dot;
63 }
64
65 @Override
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 public boolean equals(Object o){
102 OBTanimoto other = (OBTanimoto)o;
103 return Arrays.equals(ids, other.ids) && Arrays.equals(counts, other.counts);
104 }
105
106 private long euclideanNormSquared(){
107 long res = 0;
108 for(long d : counts){
109 res += d * d;
110 }
111 return res;
112 }
113
114 }