aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/features/documenttestutils.cpp
blob: ad64af1aa097b34e7afee5649675f6924569e55c (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.



#include "utils.h"
#include <vespa/searchlib/fef/itablemanager.h>
#include <vespa/searchlib/fef/properties.h>
#include <vespa/searchlib/fef/itermdata.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/vespalib/stllike/asciistream.h>

#include <cmath>
#include <ostream>

#include <vespa/vespalib/util/issue.h>
using vespalib::Issue;

#include <vespa/log/log.h>
LOG_SETUP(".features.utils");
using namespace search::fef;

namespace search::features::util {

feature_t
lookupConnectedness(const search::fef::IQueryEnvironment& env, uint32_t termId, feature_t fallback)
{
    if (termId == 0) {
        return fallback; // no previous term
    }

    const ITermData * data = env.getTerm(termId);
    const ITermData * prev = env.getTerm(termId - 1);
    if (data == nullptr || prev == nullptr) {
        return fallback; // default value
    }
    return lookupConnectedness(env, data->getUniqueId(), prev->getUniqueId(), fallback);
}

feature_t
lookupConnectedness(const search::fef::IQueryEnvironment& env,
                    uint32_t currUniqueId, uint32_t prevUniqueId, feature_t fallback)
{
    // Connectedness of 0.5 between term with unique id 2 and term with unique id 1 is represented as:
    // [vespa.term.2.connexity: "1", vespa.term.2.connexity: "0.5"]
    vespalib::asciistream os;
    os << "vespa.term." << currUniqueId << ".connexity";
    Property p = env.getProperties().lookup(os.str());
    if (p.size() == 2) {
        // we have a defined connectedness with the previous term
        if (strToNum<uint32_t>(p.getAt(0)) == prevUniqueId) {
            return strToNum<feature_t>(p.getAt(1));
        }
    }
    return fallback;
}

feature_t
lookupSignificance(const search::fef::IQueryEnvironment& env, const ITermData& term, feature_t fallback)
{
    // Significance of 0.5 for term with unique id 1 is represented as:
    // [vespa.term.1.significance: "0.5"]
    vespalib::asciistream os;
    os << "vespa.term." << term.getUniqueId() << ".significance";
    Property p = env.getProperties().lookup(os.str());
    if (p.found()) {
        return strToNum<feature_t>(p.get());
    }
    return fallback;
}

feature_t
lookupSignificance(const search::fef::IQueryEnvironment& env, uint32_t termId, feature_t fallback)
{
    const ITermData* term = env.getTerm(termId);
    if (term == nullptr) {
        return fallback;
    }
    return lookupSignificance(env, *term, fallback);
}

double
getRobertsonSparckJonesWeight(double docCount, double docsInCorpus)
{
    return std::log((docsInCorpus - docCount + 0.5)/(docCount + 0.5));
}

static const double N = 1000000.0;

feature_t
getSignificance(double docFreq)
{
    if (docFreq < (1.0/N)) {
      docFreq = 1.0/N;
    }
    if (docFreq > 1.0) {
      docFreq = 1.0;
    }
    double d = std::log(docFreq)/std::log(1.0/N);
    return 0.5 + 0.5 * d;
#if 0
    double n = docFreq * N;
    n = (n == 0) ? 1 : (n > N ? N : n);
    double a = getRobertsonSparckJonesWeight(1, N + 1);
    double b = getRobertsonSparckJonesWeight(N + 1, N + 1);
    double w = getRobertsonSparckJonesWeight(n, N + 1);
    return ((w - b)/(a - b));
#endif
}

feature_t
getSignificance(const search::fef::ITermData& termData)
{
    using FRA = search::fef::ITermFieldRangeAdapter;
    double df = 0;
    for (FRA iter(termData); iter.valid(); iter.next()) {
        df = std::max(df, iter.get().getDocFreq());
    }

    feature_t signif = getSignificance(df);
    LOG(debug, "getSignificance %e %f [ %e %f ] = %e", df, df, df * N, df * N, signif);
    return signif;
}

const search::fef::Table *
lookupTable(const search::fef::IIndexEnvironment & env, const vespalib::string & featureName,
            const vespalib::string & table, const vespalib::string & fieldName, const vespalib::string & fallback)
{
    vespalib::string tn1 = env.getProperties().lookup(featureName, table).get(fallback);
    vespalib::string tn2 = env.getProperties().lookup(featureName, table, fieldName).get(tn1);
    const search::fef::Table * retval = env.getTableManager().getTable(tn2);
    if (retval == nullptr) {
        LOG(warning, "Could not find the %s '%s' to be used for field '%s' in feature '%s'",
            table.c_str(), tn2.c_str(), fieldName.c_str(), featureName.c_str());
    }
    return retval;
}

const search::fef::ITermData *
getTermByLabel(const search::fef::IQueryEnvironment &env, const vespalib::string &label)
{
    // Labeling the query item with unique id '5' with the label 'foo'
    // is represented as: [vespa.label.foo.id: "5"]
    vespalib::asciistream os;
    os << "vespa.label." << label << ".id";
    Property p = env.getProperties().lookup(os.str());
    if (!p.found()) {
        return 0;
    }
    uint32_t uid = strToNum<uint32_t>(p.get());
    if (uid == 0) {
        Issue::report("Query label '%s' was attached to invalid unique id: '%s'",
                      label.c_str(), p.get().c_str());
        return 0;
    }
    for (uint32_t i(0), m(env.getNumTerms()); i < m; ++i) {
        const ITermData *term = env.getTerm(i);
        if (term->getUniqueId() == uid) {
            return term;
        }
    }
    Issue::report("Query label '%s' was attached to non-existing unique id: '%s'",
                  label.c_str(), p.get().c_str());
    return 0;
}

}