Skip to content

Commit

Permalink
Improve the candidates to cursor behavior.
Browse files Browse the repository at this point in the history
1. filter candidate by align to the next segment.
   e.g.  a|ng -> will filter at ang.
         xi|an -> filter till xi.
2. Add a new sentence from lattice. This is ideally the sentence till
   cursor. To avoid extra computation, we don't compute nbest here.
  • Loading branch information
wengxt committed Mar 10, 2024
1 parent 50aef18 commit 0bd13f7
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions src/libime/pinyin/pinyincontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <fcitx-utils/charutils.h>
#include <fcitx-utils/log.h>
#include <fcitx-utils/utf8.h>
#include <iostream>
#include <iterator>
#include <unordered_set>

Expand Down Expand Up @@ -50,6 +49,25 @@ class PinyinContextPrivate : public fcitx::QPtrHolder<PinyinContext> {
mutable std::unordered_set<std::string> candidatesToCursorSet_;
std::vector<fcitx::ScopedConnection> conn_;

size_t alignCursorToNextSegment() const {
FCITX_Q();
auto currentCursor = q->cursor();
while (segs_.nodes(currentCursor).empty() &&
currentCursor < q->size()) {
currentCursor += 1;
}
return currentCursor;
}

bool needCandidatesToCursor() const {
FCITX_Q();
if (q->cursor() == q->selectedLength()) {
return false;
}

return alignCursorToNextSegment() != q->size();
}

void clearCandidates() {
candidates_.clear();
candidatesToCursor_.clear();
Expand All @@ -64,20 +82,31 @@ class PinyinContextPrivate : public fcitx::QPtrHolder<PinyinContext> {
return;
}
candidatesToCursorNeedUpdate_ = false;
auto start = q->selectedLength();
auto currentCursor = q->cursor();
candidatesToCursor_.clear();
candidatesToCursorSet_.clear();

auto start = q->selectedLength();
auto currentCursor = alignCursorToNextSegment();
// Poke best sentence from lattice, ignore nbest option for now.
auto nodeRange = lattice_.nodes(&segs_.node(currentCursor));
if (!nodeRange.empty()) {
candidatesToCursor_.push_back(nodeRange.front().toSentenceResult());
candidatesToCursorSet_.insert(
candidatesToCursor_.back().toString());
}
for (const auto &candidate : candidates_) {
const auto &sentence = candidate.sentence();
if (sentence.size() <= 1) {
if (sentence.size() == 1) {
if (sentence.back()->to()->index() + start > currentCursor) {
continue;
}
auto text = candidate.toString();
if (candidatesToCursorSet_.count(text)) {
continue;
}
candidatesToCursor_.push_back(candidate);
candidatesToCursorSet_.insert(std::move(text));
} else {
} else if (sentence.size() > 1) {
auto newSentence = sentence;
while (!newSentence.empty() &&
newSentence.back()->to()->index() + start >
Expand Down Expand Up @@ -300,7 +329,7 @@ const std::unordered_set<std::string> &PinyinContext::candidateSet() const {

const std::vector<SentenceResult> &PinyinContext::candidatesToCursor() const {
FCITX_D();
if (cursor() == selectedLength() || cursor() == size()) {
if (!d->needCandidatesToCursor()) {
return d->candidates_;
}
d->updateCandidatesToCursor();
Expand All @@ -310,7 +339,7 @@ const std::vector<SentenceResult> &PinyinContext::candidatesToCursor() const {
const std::unordered_set<std::string> &
PinyinContext::candidatesToCursorSet() const {
FCITX_D();
if (cursor() == selectedLength() || cursor() == size()) {
if (!d->needCandidatesToCursor()) {
return d->candidatesSet_;
}
d->updateCandidatesToCursor();
Expand Down

0 comments on commit 0bd13f7

Please sign in to comment.