Fix unittest to build when getting UUIDs from /proc
[xapian.git] / xapian-letor / feature / idffeature.cc
blobb2199191c997d1016a6d9459cf0f746a0b54d915
1 /** @file idffeature.cc
2 * @brief IdfFeature class
3 */
4 /* Copyright (C) 2012 Parth Gupta
5 * Copyright (C) 2016 Ayush Tomar
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <config.h>
24 #include "xapian-letor/feature.h"
26 #include "debuglog.h"
27 #include "stringutils.h"
29 using namespace std;
31 namespace Xapian {
33 string
34 IdfFeature::name() const
36 return "IdfFeature";
39 /** A helper function for feature->get_value()
41 * Checks if the term belongs to the title or is stemmed from the title.
43 inline bool
44 is_title_term(const std::string& term)
46 return startswith(term, 'S') || startswith(term, "ZS");
49 vector<double>
50 IdfFeature::get_values() const
52 LOGCALL(API, vector<double>, "IdfFeature::get_values", NO_ARGS);
54 vector<double> values;
55 double value = 0;
57 for (Xapian::TermIterator qt = feature_query.get_unique_terms_begin();
58 qt != feature_query.get_terms_end(); ++qt) {
59 if (is_title_term((*qt))) {
60 auto idf_iterator = inverse_doc_freq.find(*qt);
61 if (idf_iterator != inverse_doc_freq.end())
62 value += log10(1 + idf_iterator->second);
65 values.push_back(value);
66 value = 0;
68 for (Xapian::TermIterator qt = feature_query.get_unique_terms_begin();
69 qt != feature_query.get_terms_end(); ++qt) {
70 if (!is_title_term((*qt))) {
71 auto idf_iterator = inverse_doc_freq.find(*qt);
72 if (idf_iterator != inverse_doc_freq.end())
73 value += log10(1 + idf_iterator->second);
76 values.push_back(value);
77 value = 0;
79 for (Xapian::TermIterator qt = feature_query.get_unique_terms_begin();
80 qt != feature_query.get_terms_end(); ++qt) {
81 auto idf_iterator = inverse_doc_freq.find(*qt);
82 if (idf_iterator != inverse_doc_freq.end())
83 value += log10(1 + idf_iterator->second);
85 values.push_back(value);
87 return values;