Guest User

Untitled

a guest
May 18th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <boost/utility/string_view.hpp>
  5.  
  6.  
  7. class TokenizerExample {
  8.  public:
  9.   boost::string_view operator()(boost::string_view& str,
  10.                                 boost::string_view delimiter) const {
  11.     boost::string_view retval;
  12.  
  13.     while (retval.empty()) {
  14.       std::size_t pos = str.find_first_of(delimiter);
  15.  
  16.       if (pos == str.npos) {
  17.         retval = str;
  18.         str.clear();
  19.         return retval;
  20.       }
  21.  
  22.       retval = str.substr(0, pos);
  23.  
  24.       str.remove_prefix(pos + 1);
  25.     }
  26.  
  27.     return retval;
  28.   }
  29. };
  30.  
  31. template<typename TokenizerType>
  32. std::vector<std::size_t> Encode(const std::string& str,
  33.                                 const std::string& delimiter,
  34.                                 TokenizerType tokenizer) {
  35.   boost::string_view strView(str);
  36.   boost::string_view delimiterView(delimiter);
  37.   boost::string_view token;
  38.   std::map<boost::string_view, std::size_t> dict;
  39.   std::vector<std::size_t> result;
  40.  
  41.   token = tokenizer(strView, delimiterView);
  42.  
  43.   std::size_t numLabels = 0;
  44.   while (!token.empty()) {
  45.     if (dict.find(token) == dict.end()) {
  46.       dict[token] = numLabels++;
  47.     }
  48.  
  49.     result.push_back(dict.at(token));
  50.  
  51.     token = tokenizer(strView, delimiterView);
  52.   }
  53.  
  54.   return result;
  55. }
  56.  
  57.  
  58. int main()
  59. {
  60.   std::string tmp = "abc def  abc   \t abced  def";
  61.   std::vector<std::size_t> result = Encode(tmp, " \t\n", TokenizerExample());
  62.  
  63.   for (auto item : result)
  64.     std::cout << item << std::endl;
  65.  
  66.   std::cout << "Example #2" << std::endl;
  67.  
  68.   std::vector<std::size_t> result2 =
  69.       Encode(tmp, " \t\n", [](boost::string_view& str,
  70.                               boost::string_view ) {
  71.       if (str.empty())
  72.         return str;
  73.  
  74.       boost::string_view retval = str.substr(0, 1);
  75.  
  76.       str.remove_prefix(1);
  77.  
  78.       return retval;
  79.   });
  80.  
  81.   for (auto item : result2)
  82.     std::cout << item << std::endl;
  83.  
  84.  
  85.   return 0;
  86. }
Add Comment
Please, Sign In to add comment