OpenTREP Logo  0.07.18
C++ Open Travel Request Parsing Library
Loading...
Searching...
No Matches
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
15
16namespace 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
193 unsigned short nbOfResults = _resultList.size();
194 if (nbOfResults > 1) {
195 oCombinedPercentage /= std::pow (K_DEFAULT_ATTENUATION_FCTR, nbOfResults);
196 }
197
198 // DEBUG
199 OPENTREP_LOG_DEBUG (" [pct] The " << describeKey()
200 << " string partition overall matches at "
201 << oCombinedPercentage << "%");
202
203 // Store the combined weight
204 setCombinedWeight (oCombinedPercentage);
205 }
206
207}
#define OPENTREP_LOG_DEBUG(iToBeLogged)
Definition Logger.hpp:33
void fromStream(std::istream &)
std::string describeShortKey() const
StringSet getCorrectedStringSet() const
void calculateEnvelopeWeights() const
std::string describeKey() const
void toStream(std::ostream &) const
void calculateCodeMatches() const
void calculatePageRanks() const
void calculateHeuristicWeights() const
void setCombinedWeight(const Percentage_T &iPercentage)
std::string toString() const
void displayXapianPercentages() const
Class wrapping a set of Xapian documents having matched a given query string.
Definition Result.hpp:48
void displayXapianPercentages() const
Definition Result.cpp:562
const TravelQuery_T & getCorrectedTravelQuery() const
Definition Result.hpp:64
void calculatePageRanks()
Definition Result.cpp:746
const Percentage_T & getBestCombinedWeight() const
Definition Result.hpp:125
std::string toString() const
Definition Result.cpp:65
void calculateHeuristicWeights()
Definition Result.cpp:780
void calculateEnvelopeWeights()
Definition Result.cpp:614
void calculateCodeMatches()
Definition Result.cpp:653
void calculateCombinedWeights()
Definition Result.cpp:789
std::string TravelQuery_T
double Percentage_T
const Percentage_T K_DEFAULT_ATTENUATION_FCTR
std::string toString(const TokenList_T &iTokenList)
Class holding a set of strings, e.g., {"rio", "de", "janeiro"}.
Definition StringSet.hpp:19
void push_back(const std::string &)
Definition StringSet.cpp:49