@@ -115,14 +115,16 @@ class ScriptTranslation : public Translation {
115115 Poet* poet,
116116 const string& input,
117117 size_t start,
118- size_t end_of_input)
118+ size_t end_of_input,
119+ int max_sentences)
119120 : translator_(translator),
120121 poet_ (poet),
121122 start_(start),
122123 end_of_input_(end_of_input),
123124 syllabifier_(
124125 New<ScriptSyllabifier>(translator, corrector, input, start)),
125- enable_correction_(corrector) {
126+ enable_correction_(corrector),
127+ max_sentences_(max_sentences) {
126128 set_exhausted (true );
127129 }
128130 bool Evaluate (Dictionary* dict, UserDictionary* user_dict);
@@ -136,7 +138,11 @@ class ScriptTranslation : public Translation {
136138 template <class QueryResult >
137139 void EnrollEntries (map<int , DictEntryList>& entries_by_end_pos,
138140 const an<QueryResult>& query_result);
141+ WordGraph PrepareForMakingSentence (Dictionary* dict,
142+ UserDictionary* user_dict);
139143 an<Sentence> MakeSentence (Dictionary* dict, UserDictionary* user_dict);
144+ deque<an<Sentence>> MakeSentences (Dictionary* dict,
145+ UserDictionary* user_dict);
140146
141147 ScriptTranslator* translator_;
142148 Poet* poet_;
@@ -146,7 +152,7 @@ class ScriptTranslation : public Translation {
146152
147153 an<DictEntryCollector> phrase_;
148154 an<UserDictEntryCollector> user_phrase_;
149- an<Sentence> sentence_ ;
155+ deque< an<Sentence>> sentences_ ;
150156
151157 an<Phrase> candidate_ = nullptr ;
152158 size_t candidate_index_ = 0 ;
@@ -163,6 +169,7 @@ class ScriptTranslation : public Translation {
163169
164170 const size_t max_corrections_ = 4 ;
165171 size_t correction_count_ = 0 ;
172+ int max_sentences_ = 1 ;
166173
167174 bool enable_correction_;
168175};
@@ -210,8 +217,9 @@ an<Translation> ScriptTranslator::Query(const string& input,
210217
211218 size_t end_of_input = engine_->context ()->input ().length ();
212219 // the translator should survive translations it creates
213- auto result = New<ScriptTranslation>(this , corrector_.get (), poet_.get (),
214- input, segment.start , end_of_input);
220+ auto result =
221+ New<ScriptTranslation>(this , corrector_.get (), poet_.get (), input,
222+ segment.start , end_of_input, max_sentences_);
215223 if (!result || !result->Evaluate (
216224 dict_.get (), enable_user_dict ? user_dict_.get () : NULL )) {
217225 return nullptr ;
@@ -484,7 +492,15 @@ bool ScriptTranslation::Evaluate(Dictionary* dict, UserDictionary* user_dict) {
484492 // make sentences when there is no exact-matching phrase candidate
485493 if (has_at_least_two_syllables && !has_reliable_phrase &&
486494 !has_reliable_user_phrase) {
487- sentence_ = MakeSentence (dict, user_dict);
495+ if (max_sentences_ > 1 )
496+ sentences_ = MakeSentences (dict, user_dict);
497+ else if (max_sentences_) {
498+ auto sentence = MakeSentence (dict, user_dict);
499+ if (sentence)
500+ sentences_ = {sentence};
501+ else
502+ sentences_.clear ();
503+ }
488504 }
489505
490506 return !CheckEmpty ();
@@ -501,7 +517,8 @@ bool ScriptTranslation::Next() {
501517 case kUninitialized :
502518 break ;
503519 case kSentence :
504- sentence_.reset ();
520+ if (!sentences_.empty ())
521+ sentences_.pop_front ();
505522 break ;
506523 case kUserPhrase : {
507524 UserDictEntryIterator& uter (user_phrase_iter_->second );
@@ -575,9 +592,9 @@ bool ScriptTranslation::PrepareCandidate() {
575592 candidate_ = nullptr ;
576593 return false ;
577594 }
578- if (sentence_ ) {
595+ if (!sentences_. empty () ) {
579596 candidate_source_ = kSentence ;
580- candidate_ = sentence_ ;
597+ candidate_ = sentences_[ 0 ] ;
581598 return true ;
582599 }
583600 const size_t full_code_length = end_of_input_ - start_;
@@ -675,8 +692,9 @@ void ScriptTranslation::EnrollEntries(
675692 }
676693}
677694
678- an<Sentence> ScriptTranslation::MakeSentence (Dictionary* dict,
679- UserDictionary* user_dict) {
695+ WordGraph ScriptTranslation::PrepareForMakingSentence (
696+ Dictionary* dict,
697+ UserDictionary* user_dict) {
680698 const int kMaxSyllablesForUserPhraseQuery = 5 ;
681699 const auto & syllable_graph = syllabifier_->syllable_graph ();
682700 WordGraph graph;
@@ -691,6 +709,28 @@ an<Sentence> ScriptTranslation::MakeSentence(Dictionary* dict,
691709 EnrollEntries (same_start_pos, dict->Lookup (syllable_graph, x.first ,
692710 &translator_->blacklist ()));
693711 }
712+ return graph;
713+ }
714+
715+ deque<an<Sentence>> ScriptTranslation::MakeSentences (
716+ Dictionary* dict,
717+ UserDictionary* user_dict) {
718+ const auto & syllable_graph = syllabifier_->syllable_graph ();
719+ WordGraph graph = PrepareForMakingSentence (dict, user_dict);
720+ auto sentences = poet_->MakeSentences (
721+ graph, syllable_graph.interpreted_length ,
722+ translator_->GetPrecedingText (start_), max_sentences_);
723+ for (auto & sentence : sentences) {
724+ sentence->Offset (start_);
725+ sentence->set_syllabifier (syllabifier_);
726+ }
727+ return sentences;
728+ }
729+
730+ an<Sentence> ScriptTranslation::MakeSentence (Dictionary* dict,
731+ UserDictionary* user_dict) {
732+ const auto & syllable_graph = syllabifier_->syllable_graph ();
733+ WordGraph graph = PrepareForMakingSentence (dict, user_dict);
694734 if (auto sentence =
695735 poet_->MakeSentence (graph, syllable_graph.interpreted_length ,
696736 translator_->GetPrecedingText (start_))) {
0 commit comments