1 #ifndef ALGO_TEXT___VECTOR_SCORE__HPP
2 #define ALGO_TEXT___VECTOR_SCORE__HPP
45 template <
class iterator1,
class iterator2>
46 float Cosine(iterator1 iter1, iterator1 end1,
47 iterator2 iter2, iterator2 end2)
53 for ( ; iter1 != end1 && iter2 != end2; ) {
54 if (iter1->first == iter2->first) {
55 cosine += float(iter1->second) * float(iter2->second);
56 len_a += iter1->second * iter1->second;
57 len_b += iter2->second * iter2->second;
61 if (iter1->first < iter2->first) {
62 len_a += iter1->second * iter1->second;
65 len_b += iter2->second * iter2->second;
71 for ( ; iter1 != end1; ++iter1) {
72 len_a += iter1->second * iter1->second;
75 for ( ; iter2 != end2; ++iter2) {
76 len_b += iter2->second * iter2->second;
79 cosine /= sqrt(len_a * len_b);
88 template <
class iterator1,
class iterator2>
90 iterator2 iter2, iterator2 end2,
97 for ( ; iter1 != end1 && iter2 != end2; ) {
98 if (iter1->first == iter2->first) {
99 mink += float(iter1->second) * float(iter2->second);
100 len_a += pow(iter1->second, power);
101 len_b += pow(iter2->second, power);
105 if (iter1->first < iter2->first) {
106 len_a += pow(iter1->second, power);
109 len_b += pow(iter2->second, power);
115 for ( ; iter1 != end1; ++iter1) {
116 len_a += pow(iter1->second, power);
119 for ( ; iter2 != end2; ++iter2) {
120 len_b += pow(iter2->second, power);
123 mink /= pow(len_a * len_b, 1.0f /
float(power));
131 template <
class iterator1,
class iterator2>
132 float Dot(iterator1 iter1, iterator1 end1,
133 iterator2 iter2, iterator2 end2)
137 for ( ; iter1 != end1 && iter2 != end2; ) {
138 if (iter1->first == iter2->first) {
139 dot += float(iter1->second) * float(iter2->second);
143 if (iter1->first < iter2->first) {
159 template <
class iterator1,
class iterator2>
161 iterator2 iter2, iterator2 end2)
164 for ( ; iter1 != end1 && iter2 != end2; ) {
165 if (iter1->first == iter2->first) {
166 float diff = float(iter1->second) - iter2->second;
171 if (iter1->first < iter2->first) {
172 dist += iter1->second * iter1->second;
175 dist += iter2->second * iter2->second;
181 for ( ; iter1 != end1; ++iter1) {
182 dist += iter1->second * iter1->second;
185 for ( ; iter2 != end2; ++iter2) {
186 dist += iter2->second * iter2->second;
197 template <
class iterator1,
class iterator2>
199 iterator2 iter2, iterator2 end2,
200 float* dot_in,
float* dist_in)
204 for ( ; iter1 != end1 && iter2 != end2; ) {
205 if (iter1->first == iter2->first) {
206 float diff = iter1->second - iter2->second;
208 dot += iter1->second * iter2->second;
213 if (iter1->first < iter2->first) {
214 dist += iter1->second * iter1->second;
217 dist += iter2->second * iter2->second;
223 for ( ; iter1 != end1; ++iter1) {
224 dist += iter1->second * iter1->second;
227 for ( ; iter2 != end2; ++iter2) {
228 dist += iter2->second * iter2->second;
236 *dist_in = sqrt(dist);
247 template <
class iterator1,
class iterator2>
248 float Jaccard(iterator1 iter1, iterator1 end1,
249 iterator2 iter2, iterator2 end2)
255 for ( ; iter1 != end1 && iter2 != end2; ) {
256 if (iter1->first == iter2->first) {
257 float v1 = float(iter1->second);
258 float v2 = float(iter2->second);
267 if (iter1->first < iter2->first) {
268 score_a += iter1->second * iter1->second;
271 score_b += iter2->second * iter2->second;
277 for ( ; iter1 != end1; ++iter1) {
278 score_a += iter1->second * iter1->second;
281 for ( ; iter2 != end2; ++iter2) {
282 score_b += iter2->second * iter2->second;
285 return (dot / (score_a + score_b - dot));
293 template <
class iterator1,
class iterator2>
294 float Dice(iterator1 iter1, iterator1 end1,
295 iterator2 iter2, iterator2 end2)
301 for ( ; iter1 != end1 && iter2 != end2; ) {
302 if (iter1->first == iter2->first) {
303 float v1 = float(iter1->second);
304 float v2 = float(iter2->second);
307 score_a += iter1->second;
308 score_b += iter2->second;
313 if (iter1->first < iter2->first) {
314 score_a += iter1->second;
317 score_b += iter2->second;
323 for ( ; iter1 != end1; ++iter1) {
324 score_a += iter1->second;
327 for ( ; iter2 != end2; ++iter2) {
328 score_b += iter2->second;
331 return (dot / (score_a + score_b));
339 template <
class iterator1,
class iterator2>
340 float Overlap(iterator1 iter1, iterator1 end1,
341 iterator2 iter2, iterator2 end2)
346 for ( ; iter1 != end1 && iter2 != end2; ) {
347 if (iter1->first == iter2->first) {
348 dot += float(iter1->second) * float(iter2->second);
349 sum_a += iter1->second;
350 sum_b += iter2->second;
354 if (iter1->first < iter2->first) {
355 sum_a += iter1->second;
358 sum_b += iter2->second;
364 for ( ; iter1 != end1; ++iter1) {
365 sum_a += iter1->second;
368 for ( ; iter2 != end2; ++iter2) {
369 sum_b += iter2->second;
372 return dot /
min(sum_a, sum_b);
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define END_NCBI_SCOPE
End previously defined NCBI scope.
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
float Dice(iterator1 iter1, iterator1 end1, iterator2 iter2, iterator2 end2)
Dice coefficient.
float Distance(iterator1 iter1, iterator1 end1, iterator2 iter2, iterator2 end2)
Euclidean distance measure.
float Dot(iterator1 iter1, iterator1 end1, iterator2 iter2, iterator2 end2)
Dot-product similarity.
float Minkowski(iterator1 iter1, iterator1 end1, iterator2 iter2, iterator2 end2, size_t power)
Minkowski similarity measure.
float Jaccard(iterator1 iter1, iterator1 end1, iterator2 iter2, iterator2 end2)
Jaccard similarity.
float Cosine(iterator1 iter1, iterator1 end1, iterator2 iter2, iterator2 end2)
Cosine similarity measure.
float Overlap(iterator1 iter1, iterator1 end1, iterator2 iter2, iterator2 end2)
Overlap measure.
void DotAndDistance(iterator1 iter1, iterator1 end1, iterator2 iter2, iterator2 end2, float *dot_in, float *dist_in)
Dot and distance in one step.