Skip to content

Commit 1a34848

Browse files
committed
table_translator: deleting / moving by syllable in sentences.
1 parent 15d8d72 commit 1a34848

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/gear/table_translator.cc

+42-2
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ class SentenceTranslation : public Translation {
289289
UserDictEntryCollector* user_phrase_collector,
290290
const std::string& input,
291291
size_t start);
292-
bool Next();
293-
shared_ptr<Candidate> Peek();
292+
virtual bool Next();
293+
virtual shared_ptr<Candidate> Peek();
294294

295295
protected:
296296
void PrepareSentence();
@@ -306,6 +306,18 @@ class SentenceTranslation : public Translation {
306306
size_t start_;
307307
};
308308

309+
class SentenceSyllabification : public Syllabification {
310+
public:
311+
SentenceSyllabification(shared_ptr<Sentence> sentence)
312+
: syllabified_(sentence) {
313+
}
314+
virtual size_t PreviousStop(size_t caret_pos) const;
315+
virtual size_t NextStop(size_t caret_pos) const;
316+
317+
protected:
318+
shared_ptr<Sentence> syllabified_;
319+
};
320+
309321
SentenceTranslation::SentenceTranslation(TableTranslator* translator,
310322
shared_ptr<Sentence> sentence,
311323
DictEntryCollector* collector,
@@ -380,6 +392,8 @@ void SentenceTranslation::PrepareSentence() {
380392
if (!sentence_) return;
381393
sentence_->Offset(start_);
382394
sentence_->set_comment(kUnityTableEncoder);
395+
sentence_->set_syllabification(
396+
make_shared<SentenceSyllabification>(sentence_));
383397

384398
if (!translator_) return;
385399
std::string preedit(input_);
@@ -423,6 +437,32 @@ bool SentenceTranslation::PreferUserPhrase() const {
423437
return false;
424438
}
425439

440+
size_t SentenceSyllabification::PreviousStop(size_t caret_pos) const {
441+
if (syllabified_) {
442+
size_t stop = syllabified_->start();
443+
BOOST_FOREACH(size_t len, syllabified_->syllable_lengths()) {
444+
if (stop + len >= caret_pos) {
445+
return stop;
446+
}
447+
stop += len;
448+
}
449+
}
450+
return caret_pos;
451+
}
452+
453+
size_t SentenceSyllabification::NextStop(size_t caret_pos) const {
454+
if (syllabified_) {
455+
size_t stop = syllabified_->start();
456+
BOOST_FOREACH(size_t len, syllabified_->syllable_lengths()) {
457+
stop += len;
458+
if (stop > caret_pos) {
459+
return stop;
460+
}
461+
}
462+
}
463+
return caret_pos;
464+
}
465+
426466
template <class Iter>
427467
shared_ptr<DictEntry> get_first_entry(Iter& iter, bool filter_by_charset) {
428468
if (iter.exhausted())

0 commit comments

Comments
 (0)