tesseract  4.00.00dev
pageres.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: pageres.cpp (Formerly page_res.c)
3  * Description: Hierarchy of results classes from PAGE_RES to WERD_RES
4  * and an iterator class to iterate over the words.
5  * Main purposes:
6  * Easy way to iterate over the words without a 3-nested loop.
7  * Holds data used during word recognition.
8  * Holds information about alternative spacing paths.
9  * Author: Phil Cheatle
10  * Created: Tue Sep 22 08:42:49 BST 1992
11  *
12  * (C) Copyright 1992, Hewlett-Packard Ltd.
13  ** Licensed under the Apache License, Version 2.0 (the "License");
14  ** you may not use this file except in compliance with the License.
15  ** You may obtain a copy of the License at
16  ** http://www.apache.org/licenses/LICENSE-2.0
17  ** Unless required by applicable law or agreed to in writing, software
18  ** distributed under the License is distributed on an "AS IS" BASIS,
19  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  ** See the License for the specific language governing permissions and
21  ** limitations under the License.
22  *
23  **********************************************************************/
24 #include <stdlib.h>
25 #ifdef __UNIX__
26 #include <assert.h>
27 #endif
28 #include "blamer.h"
29 #include "pageres.h"
30 #include "blobs.h"
31 
34 
35 // Gain factor for computing thresholds that determine the ambiguity of a word.
36 static const double kStopperAmbiguityThresholdGain = 8.0;
37 // Constant offset for computing thresholds that determine the ambiguity of a
38 // word.
39 static const double kStopperAmbiguityThresholdOffset = 1.5;
40 // Max number of broken pieces to associate.
42 // Max ratio of word box height to line size to allow it to be processed as
43 // a line with other words.
44 const double kMaxWordSizeRatio = 1.25;
45 // Max ratio of line box height to line size to allow a new word to be added.
46 const double kMaxLineSizeRatio = 1.25;
47 // Max ratio of word gap to line size to allow a new word to be added.
48 const double kMaxWordGapRatio = 2.0;
49 
50 // Computes and returns a threshold of certainty difference used to determine
51 // which words to keep, based on the adjustment factors of the two words.
52 // TODO(rays) This is horrible. Replace with an enhance params training model.
53 static double StopperAmbigThreshold(double f1, double f2) {
54  return (f2 - f1) * kStopperAmbiguityThresholdGain -
55  kStopperAmbiguityThresholdOffset;
56 }
57 
58 /*************************************************************************
59  * PAGE_RES::PAGE_RES
60  *
61  * Constructor for page results
62  *************************************************************************/
64  bool merge_similar_words,
65  BLOCK_LIST *the_block_list,
66  WERD_CHOICE **prev_word_best_choice_ptr) {
67  Init();
68  BLOCK_IT block_it(the_block_list);
69  BLOCK_RES_IT block_res_it(&block_res_list);
70  for (block_it.mark_cycle_pt();
71  !block_it.cycled_list(); block_it.forward()) {
72  block_res_it.add_to_end(new BLOCK_RES(merge_similar_words,
73  block_it.data()));
74  }
75  prev_word_best_choice = prev_word_best_choice_ptr;
76 }
77 
78 /*************************************************************************
79  * BLOCK_RES::BLOCK_RES
80  *
81  * Constructor for BLOCK results
82  *************************************************************************/
83 
84 BLOCK_RES::BLOCK_RES(bool merge_similar_words, BLOCK *the_block) {
85  ROW_IT row_it (the_block->row_list ());
86  ROW_RES_IT row_res_it(&row_res_list);
87 
88  char_count = 0;
89  rej_count = 0;
90  font_class = -1; //not assigned
91  x_height = -1.0;
92  font_assigned = FALSE;
93  bold = FALSE;
94  italic = FALSE;
95  row_count = 0;
96 
97  block = the_block;
98 
99  for (row_it.mark_cycle_pt(); !row_it.cycled_list(); row_it.forward()) {
100  row_res_it.add_to_end(new ROW_RES(merge_similar_words, row_it.data()));
101  }
102 }
103 
104 /*************************************************************************
105  * ROW_RES::ROW_RES
106  *
107  * Constructor for ROW results
108  *************************************************************************/
109 
110 ROW_RES::ROW_RES(bool merge_similar_words, ROW *the_row) {
111  WERD_IT word_it(the_row->word_list());
112  WERD_RES_IT word_res_it(&word_res_list);
113  WERD_RES *combo = NULL; // current combination of fuzzies
114  WERD *copy_word;
115 
116  char_count = 0;
117  rej_count = 0;
118  whole_word_rej_count = 0;
119 
120  row = the_row;
121  bool add_next_word = false;
122  TBOX union_box;
123  float line_height = the_row->x_height() + the_row->ascenders() -
124  the_row->descenders();
125  for (word_it.mark_cycle_pt(); !word_it.cycled_list(); word_it.forward()) {
126  WERD_RES* word_res = new WERD_RES(word_it.data());
127  word_res->x_height = the_row->x_height();
128  if (add_next_word) {
129  ASSERT_HOST(combo != NULL);
130  // We are adding this word to the combination.
131  word_res->part_of_combo = TRUE;
132  combo->copy_on(word_res);
133  } else if (merge_similar_words) {
134  union_box = word_res->word->bounding_box();
135  add_next_word = !word_res->word->flag(W_REP_CHAR) &&
136  union_box.height() <= line_height * kMaxWordSizeRatio;
137  word_res->odd_size = !add_next_word;
138  }
139  WERD* next_word = word_it.data_relative(1);
140  if (merge_similar_words) {
141  if (add_next_word && !next_word->flag(W_REP_CHAR)) {
142  // Next word will be added on if all of the following are true:
143  // Not a rep char.
144  // Box height small enough.
145  // Union box height small enough.
146  // Horizontal gap small enough.
147  TBOX next_box = next_word->bounding_box();
148  int prev_right = union_box.right();
149  union_box += next_box;
150  if (next_box.height() > line_height * kMaxWordSizeRatio ||
151  union_box.height() > line_height * kMaxLineSizeRatio ||
152  next_box.left() > prev_right + line_height * kMaxWordGapRatio) {
153  add_next_word = false;
154  }
155  }
156  next_word->set_flag(W_FUZZY_NON, add_next_word);
157  } else {
158  add_next_word = next_word->flag(W_FUZZY_NON);
159  }
160  if (add_next_word) {
161  if (combo == NULL) {
162  copy_word = new WERD;
163  *copy_word = *(word_it.data()); // deep copy
164  combo = new WERD_RES(copy_word);
165  combo->x_height = the_row->x_height();
166  combo->combination = TRUE;
167  word_res_it.add_to_end(combo);
168  }
169  word_res->part_of_combo = TRUE;
170  } else {
171  combo = NULL;
172  }
173  word_res_it.add_to_end(word_res);
174  }
175 }
176 
177 
179  this->ELIST_LINK::operator=(source);
180  Clear();
181  if (source.combination) {
182  word = new WERD;
183  *word = *(source.word); // deep copy
184  } else {
185  word = source.word; // pt to same word
186  }
187  if (source.bln_boxes != NULL)
188  bln_boxes = new tesseract::BoxWord(*source.bln_boxes);
189  if (source.chopped_word != NULL)
190  chopped_word = new TWERD(*source.chopped_word);
191  if (source.rebuild_word != NULL)
192  rebuild_word = new TWERD(*source.rebuild_word);
193  // TODO(rays) Do we ever need to copy the seam_array?
194  blob_row = source.blob_row;
195  denorm = source.denorm;
196  if (source.box_word != NULL)
197  box_word = new tesseract::BoxWord(*source.box_word);
198  best_state = source.best_state;
199  correct_text = source.correct_text;
200  blob_widths = source.blob_widths;
201  blob_gaps = source.blob_gaps;
202  // None of the uses of operator= require the ratings matrix to be copied,
203  // so don't as it would be really slow.
204 
205  // Copy the cooked choices.
206  WERD_CHOICE_IT wc_it(const_cast<WERD_CHOICE_LIST*>(&source.best_choices));
207  WERD_CHOICE_IT wc_dest_it(&best_choices);
208  for (wc_it.mark_cycle_pt(); !wc_it.cycled_list(); wc_it.forward()) {
209  const WERD_CHOICE *choice = wc_it.data();
210  wc_dest_it.add_after_then_move(new WERD_CHOICE(*choice));
211  }
212  if (!wc_dest_it.empty()) {
213  wc_dest_it.move_to_first();
214  best_choice = wc_dest_it.data();
215  } else {
216  best_choice = NULL;
217  }
218 
219  if (source.raw_choice != NULL) {
220  raw_choice = new WERD_CHOICE(*source.raw_choice);
221  } else {
222  raw_choice = NULL;
223  }
224  if (source.ep_choice != NULL) {
225  ep_choice = new WERD_CHOICE(*source.ep_choice);
226  } else {
227  ep_choice = NULL;
228  }
229  reject_map = source.reject_map;
230  combination = source.combination;
231  part_of_combo = source.part_of_combo;
232  CopySimpleFields(source);
233  if (source.blamer_bundle != NULL) {
234  blamer_bundle = new BlamerBundle(*(source.blamer_bundle));
235  }
236  return *this;
237 }
238 
239 // Copies basic fields that don't involve pointers that might be useful
240 // to copy when making one WERD_RES from another.
242  tess_failed = source.tess_failed;
243  tess_accepted = source.tess_accepted;
244  tess_would_adapt = source.tess_would_adapt;
245  done = source.done;
246  unlv_crunch_mode = source.unlv_crunch_mode;
247  small_caps = source.small_caps;
248  odd_size = source.odd_size;
249  italic = source.italic;
250  bold = source.bold;
251  fontinfo = source.fontinfo;
252  fontinfo2 = source.fontinfo2;
253  fontinfo_id_count = source.fontinfo_id_count;
254  fontinfo_id2_count = source.fontinfo_id2_count;
255  x_height = source.x_height;
256  caps_height = source.caps_height;
257  baseline_shift = source.baseline_shift;
258  guessed_x_ht = source.guessed_x_ht;
259  guessed_caps_ht = source.guessed_caps_ht;
260  reject_spaces = source.reject_spaces;
261  uch_set = source.uch_set;
262  tesseract = source.tesseract;
263 }
264 
265 // Initializes a blank (default constructed) WERD_RES from one that has
266 // already been recognized.
267 // Use SetupFor*Recognition afterwards to complete the setup and make
268 // it ready for a retry recognition.
270  word = source.word;
271  CopySimpleFields(source);
272  if (source.blamer_bundle != NULL) {
273  blamer_bundle = new BlamerBundle();
274  blamer_bundle->CopyTruth(*source.blamer_bundle);
275  }
276 }
277 
278 // Sets up the members used in recognition: bln_boxes, chopped_word,
279 // seam_array, denorm. Returns false if
280 // the word is empty and sets up fake results. If use_body_size is
281 // true and row->body_size is set, then body_size will be used for
282 // blob normalization instead of xheight + ascrise. This flag is for
283 // those languages that are using CJK pitch model and thus it has to
284 // be true if and only if tesseract->textord_use_cjk_fp_model is
285 // true.
286 // If allow_detailed_fx is true, the feature extractor will receive fine
287 // precision outline information, allowing smoother features and better
288 // features on low resolution images.
289 // The norm_mode_hint sets the default mode for normalization in absence
290 // of any of the above flags.
291 // norm_box is used to override the word bounding box to determine the
292 // normalization scale and offset.
293 // Returns false if the word is empty and sets up fake results.
294 bool WERD_RES::SetupForRecognition(const UNICHARSET& unicharset_in,
295  tesseract::Tesseract* tess, Pix* pix,
296  int norm_mode,
297  const TBOX* norm_box,
298  bool numeric_mode,
299  bool use_body_size,
300  bool allow_detailed_fx,
301  ROW *row, const BLOCK* block) {
302  tesseract::OcrEngineMode norm_mode_hint =
303  static_cast<tesseract::OcrEngineMode>(norm_mode);
304  tesseract = tess;
305  POLY_BLOCK* pb = block != NULL ? block->poly_block() : NULL;
306  if ((norm_mode_hint != tesseract::OEM_LSTM_ONLY &&
307  word->cblob_list()->empty()) ||
308  (pb != NULL && !pb->IsText())) {
309  // Empty words occur when all the blobs have been moved to the rej_blobs
310  // list, which seems to occur frequently in junk.
311  SetupFake(unicharset_in);
312  word->set_flag(W_REP_CHAR, false);
313  return false;
314  }
315  ClearResults();
316  SetupWordScript(unicharset_in);
317  chopped_word = TWERD::PolygonalCopy(allow_detailed_fx, word);
318  float word_xheight = use_body_size && row != NULL && row->body_size() > 0.0f
319  ? row->body_size() : x_height;
320  chopped_word->BLNormalize(block, row, pix, word->flag(W_INVERSE),
321  word_xheight, baseline_shift, numeric_mode,
322  norm_mode_hint, norm_box, &denorm);
323  blob_row = row;
324  SetupBasicsFromChoppedWord(unicharset_in);
325  SetupBlamerBundle();
326  int num_blobs = chopped_word->NumBlobs();
327  ratings = new MATRIX(num_blobs, kWordrecMaxNumJoinChunks);
328  tess_failed = false;
329  return true;
330 }
331 
332 // Set up the seam array, bln_boxes, best_choice, and raw_choice to empty
333 // accumulators from a made chopped word. We presume the fields are already
334 // empty.
336  bln_boxes = tesseract::BoxWord::CopyFromNormalized(chopped_word);
337  start_seam_list(chopped_word, &seam_array);
338  SetupBlobWidthsAndGaps();
339  ClearWordChoices();
340 }
341 
342 // Sets up the members used in recognition for an empty recognition result:
343 // bln_boxes, chopped_word, seam_array, denorm, best_choice, raw_choice.
344 void WERD_RES::SetupFake(const UNICHARSET& unicharset_in) {
345  ClearResults();
346  SetupWordScript(unicharset_in);
347  chopped_word = new TWERD;
348  rebuild_word = new TWERD;
349  bln_boxes = new tesseract::BoxWord;
350  box_word = new tesseract::BoxWord;
351  int blob_count = word->cblob_list()->length();
352  if (blob_count > 0) {
353  BLOB_CHOICE** fake_choices = new BLOB_CHOICE*[blob_count];
354  // For non-text blocks, just pass any blobs through to the box_word
355  // and call the word failed with a fake classification.
356  C_BLOB_IT b_it(word->cblob_list());
357  int blob_id = 0;
358  for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
359  TBOX box = b_it.data()->bounding_box();
360  box_word->InsertBox(box_word->length(), box);
361  fake_choices[blob_id++] = new BLOB_CHOICE;
362  }
363  FakeClassifyWord(blob_count, fake_choices);
364  delete [] fake_choices;
365  } else {
366  WERD_CHOICE* word = new WERD_CHOICE(&unicharset_in);
367  word->make_bad();
368  LogNewRawChoice(word);
369  // Ownership of word is taken by *this WERD_RES in LogNewCookedChoice.
370  LogNewCookedChoice(1, false, word);
371  }
372  tess_failed = true;
373  done = true;
374 }
375 
377  uch_set = &uch;
378  int script = uch.default_sid();
379  word->set_script_id(script);
380  word->set_flag(W_SCRIPT_HAS_XHEIGHT, uch.script_has_xheight());
381  word->set_flag(W_SCRIPT_IS_LATIN, script == uch.latin_sid());
382 }
383 
384 // Sets up the blamer_bundle if it is not null, using the initialized denorm.
386  if (blamer_bundle != NULL) {
387  blamer_bundle->SetupNormTruthWord(denorm);
388  }
389 }
390 
391 // Computes the blob_widths and blob_gaps from the chopped_word.
393  blob_widths.truncate(0);
394  blob_gaps.truncate(0);
395  int num_blobs = chopped_word->NumBlobs();
396  for (int b = 0; b < num_blobs; ++b) {
397  TBLOB *blob = chopped_word->blobs[b];
398  TBOX box = blob->bounding_box();
399  blob_widths.push_back(box.width());
400  if (b + 1 < num_blobs) {
401  blob_gaps.push_back(
402  chopped_word->blobs[b + 1]->bounding_box().left() - box.right());
403  }
404  }
405 }
406 
407 // Updates internal data to account for a new SEAM (chop) at the given
408 // blob_number. Fixes the ratings matrix and states in the choices, as well
409 // as the blob widths and gaps.
410 void WERD_RES::InsertSeam(int blob_number, SEAM* seam) {
411  // Insert the seam into the SEAMS array.
412  seam->PrepareToInsertSeam(seam_array, chopped_word->blobs, blob_number, true);
413  seam_array.insert(seam, blob_number);
414  if (ratings != NULL) {
415  // Expand the ratings matrix.
416  ratings = ratings->ConsumeAndMakeBigger(blob_number);
417  // Fix all the segmentation states.
418  if (raw_choice != NULL)
419  raw_choice->UpdateStateForSplit(blob_number);
420  WERD_CHOICE_IT wc_it(&best_choices);
421  for (wc_it.mark_cycle_pt(); !wc_it.cycled_list(); wc_it.forward()) {
422  WERD_CHOICE* choice = wc_it.data();
423  choice->UpdateStateForSplit(blob_number);
424  }
425  SetupBlobWidthsAndGaps();
426  }
427 }
428 
429 // Returns true if all the word choices except the first have adjust_factors
430 // worse than the given threshold.
432  // The choices are not changed by this iteration.
433  WERD_CHOICE_IT wc_it(const_cast<WERD_CHOICE_LIST*>(&best_choices));
434  for (wc_it.forward(); !wc_it.at_first(); wc_it.forward()) {
435  WERD_CHOICE* choice = wc_it.data();
436  if (choice->adjust_factor() <= threshold)
437  return false;
438  }
439  return true;
440 }
441 
442 // Returns true if the current word is ambiguous (by number of answers or
443 // by dangerous ambigs.)
445  return !best_choices.singleton() || best_choice->dangerous_ambig_found();
446 }
447 
448 // Returns true if the ratings matrix size matches the sum of each of the
449 // segmentation states.
451  int ratings_dim = ratings->dimension();
452  if (raw_choice->TotalOfStates() != ratings_dim) {
453  tprintf("raw_choice has total of states = %d vs ratings dim of %d\n",
454  raw_choice->TotalOfStates(), ratings_dim);
455  return false;
456  }
457  WERD_CHOICE_IT it(&best_choices);
458  int index = 0;
459  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward(), ++index) {
460  WERD_CHOICE* choice = it.data();
461  if (choice->TotalOfStates() != ratings_dim) {
462  tprintf("Cooked #%d has total of states = %d vs ratings dim of %d\n",
463  index, choice->TotalOfStates(), ratings_dim);
464  return false;
465  }
466  }
467  return true;
468 }
469 
470 // Prints a list of words found if debug is true or the word result matches
471 // the word_to_debug.
472 void WERD_RES::DebugWordChoices(bool debug, const char* word_to_debug) {
473  if (debug ||
474  (word_to_debug != NULL && *word_to_debug != '\0' && best_choice != NULL &&
475  best_choice->unichar_string() == STRING(word_to_debug))) {
476  if (raw_choice != NULL)
477  raw_choice->print("\nBest Raw Choice");
478 
479  WERD_CHOICE_IT it(&best_choices);
480  int index = 0;
481  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward(), ++index) {
482  WERD_CHOICE* choice = it.data();
483  STRING label;
484  label.add_str_int("\nCooked Choice #", index);
485  choice->print(label.string());
486  }
487  }
488 }
489 
490 // Prints the top choice along with the accepted/done flags.
491 void WERD_RES::DebugTopChoice(const char* msg) const {
492  tprintf("Best choice: accepted=%d, adaptable=%d, done=%d : ",
493  tess_accepted, tess_would_adapt, done);
494  if (best_choice == NULL)
495  tprintf("<Null choice>\n");
496  else
497  best_choice->print(msg);
498 }
499 
500 // Removes from best_choices all choices which are not within a reasonable
501 // range of the best choice.
502 // TODO(rays) incorporate the information used here into the params training
503 // re-ranker, in place of this heuristic that is based on the previous
504 // adjustment factor.
505 void WERD_RES::FilterWordChoices(int debug_level) {
506  if (best_choice == NULL || best_choices.singleton())
507  return;
508 
509  if (debug_level >= 2)
510  best_choice->print("\nFiltering against best choice");
511  WERD_CHOICE_IT it(&best_choices);
512  int index = 0;
513  for (it.forward(); !it.at_first(); it.forward(), ++index) {
514  WERD_CHOICE* choice = it.data();
515  float threshold = StopperAmbigThreshold(best_choice->adjust_factor(),
516  choice->adjust_factor());
517  // i, j index the blob choice in choice, best_choice.
518  // chunk is an index into the chopped_word blobs (AKA chunks).
519  // Since the two words may use different segmentations of the chunks, we
520  // iterate over the chunks to find out whether a comparable blob
521  // classification is much worse than the best result.
522  int i = 0, j = 0, chunk = 0;
523  // Each iteration of the while deals with 1 chunk. On entry choice_chunk
524  // and best_chunk are the indices of the first chunk in the NEXT blob,
525  // i.e. we don't have to increment i, j while chunk < choice_chunk and
526  // best_chunk respectively.
527  int choice_chunk = choice->state(0), best_chunk = best_choice->state(0);
528  while (i < choice->length() && j < best_choice->length()) {
529  if (choice->unichar_id(i) != best_choice->unichar_id(j) &&
530  choice->certainty(i) - best_choice->certainty(j) < threshold) {
531  if (debug_level >= 2) {
532  choice->print("WorstCertaintyDiffWorseThan");
533  tprintf(
534  "i %d j %d Choice->Blob[i].Certainty %.4g"
535  " WorstOtherChoiceCertainty %g Threshold %g\n",
536  i, j, choice->certainty(i), best_choice->certainty(j), threshold);
537  tprintf("Discarding bad choice #%d\n", index);
538  }
539  delete it.extract();
540  break;
541  }
542  ++chunk;
543  // If needed, advance choice_chunk to keep up with chunk.
544  while (choice_chunk < chunk && ++i < choice->length())
545  choice_chunk += choice->state(i);
546  // If needed, advance best_chunk to keep up with chunk.
547  while (best_chunk < chunk && ++j < best_choice->length())
548  best_chunk += best_choice->state(j);
549  }
550  }
551 }
552 
553 void WERD_RES::ComputeAdaptionThresholds(float certainty_scale,
554  float min_rating,
555  float max_rating,
556  float rating_margin,
557  float* thresholds) {
558  int chunk = 0;
559  int end_chunk = best_choice->state(0);
560  int end_raw_chunk = raw_choice->state(0);
561  int raw_blob = 0;
562  for (int i = 0; i < best_choice->length(); i++, thresholds++) {
563  float avg_rating = 0.0f;
564  int num_error_chunks = 0;
565 
566  // For each chunk in best choice blob i, count non-matching raw results.
567  while (chunk < end_chunk) {
568  if (chunk >= end_raw_chunk) {
569  ++raw_blob;
570  end_raw_chunk += raw_choice->state(raw_blob);
571  }
572  if (best_choice->unichar_id(i) !=
573  raw_choice->unichar_id(raw_blob)) {
574  avg_rating += raw_choice->certainty(raw_blob);
575  ++num_error_chunks;
576  }
577  ++chunk;
578  }
579 
580  if (num_error_chunks > 0) {
581  avg_rating /= num_error_chunks;
582  *thresholds = (avg_rating / -certainty_scale) * (1.0 - rating_margin);
583  } else {
584  *thresholds = max_rating;
585  }
586 
587  if (*thresholds > max_rating)
588  *thresholds = max_rating;
589  if (*thresholds < min_rating)
590  *thresholds = min_rating;
591  }
592 }
593 
594 // Saves a copy of the word_choice if it has the best unadjusted rating.
595 // Returns true if the word_choice was the new best.
597  if (raw_choice == NULL || word_choice->rating() < raw_choice->rating()) {
598  delete raw_choice;
599  raw_choice = new WERD_CHOICE(*word_choice);
600  raw_choice->set_permuter(TOP_CHOICE_PERM);
601  return true;
602  }
603  return false;
604 }
605 
606 // Consumes word_choice by adding it to best_choices, (taking ownership) if
607 // the certainty for word_choice is some distance of the best choice in
608 // best_choices, or by deleting the word_choice and returning false.
609 // The best_choices list is kept in sorted order by rating. Duplicates are
610 // removed, and the list is kept no longer than max_num_choices in length.
611 // Returns true if the word_choice is still a valid pointer.
612 bool WERD_RES::LogNewCookedChoice(int max_num_choices, bool debug,
613  WERD_CHOICE* word_choice) {
614  if (best_choice != NULL) {
615  // Throw out obviously bad choices to save some work.
616  // TODO(rays) Get rid of this! This piece of code produces different
617  // results according to the order in which words are found, which is an
618  // undesirable behavior. It would be better to keep all the choices and
619  // prune them later when more information is available.
620  float max_certainty_delta =
621  StopperAmbigThreshold(best_choice->adjust_factor(),
622  word_choice->adjust_factor());
623  if (max_certainty_delta > -kStopperAmbiguityThresholdOffset)
624  max_certainty_delta = -kStopperAmbiguityThresholdOffset;
625  if (word_choice->certainty() - best_choice->certainty() <
626  max_certainty_delta) {
627  if (debug) {
628  STRING bad_string;
629  word_choice->string_and_lengths(&bad_string, NULL);
630  tprintf("Discarding choice \"%s\" with an overly low certainty"
631  " %.3f vs best choice certainty %.3f (Threshold: %.3f)\n",
632  bad_string.string(), word_choice->certainty(),
633  best_choice->certainty(),
634  max_certainty_delta + best_choice->certainty());
635  }
636  delete word_choice;
637  return false;
638  }
639  }
640 
641  // Insert in the list in order of increasing rating, but knock out worse
642  // string duplicates.
643  WERD_CHOICE_IT it(&best_choices);
644  const STRING& new_str = word_choice->unichar_string();
645  bool inserted = false;
646  int num_choices = 0;
647  if (!it.empty()) {
648  do {
649  WERD_CHOICE* choice = it.data();
650  if (choice->rating() > word_choice->rating() && !inserted) {
651  // Time to insert.
652  it.add_before_stay_put(word_choice);
653  inserted = true;
654  if (num_choices == 0)
655  best_choice = word_choice; // This is the new best.
656  ++num_choices;
657  }
658  if (choice->unichar_string() == new_str) {
659  if (inserted) {
660  // New is better.
661  delete it.extract();
662  } else {
663  // Old is better.
664  if (debug) {
665  tprintf("Discarding duplicate choice \"%s\", rating %g vs %g\n",
666  new_str.string(), word_choice->rating(), choice->rating());
667  }
668  delete word_choice;
669  return false;
670  }
671  } else {
672  ++num_choices;
673  if (num_choices > max_num_choices)
674  delete it.extract();
675  }
676  it.forward();
677  } while (!it.at_first());
678  }
679  if (!inserted && num_choices < max_num_choices) {
680  it.add_to_end(word_choice);
681  inserted = true;
682  if (num_choices == 0)
683  best_choice = word_choice; // This is the new best.
684  }
685  if (debug) {
686  if (inserted)
687  tprintf("New %s", best_choice == word_choice ? "Best" : "Secondary");
688  else
689  tprintf("Poor");
690  word_choice->print(" Word Choice");
691  }
692  if (!inserted) {
693  delete word_choice;
694  return false;
695  }
696  return true;
697 }
698 
699 
700 // Simple helper moves the ownership of the pointer data from src to dest,
701 // first deleting anything in dest, and nulling out src afterwards.
702 template<class T> static void MovePointerData(T** dest, T**src) {
703  delete *dest;
704  *dest = *src;
705  *src = NULL;
706 }
707 
708 // Prints a brief list of all the best choices.
710  STRING alternates_str;
711  WERD_CHOICE_IT it(const_cast<WERD_CHOICE_LIST*>(&best_choices));
712  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
713  if (!it.at_first()) alternates_str += "\", \"";
714  alternates_str += it.data()->unichar_string();
715  }
716  tprintf("Alternates for \"%s\": {\"%s\"}\n",
717  best_choice->unichar_string().string(), alternates_str.string());
718 }
719 
720 // Returns the sum of the widths of the blob between start_blob and last_blob
721 // inclusive.
722 int WERD_RES::GetBlobsWidth(int start_blob, int last_blob) {
723  int result = 0;
724  for (int b = start_blob; b <= last_blob; ++b) {
725  result += blob_widths[b];
726  if (b < last_blob)
727  result += blob_gaps[b];
728  }
729  return result;
730 }
731 // Returns the width of a gap between the specified blob and the next one.
732 int WERD_RES::GetBlobsGap(int blob_index) {
733  if (blob_index < 0 || blob_index >= blob_gaps.size())
734  return 0;
735  return blob_gaps[blob_index];
736 }
737 
738 // Returns the BLOB_CHOICE corresponding to the given index in the
739 // best choice word taken from the appropriate cell in the ratings MATRIX.
740 // Borrowed pointer, so do not delete. May return NULL if there is no
741 // BLOB_CHOICE matching the unichar_id at the given index.
743  if (index < 0 || index >= best_choice->length()) return NULL;
744  BLOB_CHOICE_LIST* choices = GetBlobChoices(index);
745  return FindMatchingChoice(best_choice->unichar_id(index), choices);
746 }
747 
748 // Returns the BLOB_CHOICE_LIST corresponding to the given index in the
749 // best choice word taken from the appropriate cell in the ratings MATRIX.
750 // Borrowed pointer, so do not delete.
751 BLOB_CHOICE_LIST* WERD_RES::GetBlobChoices(int index) const {
752  return best_choice->blob_choices(index, ratings);
753 }
754 
755 // Moves the results fields from word to this. This takes ownership of all
756 // the data, so src can be destructed.
758  denorm = word->denorm;
759  blob_row = word->blob_row;
760  MovePointerData(&chopped_word, &word->chopped_word);
761  MovePointerData(&rebuild_word, &word->rebuild_word);
762  MovePointerData(&box_word, &word->box_word);
763  seam_array.delete_data_pointers();
764  seam_array = word->seam_array;
765  word->seam_array.clear();
766  best_state.move(&word->best_state);
767  correct_text.move(&word->correct_text);
768  blob_widths.move(&word->blob_widths);
769  blob_gaps.move(&word->blob_gaps);
770  if (ratings != NULL) ratings->delete_matrix_pointers();
771  MovePointerData(&ratings, &word->ratings);
772  best_choice = word->best_choice;
773  MovePointerData(&raw_choice, &word->raw_choice);
774  best_choices.clear();
775  WERD_CHOICE_IT wc_it(&best_choices);
776  wc_it.add_list_after(&word->best_choices);
777  reject_map = word->reject_map;
778  if (word->blamer_bundle != NULL) {
779  assert(blamer_bundle != NULL);
780  blamer_bundle->CopyResults(*(word->blamer_bundle));
781  }
782  CopySimpleFields(*word);
783 }
784 
785 // Replace the best choice and rebuild box word.
786 // choice must be from the current best_choices list.
788  best_choice = choice;
789  RebuildBestState();
790  SetupBoxWord();
791  // Make up a fake reject map of the right length to keep the
792  // rejection pass happy.
793  reject_map.initialise(best_state.length());
794  done = tess_accepted = tess_would_adapt = true;
795  SetScriptPositions();
796 }
797 
798 // Builds the rebuild_word and sets the best_state from the chopped_word and
799 // the best_choice->state.
801  ASSERT_HOST(best_choice != NULL);
802  if (rebuild_word != NULL)
803  delete rebuild_word;
804  rebuild_word = new TWERD;
805  if (seam_array.empty())
806  start_seam_list(chopped_word, &seam_array);
807  best_state.truncate(0);
808  int start = 0;
809  for (int i = 0; i < best_choice->length(); ++i) {
810  int length = best_choice->state(i);
811  best_state.push_back(length);
812  if (length > 1) {
813  SEAM::JoinPieces(seam_array, chopped_word->blobs, start,
814  start + length - 1);
815  }
816  TBLOB* blob = chopped_word->blobs[start];
817  rebuild_word->blobs.push_back(new TBLOB(*blob));
818  if (length > 1) {
819  SEAM::BreakPieces(seam_array, chopped_word->blobs, start,
820  start + length - 1);
821  }
822  start += length;
823  }
824 }
825 
826 // Copies the chopped_word to the rebuild_word, faking a best_state as well.
827 // Also sets up the output box_word.
829  if (rebuild_word != NULL)
830  delete rebuild_word;
831  rebuild_word = new TWERD(*chopped_word);
832  SetupBoxWord();
833  int word_len = box_word->length();
834  best_state.reserve(word_len);
835  correct_text.reserve(word_len);
836  for (int i = 0; i < word_len; ++i) {
837  best_state.push_back(1);
838  correct_text.push_back(STRING(""));
839  }
840 }
841 
842 // Sets/replaces the box_word with one made from the rebuild_word.
844  if (box_word != NULL)
845  delete box_word;
846  rebuild_word->ComputeBoundingBoxes();
847  box_word = tesseract::BoxWord::CopyFromNormalized(rebuild_word);
848  box_word->ClipToOriginalWord(denorm.block(), word);
849 }
850 
851 // Sets up the script positions in the output best_choice using the best_choice
852 // to get the unichars, and the unicharset to get the target positions.
854  best_choice->SetScriptPositions(small_caps, chopped_word);
855 }
856 // Sets all the blobs in all the words (raw choice and best choices) to be
857 // the given position. (When a sub/superscript is recognized as a separate
858 // word, it falls victim to the rule that a whole word cannot be sub or
859 // superscript, so this function overrides that problem.)
861  raw_choice->SetAllScriptPositions(position);
862  WERD_CHOICE_IT wc_it(&best_choices);
863  for (wc_it.mark_cycle_pt(); !wc_it.cycled_list(); wc_it.forward())
864  wc_it.data()->SetAllScriptPositions(position);
865 }
866 
867 // Classifies the word with some already-calculated BLOB_CHOICEs.
868 // The choices are an array of blob_count pointers to BLOB_CHOICE,
869 // providing a single classifier result for each blob.
870 // The BLOB_CHOICEs are consumed and the word takes ownership.
871 // The number of blobs in the box_word must match blob_count.
872 void WERD_RES::FakeClassifyWord(int blob_count, BLOB_CHOICE** choices) {
873  // Setup the WERD_RES.
874  ASSERT_HOST(box_word != NULL);
875  ASSERT_HOST(blob_count == box_word->length());
876  ClearWordChoices();
877  ClearRatings();
878  ratings = new MATRIX(blob_count, 1);
879  for (int c = 0; c < blob_count; ++c) {
880  BLOB_CHOICE_LIST* choice_list = new BLOB_CHOICE_LIST;
881  BLOB_CHOICE_IT choice_it(choice_list);
882  choice_it.add_after_then_move(choices[c]);
883  ratings->put(c, c, choice_list);
884  }
885  FakeWordFromRatings(TOP_CHOICE_PERM);
886  reject_map.initialise(blob_count);
887  best_state.init_to_size(blob_count, 1);
888  done = true;
889 }
890 
891 // Creates a WERD_CHOICE for the word using the top choices from the leading
892 // diagonal of the ratings matrix.
894  int num_blobs = ratings->dimension();
895  WERD_CHOICE* word_choice = new WERD_CHOICE(uch_set, num_blobs);
896  word_choice->set_permuter(permuter);
897  for (int b = 0; b < num_blobs; ++b) {
898  UNICHAR_ID unichar_id = UNICHAR_SPACE;
899  float rating = MAX_INT32;
900  float certainty = -MAX_INT32;
901  BLOB_CHOICE_LIST* choices = ratings->get(b, b);
902  if (choices != NULL && !choices->empty()) {
903  BLOB_CHOICE_IT bc_it(choices);
904  BLOB_CHOICE* choice = bc_it.data();
905  unichar_id = choice->unichar_id();
906  rating = choice->rating();
907  certainty = choice->certainty();
908  }
909  word_choice->append_unichar_id_space_allocated(unichar_id, 1, rating,
910  certainty);
911  }
912  LogNewRawChoice(word_choice);
913  // Ownership of word_choice taken by word here.
914  LogNewCookedChoice(1, false, word_choice);
915 }
916 
917 // Copies the best_choice strings to the correct_text for adaption/training.
919  correct_text.clear();
920  ASSERT_HOST(best_choice != NULL);
921  for (int i = 0; i < best_choice->length(); ++i) {
922  UNICHAR_ID choice_id = best_choice->unichar_id(i);
923  const char* blob_choice = uch_set->id_to_unichar(choice_id);
924  correct_text.push_back(STRING(blob_choice));
925  }
926 }
927 
928 // Merges 2 adjacent blobs in the result if the permanent callback
929 // class_cb returns other than INVALID_UNICHAR_ID, AND the permanent
930 // callback box_cb is NULL or returns true, setting the merged blob
931 // result to the class returned from class_cb.
932 // Returns true if anything was merged.
936  ASSERT_HOST(best_choice->length() == 0 || ratings != NULL);
937  bool modified = false;
938  for (int i = 0; i + 1 < best_choice->length(); ++i) {
939  UNICHAR_ID new_id = class_cb->Run(best_choice->unichar_id(i),
940  best_choice->unichar_id(i+1));
941  if (new_id != INVALID_UNICHAR_ID &&
942  (box_cb == NULL || box_cb->Run(box_word->BlobBox(i),
943  box_word->BlobBox(i + 1)))) {
944  // Raw choice should not be fixed.
945  best_choice->set_unichar_id(new_id, i);
946  modified = true;
947  MergeAdjacentBlobs(i);
948  const MATRIX_COORD& coord = best_choice->MatrixCoord(i);
949  if (!coord.Valid(*ratings)) {
950  ratings->IncreaseBandSize(coord.row + 1 - coord.col);
951  }
952  BLOB_CHOICE_LIST* blob_choices = GetBlobChoices(i);
953  if (FindMatchingChoice(new_id, blob_choices) == NULL) {
954  // Insert a fake result.
955  BLOB_CHOICE* blob_choice = new BLOB_CHOICE;
956  blob_choice->set_unichar_id(new_id);
957  BLOB_CHOICE_IT bc_it(blob_choices);
958  bc_it.add_before_then_move(blob_choice);
959  }
960  }
961  }
962  delete class_cb;
963  delete box_cb;
964  return modified;
965 }
966 
967 // Merges 2 adjacent blobs in the result (index and index+1) and corrects
968 // all the data to account for the change.
970  if (reject_map.length() == best_choice->length())
971  reject_map.remove_pos(index);
972  best_choice->remove_unichar_id(index + 1);
973  rebuild_word->MergeBlobs(index, index + 2);
974  box_word->MergeBoxes(index, index + 2);
975  if (index + 1 < best_state.length()) {
976  best_state[index] += best_state[index + 1];
977  best_state.remove(index + 1);
978  }
979 }
980 
981 // TODO(tkielbus) Decide between keeping this behavior here or modifying the
982 // training data.
983 
984 // Utility function for fix_quotes
985 // Return true if the next character in the string (given the UTF8 length in
986 // bytes) is a quote character.
987 static int is_simple_quote(const char* signed_str, int length) {
988  const unsigned char* str =
989  reinterpret_cast<const unsigned char*>(signed_str);
990  // Standard 1 byte quotes.
991  return (length == 1 && (*str == '\'' || *str == '`')) ||
992  // UTF-8 3 bytes curved quotes.
993  (length == 3 && ((*str == 0xe2 &&
994  *(str + 1) == 0x80 &&
995  *(str + 2) == 0x98) ||
996  (*str == 0xe2 &&
997  *(str + 1) == 0x80 &&
998  *(str + 2) == 0x99)));
999 }
1000 
1001 // Callback helper for fix_quotes returns a double quote if both
1002 // arguments are quote, otherwise INVALID_UNICHAR_ID.
1004  const char *ch = uch_set->id_to_unichar(id1);
1005  const char *next_ch = uch_set->id_to_unichar(id2);
1006  if (is_simple_quote(ch, strlen(ch)) &&
1007  is_simple_quote(next_ch, strlen(next_ch)))
1008  return uch_set->unichar_to_id("\"");
1009  return INVALID_UNICHAR_ID;
1010 }
1011 
1012 // Change pairs of quotes to double quotes.
1014  if (!uch_set->contains_unichar("\"") ||
1015  !uch_set->get_enabled(uch_set->unichar_to_id("\"")))
1016  return; // Don't create it if it is disallowed.
1017 
1018  ConditionalBlobMerge(
1020  NULL);
1021 }
1022 
1023 // Callback helper for fix_hyphens returns UNICHAR_ID of - if both
1024 // arguments are hyphen, otherwise INVALID_UNICHAR_ID.
1026  const char *ch = uch_set->id_to_unichar(id1);
1027  const char *next_ch = uch_set->id_to_unichar(id2);
1028  if (strlen(ch) == 1 && strlen(next_ch) == 1 &&
1029  (*ch == '-' || *ch == '~') && (*next_ch == '-' || *next_ch == '~'))
1030  return uch_set->unichar_to_id("-");
1031  return INVALID_UNICHAR_ID;
1032 }
1033 
1034 // Callback helper for fix_hyphens returns true if box1 and box2 overlap
1035 // (assuming both on the same textline, are in order and a chopped em dash.)
1036 bool WERD_RES::HyphenBoxesOverlap(const TBOX& box1, const TBOX& box2) {
1037  return box1.right() >= box2.left();
1038 }
1039 
1040 // Change pairs of hyphens to a single hyphen if the bounding boxes touch
1041 // Typically a long dash which has been segmented.
1043  if (!uch_set->contains_unichar("-") ||
1044  !uch_set->get_enabled(uch_set->unichar_to_id("-")))
1045  return; // Don't create it if it is disallowed.
1046 
1047  ConditionalBlobMerge(
1050 }
1051 
1052 // Callback helper for merge_tess_fails returns a space if both
1053 // arguments are space, otherwise INVALID_UNICHAR_ID.
1055  if (id1 == id2 && id1 == uch_set->unichar_to_id(" "))
1056  return id1;
1057  else
1058  return INVALID_UNICHAR_ID;
1059 }
1060 
1061 // Change pairs of tess failures to a single one
1063  if (ConditionalBlobMerge(
1065  int len = best_choice->length();
1066  ASSERT_HOST(reject_map.length() == len);
1067  ASSERT_HOST(box_word->length() == len);
1068  }
1069 }
1070 
1071 // Returns true if the collection of count pieces, starting at start, are all
1072 // natural connected components, ie there are no real chops involved.
1073 bool WERD_RES::PiecesAllNatural(int start, int count) const {
1074  // all seams must have no splits.
1075  for (int index = start; index < start + count - 1; ++index) {
1076  if (index >= 0 && index < seam_array.size()) {
1077  SEAM* seam = seam_array[index];
1078  if (seam != NULL && seam->HasAnySplits()) return false;
1079  }
1080  }
1081  return true;
1082 }
1083 
1084 
1086  Clear();
1087 }
1088 
1090  tess_failed = FALSE;
1091  tess_accepted = FALSE;
1092  tess_would_adapt = FALSE;
1093  done = FALSE;
1094  unlv_crunch_mode = CR_NONE;
1095  small_caps = false;
1096  odd_size = false;
1097  italic = FALSE;
1098  bold = FALSE;
1099  // The fontinfos and tesseract count as non-pointers as they point to
1100  // data owned elsewhere.
1101  fontinfo = NULL;
1102  fontinfo2 = NULL;
1103  tesseract = NULL;
1104  fontinfo_id_count = 0;
1105  fontinfo_id2_count = 0;
1106  x_height = 0.0;
1107  caps_height = 0.0;
1108  baseline_shift = 0.0f;
1109  space_certainty = 0.0f;
1110  guessed_x_ht = TRUE;
1111  guessed_caps_ht = TRUE;
1112  combination = FALSE;
1113  part_of_combo = FALSE;
1114  reject_spaces = FALSE;
1115 }
1116 
1118  word = NULL;
1119  bln_boxes = NULL;
1120  blob_row = NULL;
1121  uch_set = NULL;
1122  chopped_word = NULL;
1123  rebuild_word = NULL;
1124  box_word = NULL;
1125  ratings = NULL;
1126  best_choice = NULL;
1127  raw_choice = NULL;
1128  ep_choice = NULL;
1129  blamer_bundle = NULL;
1130 }
1131 
1133  if (word != NULL && combination) {
1134  delete word;
1135  }
1136  word = NULL;
1137  delete blamer_bundle;
1138  blamer_bundle = NULL;
1139  ClearResults();
1140 }
1141 
1143  done = false;
1144  fontinfo = NULL;
1145  fontinfo2 = NULL;
1146  fontinfo_id_count = 0;
1147  fontinfo_id2_count = 0;
1148  if (bln_boxes != NULL) {
1149  delete bln_boxes;
1150  bln_boxes = NULL;
1151  }
1152  blob_row = NULL;
1153  if (chopped_word != NULL) {
1154  delete chopped_word;
1155  chopped_word = NULL;
1156  }
1157  if (rebuild_word != NULL) {
1158  delete rebuild_word;
1159  rebuild_word = NULL;
1160  }
1161  if (box_word != NULL) {
1162  delete box_word;
1163  box_word = NULL;
1164  }
1165  best_state.clear();
1166  correct_text.clear();
1167  seam_array.delete_data_pointers();
1168  seam_array.clear();
1169  blob_widths.clear();
1170  blob_gaps.clear();
1171  ClearRatings();
1172  ClearWordChoices();
1173  if (blamer_bundle != NULL) blamer_bundle->ClearResults();
1174 }
1176  best_choice = NULL;
1177  if (raw_choice != NULL) {
1178  delete raw_choice;
1179  raw_choice = NULL;
1180  }
1181  best_choices.clear();
1182  if (ep_choice != NULL) {
1183  delete ep_choice;
1184  ep_choice = NULL;
1185  }
1186 }
1188  if (ratings != NULL) {
1189  ratings->delete_matrix_pointers();
1190  delete ratings;
1191  ratings = NULL;
1192  }
1193 }
1194 
1195 
1196 bool PAGE_RES_IT::operator ==(const PAGE_RES_IT &other) const {
1197  return word_res == other.word_res &&
1198  row_res == other.row_res &&
1199  block_res == other.block_res;
1200 }
1201 
1202 int PAGE_RES_IT::cmp(const PAGE_RES_IT &other) const {
1203  ASSERT_HOST(page_res == other.page_res);
1204  if (other.block_res == NULL) {
1205  // other points to the end of the page.
1206  if (block_res == NULL)
1207  return 0;
1208  return -1;
1209  }
1210  if (block_res == NULL) {
1211  return 1; // we point to the end of the page.
1212  }
1213  if (block_res == other.block_res) {
1214  if (other.row_res == NULL || row_res == NULL) {
1215  // this should only happen if we hit an image block.
1216  return 0;
1217  }
1218  if (row_res == other.row_res) {
1219  // we point to the same block and row.
1220  ASSERT_HOST(other.word_res != NULL && word_res != NULL);
1221  if (word_res == other.word_res) {
1222  // we point to the same word!
1223  return 0;
1224  }
1225 
1226  WERD_RES_IT word_res_it(&row_res->word_res_list);
1227  for (word_res_it.mark_cycle_pt(); !word_res_it.cycled_list();
1228  word_res_it.forward()) {
1229  if (word_res_it.data() == word_res) {
1230  return -1;
1231  } else if (word_res_it.data() == other.word_res) {
1232  return 1;
1233  }
1234  }
1235  ASSERT_HOST("Error: Incomparable PAGE_RES_ITs" == NULL);
1236  }
1237 
1238  // we both point to the same block, but different rows.
1239  ROW_RES_IT row_res_it(&block_res->row_res_list);
1240  for (row_res_it.mark_cycle_pt(); !row_res_it.cycled_list();
1241  row_res_it.forward()) {
1242  if (row_res_it.data() == row_res) {
1243  return -1;
1244  } else if (row_res_it.data() == other.row_res) {
1245  return 1;
1246  }
1247  }
1248  ASSERT_HOST("Error: Incomparable PAGE_RES_ITs" == NULL);
1249  }
1250 
1251  // We point to different blocks.
1252  BLOCK_RES_IT block_res_it(&page_res->block_res_list);
1253  for (block_res_it.mark_cycle_pt();
1254  !block_res_it.cycled_list(); block_res_it.forward()) {
1255  if (block_res_it.data() == block_res) {
1256  return -1;
1257  } else if (block_res_it.data() == other.block_res) {
1258  return 1;
1259  }
1260  }
1261  // Shouldn't happen...
1262  ASSERT_HOST("Error: Incomparable PAGE_RES_ITs" == NULL);
1263  return 0;
1264 }
1265 
1266 // Inserts the new_word as a combination owned by a corresponding WERD_RES
1267 // before the current position. The simple fields of the WERD_RES are copied
1268 // from clone_res and the resulting WERD_RES is returned for further setup
1269 // with best_choice etc.
1271  WERD* new_word) {
1272  // Make a WERD_RES for the new_word.
1273  WERD_RES* new_res = new WERD_RES(new_word);
1274  new_res->CopySimpleFields(clone_res);
1275  new_res->combination = true;
1276  // Insert into the appropriate place in the ROW_RES.
1277  WERD_RES_IT wr_it(&row()->word_res_list);
1278  for (wr_it.mark_cycle_pt(); !wr_it.cycled_list(); wr_it.forward()) {
1279  WERD_RES* word = wr_it.data();
1280  if (word == word_res)
1281  break;
1282  }
1283  ASSERT_HOST(!wr_it.cycled_list());
1284  wr_it.add_before_then_move(new_res);
1285  if (wr_it.at_first()) {
1286  // This is the new first word, so reset the member iterator so it
1287  // detects the cycled_list state correctly.
1288  ResetWordIterator();
1289  }
1290  return new_res;
1291 }
1292 
1293 // Helper computes the boundaries between blobs in the word. The blob bounds
1294 // are likely very poor, if they come from LSTM, where it only outputs the
1295 // character at one pixel within it, so we find the midpoints between them.
1296 static void ComputeBlobEnds(const WERD_RES& word, C_BLOB_LIST* next_word_blobs,
1297  GenericVector<int>* blob_ends) {
1298  C_BLOB_IT blob_it(word.word->cblob_list());
1299  for (int i = 0; i < word.best_state.size(); ++i) {
1300  int length = word.best_state[i];
1301  // Get the bounding box of the fake blobs
1302  TBOX blob_box = blob_it.data()->bounding_box();
1303  blob_it.forward();
1304  for (int b = 1; b < length; ++b) {
1305  blob_box += blob_it.data()->bounding_box();
1306  blob_it.forward();
1307  }
1308  // This blob_box is crap, so for now we are only looking for the
1309  // boundaries between them.
1310  int blob_end = MAX_INT32;
1311  if (!blob_it.at_first() || next_word_blobs != NULL) {
1312  if (blob_it.at_first())
1313  blob_it.set_to_list(next_word_blobs);
1314  blob_end = (blob_box.right() + blob_it.data()->bounding_box().left()) / 2;
1315  }
1316  blob_ends->push_back(blob_end);
1317  }
1318 }
1319 
1320 // Replaces the current WERD/WERD_RES with the given words. The given words
1321 // contain fake blobs that indicate the position of the characters. These are
1322 // replaced with real blobs from the current word as much as possible.
1325  if (words->empty()) {
1326  DeleteCurrentWord();
1327  return;
1328  }
1329  WERD_RES* input_word = word();
1330  // Set the BOL/EOL flags on the words from the input word.
1331  if (input_word->word->flag(W_BOL)) {
1332  (*words)[0]->word->set_flag(W_BOL, true);
1333  } else {
1334  (*words)[0]->word->set_blanks(1);
1335  }
1336  words->back()->word->set_flag(W_EOL, input_word->word->flag(W_EOL));
1337 
1338  // Move the blobs from the input word to the new set of words.
1339  // If the input word_res is a combination, then the replacements will also be
1340  // combinations, and will own their own words. If the input word_res is not a
1341  // combination, then the final replacements will not be either, (although it
1342  // is allowed for the input words to be combinations) and their words
1343  // will get put on the row list. This maintains the ownership rules.
1344  WERD_IT w_it(row()->row->word_list());
1345  if (!input_word->combination) {
1346  for (w_it.mark_cycle_pt(); !w_it.cycled_list(); w_it.forward()) {
1347  WERD* word = w_it.data();
1348  if (word == input_word->word)
1349  break;
1350  }
1351  // w_it is now set to the input_word's word.
1352  ASSERT_HOST(!w_it.cycled_list());
1353  }
1354  // Insert into the appropriate place in the ROW_RES.
1355  WERD_RES_IT wr_it(&row()->word_res_list);
1356  for (wr_it.mark_cycle_pt(); !wr_it.cycled_list(); wr_it.forward()) {
1357  WERD_RES* word = wr_it.data();
1358  if (word == input_word)
1359  break;
1360  }
1361  ASSERT_HOST(!wr_it.cycled_list());
1362  // Since we only have an estimate of the bounds between blobs, use the blob
1363  // x-middle as the determiner of where to put the blobs
1364  C_BLOB_IT src_b_it(input_word->word->cblob_list());
1365  src_b_it.sort(&C_BLOB::SortByXMiddle);
1366  C_BLOB_IT rej_b_it(input_word->word->rej_cblob_list());
1367  rej_b_it.sort(&C_BLOB::SortByXMiddle);
1368  for (int w = 0; w < words->size(); ++w) {
1369  WERD_RES* word_w = (*words)[w];
1370  // Compute blob boundaries.
1371  GenericVector<int> blob_ends;
1372  C_BLOB_LIST* next_word_blobs =
1373  w + 1 < words->size() ? (*words)[w + 1]->word->cblob_list() : NULL;
1374  ComputeBlobEnds(*word_w, next_word_blobs, &blob_ends);
1375  // Delete the fake blobs on the current word.
1376  word_w->word->cblob_list()->clear();
1377  C_BLOB_IT dest_it(word_w->word->cblob_list());
1378  // Build the box word as we move the blobs.
1379  tesseract::BoxWord* box_word = new tesseract::BoxWord;
1380  for (int i = 0; i < blob_ends.size(); ++i) {
1381  int end_x = blob_ends[i];
1382  TBOX blob_box;
1383  // Add the blobs up to end_x.
1384  while (!src_b_it.empty() &&
1385  src_b_it.data()->bounding_box().x_middle() < end_x) {
1386  blob_box += src_b_it.data()->bounding_box();
1387  dest_it.add_after_then_move(src_b_it.extract());
1388  src_b_it.forward();
1389  }
1390  while (!rej_b_it.empty() &&
1391  rej_b_it.data()->bounding_box().x_middle() < end_x) {
1392  blob_box += rej_b_it.data()->bounding_box();
1393  dest_it.add_after_then_move(rej_b_it.extract());
1394  rej_b_it.forward();
1395  }
1396  // Clip to the previously computed bounds. Although imperfectly accurate,
1397  // it is good enough, and much more complicated to determine where else
1398  // to clip.
1399  if (i > 0 && blob_box.left() < blob_ends[i - 1])
1400  blob_box.set_left(blob_ends[i - 1]);
1401  if (blob_box.right() > end_x)
1402  blob_box.set_right(end_x);
1403  box_word->InsertBox(i, blob_box);
1404  }
1405  // Fix empty boxes. If a very joined blob sits over multiple characters,
1406  // then we will have some empty boxes from using the middle, so look for
1407  // overlaps.
1408  for (int i = 0; i < box_word->length(); ++i) {
1409  TBOX box = box_word->BlobBox(i);
1410  if (box.null_box()) {
1411  // Nothing has its middle in the bounds of this blob, so use anything
1412  // that overlaps.
1413  for (dest_it.mark_cycle_pt(); !dest_it.cycled_list();
1414  dest_it.forward()) {
1415  TBOX blob_box = dest_it.data()->bounding_box();
1416  if (blob_box.left() < blob_ends[i] &&
1417  (i == 0 || blob_box.right() >= blob_ends[i - 1])) {
1418  if (i > 0 && blob_box.left() < blob_ends[i - 1])
1419  blob_box.set_left(blob_ends[i - 1]);
1420  if (blob_box.right() > blob_ends[i])
1421  blob_box.set_right(blob_ends[i]);
1422  box_word->ChangeBox(i, blob_box);
1423  break;
1424  }
1425  }
1426  }
1427  }
1428  delete word_w->box_word;
1429  word_w->box_word = box_word;
1430  if (!input_word->combination) {
1431  // Insert word_w->word into the ROW. It doesn't own its word, so the
1432  // ROW needs to own it.
1433  w_it.add_before_stay_put(word_w->word);
1434  word_w->combination = false;
1435  }
1436  (*words)[w] = NULL; // We are taking ownership.
1437  wr_it.add_before_stay_put(word_w);
1438  }
1439  // We have taken ownership of the words.
1440  words->clear();
1441  // Delete the current word, which has been replaced. We could just call
1442  // DeleteCurrentWord, but that would iterate both lists again, and we know
1443  // we are already in the right place.
1444  if (!input_word->combination)
1445  delete w_it.extract();
1446  delete wr_it.extract();
1447  ResetWordIterator();
1448 }
1449 
1450 // Deletes the current WERD_RES and its underlying WERD.
1452  // Check that this word is as we expect. part_of_combos are NEVER iterated
1453  // by the normal iterator, so we should never be trying to delete them.
1454  ASSERT_HOST(!word_res->part_of_combo);
1455  if (!word_res->combination) {
1456  // Combinations own their own word, so we won't find the word on the
1457  // row's word_list, but it is legitimate to try to delete them.
1458  // Delete word from the ROW when not a combination.
1459  WERD_IT w_it(row()->row->word_list());
1460  for (w_it.mark_cycle_pt(); !w_it.cycled_list(); w_it.forward()) {
1461  if (w_it.data() == word_res->word) {
1462  break;
1463  }
1464  }
1465  ASSERT_HOST(!w_it.cycled_list());
1466  delete w_it.extract();
1467  }
1468  // Remove the WERD_RES for the new_word.
1469  // Remove the WORD_RES from the ROW_RES.
1470  WERD_RES_IT wr_it(&row()->word_res_list);
1471  for (wr_it.mark_cycle_pt(); !wr_it.cycled_list(); wr_it.forward()) {
1472  if (wr_it.data() == word_res) {
1473  word_res = NULL;
1474  break;
1475  }
1476  }
1477  ASSERT_HOST(!wr_it.cycled_list());
1478  delete wr_it.extract();
1479  ResetWordIterator();
1480 }
1481 
1482 // Makes the current word a fuzzy space if not already fuzzy. Updates
1483 // corresponding part of combo if required.
1485  WERD* real_word = word_res->word;
1486  if (!real_word->flag(W_FUZZY_SP) && !real_word->flag(W_FUZZY_NON)) {
1487  real_word->set_flag(W_FUZZY_SP, true);
1488  if (word_res->combination) {
1489  // The next word should be the corresponding part of combo, but we have
1490  // already stepped past it, so find it by search.
1491  WERD_RES_IT wr_it(&row()->word_res_list);
1492  for (wr_it.mark_cycle_pt();
1493  !wr_it.cycled_list() && wr_it.data() != word_res; wr_it.forward()) {
1494  }
1495  wr_it.forward();
1496  ASSERT_HOST(wr_it.data()->part_of_combo);
1497  real_word = wr_it.data()->word;
1498  ASSERT_HOST(!real_word->flag(W_FUZZY_SP) &&
1499  !real_word->flag(W_FUZZY_NON));
1500  real_word->set_flag(W_FUZZY_SP, true);
1501  }
1502  }
1503 }
1504 
1505 /*************************************************************************
1506  * PAGE_RES_IT::restart_page
1507  *
1508  * Set things up at the start of the page
1509  *************************************************************************/
1510 
1512  block_res_it.set_to_list(&page_res->block_res_list);
1513  block_res_it.mark_cycle_pt();
1514  prev_block_res = NULL;
1515  prev_row_res = NULL;
1516  prev_word_res = NULL;
1517  block_res = NULL;
1518  row_res = NULL;
1519  word_res = NULL;
1520  next_block_res = NULL;
1521  next_row_res = NULL;
1522  next_word_res = NULL;
1523  internal_forward(true, empty_ok);
1524  return internal_forward(false, empty_ok);
1525 }
1526 
1527 // Recovers from operations on the current word, such as in InsertCloneWord
1528 // and DeleteCurrentWord.
1529 // Resets the word_res_it so that it is one past the next_word_res, as
1530 // it should be after internal_forward. If next_row_res != row_res,
1531 // then the next_word_res is in the next row, so there is no need to do
1532 // anything to word_res_it, but it is still a good idea to reset the pointers
1533 // word_res and prev_word_res, which are still in the current row.
1535  if (row_res == next_row_res) {
1536  // Reset the member iterator so it can move forward and detect the
1537  // cycled_list state correctly.
1538  word_res_it.move_to_first();
1539  for (word_res_it.mark_cycle_pt();
1540  !word_res_it.cycled_list() && word_res_it.data() != next_word_res;
1541  word_res_it.forward()) {
1542  if (!word_res_it.data()->part_of_combo) {
1543  if (prev_row_res == row_res) prev_word_res = word_res;
1544  word_res = word_res_it.data();
1545  }
1546  }
1547  ASSERT_HOST(!word_res_it.cycled_list());
1548  word_res_it.forward();
1549  } else {
1550  // word_res_it is OK, but reset word_res and prev_word_res if needed.
1551  WERD_RES_IT wr_it(&row_res->word_res_list);
1552  for (wr_it.mark_cycle_pt(); !wr_it.cycled_list(); wr_it.forward()) {
1553  if (!wr_it.data()->part_of_combo) {
1554  if (prev_row_res == row_res) prev_word_res = word_res;
1555  word_res = wr_it.data();
1556  }
1557  }
1558  }
1559 }
1560 
1561 /*************************************************************************
1562  * PAGE_RES_IT::internal_forward
1563  *
1564  * Find the next word on the page. If empty_ok is true, then non-text blocks
1565  * and text blocks with no text are visited as if they contain a single
1566  * imaginary word in a single imaginary row. (word() and row() both return NULL
1567  * in such a block and the return value is NULL.)
1568  * If empty_ok is false, the old behaviour is maintained. Each real word
1569  * is visited and empty and non-text blocks and rows are skipped.
1570  * new_block is used to initialize the iterators for a new block.
1571  * The iterator maintains pointers to block, row and word for the previous,
1572  * current and next words. These are correct, regardless of block/row
1573  * boundaries. NULL values denote start and end of the page.
1574  *************************************************************************/
1575 
1576 WERD_RES *PAGE_RES_IT::internal_forward(bool new_block, bool empty_ok) {
1577  bool new_row = false;
1578 
1579  prev_block_res = block_res;
1580  prev_row_res = row_res;
1581  prev_word_res = word_res;
1582  block_res = next_block_res;
1583  row_res = next_row_res;
1584  word_res = next_word_res;
1585  next_block_res = NULL;
1586  next_row_res = NULL;
1587  next_word_res = NULL;
1588 
1589  while (!block_res_it.cycled_list()) {
1590  if (new_block) {
1591  new_block = false;
1592  row_res_it.set_to_list(&block_res_it.data()->row_res_list);
1593  row_res_it.mark_cycle_pt();
1594  if (row_res_it.empty() && empty_ok) {
1595  next_block_res = block_res_it.data();
1596  break;
1597  }
1598  new_row = true;
1599  }
1600  while (!row_res_it.cycled_list()) {
1601  if (new_row) {
1602  new_row = false;
1603  word_res_it.set_to_list(&row_res_it.data()->word_res_list);
1604  word_res_it.mark_cycle_pt();
1605  }
1606  // Skip any part_of_combo words.
1607  while (!word_res_it.cycled_list() && word_res_it.data()->part_of_combo)
1608  word_res_it.forward();
1609  if (!word_res_it.cycled_list()) {
1610  next_block_res = block_res_it.data();
1611  next_row_res = row_res_it.data();
1612  next_word_res = word_res_it.data();
1613  word_res_it.forward();
1614  goto foundword;
1615  }
1616  // end of row reached
1617  row_res_it.forward();
1618  new_row = true;
1619  }
1620  // end of block reached
1621  block_res_it.forward();
1622  new_block = true;
1623  }
1624  foundword:
1625  // Update prev_word_best_choice pointer.
1626  if (page_res != NULL && page_res->prev_word_best_choice != NULL) {
1627  *page_res->prev_word_best_choice =
1628  (new_block || prev_word_res == NULL) ? NULL : prev_word_res->best_choice;
1629  }
1630  return word_res;
1631 }
1632 
1633 /*************************************************************************
1634  * PAGE_RES_IT::restart_row()
1635  *
1636  * Move to the beginning (leftmost word) of the current row.
1637  *************************************************************************/
1639  ROW_RES *row = this->row();
1640  if (!row) return NULL;
1641  for (restart_page(); this->row() != row; forward()) {
1642  // pass
1643  }
1644  return word();
1645 }
1646 
1647 /*************************************************************************
1648  * PAGE_RES_IT::forward_paragraph
1649  *
1650  * Move to the beginning of the next paragraph, allowing empty blocks.
1651  *************************************************************************/
1652 
1654  while (block_res == next_block_res &&
1655  (next_row_res != NULL && next_row_res->row != NULL &&
1656  row_res->row->para() == next_row_res->row->para())) {
1657  internal_forward(false, true);
1658  }
1659  return internal_forward(false, true);
1660 }
1661 
1662 /*************************************************************************
1663  * PAGE_RES_IT::forward_block
1664  *
1665  * Move to the beginning of the next block, allowing empty blocks.
1666  *************************************************************************/
1667 
1669  while (block_res == next_block_res) {
1670  internal_forward(false, true);
1671  }
1672  return internal_forward(false, true);
1673 }
1674 
1676  inT16 chars_in_word;
1677  inT16 rejects_in_word = 0;
1678 
1679  chars_in_word = word_res->reject_map.length ();
1680  page_res->char_count += chars_in_word;
1681  block_res->char_count += chars_in_word;
1682  row_res->char_count += chars_in_word;
1683 
1684  rejects_in_word = word_res->reject_map.reject_count ();
1685 
1686  page_res->rej_count += rejects_in_word;
1687  block_res->rej_count += rejects_in_word;
1688  row_res->rej_count += rejects_in_word;
1689  if (chars_in_word == rejects_in_word)
1690  row_res->whole_word_rej_count += rejects_in_word;
1691 }
Definition: werd.h:44
void add_str_int(const char *str, int number)
Definition: strngs.cpp:381
ROW_RES()
Definition: pageres.h:133
void InitForRetryRecognition(const WERD_RES &source)
Definition: pageres.cpp:269
const double kMaxWordGapRatio
Definition: pageres.cpp:48
int latin_sid() const
Definition: unicharset.h:845
inT32 char_count
Definition: pageres.h:60
void ReplaceBestChoice(WERD_CHOICE *choice)
Definition: pageres.cpp:787
float body_size() const
Definition: ocrrow.h:70
#define TRUE
Definition: capi.h:45
UNICHAR_ID unichar_id(int index) const
Definition: ratngs.h:313
WERD_RES * restart_row()
Definition: pageres.cpp:1638
void RebuildBestState()
Definition: pageres.cpp:800
const double kMaxLineSizeRatio
Definition: pageres.cpp:46
void InitPointers()
Definition: pageres.cpp:1117
void print() const
Definition: ratngs.h:578
bool PrepareToInsertSeam(const GenericVector< SEAM *> &seams, const GenericVector< TBLOB *> &blobs, int insert_index, bool modify)
Definition: seam.cpp:82
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
Definition: tesscallback.h:116
static void BreakPieces(const GenericVector< SEAM *> &seams, const GenericVector< TBLOB *> &blobs, int first, int last)
Definition: seam.cpp:194
int UNICHAR_ID
Definition: unichar.h:33
bool LogNewCookedChoice(int max_num_choices, bool debug, WERD_CHOICE *word_choice)
Definition: pageres.cpp:612
void start_seam_list(TWERD *word, GenericVector< SEAM *> *seam_array)
Definition: seam.cpp:269
float baseline_shift
Definition: pageres.h:297
BOOL8 tess_failed
Definition: pageres.h:272
GenericVector< int > best_state
Definition: pageres.h:255
WERD_RES * forward_paragraph()
Definition: pageres.cpp:1653
const FontInfo * fontinfo2
Definition: pageres.h:289
WERD_CHOICE * best_choice
Definition: pageres.h:219
const int kWordrecMaxNumJoinChunks
Definition: pageres.cpp:41
void SetAllScriptPositions(tesseract::ScriptPos position)
Definition: pageres.cpp:860
inT8 bold
Definition: pageres.h:286
void SetScriptPositions()
Definition: pageres.cpp:853
BOOL8 guessed_caps_ht
Definition: pageres.h:293
BlamerBundle * blamer_bundle
Definition: pageres.h:230
#define MAX_INT32
Definition: host.h:62
void ClearRatings()
Definition: pageres.cpp:1187
static BoxWord * CopyFromNormalized(TWERD *tessword)
Definition: boxword.cpp:59
virtual R Run(A1, A2)=0
BLOCK_RES_LIST block_res_list
Definition: pageres.h:62
Definition: werd.h:36
void ReplaceCurrentWord(tesseract::PointerVector< WERD_RES > *words)
Definition: pageres.cpp:1323
GenericVector< STRING > correct_text
Definition: pageres.h:259
int push_back(T object)
float rating() const
Definition: ratngs.h:79
void set_flag(WERD_FLAGS mask, BOOL8 value)
Definition: werd.h:129
inT32 rej_count
Definition: pageres.h:61
WERD_RES & operator=(const WERD_RES &source)
Definition: pageres.cpp:178
TWERD * rebuild_word
Definition: pageres.h:244
#define tprintf(...)
Definition: tprintf.h:31
void ClearResults()
Definition: pageres.cpp:1142
void rej_stat_word()
Definition: pageres.cpp:1675
void BestChoiceToCorrectText()
Definition: pageres.cpp:918
const char * string() const
Definition: strngs.cpp:198
int TotalOfStates() const
Definition: ratngs.cpp:697
inT8 italic
Definition: pageres.h:285
bool StatesAllValid()
Definition: pageres.cpp:450
GenericVector< int > blob_widths
Definition: pageres.h:205
WERD_LIST * word_list()
Definition: ocrrow.h:52
bool empty() const
Definition: genericvector.h:90
float x_height() const
Definition: ocrrow.h:61
void CloneChoppedToRebuild()
Definition: pageres.cpp:828
UNICHAR_ID BothHyphens(UNICHAR_ID id1, UNICHAR_ID id2)
Definition: pageres.cpp:1025
void FilterWordChoices(int debug_level)
Definition: pageres.cpp:505
bool ConditionalBlobMerge(TessResultCallback2< UNICHAR_ID, UNICHAR_ID, UNICHAR_ID > *class_cb, TessResultCallback2< bool, const TBOX &, const TBOX &> *box_cb)
Definition: pageres.cpp:933
int size() const
Definition: genericvector.h:72
void MergeAdjacentBlobs(int index)
Definition: pageres.cpp:969
float certainty() const
Definition: ratngs.h:82
TBOX bounding_box() const
Definition: werd.cpp:160
tesseract::Tesseract * tesseract
Definition: pageres.h:266
void fix_hyphens()
Definition: pageres.cpp:1042
int16_t inT16
Definition: host.h:36
bool HyphenBoxesOverlap(const TBOX &box1, const TBOX &box2)
Definition: pageres.cpp:1036
tesseract::BoxWord * box_word
Definition: pageres.h:250
BOOL8 guessed_x_ht
Definition: pageres.h:292
#define ASSERT_HOST(x)
Definition: errcode.h:84
UNICHAR_ID BothQuotes(UNICHAR_ID id1, UNICHAR_ID id2)
Definition: pageres.cpp:1003
void string_and_lengths(STRING *word_str, STRING *word_lengths_str) const
Definition: ratngs.cpp:427
BOOL8 flag(WERD_FLAGS mask) const
Definition: werd.h:128
BOOL8 reject_spaces
Definition: pageres.h:320
inT16 left() const
Definition: rect.h:68
MATRIX * ratings
Definition: pageres.h:215
Definition: blobs.h:395
void copy_on(WERD_RES *word_res)
Definition: pageres.h:644
int cmp(const PAGE_RES_IT &other) const
Definition: pageres.cpp:1202
WERD_CHOICE ** prev_word_best_choice
Definition: pageres.h:66
bool IsText() const
Definition: polyblk.h:52
static int SortByXMiddle(const void *v1, const void *v2)
Definition: stepblob.h:119
BOOL8 combination
Definition: pageres.h:318
void InsertSeam(int blob_number, SEAM *seam)
Definition: pageres.cpp:410
void fix_quotes()
Definition: pageres.cpp:1013
void FakeClassifyWord(int blob_count, BLOB_CHOICE **choices)
Definition: pageres.cpp:872
void SetupWordScript(const UNICHARSET &unicharset_in)
Definition: pageres.cpp:376
PAGE_RES * page_res
Definition: pageres.h:661
void Clear()
Definition: pageres.cpp:1132
WERD_RES * start_page(bool empty_ok)
Definition: pageres.cpp:1511
static void JoinPieces(const GenericVector< SEAM *> &seams, const GenericVector< TBLOB *> &blobs, int first, int last)
Definition: seam.cpp:216
Definition: seam.h:44
Definition: strngs.h:45
float caps_height
Definition: pageres.h:296
WERD_CHOICE_LIST best_choices
Definition: pageres.h:227
#define FALSE
Definition: capi.h:46
const FontInfo * fontinfo
Definition: pageres.h:288
void FakeWordFromRatings(PermuterType permuter)
Definition: pageres.cpp:893
void SetupBasicsFromChoppedWord(const UNICHARSET &unicharset_in)
Definition: pageres.cpp:335
WERD_CHOICE * raw_choice
Definition: pageres.h:224
WERD_RES * forward_block()
Definition: pageres.cpp:1668
BOOL8 tess_would_adapt
Definition: pageres.h:281
void SetupFake(const UNICHARSET &uch)
Definition: pageres.cpp:344
BOOL8 tess_accepted
Definition: pageres.h:280
bool odd_size
Definition: pageres.h:284
bool null_box() const
Definition: rect.h:46
void set_unichar_id(UNICHAR_ID newunichar_id)
Definition: ratngs.h:144
void DeleteCurrentWord()
Definition: pageres.cpp:1451
void SetupBlobWidthsAndGaps()
Definition: pageres.cpp:392
float certainty() const
Definition: ratngs.h:328
Definition: werd.h:35
BOOL8 part_of_combo
Definition: pageres.h:319
void ClearWordChoices()
Definition: pageres.cpp:1175
inT8 fontinfo_id2_count
Definition: pageres.h:291
void ResetWordIterator()
Definition: pageres.cpp:1534
bool AlternativeChoiceAdjustmentsWorseThan(float threshold) const
Definition: pageres.cpp:431
CRUNCH_MODE unlv_crunch_mode
Definition: pageres.h:294
float ascenders() const
Definition: ocrrow.h:79
int state(int index) const
Definition: ratngs.h:317
void SetupBoxWord()
Definition: pageres.cpp:843
const STRING & unichar_string() const
Definition: ratngs.h:539
Definition: rect.h:30
tesseract::BoxWord * bln_boxes
Definition: pageres.h:184
T & back() const
bool operator==(const PAGE_RES_IT &other) const
Definition: pageres.cpp:1196
POLY_BLOCK * poly_block() const
Definition: pdblock.h:55
Definition: matrix.h:563
Definition: blobs.h:261
void append_unichar_id_space_allocated(UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)
Definition: ratngs.h:450
DENORM denorm
Definition: pageres.h:190
float adjust_factor() const
Definition: ratngs.h:304
const double kMaxWordSizeRatio
Definition: pageres.cpp:44
void merge_tess_fails()
Definition: pageres.cpp:1062
UNICHAR_ID BothSpaces(UNICHAR_ID id1, UNICHAR_ID id2)
Definition: pageres.cpp:1054
C_BLOB_LIST * cblob_list()
Definition: werd.h:100
inT16 height() const
Definition: rect.h:104
void PrintBestChoices() const
Definition: pageres.cpp:709
void SetupBlamerBundle()
Definition: pageres.cpp:385
inT8 fontinfo_id_count
Definition: pageres.h:290
BLOB_CHOICE * GetBlobChoice(int index) const
Definition: pageres.cpp:742
bool small_caps
Definition: pageres.h:283
void make_bad()
Set the fields in this choice to be default (bad) values.
Definition: ratngs.h:441
WERD * word
Definition: pageres.h:175
bool Valid(const MATRIX &m) const
Definition: matrix.h:601
inT16 right() const
Definition: rect.h:75
void UpdateStateForSplit(int blob_position)
Definition: ratngs.cpp:685
BLOB_CHOICE_LIST * GetBlobChoices(int index) const
Definition: pageres.cpp:751
inT16 width() const
Definition: rect.h:111
void set_right(int x)
Definition: rect.h:78
void set_left(int x)
Definition: rect.h:71
UNICHAR_ID unichar_id() const
Definition: ratngs.h:76
WERD_CHOICE * ep_choice
Definition: pageres.h:270
bool SetupForRecognition(const UNICHARSET &unicharset_in, tesseract::Tesseract *tesseract, Pix *pix, int norm_mode, const TBOX *norm_box, bool numeric_mode, bool use_body_size, bool allow_detailed_fx, ROW *row, const BLOCK *block)
Definition: pageres.cpp:294
C_BLOB_LIST * rej_cblob_list()
Definition: werd.h:95
void ComputeAdaptionThresholds(float certainty_scale, float min_rating, float max_rating, float rating_margin, float *thresholds)
Definition: pageres.cpp:553
GenericVector< int > blob_gaps
Definition: pageres.h:208
void operator=(const ELIST_LINK &)
Definition: elst.h:101
void ConsumeWordResults(WERD_RES *word)
Definition: pageres.cpp:757
#define ELISTIZE(CLASSNAME)
Definition: elst.h:961
void InitNonPointers()
Definition: pageres.cpp:1089
bool LogNewRawChoice(WERD_CHOICE *word_choice)
Definition: pageres.cpp:596
void DebugTopChoice(const char *msg) const
Definition: pageres.cpp:491
WERD_RES * InsertSimpleCloneWord(const WERD_RES &clone_res, WERD *new_word)
Definition: pageres.cpp:1270
bool IsAmbiguous()
Definition: pageres.cpp:444
BLOB_CHOICE * FindMatchingChoice(UNICHAR_ID char_id, BLOB_CHOICE_LIST *bc_list)
Definition: ratngs.cpp:160
const UNICHARSET * uch_set
Definition: pageres.h:192
CLISTIZE(BLOCK_RES) ELISTIZE(ROW_RES) ELISTIZE(WERD_RES) static const double kStopperAmbiguityThresholdGain
int default_sid() const
Definition: unicharset.h:853
void Init()
Definition: pageres.h:75
void DebugWordChoices(bool debug, const char *word_to_debug)
Definition: pageres.cpp:472
TBOX bounding_box() const
Definition: blobs.cpp:482
Definition: werd.h:60
TWERD * chopped_word
Definition: pageres.h:201
REJMAP reject_map
Definition: pageres.h:271
void set_permuter(uinT8 perm)
Definition: ratngs.h:373
Definition: ocrrow.h:32
int count(LIST var_list)
Definition: oldlist.cpp:103
ROW_LIST * row_list()
get rows
Definition: ocrblock.h:120
int GetBlobsWidth(int start_blob, int last_blob)
Definition: pageres.cpp:722
void MakeCurrentWordFuzzy()
Definition: pageres.cpp:1484
float x_height
Definition: pageres.h:295
static TWERD * PolygonalCopy(bool allow_detailed_fx, WERD *src)
Definition: blobs.cpp:793
Definition: ocrblock.h:30
PermuterType
Definition: ratngs.h:240
bool script_has_xheight() const
Definition: unicharset.h:863
ROW * blob_row
Definition: pageres.h:186
void CopySimpleFields(const WERD_RES &source)
Definition: pageres.cpp:241
BOOL8 done
Definition: pageres.h:282
int length() const
Definition: boxword.h:85
float rating() const
Definition: ratngs.h:325
float descenders() const
Definition: ocrrow.h:82
GenericVector< SEAM * > seam_array
Definition: pageres.h:203
BLOCK_RES()
Definition: pageres.h:112
bool PiecesAllNatural(int start, int count) const
Definition: pageres.cpp:1073
PAGE_RES()
Definition: pageres.h:83
bool HasAnySplits() const
Definition: seam.h:67
int GetBlobsGap(int blob_index)
Definition: pageres.cpp:732