Advertisement
35657

Untitled

Nov 9th, 2022
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.11 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<vector<Token>> SplitIntoSentences(vector<Token> tokens) {
  28.     auto begin = tokens.begin();
  29.     vector<vector<Token>> v;
  30.     while (begin != tokens.end()) {
  31.         vector<Token> temp;
  32.         auto it = FindSentenceEnd(begin, tokens.end());
  33.         for (auto i = begin; i != it; ++i){
  34.             temp.push_back(move(*i));
  35.         }
  36.         v.push_back(move(temp));
  37.         begin = it;
  38.     }
  39.     return v;
  40. }
  41.  
  42. struct TestToken {
  43.     string data;
  44.     bool is_end_sentence_punctuation = false;
  45.  
  46.     bool IsEndSentencePunctuation() const {
  47.         return is_end_sentence_punctuation;
  48.     }
  49.     bool operator==(const TestToken& other) const {
  50.         return data == other.data && is_end_sentence_punctuation == other.is_end_sentence_punctuation;
  51.     }
  52. };
  53.  
  54. ostream& operator<<(ostream& stream, const TestToken& token) {
  55.     return stream << token.data;
  56. }
  57.  
  58. // Тест содержит копирования объектов класса TestToken.
  59. // Для проверки отсутствия копирований в функции SplitIntoSentences
  60. // необходимо написать отдельный тест.
  61. void TestSplitting() {
  62.     assert(SplitIntoSentences(vector<TestToken>({{"Split"s}, {"into"s}, {"sentences"s}, {"!"s}}))
  63.            == vector<Sentence<TestToken>>({{{"Split"s}, {"into"s}, {"sentences"s}, {"!"s}}}));
  64.  
  65.     assert(SplitIntoSentences(vector<TestToken>({{"Split"s}, {"into"s}, {"sentences"s}, {"!"s, true}}))
  66.            == vector<Sentence<TestToken>>({{{"Split"s}, {"into"s}, {"sentences"s}, {"!"s, true}}}));
  67.  
  68.     assert(SplitIntoSentences(vector<TestToken>(
  69.                {{"Split"s}, {"into"s}, {"sentences"s}, {"!"s, true}, {"!"s, true}, {"Without"s}, {"copies"s}, {"."s, true}}))
  70.            == vector<Sentence<TestToken>>({
  71.                {{"Split"s}, {"into"s}, {"sentences"s}, {"!"s, true}, {"!"s, true}},
  72.                {{"Without"s}, {"copies"s}, {"."s, true}},
  73.            }));
  74. }
  75.  
  76. int main() {
  77.     TestSplitting();
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement