OpenTREP Logo  0.07.9
C++ Open Travel Request Parsing Library
StringPartition.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 #include <sstream>
7 #include <set>
8 // OpenTrep
11 #include <opentrep/bom/Filter.hpp>
14 
15 namespace OPENTREP {
16 
17  // //////////////////////////////////////////////////////////////////////
18  StringPartition::StringPartition (const std::string& iString)
19  : _initialString (iString) {
20  init (iString);
21  }
22 
23  // //////////////////////////////////////////////////////////////////////
25  }
26 
27  // //////////////////////////////////////////////////////////////////////
28  void StringPartition::push_back (const StringSet& iStringSet) {
29  if (iStringSet.empty() == false) {
30  _partition.push_back (iStringSet);
31  }
32  }
33 
34  // //////////////////////////////////////////////////////////////////////
35  size_t StringPartition::size() const {
36  return _partition.size();
37  }
38 
39  // //////////////////////////////////////////////////////////////////////
40  bool StringPartition::empty() const {
41  return _partition.empty();
42  }
43 
44  // //////////////////////////////////////////////////////////////////////
46  _partition.clear();
47  }
48 
49  // //////////////////////////////////////////////////////////////////////
50  std::string StringPartition::describeKey() const {
51  std::ostringstream oStr;
52  oStr << "";
53  return oStr.str();
54  }
55 
56  // //////////////////////////////////////////////////////////////////////
57  std::string StringPartition::describe() const {
58  std::ostringstream oStr;
59  oStr << describeKey();
60 
61  //
62  oStr << "{";
63 
64  short idx_sublist = 0;
65  for (StringPartition_T::const_iterator itSet = _partition.begin();
66  itSet != _partition.end(); ++itSet, ++idx_sublist) {
67  //
68  if (idx_sublist != 0) {
69  oStr << ", ";
70  }
71 
72  //
73  const StringSet& lStringSet = *itSet;
74 
75  //
76  oStr << lStringSet;
77  }
78 
79  //
80  oStr << " }";
81 
82  return oStr.str();
83  }
84 
85  // //////////////////////////////////////////////////////////////////////
86  void StringPartition::toStream (std::ostream& ioOut) const {
87  ioOut << describe();
88  }
89 
90  // //////////////////////////////////////////////////////////////////////
91  void StringPartition::fromStream (std::istream& ioIn) {
92  }
93 
94  // //////////////////////////////////////////////////////////////////////
95  void StringPartition::init (const std::string& iPhrase) {
100  WordList_T lWordList;
101  tokeniseStringIntoWordList (iPhrase, lWordList);
102  NbOfWords_T nbOfWords = lWordList.size();
103 
117  const std::string lPhrase =
118  createStringFromWordList (lWordList,
120 
126  // DEBUG
127  OPENTREP_LOG_DEBUG ("The initial string ('" << iPhrase << "') has got "
128  << nbOfWords << " words, and will be strip down to "
130  << " words, giving: ':" << lPhrase << "'");
131 
132  // Rebuild the list of words
133  tokeniseStringIntoWordList (lPhrase, lWordList);
134 
135  // Recalculate the number of words
136  nbOfWords = lWordList.size();
137 
138  // The number of wors must be, by construction, the maximum number of words
139  assert (nbOfWords == K_DEFAULT_MAXIMUM_NUMBER_OF_WORDS_IN_STRING);
140  }
141 
146  StringSet oStringSet (lPhrase);
147 
151  if (nbOfWords <= 1) {
152  _partition.push_back (oStringSet);
153  return;
154  }
155 
159  for (NbOfWords_T idx_word = 1; idx_word != nbOfWords; ++idx_word) {
160  // 1.1. Create a sub-string copy of the first idx_word
161  const std::string& lLeftHandString = createStringFromWordList (lWordList,
162  idx_word);
163 
164  // DEBUG
165  /*
166  OPENTREP_LOG_DEBUG ("[" << lPhrase << ", " << idx_word
167  << "] Left-hand string: '" << lLeftHandString << "'");
168  */
169 
173  const std::string& lRightHandString = createStringFromWordList (lWordList,
174  idx_word,
175  false);
176 
177  // DEBUG
178  // std::cout << "[" << lPhrase << ", " << idx_word
179  // << "] Right-hand string: '" << lRightHandString << "'"
180  // << std::endl;
181 
186  StringPartition lStringPartition (lRightHandString);
187 
192  const StringPartition_T& lStringRHSPartition= lStringPartition._partition;
193  for (StringPartition_T::const_iterator itSet= lStringRHSPartition.begin();
194  itSet != lStringRHSPartition.end(); ++itSet) {
195  const StringSet& lRHSStringSet = *itSet;
196 
203  StringSet lNewStringSet;
204 
205  // Add the string to the list
206  lNewStringSet.push_back (lLeftHandString);
207 
208  //
209  lNewStringSet.push_back (lRHSStringSet);
210 
211  //
212  _partition.push_back (lNewStringSet);
213  }
214  }
215 
216 
222  _partition.push_back (oStringSet);
223  }
224 
225  // //////////////////////////////////////////////////////////////////////
227  StringSet oStringSet;
228 
229  // Set of unique strings
230  WordSet_T lStringList;
231 
232  // Browse all the word combinations. Then, for every word combination,
233  // add it if not already in the list (STD set) of strings.
234  for (StringPartition_T::const_iterator itSet = _partition.begin();
235  itSet != _partition.end(); ++itSet) {
236  const StringSet& itStringSet = *itSet;
237 
238  const StringSet::StringSet_T& lStringSet = itStringSet._set;
239  for (StringSet::StringSet_T::const_iterator itString = lStringSet.begin();
240  itString != lStringSet.end(); ++itString) {
241  const std::string& lWordCombination = *itString;
242 
243  // Check whether that word combination has already been stored once.
244  WordSet_T::const_iterator itExistingString =
245  lStringList.find (lWordCombination);
246  if (itExistingString == lStringList.end()) {
247  // If not, add it to the dedicated list (STD set).
248  lStringList.insert (lWordCombination);
249  }
250  }
251  }
252 
253  // Convert the STD set into a StringSet structure
254  for (WordSet_T::const_iterator itString = lStringList.begin();
255  itString != lStringList.end(); ++itString) {
256  const std::string& lWordCombination = *itString;
257  oStringSet.push_back (lWordCombination);
258  }
259 
260  //
261  return oStringSet;
262  }
263 
264 }
#define OPENTREP_LOG_DEBUG(iToBeLogged)
Definition: Logger.hpp:33
std::list< Word_T > WordList_T
void tokeniseStringIntoWordList(const std::string &iPhrase, WordList_T &ioWordList)
Definition: Utilities.cpp:19
std::string createStringFromWordList(const WordList_T &iWordList, const NbOfWords_T iSplitIdx, const bool iFromBeginningFlag)
Definition: Utilities.cpp:43
const NbOfWords_T K_DEFAULT_MAXIMUM_NUMBER_OF_WORDS_IN_STRING
std::set< std::string > WordSet_T
unsigned short NbOfWords_T
std::string describe() const
void toStream(std::ostream &ioOut) const
void push_back(const StringSet &iStringSet)
StringSet calculateUniqueCombinations() const
std::list< StringSet > StringPartition_T
void fromStream(std::istream &ioIn)
StringPartition_T _partition
StringPartition(const std::string &iStringToBePartitioned)
std::string describeKey() const
Class holding a set of strings, e.g., {"rio", "de", "janeiro"}.
Definition: StringSet.hpp:19
bool empty() const
Definition: StringSet.cpp:39
StringSet_T _set
Definition: StringSet.hpp:118
void push_back(const std::string &)
Definition: StringSet.cpp:49
std::list< std::string > StringSet_T
Definition: StringSet.hpp:25