-
Notifications
You must be signed in to change notification settings - Fork 556
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(contextual_translation): weight and re-order phrases by context
- Loading branch information
Showing
6 changed files
with
126 additions
and
7 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
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
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