shushus

Спринт 8. Тайные техники передачи аргументов.

Feb 2nd, 2023
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cassert>
  3. #include <string>
  4. #include <vector>
  5. #include <utility>
  6.  
  7. using namespace std;
  8.  
  9. template <typename Token>
  10. using Sentence = vector<Token>;
  11.  
  12. template <typename TokenForwardIt>
  13. TokenForwardIt FindSentenceEnd(TokenForwardIt tokens_begin, TokenForwardIt tokens_end) {
  14.     const TokenForwardIt before_sentence_end
  15.         = adjacent_find(tokens_begin, tokens_end, [](const auto& left_token, const auto& right_token) {
  16.               return left_token.IsEndSentencePunctuation() && !right_token.IsEndSentencePunctuation();
  17.           });
  18.     return before_sentence_end == tokens_end ? tokens_end : next(before_sentence_end);
  19. }
  20.  
  21. template <typename Token>
  22. vector<Sentence<Token>> SplitIntoSentences(vector<Token> tokens) {
  23.     vector<Sentence<Token>> vector_helper; Sentence<Token> helper_part;
  24.     for (auto begin = tokens.begin();;){
  25.         auto end = FindSentenceEnd(begin, tokens.end());
  26.         move(begin, end, std::back_inserter(helper_part));
  27.         vector_helper.push_back(move(helper_part));
  28.         if (end == tokens.end()){
  29.             break;
  30.         }
  31.         begin = end;
  32.     }
  33.     return vector_helper;
  34. }
  35.  
  36. struct TestToken {
  37.     string data;
  38.     bool is_end_sentence_punctuation = false;
  39.  
  40.     bool IsEndSentencePunctuation() const {
  41.         return is_end_sentence_punctuation;
  42.     }
  43.     bool operator==(const TestToken& other) const {
  44.         return data == other.data && is_end_sentence_punctuation == other.is_end_sentence_punctuation;
  45.     }
  46. };
  47.  
  48. ostream& operator<<(ostream& stream, const TestToken& token) {
  49.     return stream << token.data;
  50. }
  51.  
  52. void TestSplitting() {
  53.     assert(SplitIntoSentences(vector<TestToken>({{"Split"s}, {"into"s}, {"sentences"s}, {"!"s}}))
  54.            == vector<Sentence<TestToken>>({{{"Split"s}, {"into"s}, {"sentences"s}, {"!"s}}}));
  55.  
  56.     assert(SplitIntoSentences(vector<TestToken>({{"Split"s}, {"into"s}, {"sentences"s}, {"!"s, true}}))
  57.            == vector<Sentence<TestToken>>({{{"Split"s}, {"into"s}, {"sentences"s}, {"!"s, true}}}));
  58.  
  59.     assert(SplitIntoSentences(vector<TestToken>(
  60.                {{"Split"s}, {"into"s}, {"sentences"s}, {"!"s, true}, {"!"s, true}, {"Without"s}, {"copies"s}, {"."s, true}}))
  61.            == vector<Sentence<TestToken>>({
  62.                {{"Split"s}, {"into"s}, {"sentences"s}, {"!"s, true}, {"!"s, true}},
  63.                {{"Without"s}, {"copies"s}, {"."s, true}},
  64.            }));
  65. }
  66.  
  67. int main() {
  68.     TestSplitting();
  69. }
Advertisement
Add Comment
Please, Sign In to add comment