Skip to content

Commit

Permalink
feat(translator): look at preceding text when making sentence
Browse files Browse the repository at this point in the history
  • Loading branch information
lotem committed Apr 16, 2019
1 parent 57c70a7 commit 6ae34de
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 10 deletions.
3 changes: 3 additions & 0 deletions src/rime/commit_history.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class CommitHistory : public list<CommitRecord> {
void Push(const KeyEvent& key_event);
void Push(const Composition& composition, const string& input);
string repr() const;
string latest_text() const {
return empty() ? string() : back().text;
}
};

} // Namespace rime
Expand Down
6 changes: 4 additions & 2 deletions src/rime/gear/poet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ bool Poet::LeftAssociateCompare(const Sentence& one, const Sentence& other) {
}

an<Sentence> Poet::MakeSentence(const WordGraph& graph,
size_t total_length) {
size_t total_length,
const string& preceding_text) {
// TODO: save more intermediate sentence candidates
map<int, an<Sentence>> sentences;
sentences[0] = New<Sentence>(language_);
Expand All @@ -61,7 +62,8 @@ an<Sentence> Poet::MakeSentence(const WordGraph& graph,
const DictEntryList& entries(x.second);
for (const auto& entry : entries) {
auto new_sentence = New<Sentence>(*sentences[start_pos]);
new_sentence->Extend(*entry, end_pos, is_rear, grammar_.get());
new_sentence->Extend(
*entry, end_pos, is_rear, preceding_text, grammar_.get());
if (sentences.find(end_pos) == sentences.end() ||
compare_(*sentences[end_pos], *new_sentence)) {
DLOG(INFO) << "updated sentences " << end_pos << ") with "
Expand Down
4 changes: 3 additions & 1 deletion src/rime/gear/poet.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ class Poet {
Compare compare = CompareWeight);
~Poet();

an<Sentence> MakeSentence(const WordGraph& graph, size_t total_length);
an<Sentence> MakeSentence(const WordGraph& graph,
size_t total_length,
const string& preceding_text);

private:
const Language* language_;
Expand Down
13 changes: 10 additions & 3 deletions src/rime/gear/script_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ string ScriptTranslator::Spell(const Code& code) {
return result;
}

string ScriptTranslator::GetPrecedingText() const {
return contextual_suggestions_ ?
engine_->context()->commit_history().latest_text() : string();
}

bool ScriptTranslator::Memorize(const CommitEntry& commit_entry) {
bool update_elements = false;
// avoid updating single character entries within a phrase which is
Expand Down Expand Up @@ -538,12 +543,14 @@ an<Sentence> ScriptTranslation::MakeSentence(Dictionary* dict,
}
}
}
auto sentence = poet_->MakeSentence(graph, syllable_graph.interpreted_length);
if (sentence) {
if (auto sentence = poet_->MakeSentence(graph,
syllable_graph.interpreted_length,
translator_->GetPrecedingText())) {
sentence->Offset(start_);
sentence->set_syllabifier(syllabifier_);
return sentence;
}
return sentence;
return nullptr;
}

} // namespace rime
1 change: 1 addition & 0 deletions src/rime/gear/script_translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ScriptTranslator : public Translator,

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

// options
int max_homophones() const { return max_homophones_; }
Expand Down
13 changes: 11 additions & 2 deletions src/rime/gear/table_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,9 @@ bool TableTranslator::Memorize(const CommitEntry& commit_entry) {
}
string phrase;
for (; it != history.rend(); ++it) {
if (it->type != "table" && it->type != "sentence" && it->type != "uniquified")
if (it->type != "table" &&
it->type != "sentence" &&
it->type != "uniquified")
break;
if (phrase.empty()) {
phrase = it->text; // last word
Expand All @@ -362,6 +364,11 @@ bool TableTranslator::Memorize(const CommitEntry& commit_entry) {
return true;
}

string TableTranslator::GetPrecedingText() const {
return contextual_suggestions_ ?
engine_->context()->commit_history().latest_text() : string();
}

// SentenceSyllabifier

class SentenceSyllabifier : public PhraseSyllabifier {
Expand Down Expand Up @@ -680,7 +687,9 @@ TableTranslator::MakeSentence(const string& input, size_t start,
}
}
}
if (auto sentence = poet_->MakeSentence(graph, input.length())) {
if (auto sentence = poet_->MakeSentence(graph,
input.length(),
GetPrecedingText())) {
auto result = Cached<SentenceTranslation>(
this,
std::move(sentence),
Expand Down
2 changes: 1 addition & 1 deletion src/rime/gear/table_translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TableTranslator : public Translator,
an<Translation> MakeSentence(const string& input,
size_t start,
bool include_prefix_phrases = false);

string GetPrecedingText() const;
UnityTableEncoder* encoder() const { return encoder_.get(); }

protected:
Expand Down
6 changes: 5 additions & 1 deletion src/rime/gear/translator_commons.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ bool Spans::HasVertex(size_t vertex) const {
void Sentence::Extend(const DictEntry& entry,
size_t end_pos,
bool is_rear,
const string& preceding_text,
Grammar* grammar) {
entry_->weight += Grammar::Evaluate(text(), entry, is_rear, grammar);
const string& context = empty() ? preceding_text : text();
entry_->weight += Grammar::Evaluate(context, entry, is_rear, grammar);
entry_->text.append(entry.text);
entry_->code.insert(entry_->code.end(),
entry.code.begin(),
Expand All @@ -118,6 +120,8 @@ TranslatorOptions::TranslatorOptions(const Ticket& ticket) {
config->GetString(ticket.name_space + "/delimiter", &delimiters_) ||
config->GetString("speller/delimiter", &delimiters_);
config->GetString(ticket.name_space + "/tag", &tag_);
config->GetBool(ticket.name_space + "/contextual_suggestions",
&contextual_suggestions_);
config->GetBool(ticket.name_space + "/enable_completion",
&enable_completion_);
config->GetBool(ticket.name_space + "/strict_spelling",
Expand Down
6 changes: 6 additions & 0 deletions src/rime/gear/translator_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class Sentence : public Phrase {
void Extend(const DictEntry& entry,
size_t end_pos,
bool is_rear,
const string& preceding_text,
Grammar* grammar);
void Offset(size_t offset);

Expand Down Expand Up @@ -159,6 +160,10 @@ class TranslatorOptions {
const string& delimiters() const { return delimiters_; }
const string& tag() const { return tag_; }
void set_tag(const string& tag) { tag_ = tag; }
bool contextual_suggestions() const { return contextual_suggestions_; }
void set_contextual_suggestions(bool enabled) {
contextual_suggestions_ = enabled;
}
bool enable_completion() const { return enable_completion_; }
void set_enable_completion(bool enabled) { enable_completion_ = enabled; }
bool strict_spelling() const { return strict_spelling_; }
Expand All @@ -171,6 +176,7 @@ class TranslatorOptions {
protected:
string delimiters_;
string tag_ = "abc";
bool contextual_suggestions_ = false;
bool enable_completion_ = true;
bool strict_spelling_ = false;
double initial_quality_ = 0.;
Expand Down

0 comments on commit 6ae34de

Please sign in to comment.