OpenTREP Logo  0.07.18
C++ Open Travel Request Parsing Library
Loading...
Searching...
No Matches
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
15namespace 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 // //////////////////////////////////////////////////////////////////////
227
228 // //////////////////////////////////////////////////////////////////////
230
231 // Identify the ResultHolder object corresponding to the best
232 // matching percentage
233 Percentage_T lMaxMatchingPercentage = 0.0;
234 for (ResultHolderList_T::const_iterator itResultHolder =
235 _resultHolderList.begin();
236 itResultHolder != _resultHolderList.end(); ++itResultHolder) {
237 ResultHolder* lResultHolder_ptr = *itResultHolder;
238 assert (lResultHolder_ptr != NULL);
239
240 // Retrieve the weight from the all the matching processes: full-text,
241 // envelope ID, IATA/ICAO code, heuristic PageRank.
242 const Percentage_T& lMatchingPercentage =
243 lResultHolder_ptr->getCombinedWeight();
244
245 // Override the maximum percentage, if needed
246 if (lMatchingPercentage > lMaxMatchingPercentage) {
247 lMaxMatchingPercentage = lMatchingPercentage;
248 _bestMatchingResultHolder = lResultHolder_ptr;
249 }
250 }
251
252 // DEBUG
253 if (_bestMatchingResultHolder != NULL) {
254 OPENTREP_LOG_DEBUG (" [pct] The best match for the '" << describeKey()
255 << "' string has a weight of "
256 << lMaxMatchingPercentage
257 << "%. It is the following string partition: "
258 << _bestMatchingResultHolder->describeKey());
259
260 } else {
261 OPENTREP_LOG_DEBUG (" [pct] There is no match for the '"
262 << describeKey() << "' string");
263 }
264
265 //
266 const bool doesBestMatchingResultHolderExist =
267 (_bestMatchingResultHolder != NULL);
268 return doesBestMatchingResultHolderExist;
269 }
270
271}
#define OPENTREP_LOG_ERROR(iToBeLogged)
Definition Logger.hpp:24
#define OPENTREP_LOG_DEBUG(iToBeLogged)
Definition Logger.hpp:33
const ResultHolder & getBestMatchingResultHolder() const
StringSet getCorrectedStringSet() const
const Percentage_T & getBestMatchingWeight() const
void toStream(std::ostream &) const
std::string describeShortKey() const
Class wrapping functions on a list of Result objects.
StringSet getCorrectedStringSet() const
void calculateEnvelopeWeights() const
void calculateCodeMatches() const
void calculatePageRanks() const
void calculateHeuristicWeights() const
const Percentage_T & getCombinedWeight() const
std::string toString() const
void displayXapianPercentages() const
double Percentage_T
std::string toString(const TokenList_T &iTokenList)
Class holding a set of strings, e.g., {"rio", "de", "janeiro"}.
Definition StringSet.hpp:19