Skip to content

Commit

Permalink
fix(user_dictionary, contextual_translation): fix user phrase quality…
Browse files Browse the repository at this point in the history
…; order contextual suggestions by type
  • Loading branch information
lotem committed Jun 17, 2019
1 parent d9fd0cc commit 69d5c32
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
3 changes: 1 addition & 2 deletions src/rime/dict/user_dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,7 @@ an<DictEntry> UserDictionary::CreateDictEntry(const string& key,
(double)v.commits / present_tick,
(double)present_tick,
v.dee);
constexpr double kUser = 13; // log(1e8) - log(200)
e->weight = kUser + log(weight > 0 ? weight : DBL_EPSILON) + credibility;
e->weight = log(weight > 0 ? weight : DBL_EPSILON) + credibility;
if (full_code) {
*full_code = key.substr(0, separator_pos);
}
Expand Down
7 changes: 5 additions & 2 deletions src/rime/gear/contextual_translation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ const int kContextualSearchLimit = 32;
bool ContextualTranslation::Replenish() {
vector<of<Phrase>> queue;
size_t end_pos = 0;
std::string last_type;
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()) {
if (cand->type() == "phrase" || cand->type() == "user_phrase" ||
cand->type() == "table" || cand->type() == "user_table") {
if (end_pos != cand->end() || last_type != cand->type()) {
end_pos = cand->end();
last_type = cand->type();
AppendToCache(queue);
}
queue.push_back(Evaluate(As<Phrase>(cand)));
Expand Down
2 changes: 1 addition & 1 deletion src/rime/gear/script_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ void ScriptTranslation::PrepareCandidate() {
DLOG(INFO) << "user phrase '" << entry->text
<< "', code length: " << user_phrase_code_length;
cand = New<Phrase>(translator_->language(),
"phrase",
"user_phrase",
start_,
start_ + user_phrase_code_length,
entry);
Expand Down
2 changes: 1 addition & 1 deletion src/rime/gear/single_char_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ bool SingleCharFirstTranslation::Rearrange() {
while (!translation_->exhausted()) {
auto cand = translation_->Peek();
auto phrase = As<Phrase>(Candidate::GetGenuineCandidate(cand));
if (!phrase || phrase->type() != "table") {
if (!phrase || phrase->type() != "table" || phrase->type() != "user_table") {
break;
}
if (unistrlen(cand->text()) == 1) {
Expand Down
8 changes: 6 additions & 2 deletions src/rime/gear/speller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,18 @@ static bool reached_max_code_length(const an<Candidate>& cand,
return code_length >= max_code_length;
}

static inline bool is_table_entry(const an<Candidate>& cand) {
const auto& type = Candidate::GetGenuineCandidate(cand)->type();
return type == "table" || type == "user_table";
}

static bool is_auto_selectable(const an<Candidate>& cand,
const string& input,
const string& delimiters) {
return
// reaches end of input
cand->end() == input.length() &&
// is table entry
Candidate::GetGenuineCandidate(cand)->type() == "table" &&
is_table_entry(cand) &&
// no delimiters
input.find_first_of(delimiters, cand->start()) == string::npos;
}
Expand Down
14 changes: 7 additions & 7 deletions src/rime/gear/table_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,12 @@ an<Candidate> TableTranslation::Peek() {
if (options_) {
options_->comment_formatter().Apply(&comment);
}
auto phrase = New<Phrase>(
language_,
e->remaining_code_length == 0 ? "table" : "completion",
start_, end_, e);
bool incomplete = e->remaining_code_length != 0;
auto type = incomplete ? "completion" : is_user_phrase ? "user_table" : "table";
auto phrase = New<Phrase>(language_, type, start_, end_, e);
if (phrase) {
phrase->set_comment(comment);
phrase->set_preedit(preedit_);
bool incomplete = e->remaining_code_length != 0;
phrase->set_quality(exp(e->weight) +
options_->initial_quality() +
(incomplete ? -1 : 0) +
Expand Down Expand Up @@ -345,6 +343,7 @@ bool TableTranslator::Memorize(const CommitEntry& commit_entry) {
string phrase;
for (; it != history.rend(); ++it) {
if (it->type != "table" &&
it->type != "user_table" &&
it->type != "sentence" &&
it->type != "uniquified")
break;
Expand Down Expand Up @@ -464,7 +463,8 @@ an<Candidate> SentenceTranslation::Peek() {
}
size_t code_length = 0;
an<DictEntry> entry;
if (PreferUserPhrase()) {
bool is_user_phrase = PreferUserPhrase();
if (is_user_phrase) {
auto r = user_phrase_collector_.rbegin();
code_length = r->first;
entry = r->second[user_phrase_index_];
Expand All @@ -476,7 +476,7 @@ an<Candidate> SentenceTranslation::Peek() {
}
auto result = New<Phrase>(
translator_ ? translator_->language() : NULL,
"table",
is_user_phrase ? "user_table" : "table",
start_,
start_ + code_length,
entry);
Expand Down

0 comments on commit 69d5c32

Please sign in to comment.