Skip to content

Commit

Permalink
扩充enable_completion配置项功能,实现script_table下的长词预测/联想
Browse files Browse the repository at this point in the history
  • Loading branch information
siuze committed Mar 2, 2023
1 parent d0d227c commit 8c8ca61
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
22 changes: 14 additions & 8 deletions src/rime/dict/dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ bool compare_chunk_by_head_element(const Chunk& a, const Chunk& b) {
}

size_t match_extra_code(const table::Code* extra_code, size_t depth,
const SyllableGraph& syll_graph, size_t current_pos) {
const SyllableGraph& syll_graph, size_t current_pos, bool enable_completion_=false) {
if (!extra_code || depth >= extra_code->size)
return current_pos; // success
if (current_pos >= syll_graph.interpreted_length)
return 0; // failure (possibly success for completion in the future)
if (current_pos >= syll_graph.interpreted_length){
if (enable_completion_) return match_extra_code(extra_code, depth + 1,syll_graph, current_pos+1, enable_completion_); //长词联想
else return 0;
}
auto index = syll_graph.indices.find(current_pos);
if (index == syll_graph.indices.end())
return 0;
Expand All @@ -66,7 +68,7 @@ size_t match_extra_code(const table::Code* extra_code, size_t depth,
size_t best_match = 0;
for (const SpellingProperties* props : spellings->second) {
size_t match_end_pos = match_extra_code(extra_code, depth + 1,
syll_graph, props->end_pos);
syll_graph, props->end_pos, enable_completion_);
if (!match_end_pos) continue;
if (match_end_pos > best_match)
best_match = match_end_pos;
Expand Down Expand Up @@ -191,7 +193,8 @@ static void lookup_table(Table* table,
DictEntryCollector* collector,
const SyllableGraph& syllable_graph,
size_t start_pos,
double initial_credibility) {
double initial_credibility,
bool enable_completion_=false) {
TableQueryResult result;
if (!table->Query(syllable_graph, start_pos, &result)) {
return;
Expand All @@ -204,8 +207,9 @@ static void lookup_table(Table* table,
if (a.extra_code()) {
do {
size_t actual_end_pos = dictionary::match_extra_code(
a.extra_code(), 0, syllable_graph, end_pos);
a.extra_code(), 0, syllable_graph, end_pos, enable_completion_);
if (actual_end_pos == 0) continue;
if (actual_end_pos > syllable_graph.interpreted_length) actual_end_pos = end_pos+2; //和无长词预测的候选项统一排序,不设置这个的话长词联想的词汇会因为长度更长,总是排在最前
(*collector)[actual_end_pos].AddChunk(
{table, a.code(), a.entry(), cr});
}
Expand All @@ -221,15 +225,17 @@ static void lookup_table(Table* table,
an<DictEntryCollector>
Dictionary::Lookup(const SyllableGraph& syllable_graph,
size_t start_pos,
double initial_credibility) {
double initial_credibility,
bool enable_completion_
) {
if (!loaded())
return nullptr;
auto collector = New<DictEntryCollector>();
for (const auto& table : tables_) {
if (!table->IsOpen())
continue;
lookup_table(table.get(), collector.get(),
syllable_graph, start_pos, initial_credibility);
syllable_graph, start_pos, initial_credibility,enable_completion_);
}
if (collector->empty())
return nullptr;
Expand Down
4 changes: 3 additions & 1 deletion src/rime/dict/dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ class Dictionary : public Class<Dictionary, const Ticket&> {

RIME_API an<DictEntryCollector> Lookup(const SyllableGraph& syllable_graph,
size_t start_pos,
double initial_credibility = 0.0);
double initial_credibility = 0.0,
bool enable_completion_ = false
);
// if predictive is true, do an expand search with limit,
// otherwise do an exact match.
// return num of matching keys.
Expand Down
4 changes: 3 additions & 1 deletion src/rime/gear/script_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class ScriptTranslation : public Translation {
size_t correction_count_ = 0;

bool enable_correction_;
bool enable_completion_;
};

// ScriptTranslator implementation
Expand All @@ -162,6 +163,7 @@ ScriptTranslator::ScriptTranslator(const Ticket& ticket)
config->GetBool(name_space_ + "/always_show_comments",
&always_show_comments_);
config->GetBool(name_space_ + "/enable_correction", &enable_correction_);
config->GetBool(name_space_ + "/enable_completion", &enable_completion_);
config->GetInt(name_space_ + "/max_homophones", &max_homophones_);
poet_.reset(new Poet(language(), config));
}
Expand Down Expand Up @@ -361,7 +363,7 @@ bool ScriptTranslation::Evaluate(Dictionary* dict, UserDictionary* user_dict) {
size_t consumed = syllabifier_->BuildSyllableGraph(*dict->prism());
const auto& syllable_graph = syllabifier_->syllable_graph();

phrase_ = dict->Lookup(syllable_graph, 0);
phrase_ = dict->Lookup(syllable_graph, 0, 0.0, translator_->enable_completion());
if (user_dict) {
user_phrase_ = user_dict->Lookup(syllable_graph, 0);
}
Expand Down
2 changes: 2 additions & 0 deletions src/rime/gear/script_translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ class ScriptTranslator : public Translator,
int max_homophones() const { return max_homophones_; }
int spelling_hints() const { return spelling_hints_; }
bool always_show_comments() const { return always_show_comments_; }
bool enable_completion() const { return enable_completion_; }

protected:
int max_homophones_ = 1;
int spelling_hints_ = 0;
bool always_show_comments_ = false;
bool enable_correction_ = false;
bool enable_completion_ = false;
the<Corrector> corrector_;
the<Poet> poet_;
};
Expand Down

0 comments on commit 8c8ca61

Please sign in to comment.