tesseract  4.00.00dev
pageres.h
Go to the documentation of this file.
1 /**********************************************************************
2  * File: pageres.h (Formerly page_res.h)
3  * Description: Results classes used by control.c
4  * Author: Phil Cheatle
5  * Created: Tue Sep 22 08:42:49 BST 1992
6  *
7  * (C) Copyright 1992, Hewlett-Packard Ltd.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 #ifndef PAGERES_H
20 #define PAGERES_H
21 
22 #include "blamer.h"
23 #include "blobs.h"
24 #include "boxword.h"
25 #include "elst.h"
26 #include "genericvector.h"
27 #include "normalis.h"
28 #include "ocrblock.h"
29 #include "ocrrow.h"
31 #include "ratngs.h"
32 #include "rejctmap.h"
33 #include "seam.h"
34 #include "werd.h"
35 
36 namespace tesseract {
37 struct FontInfo;
38 class Tesseract;
39 }
41 
42 /* Forward declarations */
43 
44 class BLOCK_RES;
45 
47 class
48 ROW_RES;
49 
51 class WERD_RES;
52 
54 
55 /*************************************************************************
56  * PAGE_RES - Page results
57  *************************************************************************/
58 class PAGE_RES { // page result
59  public:
62  BLOCK_RES_LIST block_res_list;
64  // Updated every time PAGE_RES_IT iterating on this PAGE_RES moves to
65  // the next word. This pointer is not owned by PAGE_RES class.
67  // Sums of blame reasons computed by the blamer.
69  // Debug information about all the misadaptions on this page.
70  // Each BlamerBundle contains an index into this vector, so that words that
71  // caused misadaption could be marked. However, since words could be
72  // deleted/split/merged, the log is stored on the PAGE_RES level.
74 
75  inline void Init() {
76  char_count = 0;
77  rej_count = 0;
78  rejected = FALSE;
79  prev_word_best_choice = NULL;
80  blame_reasons.init_to_size(IRR_NUM_REASONS, 0);
81  }
82 
83  PAGE_RES() { Init(); } // empty constructor
84 
85  PAGE_RES(bool merge_similar_words,
86  BLOCK_LIST *block_list, // real blocks
87  WERD_CHOICE **prev_word_best_choice_ptr);
88 
89  ~PAGE_RES () { // destructor
90  }
91 };
92 
93 /*************************************************************************
94  * BLOCK_RES - Block results
95  *************************************************************************/
96 
97 class BLOCK_RES:public ELIST_LINK {
98  public:
99  BLOCK * block; // real block
100  inT32 char_count; // chars in block
101  inT32 rej_count; // rejected chars
104  float x_height;
105  BOOL8 font_assigned; // block already
106  // processed
107  BOOL8 bold; // all bold
108  BOOL8 italic; // all italic
109 
110  ROW_RES_LIST row_res_list;
111 
113  } // empty constructor
114 
115  BLOCK_RES(bool merge_similar_words, BLOCK *the_block); // real block
116 
117  ~BLOCK_RES () { // destructor
118  }
119 };
120 
121 /*************************************************************************
122  * ROW_RES - Row results
123  *************************************************************************/
124 
125 class ROW_RES:public ELIST_LINK {
126  public:
127  ROW * row; // real row
128  inT32 char_count; // chars in block
129  inT32 rej_count; // rejected chars
130  inT32 whole_word_rej_count; // rejs in total rej wds
131  WERD_RES_LIST word_res_list;
132 
134  } // empty constructor
135 
136  ROW_RES(bool merge_similar_words, ROW *the_row); // real row
137 
138  ~ROW_RES() { // destructor
139  }
140 };
141 
142 /*************************************************************************
143  * WERD_RES - Word results
144  *************************************************************************/
146 {
151 };
152 
153 // WERD_RES is a collection of publicly accessible members that gathers
154 // information about a word result.
155 class WERD_RES : public ELIST_LINK {
156  public:
157  // Which word is which?
158  // There are 3 coordinate spaces in use here: a possibly rotated pixel space,
159  // the original image coordinate space, and the BLN space in which the
160  // baseline of a word is at kBlnBaselineOffset, the xheight is kBlnXHeight,
161  // and the x-middle of the word is at 0.
162  // In the rotated pixel space, coordinates correspond to the input image,
163  // but may be rotated about the origin by a multiple of 90 degrees,
164  // and may therefore be negative.
165  // In any case a rotation by denorm.block()->re_rotation() will take them
166  // back to the original image.
167  // The other differences between words all represent different stages of
168  // processing during recognition.
169 
170  // ---------------------------INPUT-------------------------------------
171 
172  // The word is the input C_BLOBs in the rotated pixel space.
173  // word is NOT owned by the WERD_RES unless combination is true.
174  // All the other word pointers ARE owned by the WERD_RES.
175  WERD* word; // Input C_BLOB word.
176 
177  // -------------SETUP BY SetupFor*Recognition---READONLY-INPUT------------
178 
179  // The bln_boxes contains the bounding boxes (only) of the input word, in the
180  // BLN space. The lengths of word and bln_boxes
181  // match as they are both before any chopping.
182  // TODO(rays) determine if docqual does anything useful and delete bln_boxes
183  // if it doesn't.
184  tesseract::BoxWord* bln_boxes; // BLN input bounding boxes.
185  // The ROW that this word sits in. NOT owned by the WERD_RES.
187  // The denorm provides the transformation to get back to the rotated image
188  // coords from the chopped_word/rebuild_word BLN coords, but each blob also
189  // has its own denorm.
190  DENORM denorm; // For use on chopped_word.
191  // Unicharset used by the classifier output in best_choice and raw_choice.
192  const UNICHARSET* uch_set; // For converting back to utf8.
193 
194  // ----Initialized by SetupFor*Recognition---BUT OUTPUT FROM RECOGNITION----
195  // ----Setup to a (different!) state expected by the various classifiers----
196  // TODO(rays) Tidy and make more consistent.
197 
198  // The chopped_word is also in BLN space, and represents the fully chopped
199  // character fragments that make up the word.
200  // The length of chopped_word matches length of seam_array + 1 (if set).
201  TWERD* chopped_word; // BLN chopped fragments output.
202  // Vector of SEAM* holding chopping points matching chopped_word.
204  // Widths of blobs in chopped_word.
206  // Gaps between blobs in chopped_word. blob_gaps[i] is the gap between
207  // blob i and blob i+1.
209  // Ratings matrix contains classifier choices for each classified combination
210  // of blobs. The dimension is the same as the number of blobs in chopped_word
211  // and the leading diagonal corresponds to classifier results of the blobs
212  // in chopped_word. The state_ members of best_choice, raw_choice and
213  // best_choices all correspond to this ratings matrix and allow extraction
214  // of the blob choices for any given WERD_CHOICE.
215  MATRIX* ratings; // Owned pointer.
216  // Pointer to the first WERD_CHOICE in best_choices. This is the result that
217  // will be output from Tesseract. Note that this is now a borrowed pointer
218  // and should NOT be deleted.
219  WERD_CHOICE* best_choice; // Borrowed pointer.
220  // The best raw_choice found during segmentation search. Differs from the
221  // best_choice by being the best result according to just the character
222  // classifier, not taking any language model information into account.
223  // Unlike best_choice, the pointer IS owned by this WERD_RES.
224  WERD_CHOICE* raw_choice; // Owned pointer.
225  // Alternative results found during chopping/segmentation search stages.
226  // Note that being an ELIST, best_choices owns the WERD_CHOICEs.
227  WERD_CHOICE_LIST best_choices;
228 
229  // Truth bounding boxes, text and incorrect choice reason.
231 
232  // --------------OUTPUT FROM RECOGNITION-------------------------------
233  // --------------Not all fields are necessarily set.-------------------
234  // ---best_choice, raw_choice *must* end up set, with a box_word-------
235  // ---In complete output, the number of blobs in rebuild_word matches---
236  // ---the number of boxes in box_word, the number of unichar_ids in---
237  // ---best_choice, the number of ints in best_state, and the number---
238  // ---of strings in correct_text--------------------------------------
239  // ---SetupFake Sets everything to appropriate values if the word is---
240  // ---known to be bad before recognition.------------------------------
241 
242  // The rebuild_word is also in BLN space, but represents the final best
243  // segmentation of the word. Its length is therefore the same as box_word.
244  TWERD* rebuild_word; // BLN best segmented word.
245  // The box_word is in the original image coordinate space. It is the
246  // bounding boxes of the rebuild_word, after denormalization.
247  // The length of box_word matches rebuild_word, best_state (if set) and
248  // correct_text (if set), as well as best_choice and represents the
249  // number of classified units in the output.
250  tesseract::BoxWord* box_word; // Denormalized output boxes.
251  // The best_state stores the relationship between chopped_word and
252  // rebuild_word. Each blob[i] in rebuild_word is composed of best_state[i]
253  // adjacent blobs in chopped_word. The seams in seam_array are hidden
254  // within a rebuild_word blob and revealed between them.
255  GenericVector<int> best_state; // Number of blobs in each best blob.
256  // The correct_text is used during training and adaption to carry the
257  // text to the training system without the need for a unicharset. There
258  // is one entry in the vector for each blob in rebuild_word and box_word.
260  // The Tesseract that was used to recognize this word. Just a borrowed
261  // pointer. Note: Tesseract's class definition is in a higher-level library.
262  // We avoid introducing a cyclic dependency by not using the Tesseract
263  // within WERD_RES. We are just storing it to provide access to it
264  // for the top-level multi-language controller, and maybe for output of
265  // the recognized language.
267 
268  // Less-well documented members.
269  // TODO(rays) Add more documentation here.
270  WERD_CHOICE *ep_choice; // ep text TODO(rays) delete this.
271  REJMAP reject_map; // best_choice rejects
273  /*
274  If tess_failed is TRUE, one of the following tests failed when Tess
275  returned:
276  - The outword blob list was not the same length as the best_choice string;
277  - The best_choice string contained ALL blanks;
278  - The best_choice string was zero length
279  */
280  BOOL8 tess_accepted; // Tess thinks its ok?
281  BOOL8 tess_would_adapt; // Tess would adapt?
282  BOOL8 done; // ready for output?
283  bool small_caps; // word appears to be small caps
284  bool odd_size; // word is bigger than line or leader dots.
287  // The fontinfos are pointers to data owned by the classifier.
290  inT8 fontinfo_id_count; // number of votes
291  inT8 fontinfo_id2_count; // number of votes
295  float x_height; // post match estimate
296  float caps_height; // post match estimate
297  float baseline_shift; // post match estimate.
298  // Certainty score for the spaces either side of this word (LSTM mode).
299  // MIN this value with the actual word certainty.
301 
302  /*
303  To deal with fuzzy spaces we need to be able to combine "words" to form
304  combinations when we suspect that the gap is a non-space. The (new) text
305  ord code generates separate words for EVERY fuzzy gap - flags in the word
306  indicate whether the gap is below the threshold (fuzzy kern) and is thus
307  NOT a real word break by default, or above the threshold (fuzzy space) and
308  this is a real word break by default.
309 
310  The WERD_RES list contains all these words PLUS "combination" words built
311  out of (copies of) the words split by fuzzy kerns. The separate parts have
312  their "part_of_combo" flag set true and should be IGNORED on a default
313  reading of the list.
314 
315  Combination words are FOLLOWED by the sequence of part_of_combo words
316  which they combine.
317  */
318  BOOL8 combination; //of two fuzzy gap wds
319  BOOL8 part_of_combo; //part of a combo
320  BOOL8 reject_spaces; //Reject spacing?
321 
323  InitNonPointers();
324  InitPointers();
325  }
326  WERD_RES(WERD *the_word) {
327  InitNonPointers();
328  InitPointers();
329  word = the_word;
330  }
331  // Deep copies everything except the ratings MATRIX.
332  // To get that use deep_copy below.
333  WERD_RES(const WERD_RES& source) : ELIST_LINK(source) {
334  InitPointers();
335  *this = source; // see operator=
336  }
337 
338  ~WERD_RES();
339 
340  // Returns the UTF-8 string for the given blob index in the best_choice word,
341  // given that we know whether we are in a right-to-left reading context.
342  // This matters for mirrorable characters such as parentheses. We recognize
343  // characters purely based on their shape on the page, and by default produce
344  // the corresponding unicode for a left-to-right context.
345  const char* BestUTF8(int blob_index, bool in_rtl_context) const {
346  if (blob_index < 0 || best_choice == NULL ||
347  blob_index >= best_choice->length())
348  return NULL;
349  UNICHAR_ID id = best_choice->unichar_id(blob_index);
350  if (id < 0 || id >= uch_set->size() || id == INVALID_UNICHAR_ID)
351  return NULL;
352  UNICHAR_ID mirrored = uch_set->get_mirror(id);
353  if (in_rtl_context && mirrored > 0 && mirrored != INVALID_UNICHAR_ID)
354  id = mirrored;
355  return uch_set->id_to_unichar_ext(id);
356  }
357  // Returns the UTF-8 string for the given blob index in the raw_choice word.
358  const char* RawUTF8(int blob_index) const {
359  if (blob_index < 0 || blob_index >= raw_choice->length())
360  return NULL;
361  UNICHAR_ID id = raw_choice->unichar_id(blob_index);
362  if (id < 0 || id >= uch_set->size() || id == INVALID_UNICHAR_ID)
363  return NULL;
364  return uch_set->id_to_unichar(id);
365  }
366 
367  UNICHARSET::Direction SymbolDirection(int blob_index) const {
368  if (best_choice == NULL ||
369  blob_index >= best_choice->length() ||
370  blob_index < 0)
372  return uch_set->get_direction(best_choice->unichar_id(blob_index));
373  }
374 
375  bool AnyRtlCharsInWord() const {
376  if (uch_set == NULL || best_choice == NULL || best_choice->length() < 1)
377  return false;
378  for (int id = 0; id < best_choice->length(); id++) {
379  int unichar_id = best_choice->unichar_id(id);
380  if (unichar_id < 0 || unichar_id >= uch_set->size())
381  continue; // Ignore illegal chars.
383  uch_set->get_direction(unichar_id);
384  if (dir == UNICHARSET::U_RIGHT_TO_LEFT ||
387  return true;
388  }
389  return false;
390  }
391 
392  bool AnyLtrCharsInWord() const {
393  if (uch_set == NULL || best_choice == NULL || best_choice->length() < 1)
394  return false;
395  for (int id = 0; id < best_choice->length(); id++) {
396  int unichar_id = best_choice->unichar_id(id);
397  if (unichar_id < 0 || unichar_id >= uch_set->size())
398  continue; // Ignore illegal chars.
399  UNICHARSET::Direction dir = uch_set->get_direction(unichar_id);
400  if (dir == UNICHARSET::U_LEFT_TO_RIGHT)
401  return true;
402  }
403  return false;
404  }
405 
406  // Return whether the blobs in this WERD_RES 0, 1,... come from an engine
407  // that gave us the unichars in reading order (as opposed to strict left
408  // to right).
409  bool UnicharsInReadingOrder() const {
410  return best_choice->unichars_in_script_order();
411  }
412 
413  void InitNonPointers();
414  void InitPointers();
415  void Clear();
416  void ClearResults();
417  void ClearWordChoices();
418  void ClearRatings();
419 
420  // Deep copies everything except the ratings MATRIX.
421  // To get that use deep_copy below.
422  WERD_RES& operator=(const WERD_RES& source); //from this
423 
424  void CopySimpleFields(const WERD_RES& source);
425 
426  // Initializes a blank (default constructed) WERD_RES from one that has
427  // already been recognized.
428  // Use SetupFor*Recognition afterwards to complete the setup and make
429  // it ready for a retry recognition.
430  void InitForRetryRecognition(const WERD_RES& source);
431 
432  // Sets up the members used in recognition: bln_boxes, chopped_word,
433  // seam_array, denorm. Returns false if
434  // the word is empty and sets up fake results. If use_body_size is
435  // true and row->body_size is set, then body_size will be used for
436  // blob normalization instead of xheight + ascrise. This flag is for
437  // those languages that are using CJK pitch model and thus it has to
438  // be true if and only if tesseract->textord_use_cjk_fp_model is
439  // true.
440  // If allow_detailed_fx is true, the feature extractor will receive fine
441  // precision outline information, allowing smoother features and better
442  // features on low resolution images.
443  // The norm_mode sets the default mode for normalization in absence
444  // of any of the above flags. It should really be a tesseract::OcrEngineMode
445  // but is declared as int for ease of use with tessedit_ocr_engine_mode.
446  // Returns false if the word is empty and sets up fake results.
447  bool SetupForRecognition(const UNICHARSET& unicharset_in,
448  tesseract::Tesseract* tesseract, Pix* pix,
449  int norm_mode,
450  const TBOX* norm_box, bool numeric_mode,
451  bool use_body_size, bool allow_detailed_fx,
452  ROW *row, const BLOCK* block);
453 
454  // Set up the seam array, bln_boxes, best_choice, and raw_choice to empty
455  // accumulators from a made chopped word. We presume the fields are already
456  // empty.
457  void SetupBasicsFromChoppedWord(const UNICHARSET &unicharset_in);
458 
459  // Sets up the members used in recognition for an empty recognition result:
460  // bln_boxes, chopped_word, seam_array, denorm, best_choice, raw_choice.
461  void SetupFake(const UNICHARSET& uch);
462 
463  // Set the word as having the script of the input unicharset.
464  void SetupWordScript(const UNICHARSET& unicharset_in);
465 
466  // Sets up the blamer_bundle if it is not null, using the initialized denorm.
467  void SetupBlamerBundle();
468 
469  // Computes the blob_widths and blob_gaps from the chopped_word.
470  void SetupBlobWidthsAndGaps();
471 
472  // Updates internal data to account for a new SEAM (chop) at the given
473  // blob_number. Fixes the ratings matrix and states in the choices, as well
474  // as the blob widths and gaps.
475  void InsertSeam(int blob_number, SEAM* seam);
476 
477  // Returns true if all the word choices except the first have adjust_factors
478  // worse than the given threshold.
479  bool AlternativeChoiceAdjustmentsWorseThan(float threshold) const;
480 
481  // Returns true if the current word is ambiguous (by number of answers or
482  // by dangerous ambigs.)
483  bool IsAmbiguous();
484 
485  // Returns true if the ratings matrix size matches the sum of each of the
486  // segmentation states.
487  bool StatesAllValid();
488 
489  // Prints a list of words found if debug is true or the word result matches
490  // the word_to_debug.
491  void DebugWordChoices(bool debug, const char* word_to_debug);
492 
493  // Prints the top choice along with the accepted/done flags.
494  void DebugTopChoice(const char* msg) const;
495 
496  // Removes from best_choices all choices which are not within a reasonable
497  // range of the best choice.
498  void FilterWordChoices(int debug_level);
499 
500  // Computes a set of distance thresholds used to control adaption.
501  // Compares the best choice for the current word to the best raw choice
502  // to determine which characters were classified incorrectly by the
503  // classifier. Then places a separate threshold into thresholds for each
504  // character in the word. If the classifier was correct, max_rating is placed
505  // into thresholds. If the classifier was incorrect, the mean match rating
506  // (error percentage) of the classifier's incorrect choice minus some margin
507  // is placed into thresholds. This can then be used by the caller to try to
508  // create a new template for the desired class that will classify the
509  // character with a rating better than the threshold value. The match rating
510  // placed into thresholds is never allowed to be below min_rating in order to
511  // prevent trying to make overly tight templates.
512  // min_rating limits how tight to make a template.
513  // max_rating limits how loose to make a template.
514  // rating_margin denotes the amount of margin to put in template.
515  void ComputeAdaptionThresholds(float certainty_scale,
516  float min_rating,
517  float max_rating,
518  float rating_margin,
519  float* thresholds);
520 
521  // Saves a copy of the word_choice if it has the best unadjusted rating.
522  // Returns true if the word_choice was the new best.
523  bool LogNewRawChoice(WERD_CHOICE* word_choice);
524  // Consumes word_choice by adding it to best_choices, (taking ownership) if
525  // the certainty for word_choice is some distance of the best choice in
526  // best_choices, or by deleting the word_choice and returning false.
527  // The best_choices list is kept in sorted order by rating. Duplicates are
528  // removed, and the list is kept no longer than max_num_choices in length.
529  // Returns true if the word_choice is still a valid pointer.
530  bool LogNewCookedChoice(int max_num_choices, bool debug,
531  WERD_CHOICE* word_choice);
532 
533  // Prints a brief list of all the best choices.
534  void PrintBestChoices() const;
535 
536  // Returns the sum of the widths of the blob between start_blob and last_blob
537  // inclusive.
538  int GetBlobsWidth(int start_blob, int last_blob);
539  // Returns the width of a gap between the specified blob and the next one.
540  int GetBlobsGap(int blob_index);
541 
542  // Returns the BLOB_CHOICE corresponding to the given index in the
543  // best choice word taken from the appropriate cell in the ratings MATRIX.
544  // Borrowed pointer, so do not delete. May return NULL if there is no
545  // BLOB_CHOICE matching the unichar_id at the given index.
546  BLOB_CHOICE* GetBlobChoice(int index) const;
547 
548  // Returns the BLOB_CHOICE_LIST corresponding to the given index in the
549  // best choice word taken from the appropriate cell in the ratings MATRIX.
550  // Borrowed pointer, so do not delete.
551  BLOB_CHOICE_LIST* GetBlobChoices(int index) const;
552 
553  // Moves the results fields from word to this. This takes ownership of all
554  // the data, so src can be destructed.
555  // word1.ConsumeWordResult(word);
556  // delete word;
557  // is simpler and faster than:
558  // word1 = *word;
559  // delete word;
560  // as it doesn't need to copy and reallocate anything.
561  void ConsumeWordResults(WERD_RES* word);
562 
563  // Replace the best choice and rebuild box word.
564  // choice must be from the current best_choices list.
565  void ReplaceBestChoice(WERD_CHOICE* choice);
566 
567  // Builds the rebuild_word and sets the best_state from the chopped_word and
568  // the best_choice->state.
569  void RebuildBestState();
570 
571  // Copies the chopped_word to the rebuild_word, faking a best_state as well.
572  // Also sets up the output box_word.
573  void CloneChoppedToRebuild();
574 
575  // Sets/replaces the box_word with one made from the rebuild_word.
576  void SetupBoxWord();
577 
578  // Sets up the script positions in the best_choice using the best_choice
579  // to get the unichars, and the unicharset to get the target positions.
580  void SetScriptPositions();
581  // Sets all the blobs in all the words (best choice and alternates) to be
582  // the given position. (When a sub/superscript is recognized as a separate
583  // word, it falls victim to the rule that a whole word cannot be sub or
584  // superscript, so this function overrides that problem.)
585  void SetAllScriptPositions(tesseract::ScriptPos position);
586 
587  // Classifies the word with some already-calculated BLOB_CHOICEs.
588  // The choices are an array of blob_count pointers to BLOB_CHOICE,
589  // providing a single classifier result for each blob.
590  // The BLOB_CHOICEs are consumed and the word takes ownership.
591  // The number of blobs in the box_word must match blob_count.
592  void FakeClassifyWord(int blob_count, BLOB_CHOICE** choices);
593 
594  // Creates a WERD_CHOICE for the word using the top choices from the leading
595  // diagonal of the ratings matrix.
596  void FakeWordFromRatings(PermuterType permuter);
597 
598  // Copies the best_choice strings to the correct_text for adaption/training.
599  void BestChoiceToCorrectText();
600 
601  // Merges 2 adjacent blobs in the result if the permanent callback
602  // class_cb returns other than INVALID_UNICHAR_ID, AND the permanent
603  // callback box_cb is NULL or returns true, setting the merged blob
604  // result to the class returned from class_cb.
605  // Returns true if anything was merged.
606  bool ConditionalBlobMerge(
609 
610  // Merges 2 adjacent blobs in the result (index and index+1) and corrects
611  // all the data to account for the change.
612  void MergeAdjacentBlobs(int index);
613 
614  // Callback helper for fix_quotes returns a double quote if both
615  // arguments are quote, otherwise INVALID_UNICHAR_ID.
616  UNICHAR_ID BothQuotes(UNICHAR_ID id1, UNICHAR_ID id2);
617  void fix_quotes();
618 
619  // Callback helper for fix_hyphens returns UNICHAR_ID of - if both
620  // arguments are hyphen, otherwise INVALID_UNICHAR_ID.
621  UNICHAR_ID BothHyphens(UNICHAR_ID id1, UNICHAR_ID id2);
622  // Callback helper for fix_hyphens returns true if box1 and box2 overlap
623  // (assuming both on the same textline, are in order and a chopped em dash.)
624  bool HyphenBoxesOverlap(const TBOX& box1, const TBOX& box2);
625  void fix_hyphens();
626 
627  // Callback helper for merge_tess_fails returns a space if both
628  // arguments are space, otherwise INVALID_UNICHAR_ID.
629  UNICHAR_ID BothSpaces(UNICHAR_ID id1, UNICHAR_ID id2);
630  void merge_tess_fails();
631 
632  // Returns a really deep copy of *src, including the ratings MATRIX.
633  static WERD_RES* deep_copy(const WERD_RES* src) {
634  WERD_RES* result = new WERD_RES(*src);
635  // That didn't copy the ratings, but we want a copy if there is one to
636  // begin with.
637  if (src->ratings != NULL)
638  result->ratings = src->ratings->DeepCopy();
639  return result;
640  }
641 
642  // Copy blobs from word_res onto this word (eliminating spaces between).
643  // Since this may be called bidirectionally OR both the BOL and EOL flags.
644  void copy_on(WERD_RES *word_res) { //from this word
645  word->set_flag(W_BOL, word->flag(W_BOL) || word_res->word->flag(W_BOL));
646  word->set_flag(W_EOL, word->flag(W_EOL) || word_res->word->flag(W_EOL));
647  word->copy_on(word_res->word);
648  }
649 
650  // Returns true if the collection of count pieces, starting at start, are all
651  // natural connected components, ie there are no real chops involved.
652  bool PiecesAllNatural(int start, int count) const;
653 };
654 
655 /*************************************************************************
656  * PAGE_RES_IT - Page results iterator
657  *************************************************************************/
658 
659 class PAGE_RES_IT {
660  public:
661  PAGE_RES * page_res; // page being iterated
662 
664  } // empty contructor
665 
666  PAGE_RES_IT(PAGE_RES *the_page_res) { // page result
667  page_res = the_page_res;
668  restart_page(); // ready to scan
669  }
670 
671  // Do two PAGE_RES_ITs point at the same word?
672  // This is much cheaper than cmp().
673  bool operator ==(const PAGE_RES_IT &other) const;
674 
675  bool operator !=(const PAGE_RES_IT &other) const {return !(*this == other); }
676 
677  // Given another PAGE_RES_IT to the same page,
678  // this before other: -1
679  // this equal to other: 0
680  // this later than other: 1
681  int cmp(const PAGE_RES_IT &other) const;
682 
684  return start_page(false); // Skip empty blocks.
685  }
687  return start_page(true); // Allow empty blocks.
688  }
689  WERD_RES *start_page(bool empty_ok);
690 
691  WERD_RES *restart_row();
692 
693  // ============ Methods that mutate the underling structures ===========
694  // Note that these methods will potentially invalidate other PAGE_RES_ITs
695  // and are intended to be used only while a single PAGE_RES_IT is active.
696  // This problem needs to be taken into account if these mutation operators
697  // are ever provided to PageIterator or its subclasses.
698 
699  // Inserts the new_word and a corresponding WERD_RES before the current
700  // position. The simple fields of the WERD_RES are copied from clone_res and
701  // the resulting WERD_RES is returned for further setup with best_choice etc.
702  WERD_RES* InsertSimpleCloneWord(const WERD_RES& clone_res, WERD* new_word);
703 
704  // Replaces the current WERD/WERD_RES with the given words. The given words
705  // contain fake blobs that indicate the position of the characters. These are
706  // replaced with real blobs from the current word as much as possible.
707  void ReplaceCurrentWord(tesseract::PointerVector<WERD_RES>* words);
708 
709  // Deletes the current WERD_RES and its underlying WERD.
710  void DeleteCurrentWord();
711 
712  // Makes the current word a fuzzy space if not already fuzzy. Updates
713  // corresponding part of combo if required.
714  void MakeCurrentWordFuzzy();
715 
716  WERD_RES *forward() { // Get next word.
717  return internal_forward(false, false);
718  }
719  // Move forward, but allow empty blocks to show as single NULL words.
721  return internal_forward(false, true);
722  }
723 
724  WERD_RES *forward_paragraph(); // get first word in next non-empty paragraph
725  WERD_RES *forward_block(); // get first word in next non-empty block
726 
727  WERD_RES *prev_word() const { // previous word
728  return prev_word_res;
729  }
730  ROW_RES *prev_row() const { // row of prev word
731  return prev_row_res;
732  }
733  BLOCK_RES *prev_block() const { // block of prev word
734  return prev_block_res;
735  }
736  WERD_RES *word() const { // current word
737  return word_res;
738  }
739  ROW_RES *row() const { // row of current word
740  return row_res;
741  }
742  BLOCK_RES *block() const { // block of cur. word
743  return block_res;
744  }
745  WERD_RES *next_word() const { // next word
746  return next_word_res;
747  }
748  ROW_RES *next_row() const { // row of next word
749  return next_row_res;
750  }
751  BLOCK_RES *next_block() const { // block of next word
752  return next_block_res;
753  }
754  void rej_stat_word(); // for page/block/row
755  void ResetWordIterator();
756 
757  private:
758  WERD_RES *internal_forward(bool new_block, bool empty_ok);
759 
760  WERD_RES * prev_word_res; // previous word
761  ROW_RES *prev_row_res; // row of prev word
762  BLOCK_RES *prev_block_res; // block of prev word
763 
764  WERD_RES *word_res; // current word
765  ROW_RES *row_res; // row of current word
766  BLOCK_RES *block_res; // block of cur. word
767 
768  WERD_RES *next_word_res; // next word
769  ROW_RES *next_row_res; // row of next word
770  BLOCK_RES *next_block_res; // block of next word
771 
772  BLOCK_RES_IT block_res_it; // iterators
773  ROW_RES_IT row_res_it;
774  WERD_RES_IT word_res_it;
775 };
776 #endif
ROW_RES()
Definition: pageres.h:133
ROW_RES_LIST row_res_list
Definition: pageres.h:110
inT32 rej_count
Definition: pageres.h:129
BOOL8 font_assigned
Definition: pageres.h:105
inT32 char_count
Definition: pageres.h:60
WERD_RES * next_word() const
Definition: pageres.h:745
UNICHAR_ID unichar_id(int index) const
Definition: ratngs.h:313
int32_t inT32
Definition: host.h:38
inT16 row_count
Definition: pageres.h:103
float x_height
Definition: pageres.h:104
void init_to_size(int size, T t)
int UNICHAR_ID
Definition: unichar.h:33
float baseline_shift
Definition: pageres.h:297
BOOL8 tess_failed
Definition: pageres.h:272
GenericVector< int > best_state
Definition: pageres.h:255
const FontInfo * fontinfo2
Definition: pageres.h:289
WERD_CHOICE * best_choice
Definition: pageres.h:219
int length() const
Definition: ratngs.h:301
inT8 bold
Definition: pageres.h:286
ROW_RES * prev_row() const
Definition: pageres.h:730
BOOL8 guessed_caps_ht
Definition: pageres.h:293
BlamerBundle * blamer_bundle
Definition: pageres.h:230
bool AnyRtlCharsInWord() const
Definition: pageres.h:375
BLOCK_RES_LIST block_res_list
Definition: pageres.h:62
Definition: werd.h:36
GenericVector< STRING > correct_text
Definition: pageres.h:259
inT32 rej_count
Definition: pageres.h:61
inT16 font_class
Definition: pageres.h:102
PAGE_RES_IT(PAGE_RES *the_page_res)
Definition: pageres.h:666
TWERD * rebuild_word
Definition: pageres.h:244
BLOCK * block
Definition: pageres.h:99
inT8 italic
Definition: pageres.h:285
PAGE_RES_IT()
Definition: pageres.h:663
ROW * row
Definition: pageres.h:127
GenericVector< int > blob_widths
Definition: pageres.h:205
WERD_RES(WERD *the_word)
Definition: pageres.h:326
tesseract::Tesseract * tesseract
Definition: pageres.h:266
int16_t inT16
Definition: host.h:36
tesseract::BoxWord * box_word
Definition: pageres.h:250
BOOL8 guessed_x_ht
Definition: pageres.h:292
Direction get_direction(UNICHAR_ID unichar_id) const
Definition: unicharset.h:650
ROW_RES * row() const
Definition: pageres.h:739
BOOL8 flag(WERD_FLAGS mask) const
Definition: werd.h:128
const char * id_to_unichar(UNICHAR_ID id) const
Definition: unicharset.cpp:266
BOOL8 reject_spaces
Definition: pageres.h:320
WERD_RES * forward_with_empties()
Definition: pageres.h:720
ROW_RES * next_row() const
Definition: pageres.h:748
MATRIX * ratings
Definition: pageres.h:215
Definition: blobs.h:395
void copy_on(WERD_RES *word_res)
Definition: pageres.h:644
WERD_RES * restart_page()
Definition: pageres.h:683
~PAGE_RES()
Definition: pageres.h:89
WERD_CHOICE ** prev_word_best_choice
Definition: pageres.h:66
WERD_RES * restart_page_with_empties()
Definition: pageres.h:686
BOOL8 combination
Definition: pageres.h:318
const char * id_to_unichar_ext(UNICHAR_ID id) const
Definition: unicharset.cpp:274
inT32 whole_word_rej_count
Definition: pageres.h:130
PAGE_RES * page_res
Definition: pageres.h:661
CRUNCH_MODE
Definition: pageres.h:145
WERD_RES * forward()
Definition: pageres.h:716
inT32 rej_count
Definition: pageres.h:101
BOOL8 rejected
Definition: pageres.h:63
Definition: seam.h:44
unsigned char BOOL8
Definition: host.h:44
BOOL8 italic
Definition: pageres.h:108
float caps_height
Definition: pageres.h:296
WERD_CHOICE_LIST best_choices
Definition: pageres.h:227
#define FALSE
Definition: capi.h:46
UNICHAR_ID get_mirror(UNICHAR_ID unichar_id) const
Definition: unicharset.h:657
const FontInfo * fontinfo
Definition: pageres.h:288
~ROW_RES()
Definition: pageres.h:138
inT32 char_count
Definition: pageres.h:128
WERD_CHOICE * raw_choice
Definition: pageres.h:224
BOOL8 tess_would_adapt
Definition: pageres.h:281
BOOL8 tess_accepted
Definition: pageres.h:280
bool odd_size
Definition: pageres.h:284
bool UnicharsInReadingOrder() const
Definition: pageres.h:409
Definition: werd.h:35
BOOL8 part_of_combo
Definition: pageres.h:319
inT8 fontinfo_id2_count
Definition: pageres.h:291
CRUNCH_MODE unlv_crunch_mode
Definition: pageres.h:294
WERD_RES()
Definition: pageres.h:322
BLOCK_RES * next_block() const
Definition: pageres.h:751
const char * RawUTF8(int blob_index) const
Definition: pageres.h:358
Definition: rect.h:30
int8_t inT8
Definition: host.h:34
tesseract::BoxWord * bln_boxes
Definition: pageres.h:184
Definition: matrix.h:563
DENORM denorm
Definition: pageres.h:190
WERD_RES(const WERD_RES &source)
Definition: pageres.h:333
inT8 fontinfo_id_count
Definition: pageres.h:290
bool small_caps
Definition: pageres.h:283
WERD * word
Definition: pageres.h:175
const char * BestUTF8(int blob_index, bool in_rtl_context) const
Definition: pageres.h:345
BOOL8 bold
Definition: pageres.h:107
WERD_CHOICE * ep_choice
Definition: pageres.h:270
int size() const
Definition: unicharset.h:299
ELISTIZEH(AmbigSpec)
WERD_RES * word() const
Definition: pageres.h:736
GenericVector< int > blob_gaps
Definition: pageres.h:208
MATRIX * DeepCopy() const
Definition: matrix.cpp:94
WERD_RES * prev_word() const
Definition: pageres.h:727
UNICHARSET::Direction SymbolDirection(int blob_index) const
Definition: pageres.h:367
const UNICHARSET * uch_set
Definition: pageres.h:192
WERD_RES_LIST word_res_list
Definition: pageres.h:131
BLOCK_RES * prev_block() const
Definition: pageres.h:733
void Init()
Definition: pageres.h:75
bool unichars_in_script_order() const
Definition: ratngs.h:533
#define CLISTIZEH(CLASSNAME)
Definition: clst.h:901
float space_certainty
Definition: pageres.h:300
inT32 char_count
Definition: pageres.h:100
Definition: werd.h:60
bool AnyLtrCharsInWord() const
Definition: pageres.h:392
TWERD * chopped_word
Definition: pageres.h:201
REJMAP reject_map
Definition: pageres.h:271
GenericVector< STRING > misadaption_log
Definition: pageres.h:73
Definition: ocrrow.h:32
int count(LIST var_list)
Definition: oldlist.cpp:103
GenericVector< int > blame_reasons
Definition: pageres.h:68
float x_height
Definition: pageres.h:295
Definition: ocrblock.h:30
PermuterType
Definition: ratngs.h:240
ROW * blob_row
Definition: pageres.h:186
static WERD_RES * deep_copy(const WERD_RES *src)
Definition: pageres.h:633
~BLOCK_RES()
Definition: pageres.h:117
BLOCK_RES * block() const
Definition: pageres.h:742
BOOL8 done
Definition: pageres.h:282
GenericVector< SEAM * > seam_array
Definition: pageres.h:203
BLOCK_RES()
Definition: pageres.h:112
PAGE_RES()
Definition: pageres.h:83