Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(table_translator): Allow sorting completions #821

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/rime/algo/algebra.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void Script::Dump(const path& file_path) const {
bool first = true;
for (const Spelling& s : v.second) {
out << (first ? v.first : "") << '\t' << s.str << '\t'
<< "-ac?!"[s.properties.type] << '\t' << s.properties.credibility
<< "-fac?!"[s.properties.type] << '\t' << s.properties.credibility
<< '\t' << s.properties.tips << std::endl;
first = false;
}
Expand Down
24 changes: 23 additions & 1 deletion src/rime/dict/dict_compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ bool DictCompiler::BuildPrism(const path& schema_file,
if (!primary_table->Load() || !primary_table->GetSyllabary(&syllabary) ||
syllabary.empty())
return false;
// apply spelling algebra and prepare corrections (if enabled)
// apply spelling algebra, precompute completions, and prepare corrections (if
// enabled)
Script script;
if (!schema_file.empty()) {
Config config;
Expand All @@ -326,6 +327,27 @@ bool DictCompiler::BuildPrism(const path& schema_file,
}
}

bool sort_completions = false;
if (config.GetBool("translator/sort_completions", &sort_completions) &&
sort_completions) {
LOG(INFO) << "precomputing completions";
if (script.empty()) {
for (auto s : syllabary) {
script.AddSyllable(s);
}
}
Script temp;
for (auto [syll, spellings] : script) {
for (int len = syll.length(); len >= 1; --len) {
auto input = syll.substr(0, len);
SpellingProperties props;
props.type = kCompletion;
temp.Merge(input, props, spellings);
}
}
script.swap(temp);
}

#if 0
// build corrector
bool enable_correction = false; // Avoid if initializer to comfort compilers
Expand Down
5 changes: 3 additions & 2 deletions src/rime/dict/dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,12 @@ size_t Dictionary::LookupWords(DictEntryIterator* result,
while (!accessor.exhausted()) {
SyllableId syllable_id = accessor.syllable_id();
SpellingType type = accessor.properties().type;
bool is_sorted_completion = predictive && type == kCompletion;
accessor.Next();
if (type > kNormalSpelling)
if (type > kNormalSpelling && !is_sorted_completion)
continue;
string remaining_code;
if (match.length > code_length) {
if (match.length > code_length || is_sorted_completion) {
string syllable = primary_table()->GetSyllableById(syllable_id);
if (syllable.length() > code_length)
remaining_code = syllable.substr(code_length);
Expand Down
1 change: 1 addition & 0 deletions src/rime/dict/dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class Dictionary : public Class<Dictionary, const Ticket&> {
// if predictive is true, do an expand search with limit,
// otherwise do an exact match.
// return num of matching keys.
// kCompletion spellings are considered iff predictive=true.
RIME_API size_t LookupWords(DictEntryIterator* result,
const string& str_code,
bool predictive,
Expand Down
20 changes: 14 additions & 6 deletions src/rime/gear/table_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ class LazyTableTranslation : public TableTranslation {
size_t start,
size_t end,
const string& preedit,
bool enable_user_dict);
bool enable_user_dict,
bool sort_completions);
bool FetchUserPhrases(TableTranslator* translator);
virtual bool FetchMoreUserPhrases();
virtual bool FetchMoreTableEntries();
Expand All @@ -135,14 +136,16 @@ class LazyTableTranslation : public TableTranslation {
size_t limit_;
size_t user_dict_limit_;
string user_dict_key_;
bool sort_completions_;
};

LazyTableTranslation::LazyTableTranslation(TableTranslator* translator,
const string& input,
size_t start,
size_t end,
const string& preedit,
bool enable_user_dict)
bool enable_user_dict,
bool sort_completions)
: TableTranslation(translator,
translator->language(),
input,
Expand All @@ -152,7 +155,8 @@ LazyTableTranslation::LazyTableTranslation(TableTranslator* translator,
dict_(translator->dict()),
user_dict_(enable_user_dict ? translator->user_dict() : NULL),
limit_(kInitialSearchLimit),
user_dict_limit_(kInitialSearchLimit) {
user_dict_limit_(kInitialSearchLimit),
sort_completions_(sort_completions) {
FetchUserPhrases(translator) || FetchMoreUserPhrases();
FetchMoreTableEntries();
CheckEmpty();
Expand Down Expand Up @@ -198,6 +202,10 @@ bool LazyTableTranslation::FetchMoreTableEntries() {
limit_ *= kExpandingFactor;
}
if (more.entry_count() > previous_entry_count) {
if (sort_completions_) {
// make sure the first candidate is also ordered.
more.Sort();
}
more.Skip(previous_entry_count);
iter_ = std::move(more);
}
Expand Down Expand Up @@ -257,9 +265,9 @@ an<Translation> TableTranslator::Query(const string& input,

an<Translation> translation;
if (enable_completion_) {
translation = Cached<LazyTableTranslation>(this, code, segment.start,
segment.start + input.length(),
preedit, enable_user_dict);
translation = Cached<LazyTableTranslation>(
this, code, segment.start, segment.start + input.length(), preedit,
enable_user_dict, sort_completions_);
} else {
DictEntryIterator iter;
if (dict_ && dict_->loaded()) {
Expand Down
2 changes: 2 additions & 0 deletions src/rime/gear/translator_commons.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ TranslatorOptions::TranslatorOptions(const Ticket& ticket) {
&contextual_suggestions_);
config->GetBool(ticket.name_space + "/enable_completion",
&enable_completion_);
config->GetBool(ticket.name_space + "/sort_completions",
&sort_completions_);
config->GetBool(ticket.name_space + "/strict_spelling", &strict_spelling_);
config->GetDouble(ticket.name_space + "/initial_quality",
&initial_quality_);
Expand Down
1 change: 1 addition & 0 deletions src/rime/gear/translator_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class TranslatorOptions {
string tag_ = "abc";
bool contextual_suggestions_ = false;
bool enable_completion_ = true;
bool sort_completions_ = false;
bool strict_spelling_ = false;
double initial_quality_ = 0.;
Projection preedit_formatter_;
Expand Down
Loading