2 * @brief Set the weighting scheme for Omega
4 /* Copyright (C) 2009,2013,2016 Olly Betts
5 * Copyright (C) 2013 Aarsh Shah
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (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
26 #include "stringutils.h"
30 #include "common/noreturn.h"
34 XAPIAN_NORETURN(static void
35 parameter_error(const char * param
, const string
& scheme
));
38 parameter_error(const char * msg
, const string
& scheme
)
48 double_param(const char ** p
, double * ptr_val
)
52 double v
= strtod(*p
, &end
);
53 if (*p
== end
|| errno
) return false;
60 type_smoothing_param(const char ** p
, Xapian::Weight::type_smoothing
* ptr_val
)
64 int v
= strtol(*p
, &end
, 10);
65 if (*p
== end
|| errno
|| v
< 1 || v
> 4)
68 static const Xapian::Weight::type_smoothing smooth_tab
[4] = {
69 Xapian::Weight::TWO_STAGE_SMOOTHING
,
70 Xapian::Weight::DIRICHLET_SMOOTHING
,
71 Xapian::Weight::ABSOLUTE_DISCOUNT_SMOOTHING
,
72 Xapian::Weight::JELINEK_MERCER_SMOOTHING
74 *ptr_val
= smooth_tab
[v
- 1];
79 set_weighting_scheme(Xapian::Enquire
& enq
, const string
& scheme
,
83 if (scheme
.empty()) return;
85 if (startswith(scheme
, "bm25")) {
86 const char *p
= scheme
.c_str() + 4;
88 enq
.set_weighting_scheme(Xapian::BM25Weight());
96 double min_normlen
= 0.5;
97 if (!double_param(&p
, &k1
))
98 parameter_error("Parameter 1 (k1) is invalid", scheme
);
99 if (*p
&& !double_param(&p
, &k2
))
100 parameter_error("Parameter 2 (k2) is invalid", scheme
);
101 if (*p
&& !double_param(&p
, &k3
))
102 parameter_error("Parameter 3 (k3) is invalid", scheme
);
103 if (*p
&& !double_param(&p
, &b
))
104 parameter_error("Parameter 4 (b) is invalid", scheme
);
105 if (*p
&& !double_param(&p
, &min_normlen
))
106 parameter_error("Parameter 5 (min_normlen) is invalid", scheme
);
108 parameter_error("Extra data after parameter 5", scheme
);
109 Xapian::BM25Weight
wt(k1
, k2
, k3
, b
, min_normlen
);
110 enq
.set_weighting_scheme(wt
);
115 if (startswith(scheme
, "trad")) {
116 const char *p
= scheme
.c_str() + 4;
118 enq
.set_weighting_scheme(Xapian::TradWeight());
123 if (!double_param(&p
, &k
))
124 parameter_error("Parameter is invalid", scheme
);
126 parameter_error("Extra data after parameter", scheme
);
127 enq
.set_weighting_scheme(Xapian::TradWeight(k
));
132 if (startswith(scheme
, "tfidf")) {
133 const char *p
= scheme
.c_str() + 5;
135 enq
.set_weighting_scheme(Xapian::TfIdfWeight());
139 enq
.set_weighting_scheme(Xapian::TfIdfWeight(p
+ 1));
144 if (startswith(scheme
, "inl2")) {
145 const char *p
= scheme
.c_str() + 4;
147 enq
.set_weighting_scheme(Xapian::InL2Weight());
152 if (!double_param(&p
, &k
))
153 parameter_error("Parameter is invalid", scheme
);
155 parameter_error("Extra data after parameter", scheme
);
156 enq
.set_weighting_scheme(Xapian::InL2Weight(k
));
161 if (startswith(scheme
, "ifb2")) {
162 const char *p
= scheme
.c_str() + 4;
164 enq
.set_weighting_scheme(Xapian::IfB2Weight());
169 if (!double_param(&p
, &k
))
170 parameter_error("Parameter is invalid", scheme
);
172 parameter_error("Extra data after parameter", scheme
);
173 enq
.set_weighting_scheme(Xapian::IfB2Weight(k
));
178 if (startswith(scheme
, "ineb2")) {
179 const char *p
= scheme
.c_str() + 5;
181 enq
.set_weighting_scheme(Xapian::IneB2Weight());
186 if (!double_param(&p
, &k
))
187 parameter_error("Parameter is invalid", scheme
);
189 parameter_error("Extra data after parameter", scheme
);
190 enq
.set_weighting_scheme(Xapian::IneB2Weight(k
));
195 if (startswith(scheme
, "bb2")) {
196 const char *p
= scheme
.c_str() + 3;
198 enq
.set_weighting_scheme(Xapian::BB2Weight());
203 if (!double_param(&p
, &k
))
204 parameter_error("Parameter is invalid", scheme
);
206 parameter_error("Extra data after parameter", scheme
);
207 enq
.set_weighting_scheme(Xapian::BB2Weight(k
));
212 if (startswith(scheme
, "dlh")) {
213 const char *p
= scheme
.c_str() + 3;
215 enq
.set_weighting_scheme(Xapian::DLHWeight());
219 throw "No parameters are required for DLH";
223 if (startswith(scheme
, "pl2")) {
224 const char *p
= scheme
.c_str() + 3;
226 enq
.set_weighting_scheme(Xapian::PL2Weight());
231 if (!double_param(&p
, &k
))
232 parameter_error("Parameter is invalid", scheme
);
234 parameter_error("Extra data after parameter", scheme
);
235 enq
.set_weighting_scheme(Xapian::PL2Weight(k
));
240 if (startswith(scheme
, "dph")) {
241 const char *p
= scheme
.c_str() + 3;
243 enq
.set_weighting_scheme(Xapian::DPHWeight());
247 throw "No parameters are required for DPH";
251 if (startswith(scheme
, "lm")) {
252 const char *p
= scheme
.c_str() + 2;
254 enq
.set_weighting_scheme(Xapian::LMWeight());
258 double param_log
= 0;
259 Xapian::Weight::type_smoothing type
= Xapian::Weight::TWO_STAGE_SMOOTHING
;
260 double smoothing1
= 0.7;
261 double smoothing2
= 2000;
262 if (!double_param(&p
, ¶m_log
))
263 parameter_error("Parameter 1 (log) is invalid", scheme
);
264 if (*p
&& !type_smoothing_param(&p
, &type
))
265 parameter_error("Parameter 2 (smoothing_type) is invalid", scheme
);
266 if (*p
&& !double_param(&p
, &smoothing1
))
267 parameter_error("Parameter 3 (smoothing1) is invalid", scheme
);
268 if (*p
&& !double_param(&p
, &smoothing2
))
269 parameter_error("Parameter 4 (smoothing2) is invalid", scheme
);
271 parameter_error("Extra data after parameter 4", scheme
);
272 Xapian::LMWeight
wt(param_log
, type
, smoothing1
, smoothing2
);
273 enq
.set_weighting_scheme(wt
);
278 if (scheme
== "coord") {
279 enq
.set_weighting_scheme(Xapian::CoordWeight());
283 if (scheme
!= "bool") {
284 throw "Unknown $opt{weighting} setting: " + scheme
;
288 enq
.set_weighting_scheme(Xapian::BoolWeight());