tesseract  4.00.00dev
tesseract::RecodeBeamSearch Class Reference

#include <recodebeam.h>

Public Member Functions

 RecodeBeamSearch (const UnicharCompress &recoder, int null_char, bool simple_text, Dict *dict)
 
void Decode (const NetworkIO &output, double dict_ratio, double cert_offset, double worst_dict_cert, const UNICHARSET *charset)
 
void Decode (const GENERIC_2D_ARRAY< float > &output, double dict_ratio, double cert_offset, double worst_dict_cert, const UNICHARSET *charset)
 
void ExtractBestPathAsLabels (GenericVector< int > *labels, GenericVector< int > *xcoords) const
 
void ExtractBestPathAsUnicharIds (bool debug, const UNICHARSET *unicharset, GenericVector< int > *unichar_ids, GenericVector< float > *certs, GenericVector< float > *ratings, GenericVector< int > *xcoords) const
 
void ExtractBestPathAsWords (const TBOX &line_box, float scale_factor, bool debug, const UNICHARSET *unicharset, PointerVector< WERD_RES > *words)
 
void DebugBeams (const UNICHARSET &unicharset) const
 

Static Public Member Functions

static int LengthFromBeamsIndex (int index)
 
static NodeContinuation ContinuationFromBeamsIndex (int index)
 
static bool IsDawgFromBeamsIndex (int index)
 
static int BeamIndex (bool is_dawg, NodeContinuation cont, int length)
 

Static Public Attributes

static const float kMinCertainty = -20.0f
 
static const int kNumLengths = RecodedCharID::kMaxCodeLen + 1
 
static const int kNumBeams = 2 * NC_COUNT * kNumLengths
 

Detailed Description

Definition at line 176 of file recodebeam.h.

Constructor & Destructor Documentation

◆ RecodeBeamSearch()

tesseract::RecodeBeamSearch::RecodeBeamSearch ( const UnicharCompress recoder,
int  null_char,
bool  simple_text,
Dict dict 
)

Definition at line 62 of file recodebeam.cpp.

64  : recoder_(recoder),
65  beam_size_(0),
66  top_code_(-1),
67  second_code_(-1),
68  dict_(dict),
69  space_delimited_(true),
70  is_simple_text_(simple_text),
71  null_char_(null_char) {
72  if (dict_ != NULL && !dict_->IsSpaceDelimitedLang()) space_delimited_ = false;
73 }
bool IsSpaceDelimitedLang() const
Returns true if the language is space-delimited (not CJ, or T).
Definition: dict.cpp:853

Member Function Documentation

◆ BeamIndex()

static int tesseract::RecodeBeamSearch::BeamIndex ( bool  is_dawg,
NodeContinuation  cont,
int  length 
)
inlinestatic

Definition at line 228 of file recodebeam.h.

228  {
229  return (is_dawg * NC_COUNT + cont) * kNumLengths + length;
230  }
static const int kNumLengths
Definition: recodebeam.h:215

◆ ContinuationFromBeamsIndex()

static NodeContinuation tesseract::RecodeBeamSearch::ContinuationFromBeamsIndex ( int  index)
inlinestatic

Definition at line 221 of file recodebeam.h.

221  {
222  return static_cast<NodeContinuation>((index / kNumLengths) % NC_COUNT);
223  }
static const int kNumLengths
Definition: recodebeam.h:215
NodeContinuation
Definition: recodebeam.h:69

◆ DebugBeams()

void tesseract::RecodeBeamSearch::DebugBeams ( const UNICHARSET unicharset) const

Definition at line 210 of file recodebeam.cpp.

210  {
211  for (int p = 0; p < beam_size_; ++p) {
212  for (int d = 0; d < 2; ++d) {
213  for (int c = 0; c < NC_COUNT; ++c) {
214  NodeContinuation cont = static_cast<NodeContinuation>(c);
215  int index = BeamIndex(d, cont, 0);
216  if (beam_[p]->beams_[index].empty()) continue;
217  // Print all the best scoring nodes for each unichar found.
218  tprintf("Position %d: %s+%s beam\n", p, d ? "Dict" : "Non-Dict",
219  kNodeContNames[c]);
220  DebugBeamPos(unicharset, beam_[p]->beams_[index]);
221  }
222  }
223  }
224 }
#define tprintf(...)
Definition: tprintf.h:31
const char * kNodeContNames[]
Definition: recodebeam.cpp:39
static int BeamIndex(bool is_dawg, NodeContinuation cont, int length)
Definition: recodebeam.h:228
NodeContinuation
Definition: recodebeam.h:69

◆ Decode() [1/2]

void tesseract::RecodeBeamSearch::Decode ( const NetworkIO output,
double  dict_ratio,
double  cert_offset,
double  worst_dict_cert,
const UNICHARSET charset 
)

Definition at line 76 of file recodebeam.cpp.

78  {
79  beam_size_ = 0;
80  int width = output.Width();
81  for (int t = 0; t < width; ++t) {
82  ComputeTopN(output.f(t), output.NumFeatures(), kBeamWidths[0]);
83  DecodeStep(output.f(t), t, dict_ratio, cert_offset, worst_dict_cert,
84  charset);
85  }
86 }

◆ Decode() [2/2]

void tesseract::RecodeBeamSearch::Decode ( const GENERIC_2D_ARRAY< float > &  output,
double  dict_ratio,
double  cert_offset,
double  worst_dict_cert,
const UNICHARSET charset 
)

Definition at line 87 of file recodebeam.cpp.

90  {
91  beam_size_ = 0;
92  int width = output.dim1();
93  for (int t = 0; t < width; ++t) {
94  ComputeTopN(output[t], output.dim2(), kBeamWidths[0]);
95  DecodeStep(output[t], t, dict_ratio, cert_offset, worst_dict_cert, charset);
96  }
97 }
int dim1() const
Definition: matrix.h:201
int dim2() const
Definition: matrix.h:202

◆ ExtractBestPathAsLabels()

void tesseract::RecodeBeamSearch::ExtractBestPathAsLabels ( GenericVector< int > *  labels,
GenericVector< int > *  xcoords 
) const

Definition at line 100 of file recodebeam.cpp.

101  {
102  labels->truncate(0);
103  xcoords->truncate(0);
105  ExtractBestPaths(&best_nodes, NULL);
106  // Now just run CTC on the best nodes.
107  int t = 0;
108  int width = best_nodes.size();
109  while (t < width) {
110  int label = best_nodes[t]->code;
111  if (label != null_char_) {
112  labels->push_back(label);
113  xcoords->push_back(t);
114  }
115  while (++t < width && !is_simple_text_ && best_nodes[t]->code == label) {
116  }
117  }
118  xcoords->push_back(width);
119 }
int push_back(T object)
void truncate(int size)
int size() const
Definition: genericvector.h:72

◆ ExtractBestPathAsUnicharIds()

void tesseract::RecodeBeamSearch::ExtractBestPathAsUnicharIds ( bool  debug,
const UNICHARSET unicharset,
GenericVector< int > *  unichar_ids,
GenericVector< float > *  certs,
GenericVector< float > *  ratings,
GenericVector< int > *  xcoords 
) const

Definition at line 123 of file recodebeam.cpp.

126  {
128  ExtractBestPaths(&best_nodes, NULL);
129  ExtractPathAsUnicharIds(best_nodes, unichar_ids, certs, ratings, xcoords);
130  if (debug) {
131  DebugPath(unicharset, best_nodes);
132  DebugUnicharPath(unicharset, best_nodes, *unichar_ids, *certs, *ratings,
133  *xcoords);
134  }
135 }

◆ ExtractBestPathAsWords()

void tesseract::RecodeBeamSearch::ExtractBestPathAsWords ( const TBOX line_box,
float  scale_factor,
bool  debug,
const UNICHARSET unicharset,
PointerVector< WERD_RES > *  words 
)

Definition at line 138 of file recodebeam.cpp.

141  {
142  words->truncate(0);
143  GenericVector<int> unichar_ids;
144  GenericVector<float> certs;
145  GenericVector<float> ratings;
146  GenericVector<int> xcoords;
149  ExtractBestPaths(&best_nodes, &second_nodes);
150  if (debug) {
151  DebugPath(unicharset, best_nodes);
152  ExtractPathAsUnicharIds(second_nodes, &unichar_ids, &certs, &ratings,
153  &xcoords);
154  tprintf("\nSecond choice path:\n");
155  DebugUnicharPath(unicharset, second_nodes, unichar_ids, certs, ratings,
156  xcoords);
157  }
158  ExtractPathAsUnicharIds(best_nodes, &unichar_ids, &certs, &ratings, &xcoords);
159  int num_ids = unichar_ids.size();
160  if (debug) {
161  DebugUnicharPath(unicharset, best_nodes, unichar_ids, certs, ratings,
162  xcoords);
163  }
164  // Convert labels to unichar-ids.
165  int word_end = 0;
166  float prev_space_cert = 0.0f;
167  for (int word_start = 0; word_start < num_ids; word_start = word_end) {
168  for (word_end = word_start + 1; word_end < num_ids; ++word_end) {
169  // A word is terminated when a space character or start_of_word flag is
170  // hit. We also want to force a separate word for every non
171  // space-delimited character when not in a dictionary context.
172  if (unichar_ids[word_end] == UNICHAR_SPACE) break;
173  int index = xcoords[word_end];
174  if (best_nodes[index]->start_of_word) break;
175  if (best_nodes[index]->permuter == TOP_CHOICE_PERM &&
176  (!unicharset->IsSpaceDelimited(unichar_ids[word_end]) ||
177  !unicharset->IsSpaceDelimited(unichar_ids[word_end - 1])))
178  break;
179  }
180  float space_cert = 0.0f;
181  if (word_end < num_ids && unichar_ids[word_end] == UNICHAR_SPACE)
182  space_cert = certs[word_end];
183  bool leading_space =
184  word_start > 0 && unichar_ids[word_start - 1] == UNICHAR_SPACE;
185  // Create a WERD_RES for the output word.
186  WERD_RES* word_res = InitializeWord(
187  leading_space, line_box, word_start, word_end,
188  MIN(space_cert, prev_space_cert), unicharset, xcoords, scale_factor);
189  for (int i = word_start; i < word_end; ++i) {
190  BLOB_CHOICE_LIST* choices = new BLOB_CHOICE_LIST;
191  BLOB_CHOICE_IT bc_it(choices);
192  BLOB_CHOICE* choice = new BLOB_CHOICE(
193  unichar_ids[i], ratings[i], certs[i], -1, 1.0f,
194  static_cast<float>(MAX_INT16), 0.0f, BCC_STATIC_CLASSIFIER);
195  int col = i - word_start;
196  choice->set_matrix_cell(col, col);
197  bc_it.add_after_then_move(choice);
198  word_res->ratings->put(col, col, choices);
199  }
200  int index = xcoords[word_end - 1];
201  word_res->FakeWordFromRatings(best_nodes[index]->permuter);
202  words->push_back(word_res);
203  prev_space_cert = space_cert;
204  if (word_end < num_ids && unichar_ids[word_end] == UNICHAR_SPACE)
205  ++word_end;
206  }
207 }
#define MAX_INT16
Definition: host.h:61
int push_back(T object)
#define tprintf(...)
Definition: tprintf.h:31
void set_matrix_cell(int col, int row)
Definition: ratngs.h:156
int size() const
Definition: genericvector.h:72
MATRIX * ratings
Definition: pageres.h:215
void FakeWordFromRatings(PermuterType permuter)
Definition: pageres.cpp:893
bool IsSpaceDelimited(UNICHAR_ID unichar_id) const
Definition: unicharset.h:612
void put(ICOORD pos, const T &thing)
Definition: matrix.h:215
#define MIN(x, y)
Definition: ndminx.h:28

◆ IsDawgFromBeamsIndex()

static bool tesseract::RecodeBeamSearch::IsDawgFromBeamsIndex ( int  index)
inlinestatic

Definition at line 224 of file recodebeam.h.

224  {
225  return index / (kNumLengths * NC_COUNT) > 0;
226  }
static const int kNumLengths
Definition: recodebeam.h:215

◆ LengthFromBeamsIndex()

static int tesseract::RecodeBeamSearch::LengthFromBeamsIndex ( int  index)
inlinestatic

Definition at line 220 of file recodebeam.h.

220 { return index % kNumLengths; }
static const int kNumLengths
Definition: recodebeam.h:215

Member Data Documentation

◆ kMinCertainty

const float tesseract::RecodeBeamSearch::kMinCertainty = -20.0f
static

Definition at line 213 of file recodebeam.h.

◆ kNumBeams

const int tesseract::RecodeBeamSearch::kNumBeams = 2 * NC_COUNT * kNumLengths
static

Definition at line 218 of file recodebeam.h.

◆ kNumLengths

const int tesseract::RecodeBeamSearch::kNumLengths = RecodedCharID::kMaxCodeLen + 1
static

Definition at line 215 of file recodebeam.h.


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