Skip to content

Commit 0bd13f7

Browse files
committed
Improve the candidates to cursor behavior.
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.
1 parent 50aef18 commit 0bd13f7

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

src/libime/pinyin/pinyincontext.cpp

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <fcitx-utils/charutils.h>
1616
#include <fcitx-utils/log.h>
1717
#include <fcitx-utils/utf8.h>
18-
#include <iostream>
1918
#include <iterator>
2019
#include <unordered_set>
2120

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

52+
size_t alignCursorToNextSegment() const {
53+
FCITX_Q();
54+
auto currentCursor = q->cursor();
55+
while (segs_.nodes(currentCursor).empty() &&
56+
currentCursor < q->size()) {
57+
currentCursor += 1;
58+
}
59+
return currentCursor;
60+
}
61+
62+
bool needCandidatesToCursor() const {
63+
FCITX_Q();
64+
if (q->cursor() == q->selectedLength()) {
65+
return false;
66+
}
67+
68+
return alignCursorToNextSegment() != q->size();
69+
}
70+
5371
void clearCandidates() {
5472
candidates_.clear();
5573
candidatesToCursor_.clear();
@@ -64,20 +82,31 @@ class PinyinContextPrivate : public fcitx::QPtrHolder<PinyinContext> {
6482
return;
6583
}
6684
candidatesToCursorNeedUpdate_ = false;
67-
auto start = q->selectedLength();
68-
auto currentCursor = q->cursor();
6985
candidatesToCursor_.clear();
7086
candidatesToCursorSet_.clear();
87+
88+
auto start = q->selectedLength();
89+
auto currentCursor = alignCursorToNextSegment();
90+
// Poke best sentence from lattice, ignore nbest option for now.
91+
auto nodeRange = lattice_.nodes(&segs_.node(currentCursor));
92+
if (!nodeRange.empty()) {
93+
candidatesToCursor_.push_back(nodeRange.front().toSentenceResult());
94+
candidatesToCursorSet_.insert(
95+
candidatesToCursor_.back().toString());
96+
}
7197
for (const auto &candidate : candidates_) {
7298
const auto &sentence = candidate.sentence();
73-
if (sentence.size() <= 1) {
99+
if (sentence.size() == 1) {
100+
if (sentence.back()->to()->index() + start > currentCursor) {
101+
continue;
102+
}
74103
auto text = candidate.toString();
75104
if (candidatesToCursorSet_.count(text)) {
76105
continue;
77106
}
78107
candidatesToCursor_.push_back(candidate);
79108
candidatesToCursorSet_.insert(std::move(text));
80-
} else {
109+
} else if (sentence.size() > 1) {
81110
auto newSentence = sentence;
82111
while (!newSentence.empty() &&
83112
newSentence.back()->to()->index() + start >
@@ -300,7 +329,7 @@ const std::unordered_set<std::string> &PinyinContext::candidateSet() const {
300329

301330
const std::vector<SentenceResult> &PinyinContext::candidatesToCursor() const {
302331
FCITX_D();
303-
if (cursor() == selectedLength() || cursor() == size()) {
332+
if (!d->needCandidatesToCursor()) {
304333
return d->candidates_;
305334
}
306335
d->updateCandidatesToCursor();
@@ -310,7 +339,7 @@ const std::vector<SentenceResult> &PinyinContext::candidatesToCursor() const {
310339
const std::unordered_set<std::string> &
311340
PinyinContext::candidatesToCursorSet() const {
312341
FCITX_D();
313-
if (cursor() == selectedLength() || cursor() == size()) {
342+
if (!d->needCandidatesToCursor()) {
314343
return d->candidatesSet_;
315344
}
316345
d->updateCandidatesToCursor();

0 commit comments

Comments
 (0)