Repository: tesseract-ocr/tesseract
File: lstm/lstmtrainer.cpp (C++)

Code before:
  tprintf("Loaded file %s, unpacking...\n", filename);
  return checkpoint_reader_->Run(data, this);
}

// Initializes the character set encode/decode mechanism.
// train_flags control training behavior according to the TrainingFlags
// enum, including character set encoding.
// script_dir is required for TF_COMPRESS_UNICHARSET, and, if provided,
// fully initializes the unicharset from the universal unicharsets.
// Note: Call before InitNetwork!
void LSTMTrainer::InitCharSet(const UNICHARSET& unicharset,
                              const STRING& script_dir, int train_flags) {
  // Call before InitNetwork.
  ASSERT_HOST(network_ == NULL);
  EmptyConstructor();
  training_flags_ = train_flags;
  ccutil_.unicharset.CopyFrom(unicharset);
  null_char_ = GetUnicharset().has_special_codes() ? UNICHAR_BROKEN
                                                   : GetUnicharset().size();
  SetUnicharsetProperties(script_dir);
}

// Initializes the character set encode/decode mechanism directly from a
// previously setup UNICHARSET and UnicharCompress.
// ctc_mode controls how the truth text is mapped to the network targets.
// Note: Call before InitNetwork!
void LSTMTrainer::InitCharSet(const UNICHARSET& unicharset,
                              const UnicharCompress& recoder) {
  // Call before InitNetwork.
  ASSERT_HOST(network_ == NULL);
  EmptyConstructor();
  int flags = TF_COMPRESS_UNICHARSET;
  training_flags_ = static_cast<TrainingFlags>(flags);
  ccutil_.unicharset.CopyFrom(unicharset);
  recoder_ = recoder;
  null_char_ = GetUnicharset().has_special_codes() ? UNICHAR_BROKEN
                                                   : GetUnicharset().size();
  RecodedCharID code;
  recoder_.EncodeUnichar(null_char_, &code);
  null_char_ = code(0);
  // Space should encode as itself.
  recoder_.EncodeUnichar(UNICHAR_SPACE, &code);
  ASSERT_HOST(code(0) == UNICHAR_SPACE);
}

// Initializes the trainer with a network_spec in the network description
// net_flags control network behavior according to the NetworkFlags enum.
// There isn't really much difference between them - only where the effects
// are implemented.
// For other args see NetworkBuilder::InitNetwork.
// Note: Be sure to call InitCharSet before InitNetwork!

Review comment (suggestion):
`recoder` can be `NULL`.

In the same file you have this:
`LSTMTrainer::EncodeString(const STRING& str, const UNICHARSET& unicharset, const UnicharCompress* recoder, ...`
and
`if (recoder != NULL) {`

Code after:
  tprintf("Loaded file %s, unpacking...\n", filename);
  return checkpoint_reader_->Run(data, this);
}

// Initializes the character set encode/decode mechanism.
// train_flags control training behavior according to the TrainingFlags
// enum, including character set encoding.
// script_dir is required for TF_COMPRESS_UNICHARSET, and, if provided,
// fully initializes the unicharset from the universal unicharsets.
// Note: Call before InitNetwork!
void LSTMTrainer::InitCharSet(const UNICHARSET& unicharset,
                              const STRING& script_dir, int train_flags) {
  EmptyConstructor();
  training_flags_ = train_flags;
  ccutil_.unicharset.CopyFrom(unicharset);
  null_char_ = GetUnicharset().has_special_codes() ? UNICHAR_BROKEN
                                                   : GetUnicharset().size();
  SetUnicharsetProperties(script_dir);
}

// Initializes the character set encode/decode mechanism directly from a
// previously setup UNICHARSET and UnicharCompress.
// ctc_mode controls how the truth text is mapped to the network targets.
// Note: Call before InitNetwork!
void LSTMTrainer::InitCharSet(const UNICHARSET& unicharset,
                              const UnicharCompress& recoder) {
  EmptyConstructor();
  int flags = TF_COMPRESS_UNICHARSET;
  training_flags_ = static_cast<TrainingFlags>(flags);
  ccutil_.unicharset.CopyFrom(unicharset);
  recoder_ = recoder;
  null_char_ = GetUnicharset().has_special_codes() ? UNICHAR_BROKEN
                                                   : GetUnicharset().size();
  RecodedCharID code;
  recoder_.EncodeUnichar(null_char_, &code);
  null_char_ = code(0);
  // Space should encode as itself.
  recoder_.EncodeUnichar(UNICHAR_SPACE, &code);
  ASSERT_HOST(code(0) == UNICHAR_SPACE);
}

// Initializes the trainer with a network_spec in the network description
// net_flags control network behavior according to the NetworkFlags enum.
// There isn't really much difference between them - only where the effects
// are implemented.
// For other args see NetworkBuilder::InitNetwork.
// Note: Be sure to call InitCharSet before InitNetwork!
bool LSTMTrainer::InitNetwork(const STRING& network_spec, int append_index,
                              int net_flags, float weight_range,
                              float learning_rate, float momentum) {
  // Call after InitCharSet.