Skip to content

Commit

Permalink
Fix New Prediction Syllabification
Browse files Browse the repository at this point in the history
  • Loading branch information
graphemecluster committed Mar 18, 2024
1 parent 84ac2a3 commit f8a297c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 35 deletions.
6 changes: 2 additions & 4 deletions src/rime/dict/dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ static void lookup_table(Table* table,
return;
}
// copy result
int predict_pos = 0;
dictionary::Chunk predict;
for (auto& v : result) {
int end_pos = v.first;
Expand All @@ -254,8 +253,7 @@ static void lookup_table(Table* table,
if (!predict.entries ||
cr + entry->weight > predict.credibility +
predict.entries[predict.cursor].weight) {
predict_pos = end_pos;
predict = {table, a.code(), entry, cr};
predict = {table, a.code(), entry, (size_t)-end_pos, cr};
}
} while (a.Next());
} else if (a.extra_code()) {
Expand All @@ -275,7 +273,7 @@ static void lookup_table(Table* table,
}
if (predict.entries && predict.entries[predict.cursor].weight >=
kPredictionThreshold - DBL_EPSILON) {
(*collector)[predict_pos].AddChunk(std::move(predict));
(*collector)[-1].AddChunk(std::move(predict));
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/rime/gear/memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ void CommitEntry::Clear() {

void CommitEntry::AppendPhrase(const an<Phrase>& phrase) {
text += phrase->text();
Code phrase_code = phrase->full_code();
code.insert(code.end(), phrase_code.begin(), phrase_code.end());
code.insert(code.end(), phrase->code().begin(), phrase->code().end());
if (auto sentence = As<Sentence>(phrase)) {
for (const DictEntry& e : sentence->components()) {
elements.push_back(&e);
Expand Down
19 changes: 9 additions & 10 deletions src/rime/gear/script_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ struct SyllabifyTask {
static bool syllabify_dfs(SyllabifyTask* task,
size_t depth,
size_t current_pos) {
if (depth == task->code.size()) {
return current_pos == task->target_pos;
if (current_pos == task->target_pos) {
return true;
}
SyllableId syllable_id = task->code.at(depth);
auto z = task->graph.edges.find(current_pos);
Expand Down Expand Up @@ -312,10 +312,10 @@ string ScriptSyllabifier::GetPreeditString(const Phrase& cand) const {
}

string ScriptSyllabifier::GetOriginalSpelling(const Phrase& cand) const {
if (translator_ && (translator_->always_show_comments() ||
static_cast<int>(cand.full_code().size()) <=
translator_->spelling_hints())) {
return translator_->Spell(cand.full_code());
if (translator_ &&
(translator_->always_show_comments() ||
static_cast<int>(cand.code().size()) <= translator_->spelling_hints())) {
return translator_->Spell(cand.code());
}
return string();
}
Expand Down Expand Up @@ -343,15 +343,14 @@ bool ScriptTranslation::Evaluate(Dictionary* dict, UserDictionary* user_dict) {
}
if (!phrase_ && !user_phrase_)
return false;
if (phrase_ && !phrase_->empty() && phrase_->begin()->first < 0) {
if (phrase_ && !phrase_->empty() && phrase_->begin()->first == -1) {
const auto& entry = phrase_->begin()->second.Peek();
prediction_ =
New<Phrase>(translator_->language(), "predicted_phrase", start_,
start_ + syllable_graph.interpreted_length, entry,
-phrase_->begin()->first);
start_ + syllable_graph.interpreted_length, entry);
prediction_->set_quality(std::exp(entry->weight) +
translator_->initial_quality());
phrase_->erase(phrase_->begin()->first);
phrase_->erase(-1);
}

if (phrase_)
Expand Down
22 changes: 3 additions & 19 deletions src/rime/gear/translator_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,8 @@ class Phrase : public Candidate {
const string& type,
size_t start,
size_t end,
const an<DictEntry>& entry,
size_t original_code_length = 0)
: Candidate(type, start, end),
language_(language),
entry_(entry),
original_code_length_(original_code_length) {}
const an<DictEntry>& entry)
: Candidate(type, start, end), language_(language), entry_(entry) {}
const string& text() const { return entry_->text; }
string comment() const { return entry_->comment; }
string preedit() const { return entry_->preedit; }
Expand All @@ -83,18 +79,7 @@ class Phrase : public Candidate {
an<PhraseSyllabifier> syllabifier() const { return syllabifier_; }
double weight() const { return entry_->weight; }
void set_weight(double weight) { entry_->weight = weight; }
Code& full_code() const { return entry_->code; }
Code code() const {
Code& full_code_ = full_code();
if (!original_code_length_)
return full_code_;
Code code;
code.insert(code.end(), full_code_.begin(),
original_code_length_
? full_code_.begin() + original_code_length_
: full_code_.end());
return code;
}
Code& code() const { return entry_->code; }
const DictEntry& entry() const { return *entry_; }
const Language* language() const { return language_; }
size_t matching_code_size() const {
Expand Down Expand Up @@ -123,7 +108,6 @@ class Phrase : public Candidate {
const Language* language_;
an<DictEntry> entry_;
an<PhraseSyllabifier> syllabifier_;
size_t original_code_length_;
};

//
Expand Down

0 comments on commit f8a297c

Please sign in to comment.