From 1a348480b2709ab74711f9accc1899055b4ba9a1 Mon Sep 17 00:00:00 2001 From: lotem Date: Fri, 26 Jul 2013 14:41:39 +0800 Subject: [PATCH] table_translator: deleting / moving by syllable in sentences. --- src/gear/table_translator.cc | 44 ++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/gear/table_translator.cc b/src/gear/table_translator.cc index 1c938020f..eaeb6e9b5 100644 --- a/src/gear/table_translator.cc +++ b/src/gear/table_translator.cc @@ -289,8 +289,8 @@ class SentenceTranslation : public Translation { UserDictEntryCollector* user_phrase_collector, const std::string& input, size_t start); - bool Next(); - shared_ptr Peek(); + virtual bool Next(); + virtual shared_ptr Peek(); protected: void PrepareSentence(); @@ -306,6 +306,18 @@ class SentenceTranslation : public Translation { size_t start_; }; +class SentenceSyllabification : public Syllabification { + public: + SentenceSyllabification(shared_ptr sentence) + : syllabified_(sentence) { + } + virtual size_t PreviousStop(size_t caret_pos) const; + virtual size_t NextStop(size_t caret_pos) const; + + protected: + shared_ptr syllabified_; +}; + SentenceTranslation::SentenceTranslation(TableTranslator* translator, shared_ptr sentence, DictEntryCollector* collector, @@ -380,6 +392,8 @@ void SentenceTranslation::PrepareSentence() { if (!sentence_) return; sentence_->Offset(start_); sentence_->set_comment(kUnityTableEncoder); + sentence_->set_syllabification( + make_shared(sentence_)); if (!translator_) return; std::string preedit(input_); @@ -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 shared_ptr get_first_entry(Iter& iter, bool filter_by_charset) { if (iter.exhausted())