aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/fef/test/rankresult.cpp
blob: 1ee3fe1c139dd7a5d135a124f9f9d23d4221fd20 (plain) (blame)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
102
103
104
105
106
107
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "rankresult.h"
#include <cmath>
#include <ostream>

#include <vespa/log/log.h>
LOG_SETUP(".fef.rankresult");

namespace search::fef::test {

RankResult::RankResult() :
    _rankScores(),
    _epsilon(0.0)
{
    // empty
}

RankResult &
RankResult::addScore(const vespalib::string & featureName, feature_t score)
{
    _rankScores[featureName] = score;
    return *this;
}

feature_t
RankResult::getScore(const vespalib::string & featureName) const
{
    auto itr = _rankScores.find(featureName);
    if (itr != _rankScores.end()) {
        return itr->second;
    }
    return 0.0f;
}

bool
RankResult::operator==(const RankResult & rhs) const
{
    return includes(rhs) && rhs.includes(*this);
}

bool
RankResult::includes(const RankResult & rhs) const
{
    double epsilon = std::max(_epsilon, rhs._epsilon);

    for (const auto& score : rhs._rankScores) {
        auto findItr = _rankScores.find(score.first);
        if (findItr == _rankScores.end()) {
            LOG(info, "Did not find expected feature '%s' in this rank result", score.first.c_str());
            return false;
        }
        if (score.second < findItr->second - epsilon ||
            score.second > findItr->second + epsilon ||
            (std::isnan(findItr->second) && !std::isnan(score.second)))
        {
            LOG(info, "Feature '%s' did not have expected score.", score.first.c_str());
            LOG(info, "Expected: %f ~ %f", score.second, epsilon);
            LOG(info, "Actual  : %f", findItr->second);
            return false;
        }
    }
    return true;
}

RankResult &
RankResult::clear()
{
    _rankScores.clear();
    return *this;
}

std::vector<vespalib::string> &
RankResult::getKeys(std::vector<vespalib::string> &ret)
{
    for (const auto& score : _rankScores) {
        ret.push_back(score.first);
    }
    return ret;
}

std::vector<vespalib::string>
RankResult::getKeys()
{
    std::vector<vespalib::string> ret;
    return getKeys(ret);
}

RankResult &
RankResult::setEpsilon(double epsilon) {
    _epsilon = epsilon;
    return *this;
}

double
RankResult::getEpsilon() const {
    return _epsilon;
}

std::ostream & operator<<(std::ostream & os, const RankResult & rhs) {
    os << "[";
    for (const auto& score : rhs._rankScores) {
        os << "['" << score.first << "' = " << score.second << "]";
    }
    return os << "]";
}

}