tesseract v5.3.3.20231005
tesseract::ChoiceIterator Class Reference

#include <ltrresultiterator.h>

Public Member Functions

 ChoiceIterator (const LTRResultIterator &result_it)
 
 ~ChoiceIterator ()
 
bool Next ()
 
const char * GetUTF8Text () const
 
float Confidence () const
 
std::vector< std::vector< std::pair< const char *, float > > > * Timesteps () const
 

Detailed Description

Definition at line 180 of file ltrresultiterator.h.

Constructor & Destructor Documentation

◆ ChoiceIterator()

tesseract::ChoiceIterator::ChoiceIterator ( const LTRResultIterator result_it)
explicit

Definition at line 382 of file ltrresultiterator.cpp.

382 {
383 ASSERT_HOST(result_it.it_->word() != nullptr);
384 word_res_ = result_it.it_->word();
385 oemLSTM_ = word_res_->tesseract->AnyLSTMLang();
386 // Is there legacy engine related trained data?
387 bool oemLegacy = word_res_->tesseract->AnyTessLang();
388 // Is lstm_choice_mode activated?
389 bool lstm_choice_mode = word_res_->tesseract->lstm_choice_mode;
390 rating_coefficient_ = word_res_->tesseract->lstm_rating_coefficient;
391 blanks_before_word_ = result_it.BlanksBeforeWord();
392 BLOB_CHOICE_LIST *choices = nullptr;
393 tstep_index_ = &result_it.blob_index_;
394 if (oemLSTM_ && !word_res_->CTC_symbol_choices.empty()) {
395 if (!word_res_->CTC_symbol_choices[0].empty() &&
396 strcmp(word_res_->CTC_symbol_choices[0][0].first, " ")) {
397 blanks_before_word_ = 0;
398 }
399 unsigned index = *tstep_index_;
400 index += blanks_before_word_;
401 if (index < word_res_->CTC_symbol_choices.size()) {
402 LSTM_choices_ = &word_res_->CTC_symbol_choices[index];
403 filterSpaces();
404 }
405 }
406 if ((oemLegacy || !lstm_choice_mode) && word_res_->ratings != nullptr) {
407 choices = word_res_->GetBlobChoices(result_it.blob_index_);
408 }
409 if (choices != nullptr && !choices->empty()) {
410 choice_it_ = new BLOB_CHOICE_IT(choices);
411 choice_it_->mark_cycle_pt();
412 } else {
413 choice_it_ = nullptr;
414 }
415 if (LSTM_choices_ != nullptr && !LSTM_choices_->empty()) {
416 LSTM_choice_it_ = LSTM_choices_->begin();
417 }
418}
#define ASSERT_HOST(x)
Definition: errcode.h:54
bool AnyTessLang() const
bool AnyLSTMLang() const
tesseract::Tesseract * tesseract
Definition: pageres.h:278
BLOB_CHOICE_LIST * GetBlobChoices(int index) const
Definition: pageres.cpp:779
MATRIX * ratings
Definition: pageres.h:235
std::vector< std::vector< std::pair< const char *, float > > > CTC_symbol_choices
Definition: pageres.h:224

◆ ~ChoiceIterator()

tesseract::ChoiceIterator::~ChoiceIterator ( )

Definition at line 419 of file ltrresultiterator.cpp.

419 {
420 delete choice_it_;
421}

Member Function Documentation

◆ Confidence()

float tesseract::ChoiceIterator::Confidence ( ) const

Definition at line 464 of file ltrresultiterator.cpp.

464 {
465 float confidence;
466 if (oemLSTM_ && LSTM_choices_ != nullptr && !LSTM_choices_->empty()) {
467 std::pair<const char *, float> choice = *LSTM_choice_it_;
468 confidence = 100 - rating_coefficient_ * choice.second;
469 } else {
470 if (choice_it_ == nullptr) {
471 return 0.0f;
472 }
473 confidence = 100 + 5 * choice_it_->data()->certainty();
474 }
475 return ClipToRange(confidence, 0.0f, 100.0f);
476}
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:105

◆ GetUTF8Text()

const char * tesseract::ChoiceIterator::GetUTF8Text ( ) const

Definition at line 445 of file ltrresultiterator.cpp.

445 {
446 if (oemLSTM_ && LSTM_choices_ != nullptr && !LSTM_choices_->empty()) {
447 std::pair<const char *, float> choice = *LSTM_choice_it_;
448 return choice.first;
449 } else {
450 if (choice_it_ == nullptr) {
451 return nullptr;
452 }
453 UNICHAR_ID id = choice_it_->data()->unichar_id();
454 return word_res_->uch_set->id_to_unichar_ext(id);
455 }
456}
int UNICHAR_ID
Definition: unichar.h:34
const UNICHARSET * uch_set
Definition: pageres.h:201
const char * id_to_unichar_ext(UNICHAR_ID id) const
Definition: unicharset.cpp:287

◆ Next()

bool tesseract::ChoiceIterator::Next ( )

Definition at line 425 of file ltrresultiterator.cpp.

425 {
426 if (oemLSTM_ && LSTM_choices_ != nullptr && !LSTM_choices_->empty()) {
427 if (LSTM_choice_it_ == LSTM_choices_->end() ||
428 next(LSTM_choice_it_) == LSTM_choices_->end()) {
429 return false;
430 } else {
431 ++LSTM_choice_it_;
432 return true;
433 }
434 } else {
435 if (choice_it_ == nullptr) {
436 return false;
437 }
438 choice_it_->forward();
439 return !choice_it_->cycled_list();
440 }
441}
def next(obj)
Definition: ast.py:56

◆ Timesteps()

std::vector< std::vector< std::pair< const char *, float > > > * tesseract::ChoiceIterator::Timesteps ( ) const

Definition at line 479 of file ltrresultiterator.cpp.

479 {
480 unsigned offset = *tstep_index_ + blanks_before_word_;
481 if (offset >= word_res_->segmented_timesteps.size() || !oemLSTM_) {
482 return nullptr;
483 }
484 return &word_res_->segmented_timesteps[offset];
485}
std::vector< std::vector< std::vector< std::pair< const char *, float > > > > segmented_timesteps
Definition: pageres.h:222

The documentation for this class was generated from the following files: