123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- #include <cfloat>
- #include "TemplatedVocabulary.h"
- #include "BowVector.h"
- using namespace DBoW2;
- const double GeneralScoring::LOG_EPS = log(DBL_EPSILON);
- double L1Scoring::score(const BowVector &v1, const BowVector &v2) const
- {
- BowVector::const_iterator v1_it, v2_it;
- const BowVector::const_iterator v1_end = v1.end();
- const BowVector::const_iterator v2_end = v2.end();
-
- v1_it = v1.begin();
- v2_it = v2.begin();
-
- double score = 0;
-
- while(v1_it != v1_end && v2_it != v2_end)
- {
- const WordValue& vi = v1_it->second;
- const WordValue& wi = v2_it->second;
-
- if(v1_it->first == v2_it->first)
- {
- score += fabs(vi - wi) - fabs(vi) - fabs(wi);
-
-
- ++v1_it;
- ++v2_it;
- }
- else if(v1_it->first < v2_it->first)
- {
-
- v1_it = v1.lower_bound(v2_it->first);
-
- }
- else
- {
-
- v2_it = v2.lower_bound(v1_it->first);
-
- }
- }
-
-
-
-
-
- score = -score/2.0;
- return score;
- }
- double L2Scoring::score(const BowVector &v1, const BowVector &v2) const
- {
- BowVector::const_iterator v1_it, v2_it;
- const BowVector::const_iterator v1_end = v1.end();
- const BowVector::const_iterator v2_end = v2.end();
-
- v1_it = v1.begin();
- v2_it = v2.begin();
-
- double score = 0;
-
- while(v1_it != v1_end && v2_it != v2_end)
- {
- const WordValue& vi = v1_it->second;
- const WordValue& wi = v2_it->second;
-
- if(v1_it->first == v2_it->first)
- {
- score += vi * wi;
-
-
- ++v1_it;
- ++v2_it;
- }
- else if(v1_it->first < v2_it->first)
- {
-
- v1_it = v1.lower_bound(v2_it->first);
-
- }
- else
- {
-
- v2_it = v2.lower_bound(v1_it->first);
-
- }
- }
-
-
-
-
- if(score >= 1)
- score = 1.0;
- else
- score = 1.0 - sqrt(1.0 - score);
- return score;
- }
- double ChiSquareScoring::score(const BowVector &v1, const BowVector &v2)
- const
- {
- BowVector::const_iterator v1_it, v2_it;
- const BowVector::const_iterator v1_end = v1.end();
- const BowVector::const_iterator v2_end = v2.end();
-
- v1_it = v1.begin();
- v2_it = v2.begin();
-
- double score = 0;
-
-
-
- while(v1_it != v1_end && v2_it != v2_end)
- {
- const WordValue& vi = v1_it->second;
- const WordValue& wi = v2_it->second;
-
- if(v1_it->first == v2_it->first)
- {
-
-
- if(vi + wi != 0.0) score += vi * wi / (vi + wi);
-
-
- ++v1_it;
- ++v2_it;
- }
- else if(v1_it->first < v2_it->first)
- {
-
- v1_it = v1.lower_bound(v2_it->first);
- }
- else
- {
-
- v2_it = v2.lower_bound(v1_it->first);
- }
- }
-
-
- score = 2. * score;
- return score;
- }
- double KLScoring::score(const BowVector &v1, const BowVector &v2) const
- {
- BowVector::const_iterator v1_it, v2_it;
- const BowVector::const_iterator v1_end = v1.end();
- const BowVector::const_iterator v2_end = v2.end();
-
- v1_it = v1.begin();
- v2_it = v2.begin();
-
- double score = 0;
-
-
-
- while(v1_it != v1_end && v2_it != v2_end)
- {
- const WordValue& vi = v1_it->second;
- const WordValue& wi = v2_it->second;
-
- if(v1_it->first == v2_it->first)
- {
- if(vi != 0 && wi != 0) score += vi * log(vi/wi);
-
-
- ++v1_it;
- ++v2_it;
- }
- else if(v1_it->first < v2_it->first)
- {
-
- score += vi * (log(vi) - LOG_EPS);
- ++v1_it;
- }
- else
- {
-
- v2_it = v2.lower_bound(v1_it->first);
-
- }
- }
-
-
- for(; v1_it != v1_end; ++v1_it)
- if(v1_it->second != 0)
- score += v1_it->second * (log(v1_it->second) - LOG_EPS);
-
- return score;
- }
- double BhattacharyyaScoring::score(const BowVector &v1,
- const BowVector &v2) const
- {
- BowVector::const_iterator v1_it, v2_it;
- const BowVector::const_iterator v1_end = v1.end();
- const BowVector::const_iterator v2_end = v2.end();
-
- v1_it = v1.begin();
- v2_it = v2.begin();
-
- double score = 0;
-
- while(v1_it != v1_end && v2_it != v2_end)
- {
- const WordValue& vi = v1_it->second;
- const WordValue& wi = v2_it->second;
-
- if(v1_it->first == v2_it->first)
- {
- score += sqrt(vi * wi);
-
-
- ++v1_it;
- ++v2_it;
- }
- else if(v1_it->first < v2_it->first)
- {
-
- v1_it = v1.lower_bound(v2_it->first);
-
- }
- else
- {
-
- v2_it = v2.lower_bound(v1_it->first);
-
- }
- }
- return score;
- }
- double DotProductScoring::score(const BowVector &v1,
- const BowVector &v2) const
- {
- BowVector::const_iterator v1_it, v2_it;
- const BowVector::const_iterator v1_end = v1.end();
- const BowVector::const_iterator v2_end = v2.end();
-
- v1_it = v1.begin();
- v2_it = v2.begin();
-
- double score = 0;
-
- while(v1_it != v1_end && v2_it != v2_end)
- {
- const WordValue& vi = v1_it->second;
- const WordValue& wi = v2_it->second;
-
- if(v1_it->first == v2_it->first)
- {
- score += vi * wi;
-
-
- ++v1_it;
- ++v2_it;
- }
- else if(v1_it->first < v2_it->first)
- {
-
- v1_it = v1.lower_bound(v2_it->first);
-
- }
- else
- {
-
- v2_it = v2.lower_bound(v1_it->first);
-
- }
- }
- return score;
- }
|