1111#include < iterator>
1212#include < limits>
1313#include < memory>
14+ #include < span>
1415#include < stdexcept>
1516#include < string>
1617#include < string_view>
@@ -80,6 +81,7 @@ class PinyinContextPrivate : public fcitx::QPtrHolder<PinyinContext> {
8081 mutable std::vector<SentenceResult> candidatesToCursor_;
8182 mutable std::unordered_set<std::string> candidatesToCursorSet_;
8283 std::vector<fcitx::ScopedConnection> conn_;
84+ std::vector<WordNode> contextWords_;
8385
8486 size_t alignCursorToNextSegment () const {
8587 FCITX_Q ();
@@ -526,16 +528,19 @@ State PinyinContext::state() const {
526528 FCITX_D ();
527529 auto *model = d->ime_ ->model ();
528530 State state = model->nullState ();
529- if (! d->selected_ . empty () ) {
530- for ( const auto &s : d-> selected_ ) {
531- for ( const auto &item : s) {
532- if (item. word_ . word (). empty ()) {
533- continue ;
534- }
535- State temp;
536- model-> score (state, item.word_ , temp);
537- state = std::move (temp) ;
531+ for ( const auto &word : d->contextWords_ ) {
532+ State temp;
533+ model-> score (state, word, temp);
534+ state = std::move (temp);
535+ }
536+ for ( const auto &s : d-> selected_ ) {
537+ for ( const auto &item : s) {
538+ if ( item.word_ . word (). empty ()) {
539+ continue ;
538540 }
541+ State temp;
542+ model->score (state, item.word_ , temp);
543+ state = std::move (temp);
539544 }
540545 }
541546 return state;
@@ -552,21 +557,9 @@ void PinyinContext::update() {
552557 d->clearCandidates ();
553558 } else {
554559 size_t start = 0 ;
555- auto *model = d->ime_ ->model ();
556- State state = model->nullState ();
560+ State state = this ->state ();
557561 if (!d->selected_ .empty ()) {
558562 start = d->selected_ .back ().back ().offset_ ;
559-
560- for (auto &s : d->selected_ ) {
561- for (auto &item : s) {
562- if (item.word_ .word ().empty ()) {
563- continue ;
564- }
565- State temp;
566- model->score (state, item.word_ , temp);
567- state = std::move (temp);
568- }
569- }
570563 }
571564 SegmentGraph newGraph;
572565 if (auto spProfile = d->matchState_ .shuangpinProfile ()) {
@@ -991,6 +984,16 @@ void PinyinContext::learn() {
991984 }
992985}
993986
987+ void PinyinContext::setContextWords (
988+ const std::vector<std::string> &contextWords) {
989+ FCITX_D ();
990+ d->contextWords_ .clear ();
991+ for (const auto &word : contextWords) {
992+ d->contextWords_ .push_back (
993+ WordNode (word, d->ime_ ->model ()->index (word)));
994+ }
995+ }
996+
994997bool PinyinContext::learnWord () { return false ; }
995998
996999PinyinIME *PinyinContext::ime () const {
0 commit comments