Skip to content

Commit

Permalink
refactor(poet): support making sentences in left associate fashion
Browse files Browse the repository at this point in the history
  • Loading branch information
lotem committed Apr 16, 2019
1 parent 588ddcf commit 57c70a7
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
25 changes: 20 additions & 5 deletions src/rime/gear/poet.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//
// 2011-10-06 GONG Chen <chen.sst@gmail.com>
//
#include <algorithm>
#include <functional>
#include <rime/candidate.h>
#include <rime/config.h>
#include <rime/dict/vocabulary.h>
Expand All @@ -21,12 +23,24 @@ inline static Grammar* create_grammar(Config* config) {
return nullptr;
}

Poet::Poet(const Language* language, Config* config)
Poet::Poet(const Language* language, Config* config, Compare compare)
: language_(language),
grammar_(create_grammar(config)) {}
grammar_(create_grammar(config)),
compare_(compare) {}

Poet::~Poet() {}

bool Poet::LeftAssociateCompare(const Sentence& one, const Sentence& other) {
return one.weight() < other.weight() || ( // left associate if even
one.weight() == other.weight() && (
one.size() > other.size() || ( // less components is more favorable
one.size() == other.size() &&
std::lexicographical_compare(one.syllable_lengths().begin(),
one.syllable_lengths().end(),
other.syllable_lengths().begin(),
other.syllable_lengths().end()))));
}

an<Sentence> Poet::MakeSentence(const WordGraph& graph,
size_t total_length) {
// TODO: save more intermediate sentence candidates
Expand All @@ -49,9 +63,10 @@ an<Sentence> Poet::MakeSentence(const WordGraph& graph,
auto new_sentence = New<Sentence>(*sentences[start_pos]);
new_sentence->Extend(*entry, end_pos, is_rear, grammar_.get());
if (sentences.find(end_pos) == sentences.end() ||
sentences[end_pos]->weight() < new_sentence->weight()) {
DLOG(INFO) << "updated sentences " << end_pos << ") with '"
<< new_sentence->text() << "', " << new_sentence->weight();
compare_(*sentences[end_pos], *new_sentence)) {
DLOG(INFO) << "updated sentences " << end_pos << ") with "
<< new_sentence->text() << " weight: "
<< new_sentence->weight();
sentences[end_pos] = std::move(new_sentence);
}
}
Expand Down
14 changes: 12 additions & 2 deletions src/rime/gear/poet.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,24 @@ class Language;

class Poet {
public:
Poet(const Language* language, Config* config);
// sentence "less", used to compare sentences of the same input range.
using Compare = function<bool (const Sentence&, const Sentence&)>;

static bool CompareWeight(const Sentence& one, const Sentence& other) {
return one.weight() < other.weight();
}
static bool LeftAssociateCompare(const Sentence& one, const Sentence& other);

Poet(const Language* language, Config* config,
Compare compare = CompareWeight);
~Poet();

an<Sentence> MakeSentence(const WordGraph& graph, size_t total_length);

protected:
private:
const Language* language_;
the<Grammar> grammar_;
Compare compare_;
};

} // namespace rime
Expand Down
2 changes: 1 addition & 1 deletion src/rime/gear/table_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ TableTranslator::TableTranslator(const Ticket& ticket)
config->GetInt(name_space_ + "/max_homographs",
&max_homographs_);
if (enable_sentence_ || sentence_over_completion_) {
poet_.reset(new Poet(language(), config));
poet_.reset(new Poet(language(), config, Poet::LeftAssociateCompare));
}
}
if (enable_encoder_ && user_dict_) {
Expand Down
4 changes: 2 additions & 2 deletions src/rime/gear/translator_commons.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void Sentence::Extend(const DictEntry& entry,
size_t end_pos,
bool is_rear,
Grammar* grammar) {
entry_->weight += Grammar::Evaluate(entry_->text, entry, is_rear, grammar);
entry_->weight += Grammar::Evaluate(text(), entry, is_rear, grammar);
entry_->text.append(entry.text);
entry_->code.insert(entry_->code.end(),
entry.code.begin(),
Expand All @@ -101,7 +101,7 @@ void Sentence::Extend(const DictEntry& entry,
syllable_lengths_.push_back(end_pos - end());
set_end(end_pos);
DLOG(INFO) << "extend sentence " << end_pos << ") "
<< entry_->text << " : " << entry_->weight;
<< text() << " weight: " << weight();
}

void Sentence::Offset(size_t offset) {
Expand Down
16 changes: 12 additions & 4 deletions src/rime/gear/translator_commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ class Language;
class Phrase : public Candidate {
public:
Phrase(const Language* language,
const string& type, size_t start, size_t end,
const string& type,
size_t start,
size_t end,
const an<DictEntry>& entry)
: Candidate(type, start, end),
language_(language),
Expand Down Expand Up @@ -112,9 +114,7 @@ class Grammar;
class Sentence : public Phrase {
public:
Sentence(const Language* language)
: Phrase(language, "sentence", 0, 0, New<DictEntry>()) {
entry_->weight = 0.0;
}
: Phrase(language, "sentence", 0, 0, New<DictEntry>()) {}
Sentence(const Sentence& other)
: Phrase(other),
components_(other.components_),
Expand All @@ -127,6 +127,14 @@ class Sentence : public Phrase {
Grammar* grammar);
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_;
}
Expand Down

0 comments on commit 57c70a7

Please sign in to comment.