OpenTREP Logo  0.6.0
C++ Open Travel Request Parsing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ScoreBoard.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 #include <sstream>
7 // OpenTrep
13 
14 namespace OPENTREP {
15 
16  // //////////////////////////////////////////////////////////////////////
17  ScoreBoard::ScoreBoard (const TravelQuery_T& iQueryString)
18  : _queryString (iQueryString) {
19  }
20 
21  // //////////////////////////////////////////////////////////////////////
22  ScoreBoard::ScoreBoard (const ScoreBoard& iScoreBoard)
23  : _queryString (iScoreBoard._queryString),
24  _scoreMap (iScoreBoard._scoreMap) {
25  }
26 
27  // //////////////////////////////////////////////////////////////////////
28  ScoreBoard::ScoreBoard (const TravelQuery_T& iQueryString,
29  const ScoreType& iType, const Score_T& iScore)
30  : _queryString (iQueryString) {
31  setScore (iType, iScore);
32  }
33 
34  // //////////////////////////////////////////////////////////////////////
36  _scoreMap.clear();
37  }
38 
39  // //////////////////////////////////////////////////////////////////////
40  Score_T ScoreBoard::getScore (const ScoreType& iScoreType) const {
41  Score_T oScore = 0.0;
42 
43  // Check whether a score value already exists for that type
44  const ScoreType::EN_ScoreType& lScoreTypeEnum = iScoreType.getType();
45  ScoreMap_T::const_iterator itScore = _scoreMap.find (lScoreTypeEnum);
46  if (itScore != _scoreMap.end()) {
47  oScore = itScore->second;
48  }
49 
50  return oScore;
51  }
52 
53  // //////////////////////////////////////////////////////////////////////
54  void ScoreBoard::setScore (const ScoreType& iScoreType,
55  const Score_T& iScore) {
56  Score_T oScore = iScore;
57 
73  if (iScoreType == ScoreType::CODE_FULL_MATCH) {
74  const FloatingPoint<Percentage_T> lComparablePct (oScore);
75  const FloatingPoint<Percentage_T> lCodeFullMatchingPct (1.0);
76  if (lComparablePct.AlmostEquals (lCodeFullMatchingPct) == true) {
78 
79  } else {
80  // Normally, K_DEFAULT_MODIFIED_MATCHING_PCT == 99.999.
81  // See basic/BasConst.cpp
83  }
84  }
85 
91  /*
92  if (iScoreType == ScoreType::XAPIAN_PCT) {
93  const FloatingPoint<Percentage_T> lComparablePct (oScore);
94  const FloatingPoint<Percentage_T> lFullMatchingPct (100.0);
95  if (lComparablePct.AlmostEquals (lFullMatchingPct) == true) {
96  // Normally, K_DEFAULT_MODIFIED_MATCHING_PCT == 99.999.
97  // See basic/BasConst.cpp
98  oScore = K_DEFAULT_MODIFIED_MATCHING_PCT;
99  }
100  }
101  */
102 
113  if (iScoreType == ScoreType::ENV_ID) {
114  const FloatingPoint<Percentage_T> lComparableValue (oScore);
115  const FloatingPoint<Percentage_T> lNoEnvelopeIDValue (0.0);
116  if (lComparableValue.AlmostEquals (lNoEnvelopeIDValue) == true) {
117  oScore = 100.00;
118  } else {
119  // Normally, K_DEFAULT_ENVELOPE_PCT == 0.001. See basic/BasConst.cpp
120  oScore = K_DEFAULT_ENVELOPE_PCT;
121  }
122  }
123 
124  // Check whether a score value already exists for that type
125  const ScoreType::EN_ScoreType& lScoreTypeEnum = iScoreType.getType();
126  ScoreMap_T::iterator itScore = _scoreMap.find (lScoreTypeEnum);
127 
128  if (itScore != _scoreMap.end()) {
129  // Just replace the score value
130  Score_T& lScore = itScore->second;
131  lScore = oScore;
132 
133  } else {
134  // Insert the score value for that new type
135  const bool insertSucceeded =
136  _scoreMap.insert (ScoreMap_T::value_type (lScoreTypeEnum,
137  oScore)).second;
138 
139  // Sanity check
140  if (insertSucceeded == false) {
141  OPENTREP_LOG_ERROR ("The " << iScore << " score value can not be "
142  << "inserted in the dedicated list for the "
143  << iScoreType.describe() << " score type");
144  }
145  assert (insertSucceeded == true);
146  }
147  }
148 
149  // //////////////////////////////////////////////////////////////////////
150  std::string ScoreBoard::describeKey() const {
151  std::ostringstream oStr;
152  oStr << _queryString;
153  return oStr.str();
154  }
155 
156  // //////////////////////////////////////////////////////////////////////
157  std::string ScoreBoard::describe() const {
158  std::ostringstream oStr;
159  oStr << describeKey() << " - ";
160 
161  unsigned short idx = 0;
162  for (ScoreMap_T::const_iterator itScore = _scoreMap.begin();
163  itScore != _scoreMap.end(); ++itScore, ++idx) {
164  if (idx != 0) {
165  oStr << ", ";
166  }
167  const ScoreType::EN_ScoreType& lScoreType = itScore->first;
168  const Score_T& lScore = itScore->second;
169  oStr << ScoreType::getTypeLabelAsString (lScoreType) << ": "
170  << lScore << "%";
171  }
172 
173  return oStr.str();
174  }
175 
176  // //////////////////////////////////////////////////////////////////////
177  void ScoreBoard::toStream (std::ostream& ioOut) const {
178  ioOut << describe();
179  }
180 
181  // //////////////////////////////////////////////////////////////////////
182  void ScoreBoard::fromStream (std::istream& ioIn) {
183  }
184 
185  // //////////////////////////////////////////////////////////////////////
187  Percentage_T oPercentage = 100.0;
188 
189  // Browse the registered scores
190  for (ScoreMap_T::iterator itScore = _scoreMap.begin();
191  itScore != _scoreMap.end(); ++itScore) {
192  const ScoreType::EN_ScoreType& lScoreType = itScore->first;
193  Score_T& lScore = itScore->second;
194 
200  const bool isIndividual = ScoreType::isIndividualScore (lScoreType);
201  if (isIndividual == true) {
202  oPercentage *= lScore / 100.0;
203  }
204 
212  }
213 
214  // Register the combined score
215  setCombinedWeight (oPercentage);
216 
217  //
218  return oPercentage;
219  }
220 
221 }
void toStream(std::ostream &) const
Definition: ScoreBoard.cpp:177
Structure holding a board for all the types of score/matching having been performed.
Definition: ScoreBoard.hpp:22
void setCombinedWeight(const Score_T &iScore)
Definition: ScoreBoard.hpp:103
#define OPENTREP_LOG_ERROR(iToBeLogged)
Definition: Logger.hpp:23
Score_T getScore(const ScoreType &) const
Definition: ScoreBoard.cpp:40
double Percentage_T
double Score_T
bool isIndividualScore() const
Definition: ScoreType.cpp:104
ScoreBoard(const TravelQuery_T &, const ScoreType &, const Score_T &)
Definition: ScoreBoard.cpp:28
Percentage_T calculateCombinedWeight()
Definition: ScoreBoard.cpp:186
std::string describeKey() const
Definition: ScoreBoard.cpp:150
bool AlmostEquals(const FloatingPoint &rhs) const
const Percentage_T K_DEFAULT_MODIFIED_MATCHING_PCT
const Percentage_T K_DEFAULT_ENVELOPE_PCT
std::string describe() const
Definition: ScoreType.cpp:97
const Percentage_T K_DEFAULT_FULL_CODE_MATCH_PCT
EN_ScoreType getType() const
Definition: ScoreType.cpp:85
static std::string getTypeLabelAsString(const EN_ScoreType &)
Definition: ScoreType.cpp:66
Enumeration of score types.
Definition: ScoreType.hpp:25
void setScore(const ScoreType &, const Score_T &)
Definition: ScoreBoard.cpp:54
std::string describe() const
Definition: ScoreBoard.cpp:157
std::string TravelQuery_T
void fromStream(std::istream &)
Definition: ScoreBoard.cpp:182