OpenTREP Logo  0.6.0
C++ Open Travel Request Parsing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ResultHolder.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 #include <sstream>
7 // Xapian
8 #include <xapian.h>
9 // OpenTrep
12 #include <opentrep/bom/Result.hpp>
15 
16 namespace OPENTREP {
17 
18  // //////////////////////////////////////////////////////////////////////
19  ResultHolder::ResultHolder (const TravelQuery_T& iQueryString,
20  const Xapian::Database& iDatabase)
21  : _resultCombination (NULL),
22  _queryString (iQueryString), _database (iDatabase) {
23  init();
24  }
25 
26  // //////////////////////////////////////////////////////////////////////
27  ResultHolder::~ResultHolder () {
28  }
29 
30  // //////////////////////////////////////////////////////////////////////
31  void ResultHolder::init () {
32  _resultCombination = NULL;
33  _resultList.clear();
34  }
35 
36  // //////////////////////////////////////////////////////////////////////
37  std::string ResultHolder::describeShortKey() const {
38  std::ostringstream oStr;
39  oStr << _queryString;
40  return oStr.str();
41  }
42 
43  // //////////////////////////////////////////////////////////////////////
44  std::string ResultHolder::describeKey() const {
45  return describeShortKey();
46  }
47 
48  // //////////////////////////////////////////////////////////////////////
49  std::string ResultHolder::toString() const {
50  std::ostringstream oStr;
51  oStr << describeShortKey() << std::endl;
52 
53  unsigned short idx = 0;
54  for (ResultList_T::const_iterator itResult = _resultList.begin();
55  itResult != _resultList.end(); ++itResult, ++idx) {
56  const Result* lResult_ptr = *itResult;
57  assert (lResult_ptr != NULL);
58 
59  if (idx != 0) {
60  oStr << std::endl;
61  }
62  oStr << " ==> " << std::endl << lResult_ptr->toString();
63  }
64 
65  return oStr.str();
66  }
67 
68  // //////////////////////////////////////////////////////////////////////
69  void ResultHolder::toStream (std::ostream& ioOut) const {
70  ioOut << toString();
71  }
72 
73  // //////////////////////////////////////////////////////////////////////
74  void ResultHolder::fromStream (std::istream& ioIn) {
75  }
76 
77  // //////////////////////////////////////////////////////////////////////
79  StringSet oStringSet;
80  for (ResultList_T::const_iterator itResult = _resultList.begin();
81  itResult != _resultList.end(); ++itResult) {
82  const Result* lResult_ptr = *itResult;
83  assert (lResult_ptr != NULL);
84 
85  // Retrieve the corrected string
86  const TravelQuery_T& lCorrectedQueryString =
87  lResult_ptr->getCorrectedTravelQuery();
88  oStringSet.push_back (lCorrectedQueryString);
89  }
90 
91  //
92  return oStringSet;
93  }
94 
95  // //////////////////////////////////////////////////////////////////////
97  // Browse the Result objects
98  for (ResultList_T::const_iterator itResult = _resultList.begin();
99  itResult != _resultList.end(); ++itResult) {
100  Result* lResult_ptr = *itResult;
101  assert (lResult_ptr != NULL);
102 
103  //
104  lResult_ptr->displayXapianPercentages();
105  }
106  }
107 
108  // //////////////////////////////////////////////////////////////////////
110  // Browse the Result objects
111  for (ResultList_T::const_iterator itResult = _resultList.begin();
112  itResult != _resultList.end(); ++itResult) {
113  Result* lResult_ptr = *itResult;
114  assert (lResult_ptr != NULL);
115 
116  //
117  lResult_ptr->calculateEnvelopeWeights();
118  }
119  }
120 
121  // //////////////////////////////////////////////////////////////////////
123  // Browse the Result objects
124  for (ResultList_T::const_iterator itResult = _resultList.begin();
125  itResult != _resultList.end(); ++itResult) {
126  Result* lResult_ptr = *itResult;
127  assert (lResult_ptr != NULL);
128 
129  //
130  lResult_ptr->calculateCodeMatches();
131  }
132  }
133 
134  // //////////////////////////////////////////////////////////////////////
136  // Browse the Result objects
137  for (ResultList_T::const_iterator itResult = _resultList.begin();
138  itResult != _resultList.end(); ++itResult) {
139  Result* lResult_ptr = *itResult;
140  assert (lResult_ptr != NULL);
141 
142  //
143  lResult_ptr->calculatePageRanks();
144  }
145  }
146 
147  // //////////////////////////////////////////////////////////////////////
149  // Browse the Result objects
150  for (ResultList_T::const_iterator itResult = _resultList.begin();
151  itResult != _resultList.end(); ++itResult) {
152  Result* lResult_ptr = *itResult;
153  assert (lResult_ptr != NULL);
154 
155  //
156  lResult_ptr->calculateHeuristicWeights();
157  }
158  }
159 
160  // //////////////////////////////////////////////////////////////////////
162  Percentage_T oCombinedPercentage = 100.0;
163 
164  // When there is no result, the weight is obviously 0%
165  if (_resultList.empty() == true) {
166  oCombinedPercentage = 0.0;
167  }
168 
169  // Browse the Result objects
170  for (ResultList_T::const_iterator itResult = _resultList.begin();
171  itResult != _resultList.end(); ++itResult) {
172  Result* lResult_ptr = *itResult;
173  assert (lResult_ptr != NULL);
174 
175  // Calculate the combined weights, for all the matching documents,
176  // and set the best combined weight to the greatest one
177  lResult_ptr->calculateCombinedWeights();
178 
179  // Retrieve the just calculated combined weight
180  const Percentage_T& lPercentage = lResult_ptr->getBestCombinedWeight();
181 
182  // Take into account the current weight into the total
183  oCombinedPercentage *= lPercentage / 100.0;
184  }
185 
192  unsigned short nbOfResults = _resultList.size();
193  if (nbOfResults > 1) {
194  oCombinedPercentage /= std::pow (K_DEFAULT_ATTENUATION_FCTR, nbOfResults);
195  }
196 
197  // DEBUG
198  OPENTREP_LOG_DEBUG (" [pct] The " << describeKey()
199  << " string partition overall matches at "
200  << oCombinedPercentage << "%");
201 
202  // Store the combined weight
203  setCombinedWeight (oCombinedPercentage);
204  }
205 
206 }
void setCombinedWeight(const Percentage_T &iPercentage)
#define OPENTREP_LOG_DEBUG(iToBeLogged)
Definition: Logger.hpp:32
double Percentage_T
void calculateCombinedWeights()
Definition: Result.cpp:783
const TravelQuery_T & getCorrectedTravelQuery() const
Definition: Result.hpp:64
void fromStream(std::istream &)
void calculateCodeMatches() const
void calculateHeuristicWeights() const
void calculatePageRanks() const
void displayXapianPercentages() const
Definition: Result.cpp:556
std::string describeShortKey() const
const Percentage_T & getBestCombinedWeight() const
Definition: Result.hpp:125
std::string describeKey() const
void calculateEnvelopeWeights() const
void displayXapianPercentages() const
std::string toString() const
Definition: Result.cpp:65
const Percentage_T K_DEFAULT_ATTENUATION_FCTR
std::string toString() const
void calculateHeuristicWeights()
Definition: Result.cpp:774
void calculatePageRanks()
Definition: Result.cpp:740
StringSet getCorrectedStringSet() const
Class holding a set of strings, e.g., {"rio", "de", "janeiro"}.
Definition: StringSet.hpp:19
std::string TravelQuery_T
void push_back(const std::string &)
Definition: StringSet.cpp:49
void toStream(std::ostream &) const
void calculateEnvelopeWeights()
Definition: Result.cpp:608
void calculateCodeMatches()
Definition: Result.cpp:647
Class wrapping a set of Xapian documents having matched a given query string.
Definition: Result.hpp:48