OpenTREP Logo  0.6.0
C++ Open Travel Request Parsing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Utilities.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 #include <sstream>
7 // Boost (Extended STL)
8 #include <boost/tokenizer.hpp>
9 // OpenTrep
12 
13 namespace OPENTREP {
14 
15  // //////////////////////////////////////////////////////////////////////
16  void tokeniseStringIntoWordList (const std::string& iPhrase,
17  WordList_T& ioWordList) {
18  // Empty the word list
19  ioWordList.clear();
20 
21  // Boost Tokeniser
22  typedef boost::tokenizer<boost::char_separator<char> > Tokeniser_T;
23 
24  // Define the separators
25  const boost::char_separator<char>
26  lSepatorList(" .,;:|+-*/_=!@#$%`~^&(){}[]?'<>\"");
27 
28  // Initialise the phrase to be tokenised
29  Tokeniser_T lTokens (iPhrase, lSepatorList);
30  for (Tokeniser_T::const_iterator tok_iter = lTokens.begin();
31  tok_iter != lTokens.end(); ++tok_iter) {
32  const std::string& lTerm = *tok_iter;
33  ioWordList.push_back (lTerm);
34  }
35  }
36 
37  // //////////////////////////////////////////////////////////////////////
38  std::string createStringFromWordList (const WordList_T& iWordList,
39  const unsigned short iSplitIdx,
40  const bool iFromBeginningFlag) {
41  std::ostringstream oStr;
42 
43  // Browse the left-hand side of the string
44  unsigned short idx = 0;
45  WordList_T::const_iterator itWord = iWordList.begin();
46  for ( ; itWord != iWordList.end(); ++itWord, ++idx) {
47 
48  if (iFromBeginningFlag == true) {
49  // The beginning of the word list is needed
50 
51  // Check whether the sub-list has reached the expected split point,
52  // if any
53  if (iSplitIdx != 0 && idx >= iSplitIdx) {
54  break;
55  }
56 
57  //
58  if (idx > 0) {
59  oStr << " ";
60  }
61  //
62  const std::string& lWord = *itWord;
63  oStr << lWord;
64 
65  } else {
66  // The end of the word list is needed
67 
68  // Check whether the sub-list has reached the expected split point,
69  // if any
70  if (iSplitIdx == 0 || idx >= iSplitIdx) {
71  break;
72  }
73  }
74  }
75 
76  // The beginning of the word list is needed
77  if (iFromBeginningFlag == true) {
78  return oStr.str();
79  }
80 
81  // The end of the word list is needed
82  assert (iFromBeginningFlag == false);
83 
84  // Browse the right-hand side of the string
85  for ( ; itWord != iWordList.end(); ++itWord, ++idx) {
86  // The end of the word list is needed
87 
88  //
89  if (idx > iSplitIdx) {
90  oStr << " ";
91  }
92  //
93  const std::string& lWord = *itWord;
94  oStr << lWord;
95  }
96 
97  return oStr.str();
98  }
99 
100 }
std::string createStringFromWordList(const WordList_T &iWordList, const unsigned short iSplitIdx, const bool iFromBeginningFlag)
Definition: Utilities.cpp:38
std::list< Word_T > WordList_T
void tokeniseStringIntoWordList(const std::string &iPhrase, WordList_T &ioWordList)
Definition: Utilities.cpp:16