Skip to content

Commit

Permalink
Fix: Incorrect or missing pronunciation of sentences when composition…
Browse files Browse the repository at this point in the history
…s are mixed with candidates from user dictionary
  • Loading branch information
graphemecluster committed Nov 28, 2023
1 parent d8667c9 commit ae2e49c
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 45 deletions.
6 changes: 5 additions & 1 deletion src/rime/dict/user_db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ UserDbValue::UserDbValue(const string& value) {
}

void UserDbValue::AppendElements(const DictEntry& entry) {
if (auto commit_entry = dynamic_cast<const CommitEntry*>(&entry)) {
auto user_dict_entry = dynamic_cast<const UserDictEntry*>(&entry);
if (user_dict_entry && user_dict_entry->elements.size() > 1) {
elements.insert(elements.end(), user_dict_entry->elements.begin(),
user_dict_entry->elements.end());
} else if (auto commit_entry = dynamic_cast<const CommitEntry*>(&entry)) {
for (const DictEntry* element : commit_entry->elements) {
AppendElements(*element);
}
Expand Down
4 changes: 2 additions & 2 deletions src/rime/gear/memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ void CommitEntry::AppendPhrase(const an<Phrase>& phrase) {
Code phrase_code = phrase->code();
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);
for (const DictEntry* e : sentence->components()) {
elements.push_back(e);
}
} else {
elements.push_back(&phrase->entry());
Expand Down
2 changes: 1 addition & 1 deletion src/rime/gear/poet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ an<Sentence> Poet::MakeSentenceWithStrategy(const WordGraph& graph,
for (const auto* c : best.components()) {
if (!c->entry)
continue;
sentence->Extend(*c->entry, c->end_pos, c->weight);
sentence->Extend(c->entry, c->end_pos, c->weight);
}
return sentence;
}
Expand Down
36 changes: 3 additions & 33 deletions src/rime/gear/script_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,39 +73,6 @@ static bool syllabify_dfs(SyllabifyTask* task,

} // anonymous namespace

class ScriptSyllabifier : public PhraseSyllabifier {
public:
ScriptSyllabifier(ScriptTranslator* translator,
Corrector* corrector,
const string& input,
size_t start)
: translator_(translator),
input_(input),
start_(start),
syllabifier_(translator->delimiters(),
translator->enable_completion(),
translator->strict_spelling()) {
if (corrector) {
syllabifier_.EnableCorrection(corrector);
}
}

virtual Spans Syllabify(const Phrase* phrase);
size_t BuildSyllableGraph(Prism& prism);
string GetPreeditString(const Phrase& cand) const;
string GetOriginalSpelling(const Phrase& cand) const;
bool IsCandidateCorrection(const Phrase& cand) const;

const SyllableGraph& syllable_graph() const { return syllable_graph_; }

protected:
ScriptTranslator* translator_;
string input_;
size_t start_;
Syllabifier syllabifier_;
SyllableGraph syllable_graph_;
};

class ScriptTranslation : public Translation {
public:
ScriptTranslation(ScriptTranslator* translator,
Expand Down Expand Up @@ -155,6 +122,8 @@ class ScriptTranslation : public Translation {
size_t cand_count_ = 0;

bool enable_correction_;

WordGraph graph_;
};

// ScriptTranslator implementation
Expand Down Expand Up @@ -587,6 +556,7 @@ an<Sentence> ScriptTranslation::MakeSentence(Dictionary* dict,
translator_->GetPrecedingText(start_))) {
sentence->Offset(start_);
sentence->set_syllabifier(syllabifier_);
graph_ = graph; // Prevent deallocation
return sentence;
}
return nullptr;
Expand Down
34 changes: 34 additions & 0 deletions src/rime/gear/script_translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,40 @@ class ScriptTranslator : public Translator,
the<Poet> poet_;
};

class ScriptSyllabifier : public PhraseSyllabifier {
public:
ScriptSyllabifier(ScriptTranslator* translator,
Corrector* corrector,
const string& input,
size_t start)
: translator_(translator),
input_(input),
start_(start),
syllabifier_(translator->delimiters(),
translator->enable_completion(),
translator->strict_spelling()) {
if (corrector) {
syllabifier_.EnableCorrection(corrector);
}
}

virtual Spans Syllabify(const Phrase* phrase);
size_t BuildSyllableGraph(Prism& prism);
string GetPreeditString(const Phrase& cand) const;
string GetOriginalSpelling(const Phrase& cand) const;
bool IsCandidateCorrection(const Phrase& cand) const;

ScriptTranslator* translator() const { return translator_; }
const SyllableGraph& syllable_graph() const { return syllable_graph_; }

protected:
ScriptTranslator* translator_;
string input_;
size_t start_;
Syllabifier syllabifier_;
SyllableGraph syllable_graph_;
};

} // namespace rime

#endif // RIME_SCRIPT_TRANSLATOR_H_
8 changes: 4 additions & 4 deletions src/rime/gear/translator_commons.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ bool Spans::HasVertex(size_t vertex) const {

// Sentence

void Sentence::Extend(const DictEntry& another,
void Sentence::Extend(const DictEntry* another,
size_t end_pos,
double new_weight) {
entry_->weight = new_weight;
entry_->text.append(another.text);
entry_->code.insert(entry_->code.end(), another.code.begin(),
another.code.end());
entry_->text.append(another->text);
entry_->code.insert(entry_->code.end(), another->code.begin(),
another->code.end());
components_.push_back(another);
word_lengths_.push_back(end_pos - end());
set_end(end_pos);
Expand Down
7 changes: 4 additions & 3 deletions src/rime/gear/translator_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Phrase : public Candidate {
void set_syllabifier(an<PhraseSyllabifier> syllabifier) {
syllabifier_ = syllabifier;
}
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; }
Expand Down Expand Up @@ -119,18 +120,18 @@ class Sentence : public Phrase {
word_lengths_(other.word_lengths_) {
entry_ = New<DictEntry>(other.entry());
}
void Extend(const DictEntry& another, size_t end_pos, double new_weight);
void Extend(const DictEntry* another, size_t end_pos, double new_weight);
void Offset(size_t offset);

bool empty() const { return components_.empty(); }

size_t size() const { return components_.size(); }

const vector<DictEntry>& components() const { return components_; }
const vector<const DictEntry*>& components() const { return components_; }
const vector<size_t>& word_lengths() const { return word_lengths_; }

protected:
vector<DictEntry> components_;
vector<const DictEntry*> components_;
vector<size_t> word_lengths_;
};

Expand Down

0 comments on commit ae2e49c

Please sign in to comment.