|
10 | 10 | #include <functional> |
11 | 11 | #include <iterator> |
12 | 12 | #include <limits> |
| 13 | +#include <list> |
13 | 14 | #include <memory> |
14 | 15 | #include <span> |
15 | 16 | #include <stdexcept> |
@@ -81,7 +82,7 @@ class PinyinContextPrivate : public fcitx::QPtrHolder<PinyinContext> { |
81 | 82 | mutable std::vector<SentenceResult> candidatesToCursor_; |
82 | 83 | mutable std::unordered_set<std::string> candidatesToCursorSet_; |
83 | 84 | std::vector<fcitx::ScopedConnection> conn_; |
84 | | - std::vector<WordNode> contextWords_; |
| 85 | + std::list<WordNode> contextWords_; |
85 | 86 |
|
86 | 87 | size_t alignCursorToNextSegment() const { |
87 | 88 | FCITX_Q(); |
@@ -988,10 +989,38 @@ void PinyinContext::setContextWords( |
988 | 989 | const std::vector<std::string> &contextWords) { |
989 | 990 | FCITX_D(); |
990 | 991 | d->contextWords_.clear(); |
991 | | - for (const auto &word : contextWords) { |
| 992 | + appendContextWords(contextWords); |
| 993 | +} |
| 994 | + |
| 995 | +void PinyinContext::clearContextWords() { |
| 996 | + FCITX_D(); |
| 997 | + d->contextWords_.clear(); |
| 998 | +} |
| 999 | + |
| 1000 | +void PinyinContext::appendContextWords( |
| 1001 | + const std::vector<std::string> &contextWords) { |
| 1002 | + FCITX_D(); |
| 1003 | + |
| 1004 | + size_t needed = LanguageModel::maxOrder() - 1; |
| 1005 | + |
| 1006 | + for (const auto &word : |
| 1007 | + std::span{contextWords}.last(std::min(contextWords.size(), needed))) { |
992 | 1008 | d->contextWords_.push_back( |
993 | 1009 | WordNode(word, d->ime_->model()->index(word))); |
994 | 1010 | } |
| 1011 | + while (d->contextWords_.size() > needed) { |
| 1012 | + d->contextWords_.pop_front(); |
| 1013 | + } |
| 1014 | +} |
| 1015 | + |
| 1016 | +std::vector<std::string> PinyinContext::contextWords() const { |
| 1017 | + FCITX_D(); |
| 1018 | + std::vector<std::string> words; |
| 1019 | + words.reserve(d->contextWords_.size()); |
| 1020 | + for (const auto &word : d->contextWords_) { |
| 1021 | + words.push_back(word.word()); |
| 1022 | + } |
| 1023 | + return words; |
995 | 1024 | } |
996 | 1025 |
|
997 | 1026 | bool PinyinContext::learnWord() { return false; } |
|
0 commit comments