Fix BufferedFile bug handling short pread()
[xapian.git] / xapian-letor / feature / tffeature.cc
blobcef6e9ddfc53745bacd6aa81bbc4c286b1ac1142
1 /** @file tffeature.cc
2 * @brief TfFeature 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 TfFeature::name() const
36 return "TfFeature";
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 TfFeature::get_values() const
52 LOGCALL(API, vector<double>, "TfFeature::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 tf_iterator = termfreq.find(*qt);
61 if (tf_iterator != termfreq.end())
62 value += log10(1 + tf_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 tf_iterator = termfreq.find(*qt);
72 if (tf_iterator != termfreq.end())
73 value += log10(1 + tf_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 tf_iterator = termfreq.find(*qt);
82 if (tf_iterator != termfreq.end())
83 value += log10(1 + tf_iterator->second);
85 values.push_back(value);
87 return values;