1 /** @file weight.cc
2 * @brief Set the weighting scheme for Omega
3 */
4 /* Copyright (C) 2009 Olly Betts
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (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 "weight.h"
24
25 #include "stringutils.h"
26
27 #include <cstdlib>
28 #include "safeerrno.h"
29
30 using namespace std;
31
32 static bool
double_param(const char ** p,double * ptr_val)33 double_param(const char ** p, double * ptr_val)
34 {
35 char *end;
36 errno = 0;
37 double v = strtod(*p, &end);
38 if (*p == end || errno) return false;
39 *p = end;
40 *ptr_val = v;
41 return true;
42 }
43
44 void
set_weighting_scheme(Xapian::Enquire & enq,const map<string,string> & opt,bool force_boolean)45 set_weighting_scheme(Xapian::Enquire & enq, const map<string, string> & opt,
46 bool force_boolean)
47 {
48 if (!force_boolean) {
49 map<string, string>::const_iterator i = opt.find("weighting");
50 if (i == opt.end()) return;
51
52 const string & scheme = i->second;
53 if (scheme.empty()) return;
54
55 if (startswith(scheme, "bm25")) {
56 const char *p = scheme.c_str() + 4;
57 if (*p == '\0' || C_isspace((unsigned char)*p)) {
58 double k1 = 1;
59 double k2 = 0;
60 double k3 = 1;
61 double b = 0.5;
62 double min_normlen = 0.5;
63 (void)(double_param(&p, &k1) &&
64 double_param(&p, &k2) &&
65 double_param(&p, &k3) &&
66 double_param(&p, &b) &&
67 double_param(&p, &min_normlen));
68 Xapian::BM25Weight wt(k1, k2, k3, b, min_normlen);
69 enq.set_weighting_scheme(wt);
70 return;
71 }
72 }
73
74 if (startswith(scheme, "trad")) {
75 const char *p = scheme.c_str() + 4;
76 if (*p == '\0' || C_isspace((unsigned char)*p)) {
77 double k = 1;
78 double_param(&p, &k);
79 enq.set_weighting_scheme(Xapian::TradWeight(k));
80 return;
81 }
82 }
83
84 if (scheme != "bool") {
85 throw "Unknown $opt{weighting} setting: " + scheme;
86 }
87 }
88
89 enq.set_weighting_scheme(Xapian::BoolWeight());
90 }
91