OpenTREP Logo  0.6.0
C++ Open Travel Request Parsing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ResultCombination.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
14 
15 namespace OPENTREP {
16 
17  // //////////////////////////////////////////////////////////////////////
18  ResultCombination::ResultCombination()
19  : _travelQuery (""), _bestMatchingResultHolder (NULL) {
20  assert (false);
21  }
22 
23  // //////////////////////////////////////////////////////////////////////
24  ResultCombination::ResultCombination (const ResultCombination&)
25  : _travelQuery (""), _bestMatchingResultHolder (NULL) {
26  assert (false);
27  }
28 
29  // //////////////////////////////////////////////////////////////////////
30  ResultCombination::ResultCombination (const TravelQuery_T& iQueryString)
31  : _travelQuery (iQueryString), _bestMatchingResultHolder (NULL) {
32  init();
33  }
34 
35  // //////////////////////////////////////////////////////////////////////
36  ResultCombination::~ResultCombination() {
37  _bestMatchingResultHolder = NULL;
38  }
39 
40  // //////////////////////////////////////////////////////////////////////
41  void ResultCombination::init () {
42  _resultHolderList.clear();
43  }
44 
45  // //////////////////////////////////////////////////////////////////////
47  if (_bestMatchingResultHolder == NULL) {
48  std::ostringstream oStr;
49  oStr << "There is no best matching ResultHolder object for the given "
50  << "query string: '" << _travelQuery << "'";
51  OPENTREP_LOG_ERROR (oStr.str());
52  throw NoBestMatchingResultHolderException (oStr.str());
53  }
54  assert (_bestMatchingResultHolder != NULL);
55  return *_bestMatchingResultHolder;
56  }
57 
58  // //////////////////////////////////////////////////////////////////////
60  const ResultHolder& lResultHolder = getBestMatchingResultHolder();
61  const Percentage_T& oPercentage = lResultHolder.getCombinedWeight();
62  return oPercentage;
63  }
64 
65  // //////////////////////////////////////////////////////////////////////
67  const ResultHolder& lResultHolder = getBestMatchingResultHolder();
68  const StringSet& oStringSet = lResultHolder.getCorrectedStringSet();
69  return oStringSet;
70  }
71 
72  // //////////////////////////////////////////////////////////////////////
74  std::ostringstream oStr;
75  oStr << _travelQuery;
76  return oStr.str();
77  }
78 
79  // //////////////////////////////////////////////////////////////////////
80  std::string ResultCombination::describeKey() const {
81  return describeShortKey();
82  }
83 
84  // //////////////////////////////////////////////////////////////////////
85  std::string ResultCombination::toString() const {
86  std::ostringstream oStr;
87  oStr << describeShortKey() << std::endl;
88 
89  for (ResultHolderList_T::const_iterator itResultHolder =
90  _resultHolderList.begin();
91  itResultHolder != _resultHolderList.end(); ++itResultHolder) {
92  const ResultHolder* lResultHolder_ptr = *itResultHolder;
93  assert (lResultHolder_ptr != NULL);
94 
95  oStr << " ==> " << std::endl << lResultHolder_ptr->toString();
96  }
97 
98  return oStr.str();
99  }
100 
101  // //////////////////////////////////////////////////////////////////////
102  void ResultCombination::toStream (std::ostream& ioOut) const {
103  ioOut << toString();
104  }
105 
106  // //////////////////////////////////////////////////////////////////////
107  void ResultCombination::fromStream (std::istream& ioIn) {
108  }
109 
110  // //////////////////////////////////////////////////////////////////////
112  // Browse the ResultHolder objects
113  for (ResultHolderList_T::const_iterator itResultHolder =
114  _resultHolderList.begin();
115  itResultHolder != _resultHolderList.end(); ++itResultHolder) {
116  ResultHolder* lResultHolder_ptr = *itResultHolder;
117  assert (lResultHolder_ptr != NULL);
118 
119  //
120  lResultHolder_ptr->displayXapianPercentages();
121  }
122  }
123 
124  // //////////////////////////////////////////////////////////////////////
126  // Browse the ResultHolder objects
127  for (ResultHolderList_T::const_iterator itResultHolder =
128  _resultHolderList.begin();
129  itResultHolder != _resultHolderList.end(); ++itResultHolder) {
130  ResultHolder* lResultHolder_ptr = *itResultHolder;
131  assert (lResultHolder_ptr != NULL);
132 
133  //
134  lResultHolder_ptr->calculateEnvelopeWeights();
135  }
136  }
137 
138  // //////////////////////////////////////////////////////////////////////
140  // Browse the ResultHolder objects
141  for (ResultHolderList_T::const_iterator itResultHolder =
142  _resultHolderList.begin();
143  itResultHolder != _resultHolderList.end(); ++itResultHolder) {
144  ResultHolder* lResultHolder_ptr = *itResultHolder;
145  assert (lResultHolder_ptr != NULL);
146 
147  //
148  lResultHolder_ptr->calculateCodeMatches();
149  }
150  }
151 
152  // //////////////////////////////////////////////////////////////////////
154  // Browse the ResultHolder objects
155  for (ResultHolderList_T::const_iterator itResultHolder =
156  _resultHolderList.begin();
157  itResultHolder != _resultHolderList.end(); ++itResultHolder) {
158  ResultHolder* lResultHolder_ptr = *itResultHolder;
159  assert (lResultHolder_ptr != NULL);
160 
161  //
162  lResultHolder_ptr->calculatePageRanks();
163  }
164  }
165 
166  // //////////////////////////////////////////////////////////////////////
168  // Browse the ResultHolder objects
169  for (ResultHolderList_T::const_iterator itResultHolder =
170  _resultHolderList.begin();
171  itResultHolder != _resultHolderList.end(); ++itResultHolder) {
172  ResultHolder* lResultHolder_ptr = *itResultHolder;
173  assert (lResultHolder_ptr != NULL);
174 
175  //
176  lResultHolder_ptr->calculateHeuristicWeights();
177  }
178  }
179 
180  // //////////////////////////////////////////////////////////////////////
182  // Browse the ResultHolder objects
183  for (ResultHolderList_T::const_iterator itResultHolder =
184  _resultHolderList.begin();
185  itResultHolder != _resultHolderList.end(); ++itResultHolder) {
186  ResultHolder* lResultHolder_ptr = *itResultHolder;
187  assert (lResultHolder_ptr != NULL);
188 
189  //
190  lResultHolder_ptr->calculateCombinedWeights();
191  }
192  }
193 
194  // //////////////////////////////////////////////////////////////////////
196 
197  // Identify the ResultHolder object corresponding to the best
198  // matching percentage
199  Percentage_T lMaxMatchingPercentage = 0.0;
200  for (ResultHolderList_T::const_iterator itResultHolder =
201  _resultHolderList.begin();
202  itResultHolder != _resultHolderList.end(); ++itResultHolder) {
203  ResultHolder* lResultHolder_ptr = *itResultHolder;
204  assert (lResultHolder_ptr != NULL);
205 
206  // Retrieve the weight from the all the matching processes: full-text,
207  // envelope ID, IATA/ICAO code, heuristic PageRank.
208  const Percentage_T& lMatchingPercentage =
209  lResultHolder_ptr->getCombinedWeight();
210 
211  // Override the maximum percentage, if needed
212  if (lMatchingPercentage > lMaxMatchingPercentage) {
213  lMaxMatchingPercentage = lMatchingPercentage;
214  _bestMatchingResultHolder = lResultHolder_ptr;
215  }
216  }
217 
218  // DEBUG
219  if (_bestMatchingResultHolder != NULL) {
220  OPENTREP_LOG_DEBUG (" [pct] The best match for the '" << describeKey()
221  << "' string has a weight of "
222  << lMaxMatchingPercentage
223  << "%. It is the following string partition: "
224  << _bestMatchingResultHolder->describeKey());
225 
226  } else {
227  OPENTREP_LOG_DEBUG (" [pct] There is no match for the '"
228  << describeKey() << "' string");
229  }
230 
231  //
232  const bool doesBestMatchingResultHolderExist =
233  (_bestMatchingResultHolder != NULL);
234  return doesBestMatchingResultHolderExist;
235  }
236 
237 }
#define OPENTREP_LOG_ERROR(iToBeLogged)
Definition: Logger.hpp:23
#define OPENTREP_LOG_DEBUG(iToBeLogged)
Definition: Logger.hpp:32
double Percentage_T
const ResultHolder & getBestMatchingResultHolder() const
StringSet getCorrectedStringSet() const
void calculateCodeMatches() const
const Percentage_T & getCombinedWeight() const
std::string toString() const
void toStream(std::ostream &) const
void calculateHeuristicWeights() const
void calculatePageRanks() const
std::string describeShortKey() const
const Percentage_T & getBestMatchingWeight() const
Class wrapping functions on a list of Result objects.
std::string describeKey() const
void calculateEnvelopeWeights() const
void displayXapianPercentages() const
std::string toString() const
std::string describeKey() const
StringSet getCorrectedStringSet() const
Class holding a set of strings, e.g., {"rio", "de", "janeiro"}.
Definition: StringSet.hpp:19
std::string TravelQuery_T
void fromStream(std::istream &)