| 333 | } |
| 334 | |
| 335 | std::string normalize_chatterbox_tts_text(const std::string & text) { |
| 336 | if (text.empty()) { |
| 337 | return "You need to add some text for me to talk."; |
| 338 | } |
| 339 | |
| 340 | std::string normalized = text; |
| 341 | if (!normalized.empty() && std::islower(static_cast<unsigned char>(normalized.front())) != 0) { |
| 342 | normalized.front() = static_cast<char>(std::toupper(static_cast<unsigned char>(normalized.front()))); |
| 343 | } |
| 344 | |
| 345 | std::string compact; |
| 346 | compact.reserve(normalized.size()); |
| 347 | bool previous_space = false; |
| 348 | for (char ch : normalized) { |
| 349 | if (std::isspace(static_cast<unsigned char>(ch)) != 0) { |
| 350 | if (!previous_space) { |
| 351 | compact.push_back(' '); |
| 352 | previous_space = true; |
| 353 | } |
| 354 | continue; |
| 355 | } |
| 356 | compact.push_back(ch); |
| 357 | previous_space = false; |
| 358 | } |
| 359 | normalized = trim_spaces(std::move(compact)); |
| 360 | |
| 361 | const std::vector<std::pair<std::string, std::string>> replacements = { |
| 362 | {"...", ", "}, |
| 363 | {"…", ", "}, |
| 364 | {":", ","}, |
| 365 | {" - ", ", "}, |
| 366 | {";", ", "}, |
| 367 | {"—", "-"}, |
| 368 | {"–", "-"}, |
| 369 | {" ,", ","}, |
| 370 | {"“", "\""}, |
| 371 | {"”", "\""}, |
| 372 | {"‘", "'"}, |
| 373 | {"’", "'"}, |
| 374 | }; |
| 375 | for (const auto & [old_text, new_text] : replacements) { |
| 376 | size_t search = 0; |
| 377 | while ((search = normalized.find(old_text, search)) != std::string::npos) { |
| 378 | normalized.replace(search, old_text.size(), new_text); |
| 379 | search += new_text.size(); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | normalized = trim_spaces(std::move(normalized)); |
| 384 | const std::vector<std::string> sentence_enders = {".", "!", "?", "-", ",", "、", ",", "。", "?", "!"}; |
| 385 | const bool has_sentence_ender = std::any_of( |
| 386 | sentence_enders.begin(), |
| 387 | sentence_enders.end(), |
| 388 | [&](const std::string & ending) { |
| 389 | return normalized.size() >= ending.size() && |
| 390 | normalized.compare(normalized.size() - ending.size(), ending.size(), ending) == 0; |
| 391 | }); |
| 392 | if (normalized.empty() || !has_sentence_ender) { |
no test coverage detected