tesseract  3.04.00
tesseractclass.h
Go to the documentation of this file.
1 // File: tesseractclass.h
3 // Description: The Tesseract class. It holds/owns everything needed
4 // to run Tesseract on a single language, and also a set of
5 // sub-Tesseracts to run sub-languages. For thread safety, *every*
6 // global variable goes in here, directly, or indirectly.
7 // This makes it safe to run multiple Tesseracts in different
8 // threads in parallel, and keeps the different language
9 // instances separate.
10 // Author: Ray Smith
11 // Created: Fri Mar 07 08:17:01 PST 2008
12 //
13 // (C) Copyright 2008, Google Inc.
14 // Licensed under the Apache License, Version 2.0 (the "License");
15 // you may not use this file except in compliance with the License.
16 // You may obtain a copy of the License at
17 // http://www.apache.org/licenses/LICENSE-2.0
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the License is distributed on an "AS IS" BASIS,
20 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 // See the License for the specific language governing permissions and
22 // limitations under the License.
23 //
25 
26 #ifndef TESSERACT_CCMAIN_TESSERACTCLASS_H__
27 #define TESSERACT_CCMAIN_TESSERACTCLASS_H__
28 
29 #include "allheaders.h"
30 #include "control.h"
31 #include "docqual.h"
32 #include "devanagari_processing.h"
33 #include "genericvector.h"
34 #include "params.h"
35 #include "ocrclass.h"
36 #include "textord.h"
37 #include "wordrec.h"
38 
39 class BLOB_CHOICE_LIST_CLIST;
40 class BLOCK_LIST;
41 class CharSamp;
42 struct OSResults;
43 class PAGE_RES;
44 class PAGE_RES_IT;
45 struct Pix;
46 class ROW;
47 class SVMenuNode;
48 class TBOX;
49 class TO_BLOCK_LIST;
50 class WERD;
51 class WERD_CHOICE;
52 class WERD_RES;
53 
54 
55 // Top-level class for all tesseract global instance data.
56 // This class either holds or points to all data used by an instance
57 // of Tesseract, including the memory allocator. When this is
58 // complete, Tesseract will be thread-safe. UNTIL THEN, IT IS NOT!
59 //
60 // NOTE to developers: Do not create cyclic dependencies through this class!
61 // The directory dependency tree must remain a tree! The keep this clean,
62 // lower-level code (eg in ccutil, the bottom level) must never need to
63 // know about the content of a higher-level directory.
64 // The following scheme will grant the easiest access to lower-level
65 // global members without creating a cyclic dependency:
66 //
67 // Class Hierarchy (^ = inheritance):
68 //
69 // CCUtil (ccutil/ccutil.h)
70 // ^ Members include: UNICHARSET
71 // CUtil (cutil/cutil_class.h)
72 // ^ Members include: TBLOB*, TEXTBLOCK*
73 // CCStruct (ccstruct/ccstruct.h)
74 // ^ Members include: Image
75 // Classify (classify/classify.h)
76 // ^ Members include: Dict
77 // WordRec (wordrec/wordrec.h)
78 // ^ Members include: WERD*, DENORM*
79 // Tesseract (ccmain/tesseractclass.h)
80 // Members include: Pix*, CubeRecoContext*,
81 // TesseractCubeCombiner*
82 //
83 // Other important classes:
84 //
85 // TessBaseAPI (api/baseapi.h)
86 // Members include: BLOCK_LIST*, PAGE_RES*,
87 // Tesseract*, ImageThresholder*
88 // Dict (dict/dict.h)
89 // Members include: Image* (private)
90 //
91 // NOTE: that each level contains members that correspond to global
92 // data that is defined (and used) at that level, not necessarily where
93 // the type is defined so for instance:
94 // BOOL_VAR_H(textord_show_blobs, false, "Display unsorted blobs");
95 // goes inside the Textord class, not the cc_util class.
96 
97 namespace tesseract {
98 
99 class ColumnFinder;
100 #ifndef ANDROID_BUILD
101 class CubeLineObject;
102 class CubeObject;
103 class CubeRecoContext;
104 #endif
105 class EquationDetect;
106 class Tesseract;
107 #ifndef ANDROID_BUILD
108 class TesseractCubeCombiner;
109 #endif
110 
111 // A collection of various variables for statistics and debugging.
115  doc_blob_quality(0),
116  doc_outline_errs(0),
117  doc_char_quality(0),
118  good_char_count(0),
120  word_count(0),
121  dict_words(0),
122  tilde_crunch_written(false),
123  last_char_was_newline(true),
124  last_char_was_tilde(false),
126 
133  inT32 word_count; // count of word in the document
134  inT32 dict_words; // number of dicitionary words in the document
135  STRING dump_words_str; // accumulator used by dump_words()
136  // Flags used by write_results()
141 };
142 
143 // Struct to hold all the pointers to relevant data for processing a word.
144 struct WordData {
145  WordData() : word(NULL), row(NULL), block(NULL), prev_word(NULL) {}
146  explicit WordData(const PAGE_RES_IT& page_res_it)
147  : word(page_res_it.word()), row(page_res_it.row()->row),
148  block(page_res_it.block()->block), prev_word(NULL) {}
149  WordData(BLOCK* block_in, ROW* row_in, WERD_RES* word_res)
150  : word(word_res), row(row_in), block(block_in), prev_word(NULL) {}
151 
157 };
158 
159 // Definition of a Tesseract WordRecognizer. The WordData provides the context
160 // of row/block, in_word holds an initialized, possibly pre-classified word,
161 // that the recognizer may or may not consume (but if so it sets *in_word=NULL)
162 // and produces one or more output words in out_words, which may be the
163 // consumed in_word, or may be generated independently.
164 // This api allows both a conventional tesseract classifier to work, or a
165 // line-level classifier that generates multiple words from a merged input.
166 typedef void (Tesseract::*WordRecognizer)(const WordData& word_data,
167  WERD_RES** in_word,
168  PointerVector<WERD_RES>* out_words);
169 
170 class Tesseract : public Wordrec {
171  public:
172  Tesseract();
173  ~Tesseract();
174 
175  // Clear as much used memory as possible without resetting the adaptive
176  // classifier or losing any other classifier data.
177  void Clear();
178  // Clear all memory of adaption for this and all subclassifiers.
179  void ResetAdaptiveClassifier();
180  // Clear the document dictionary for this and all subclassifiers.
181  void ResetDocumentDictionary();
182 
183  // Set the equation detector.
184  void SetEquationDetect(EquationDetect* detector);
185 
186  // Simple accessors.
187  const FCOORD& reskew() const {
188  return reskew_;
189  }
190  // Destroy any existing pix and return a pointer to the pointer.
192  Clear();
193  return &pix_binary_;
194  }
195  Pix* pix_binary() const {
196  return pix_binary_;
197  }
198  Pix* pix_grey() const {
199  return pix_grey_;
200  }
201  void set_pix_grey(Pix* grey_pix) {
202  pixDestroy(&pix_grey_);
203  pix_grey_ = grey_pix;
204  }
205  // Returns a pointer to a Pix representing the best available image of the
206  // page. The image will be 8-bit grey if the input was grey or color. Note
207  // that in grey 0 is black and 255 is white. If the input was binary, then
208  // the returned Pix will be binary. Note that here black is 1 and white is 0.
209  // To tell the difference pixGetDepth() will return 8 or 1.
210  // In either case, the return value is a borrowed Pix, and should not be
211  // deleted or pixDestroyed.
212  Pix* BestPix() const {
213  return pix_grey_ != NULL ? pix_grey_ : pix_binary_;
214  }
215  void set_pix_thresholds(Pix* thresholds) {
216  pixDestroy(&pix_thresholds_);
217  pix_thresholds_ = thresholds;
218  }
219  int source_resolution() const {
220  return source_resolution_;
221  }
222  void set_source_resolution(int ppi) {
223  source_resolution_ = ppi;
224  }
225  int ImageWidth() const {
226  return pixGetWidth(pix_binary_);
227  }
228  int ImageHeight() const {
229  return pixGetHeight(pix_binary_);
230  }
231  Pix* scaled_color() const {
232  return scaled_color_;
233  }
234  int scaled_factor() const {
235  return scaled_factor_;
236  }
237  void SetScaledColor(int factor, Pix* color) {
238  scaled_factor_ = factor;
239  scaled_color_ = color;
240  }
241  const Textord& textord() const {
242  return textord_;
243  }
245  return &textord_;
246  }
247 
248  bool right_to_left() const {
249  return right_to_left_;
250  }
251  int num_sub_langs() const {
252  return sub_langs_.size();
253  }
254  Tesseract* get_sub_lang(int index) const {
255  return sub_langs_[index];
256  }
257  // Returns true if any language uses Tesseract (as opposed to cube).
258  bool AnyTessLang() const {
259  if (tessedit_ocr_engine_mode != OEM_CUBE_ONLY) return true;
260  for (int i = 0; i < sub_langs_.size(); ++i) {
261  if (sub_langs_[i]->tessedit_ocr_engine_mode != OEM_CUBE_ONLY)
262  return true;
263  }
264  return false;
265  }
266 
267  void SetBlackAndWhitelist();
268 
269  // Perform steps to prepare underlying binary image/other data structures for
270  // page segmentation. Uses the strategy specified in the global variable
271  // pageseg_devanagari_split_strategy for perform splitting while preparing for
272  // page segmentation.
273  void PrepareForPageseg();
274 
275  // Perform steps to prepare underlying binary image/other data structures for
276  // Tesseract OCR. The current segmentation is required by this method.
277  // Uses the strategy specified in the global variable
278  // ocr_devanagari_split_strategy for performing splitting while preparing for
279  // Tesseract ocr.
280  void PrepareForTessOCR(BLOCK_LIST* block_list,
281  Tesseract* osd_tess, OSResults* osr);
282 
283  int SegmentPage(const STRING* input_file, BLOCK_LIST* blocks,
284  Tesseract* osd_tess, OSResults* osr);
285  void SetupWordScripts(BLOCK_LIST* blocks);
286  int AutoPageSeg(PageSegMode pageseg_mode, BLOCK_LIST* blocks,
287  TO_BLOCK_LIST* to_blocks, BLOBNBOX_LIST* diacritic_blobs,
288  Tesseract* osd_tess, OSResults* osr);
289  ColumnFinder* SetupPageSegAndDetectOrientation(
290  PageSegMode pageseg_mode, BLOCK_LIST* blocks, Tesseract* osd_tess,
291  OSResults* osr, TO_BLOCK_LIST* to_blocks, Pix** photo_mask_pix,
292  Pix** music_mask_pix);
293  // par_control.cpp
294  void PrerecAllWordsPar(const GenericVector<WordData>& words);
295 
297  bool ProcessTargetWord(const TBOX& word_box, const TBOX& target_word_box,
298  const char* word_config, int pass);
299  // Sets up the words ready for whichever engine is to be run
300  void SetupAllWordsPassN(int pass_n,
301  const TBOX* target_word_box,
302  const char* word_config,
303  PAGE_RES* page_res,
304  GenericVector<WordData>* words);
305  // Sets up the single word ready for whichever engine is to be run.
306  void SetupWordPassN(int pass_n, WordData* word);
307  // Runs word recognition on all the words.
308  bool RecogAllWordsPassN(int pass_n, ETEXT_DESC* monitor,
309  PAGE_RES_IT* pr_it,
310  GenericVector<WordData>* words);
311  bool recog_all_words(PAGE_RES* page_res,
312  ETEXT_DESC* monitor,
313  const TBOX* target_word_box,
314  const char* word_config,
315  int dopasses);
316  void rejection_passes(PAGE_RES* page_res,
317  ETEXT_DESC* monitor,
318  const TBOX* target_word_box,
319  const char* word_config);
320  void bigram_correction_pass(PAGE_RES *page_res);
321  void blamer_pass(PAGE_RES* page_res);
322  // Sets script positions and detects smallcaps on all output words.
323  void script_pos_pass(PAGE_RES* page_res);
324  // Helper to recognize the word using the given (language-specific) tesseract.
325  // Returns positive if this recognizer found more new best words than the
326  // number kept from best_words.
327  int RetryWithLanguage(const WordData& word_data,
328  WordRecognizer recognizer,
329  WERD_RES** in_word,
330  PointerVector<WERD_RES>* best_words);
331  // Moves good-looking "noise"/diacritics from the reject list to the main
332  // blob list on the current word. Returns true if anything was done, and
333  // sets make_next_word_fuzzy if blob(s) were added to the end of the word.
334  bool ReassignDiacritics(int pass, PAGE_RES_IT* pr_it,
335  bool* make_next_word_fuzzy);
336  // Attempts to put noise/diacritic outlines into the blobs that they overlap.
337  // Input: a set of noisy outlines that probably belong to the real_word.
338  // Output: outlines that overlapped blobs are set to NULL and put back into
339  // the word, either in the blobs or in the reject list.
340  void AssignDiacriticsToOverlappingBlobs(
341  const GenericVector<C_OUTLINE*>& outlines, int pass, WERD* real_word,
342  PAGE_RES_IT* pr_it, GenericVector<bool>* word_wanted,
343  GenericVector<bool>* overlapped_any_blob,
344  GenericVector<C_BLOB*>* target_blobs);
345  // Attempts to assign non-overlapping outlines to their nearest blobs or
346  // make new blobs out of them.
347  void AssignDiacriticsToNewBlobs(const GenericVector<C_OUTLINE*>& outlines,
348  int pass, WERD* real_word, PAGE_RES_IT* pr_it,
349  GenericVector<bool>* word_wanted,
350  GenericVector<C_BLOB*>* target_blobs);
351  // Starting with ok_outlines set to indicate which outlines overlap the blob,
352  // chooses the optimal set (approximately) and returns true if any outlines
353  // are desired, in which case ok_outlines indicates which ones.
354  bool SelectGoodDiacriticOutlines(int pass, float certainty_threshold,
355  PAGE_RES_IT* pr_it, C_BLOB* blob,
356  const GenericVector<C_OUTLINE*>& outlines,
357  int num_outlines,
358  GenericVector<bool>* ok_outlines);
359  // Classifies the given blob plus the outlines flagged by ok_outlines, undoes
360  // the inclusion of the outlines, and returns the certainty of the raw choice.
361  float ClassifyBlobPlusOutlines(const GenericVector<bool>& ok_outlines,
362  const GenericVector<C_OUTLINE*>& outlines,
363  int pass_n, PAGE_RES_IT* pr_it, C_BLOB* blob,
364  STRING* best_str);
365  // Classifies the given blob (part of word_data->word->word) as an individual
366  // word, using languages, chopper etc, returning only the certainty of the
367  // best raw choice, and undoing all the work done to fake out the word.
368  float ClassifyBlobAsWord(int pass_n, PAGE_RES_IT* pr_it, C_BLOB* blob,
369  STRING* best_str, float* c2);
370  void classify_word_and_language(int pass_n, PAGE_RES_IT* pr_it,
371  WordData* word_data);
372  void classify_word_pass1(const WordData& word_data,
373  WERD_RES** in_word,
374  PointerVector<WERD_RES>* out_words);
375  void recog_pseudo_word(PAGE_RES* page_res, // blocks to check
376  TBOX &selection_box);
377 
378  void fix_rep_char(PAGE_RES_IT* page_res_it);
379 
380  ACCEPTABLE_WERD_TYPE acceptable_word_string(const UNICHARSET& char_set,
381  const char *s,
382  const char *lengths);
383  void match_word_pass_n(int pass_n, WERD_RES *word, ROW *row, BLOCK* block);
384  void classify_word_pass2(const WordData& word_data,
385  WERD_RES** in_word,
386  PointerVector<WERD_RES>* out_words);
387  void ReportXhtFixResult(bool accept_new_word, float new_x_ht,
388  WERD_RES* word, WERD_RES* new_word);
389  bool RunOldFixXht(WERD_RES *word, BLOCK* block, ROW *row);
390  bool TrainedXheightFix(WERD_RES *word, BLOCK* block, ROW *row);
391  // Runs recognition with the test baseline shift and x-height and returns true
392  // if there was an improvement in recognition result.
393  bool TestNewNormalization(int original_misfits, float baseline_shift,
394  float new_x_ht, WERD_RES *word, BLOCK* block,
395  ROW *row);
396  BOOL8 recog_interactive(PAGE_RES_IT* pr_it);
397 
398  // Set fonts of this word.
399  void set_word_fonts(WERD_RES *word);
400  void font_recognition_pass(PAGE_RES* page_res);
401  void dictionary_correction_pass(PAGE_RES* page_res);
402  BOOL8 check_debug_pt(WERD_RES *word, int location);
403 
405  bool SubAndSuperscriptFix(WERD_RES *word_res);
406  void GetSubAndSuperscriptCandidates(const WERD_RES *word,
407  int *num_rebuilt_leading,
408  ScriptPos *leading_pos,
409  float *leading_certainty,
410  int *num_rebuilt_trailing,
411  ScriptPos *trailing_pos,
412  float *trailing_certainty,
413  float *avg_certainty,
414  float *unlikely_threshold);
415  WERD_RES *TrySuperscriptSplits(int num_chopped_leading,
416  float leading_certainty,
417  ScriptPos leading_pos,
418  int num_chopped_trailing,
419  float trailing_certainty,
420  ScriptPos trailing_pos,
421  WERD_RES *word,
422  bool *is_good,
423  int *retry_leading,
424  int *retry_trailing);
425  bool BelievableSuperscript(bool debug,
426  const WERD_RES &word,
427  float certainty_threshold,
428  int *left_ok,
429  int *right_ok) const;
430 
432 #ifndef ANDROID_BUILD
433  bool init_cube_objects(bool load_combiner,
434  TessdataManager *tessdata_manager);
435  // Iterates through tesseract's results and calls cube on each word,
436  // combining the results with the existing tesseract result.
437  void run_cube_combiner(PAGE_RES *page_res);
438  // Recognizes a single word using (only) cube. Compatible with
439  // Tesseract's classify_word_pass1/classify_word_pass2.
440  void cube_word_pass1(BLOCK* block, ROW *row, WERD_RES *word);
441  // Cube recognizer to recognize a single word as with classify_word_pass1
442  // but also returns the cube object in case the combiner is needed.
443  CubeObject* cube_recognize_word(BLOCK* block, WERD_RES* word);
444  // Combines the cube and tesseract results for a single word, leaving the
445  // result in tess_word.
446  void cube_combine_word(CubeObject* cube_obj, WERD_RES* cube_word,
447  WERD_RES* tess_word);
448  // Call cube on the current word, and write the result to word.
449  // Sets up a fake result and returns false if something goes wrong.
450  bool cube_recognize(CubeObject *cube_obj, BLOCK* block, WERD_RES *word);
451  void fill_werd_res(const BoxWord& cube_box_word,
452  const char* cube_best_str,
453  WERD_RES* tess_werd_res);
454  bool extract_cube_state(CubeObject* cube_obj, int* num_chars,
455  Boxa** char_boxes, CharSamp*** char_samples);
456  bool create_cube_box_word(Boxa *char_boxes, int num_chars,
457  TBOX word_box, BoxWord* box_word);
458 #endif
459 
461  void output_pass(PAGE_RES_IT &page_res_it, const TBOX *target_word_box);
462  void write_results(PAGE_RES_IT &page_res_it, // full info
463  char newline_type, // type of newline
464  BOOL8 force_eol // override tilde crunch?
465  );
466  void set_unlv_suspects(WERD_RES *word);
467  UNICHAR_ID get_rep_char(WERD_RES *word); // what char is repeated?
468  BOOL8 acceptable_number_string(const char *s,
469  const char *lengths);
470  inT16 count_alphanums(const WERD_CHOICE &word);
471  inT16 count_alphas(const WERD_CHOICE &word);
473  void read_config_file(const char *filename, SetParamConstraint constraint);
474  // Initialize for potentially a set of languages defined by the language
475  // string and recursively any additional languages required by any language
476  // traineddata file (via tessedit_load_sublangs in its config) that is loaded.
477  // See init_tesseract_internal for args.
478  int init_tesseract(const char *arg0,
479  const char *textbase,
480  const char *language,
481  OcrEngineMode oem,
482  char **configs,
483  int configs_size,
484  const GenericVector<STRING> *vars_vec,
485  const GenericVector<STRING> *vars_values,
486  bool set_only_init_params);
487  int init_tesseract(const char *datapath,
488  const char *language,
489  OcrEngineMode oem) {
490  return init_tesseract(datapath, NULL, language, oem,
491  NULL, 0, NULL, NULL, false);
492  }
493  // Common initialization for a single language.
494  // arg0 is the datapath for the tessdata directory, which could be the
495  // path of the tessdata directory with no trailing /, or (if tessdata
496  // lives in the same directory as the executable, the path of the executable,
497  // hence the name arg0.
498  // textbase is an optional output file basename (used only for training)
499  // language is the language code to load.
500  // oem controls which engine(s) will operate on the image
501  // configs (argv) is an array of config filenames to load variables from.
502  // May be NULL.
503  // configs_size (argc) is the number of elements in configs.
504  // vars_vec is an optional vector of variables to set.
505  // vars_values is an optional corresponding vector of values for the variables
506  // in vars_vec.
507  // If set_only_init_params is true, then only the initialization variables
508  // will be set.
509  int init_tesseract_internal(const char *arg0,
510  const char *textbase,
511  const char *language,
512  OcrEngineMode oem,
513  char **configs,
514  int configs_size,
515  const GenericVector<STRING> *vars_vec,
516  const GenericVector<STRING> *vars_values,
517  bool set_only_init_params);
518 
519  // Set the universal_id member of each font to be unique among all
520  // instances of the same font loaded.
521  void SetupUniversalFontIds();
522 
523  int init_tesseract_lm(const char *arg0,
524  const char *textbase,
525  const char *language);
526 
527  void recognize_page(STRING& image_name);
528  void end_tesseract();
529 
530  bool init_tesseract_lang_data(const char *arg0,
531  const char *textbase,
532  const char *language,
533  OcrEngineMode oem,
534  char **configs,
535  int configs_size,
536  const GenericVector<STRING> *vars_vec,
537  const GenericVector<STRING> *vars_values,
538  bool set_only_init_params);
539 
540  void ParseLanguageString(const char* lang_str,
541  GenericVector<STRING>* to_load,
542  GenericVector<STRING>* not_to_load);
543 
545  SVMenuNode *build_menu_new();
546  #ifndef GRAPHICS_DISABLED
547  void pgeditor_main(int width, int height, PAGE_RES* page_res);
548  #endif // GRAPHICS_DISABLED
549  void process_image_event( // action in image win
550  const SVEvent &event);
551  BOOL8 process_cmd_win_event( // UI command semantics
552  inT32 cmd_event, // which menu item?
553  char *new_value // any prompt data
554  );
555  void debug_word(PAGE_RES* page_res, const TBOX &selection_box);
556  void do_re_display(
557  BOOL8 (tesseract::Tesseract::*word_painter)(PAGE_RES_IT* pr_it));
558  BOOL8 word_display(PAGE_RES_IT* pr_it);
559  BOOL8 word_bln_display(PAGE_RES_IT* pr_it);
560  BOOL8 word_blank_and_set_display(PAGE_RES_IT* pr_its);
561  BOOL8 word_set_display(PAGE_RES_IT* pr_it);
562  // #ifndef GRAPHICS_DISABLED
563  BOOL8 word_dumper(PAGE_RES_IT* pr_it);
564  // #endif // GRAPHICS_DISABLED
565  void blob_feature_display(PAGE_RES* page_res, const TBOX& selection_box);
567  // make rej map for word
568  void make_reject_map(WERD_RES *word, ROW *row, inT16 pass);
569  BOOL8 one_ell_conflict(WERD_RES *word_res, BOOL8 update_map);
570  inT16 first_alphanum_index(const char *word,
571  const char *word_lengths);
572  inT16 first_alphanum_offset(const char *word,
573  const char *word_lengths);
574  inT16 alpha_count(const char *word,
575  const char *word_lengths);
576  BOOL8 word_contains_non_1_digit(const char *word,
577  const char *word_lengths);
578  void dont_allow_1Il(WERD_RES *word);
579  inT16 count_alphanums( //how many alphanums
580  WERD_RES *word);
581  void flip_0O(WERD_RES *word);
582  BOOL8 non_0_digit(const UNICHARSET& ch_set, UNICHAR_ID unichar_id);
583  BOOL8 non_O_upper(const UNICHARSET& ch_set, UNICHAR_ID unichar_id);
584  BOOL8 repeated_nonalphanum_wd(WERD_RES *word, ROW *row);
585  void nn_match_word( //Match a word
586  WERD_RES *word,
587  ROW *row);
588  void nn_recover_rejects(WERD_RES *word, ROW *row);
589  void set_done( //set done flag
590  WERD_RES *word,
591  inT16 pass);
592  inT16 safe_dict_word(const WERD_RES *werd_res); // is best_choice in dict?
593  void flip_hyphens(WERD_RES *word);
594  void reject_I_1_L(WERD_RES *word);
595  void reject_edge_blobs(WERD_RES *word);
596  void reject_mostly_rejects(WERD_RES *word);
598  BOOL8 word_adaptable( //should we adapt?
599  WERD_RES *word,
600  uinT16 mode);
601 
603  void recog_word_recursive(WERD_RES* word);
604  void recog_word(WERD_RES *word);
605  void split_and_recog_word(WERD_RES* word);
606  void split_word(WERD_RES *word,
607  int split_pt,
608  WERD_RES **right_piece,
609  BlamerBundle **orig_blamer_bundle) const;
610  void join_words(WERD_RES *word,
611  WERD_RES *word2,
612  BlamerBundle *orig_bb) const;
614  BOOL8 digit_or_numeric_punct(WERD_RES *word, int char_position);
615  inT16 eval_word_spacing(WERD_RES_LIST &word_res_list);
616  void match_current_words(WERD_RES_LIST &words, ROW *row, BLOCK* block);
617  inT16 fp_eval_word_spacing(WERD_RES_LIST &word_res_list);
618  void fix_noisy_space_list(WERD_RES_LIST &best_perm, ROW *row, BLOCK* block);
619  void fix_fuzzy_space_list(WERD_RES_LIST &best_perm, ROW *row, BLOCK* block);
620  void fix_sp_fp_word(WERD_RES_IT &word_res_it, ROW *row, BLOCK* block);
621  void fix_fuzzy_spaces( //find fuzzy words
622  ETEXT_DESC *monitor, //progress monitor
623  inT32 word_count, //count of words in doc
624  PAGE_RES *page_res);
625  void dump_words(WERD_RES_LIST &perm, inT16 score,
626  inT16 mode, BOOL8 improved);
627  BOOL8 fixspace_thinks_word_done(WERD_RES *word);
628  inT16 worst_noise_blob(WERD_RES *word_res, float *worst_noise_score);
629  float blob_noise_score(TBLOB *blob);
630  void break_noisiest_blob_word(WERD_RES_LIST &words);
632  GARBAGE_LEVEL garbage_word(WERD_RES *word, BOOL8 ok_dict_word);
633  BOOL8 potential_word_crunch(WERD_RES *word,
634  GARBAGE_LEVEL garbage_level,
635  BOOL8 ok_dict_word);
636  void tilde_crunch(PAGE_RES_IT &page_res_it);
637  void unrej_good_quality_words( //unreject potential
638  PAGE_RES_IT &page_res_it);
639  void doc_and_block_rejection( //reject big chunks
640  PAGE_RES_IT &page_res_it,
641  BOOL8 good_quality_doc);
642  void quality_based_rejection(PAGE_RES_IT &page_res_it,
643  BOOL8 good_quality_doc);
644  void convert_bad_unlv_chs(WERD_RES *word_res);
645  void tilde_delete(PAGE_RES_IT &page_res_it);
646  inT16 word_blob_quality(WERD_RES *word, ROW *row);
647  void word_char_quality(WERD_RES *word, ROW *row, inT16 *match_count,
648  inT16 *accepted_match_count);
649  void unrej_good_chs(WERD_RES *word, ROW *row);
650  inT16 count_outline_errs(char c, inT16 outline_count);
651  inT16 word_outline_errs(WERD_RES *word);
652  BOOL8 terrible_word_crunch(WERD_RES *word, GARBAGE_LEVEL garbage_level);
653  CRUNCH_MODE word_deletable(WERD_RES *word, inT16 &delete_mode);
654  inT16 failure_count(WERD_RES *word);
655  BOOL8 noise_outlines(TWERD *word);
657  void
658  process_selected_words (
659  PAGE_RES* page_res, // blocks to check
660  //function to call
661  TBOX & selection_box,
662  BOOL8 (tesseract::Tesseract::*word_processor)(PAGE_RES_IT* pr_it));
664  void tess_add_doc_word( //test acceptability
665  WERD_CHOICE *word_choice //after context
666  );
667  void tess_segment_pass_n(int pass_n, WERD_RES *word);
668  bool tess_acceptable_word(WERD_RES *word);
669 
671  // Applies the box file based on the image name fname, and resegments
672  // the words in the block_list (page), with:
673  // blob-mode: one blob per line in the box file, words as input.
674  // word/line-mode: one blob per space-delimited unit after the #, and one word
675  // per line in the box file. (See comment above for box file format.)
676  // If find_segmentation is true, (word/line mode) then the classifier is used
677  // to re-segment words/lines to match the space-delimited truth string for
678  // each box. In this case, the input box may be for a word or even a whole
679  // text line, and the output words will contain multiple blobs corresponding
680  // to the space-delimited input string.
681  // With find_segmentation false, no classifier is needed, but the chopper
682  // can still be used to correctly segment touching characters with the help
683  // of the input boxes.
684  // In the returned PAGE_RES, the WERD_RES are setup as they would be returned
685  // from normal classification, ie. with a word, chopped_word, rebuild_word,
686  // seam_array, denorm, box_word, and best_state, but NO best_choice or
687  // raw_choice, as they would require a UNICHARSET, which we aim to avoid.
688  // Instead, the correct_text member of WERD_RES is set, and this may be later
689  // converted to a best_choice using CorrectClassifyWords. CorrectClassifyWords
690  // is not required before calling ApplyBoxTraining.
691  PAGE_RES* ApplyBoxes(const STRING& fname, bool find_segmentation,
692  BLOCK_LIST *block_list);
693 
694  // Any row xheight that is significantly different from the median is set
695  // to the median.
696  void PreenXHeights(BLOCK_LIST *block_list);
697 
698  // Builds a PAGE_RES from the block_list in the way required for ApplyBoxes:
699  // All fuzzy spaces are removed, and all the words are maximally chopped.
700  PAGE_RES* SetupApplyBoxes(const GenericVector<TBOX>& boxes,
701  BLOCK_LIST *block_list);
702  // Tests the chopper by exhaustively running chop_one_blob.
703  // The word_res will contain filled chopped_word, seam_array, denorm,
704  // box_word and best_state for the maximally chopped word.
705  void MaximallyChopWord(const GenericVector<TBOX>& boxes,
706  BLOCK* block, ROW* row, WERD_RES* word_res);
707  // Gather consecutive blobs that match the given box into the best_state
708  // and corresponding correct_text.
709  // Fights over which box owns which blobs are settled by pre-chopping and
710  // applying the blobs to box or next_box with the least non-overlap.
711  // Returns false if the box was in error, which can only be caused by
712  // failing to find an appropriate blob for a box.
713  // This means that occasionally, blobs may be incorrectly segmented if the
714  // chopper fails to find a suitable chop point.
715  bool ResegmentCharBox(PAGE_RES* page_res, const TBOX *prev_box,
716  const TBOX& box, const TBOX& next_box,
717  const char* correct_text);
718  // Consume all source blobs that strongly overlap the given box,
719  // putting them into a new word, with the correct_text label.
720  // Fights over which box owns which blobs are settled by
721  // applying the blobs to box or next_box with the least non-overlap.
722  // Returns false if the box was in error, which can only be caused by
723  // failing to find an overlapping blob for a box.
724  bool ResegmentWordBox(BLOCK_LIST *block_list,
725  const TBOX& box, const TBOX& next_box,
726  const char* correct_text);
727  // Resegments the words by running the classifier in an attempt to find the
728  // correct segmentation that produces the required string.
729  void ReSegmentByClassification(PAGE_RES* page_res);
730  // Converts the space-delimited string of utf8 text to a vector of UNICHAR_ID.
731  // Returns false if an invalid UNICHAR_ID is encountered.
732  bool ConvertStringToUnichars(const char* utf8,
733  GenericVector<UNICHAR_ID>* class_ids);
734  // Resegments the word to achieve the target_text from the classifier.
735  // Returns false if the re-segmentation fails.
736  // Uses brute-force combination of upto kMaxGroupSize adjacent blobs, and
737  // applies a full search on the classifier results to find the best classified
738  // segmentation. As a compromise to obtain better recall, 1-1 ambigiguity
739  // substitutions ARE used.
740  bool FindSegmentation(const GenericVector<UNICHAR_ID>& target_text,
741  WERD_RES* word_res);
742  // Recursive helper to find a match to the target_text (from text_index
743  // position) in the choices (from choices_pos position).
744  // Choices is an array of GenericVectors, of length choices_length, with each
745  // element representing a starting position in the word, and the
746  // GenericVector holding classification results for a sequence of consecutive
747  // blobs, with index 0 being a single blob, index 1 being 2 blobs etc.
748  void SearchForText(const GenericVector<BLOB_CHOICE_LIST*>* choices,
749  int choices_pos, int choices_length,
750  const GenericVector<UNICHAR_ID>& target_text,
751  int text_index,
752  float rating, GenericVector<int>* segmentation,
753  float* best_rating, GenericVector<int>* best_segmentation);
754  // Counts up the labelled words and the blobs within.
755  // Deletes all unused or emptied words, counting the unused ones.
756  // Resets W_BOL and W_EOL flags correctly.
757  // Builds the rebuild_word and rebuilds the box_word.
758  void TidyUp(PAGE_RES* page_res);
759  // Logs a bad box by line in the box file and box coords.
760  void ReportFailedBox(int boxfile_lineno, TBOX box, const char *box_ch,
761  const char *err_msg);
762  // Creates a fake best_choice entry in each WERD_RES with the correct text.
763  void CorrectClassifyWords(PAGE_RES* page_res);
764  // Call LearnWord to extract features for labelled blobs within each word.
765  // Features are stored in an internal buffer.
766  void ApplyBoxTraining(const STRING& fontname, PAGE_RES* page_res);
767 
769  // Returns the number of misfit blob tops in this word.
770  int CountMisfitTops(WERD_RES *word_res);
771  // Returns a new x-height in pixels (original image coords) that is
772  // maximally compatible with the result in word_res.
773  // Returns 0.0f if no x-height is found that is better than the current
774  // estimate.
775  float ComputeCompatibleXheight(WERD_RES *word_res, float* baseline_shift);
777  // TODO(ocr-team): Find and remove obsolete parameters.
778  BOOL_VAR_H(tessedit_resegment_from_boxes, false,
779  "Take segmentation and labeling from box file");
780  BOOL_VAR_H(tessedit_resegment_from_line_boxes, false,
781  "Conversion of word/line box file to char box file");
782  BOOL_VAR_H(tessedit_train_from_boxes, false,
783  "Generate training data from boxed chars");
784  BOOL_VAR_H(tessedit_make_boxes_from_boxes, false,
785  "Generate more boxes from boxed chars");
786  BOOL_VAR_H(tessedit_dump_pageseg_images, false,
787  "Dump intermediate images made during page segmentation");
788  INT_VAR_H(tessedit_pageseg_mode, PSM_SINGLE_BLOCK,
789  "Page seg mode: 0=osd only, 1=auto+osd, 2=auto, 3=col, 4=block,"
790  " 5=line, 6=word, 7=char"
791  " (Values from PageSegMode enum in publictypes.h)");
792  INT_VAR_H(tessedit_ocr_engine_mode, tesseract::OEM_TESSERACT_ONLY,
793  "Which OCR engine(s) to run (Tesseract, Cube, both). Defaults"
794  " to loading and running only Tesseract (no Cube, no combiner)."
795  " (Values from OcrEngineMode enum in tesseractclass.h)");
796  STRING_VAR_H(tessedit_char_blacklist, "",
797  "Blacklist of chars not to recognize");
798  STRING_VAR_H(tessedit_char_whitelist, "",
799  "Whitelist of chars to recognize");
800  STRING_VAR_H(tessedit_char_unblacklist, "",
801  "List of chars to override tessedit_char_blacklist");
802  BOOL_VAR_H(tessedit_ambigs_training, false,
803  "Perform training for ambiguities");
804  INT_VAR_H(pageseg_devanagari_split_strategy,
806  "Whether to use the top-line splitting process for Devanagari "
807  "documents while performing page-segmentation.");
808  INT_VAR_H(ocr_devanagari_split_strategy,
810  "Whether to use the top-line splitting process for Devanagari "
811  "documents while performing ocr.");
812  STRING_VAR_H(tessedit_write_params_to_file, "",
813  "Write all parameters to the given file.");
814  BOOL_VAR_H(tessedit_adaption_debug, false,
815  "Generate and print debug information for adaption");
816  INT_VAR_H(bidi_debug, 0, "Debug level for BiDi");
817  INT_VAR_H(applybox_debug, 1, "Debug level");
818  INT_VAR_H(applybox_page, 0, "Page number to apply boxes from");
819  STRING_VAR_H(applybox_exposure_pattern, ".exp",
820  "Exposure value follows this pattern in the image"
821  " filename. The name of the image files are expected"
822  " to be in the form [lang].[fontname].exp[num].tif");
823  BOOL_VAR_H(applybox_learn_chars_and_char_frags_mode, false,
824  "Learn both character fragments (as is done in the"
825  " special low exposure mode) as well as unfragmented"
826  " characters.");
827  BOOL_VAR_H(applybox_learn_ngrams_mode, false,
828  "Each bounding box is assumed to contain ngrams. Only"
829  " learn the ngrams whose outlines overlap horizontally.");
830  BOOL_VAR_H(tessedit_display_outwords, false, "Draw output words");
831  BOOL_VAR_H(tessedit_dump_choices, false, "Dump char choices");
832  BOOL_VAR_H(tessedit_timing_debug, false, "Print timing stats");
833  BOOL_VAR_H(tessedit_fix_fuzzy_spaces, true,
834  "Try to improve fuzzy spaces");
835  BOOL_VAR_H(tessedit_unrej_any_wd, false,
836  "Dont bother with word plausibility");
837  BOOL_VAR_H(tessedit_fix_hyphens, true, "Crunch double hyphens?");
838  BOOL_VAR_H(tessedit_redo_xheight, true, "Check/Correct x-height");
839  BOOL_VAR_H(tessedit_enable_doc_dict, true,
840  "Add words to the document dictionary");
841  BOOL_VAR_H(tessedit_debug_fonts, false, "Output font info per char");
842  BOOL_VAR_H(tessedit_debug_block_rejection, false, "Block and Row stats");
843  BOOL_VAR_H(tessedit_enable_bigram_correction, true,
844  "Enable correction based on the word bigram dictionary.");
845  BOOL_VAR_H(tessedit_enable_dict_correction, false,
846  "Enable single word correction based on the dictionary.");
847  INT_VAR_H(tessedit_bigram_debug, 0, "Amount of debug output for bigram "
848  "correction.");
849  BOOL_VAR_H(enable_noise_removal, true,
850  "Remove and conditionally reassign small outlines when they"
851  " confuse layout analysis, determining diacritics vs noise");
852  INT_VAR_H(debug_noise_removal, 0, "Debug reassignment of small outlines");
853  // Worst (min) certainty, for which a diacritic is allowed to make the base
854  // character worse and still be included.
855  double_VAR_H(noise_cert_basechar, -8.0, "Hingepoint for base char certainty");
856  // Worst (min) certainty, for which a non-overlapping diacritic is allowed to
857  // make the base character worse and still be included.
858  double_VAR_H(noise_cert_disjoint, -2.5, "Hingepoint for disjoint certainty");
859  // Worst (min) certainty, for which a diacritic is allowed to make a new
860  // stand-alone blob.
861  double_VAR_H(noise_cert_punc, -2.5, "Threshold for new punc char certainty");
862  // Factor of certainty margin for adding diacritics to not count as worse.
863  double_VAR_H(noise_cert_factor, 0.375,
864  "Scaling on certainty diff from Hingepoint");
865  INT_VAR_H(noise_maxperblob, 8, "Max diacritics to apply to a blob");
866  INT_VAR_H(noise_maxperword, 16, "Max diacritics to apply to a word");
867  INT_VAR_H(debug_x_ht_level, 0, "Reestimate debug");
868  BOOL_VAR_H(debug_acceptable_wds, false, "Dump word pass/fail chk");
869  STRING_VAR_H(chs_leading_punct, "('`\"", "Leading punctuation");
870  STRING_VAR_H(chs_trailing_punct1, ").,;:?!", "1st Trailing punctuation");
871  STRING_VAR_H(chs_trailing_punct2, ")'`\"", "2nd Trailing punctuation");
872  double_VAR_H(quality_rej_pc, 0.08, "good_quality_doc lte rejection limit");
873  double_VAR_H(quality_blob_pc, 0.0, "good_quality_doc gte good blobs limit");
874  double_VAR_H(quality_outline_pc, 1.0,
875  "good_quality_doc lte outline error limit");
876  double_VAR_H(quality_char_pc, 0.95, "good_quality_doc gte good char limit");
877  INT_VAR_H(quality_min_initial_alphas_reqd, 2, "alphas in a good word");
878  INT_VAR_H(tessedit_tess_adaption_mode, 0x27,
879  "Adaptation decision algorithm for tess");
880  BOOL_VAR_H(tessedit_minimal_rej_pass1, false,
881  "Do minimal rejection on pass 1 output");
882  BOOL_VAR_H(tessedit_test_adaption, false, "Test adaption criteria");
883  BOOL_VAR_H(tessedit_matcher_log, false, "Log matcher activity");
884  INT_VAR_H(tessedit_test_adaption_mode, 3,
885  "Adaptation decision algorithm for tess");
886  BOOL_VAR_H(test_pt, false, "Test for point");
887  double_VAR_H(test_pt_x, 99999.99, "xcoord");
888  double_VAR_H(test_pt_y, 99999.99, "ycoord");
889  INT_VAR_H(paragraph_debug_level, 0, "Print paragraph debug info.");
890  BOOL_VAR_H(paragraph_text_based, true,
891  "Run paragraph detection on the post-text-recognition "
892  "(more accurate)");
893  INT_VAR_H(cube_debug_level, 1, "Print cube debug info.");
894  STRING_VAR_H(outlines_odd, "%| ", "Non standard number of outlines");
895  STRING_VAR_H(outlines_2, "ij!?%\":;", "Non standard number of outlines");
896  BOOL_VAR_H(docqual_excuse_outline_errs, false,
897  "Allow outline errs in unrejection?");
898  BOOL_VAR_H(tessedit_good_quality_unrej, true,
899  "Reduce rejection on good docs");
900  BOOL_VAR_H(tessedit_use_reject_spaces, true, "Reject spaces?");
901  double_VAR_H(tessedit_reject_doc_percent, 65.00,
902  "%rej allowed before rej whole doc");
903  double_VAR_H(tessedit_reject_block_percent, 45.00,
904  "%rej allowed before rej whole block");
905  double_VAR_H(tessedit_reject_row_percent, 40.00,
906  "%rej allowed before rej whole row");
907  double_VAR_H(tessedit_whole_wd_rej_row_percent, 70.00,
908  "Number of row rejects in whole word rejects"
909  "which prevents whole row rejection");
910  BOOL_VAR_H(tessedit_preserve_blk_rej_perfect_wds, true,
911  "Only rej partially rejected words in block rejection");
912  BOOL_VAR_H(tessedit_preserve_row_rej_perfect_wds, true,
913  "Only rej partially rejected words in row rejection");
914  BOOL_VAR_H(tessedit_dont_blkrej_good_wds, false,
915  "Use word segmentation quality metric");
916  BOOL_VAR_H(tessedit_dont_rowrej_good_wds, false,
917  "Use word segmentation quality metric");
918  INT_VAR_H(tessedit_preserve_min_wd_len, 2,
919  "Only preserve wds longer than this");
920  BOOL_VAR_H(tessedit_row_rej_good_docs, true,
921  "Apply row rejection to good docs");
922  double_VAR_H(tessedit_good_doc_still_rowrej_wd, 1.1,
923  "rej good doc wd if more than this fraction rejected");
924  BOOL_VAR_H(tessedit_reject_bad_qual_wds, true,
925  "Reject all bad quality wds");
926  BOOL_VAR_H(tessedit_debug_doc_rejection, false, "Page stats");
927  BOOL_VAR_H(tessedit_debug_quality_metrics, false,
928  "Output data to debug file");
929  BOOL_VAR_H(bland_unrej, false, "unrej potential with no chekcs");
930  double_VAR_H(quality_rowrej_pc, 1.1,
931  "good_quality_doc gte good char limit");
932  BOOL_VAR_H(unlv_tilde_crunching, true,
933  "Mark v.bad words for tilde crunch");
934  BOOL_VAR_H(hocr_font_info, false,
935  "Add font info to hocr output");
936  BOOL_VAR_H(crunch_early_merge_tess_fails, true, "Before word crunch?");
937  BOOL_VAR_H(crunch_early_convert_bad_unlv_chs, false, "Take out ~^ early?");
938  double_VAR_H(crunch_terrible_rating, 80.0, "crunch rating lt this");
939  BOOL_VAR_H(crunch_terrible_garbage, true, "As it says");
940  double_VAR_H(crunch_poor_garbage_cert, -9.0,
941  "crunch garbage cert lt this");
942  double_VAR_H(crunch_poor_garbage_rate, 60, "crunch garbage rating lt this");
943  double_VAR_H(crunch_pot_poor_rate, 40, "POTENTIAL crunch rating lt this");
944  double_VAR_H(crunch_pot_poor_cert, -8.0, "POTENTIAL crunch cert lt this");
945  BOOL_VAR_H(crunch_pot_garbage, true, "POTENTIAL crunch garbage");
946  double_VAR_H(crunch_del_rating, 60, "POTENTIAL crunch rating lt this");
947  double_VAR_H(crunch_del_cert, -10.0, "POTENTIAL crunch cert lt this");
948  double_VAR_H(crunch_del_min_ht, 0.7, "Del if word ht lt xht x this");
949  double_VAR_H(crunch_del_max_ht, 3.0, "Del if word ht gt xht x this");
950  double_VAR_H(crunch_del_min_width, 3.0, "Del if word width lt xht x this");
951  double_VAR_H(crunch_del_high_word, 1.5,
952  "Del if word gt xht x this above bl");
953  double_VAR_H(crunch_del_low_word, 0.5, "Del if word gt xht x this below bl");
954  double_VAR_H(crunch_small_outlines_size, 0.6, "Small if lt xht x this");
955  INT_VAR_H(crunch_rating_max, 10, "For adj length in rating per ch");
956  INT_VAR_H(crunch_pot_indicators, 1, "How many potential indicators needed");
957  BOOL_VAR_H(crunch_leave_ok_strings, true, "Dont touch sensible strings");
958  BOOL_VAR_H(crunch_accept_ok, true, "Use acceptability in okstring");
959  BOOL_VAR_H(crunch_leave_accept_strings, false,
960  "Dont pot crunch sensible strings");
961  BOOL_VAR_H(crunch_include_numerals, false, "Fiddle alpha figures");
962  INT_VAR_H(crunch_leave_lc_strings, 4,
963  "Dont crunch words with long lower case strings");
964  INT_VAR_H(crunch_leave_uc_strings, 4,
965  "Dont crunch words with long lower case strings");
966  INT_VAR_H(crunch_long_repetitions, 3, "Crunch words with long repetitions");
967  INT_VAR_H(crunch_debug, 0, "As it says");
968  INT_VAR_H(fixsp_non_noise_limit, 1,
969  "How many non-noise blbs either side?");
970  double_VAR_H(fixsp_small_outlines_size, 0.28, "Small if lt xht x this");
971  BOOL_VAR_H(tessedit_prefer_joined_punct, false, "Reward punctation joins");
972  INT_VAR_H(fixsp_done_mode, 1, "What constitues done for spacing");
973  INT_VAR_H(debug_fix_space_level, 0, "Contextual fixspace debug");
974  STRING_VAR_H(numeric_punctuation, ".,",
975  "Punct. chs expected WITHIN numbers");
976  INT_VAR_H(x_ht_acceptance_tolerance, 8,
977  "Max allowed deviation of blob top outside of font data");
978  INT_VAR_H(x_ht_min_change, 8, "Min change in xht before actually trying it");
979  INT_VAR_H(superscript_debug, 0, "Debug level for sub & superscript fixer");
980  double_VAR_H(superscript_worse_certainty, 2.0, "How many times worse "
981  "certainty does a superscript position glyph need to be for us "
982  "to try classifying it as a char with a different baseline?");
983  double_VAR_H(superscript_bettered_certainty, 0.97, "What reduction in "
984  "badness do we think sufficient to choose a superscript over "
985  "what we'd thought. For example, a value of 0.6 means we want "
986  "to reduce badness of certainty by 40%");
987  double_VAR_H(superscript_scaledown_ratio, 0.4,
988  "A superscript scaled down more than this is unbelievably "
989  "small. For example, 0.3 means we expect the font size to "
990  "be no smaller than 30% of the text line font size.");
991  double_VAR_H(subscript_max_y_top, 0.5,
992  "Maximum top of a character measured as a multiple of x-height "
993  "above the baseline for us to reconsider whether it's a "
994  "subscript.");
995  double_VAR_H(superscript_min_y_bottom, 0.3,
996  "Minimum bottom of a character measured as a multiple of "
997  "x-height above the baseline for us to reconsider whether it's "
998  "a superscript.");
999  BOOL_VAR_H(tessedit_write_block_separators, false,
1000  "Write block separators in output");
1001  BOOL_VAR_H(tessedit_write_rep_codes, false,
1002  "Write repetition char code");
1003  BOOL_VAR_H(tessedit_write_unlv, false, "Write .unlv output file");
1004  BOOL_VAR_H(tessedit_create_txt, true, "Write .txt output file");
1005  BOOL_VAR_H(tessedit_create_hocr, false, "Write .html hOCR output file");
1006  BOOL_VAR_H(tessedit_create_pdf, false, "Write .pdf output file");
1007  STRING_VAR_H(unrecognised_char, "|",
1008  "Output char for unidentified blobs");
1009  INT_VAR_H(suspect_level, 99, "Suspect marker level");
1010  INT_VAR_H(suspect_space_level, 100,
1011  "Min suspect level for rejecting spaces");
1012  INT_VAR_H(suspect_short_words, 2,
1013  "Dont Suspect dict wds longer than this");
1014  BOOL_VAR_H(suspect_constrain_1Il, false, "UNLV keep 1Il chars rejected");
1015  double_VAR_H(suspect_rating_per_ch, 999.9, "Dont touch bad rating limit");
1016  double_VAR_H(suspect_accept_rating, -999.9, "Accept good rating limit");
1017  BOOL_VAR_H(tessedit_minimal_rejection, false, "Only reject tess failures");
1018  BOOL_VAR_H(tessedit_zero_rejection, false, "Dont reject ANYTHING");
1019  BOOL_VAR_H(tessedit_word_for_word, false,
1020  "Make output have exactly one word per WERD");
1021  BOOL_VAR_H(tessedit_zero_kelvin_rejection, false,
1022  "Dont reject ANYTHING AT ALL");
1023  BOOL_VAR_H(tessedit_consistent_reps, true, "Force all rep chars the same");
1024  INT_VAR_H(tessedit_reject_mode, 0, "Rejection algorithm");
1025  BOOL_VAR_H(tessedit_rejection_debug, false, "Adaption debug");
1026  BOOL_VAR_H(tessedit_flip_0O, true, "Contextual 0O O0 flips");
1027  double_VAR_H(tessedit_lower_flip_hyphen, 1.5,
1028  "Aspect ratio dot/hyphen test");
1029  double_VAR_H(tessedit_upper_flip_hyphen, 1.8,
1030  "Aspect ratio dot/hyphen test");
1031  BOOL_VAR_H(rej_trust_doc_dawg, false, "Use DOC dawg in 11l conf. detector");
1032  BOOL_VAR_H(rej_1Il_use_dict_word, false, "Use dictword test");
1033  BOOL_VAR_H(rej_1Il_trust_permuter_type, true, "Dont double check");
1034  BOOL_VAR_H(rej_use_tess_accepted, true, "Individual rejection control");
1035  BOOL_VAR_H(rej_use_tess_blanks, true, "Individual rejection control");
1036  BOOL_VAR_H(rej_use_good_perm, true, "Individual rejection control");
1037  BOOL_VAR_H(rej_use_sensible_wd, false, "Extend permuter check");
1038  BOOL_VAR_H(rej_alphas_in_number_perm, false, "Extend permuter check");
1039  double_VAR_H(rej_whole_of_mostly_reject_word_fract, 0.85, "if >this fract");
1040  INT_VAR_H(tessedit_image_border, 2, "Rej blbs near image edge limit");
1041  STRING_VAR_H(ok_repeated_ch_non_alphanum_wds, "-?*\075",
1042  "Allow NN to unrej");
1043  STRING_VAR_H(conflict_set_I_l_1, "Il1[]", "Il1 conflict set");
1044  INT_VAR_H(min_sane_x_ht_pixels, 8, "Reject any x-ht lt or eq than this");
1045  BOOL_VAR_H(tessedit_create_boxfile, false, "Output text with boxes");
1046  INT_VAR_H(tessedit_page_number, -1,
1047  "-1 -> All pages, else specifc page to process");
1048  BOOL_VAR_H(tessedit_write_images, false, "Capture the image from the IPE");
1049  BOOL_VAR_H(interactive_display_mode, false, "Run interactively?");
1050  STRING_VAR_H(file_type, ".tif", "Filename extension");
1051  BOOL_VAR_H(tessedit_override_permuter, true, "According to dict_word");
1052  INT_VAR_H(tessdata_manager_debug_level, 0,
1053  "Debug level for TessdataManager functions.");
1054  STRING_VAR_H(tessedit_load_sublangs, "",
1055  "List of languages to load with this one");
1056  BOOL_VAR_H(tessedit_use_primary_params_model, false,
1057  "In multilingual mode use params model of the primary language");
1058  // Min acceptable orientation margin (difference in scores between top and 2nd
1059  // choice in OSResults::orientations) to believe the page orientation.
1060  double_VAR_H(min_orientation_margin, 7.0,
1061  "Min acceptable orientation margin");
1062  BOOL_VAR_H(textord_tabfind_show_vlines, false, "Debug line finding");
1063  BOOL_VAR_H(textord_use_cjk_fp_model, FALSE, "Use CJK fixed pitch model");
1064  BOOL_VAR_H(poly_allow_detailed_fx, false,
1065  "Allow feature extractors to see the original outline");
1066  BOOL_VAR_H(tessedit_init_config_only, false,
1067  "Only initialize with the config file. Useful if the instance is "
1068  "not going to be used for OCR but say only for layout analysis.");
1069  BOOL_VAR_H(textord_equation_detect, false, "Turn on equation detector");
1070  BOOL_VAR_H(textord_tabfind_vertical_text, true, "Enable vertical detection");
1071  BOOL_VAR_H(textord_tabfind_force_vertical_text, false,
1072  "Force using vertical text page mode");
1073  double_VAR_H(textord_tabfind_vertical_text_ratio, 0.5,
1074  "Fraction of textlines deemed vertical to use vertical page "
1075  "mode");
1076  double_VAR_H(textord_tabfind_aligned_gap_fraction, 0.75,
1077  "Fraction of height used as a minimum gap for aligned blobs.");
1078  INT_VAR_H(tessedit_parallelize, 0, "Run in parallel where possible");
1079  BOOL_VAR_H(preserve_interword_spaces, false,
1080  "Preserve multiple interword spaces");
1081  BOOL_VAR_H(include_page_breaks, false,
1082  "Include page separator string in output text after each "
1083  "image/page.");
1084  STRING_VAR_H(page_separator, "\f",
1085  "Page separator (default is form feed control character)");
1086 
1087  // The following parameters were deprecated and removed from their original
1088  // locations. The parameters are temporarily kept here to give Tesseract
1089  // users a chance to updated their [lang].traineddata and config files
1090  // without introducing failures during Tesseract initialization.
1091  // TODO(ocr-team): remove these parameters from the code once we are
1092  // reasonably sure that Tesseract users have updated their data files.
1093  //
1094  // BEGIN DEPRECATED PARAMETERS
1095  BOOL_VAR_H(textord_tabfind_vertical_horizontal_mix, true,
1096  "find horizontal lines such as headers in vertical page mode");
1097  INT_VAR_H(tessedit_ok_mode, 5, "Acceptance decision algorithm");
1098  BOOL_VAR_H(load_fixed_length_dawgs, true, "Load fixed length"
1099  " dawgs (e.g. for non-space delimited languages)");
1100  INT_VAR_H(segment_debug, 0, "Debug the whole segmentation process");
1101  BOOL_VAR_H(permute_debug, 0, "char permutation debug");
1102  double_VAR_H(bestrate_pruning_factor, 2.0, "Multiplying factor of"
1103  " current best rate to prune other hypotheses");
1104  BOOL_VAR_H(permute_script_word, 0,
1105  "Turn on word script consistency permuter");
1106  BOOL_VAR_H(segment_segcost_rating, 0,
1107  "incorporate segmentation cost in word rating?");
1108  double_VAR_H(segment_reward_script, 0.95,
1109  "Score multipler for script consistency within a word. "
1110  "Being a 'reward' factor, it should be <= 1. "
1111  "Smaller value implies bigger reward.");
1112  BOOL_VAR_H(permute_fixed_length_dawg, 0,
1113  "Turn on fixed-length phrasebook search permuter");
1114  BOOL_VAR_H(permute_chartype_word, 0,
1115  "Turn on character type (property) consistency permuter");
1116  double_VAR_H(segment_reward_chartype, 0.97,
1117  "Score multipler for char type consistency within a word. ");
1118  double_VAR_H(segment_reward_ngram_best_choice, 0.99,
1119  "Score multipler for ngram permuter's best choice"
1120  " (only used in the Han script path).");
1121  BOOL_VAR_H(ngram_permuter_activated, false,
1122  "Activate character-level n-gram-based permuter");
1123  BOOL_VAR_H(permute_only_top, false, "Run only the top choice permuter");
1124  INT_VAR_H(language_model_fixed_length_choices_depth, 3,
1125  "Depth of blob choice lists to explore"
1126  " when fixed length dawgs are on");
1127  BOOL_VAR_H(use_new_state_cost, FALSE,
1128  "use new state cost heuristics for segmentation state evaluation");
1129  double_VAR_H(heuristic_segcost_rating_base, 1.25,
1130  "base factor for adding segmentation cost into word rating."
1131  "It's a multiplying factor, the larger the value above 1, "
1132  "the bigger the effect of segmentation cost.");
1133  double_VAR_H(heuristic_weight_rating, 1,
1134  "weight associated with char rating in combined cost of state");
1135  double_VAR_H(heuristic_weight_width, 1000.0,
1136  "weight associated with width evidence in combined cost of"
1137  " state");
1138  double_VAR_H(heuristic_weight_seamcut, 0,
1139  "weight associated with seam cut in combined cost of state");
1140  double_VAR_H(heuristic_max_char_wh_ratio, 2.0,
1141  "max char width-to-height ratio allowed in segmentation");
1142  BOOL_VAR_H(enable_new_segsearch, false,
1143  "Enable new segmentation search path.");
1144  double_VAR_H(segsearch_max_fixed_pitch_char_wh_ratio, 2.0,
1145  "Maximum character width-to-height ratio for"
1146  "fixed pitch fonts");
1147  // END DEPRECATED PARAMETERS
1148 
1150  FILE *init_recog_training(const STRING &fname);
1151  void recog_training_segmented(const STRING &fname,
1152  PAGE_RES *page_res,
1153  volatile ETEXT_DESC *monitor,
1154  FILE *output_file);
1155  void ambigs_classify_and_output(const char *label,
1156  PAGE_RES_IT* pr_it,
1157  FILE *output_file);
1158 
1159 #ifndef ANDROID_BUILD
1160  inline CubeRecoContext *GetCubeRecoContext() { return cube_cntxt_; }
1161 #endif
1162 
1163  private:
1164  // The filename of a backup config file. If not null, then we currently
1165  // have a temporary debug config file loaded, and backup_config_file_
1166  // will be loaded, and set to null when debug is complete.
1167  const char* backup_config_file_;
1168  // The filename of a config file to read when processing a debug word.
1169  STRING word_config_;
1170  // Image used for input to layout analysis and tesseract recognition.
1171  // May be modified by the ShiroRekhaSplitter to eliminate the top-line.
1172  Pix* pix_binary_;
1173  // Unmodified image used for input to cube. Always valid.
1174  Pix* cube_binary_;
1175  // Grey-level input image if the input was not binary, otherwise NULL.
1176  Pix* pix_grey_;
1177  // Thresholds that were used to generate the thresholded image from grey.
1178  Pix* pix_thresholds_;
1179  // Input image resolution after any scaling. The resolution is not well
1180  // transmitted by operations on Pix, so we keep an independent record here.
1181  int source_resolution_;
1182  // The shiro-rekha splitter object which is used to split top-lines in
1183  // Devanagari words to provide a better word and grapheme segmentation.
1184  ShiroRekhaSplitter splitter_;
1185  // Page segmentation/layout
1186  Textord textord_;
1187  // True if the primary language uses right_to_left reading order.
1188  bool right_to_left_;
1189  Pix* scaled_color_;
1190  int scaled_factor_;
1191  FCOORD deskew_;
1192  FCOORD reskew_;
1193  TesseractStats stats_;
1194  // Sub-languages to be tried in addition to this.
1195  GenericVector<Tesseract*> sub_langs_;
1196  // Most recently used Tesseract out of this and sub_langs_. The default
1197  // language for the next word.
1198  Tesseract* most_recently_used_;
1199  // The size of the font table, ie max possible font id + 1.
1200  int font_table_size_;
1201 #ifndef ANDROID_BUILD
1202  // Cube objects.
1203  CubeRecoContext* cube_cntxt_;
1204  TesseractCubeCombiner *tess_cube_combiner_;
1205 #endif
1206  // Equation detector. Note: this pointer is NOT owned by the class.
1207  EquationDetect* equ_detect_;
1208 };
1209 
1210 } // namespace tesseract
1211 
1212 
1213 #endif // TESSERACT_CCMAIN_TESSERACTCLASS_H__
int inT32
Definition: host.h:102
BOOL8 non_0_digit(const char *str, int length)
void dont_allow_1Il(WERD_RES *word)
void(Tesseract::* WordRecognizer)(const WordData &word_data, WERD_RES **in_word, PointerVector< WERD_RES > *out_words)
ACCEPTABLE_WERD_TYPE
Definition: control.h:34
Definition: ocrrow.h:32
Pix * BestPix() const
inT16 word_blob_quality(WERD_RES *word, ROW *row)
Pix * pix_binary() const
Tesseract * get_sub_lang(int index) const
Textord * mutable_textord()
void set_pix_grey(Pix *grey_pix)
PointerVector< WERD_RES > lang_words
#define NULL
Definition: host.h:144
WordData(const PAGE_RES_IT &page_res_it)
CubeRecoContext * GetCubeRecoContext()
Assume a single uniform block of text. (Default.)
Definition: publictypes.h:160
Definition: werd.h:60
Definition: ocrblock.h:30
int ImageWidth() const
#define FALSE
Definition: capi.h:29
int scaled_factor() const
#define STRING_VAR_H(name, val, comment)
Definition: params.h:271
int num_sub_langs() const
Definition: rect.h:30
void flip_hyphens(WERD_RES *word)
int UNICHAR_ID
Definition: unichar.h:33
void set_pix_thresholds(Pix *thresholds)
GARBAGE_LEVEL
Definition: docqual.h:25
int init_tesseract(const char *datapath, const char *language, OcrEngineMode oem)
const Textord & textord() const
short inT16
Definition: host.h:100
int source_resolution() const
WordData(BLOCK *block_in, ROW *row_in, WERD_RES *word_res)
const FCOORD & reskew() const
Definition: strngs.h:44
int ImageHeight() const
#define double_VAR_H(name, val, comment)
Definition: params.h:274
Definition: blobs.h:395
void SetScaledColor(int factor, Pix *color)
unsigned short uinT16
Definition: host.h:101
SetParamConstraint
Definition: params.h:36
CMD_EVENTS mode
Definition: pgedit.cpp:116
unsigned char BOOL8
Definition: host.h:113
Definition: points.h:189
Pix * scaled_color() const
void set_source_resolution(int ppi)
Definition: blobs.h:261
BOOL8 word_contains_non_1_digit(const char *word, const char *word_lengths)
Pix * pix_grey() const
bool right_to_left() const
bool AnyTessLang() const
#define BOOL_VAR_H(name, val, comment)
Definition: params.h:268
CRUNCH_MODE
Definition: pageres.h:145
#define INT_VAR_H(name, val, comment)
Definition: params.h:265
void flip_0O(WERD_RES *word)