-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(translator): contextual suggestions
- Loading branch information
Showing
13 changed files
with
270 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
// 2011-06-19 GONG Chen <[email protected]> | ||
// | ||
#include <boost/algorithm/string.hpp> | ||
#include <boost/range/adaptor/reversed.hpp> | ||
#include <rime/candidate.h> | ||
#include <rime/composition.h> | ||
#include <rime/menu.h> | ||
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include <algorithm> | ||
#include <iterator> | ||
#include <rime/gear/contextual_translation.h> | ||
#include <rime/gear/translator_commons.h> | ||
|
||
namespace rime { | ||
|
||
const int kContextualSearchLimit = 32; | ||
|
||
bool ContextualTranslation::Replenish() { | ||
vector<of<Phrase>> queue; | ||
size_t end_pos = 0; | ||
while (!translation_->exhausted() && | ||
cache_.size() + queue.size() < kContextualSearchLimit) { | ||
auto cand = translation_->Peek(); | ||
DLOG(INFO) << cand->text() << " cache/queue: " | ||
<< cache_.size() << "/" << queue.size(); | ||
if (cand->type() == "phrase" || cand->type() == "table") { | ||
if (end_pos != cand->end()) { | ||
end_pos = cand->end(); | ||
AppendToCache(queue); | ||
} | ||
queue.push_back(Evaluate(As<Phrase>(cand))); | ||
} else { | ||
AppendToCache(queue); | ||
cache_.push_back(cand); | ||
} | ||
if (!translation_->Next()) { | ||
break; | ||
} | ||
} | ||
AppendToCache(queue); | ||
return !cache_.empty(); | ||
} | ||
|
||
an<Phrase> ContextualTranslation::Evaluate(an<Phrase> phrase) { | ||
auto sentence = New<Sentence>(phrase->language()); | ||
sentence->Offset(phrase->start()); | ||
bool is_rear = phrase->end() == input_.length(); | ||
sentence->Extend(phrase->entry(), phrase->end(), is_rear, preceding_text_, | ||
grammar_); | ||
phrase->set_weight(sentence->weight()); | ||
DLOG(INFO) << "contextual suggestion: " << phrase->text() | ||
<< " weight: " << phrase->weight(); | ||
return phrase; | ||
} | ||
|
||
static bool compare_by_weight_desc(const an<Phrase>& a, const an<Phrase>& b) { | ||
return a->weight() > b->weight(); | ||
} | ||
|
||
void ContextualTranslation::AppendToCache(vector<of<Phrase>>& queue) { | ||
if (queue.empty()) return; | ||
DLOG(INFO) << "appending to cache " << queue.size() << " candidates."; | ||
std::sort(queue.begin(), queue.end(), compare_by_weight_desc); | ||
std::copy(queue.begin(), queue.end(), std::back_inserter(cache_)); | ||
queue.clear(); | ||
} | ||
|
||
} // namespace rime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// Copyright RIME Developers | ||
// Distributed under the BSD License | ||
// | ||
|
||
#include <rime/common.h> | ||
#include <rime/translation.h> | ||
|
||
namespace rime { | ||
|
||
class Candidate; | ||
class Grammar; | ||
class Phrase; | ||
|
||
class ContextualTranslation : public PrefetchTranslation { | ||
public: | ||
ContextualTranslation(an<Translation> translation, | ||
string input, | ||
string preceding_text, | ||
Grammar* grammar) | ||
: PrefetchTranslation(translation), | ||
input_(input), | ||
preceding_text_(preceding_text), | ||
grammar_(grammar) {} | ||
|
||
protected: | ||
bool Replenish() override; | ||
|
||
private: | ||
an<Phrase> Evaluate(an<Phrase> phrase); | ||
void AppendToCache(vector<of<Phrase>>& queue); | ||
|
||
string input_; | ||
string preceding_text_; | ||
Grammar* grammar_; | ||
}; | ||
|
||
} // namespace rime |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
// | ||
// 2011-10-06 GONG Chen <[email protected]> | ||
// | ||
#include <algorithm> | ||
#include <functional> | ||
#include <rime/candidate.h> | ||
#include <rime/config.h> | ||
#include <rime/dict/vocabulary.h> | ||
|
@@ -21,14 +23,27 @@ inline static Grammar* create_grammar(Config* config) { | |
return nullptr; | ||
} | ||
|
||
Poet::Poet(const Language* language, Config* config) | ||
Poet::Poet(const Language* language, Config* config, Compare compare) | ||
: language_(language), | ||
grammar_(create_grammar(config)) {} | ||
grammar_(create_grammar(config)), | ||
compare_(compare) {} | ||
|
||
Poet::~Poet() {} | ||
|
||
bool Poet::LeftAssociateCompare(const Sentence& one, const Sentence& other) { | ||
return one.weight() < other.weight() || ( // left associate if even | ||
one.weight() == other.weight() && ( | ||
one.size() > other.size() || ( // less components is more favorable | ||
one.size() == other.size() && | ||
std::lexicographical_compare(one.syllable_lengths().begin(), | ||
one.syllable_lengths().end(), | ||
other.syllable_lengths().begin(), | ||
other.syllable_lengths().end())))); | ||
} | ||
|
||
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_); | ||
|
@@ -43,16 +58,17 @@ an<Sentence> Poet::MakeSentence(const WordGraph& graph, | |
if (start_pos == 0 && end_pos == total_length) | ||
continue; // exclude single words from the result | ||
DLOG(INFO) << "end pos: " << end_pos; | ||
bool is_rear = end_pos == total_length; | ||
const DictEntryList& entries(x.second); | ||
for (size_t i = 0; i < entries.size(); ++i) { | ||
const auto& entry(entries[i]); | ||
for (const auto& entry : entries) { | ||
auto new_sentence = New<Sentence>(*sentences[start_pos]); | ||
bool is_rear = end_pos == total_length; | ||
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() || | ||
sentences[end_pos]->weight() < new_sentence->weight()) { | ||
DLOG(INFO) << "updated sentences " << end_pos << ") with '" | ||
<< new_sentence->text() << "', " << new_sentence->weight(); | ||
compare_(*sentences[end_pos], *new_sentence)) { | ||
DLOG(INFO) << "updated sentences " << end_pos << ") with " | ||
<< new_sentence->text() << " weight: " | ||
<< new_sentence->weight(); | ||
sentences[end_pos] = std::move(new_sentence); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.