Skip to content

Commit

Permalink
table_translator: deleting / moving by syllable in sentences.
Browse files Browse the repository at this point in the history
  • Loading branch information
lotem committed Jul 26, 2013
1 parent 15d8d72 commit 1a34848
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions src/gear/table_translator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ class SentenceTranslation : public Translation {
UserDictEntryCollector* user_phrase_collector,
const std::string& input,
size_t start);
bool Next();
shared_ptr<Candidate> Peek();
virtual bool Next();
virtual shared_ptr<Candidate> Peek();

protected:
void PrepareSentence();
Expand All @@ -306,6 +306,18 @@ class SentenceTranslation : public Translation {
size_t start_;
};

class SentenceSyllabification : public Syllabification {
public:
SentenceSyllabification(shared_ptr<Sentence> sentence)
: syllabified_(sentence) {
}
virtual size_t PreviousStop(size_t caret_pos) const;
virtual size_t NextStop(size_t caret_pos) const;

protected:
shared_ptr<Sentence> syllabified_;
};

SentenceTranslation::SentenceTranslation(TableTranslator* translator,
shared_ptr<Sentence> sentence,
DictEntryCollector* collector,
Expand Down Expand Up @@ -380,6 +392,8 @@ void SentenceTranslation::PrepareSentence() {
if (!sentence_) return;
sentence_->Offset(start_);
sentence_->set_comment(kUnityTableEncoder);
sentence_->set_syllabification(
make_shared<SentenceSyllabification>(sentence_));

if (!translator_) return;
std::string preedit(input_);
Expand Down Expand Up @@ -423,6 +437,32 @@ bool SentenceTranslation::PreferUserPhrase() const {
return false;
}

size_t SentenceSyllabification::PreviousStop(size_t caret_pos) const {
if (syllabified_) {
size_t stop = syllabified_->start();
BOOST_FOREACH(size_t len, syllabified_->syllable_lengths()) {
if (stop + len >= caret_pos) {
return stop;
}
stop += len;
}
}
return caret_pos;
}

size_t SentenceSyllabification::NextStop(size_t caret_pos) const {
if (syllabified_) {
size_t stop = syllabified_->start();
BOOST_FOREACH(size_t len, syllabified_->syllable_lengths()) {
stop += len;
if (stop > caret_pos) {
return stop;
}
}
}
return caret_pos;
}

template <class Iter>
shared_ptr<DictEntry> get_first_entry(Iter& iter, bool filter_by_charset) {
if (iter.exhausted())
Expand Down

0 comments on commit 1a34848

Please sign in to comment.