Commit ·
145d38e
1
Parent(s): bba93b5
Upload 2 files
Browse files- ChatIPC.cpp +138 -11
ChatIPC.cpp
CHANGED
|
@@ -359,13 +359,107 @@ static std::optional<std::string> get_valid_base_form(const std::string &word) {
|
|
| 359 |
}
|
| 360 |
}
|
| 361 |
|
| 362 |
-
// 4.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
if (word.length() > 3 && word.substr(word.length() - 3) == "ies") {
|
| 364 |
std::string base = word.substr(0, word.length() - 3) + "y";
|
| 365 |
if (check_dictionary(base)) return base;
|
| 366 |
}
|
| 367 |
|
| 368 |
-
//
|
| 369 |
if (word.length() > 3 && word.substr(word.length() - 3) == "ves") {
|
| 370 |
std::string base1 = word.substr(0, word.length() - 3) + "f";
|
| 371 |
if (check_dictionary(base1)) return base1;
|
|
@@ -374,13 +468,13 @@ static std::optional<std::string> get_valid_base_form(const std::string &word) {
|
|
| 374 |
if (check_dictionary(base2)) return base2;
|
| 375 |
}
|
| 376 |
|
| 377 |
-
//
|
| 378 |
if (word.length() > 2 && word.substr(word.length() - 2) == "es") {
|
| 379 |
std::string base = word.substr(0, word.length() - 2);
|
| 380 |
if (check_dictionary(base)) return base;
|
| 381 |
}
|
| 382 |
|
| 383 |
-
//
|
| 384 |
if (word.length() > 1 && word.back() == 's' && word[word.length() - 2] != 's') {
|
| 385 |
std::string base = word.substr(0, word.length() - 1);
|
| 386 |
if (check_dictionary(base)) return base;
|
|
@@ -545,19 +639,52 @@ static void build_def_tokens_cache(){
|
|
| 545 |
global_pos_cache.reserve(global_dictionary_entries.size());
|
| 546 |
|
| 547 |
for (const auto &entry : global_dictionary_entries){
|
| 548 |
-
|
| 549 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 550 |
|
| 551 |
std::string pos = normalize_pos_tag(entry.pos);
|
| 552 |
-
if (!pos.empty()) global_pos_cache[key].push_back(std::move(pos));
|
| 553 |
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 558 |
}
|
| 559 |
}
|
| 560 |
|
|
|
|
| 561 |
for (auto &pr : global_def_tokens_cache){
|
| 562 |
auto &v = pr.second;
|
| 563 |
std::sort(v.begin(), v.end());
|
|
|
|
| 359 |
}
|
| 360 |
}
|
| 361 |
|
| 362 |
+
// 4. Superlative form (-est)
|
| 363 |
+
if (word.length() > 3 && word.substr(word.length() - 3) == "est") {
|
| 364 |
+
// e.g., simplest -> simple (strip 'st')
|
| 365 |
+
std::string base1 = word.substr(0, word.length() - 2);
|
| 366 |
+
if (check_dictionary(base1)) return base1;
|
| 367 |
+
|
| 368 |
+
// e.g., fastest -> fast
|
| 369 |
+
std::string base2 = word.substr(0, word.length() - 3);
|
| 370 |
+
if (check_dictionary(base2)) return base2;
|
| 371 |
+
|
| 372 |
+
// e.g., happiest -> happy (-iest -> -y)
|
| 373 |
+
if (word.length() > 4 && word.substr(word.length() - 4) == "iest") {
|
| 374 |
+
std::string base_y = word.substr(0, word.length() - 4) + "y";
|
| 375 |
+
if (check_dictionary(base_y)) return base_y;
|
| 376 |
+
}
|
| 377 |
+
|
| 378 |
+
// e.g., biggest -> big (doubled consonant check)
|
| 379 |
+
if (base2.length() >= 2 && base2.back() == base2[base2.length() - 2]) {
|
| 380 |
+
std::string base3 = base2.substr(0, base2.length() - 1);
|
| 381 |
+
if (check_dictionary(base3)) return base3;
|
| 382 |
+
}
|
| 383 |
+
}
|
| 384 |
+
|
| 385 |
+
// 5. Comparative form (-er)
|
| 386 |
+
if (word.length() > 2 && word.substr(word.length() - 2) == "er") {
|
| 387 |
+
// e.g., simpler -> simple (strip 'r')
|
| 388 |
+
std::string base1 = word.substr(0, word.length() - 1);
|
| 389 |
+
if (check_dictionary(base1)) return base1;
|
| 390 |
+
|
| 391 |
+
// e.g., faster -> fast
|
| 392 |
+
std::string base2 = word.substr(0, word.length() - 2);
|
| 393 |
+
if (check_dictionary(base2)) return base2;
|
| 394 |
+
|
| 395 |
+
// e.g., happier -> happy (-ier -> -y)
|
| 396 |
+
if (word.length() > 3 && word.substr(word.length() - 3) == "ier") {
|
| 397 |
+
std::string base_y = word.substr(0, word.length() - 3) + "y";
|
| 398 |
+
if (check_dictionary(base_y)) return base_y;
|
| 399 |
+
}
|
| 400 |
+
|
| 401 |
+
// e.g., bigger -> big (doubled consonant check)
|
| 402 |
+
if (base2.length() >= 2 && base2.back() == base2[base2.length() - 2]) {
|
| 403 |
+
std::string base3 = base2.substr(0, base2.length() - 1);
|
| 404 |
+
if (check_dictionary(base3)) return base3;
|
| 405 |
+
}
|
| 406 |
+
}
|
| 407 |
+
|
| 408 |
+
// 6. Adverbial form (-ly)
|
| 409 |
+
if (word.length() > 2 && word.substr(word.length() - 2) == "ly") {
|
| 410 |
+
// e.g., quickly -> quick
|
| 411 |
+
std::string base1 = word.substr(0, word.length() - 2);
|
| 412 |
+
if (check_dictionary(base1)) return base1;
|
| 413 |
+
|
| 414 |
+
// e.g., happily -> happy (-ily -> -y)
|
| 415 |
+
if (word.length() > 3 && word.substr(word.length() - 3) == "ily") {
|
| 416 |
+
std::string base2 = word.substr(0, word.length() - 3) + "y";
|
| 417 |
+
if (check_dictionary(base2)) return base2;
|
| 418 |
+
}
|
| 419 |
+
|
| 420 |
+
// e.g., basically -> basic (-ally -> -ic)
|
| 421 |
+
if (word.length() > 4 && word.substr(word.length() - 4) == "ally") {
|
| 422 |
+
std::string base3 = word.substr(0, word.length() - 4);
|
| 423 |
+
if (check_dictionary(base3)) return base3;
|
| 424 |
+
|
| 425 |
+
std::string base4 = base3 + "al";
|
| 426 |
+
if (check_dictionary(base4)) return base4;
|
| 427 |
+
}
|
| 428 |
+
|
| 429 |
+
// e.g., gently -> gentle (-ly -> -le)
|
| 430 |
+
if (word.back() == 'y' && word.length() > 2 && word[word.length() - 2] == 'l') {
|
| 431 |
+
std::string base5 = word.substr(0, word.length() - 1) + "e";
|
| 432 |
+
if (check_dictionary(base5)) return base5;
|
| 433 |
+
}
|
| 434 |
+
}
|
| 435 |
+
|
| 436 |
+
// 7. Noun suffix (-ness)
|
| 437 |
+
if (word.length() > 4 && word.substr(word.length() - 4) == "ness") {
|
| 438 |
+
// e.g., sadness -> sad
|
| 439 |
+
std::string base1 = word.substr(0, word.length() - 4);
|
| 440 |
+
if (check_dictionary(base1)) return base1;
|
| 441 |
+
|
| 442 |
+
// e.g., happiness -> happy (-iness -> -y)
|
| 443 |
+
if (base1.length() > 1 && base1.back() == 'i') {
|
| 444 |
+
std::string base2 = base1.substr(0, base1.length() - 1) + "y";
|
| 445 |
+
if (check_dictionary(base2)) return base2;
|
| 446 |
+
}
|
| 447 |
+
}
|
| 448 |
+
|
| 449 |
+
// 8. Noun suffix (-ment)
|
| 450 |
+
if (word.length() > 4 && word.substr(word.length() - 4) == "ment") {
|
| 451 |
+
// e.g., development -> develop
|
| 452 |
+
std::string base1 = word.substr(0, word.length() - 4);
|
| 453 |
+
if (check_dictionary(base1)) return base1;
|
| 454 |
+
}
|
| 455 |
+
|
| 456 |
+
// 9. Plural / 3rd-person singular form (-ies -> -y)
|
| 457 |
if (word.length() > 3 && word.substr(word.length() - 3) == "ies") {
|
| 458 |
std::string base = word.substr(0, word.length() - 3) + "y";
|
| 459 |
if (check_dictionary(base)) return base;
|
| 460 |
}
|
| 461 |
|
| 462 |
+
// 10. Plural / 3rd-person singular form (-ves -> -f / -fe)
|
| 463 |
if (word.length() > 3 && word.substr(word.length() - 3) == "ves") {
|
| 464 |
std::string base1 = word.substr(0, word.length() - 3) + "f";
|
| 465 |
if (check_dictionary(base1)) return base1;
|
|
|
|
| 468 |
if (check_dictionary(base2)) return base2;
|
| 469 |
}
|
| 470 |
|
| 471 |
+
// 11. Plural form (-es)
|
| 472 |
if (word.length() > 2 && word.substr(word.length() - 2) == "es") {
|
| 473 |
std::string base = word.substr(0, word.length() - 2);
|
| 474 |
if (check_dictionary(base)) return base;
|
| 475 |
}
|
| 476 |
|
| 477 |
+
// 12. Plural form (-s)
|
| 478 |
if (word.length() > 1 && word.back() == 's' && word[word.length() - 2] != 's') {
|
| 479 |
std::string base = word.substr(0, word.length() - 1);
|
| 480 |
if (check_dictionary(base)) return base;
|
|
|
|
| 639 |
global_pos_cache.reserve(global_dictionary_entries.size());
|
| 640 |
|
| 641 |
for (const auto &entry : global_dictionary_entries){
|
| 642 |
+
// 1. Tokenize definitions once per entry to avoid redundant processing
|
| 643 |
+
std::vector<std::string> all_def_toks;
|
| 644 |
+
for (const auto &def : entry.definitions){
|
| 645 |
+
auto toks = tokenize_others(def);
|
| 646 |
+
all_def_toks.insert(all_def_toks.end(), toks.begin(), toks.end());
|
| 647 |
+
}
|
| 648 |
|
| 649 |
std::string pos = normalize_pos_tag(entry.pos);
|
|
|
|
| 650 |
|
| 651 |
+
// 2. Split entry.word by semicolons or commas
|
| 652 |
+
std::vector<std::string> sub_words;
|
| 653 |
+
std::string current_sub;
|
| 654 |
+
for (char c : entry.word) {
|
| 655 |
+
if (c == ';' || c == ',') {
|
| 656 |
+
if (!current_sub.empty()) {
|
| 657 |
+
sub_words.push_back(current_sub);
|
| 658 |
+
current_sub.clear();
|
| 659 |
+
}
|
| 660 |
+
} else {
|
| 661 |
+
current_sub.push_back(c);
|
| 662 |
+
}
|
| 663 |
+
}
|
| 664 |
+
if (!current_sub.empty()) {
|
| 665 |
+
sub_words.push_back(current_sub);
|
| 666 |
+
}
|
| 667 |
+
|
| 668 |
+
// 3. Register each sub-word to the cache
|
| 669 |
+
for (size_t i = 0; i < sub_words.size(); ++i) {
|
| 670 |
+
const std::string key = normalize_dictionary_key(sub_words[i]);
|
| 671 |
+
if (key.empty()) continue;
|
| 672 |
+
|
| 673 |
+
if (!pos.empty()) {
|
| 674 |
+
// Move on the final sub-word, copy otherwise
|
| 675 |
+
if (i == sub_words.size() - 1) {
|
| 676 |
+
global_pos_cache[key].push_back(std::move(pos));
|
| 677 |
+
} else {
|
| 678 |
+
global_pos_cache[key].push_back(pos);
|
| 679 |
+
}
|
| 680 |
+
}
|
| 681 |
+
|
| 682 |
+
auto &defs = global_def_tokens_cache[key];
|
| 683 |
+
defs.insert(defs.end(), all_def_toks.begin(), all_def_toks.end());
|
| 684 |
}
|
| 685 |
}
|
| 686 |
|
| 687 |
+
// 4. Keep existing sorting and deduplication logic intact
|
| 688 |
for (auto &pr : global_def_tokens_cache){
|
| 689 |
auto &v = pr.second;
|
| 690 |
std::sort(v.begin(), v.end());
|