xapian-core  2.0.0
inl2weight.cc
Go to the documentation of this file.
1 
4 /* Copyright (C) 2013,2014 Aarsh Shah
5  * Copyright (C) 2024 Olly Betts
6  *
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.
11  *
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.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see
19  * <https://www.gnu.org/licenses/>.
20  */
21 
22 #include <config.h>
23 
24 #include "xapian/weight.h"
25 
26 #include "weightinternal.h"
27 
28 #include "serialise-double.h"
29 
30 #include "xapian/error.h"
31 
32 #include <cmath>
33 
34 using namespace std;
35 
36 namespace Xapian {
37 
38 InL2Weight::InL2Weight(double c)
39  : param_c(c)
40 {
41  if (param_c <= 0)
42  throw Xapian::InvalidArgumentError("Parameter c is invalid");
47  need_stat(WDF);
49  need_stat(WQF);
51 }
52 
53 InL2Weight *
55 {
56  return new InL2Weight(param_c);
57 }
58 
59 void
60 InL2Weight::init(double factor)
61 {
62  if (factor == 0.0) {
63  // This object is for the term-independent contribution, and that's
64  // always zero for this scheme.
65  return;
66  }
67 
68  double wdfn_upper = get_wdf_upper_bound();
69  if (wdfn_upper == 0) {
70  upper_bound = 0.0;
71  return;
72  }
73 
74  double termfreq = get_termfreq();
75  double N = get_collection_size();
76 
77  wdfn_upper *= log2(1 + (param_c * get_average_length()) /
79 
80  // wdfn * L = wdfn / (wdfn + 1) = 1 / (1 + 1 / wdfn).
81  // To maximize the product, we need to minimize the denominator and so we use wdfn_upper in (1 / wdfn).
82  double maximum_wdfn_product_L = wdfn_upper / (wdfn_upper + 1.0);
83 
84  // This term is constant for all documents.
85  double idf_max = log2((N + 1) / (termfreq + 0.5));
86 
87  /* Calculate constant values to be used in get_sumpart() upfront. */
88  wqf_product_idf = get_wqf() * idf_max * factor;
90 
91  upper_bound = wqf_product_idf * maximum_wdfn_product_L * factor;
92 }
93 
94 string
96 {
97  return "inl2";
98 }
99 
100 string
102 {
103  return serialise_double(param_c);
104 }
105 
106 InL2Weight *
107 InL2Weight::unserialise(const string & s) const
108 {
109  const char *ptr = s.data();
110  const char *end = ptr + s.size();
111  double c = unserialise_double(&ptr, end);
112  if (rare(ptr != end))
113  throw Xapian::SerialisationError("Extra data in InL2Weight::unserialise()");
114  return new InL2Weight(c);
115 }
116 
117 double
120 {
121  if (wdf == 0) return 0.0;
122  double wdfn = wdf;
123 
124  wdfn *= log2(1 + c_product_avlen / len);
125 
126  double wdfn_product_L = wdfn / (wdfn + 1.0);
127 
128  return (wqf_product_idf * wdfn_product_L);
129 }
130 
131 double
133 {
134  return upper_bound;
135 }
136 
137 [[noreturn]]
138 static inline void
139 parameter_error(const char* message, const char* params)
140 {
141  Xapian::Weight::Internal::parameter_error(message, "inl2", params);
142 }
143 
144 InL2Weight*
145 InL2Weight::create_from_parameters(const char* params) const
146 {
147  const char* p = params;
148  if (*p == '\0')
149  return new Xapian::InL2Weight();
150  double c = 1.0;
152  parameter_error("Parameter is invalid", params);
153  if (*p)
154  parameter_error("Extra data after parameter", params);
155  return new Xapian::InL2Weight(c);
156 }
157 
158 }
This class implements the InL2 weighting scheme.
Definition: weight.h:1327
double c_product_avlen
Definition: weight.h:1336
std::string serialise() const
Return this object's parameters serialised as a single string.
Definition: inl2weight.cc:101
void init(double factor)
Allow the subclass to perform any initialisation it needs to.
Definition: inl2weight.cc:60
InL2Weight * create_from_parameters(const char *params) const
Create from a human-readable parameter string.
Definition: inl2weight.cc:145
double wqf_product_idf
The constant values which are used on every call to get_sumpart().
Definition: weight.h:1335
double get_maxpart() const
Return an upper bound on what get_sumpart() can return for any document.
Definition: inl2weight.cc:132
InL2Weight * unserialise(const std::string &serialised) const
Unserialise parameters.
Definition: inl2weight.cc:107
InL2Weight * clone() const
Clone this object.
Definition: inl2weight.cc:54
double param_c
The wdf normalization parameter in the formula.
Definition: weight.h:1329
std::string name() const
Return the name of this weighting scheme, e.g.
Definition: inl2weight.cc:95
double upper_bound
The upper bound on the weight a term can give to a document.
Definition: weight.h:1332
double get_sumpart(Xapian::termcount wdf, Xapian::termcount doclen, Xapian::termcount uniqterms, Xapian::termcount wdfdocmax) const
Calculate the weight contribution for this object's term to a document.
Definition: inl2weight.cc:118
InvalidArgumentError indicates an invalid parameter value was passed to the API.
Definition: error.h:229
Indicates an error in the std::string serialisation of an object.
Definition: error.h:917
static void parameter_error(const char *msg, const std::string &scheme, const char *params)
static bool double_param(const char **p, double *ptr_val)
Xapian::termcount get_doclength_lower_bound() const
A lower bound on the minimum length of any document in the shard.
Definition: weight.h:586
Xapian::doccount get_termfreq() const
The number of documents which this term indexes.
Definition: weight.h:558
void need_stat(stat_flags flag)
Tell Xapian that your subclass will want a particular statistic.
Definition: weight.h:183
Xapian::termcount get_wqf() const
The within-query-frequency of this term.
Definition: weight.h:570
Xapian::doccount get_collection_size() const
The number of documents in the collection.
Definition: weight.h:549
Xapian::doclength get_average_length() const
The average length of a document in the collection.
Definition: weight.h:555
@ AVERAGE_LENGTH
Average length of documents in the collection.
Definition: weight.h:47
@ DOC_LENGTH
Length of the current document (sum wdf).
Definition: weight.h:59
@ TERMFREQ
How many documents the current term is in.
Definition: weight.h:49
@ WQF
Within-query-frequency of the current term.
Definition: weight.h:55
@ COLLECTION_SIZE
Number of documents in the collection.
Definition: weight.h:43
@ WDF_MAX
Upper bound on wdf.
Definition: weight.h:81
@ DOC_LENGTH_MIN
Lower bound on (non-zero) document lengths.
Definition: weight.h:65
@ WDF
Within-document-frequency of the current term in the current document.
Definition: weight.h:57
Xapian::termcount get_wdf_upper_bound() const
An upper bound on the wdf of this term in the shard.
Definition: weight.h:594
#define rare(COND)
Definition: config.h:607
PositionList * p
Hierarchy of classes which Xapian can throw as exceptions.
The Xapian namespace contains public interfaces for the Xapian library.
Definition: compactor.cc:82
unsigned XAPIAN_TERMCOUNT_BASE_TYPE termcount
A counts of terms.
Definition: types.h:64
static void parameter_error(const char *message, const char *params)
Definition: bb2weight.cc:185
string serialise_double(double v)
Serialise a double to a string.
double unserialise_double(const char **p, const char *end)
Unserialise a double serialised by serialise_double.
functions to serialise and unserialise a double
Weighting scheme API.
Xapian::Weight::Internal class, holding database and term statistics.