tesseract  5.0.0-alpha-619-ge9db
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 186 of file ltrresultiterator.h.

Constructor & Destructor Documentation

◆ ChoiceIterator()

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

Definition at line 376 of file ltrresultiterator.cpp.

376  {
377  ASSERT_HOST(result_it.it_->word() != nullptr);
378  word_res_ = result_it.it_->word();
379  oemLSTM_ = word_res_->tesseract->AnyLSTMLang();
380  // Is there legacy engine related trained data?
381  bool oemLegacy = word_res_->tesseract->AnyTessLang();
382  // Is lstm_choice_mode activated?
383  bool lstm_choice_mode = word_res_->tesseract->lstm_choice_mode;
384  rating_coefficient_ = word_res_->tesseract->lstm_rating_coefficient;
385  blanks_before_word_ = result_it.BlanksBeforeWord();
386  BLOB_CHOICE_LIST* choices = nullptr;
387  tstep_index_ = &result_it.blob_index_;
388  if (oemLSTM_ && !word_res_->CTC_symbol_choices.empty()) {
389  if (!word_res_->CTC_symbol_choices[0].empty() &&
390  strcmp(word_res_->CTC_symbol_choices[0][0].first, " ")) {
391  blanks_before_word_ = 0;
392  }
393  auto index = *tstep_index_;
394  index += blanks_before_word_;
395  if (index < word_res_->CTC_symbol_choices.size()) {
396  LSTM_choices_ = &word_res_->CTC_symbol_choices[index];
397  filterSpaces();
398  }
399  }
400  if ((oemLegacy || !lstm_choice_mode) && word_res_->ratings != nullptr)
401  choices = word_res_->GetBlobChoices(result_it.blob_index_);
402  if (choices != nullptr && !choices->empty()) {
403  choice_it_ = new BLOB_CHOICE_IT(choices);
404  choice_it_->mark_cycle_pt();
405  } else {
406  choice_it_ = nullptr;
407  }
408  if (LSTM_choices_ != nullptr && !LSTM_choices_->empty()) {
409  LSTM_choice_it_ = LSTM_choices_->begin();
410  }
411 }

◆ ~ChoiceIterator()

tesseract::ChoiceIterator::~ChoiceIterator ( )

Definition at line 412 of file ltrresultiterator.cpp.

412  {
413  delete choice_it_;
414 }

Member Function Documentation

◆ Confidence()

float tesseract::ChoiceIterator::Confidence ( ) const

Definition at line 455 of file ltrresultiterator.cpp.

455  {
456  float confidence;
457  if (oemLSTM_ && LSTM_choices_ != nullptr && !LSTM_choices_->empty()) {
458  std::pair<const char*, float> choice = *LSTM_choice_it_;
459  confidence = 100 - rating_coefficient_ * choice.second;
460  } else {
461  if (choice_it_ == nullptr)
462  return 0.0f;
463  confidence = 100 + 5 * choice_it_->data()->certainty();
464  }
465  return ClipToRange(confidence, 0.0f, 100.0f);
466 }

◆ GetUTF8Text()

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

Definition at line 437 of file ltrresultiterator.cpp.

437  {
438  if (oemLSTM_ && LSTM_choices_ != nullptr && !LSTM_choices_->empty()) {
439  std::pair<const char*, float> choice = *LSTM_choice_it_;
440  return choice.first;
441  } else {
442  if (choice_it_ == nullptr)
443  return nullptr;
444  UNICHAR_ID id = choice_it_->data()->unichar_id();
445  return word_res_->uch_set->id_to_unichar_ext(id);
446  }
447 }

◆ Next()

bool tesseract::ChoiceIterator::Next ( )

Definition at line 418 of file ltrresultiterator.cpp.

418  {
419  if (oemLSTM_ && LSTM_choices_ != nullptr && !LSTM_choices_->empty()) {
420  if (LSTM_choice_it_ != LSTM_choices_->end() &&
421  next(LSTM_choice_it_) == LSTM_choices_->end()) {
422  return false;
423  } else {
424  ++LSTM_choice_it_;
425  return true;
426  }
427  } else {
428  if (choice_it_ == nullptr)
429  return false;
430  choice_it_->forward();
431  return !choice_it_->cycled_list();
432  }
433 }

◆ Timesteps()

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

Definition at line 470 of file ltrresultiterator.cpp.

470  {
471  int offset = *tstep_index_ + blanks_before_word_;
472  if (offset >= word_res_->segmented_timesteps.size() || !oemLSTM_) {
473  return nullptr;
474  }
475  return &word_res_->segmented_timesteps[offset];
476 }

The documentation for this class was generated from the following files:
ClipToRange
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:106
UNICHARSET::id_to_unichar_ext
const char * id_to_unichar_ext(UNICHAR_ID id) const
Definition: unicharset.cpp:298
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:87
WERD_RES::uch_set
const UNICHARSET * uch_set
Definition: pageres.h:197
WERD_RES::segmented_timesteps
std::vector< std::vector< std::vector< std::pair< const char *, float > > > > segmented_timesteps
Definition: pageres.h:218
UNICHAR_ID
int UNICHAR_ID
Definition: unichar.h:36
WERD_RES::word
WERD * word
Definition: pageres.h:180