OpenTREP Logo  0.6.0
C++ Open Travel Request Parsing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
10 #include <opentrep/bom/Filter.hpp>
13 
14 namespace OPENTREP {
15 
16  // //////////////////////////////////////////////////////////////////////
17  StringPartition::StringPartition (const std::string& iString)
18  : _initialString (iString) {
19  init (iString);
20  }
21 
22  // //////////////////////////////////////////////////////////////////////
24  }
25 
26  // //////////////////////////////////////////////////////////////////////
27  void StringPartition::push_back (const StringSet& iStringSet) {
28  if (iStringSet.empty() == false) {
29  _partition.push_back (iStringSet);
30  }
31  }
32 
33  // //////////////////////////////////////////////////////////////////////
34  size_t StringPartition::size() const {
35  return _partition.size();
36  }
37 
38  // //////////////////////////////////////////////////////////////////////
39  bool StringPartition::empty() const {
40  return _partition.empty();
41  }
42 
43  // //////////////////////////////////////////////////////////////////////
45  _partition.clear();
46  }
47 
48  // //////////////////////////////////////////////////////////////////////
49  std::string StringPartition::describeKey() const {
50  std::ostringstream oStr;
51  oStr << "";
52  return oStr.str();
53  }
54 
55  // //////////////////////////////////////////////////////////////////////
56  std::string StringPartition::describe() const {
57  std::ostringstream oStr;
58  oStr << describeKey();
59 
60  //
61  oStr << "{";
62 
63  short idx_sublist = 0;
64  for (StringPartition_T::const_iterator itSet = _partition.begin();
65  itSet != _partition.end(); ++itSet, ++idx_sublist) {
66  //
67  if (idx_sublist != 0) {
68  oStr << ", ";
69  }
70 
71  //
72  const StringSet& lStringSet = *itSet;
73 
74  //
75  oStr << lStringSet;
76  }
77 
78  //
79  oStr << " }";
80 
81  return oStr.str();
82  }
83 
84  // //////////////////////////////////////////////////////////////////////
85  void StringPartition::toStream (std::ostream& ioOut) const {
86  ioOut << describe();
87  }
88 
89  // //////////////////////////////////////////////////////////////////////
90  void StringPartition::fromStream (std::istream& ioIn) {
91  }
92 
93  // //////////////////////////////////////////////////////////////////////
94  void StringPartition::init (const std::string& iPhrase) {
95  // 0. Initialisation
96  // 0.1. Initialisation of the tokenizer
97  WordList_T lWordList;
98  tokeniseStringIntoWordList (iPhrase, lWordList);
99  const short nbOfWords = lWordList.size();
100 
101  // 0.2. Re-create the initial phrase, without any (potential) seperator
102  const std::string lPhrase = createStringFromWordList (lWordList);
103 
104  // 0.3. Create the list with a single sub-list, itself containing only
105  // the given input string.
106  StringSet oStringSet (lPhrase);
107 
108  // 0.4. If the string contains no more than one word, the job is finished.
109  if (nbOfWords <= 1) {
110  _partition.push_back (oStringSet);
111  return;
112  }
113 
114  // 1. Iteration on all the words of the given string, from 1 to nbOfWords-1
115  for (short idx_word = 1; idx_word != nbOfWords; ++idx_word) {
116  // 1.1. Create a sub-string copy of the first idx_word
117  const std::string& lLeftHandString = createStringFromWordList (lWordList,
118  idx_word);
119 
120  // DEBUG
121  // std::cout << "[" << lPhrase << ", " << idx_word
122  // << "] Left-hand string: '" << lLeftHandString << "'"
123  // << std::endl;
124 
125  // 1.2. Create another sub-string with the remaining of the string
126  const std::string& lRightHandString = createStringFromWordList (lWordList,
127  idx_word,
128  false);
129 
130  // DEBUG
131  // std::cout << "[" << lPhrase << ", " << idx_word
132  // << "] Right-hand string: '" << lRightHandString << "'"
133  // << std::endl;
134 
135  // 1.3. Recurse
136  // 1.3.1. Call the Partition algorithm on the right-hand side string
137  StringPartition lStringPartition (lRightHandString);
138 
139  // 1.3.2. Extract the sub-lists of strings, and add them to the current
140  // sub-lists.
141  const StringPartition_T& lStringRHSPartition= lStringPartition._partition;
142  for (StringPartition_T::const_iterator itSet= lStringRHSPartition.begin();
143  itSet != lStringRHSPartition.end(); ++itSet) {
144  const StringSet& lRHSStringSet = *itSet;
145 
146  // Create the sub-list which will accommodate:
147  // - The left-hand-side sub-string.
148  // - The the sub-list having been generated by the recursion on the
149  // the right-hand-side sub-string.
150  StringSet lNewStringSet;
151 
152  // Add the string to the list
153  lNewStringSet.push_back (lLeftHandString);
154 
155  //
156  lNewStringSet.push_back (lRHSStringSet);
157 
158  //
159  _partition.push_back (lNewStringSet);
160  }
161  }
162 
163 
164  // 2.
165  // 2.1. Add the sub-list with the full string (the one given as input) to
166  // the back of the list.
167  _partition.push_back (oStringSet);
168  }
169 
170  // //////////////////////////////////////////////////////////////////////
172  StringSet oStringSet;
173 
174  // Set of unique strings
175  WordSet_T lStringList;
176 
177  // Browse all the word combinations. Then, for every word combination,
178  // add it if not already in the list (STD set) of strings.
179  for (StringPartition_T::const_iterator itSet = _partition.begin();
180  itSet != _partition.end(); ++itSet) {
181  const StringSet& itStringSet = *itSet;
182 
183  const StringSet::StringSet_T& lStringSet = itStringSet._set;
184  for (StringSet::StringSet_T::const_iterator itString = lStringSet.begin();
185  itString != lStringSet.end(); ++itString) {
186  const std::string& lWordCombination = *itString;
187 
188  // Check whether that word combination has already been stored once.
189  WordSet_T::const_iterator itExistingString = lStringList.find (lWordCombination);
190  if (itExistingString == lStringList.end()) {
191  // If not, add it to the dedicated list (STD set).
192  lStringList.insert (lWordCombination);
193  }
194  }
195  }
196 
197  // Convert the STD set into a StringSet structure
198  for (WordSet_T::const_iterator itString = lStringList.begin();
199  itString != lStringList.end(); ++itString) {
200  const std::string& lWordCombination = *itString;
201  oStringSet.push_back (lWordCombination);
202  }
203 
204  //
205  return oStringSet;
206  }
207 
208 }
StringSet_T _set
Definition: StringSet.hpp:118
std::string createStringFromWordList(const WordList_T &iWordList, const unsigned short iSplitIdx, const bool iFromBeginningFlag)
Definition: Utilities.cpp:38
std::list< StringSet > StringPartition_T
void toStream(std::ostream &ioOut) const
std::list< Word_T > WordList_T
bool empty() const
Definition: StringSet.cpp:39
std::set< std::string > WordSet_T
std::string describe() const
void tokeniseStringIntoWordList(const std::string &iPhrase, WordList_T &ioWordList)
Definition: Utilities.cpp:16
StringPartition_T _partition
void push_back(const StringSet &iStringSet)
std::string describeKey() const
Class holding a set of strings, e.g., {"rio", "de", "janeiro"}.
Definition: StringSet.hpp:19
StringSet calculateUniqueCombinations() const
void fromStream(std::istream &ioIn)
StringPartition(const std::string &iStringToBePartitioned)
void push_back(const std::string &)
Definition: StringSet.cpp:49
std::list< std::string > StringSet_T
Definition: StringSet.hpp:25