Skip to content

Commit 12a7501

Browse files
committed
feat(translator): contextual suggestions in partially selected sentence
1 parent 2390da3 commit 12a7501

7 files changed

+42
-21
lines changed

src/rime/composition.cc

+13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// 2011-06-19 GONG Chen <chen.sst@gmail.com>
66
//
77
#include <boost/algorithm/string.hpp>
8+
#include <boost/range/adaptor/reversed.hpp>
89
#include <rime/candidate.h>
910
#include <rime/composition.h>
1011
#include <rime/menu.h>
@@ -167,4 +168,16 @@ string Composition::GetDebugText() const {
167168
return result;
168169
}
169170

171+
string Composition::GetTextBefore(size_t pos) const {
172+
if (empty()) return string();
173+
for (const auto& seg : boost::adaptors::reverse(*this)) {
174+
if (seg.end <= pos) {
175+
if (auto cand = seg.GetSelectedCandidate()) {
176+
return cand->text();
177+
}
178+
}
179+
}
180+
return string();
181+
}
182+
170183
} // namespace rime

src/rime/composition.h

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class Composition : public Segmentation {
2929
string GetCommitText() const;
3030
string GetScriptText() const;
3131
string GetDebugText() const;
32+
// Returns text of the last segment before the given position.
33+
string GetTextBefore(size_t pos) const;
3234
};
3335

3436
} // namespace rime

src/rime/gear/poet.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,17 @@ class Poet {
4444
template <class TranslatorT>
4545
an<Translation> ContextualWeighted(an<Translation> translation,
4646
const string& input,
47+
size_t start,
4748
TranslatorT* translator) {
4849
if (!translator->contextual_suggestions() || !grammar_) {
4950
return translation;
5051
}
51-
return New<ContextualTranslation>(translation,
52-
input,
53-
translator->GetPrecedingText(),
54-
grammar_.get());
52+
auto preceding_text = translator->GetPrecedingText(start);
53+
if (preceding_text.empty()) {
54+
return translation;
55+
}
56+
return New<ContextualTranslation>(
57+
translation, input, preceding_text, grammar_.get());
5558
}
5659

5760
private:

src/rime/gear/script_translator.cc

+9-7
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ an<Translation> ScriptTranslator::Query(const string& input,
196196
}
197197
auto deduped = New<DistinctTranslation>(result);
198198
if (contextual_suggestions_) {
199-
return poet_->ContextualWeighted(deduped, input, this);
199+
return poet_->ContextualWeighted(deduped, input, segment.start, this);
200200
}
201201
return deduped;
202202
}
@@ -218,9 +218,10 @@ string ScriptTranslator::Spell(const Code& code) {
218218
return result;
219219
}
220220

221-
string ScriptTranslator::GetPrecedingText() const {
222-
return contextual_suggestions_ ?
223-
engine_->context()->commit_history().latest_text() : string();
221+
string ScriptTranslator::GetPrecedingText(size_t start) const {
222+
return !contextual_suggestions_ ? string() :
223+
start > 0 ? engine_->context()->composition().GetTextBefore(start) :
224+
engine_->context()->commit_history().latest_text();
224225
}
225226

226227
bool ScriptTranslator::Memorize(const CommitEntry& commit_entry) {
@@ -547,9 +548,10 @@ an<Sentence> ScriptTranslation::MakeSentence(Dictionary* dict,
547548
}
548549
}
549550
}
550-
if (auto sentence = poet_->MakeSentence(graph,
551-
syllable_graph.interpreted_length,
552-
translator_->GetPrecedingText())) {
551+
if (auto sentence =
552+
poet_->MakeSentence(graph,
553+
syllable_graph.interpreted_length,
554+
translator_->GetPrecedingText(start_))) {
553555
sentence->Offset(start_);
554556
sentence->set_syllabifier(syllabifier_);
555557
return sentence;

src/rime/gear/script_translator.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class ScriptTranslator : public Translator,
3737

3838
string FormatPreedit(const string& preedit);
3939
string Spell(const Code& code);
40-
string GetPrecedingText() const;
40+
string GetPrecedingText(size_t start) const;
4141

4242
// options
4343
int max_homophones() const { return max_homophones_; }

src/rime/gear/table_translator.cc

+6-5
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ an<Translation> TableTranslator::Query(const string& input,
312312
}
313313
translation = New<DistinctTranslation>(translation);
314314
if (contextual_suggestions_) {
315-
return poet_->ContextualWeighted(translation, input, this);
315+
return poet_->ContextualWeighted(translation, input, segment.start, this);
316316
}
317317
return translation;
318318
}
@@ -366,9 +366,10 @@ bool TableTranslator::Memorize(const CommitEntry& commit_entry) {
366366
return true;
367367
}
368368

369-
string TableTranslator::GetPrecedingText() const {
370-
return contextual_suggestions_ ?
371-
engine_->context()->commit_history().latest_text() : string();
369+
string TableTranslator::GetPrecedingText(size_t start) const {
370+
return !contextual_suggestions_ ? string() :
371+
start > 0 ? engine_->context()->composition().GetTextBefore(start) :
372+
engine_->context()->commit_history().latest_text();
372373
}
373374

374375
// SentenceSyllabifier
@@ -691,7 +692,7 @@ TableTranslator::MakeSentence(const string& input, size_t start,
691692
}
692693
if (auto sentence = poet_->MakeSentence(graph,
693694
input.length(),
694-
GetPrecedingText())) {
695+
GetPrecedingText(start))) {
695696
auto result = Cached<SentenceTranslation>(
696697
this,
697698
std::move(sentence),

src/rime/gear/table_translator.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ class TableTranslator : public Translator,
2929
TableTranslator(const Ticket& ticket);
3030

3131
virtual an<Translation> Query(const string& input,
32-
const Segment& segment);
32+
const Segment& segment);
3333
virtual bool Memorize(const CommitEntry& commit_entry);
3434

3535
an<Translation> MakeSentence(const string& input,
36-
size_t start,
37-
bool include_prefix_phrases = false);
38-
string GetPrecedingText() const;
36+
size_t start,
37+
bool include_prefix_phrases = false);
38+
string GetPrecedingText(size_t start) const;
3939
UnityTableEncoder* encoder() const { return encoder_.get(); }
4040

4141
protected:

0 commit comments

Comments
 (0)