xapian-core  2.1.0
weight.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2004-2024 Olly Betts
5  * Copyright (C) 2009 Lemur Consulting Ltd
6  * Copyright (C) 2013,2014 Aarsh Shah
7  * Copyright (C) 2016,2017 Vivek Pal
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, see
21  * <https://www.gnu.org/licenses/>.
22  */
23 
24 #ifndef XAPIAN_INCLUDED_WEIGHT_H
25 #define XAPIAN_INCLUDED_WEIGHT_H
26 
27 #include <string>
28 
29 #include <xapian/database.h>
30 #include <xapian/deprecated.h>
31 #include <xapian/registry.h>
32 #include <xapian/types.h>
33 #include <xapian/visibility.h>
34 
35 namespace Xapian {
36 
39  protected:
41  typedef enum {
43  COLLECTION_SIZE = 0,
45  RSET_SIZE = 0,
47  AVERAGE_LENGTH = 4,
49  TERMFREQ = 1,
51  RELTERMFREQ = 1,
53  QUERY_LENGTH = 0,
55  WQF = 0,
57  WDF = 2,
59  DOC_LENGTH = 8,
65  DOC_LENGTH_MIN = 16,
73  DOC_LENGTH_MAX = 32,
81  WDF_MAX = 64,
83  COLLECTION_FREQ = 1,
85  UNIQUE_TERMS = 128,
89  TOTAL_LENGTH = 256,
94  WDF_DOC_MAX = 512,
104  UNIQUE_TERMS_MIN = 1024,
114  UNIQUE_TERMS_MAX = 2048,
121  DB_DOC_LENGTH_MIN = 4096,
128  DB_DOC_LENGTH_MAX = 8192,
135  DB_UNIQUE_TERMS_MIN = 16384,
142  DB_UNIQUE_TERMS_MAX = 32768,
149  DB_WDF_MAX = 65536,
153  IS_BOOLWEIGHT_ = static_cast<int>(0x80000000)
154  } stat_flags;
155 
183  void need_stat(stat_flags flag) {
184  stats_needed = stat_flags(stats_needed | flag);
185  }
186 
196  virtual void init(double factor) = 0;
197 
198  private:
200  void operator=(const Weight &);
201 
204 
207 
210 
213 
216 
217  // The collection frequency of the term.
219 
222 
225 
228 
231 
234 
237 
240 
245 
250 
253 
256 
259 
264 
269 
270  public:
271 
273  Weight() : stats_needed() { }
274 
275  class Internal;
276 
278  virtual ~Weight();
279 
296  virtual Weight * clone() const = 0;
297 
321  virtual std::string name() const;
322 
328  virtual std::string serialise() const;
329 
347  virtual Weight * unserialise(const std::string & serialised) const;
348 
374  virtual double get_sumpart(Xapian::termcount wdf,
375  Xapian::termcount doclen,
376  Xapian::termcount uniqterms,
377  Xapian::termcount wdfdocmax) const = 0;
378 
384  virtual double get_maxpart() const = 0;
385 
408  virtual double get_sumextra(Xapian::termcount doclen,
409  Xapian::termcount uniqterms,
410  Xapian::termcount wdfdocmax) const;
411 
421  virtual double get_maxextra() const;
422 
437  void init_(const Internal & stats, Xapian::termcount query_len_,
438  const std::string & term, Xapian::termcount wqf_,
439  double factor,
440  const Xapian::Database::Internal* shard,
441  void* postlist);
442 
454  void init_(const Internal & stats, Xapian::termcount query_len_,
455  double factor, Xapian::doccount termfreq,
456  Xapian::doccount reltermfreq, Xapian::termcount collection_freq,
457  const Xapian::Database::Internal* shard);
458 
466  void init_(const Internal & stats, Xapian::termcount query_len_,
467  const Xapian::Database::Internal* shard);
468 
476  return stats_needed & DOC_LENGTH;
477  }
478 
484  bool get_sumpart_needs_wdf_() const {
485  return stats_needed & WDF;
486  }
487 
495  return stats_needed & UNIQUE_TERMS;
496  }
497 
508  static const Weight * create(const std::string & scheme,
509  const Registry & reg = Registry());
510 
517  virtual Weight * create_from_parameters(const char * params) const;
518 
520  bool is_bool_weight_() const {
521  // We use a special flag bit to make this check efficient. Note we
522  // can't use (get_maxpart() == 0.0) since that's not required to work
523  // without init() having been called.
524  return stats_needed & IS_BOOLWEIGHT_;
525  }
526 
536  return stats_needed & WDF_DOC_MAX;
537  }
538 
539  protected:
546  Weight(const Weight &);
547 
549  Xapian::doccount get_collection_size() const { return collection_size_; }
550 
552  Xapian::doccount get_rset_size() const { return rset_size_; }
553 
555  Xapian::doclength get_average_length() const { return average_length_; }
556 
558  Xapian::doccount get_termfreq() const { return termfreq_; }
559 
561  Xapian::doccount get_reltermfreq() const { return reltermfreq_; }
562 
564  Xapian::termcount get_collection_freq() const { return collectionfreq_; }
565 
567  Xapian::termcount get_query_length() const { return query_length_; }
568 
570  Xapian::termcount get_wqf() const { return wqf_; }
571 
577  return doclength_upper_bound_;
578  }
579 
587  return doclength_lower_bound_;
588  }
589 
595  return wdf_upper_bound_;
596  }
597 
600  return total_length_;
601  }
602 
613  return unique_terms_upper_bound_;
614  }
615 
624  return unique_terms_lower_bound_;
625  }
626 
632  return db_doclength_upper_bound_;
633  }
634 
642  return db_doclength_lower_bound_;
643  }
644 
653  return db_unique_terms_upper_bound_;
654  }
655 
662  return db_unique_terms_lower_bound_;
663  }
664 
670  return db_wdf_upper_bound_;
671  }
672 };
673 
679  BoolWeight * clone() const;
680 
681  void init(double factor);
682 
683  public:
686  need_stat(IS_BOOLWEIGHT_);
687  }
688 
689  std::string name() const;
690 
691  std::string serialise() const;
692  BoolWeight * unserialise(const std::string & serialised) const;
693 
694  double get_sumpart(Xapian::termcount wdf,
695  Xapian::termcount doclen,
696  Xapian::termcount uniqterms,
697  Xapian::termcount wdfdocmax) const;
698  double get_maxpart() const;
699 
700  BoolWeight * create_from_parameters(const char * params) const;
701 };
702 
705  public:
710  enum class wdf_norm : unsigned char {
715  NONE = 1,
716 
721  BOOLEAN = 2,
722 
727  SQUARE = 3,
728 
733  LOG = 4,
734 
740  PIVOTED = 5,
741 
747  LOG_AVERAGE = 6,
748 
753  AUG_LOG = 7,
754 
759  SQRT = 8,
760 
765  AUG_AVERAGE = 9,
766 
771  MAX = 10,
772 
777  AUG = 11
778  };
779 
784  enum class idf_norm : unsigned char {
789  NONE = 1,
790 
797  TFIDF = 2,
798 
803  SQUARE = 3,
804 
809  FREQ = 4,
810 
815  PROB = 5,
816 
821  PIVOTED = 6,
822 
827  GLOBAL_FREQ = 7,
828 
833  LOG_GLOBAL_FREQ = 8,
834 
839  INCREMENTED_GLOBAL_FREQ = 9,
840 
845  SQRT_GLOBAL_FREQ = 10
846  };
847 
852  enum class wt_norm : unsigned char {
857  NONE = 1
858  };
859  private:
866 
868  double wqf_factor;
869 
871  double idfn;
872 
874  double param_slope, param_delta;
875 
876  TfIdfWeight * clone() const;
877 
878  void init(double factor);
879 
880  /* When additional normalizations are implemented in the future, the additional statistics for them
881  should be accessed by these functions. */
882  double get_wdfn(Xapian::termcount wdf,
883  Xapian::termcount len,
884  Xapian::termcount uniqterms,
885  Xapian::termcount wdfdocmax,
886  wdf_norm wdf_normalization) const;
887  double get_idfn(idf_norm idf_normalization) const;
888  double get_wtn(double wt, wt_norm wt_normalization) const;
889 
890  public:
936  explicit TfIdfWeight(const std::string& normalizations)
937  : TfIdfWeight(normalizations, 0.2, 1.0) {}
938 
988  TfIdfWeight(const std::string &normalizations, double slope, double delta);
989 
1001  TfIdfWeight(wdf_norm wdf_normalization,
1002  idf_norm idf_normalization,
1003  wt_norm wt_normalization)
1004  : TfIdfWeight(wdf_normalization, idf_normalization,
1005  wt_normalization, 0.2, 1.0) {}
1006 
1022  TfIdfWeight(wdf_norm wdf_norm_, idf_norm idf_norm_,
1023  wt_norm wt_norm_, double slope, double delta);
1024 
1027  : wdf_norm_(wdf_norm::NONE), idf_norm_(idf_norm::TFIDF),
1028  wt_norm_(wt_norm::NONE), param_slope(0.2), param_delta(1.0)
1029  {
1030  need_stat(WQF);
1031  need_stat(TERMFREQ);
1032  need_stat(WDF);
1033  need_stat(WDF_MAX);
1034  need_stat(COLLECTION_SIZE);
1035  }
1036 
1037  std::string name() const;
1038 
1039  std::string serialise() const;
1040  TfIdfWeight * unserialise(const std::string & serialised) const;
1041 
1042  double get_sumpart(Xapian::termcount wdf,
1043  Xapian::termcount doclen,
1044  Xapian::termcount uniqterm,
1045  Xapian::termcount wdfdocmax) const;
1046  double get_maxpart() const;
1047 
1048  TfIdfWeight * create_from_parameters(const char * params) const;
1049 };
1050 
1051 
1056 
1058  mutable double termweight;
1059 
1061  double param_k1, param_k2, param_k3, param_b;
1062 
1065 
1066  BM25Weight * clone() const;
1067 
1068  void init(double factor);
1069 
1070  public:
1098  BM25Weight(double k1, double k2, double k3, double b, double min_normlen)
1099  : param_k1(k1), param_k2(k2), param_k3(k3), param_b(b),
1100  param_min_normlen(min_normlen)
1101  {
1102  if (param_k1 < 0) param_k1 = 0;
1103  if (param_k2 < 0) param_k2 = 0;
1104  if (param_k3 < 0) param_k3 = 0;
1105  if (param_b < 0) {
1106  param_b = 0;
1107  } else if (param_b > 1) {
1108  param_b = 1;
1109  }
1110  need_stat(COLLECTION_SIZE);
1111  need_stat(RSET_SIZE);
1112  need_stat(TERMFREQ);
1113  need_stat(RELTERMFREQ);
1114  need_stat(WDF);
1115  need_stat(WDF_MAX);
1116  if (param_k2 != 0 || (param_k1 != 0 && param_b != 0)) {
1117  need_stat(DOC_LENGTH_MIN);
1118  need_stat(AVERAGE_LENGTH);
1119  }
1120  if (param_k1 != 0 && param_b != 0) need_stat(DOC_LENGTH);
1121  if (param_k2 != 0) {
1122  need_stat(DOC_LENGTH);
1123  need_stat(QUERY_LENGTH);
1124  }
1125  if (param_k3 != 0) need_stat(WQF);
1126  }
1127 
1129  : param_k1(1), param_k2(0), param_k3(1), param_b(0.5),
1130  param_min_normlen(0.5)
1131  {
1132  need_stat(COLLECTION_SIZE);
1133  need_stat(RSET_SIZE);
1134  need_stat(TERMFREQ);
1135  need_stat(RELTERMFREQ);
1136  need_stat(WDF);
1137  need_stat(WDF_MAX);
1138  need_stat(DOC_LENGTH_MIN);
1139  need_stat(AVERAGE_LENGTH);
1140  need_stat(DOC_LENGTH);
1141  need_stat(WQF);
1142  }
1143 
1144  std::string name() const;
1145 
1146  std::string serialise() const;
1147  BM25Weight * unserialise(const std::string & serialised) const;
1148 
1149  double get_sumpart(Xapian::termcount wdf,
1150  Xapian::termcount doclen,
1151  Xapian::termcount uniqterm,
1152  Xapian::termcount wdfdocmax) const;
1153  double get_maxpart() const;
1154 
1155  double get_sumextra(Xapian::termcount doclen,
1156  Xapian::termcount uniqterms,
1157  Xapian::termcount wdfdocmax) const;
1158  double get_maxextra() const;
1159 
1160  BM25Weight * create_from_parameters(const char * params) const;
1161 };
1162 
1167 
1169  mutable double termweight;
1170 
1172  double param_k1, param_k2, param_k3, param_b;
1173 
1176 
1178  double param_delta;
1179 
1180  BM25PlusWeight * clone() const;
1181 
1182  void init(double factor);
1183 
1184  public:
1219  BM25PlusWeight(double k1, double k2, double k3, double b,
1220  double min_normlen, double delta)
1221  : param_k1(k1), param_k2(k2), param_k3(k3), param_b(b),
1222  param_min_normlen(min_normlen), param_delta(delta)
1223  {
1224  if (param_k1 < 0) param_k1 = 0;
1225  if (param_k2 < 0) param_k2 = 0;
1226  if (param_k3 < 0) param_k3 = 0;
1227  if (param_delta < 0) param_delta = 0;
1228  if (param_b < 0) {
1229  param_b = 0;
1230  } else if (param_b > 1) {
1231  param_b = 1;
1232  }
1233  need_stat(COLLECTION_SIZE);
1234  need_stat(RSET_SIZE);
1235  need_stat(TERMFREQ);
1236  need_stat(RELTERMFREQ);
1237  need_stat(WDF);
1238  need_stat(WDF_MAX);
1239  if (param_k2 != 0 || (param_k1 != 0 && param_b != 0)) {
1240  need_stat(DOC_LENGTH_MIN);
1241  need_stat(AVERAGE_LENGTH);
1242  }
1243  if (param_k1 != 0 && param_b != 0) need_stat(DOC_LENGTH);
1244  if (param_k2 != 0) {
1245  need_stat(DOC_LENGTH);
1246  need_stat(QUERY_LENGTH);
1247  }
1248  if (param_k3 != 0) need_stat(WQF);
1249  }
1250 
1252  : param_k1(1), param_k2(0), param_k3(1), param_b(0.5),
1253  param_min_normlen(0.5), param_delta(1)
1254  {
1255  need_stat(COLLECTION_SIZE);
1256  need_stat(RSET_SIZE);
1257  need_stat(TERMFREQ);
1258  need_stat(RELTERMFREQ);
1259  need_stat(WDF);
1260  need_stat(WDF_MAX);
1261  need_stat(DOC_LENGTH_MIN);
1262  need_stat(AVERAGE_LENGTH);
1263  need_stat(DOC_LENGTH);
1264  need_stat(WQF);
1265  }
1266 
1267  std::string name() const;
1268 
1269  std::string serialise() const;
1270  BM25PlusWeight * unserialise(const std::string & serialised) const;
1271 
1272  double get_sumpart(Xapian::termcount wdf,
1273  Xapian::termcount doclen,
1274  Xapian::termcount uniqterms,
1275  Xapian::termcount wdfdocmax) const;
1276  double get_maxpart() const;
1277 
1278  double get_sumextra(Xapian::termcount doclen,
1279  Xapian::termcount uniqterms,
1280  Xapian::termcount wdfdocmax) const;
1281  double get_maxextra() const;
1282 
1283  BM25PlusWeight * create_from_parameters(const char * params) const;
1284 };
1285 
1299 class XAPIAN_DEPRECATED_CLASS TradWeight : public BM25Weight
1300 {
1301  public:
1309  explicit TradWeight(double k = 1.0) : BM25Weight(k, 0.0, 0.0, 1.0, 0.0) { }
1310 };
1311 
1332  double param_c;
1333 
1335  double upper_bound;
1336 
1340 
1341  InL2Weight * clone() const;
1342 
1343  void init(double factor);
1344 
1345  public:
1354  explicit InL2Weight(double c);
1355 
1357  : param_c(1.0)
1358  {
1359  need_stat(AVERAGE_LENGTH);
1360  need_stat(DOC_LENGTH);
1361  need_stat(DOC_LENGTH_MIN);
1362  need_stat(DOC_LENGTH_MAX);
1363  need_stat(COLLECTION_SIZE);
1364  need_stat(WDF);
1365  need_stat(WDF_MAX);
1366  need_stat(WQF);
1367  need_stat(TERMFREQ);
1368  }
1369 
1370  std::string name() const;
1371 
1372  std::string serialise() const;
1373  InL2Weight * unserialise(const std::string & serialised) const;
1374 
1375  double get_sumpart(Xapian::termcount wdf,
1376  Xapian::termcount doclen,
1377  Xapian::termcount uniqterms,
1378  Xapian::termcount wdfdocmax) const;
1379  double get_maxpart() const;
1380 
1381  InL2Weight * create_from_parameters(const char * params) const;
1382 };
1383 
1402  double param_c;
1403 
1405  double upper_bound;
1406 
1410  double B_constant;
1411 
1412  IfB2Weight * clone() const;
1413 
1414  void init(double factor);
1415 
1416  public:
1427  explicit IfB2Weight(double c);
1428 
1429  IfB2Weight() : param_c(1.0) {
1430  need_stat(AVERAGE_LENGTH);
1431  need_stat(DOC_LENGTH);
1432  need_stat(DOC_LENGTH_MIN);
1433  need_stat(DOC_LENGTH_MAX);
1434  need_stat(COLLECTION_SIZE);
1435  need_stat(COLLECTION_FREQ);
1436  need_stat(WDF);
1437  need_stat(WDF_MAX);
1438  need_stat(WQF);
1439  need_stat(TERMFREQ);
1440  }
1441 
1442  std::string name() const;
1443 
1444  std::string serialise() const;
1445  IfB2Weight * unserialise(const std::string & serialised) const;
1446 
1447  double get_sumpart(Xapian::termcount wdf,
1448  Xapian::termcount doclen,
1449  Xapian::termcount uniqterm,
1450  Xapian::termcount wdfdocmax) const;
1451  double get_maxpart() const;
1452 
1453  IfB2Weight * create_from_parameters(const char * params) const;
1454 };
1455 
1474  double param_c;
1475 
1477  double upper_bound;
1478 
1482  double B_constant;
1483 
1484  IneB2Weight * clone() const;
1485 
1486  void init(double factor);
1487 
1488  public:
1497  explicit IneB2Weight(double c);
1498 
1499  IneB2Weight() : param_c(1.0) {
1500  need_stat(AVERAGE_LENGTH);
1501  need_stat(DOC_LENGTH);
1502  need_stat(DOC_LENGTH_MIN);
1503  need_stat(DOC_LENGTH_MAX);
1504  need_stat(COLLECTION_SIZE);
1505  need_stat(WDF);
1506  need_stat(WDF_MAX);
1507  need_stat(WQF);
1508  need_stat(COLLECTION_FREQ);
1509  need_stat(TERMFREQ);
1510  }
1511 
1512  std::string name() const;
1513 
1514  std::string serialise() const;
1515  IneB2Weight * unserialise(const std::string & serialised) const;
1516 
1517  double get_sumpart(Xapian::termcount wdf,
1518  Xapian::termcount doclen,
1519  Xapian::termcount uniqterms,
1520  Xapian::termcount wdfdocmax) const;
1521  double get_maxpart() const;
1522 
1523  IneB2Weight * create_from_parameters(const char * params) const;
1524 };
1525 
1545  double param_c;
1546 
1548  double upper_bound;
1549 
1552  double B_constant;
1553  double wt;
1556 
1557  BB2Weight * clone() const;
1558 
1559  void init(double factor);
1560 
1561  public:
1572  explicit BB2Weight(double c);
1573 
1574  BB2Weight() : param_c(1.0) {
1575  need_stat(AVERAGE_LENGTH);
1576  need_stat(DOC_LENGTH);
1577  need_stat(DOC_LENGTH_MIN);
1578  need_stat(DOC_LENGTH_MAX);
1579  need_stat(COLLECTION_SIZE);
1580  need_stat(COLLECTION_FREQ);
1581  need_stat(WDF);
1582  need_stat(WDF_MAX);
1583  need_stat(WQF);
1584  need_stat(TERMFREQ);
1585  }
1586 
1587  std::string name() const;
1588 
1589  std::string serialise() const;
1590  BB2Weight * unserialise(const std::string & serialised) const;
1591 
1592  double get_sumpart(Xapian::termcount wdf,
1593  Xapian::termcount doclen,
1594  Xapian::termcount uniqterms,
1595  Xapian::termcount wdfdocmax) const;
1596  double get_maxpart() const;
1597 
1598  BB2Weight * create_from_parameters(const char * params) const;
1599 };
1600 
1620  double upper_bound;
1621 
1625 
1626  DLHWeight * clone() const;
1627 
1628  void init(double factor);
1629 
1630  public:
1632  need_stat(DOC_LENGTH);
1633  need_stat(COLLECTION_FREQ);
1634  need_stat(WDF);
1635  need_stat(WQF);
1636  need_stat(WDF_MAX);
1637  need_stat(DOC_LENGTH_MIN);
1638  need_stat(DOC_LENGTH_MAX);
1639  need_stat(TOTAL_LENGTH);
1640  }
1641 
1642  std::string name() const;
1643 
1644  std::string serialise() const;
1645  DLHWeight * unserialise(const std::string & serialised) const;
1646 
1647  double get_sumpart(Xapian::termcount wdf,
1648  Xapian::termcount doclen,
1649  Xapian::termcount uniqterms,
1650  Xapian::termcount wdfdocmax) const;
1651  double get_maxpart() const;
1652 
1653  DLHWeight * create_from_parameters(const char * params) const;
1654 };
1655 
1676  double factor;
1677 
1679  double param_c;
1680 
1682  double upper_bound;
1683 
1685  double P1, P2;
1686 
1688  double cl;
1689 
1690  PL2Weight * clone() const;
1691 
1692  void init(double factor_);
1693 
1694  public:
1705  explicit PL2Weight(double c);
1706 
1707  PL2Weight() : param_c(1.0) {
1708  need_stat(AVERAGE_LENGTH);
1709  need_stat(DOC_LENGTH);
1710  need_stat(DOC_LENGTH_MIN);
1711  need_stat(DOC_LENGTH_MAX);
1712  need_stat(COLLECTION_SIZE);
1713  need_stat(COLLECTION_FREQ);
1714  need_stat(WDF);
1715  need_stat(WDF_MAX);
1716  need_stat(WQF);
1717  }
1718 
1719  std::string name() const;
1720 
1721  std::string serialise() const;
1722  PL2Weight * unserialise(const std::string & serialised) const;
1723 
1724  double get_sumpart(Xapian::termcount wdf,
1725  Xapian::termcount doclen,
1726  Xapian::termcount uniqterms,
1727  Xapian::termcount wdfdocmax) const;
1728  double get_maxpart() const;
1729 
1730  PL2Weight * create_from_parameters(const char * params) const;
1731 };
1732 
1736  double factor;
1737 
1739  double param_c;
1740 
1742  double param_delta;
1743 
1745  double upper_bound;
1746 
1748  double P1, P2;
1749 
1751  double cl;
1752 
1754  double mean;
1755 
1757  double dw;
1758 
1759  PL2PlusWeight * clone() const;
1760 
1761  void init(double factor_);
1762 
1763  public:
1781  PL2PlusWeight(double c, double delta);
1782 
1784  : param_c(1.0), param_delta(0.8) {
1785  need_stat(AVERAGE_LENGTH);
1786  need_stat(DOC_LENGTH);
1787  need_stat(DOC_LENGTH_MIN);
1788  need_stat(DOC_LENGTH_MAX);
1789  need_stat(COLLECTION_SIZE);
1790  need_stat(COLLECTION_FREQ);
1791  need_stat(WDF);
1792  need_stat(WDF_MAX);
1793  need_stat(WQF);
1794  }
1795 
1796  std::string name() const;
1797 
1798  std::string serialise() const;
1799  PL2PlusWeight * unserialise(const std::string & serialised) const;
1800 
1801  double get_sumpart(Xapian::termcount wdf,
1802  Xapian::termcount doclen,
1803  Xapian::termcount uniqterms,
1804  Xapian::termcount wdfdocmax) const;
1805  double get_maxpart() const;
1806 
1807  PL2PlusWeight * create_from_parameters(const char * params) const;
1808 };
1809 
1831  double upper_bound;
1832 
1836 
1837  DPHWeight * clone() const;
1838 
1839  void init(double factor);
1840 
1841  public:
1844  need_stat(DOC_LENGTH);
1845  need_stat(COLLECTION_FREQ);
1846  need_stat(WDF);
1847  need_stat(WQF);
1848  need_stat(WDF_MAX);
1849  need_stat(DOC_LENGTH_MIN);
1850  need_stat(DOC_LENGTH_MAX);
1851  need_stat(TOTAL_LENGTH);
1852  }
1853 
1854  std::string name() const;
1855 
1856  std::string serialise() const;
1857  DPHWeight * unserialise(const std::string & serialised) const;
1858 
1859  double get_sumpart(Xapian::termcount wdf,
1860  Xapian::termcount doclen,
1861  Xapian::termcount uniqterms,
1862  Xapian::termcount wdfdocmax) const;
1863  double get_maxpart() const;
1864 
1865  DPHWeight * create_from_parameters(const char * params) const;
1866 };
1867 
1868 
1880  double factor;
1881 
1884 
1886  double multiplier;
1887 
1888  LMJMWeight* clone() const;
1889 
1890  void init(double factor_);
1891 
1892  public:
1911  explicit LMJMWeight(double lambda = 0.0) : param_lambda(lambda) {
1912  need_stat(WQF);
1913  need_stat(QUERY_LENGTH);
1914  need_stat(DOC_LENGTH);
1915  need_stat(WDF);
1916  need_stat(WDF_MAX);
1917  need_stat(COLLECTION_FREQ);
1918  need_stat(TOTAL_LENGTH);
1919  need_stat(DOC_LENGTH_MIN);
1920  }
1921 
1922  double get_sumpart(Xapian::termcount wdf,
1923  Xapian::termcount doclen,
1924  Xapian::termcount uniqterm,
1925  Xapian::termcount wdfdocmax) const;
1926 
1927  double get_maxpart() const;
1928 
1929  std::string name() const;
1930 
1931  std::string serialise() const;
1932  LMJMWeight* unserialise(const std::string& serialised) const;
1933 
1934  LMJMWeight* create_from_parameters(const char* params) const;
1935 };
1936 
1953  double factor;
1954 
1956  double param_mu;
1957 
1959  double param_delta;
1960 
1962  double multiplier;
1963 
1970 
1971  LMDirichletWeight* clone() const;
1972 
1973  void init(double factor_);
1974 
1975  public:
1984  explicit LMDirichletWeight(double mu = 2000.0, double delta = 0.05)
1985  : param_mu(mu), param_delta(delta) {
1986  need_stat(WQF);
1987  need_stat(QUERY_LENGTH);
1988  need_stat(DOC_LENGTH);
1989  need_stat(WDF);
1990  need_stat(WDF_MAX);
1991  need_stat(COLLECTION_FREQ);
1992  need_stat(TOTAL_LENGTH);
1993  need_stat(DOC_LENGTH_MIN);
1994  need_stat(DOC_LENGTH_MAX);
1995  }
1996 
1997  double get_sumpart(Xapian::termcount wdf,
1998  Xapian::termcount doclen,
1999  Xapian::termcount uniqterm,
2000  Xapian::termcount wdfdocmax) const;
2001 
2002  double get_maxpart() const;
2003 
2004  double get_sumextra(Xapian::termcount doclen,
2006  Xapian::termcount) const;
2007 
2008  double get_maxextra() const;
2009 
2010  std::string name() const;
2011 
2012  std::string serialise() const;
2013  LMDirichletWeight* unserialise(const std::string& serialised) const;
2014 
2015  LMDirichletWeight* create_from_parameters(const char* params) const;
2016 };
2017 
2029  double factor;
2030 
2032  double param_delta;
2033 
2035  double multiplier;
2036 
2043 
2044  LMAbsDiscountWeight* clone() const;
2045 
2046  void init(double factor_);
2047 
2048  public:
2053  explicit LMAbsDiscountWeight(double delta = 0.7) : param_delta(delta) {
2054  need_stat(WQF);
2055  need_stat(QUERY_LENGTH);
2056  need_stat(DOC_LENGTH);
2057  need_stat(WDF);
2058  need_stat(WDF_MAX);
2059  need_stat(COLLECTION_FREQ);
2060  need_stat(TOTAL_LENGTH);
2061  need_stat(DOC_LENGTH_MIN);
2062  need_stat(UNIQUE_TERMS);
2063  need_stat(DOC_LENGTH_MAX);
2064  }
2065 
2066  double get_sumpart(Xapian::termcount wdf,
2068  Xapian::termcount uniqterm,
2069  Xapian::termcount wdfdocmax) const;
2070 
2071  double get_maxpart() const;
2072 
2073  double get_sumextra(Xapian::termcount doclen,
2075  Xapian::termcount) const;
2076 
2077  double get_maxextra() const;
2078 
2079  std::string name() const;
2080 
2081  std::string serialise() const;
2082  LMAbsDiscountWeight* unserialise(const std::string& serialised) const;
2083 
2084  LMAbsDiscountWeight* create_from_parameters(const char* params) const;
2085 };
2086 
2098  double factor;
2099 
2102 
2104  double param_mu;
2105 
2107  double multiplier;
2108 
2115 
2116  LM2StageWeight* clone() const;
2117 
2118  void init(double factor_);
2119 
2120  public:
2128  explicit LM2StageWeight(double lambda = 0.7, double mu = 2000.0)
2129  : param_lambda(lambda), param_mu(mu)
2130  {
2131  need_stat(WQF);
2132  need_stat(QUERY_LENGTH);
2133  need_stat(DOC_LENGTH);
2134  need_stat(WDF);
2135  need_stat(WDF_MAX);
2136  need_stat(COLLECTION_FREQ);
2137  need_stat(TOTAL_LENGTH);
2138  need_stat(DOC_LENGTH_MIN);
2139  need_stat(DOC_LENGTH_MAX);
2140  }
2141 
2142  double get_sumpart(Xapian::termcount wdf,
2143  Xapian::termcount doclen,
2144  Xapian::termcount uniqterm,
2145  Xapian::termcount wdfdocmax) const;
2146  double get_maxpart() const;
2147 
2148  double get_sumextra(Xapian::termcount doclen,
2149  Xapian::termcount uniqterm,
2150  Xapian::termcount wdfdocmax) const;
2151  double get_maxextra() const;
2152 
2153  std::string name() const;
2154 
2155  std::string serialise() const;
2156  LM2StageWeight* unserialise(const std::string& serialised) const;
2157 
2158  LM2StageWeight* create_from_parameters(const char* params) const;
2159 };
2160 
2168  double factor;
2169 
2170  public:
2171  CoordWeight * clone() const;
2172 
2173  void init(double factor_);
2174 
2177 
2178  std::string name() const;
2179 
2180  std::string serialise() const;
2181  CoordWeight * unserialise(const std::string & serialised) const;
2182 
2183  double get_sumpart(Xapian::termcount wdf,
2184  Xapian::termcount doclen,
2185  Xapian::termcount uniqterms,
2186  Xapian::termcount wdfdocmax) const;
2187  double get_maxpart() const;
2188 
2189  CoordWeight * create_from_parameters(const char * params) const;
2190 };
2191 
2212  double numerator;
2213 
2215  double upper_bound;
2216 
2217  void init(double factor);
2218 
2219  public:
2220  DiceWeight* clone() const;
2221 
2224  need_stat(WQF);
2225  need_stat(QUERY_LENGTH);
2226  need_stat(UNIQUE_TERMS);
2227  need_stat(UNIQUE_TERMS_MIN);
2228  }
2229 
2230  std::string name() const;
2231 
2232  std::string serialise() const;
2233  DiceWeight* unserialise(const std::string& serialised) const;
2234 
2235  double get_sumpart(Xapian::termcount wdf,
2236  Xapian::termcount doclen,
2237  Xapian::termcount uniqterm,
2238  Xapian::termcount wdfdocmax) const;
2239  double get_maxpart() const;
2240 
2241  DiceWeight* create_from_parameters(const char* params) const;
2242 };
2243 }
2244 
2245 #endif // XAPIAN_INCLUDED_WEIGHT_H
char name[9]
Definition: dbcheck.cc:57
This class implements the BB2 weighting scheme.
Definition: weight.h:1543
double stirling_constant_2
Definition: weight.h:1555
double B_constant
Definition: weight.h:1552
double upper_bound
The upper bound on the weight.
Definition: weight.h:1548
double stirling_constant_1
Definition: weight.h:1554
double c_product_avlen
The constant values to be used in get_sumpart().
Definition: weight.h:1551
double param_c
The wdf normalization parameter in the formula.
Definition: weight.h:1545
Xapian::Weight subclass implementing the BM25+ probabilistic formula.
Definition: weight.h:1164
Xapian::doclength len_factor
Factor to multiply the document length by.
Definition: weight.h:1166
Xapian::doclength param_min_normlen
The minimum normalised document length value.
Definition: weight.h:1175
double param_delta
Additional parameter delta in the BM25+ formula.
Definition: weight.h:1178
double termweight
Factor combining all the document independent factors.
Definition: weight.h:1169
BM25PlusWeight(double k1, double k2, double k3, double b, double min_normlen, double delta)
Construct a BM25PlusWeight.
Definition: weight.h:1219
Xapian::Weight subclass implementing the BM25 probabilistic formula.
Definition: weight.h:1053
Xapian::doclength param_min_normlen
The minimum normalised document length value.
Definition: weight.h:1064
BM25Weight(double k1, double k2, double k3, double b, double min_normlen)
Construct a BM25Weight.
Definition: weight.h:1098
Xapian::doclength len_factor
Factor to multiply the document length by.
Definition: weight.h:1055
double termweight
Factor combining all the document independent factors.
Definition: weight.h:1058
Class implementing a "boolean" weighting scheme.
Definition: weight.h:678
BoolWeight()
Construct a BoolWeight.
Definition: weight.h:685
Xapian::Weight subclass implementing Coordinate Matching.
Definition: weight.h:2166
double factor
The factor to multiply weights by.
Definition: weight.h:2168
CoordWeight()
Construct a CoordWeight.
Definition: weight.h:2176
This class implements the DLH weighting scheme, which is a representative scheme of the Divergence fr...
Definition: weight.h:1618
double upper_bound
The upper bound on the weight.
Definition: weight.h:1620
double log_constant
The constant value to be used in get_sumpart().
Definition: weight.h:1623
double wqf_product_factor
Definition: weight.h:1624
This class implements the DPH weighting scheme.
Definition: weight.h:1829
double wqf_product_factor
Definition: weight.h:1835
DPHWeight()
Construct a DPHWeight.
Definition: weight.h:1843
double upper_bound
The upper bound on the weight.
Definition: weight.h:1831
double log_constant
The constant value used in get_sumpart() .
Definition: weight.h:1834
Virtual base class for Database internals.
Xapian::Weight subclass implementing Dice Coefficient.
Definition: weight.h:2210
double upper_bound
Upper bound on the weight.
Definition: weight.h:2215
double numerator
The numerator in the weight calculation.
Definition: weight.h:2212
DiceWeight()
Construct a DiceWeight.
Definition: weight.h:2223
This class implements the IfB2 weighting scheme.
Definition: weight.h:1400
double c_product_avlen
Definition: weight.h:1409
double B_constant
Definition: weight.h:1410
double upper_bound
The upper bound on the weight.
Definition: weight.h:1405
double param_c
The wdf normalization parameter in the formula.
Definition: weight.h:1402
double wqf_product_idf
The constant values which are used for calculations in get_sumpart().
Definition: weight.h:1408
This class implements the InL2 weighting scheme.
Definition: weight.h:1330
double c_product_avlen
Definition: weight.h:1339
double wqf_product_idf
The constant values which are used on every call to get_sumpart().
Definition: weight.h:1338
double param_c
The wdf normalization parameter in the formula.
Definition: weight.h:1332
double upper_bound
The upper bound on the weight a term can give to a document.
Definition: weight.h:1335
This class implements the IneB2 weighting scheme.
Definition: weight.h:1472
double upper_bound
The upper bound of the weight.
Definition: weight.h:1477
double param_c
The wdf normalization parameter in the formula.
Definition: weight.h:1474
double c_product_avlen
Definition: weight.h:1481
double wqf_product_idf
Constant values used in get_sumpart().
Definition: weight.h:1480
Language Model weighting with Two Stage smoothing.
Definition: weight.h:2096
LM2StageWeight(double lambda=0.7, double mu=2000.0)
Construct a LM2StageWeight.
Definition: weight.h:2128
double factor
The factor to multiply weights by.
Definition: weight.h:2098
double multiplier
Precalculated multiplier for use in weight calculations.
Definition: weight.h:2107
double extra_offset
Precalculated offset to add to every sumextra.
Definition: weight.h:2114
double param_mu
Parameter controlling the smoothing.
Definition: weight.h:2104
double param_lambda
Parameter controlling the smoothing.
Definition: weight.h:2101
Language Model weighting with Absolute Discount smoothing.
Definition: weight.h:2027
double factor
The factor to multiply weights by.
Definition: weight.h:2029
double param_delta
Parameter controlling the smoothing.
Definition: weight.h:2032
LMAbsDiscountWeight(double delta=0.7)
Construct a LMAbsDiscountWeight.
Definition: weight.h:2053
double multiplier
Precalculated multiplier for use in weight calculations.
Definition: weight.h:2035
double extra_offset
Precalculated offset to add to every sumextra.
Definition: weight.h:2042
Language Model weighting with Dirichlet or Dir+ smoothing.
Definition: weight.h:1951
double param_delta
A pseudo TF value to control the scale of the TF lower bound.
Definition: weight.h:1959
double param_mu
Parameter controlling the smoothing.
Definition: weight.h:1956
double factor
The factor to multiply weights by.
Definition: weight.h:1953
double extra_offset
Precalculated offset to add to every sumextra.
Definition: weight.h:1969
LMDirichletWeight(double mu=2000.0, double delta=0.05)
Construct a LMDirichletWeight.
Definition: weight.h:1984
double multiplier
Precalculated multiplier for use in weight calculations.
Definition: weight.h:1962
Language Model weighting with Jelinek-Mercer smoothing.
Definition: weight.h:1878
LMJMWeight(double lambda=0.0)
Construct a LMJMWeight.
Definition: weight.h:1911
double multiplier
Precalculated multiplier for use in weight calculations.
Definition: weight.h:1886
double factor
The factor to multiply weights by.
Definition: weight.h:1880
double param_lambda
Parameter controlling the smoothing.
Definition: weight.h:1883
Xapian::Weight subclass implementing the PL2+ probabilistic formula.
Definition: weight.h:1734
double mean
Set by init() to get_collection_freq()) / get_collection_size()
Definition: weight.h:1754
double dw
Weight contribution of delta term in the PL2+ function.
Definition: weight.h:1757
double factor
The factor to multiply weights by.
Definition: weight.h:1736
double P1
Constants for a given term in a given query.
Definition: weight.h:1748
double param_c
The wdf normalization parameter in the formula.
Definition: weight.h:1739
double cl
Set by init() to (param_c * get_average_length())
Definition: weight.h:1751
double param_delta
Additional parameter delta in the PL2+ weighting formula.
Definition: weight.h:1742
double upper_bound
The upper bound on the weight.
Definition: weight.h:1745
This class implements the PL2 weighting scheme.
Definition: weight.h:1674
double upper_bound
The upper bound on the weight.
Definition: weight.h:1682
double param_c
The wdf normalization parameter in the formula.
Definition: weight.h:1679
double cl
Set by init() to (param_c * get_average_length())
Definition: weight.h:1688
double P1
Constants for a given term in a given query.
Definition: weight.h:1685
double factor
The factor to multiply weights by.
Definition: weight.h:1676
Registry for user subclasses.
Definition: registry.h:47
Xapian::Weight subclass implementing the tf-idf weighting scheme.
Definition: weight.h:704
double wqf_factor
The factor to multiply with the weight.
Definition: weight.h:868
TfIdfWeight(const std::string &normalizations)
Construct a TfIdfWeight.
Definition: weight.h:936
idf_norm
Idf normalizations.
Definition: weight.h:784
double param_delta
Definition: weight.h:874
wt_norm wt_norm_
The parameter for normalization for the document weight.
Definition: weight.h:865
wdf_norm wdf_norm_
The parameter for normalization for the wdf.
Definition: weight.h:861
wt_norm
Weight normalizations.
Definition: weight.h:852
idf_norm idf_norm_
The parameter for normalization for the idf.
Definition: weight.h:863
double idfn
Normalised IDF value (document-independent).
Definition: weight.h:871
TfIdfWeight()
Construct a TfIdfWeight using the default normalizations ("ntn").
Definition: weight.h:1026
wdf_norm
Wdf normalizations.
Definition: weight.h:710
TfIdfWeight(wdf_norm wdf_normalization, idf_norm idf_normalization, wt_norm wt_normalization)
Construct a TfIdfWeight.
Definition: weight.h:1001
Xapian::Weight subclass implementing the traditional probabilistic formula.
Definition: weight.h:1300
TradWeight(double k=1.0)
Construct a TradWeight.
Definition: weight.h:1309
Class to hold statistics for a given collection.
Abstract base class for weighting schemes.
Definition: weight.h:38
Xapian::termcount unique_terms_upper_bound_
An upper bound on the number of unique terms in any document in the shard.
Definition: weight.h:249
Xapian::termcount get_db_wdf_upper_bound() const
An upper bound on the wdf of this term in the database.
Definition: weight.h:669
Xapian::termcount db_doclength_upper_bound_
An upper bound on the maximum length of any document in the database.
Definition: weight.h:255
Xapian::termcount wqf_
The within-query-frequency of this term.
Definition: weight.h:227
Xapian::termcount get_query_length() const
The length of the query.
Definition: weight.h:567
bool get_sumpart_needs_wdfdocmax_() const
Definition: weight.h:535
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_reltermfreq() const
The number of relevant documents which this term indexes.
Definition: weight.h:561
Xapian::totallength total_length_
Total length of all documents in the collection.
Definition: weight.h:239
void operator=(const Weight &)
Don't allow assignment.
Xapian::termcount db_wdf_upper_bound_
An upper bound on the wdf of this term in the database.
Definition: weight.h:258
Xapian::doccount collection_size_
The number of documents in the collection.
Definition: weight.h:206
Weight()
Default constructor, needed by subclass constructors.
Definition: weight.h:273
Xapian::termcount query_length_
The length of the query.
Definition: weight.h:224
Xapian::termcount get_db_unique_terms_upper_bound() const
A lower bound on the number of unique terms in any document in the database.
Definition: weight.h:652
Xapian::doccount get_termfreq() const
The number of documents which this term indexes.
Definition: weight.h:558
bool is_bool_weight_() const
Definition: weight.h:520
virtual Weight * clone() const =0
Clone this object.
Xapian::doclength average_length_
The average length of a document in the collection.
Definition: weight.h:212
Xapian::termcount doclength_upper_bound_
An upper bound on the maximum length of any document in the shard.
Definition: weight.h:233
Xapian::doccount rset_size_
The number of documents marked as relevant.
Definition: weight.h:209
bool get_sumpart_needs_wdf_() const
Definition: weight.h:484
Xapian::totallength get_total_length() const
Total length of all documents in the collection.
Definition: weight.h:599
Xapian::doccount reltermfreq_
The number of relevant documents which this term indexes.
Definition: weight.h:221
Xapian::termcount wdf_upper_bound_
An upper bound on the wdf of this term in the shard.
Definition: weight.h:236
Xapian::termcount get_db_doclength_lower_bound() const
A lower bound on the minimum length of any document in the database.
Definition: weight.h:641
virtual void init(double factor)=0
Allow the subclass to perform any initialisation it needs to.
void need_stat(stat_flags flag)
Tell Xapian that your subclass will want a particular statistic.
Definition: weight.h:183
virtual double get_maxpart() const =0
Return an upper bound on what get_sumpart() can return for any document.
Xapian::termcount get_db_unique_terms_lower_bound() const
An upper bound on the number of unique terms in any document in the database.
Definition: weight.h:661
Xapian::doccount get_rset_size() const
The number of documents marked as relevant.
Definition: weight.h:552
Xapian::termcount db_doclength_lower_bound_
A lower bound on the minimum length of any document in the database.
Definition: weight.h:252
Xapian::doccount termfreq_
The number of documents which this term indexes.
Definition: weight.h:215
Xapian::termcount get_unique_terms_lower_bound() const
An upper bound on the number of unique terms in any document in the shard.
Definition: weight.h:623
Xapian::termcount get_wqf() const
The within-query-frequency of this term.
Definition: weight.h:570
Xapian::termcount get_collection_freq() const
The collection frequency of the term.
Definition: weight.h:564
Xapian::termcount collectionfreq_
Definition: weight.h:218
Xapian::doccount get_collection_size() const
The number of documents in the collection.
Definition: weight.h:549
Weight(const Weight &)
Don't allow copying.
Xapian::doclength get_average_length() const
The average length of a document in the collection.
Definition: weight.h:555
Xapian::termcount get_unique_terms_upper_bound() const
A lower bound on the number of unique terms in any document in the shard.
Definition: weight.h:612
Xapian::termcount db_unique_terms_upper_bound_
An upper bound on the number of unique terms in any document in the database.
Definition: weight.h:268
bool get_sumpart_needs_uniqueterms_() const
Definition: weight.h:494
Xapian::termcount get_doclength_upper_bound() const
An upper bound on the maximum length of any document in the shard.
Definition: weight.h:576
stat_flags stats_needed
A bitmask of the statistics this weighting scheme needs.
Definition: weight.h:203
Xapian::termcount get_db_doclength_upper_bound() const
An upper bound on the maximum length of any document in the database.
Definition: weight.h:631
bool get_sumpart_needs_doclength_() const
Definition: weight.h:475
Xapian::termcount doclength_lower_bound_
A lower bound on the minimum length of any document in the shard.
Definition: weight.h:230
stat_flags
Stats which the weighting scheme can use (see need_stat()).
Definition: weight.h:41
Xapian::termcount db_unique_terms_lower_bound_
A lower bound on the number of unique terms in any document in the database.
Definition: weight.h:263
virtual double get_sumpart(Xapian::termcount wdf, Xapian::termcount doclen, Xapian::termcount uniqterms, Xapian::termcount wdfdocmax) const =0
Calculate the weight contribution for this object's term to a document.
Xapian::termcount unique_terms_lower_bound_
A lower bound on the number of unique terms in any document in the shard.
Definition: weight.h:244
Xapian::termcount get_wdf_upper_bound() const
An upper bound on the wdf of this term in the shard.
Definition: weight.h:594
An indexed database of documents.
string term
Define XAPIAN_DEPRECATED() and related macros.
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
double doclength
A normalised document length.
Definition: types.h:58
unsigned XAPIAN_DOCID_BASE_TYPE doccount
A count of documents.
Definition: types.h:37
XAPIAN_TOTALLENGTH_TYPE totallength
The total length of all documents in a database.
Definition: types.h:112
Class for looking up user subclasses during unserialisation.
@ NONE
Definition: sbl-dispatch.h:26
typedefs for Xapian
Define XAPIAN_VISIBILITY_* macros.
#define XAPIAN_VISIBILITY_DEFAULT
Definition: visibility.h:28
#define XAPIAN_VISIBILITY_INTERNAL
Definition: visibility.h:29