xapian-core  1.4.26
ineb2weight.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2013,2014 Aarsh Shah
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <config.h>
22 
23 #include "xapian/weight.h"
24 #include "common/log2.h"
25 
26 #include "serialise-double.h"
27 
28 #include "xapian/error.h"
29 
30 using namespace std;
31 
32 namespace Xapian {
33 
34 IneB2Weight::IneB2Weight(double c) : param_c(c) {
35  if (param_c <= 0)
36  throw Xapian::InvalidArgumentError("Parameter c is invalid");
41  need_stat(WDF);
43  need_stat(WQF);
46 }
47 
50 {
51  return new IneB2Weight(param_c);
52 }
53 
54 void
55 IneB2Weight::init(double factor)
56 {
57  if (factor == 0.0) {
58  // This object is for the term-independent contribution, and that's
59  // always zero for this scheme.
60  return;
61  }
62 
63  double wdfn_upper = get_wdf_upper_bound();
64  if (wdfn_upper == 0) {
65  upper_bound = 0.0;
66  return;
67  }
68 
69  wdfn_upper *= log2(1 + (param_c * get_average_length()) /
71 
72  double N = get_collection_size();
73  double F = get_collection_freq();
74  double termfreq = get_termfreq();
75 
76  double max_wdfn_product_B = (F + 1.0) / (termfreq + (termfreq / wdfn_upper));
77 
78  double mean = F / N;
79 
80  double expected_max = N * (1.0 - exp(-mean));
81 
82  double idf_max = log2((N + 1.0) / (expected_max + 0.5));
83 
84  /* Calculate constant values used in get_sumpart(). */
85  wqf_product_idf = get_wqf() * idf_max * factor;
87  B_constant = (F + 1.0) / termfreq;
88 
89  upper_bound = max_wdfn_product_B * idf_max * get_wqf() * factor;
90 }
91 
92 string
94 {
95  return "Xapian::IneB2Weight";
96 }
97 
98 string
100 {
101  return serialise_double(param_c);
102 }
103 
104 IneB2Weight *
105 IneB2Weight::unserialise(const string & s) const
106 {
107  const char *ptr = s.data();
108  const char *end = ptr + s.size();
109  double c = unserialise_double(&ptr, end);
110  if (rare(ptr != end))
111  throw Xapian::SerialisationError("Extra data in IneB2Weight::unserialise()");
112  return new IneB2Weight(c);
113 }
114 
115 double
117  Xapian::termcount) const
118 {
119  if (wdf == 0) return 0.0;
120  double wdfn = wdf;
121 
122  wdfn *= log2(1 + c_product_avlen / len);
123 
124  double wdfn_product_B = wdfn * B_constant / (wdfn + 1.0);
125 
126  return (wdfn_product_B * wqf_product_idf);
127 }
128 
129 double
131 {
132  return upper_bound;
133 }
134 
135 double
137 {
138  return 0;
139 }
140 
141 double
143 {
144  return 0;
145 }
146 
147 }
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:80
Xapian::doccount get_collection_size() const
The number of documents in the collection.
Definition: weight.h:374
Xapian::termcount get_collection_freq() const
The collection frequency of the term.
Definition: weight.h:389
void init(double factor)
Allow the subclass to perform any initialisation it needs to.
Definition: ineb2weight.cc:55
double get_maxpart() const
Return an upper bound on what get_sumpart() can return for any document.
Definition: ineb2weight.cc:130
STL namespace.
double get_sumpart(Xapian::termcount wdf, Xapian::termcount doclen, Xapian::termcount uniqterms) const
Calculate the weight contribution for this object&#39;s term to a document.
Definition: ineb2weight.cc:116
Lower bound on (non-zero) document lengths.
Definition: weight.h:62
std::string serialise() const
Return this object&#39;s parameters serialised as a single string.
Definition: ineb2weight.cc:99
#define rare(COND)
Definition: config.h:575
Hierarchy of classes which Xapian can throw as exceptions.
unsigned XAPIAN_TERMCOUNT_BASE_TYPE termcount
A counts of terms.
Definition: types.h:72
functions to serialise and unserialise a double
Length of the current document (sum wdf).
Definition: weight.h:56
InvalidArgumentError indicates an invalid parameter value was passed to the API.
Definition: error.h:241
double param_c
The wdf normalization parameter in the formula.
Definition: weight.h:990
Xapian::termcount get_doclength_lower_bound() const
A lower bound on the minimum length of any document in the shard.
Definition: weight.h:411
double unserialise_double(const char **p, const char *end)
Unserialise a double serialised by serialise_double.
IneB2Weight * clone() const
Clone this object.
Definition: ineb2weight.cc:49
Indicates an error in the std::string serialisation of an object.
Definition: error.h:929
Within-query-frequency of the current term.
Definition: weight.h:52
double get_sumextra(Xapian::termcount doclen, Xapian::termcount uniqterms) const
Calculate the term-independent weight component for a document.
Definition: ineb2weight.cc:136
Average length of documents in the collection.
Definition: weight.h:44
IneB2Weight * unserialise(const std::string &serialised) const
Unserialise parameters.
Definition: ineb2weight.cc:105
This class implements the IneB2 weighting scheme.
Definition: weight.h:988
Xapian::termcount get_wqf() const
The within-query-frequency of this term.
Definition: weight.h:395
double get_maxextra() const
Return an upper bound on what get_sumextra() can return for any document.
Definition: ineb2weight.cc:142
Sum of wdf over the whole collection for the current term.
Definition: weight.h:76
Weighting scheme API.
Within-document-frequency of the current term in the current document.
Definition: weight.h:54
Upper bound on wdf.
Definition: weight.h:74
Xapian::doccount get_termfreq() const
The number of documents which this term indexes.
Definition: weight.h:383
How many documents the current term is in.
Definition: weight.h:46
double log2(double x)
Definition: log2.h:31
Xapian::doclength get_average_length() const
The average length of a document in the collection.
Definition: weight.h:380
std::string serialise_double(double v)
Serialise a double to a string.
Number of documents in the collection.
Definition: weight.h:40
std::string name() const
Return the name of this weighting scheme.
Definition: ineb2weight.cc:93
Defines a log2() function to find the logarithm to base 2 if not already defined in the library...
void need_stat(stat_flags flag)
Tell Xapian that your subclass will want a particular statistic.
Definition: weight.h:94
double c_product_avlen
Definition: weight.h:997
Xapian::termcount get_wdf_upper_bound() const
An upper bound on the wdf of this term in the shard.
Definition: weight.h:419
double wqf_product_idf
Constant values used in get_sumpart().
Definition: weight.h:996
double upper_bound
The upper bound of the weight.
Definition: weight.h:993
double B_constant
Definition: weight.h:998