Skip to content

Commit 69d5c32

Browse files
committed
fix(user_dictionary, contextual_translation): fix user phrase quality; order contextual suggestions by type
1 parent d9fd0cc commit 69d5c32

6 files changed

+21
-15
lines changed

src/rime/dict/user_dictionary.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,7 @@ an<DictEntry> UserDictionary::CreateDictEntry(const string& key,
485485
(double)v.commits / present_tick,
486486
(double)present_tick,
487487
v.dee);
488-
constexpr double kUser = 13; // log(1e8) - log(200)
489-
e->weight = kUser + log(weight > 0 ? weight : DBL_EPSILON) + credibility;
488+
e->weight = log(weight > 0 ? weight : DBL_EPSILON) + credibility;
490489
if (full_code) {
491490
*full_code = key.substr(0, separator_pos);
492491
}

src/rime/gear/contextual_translation.cc

+5-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ const int kContextualSearchLimit = 32;
1010
bool ContextualTranslation::Replenish() {
1111
vector<of<Phrase>> queue;
1212
size_t end_pos = 0;
13+
std::string last_type;
1314
while (!translation_->exhausted() &&
1415
cache_.size() + queue.size() < kContextualSearchLimit) {
1516
auto cand = translation_->Peek();
1617
DLOG(INFO) << cand->text() << " cache/queue: "
1718
<< cache_.size() << "/" << queue.size();
18-
if (cand->type() == "phrase" || cand->type() == "table") {
19-
if (end_pos != cand->end()) {
19+
if (cand->type() == "phrase" || cand->type() == "user_phrase" ||
20+
cand->type() == "table" || cand->type() == "user_table") {
21+
if (end_pos != cand->end() || last_type != cand->type()) {
2022
end_pos = cand->end();
23+
last_type = cand->type();
2124
AppendToCache(queue);
2225
}
2326
queue.push_back(Evaluate(As<Phrase>(cand)));

src/rime/gear/script_translator.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ void ScriptTranslation::PrepareCandidate() {
491491
DLOG(INFO) << "user phrase '" << entry->text
492492
<< "', code length: " << user_phrase_code_length;
493493
cand = New<Phrase>(translator_->language(),
494-
"phrase",
494+
"user_phrase",
495495
start_,
496496
start_ + user_phrase_code_length,
497497
entry);

src/rime/gear/single_char_filter.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ bool SingleCharFirstTranslation::Rearrange() {
4040
while (!translation_->exhausted()) {
4141
auto cand = translation_->Peek();
4242
auto phrase = As<Phrase>(Candidate::GetGenuineCandidate(cand));
43-
if (!phrase || phrase->type() != "table") {
43+
if (!phrase || phrase->type() != "table" || phrase->type() != "user_table") {
4444
break;
4545
}
4646
if (unistrlen(cand->text()) == 1) {

src/rime/gear/speller.cc

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,18 @@ static bool reached_max_code_length(const an<Candidate>& cand,
3333
return code_length >= max_code_length;
3434
}
3535

36+
static inline bool is_table_entry(const an<Candidate>& cand) {
37+
const auto& type = Candidate::GetGenuineCandidate(cand)->type();
38+
return type == "table" || type == "user_table";
39+
}
40+
3641
static bool is_auto_selectable(const an<Candidate>& cand,
3742
const string& input,
3843
const string& delimiters) {
3944
return
4045
// reaches end of input
4146
cand->end() == input.length() &&
42-
// is table entry
43-
Candidate::GetGenuineCandidate(cand)->type() == "table" &&
47+
is_table_entry(cand) &&
4448
// no delimiters
4549
input.find_first_of(delimiters, cand->start()) == string::npos;
4650
}

src/rime/gear/table_translator.cc

+7-7
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,12 @@ an<Candidate> TableTranslation::Peek() {
7474
if (options_) {
7575
options_->comment_formatter().Apply(&comment);
7676
}
77-
auto phrase = New<Phrase>(
78-
language_,
79-
e->remaining_code_length == 0 ? "table" : "completion",
80-
start_, end_, e);
77+
bool incomplete = e->remaining_code_length != 0;
78+
auto type = incomplete ? "completion" : is_user_phrase ? "user_table" : "table";
79+
auto phrase = New<Phrase>(language_, type, start_, end_, e);
8180
if (phrase) {
8281
phrase->set_comment(comment);
8382
phrase->set_preedit(preedit_);
84-
bool incomplete = e->remaining_code_length != 0;
8583
phrase->set_quality(exp(e->weight) +
8684
options_->initial_quality() +
8785
(incomplete ? -1 : 0) +
@@ -345,6 +343,7 @@ bool TableTranslator::Memorize(const CommitEntry& commit_entry) {
345343
string phrase;
346344
for (; it != history.rend(); ++it) {
347345
if (it->type != "table" &&
346+
it->type != "user_table" &&
348347
it->type != "sentence" &&
349348
it->type != "uniquified")
350349
break;
@@ -464,7 +463,8 @@ an<Candidate> SentenceTranslation::Peek() {
464463
}
465464
size_t code_length = 0;
466465
an<DictEntry> entry;
467-
if (PreferUserPhrase()) {
466+
bool is_user_phrase = PreferUserPhrase();
467+
if (is_user_phrase) {
468468
auto r = user_phrase_collector_.rbegin();
469469
code_length = r->first;
470470
entry = r->second[user_phrase_index_];
@@ -476,7 +476,7 @@ an<Candidate> SentenceTranslation::Peek() {
476476
}
477477
auto result = New<Phrase>(
478478
translator_ ? translator_->language() : NULL,
479-
"table",
479+
is_user_phrase ? "user_table" : "table",
480480
start_,
481481
start_ + code_length,
482482
entry);

0 commit comments

Comments
 (0)