@@ -919,10 +919,10 @@ std::vector<std::string> PinyinContext::selectedWords() const {
919919 return newSentence;
920920}
921921
922- std::vector<std::pair<std::string, std::string> >
922+ std::vector<HistoryBigram::WordWithCode >
923923PinyinContext::selectedWordsWithPinyin () const {
924924 FCITX_D ();
925- std::vector<std::pair<std::string, std::string> > newSentence;
925+ std::vector<HistoryBigram::WordWithCode > newSentence;
926926 for (const auto &s : d->selected_ ) {
927927 for (const auto &item : s) {
928928 if (item.type_ != SelectedPinyinType::Separator) {
@@ -976,31 +976,30 @@ void PinyinContext::learn() {
976976 return ;
977977 }
978978
979+ std::vector<HistoryBigram::WordWithCode> newSentence;
979980 if (auto [result, encodedWordPinyin] = d->learnWord ();
980981 result != LearnWordResult::Ignored) {
981982 // Do not insert custom to history for the first time.
982983 if (result == LearnWordResult::Normal) {
983984 // Create new sentence with the whole new learned word.
984- std::vector<HistoryBigram::WordWithCode> newSentence{
985- { sentence (), encodedWordPinyin}};
986- d-> ime_ -> model ()-> history (). addWithCode (newSentence) ;
985+ newSentence. push_back ({ sentence (), encodedWordPinyin});
986+ } else {
987+ return ;
987988 }
988989 } else {
989- std::vector<HistoryBigram::WordWithCode> newSentence;
990- for (auto &s : d->selected_ ) {
991- for (auto &item : s) {
992- if (item.type_ != SelectedPinyinType::Separator) {
993- // Non pinyin word. Skip it.
994- if (item.encodedPinyin ().empty ()) {
995- return ;
996- }
997- newSentence.push_back (
998- {item.word_ .word (), item.encodedPinyin ()});
999- }
1000- }
1001- }
1002- d->ime_ ->model ()->history ().addWithCode (newSentence);
990+ newSentence = selectedWordsWithPinyin ();
991+ }
992+
993+ if (std::ranges::any_of (newSentence, [](const auto &word) {
994+ return word.second .empty ();
995+ })) {
996+ // Don't add to history if there is any non-pinyin word.
997+ return ;
1003998 }
999+
1000+ auto context = contextWordsWithPinyin ();
1001+ d->ime_ ->model ()->history ().addWithContext (contextWordsWithPinyin (),
1002+ std::move (newSentence));
10041003}
10051004
10061005void PinyinContext::setContextWords (
0 commit comments