Skip to content

Commit

Permalink
feat(translator): contextual suggestions in partially selected sentence
Browse files Browse the repository at this point in the history
  • Loading branch information
lotem committed Apr 17, 2019
1 parent 2390da3 commit 12a7501
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 21 deletions.
13 changes: 13 additions & 0 deletions src/rime/composition.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// 2011-06-19 GONG Chen <chen.sst@gmail.com>
//
#include <boost/algorithm/string.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <rime/candidate.h>
#include <rime/composition.h>
#include <rime/menu.h>
Expand Down Expand Up @@ -167,4 +168,16 @@ string Composition::GetDebugText() const {
return result;
}

string Composition::GetTextBefore(size_t pos) const {
if (empty()) return string();
for (const auto& seg : boost::adaptors::reverse(*this)) {
if (seg.end <= pos) {
if (auto cand = seg.GetSelectedCandidate()) {
return cand->text();
}
}
}
return string();
}

} // namespace rime
2 changes: 2 additions & 0 deletions src/rime/composition.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class Composition : public Segmentation {
string GetCommitText() const;
string GetScriptText() const;
string GetDebugText() const;
// Returns text of the last segment before the given position.
string GetTextBefore(size_t pos) const;
};

} // namespace rime
Expand Down
11 changes: 7 additions & 4 deletions src/rime/gear/poet.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ class Poet {
template <class TranslatorT>
an<Translation> ContextualWeighted(an<Translation> translation,
const string& input,
size_t start,
TranslatorT* translator) {
if (!translator->contextual_suggestions() || !grammar_) {
return translation;
}
return New<ContextualTranslation>(translation,
input,
translator->GetPrecedingText(),
grammar_.get());
auto preceding_text = translator->GetPrecedingText(start);
if (preceding_text.empty()) {
return translation;
}
return New<ContextualTranslation>(
translation, input, preceding_text, grammar_.get());
}

private:
Expand Down
16 changes: 9 additions & 7 deletions src/rime/gear/script_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ an<Translation> ScriptTranslator::Query(const string& input,
}
auto deduped = New<DistinctTranslation>(result);
if (contextual_suggestions_) {
return poet_->ContextualWeighted(deduped, input, this);
return poet_->ContextualWeighted(deduped, input, segment.start, this);
}
return deduped;
}
Expand All @@ -218,9 +218,10 @@ string ScriptTranslator::Spell(const Code& code) {
return result;
}

string ScriptTranslator::GetPrecedingText() const {
return contextual_suggestions_ ?
engine_->context()->commit_history().latest_text() : string();
string ScriptTranslator::GetPrecedingText(size_t start) const {
return !contextual_suggestions_ ? string() :
start > 0 ? engine_->context()->composition().GetTextBefore(start) :
engine_->context()->commit_history().latest_text();
}

bool ScriptTranslator::Memorize(const CommitEntry& commit_entry) {
Expand Down Expand Up @@ -547,9 +548,10 @@ an<Sentence> ScriptTranslation::MakeSentence(Dictionary* dict,
}
}
}
if (auto sentence = poet_->MakeSentence(graph,
syllable_graph.interpreted_length,
translator_->GetPrecedingText())) {
if (auto sentence =
poet_->MakeSentence(graph,
syllable_graph.interpreted_length,
translator_->GetPrecedingText(start_))) {
sentence->Offset(start_);
sentence->set_syllabifier(syllabifier_);
return sentence;
Expand Down
2 changes: 1 addition & 1 deletion src/rime/gear/script_translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ScriptTranslator : public Translator,

string FormatPreedit(const string& preedit);
string Spell(const Code& code);
string GetPrecedingText() const;
string GetPrecedingText(size_t start) const;

// options
int max_homophones() const { return max_homophones_; }
Expand Down
11 changes: 6 additions & 5 deletions src/rime/gear/table_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ an<Translation> TableTranslator::Query(const string& input,
}
translation = New<DistinctTranslation>(translation);
if (contextual_suggestions_) {
return poet_->ContextualWeighted(translation, input, this);
return poet_->ContextualWeighted(translation, input, segment.start, this);
}
return translation;
}
Expand Down Expand Up @@ -366,9 +366,10 @@ bool TableTranslator::Memorize(const CommitEntry& commit_entry) {
return true;
}

string TableTranslator::GetPrecedingText() const {
return contextual_suggestions_ ?
engine_->context()->commit_history().latest_text() : string();
string TableTranslator::GetPrecedingText(size_t start) const {
return !contextual_suggestions_ ? string() :
start > 0 ? engine_->context()->composition().GetTextBefore(start) :
engine_->context()->commit_history().latest_text();
}

// SentenceSyllabifier
Expand Down Expand Up @@ -691,7 +692,7 @@ TableTranslator::MakeSentence(const string& input, size_t start,
}
if (auto sentence = poet_->MakeSentence(graph,
input.length(),
GetPrecedingText())) {
GetPrecedingText(start))) {
auto result = Cached<SentenceTranslation>(
this,
std::move(sentence),
Expand Down
8 changes: 4 additions & 4 deletions src/rime/gear/table_translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ class TableTranslator : public Translator,
TableTranslator(const Ticket& ticket);

virtual an<Translation> Query(const string& input,
const Segment& segment);
const Segment& segment);
virtual bool Memorize(const CommitEntry& commit_entry);

an<Translation> MakeSentence(const string& input,
size_t start,
bool include_prefix_phrases = false);
string GetPrecedingText() const;
size_t start,
bool include_prefix_phrases = false);
string GetPrecedingText(size_t start) const;
UnityTableEncoder* encoder() const { return encoder_.get(); }

protected:
Expand Down

0 comments on commit 12a7501

Please sign in to comment.