xapian-core  1.4.25
inl2weight.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 InL2Weight::InL2Weight(double c)
35  : param_c(c)
36 {
37  if (param_c <= 0)
38  throw Xapian::InvalidArgumentError("Parameter c is invalid");
43  need_stat(WDF);
45  need_stat(WQF);
47 }
48 
49 InL2Weight *
51 {
52  return new InL2Weight(param_c);
53 }
54 
55 void
56 InL2Weight::init(double factor)
57 {
58  if (factor == 0.0) {
59  // This object is for the term-independent contribution, and that's
60  // always zero for this scheme.
61  return;
62  }
63 
64  double wdfn_upper = get_wdf_upper_bound();
65  if (wdfn_upper == 0) {
66  upper_bound = 0.0;
67  return;
68  }
69 
70  double termfreq = get_termfreq();
71  double N = get_collection_size();
72 
73  wdfn_upper *= log2(1 + (param_c * get_average_length()) /
75 
76  // wdfn * L = wdfn / (wdfn + 1) = 1 / (1 + 1 / wdfn).
77  // To maximize the product, we need to minimize the denominator and so we use wdfn_upper in (1 / wdfn).
78  double maximum_wdfn_product_L = wdfn_upper / (wdfn_upper + 1.0);
79 
80  // This term is constant for all documents.
81  double idf_max = log2((N + 1) / (termfreq + 0.5));
82 
83  /* Calculate constant values to be used in get_sumpart() upfront. */
84  wqf_product_idf = get_wqf() * idf_max * factor;
86 
87  upper_bound = wqf_product_idf * maximum_wdfn_product_L * factor;
88 }
89 
90 string
92 {
93  return "Xapian::InL2Weight";
94 }
95 
96 string
98 {
99  return serialise_double(param_c);
100 }
101 
102 InL2Weight *
103 InL2Weight::unserialise(const string & s) const
104 {
105  const char *ptr = s.data();
106  const char *end = ptr + s.size();
107  double c = unserialise_double(&ptr, end);
108  if (rare(ptr != end))
109  throw Xapian::SerialisationError("Extra data in InL2Weight::unserialise()");
110  return new InL2Weight(c);
111 }
112 
113 double
115  Xapian::termcount) const
116 {
117  if (wdf == 0) return 0.0;
118  double wdfn = wdf;
119 
120  wdfn *= log2(1 + c_product_avlen / len);
121 
122  double wdfn_product_L = wdfn / (wdfn + 1.0);
123 
124  return (wqf_product_idf * wdfn_product_L);
125 }
126 
127 double
129 {
130  return upper_bound;
131 }
132 
133 double
135 {
136  return 0;
137 }
138 
139 double
141 {
142  return 0;
143 }
144 
145 }
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:363
void init(double factor)
Allow the subclass to perform any initialisation it needs to.
Definition: inl2weight.cc:56
InL2Weight * unserialise(const std::string &serialised) const
Unserialise parameters.
Definition: inl2weight.cc:103
This class implements the InL2 weighting scheme.
Definition: weight.h:833
double get_sumextra(Xapian::termcount doclen, Xapian::termcount uniqterms) const
Calculate the term-independent weight component for a document.
Definition: inl2weight.cc:134
STL namespace.
std::string serialise() const
Return this object&#39;s parameters serialised as a single string.
Definition: inl2weight.cc:97
Lower bound on (non-zero) document lengths.
Definition: weight.h:58
double get_maxextra() const
Return an upper bound on what get_sumextra() can return for any document.
Definition: inl2weight.cc:140
double wqf_product_idf
The constant values which are used on every call to get_sumpart().
Definition: weight.h:841
#define rare(COND)
Definition: config.h:565
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
Xapian::termcount get_doclength_lower_bound() const
A lower bound on the minimum length of any document in the database.
Definition: weight.h:400
double c_product_avlen
Definition: weight.h:842
double unserialise_double(const char **p, const char *end)
Unserialise a double serialised by serialise_double.
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
Average length of documents in the collection.
Definition: weight.h:44
std::string name() const
Return the name of this weighting scheme.
Definition: inl2weight.cc:91
Xapian::termcount get_wqf() const
The within-query-frequency of this term.
Definition: weight.h:384
double upper_bound
The upper bound on the weight a term can give to a document.
Definition: weight.h:838
double get_maxpart() const
Return an upper bound on what get_sumpart() can return for any document.
Definition: inl2weight.cc:128
Weighting scheme API.
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: inl2weight.cc:114
Within-document-frequency of the current term in the current document.
Definition: weight.h:54
Upper bound on wdf.
Definition: weight.h:62
Xapian::doccount get_termfreq() const
The number of documents which this term indexes.
Definition: weight.h:372
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:369
std::string serialise_double(double v)
Serialise a double to a string.
double param_c
The wdf normalization parameter in the formula.
Definition: weight.h:835
Number of documents in the collection.
Definition: weight.h:40
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:83
Xapian::termcount get_wdf_upper_bound() const
An upper bound on the wdf of this term.
Definition: weight.h:408
InL2Weight * clone() const
Clone this object.
Definition: inl2weight.cc:50