shizaero

Untitled

Aug 23rd, 2023
961
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include <algorithm>
  2. #include <cassert>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. // Объявляем Sentence<Token> для произвольного типа Token
  9. // синонимом vector<Token>.
  10. // Благодаря этому в качестве возвращаемого значения
  11. // функции можно указать не малопонятный вектор векторов,
  12. // а вектор предложений — vector<Sentence<Token>>.
  13. template <typename Token>
  14. using Sentence = vector<Token>;
  15.  
  16. template <typename TokenForwardIt>
  17. TokenForwardIt FindSentenceEnd(TokenForwardIt tokens_begin, TokenForwardIt tokens_end) {
  18.     const TokenForwardIt before_sentence_end
  19.         = adjacent_find(tokens_begin, tokens_end, [](const auto& left_token, const auto& right_token) {
  20.               return left_token.IsEndSentencePunctuation() && !right_token.IsEndSentencePunctuation();
  21.           });
  22.     return before_sentence_end == tokens_end ? tokens_end : next(before_sentence_end);
  23. }
  24.  
  25. // Класс Token имеет метод bool IsEndSentencePunctuation() const
  26. template <typename Token>
  27. vector<Sentence<Token>> SplitIntoSentences(vector<Token> tokens) {
  28.     // Напишите реализацию функции, не копируя объекты типа Token
  29.     vector<Sentence<Token>> pool;
  30.     auto it_1 = tokens.begin();
  31.     auto it_2 = tokens.begin();
  32.     size_t sentence_counter = 0;
  33.    
  34.     while (it_1 < tokens.end()){
  35.         it_2 = FindSentenceEnd(tokens.begin(), tokens.end());
  36.         for( ;it_1 <= it_2; ++it_1){
  37.             pool.at(sentence_counter).push_back(move(*it_1));
  38.         }
  39.         ++sentence_counter;
  40.     }
  41.    
  42.     return pool;
  43. }
  44.  
  45. struct TestToken {
  46.     string data;
  47.     bool is_end_sentence_punctuation = false;
  48.  
  49.     bool IsEndSentencePunctuation() const {
  50.         return is_end_sentence_punctuation;
  51.     }
  52.     bool operator==(const TestToken& other) const {
  53.         return data == other.data && is_end_sentence_punctuation == other.is_end_sentence_punctuation;
  54.     }
  55. };
  56.  
  57. ostream& operator<<(ostream& stream, const TestToken& token) {
  58.     return stream << token.data;
  59. }
  60.  
  61. // Тест содержит копирования объектов класса TestToken.
  62. // Для проверки отсутствия копирований в функции SplitIntoSentences
  63. // необходимо написать отдельный тест.
  64. void TestSplitting() {
  65.     assert(SplitIntoSentences(vector<TestToken>({{"Split"s}, {"into"s}, {"sentences"s}, {"!"s}}))
  66.            == vector<Sentence<TestToken>>({{{"Split"s}, {"into"s}, {"sentences"s}, {"!"s}}}));
  67.  
  68.     assert(SplitIntoSentences(vector<TestToken>({{"Split"s}, {"into"s}, {"sentences"s}, {"!"s, true}}))
  69.            == vector<Sentence<TestToken>>({{{"Split"s}, {"into"s}, {"sentences"s}, {"!"s, true}}}));
  70.  
  71.     assert(SplitIntoSentences(vector<TestToken>(
  72.                {{"Split"s}, {"into"s}, {"sentences"s}, {"!"s, true}, {"!"s, true}, {"Without"s}, {"copies"s}, {"."s, true}}))
  73.            == vector<Sentence<TestToken>>({
  74.                {{"Split"s}, {"into"s}, {"sentences"s}, {"!"s, true}, {"!"s, true}},
  75.                {{"Without"s}, {"copies"s}, {"."s, true}},
  76.            }));
  77. }
  78.  
  79. int main() {
  80.     TestSplitting();
  81. }
Advertisement
Add Comment
Please, Sign In to add comment