All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
control.cpp
Go to the documentation of this file.
1 /******************************************************************
2  * File: control.cpp (Formerly control.c)
3  * Description: Module-independent matcher controller.
4  * Author: Ray Smith
5  * Created: Thu Apr 23 11:09:58 BST 1992
6  * ReHacked: Tue Sep 22 08:42:49 BST 1992 Phil Cheatle
7  *
8  * (C) Copyright 1992, Hewlett-Packard Ltd.
9  ** Licensed under the Apache License, Version 2.0 (the "License");
10  ** you may not use this file except in compliance with the License.
11  ** You may obtain a copy of the License at
12  ** http://www.apache.org/licenses/LICENSE-2.0
13  ** Unless required by applicable law or agreed to in writing, software
14  ** distributed under the License is distributed on an "AS IS" BASIS,
15  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  ** See the License for the specific language governing permissions and
17  ** limitations under the License.
18  *
19  **********************************************************************/
20 
21 #include <string.h>
22 #include <math.h>
23 #ifdef __UNIX__
24 #include <assert.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #endif
28 #include <ctype.h>
29 #include "ocrclass.h"
30 #include "werdit.h"
31 #include "drawfx.h"
32 #include "tessbox.h"
33 #include "tessvars.h"
34 #include "pgedit.h"
35 #include "reject.h"
36 #include "fixspace.h"
37 #include "docqual.h"
38 #include "control.h"
39 #include "output.h"
40 #include "callcpp.h"
41 #include "globals.h"
42 #include "sorthelper.h"
43 #include "tesseractclass.h"
44 
45 // Include automatically generated configuration file if running autoconf.
46 #ifdef HAVE_CONFIG_H
47 #include "config_auto.h"
48 #endif
49 
50 #define MIN_FONT_ROW_COUNT 8
51 #define MAX_XHEIGHT_DIFF 3
52 
53 const char* const kBackUpConfigFile = "tempconfigdata.config";
54 // Multiple of x-height to make a repeated word have spaces in it.
55 const double kRepcharGapThreshold = 0.5;
56 // Min believable x-height for any text when refitting as a fraction of
57 // original x-height
58 const double kMinRefitXHeightFraction = 0.5;
59 
60 
67 namespace tesseract {
69  TBOX &selection_box) {
70  PAGE_RES_IT* it = make_pseudo_word(page_res, selection_box);
71  if (it != NULL) {
73  it->DeleteCurrentWord();
74  delete it;
75  }
76 }
77 
78 
85  inT16 char_qual;
86  inT16 good_char_qual;
87 
88  WordData word_data(*pr_it);
89  SetupWordPassN(2, &word_data);
90  classify_word_and_language(2, pr_it, &word_data);
92  WERD_RES* word_res = pr_it->word();
93  word_char_quality(word_res, pr_it->row()->row, &char_qual, &good_char_qual);
94  tprintf("\n%d chars; word_blob_quality: %d; outline_errs: %d; "
95  "char_quality: %d; good_char_quality: %d\n",
96  word_res->reject_map.length(),
97  word_blob_quality(word_res, pr_it->row()->row),
98  word_outline_errs(word_res), char_qual, good_char_qual);
99  }
100  return TRUE;
101 }
102 
103 // Helper function to check for a target word and handle it appropriately.
104 // Inspired by Jetsoft's requirement to process only single words on pass2
105 // and beyond.
106 // If word_config is not null:
107 // If the word_box and target_word_box overlap, read the word_config file
108 // else reset to previous config data.
109 // return true.
110 // else
111 // If the word_box and target_word_box overlap or pass <= 1, return true.
112 // Note that this function uses a fixed temporary file for storing the previous
113 // configs, so it is neither thread-safe, nor process-safe, but the assumption
114 // is that it will only be used for one debug window at a time.
115 //
116 // Since this function is used for debugging (and not to change OCR results)
117 // set only debug params from the word config file.
118 bool Tesseract::ProcessTargetWord(const TBOX& word_box,
119  const TBOX& target_word_box,
120  const char* word_config,
121  int pass) {
122  if (word_config != NULL) {
123  if (word_box.major_overlap(target_word_box)) {
124  if (backup_config_file_ == NULL) {
125  backup_config_file_ = kBackUpConfigFile;
126  FILE* config_fp = fopen(backup_config_file_, "wb");
127  ParamUtils::PrintParams(config_fp, params());
128  fclose(config_fp);
129  ParamUtils::ReadParamsFile(word_config,
131  params());
132  }
133  } else {
134  if (backup_config_file_ != NULL) {
135  ParamUtils::ReadParamsFile(backup_config_file_,
137  params());
138  backup_config_file_ = NULL;
139  }
140  }
141  } else if (pass > 1 && !word_box.major_overlap(target_word_box)) {
142  return false;
143  }
144  return true;
145 }
146 
149  const TBOX* target_word_box,
150  const char* word_config,
151  PAGE_RES* page_res,
152  GenericVector<WordData>* words) {
153  // Prepare all the words.
154  PAGE_RES_IT page_res_it(page_res);
155  for (page_res_it.restart_page(); page_res_it.word() != NULL;
156  page_res_it.forward()) {
157  if (target_word_box == NULL ||
158  ProcessTargetWord(page_res_it.word()->word->bounding_box(),
159  *target_word_box, word_config, 1)) {
160  words->push_back(WordData(page_res_it));
161  }
162  }
163  // Setup all the words for recognition with polygonal approximation.
164  for (int w = 0; w < words->size(); ++w) {
165  SetupWordPassN(pass_n, &(*words)[w]);
166  if (w > 0) (*words)[w].prev_word = &(*words)[w - 1];
167  }
168 }
169 
170 // Sets up the single word ready for whichever engine is to be run.
171 void Tesseract::SetupWordPassN(int pass_n, WordData* word) {
172  if (pass_n == 1 || !word->word->done) {
173  if (pass_n == 1) {
174  word->word->SetupForRecognition(unicharset, this, BestPix(),
179  word->row, word->block);
180  } else if (pass_n == 2) {
181  // TODO(rays) Should we do this on pass1 too?
182  word->word->caps_height = 0.0;
183  if (word->word->x_height == 0.0f)
184  word->word->x_height = word->row->x_height();
185  }
186  word->lang_words.truncate(0);
187  for (int s = 0; s <= sub_langs_.size(); ++s) {
188  // The sub_langs_.size() entry is for the master language.
189  Tesseract* lang_t = s < sub_langs_.size() ? sub_langs_[s] : this;
190  WERD_RES* word_res = new WERD_RES;
191  word_res->InitForRetryRecognition(*word->word);
192  word->lang_words.push_back(word_res);
193  // Cube doesn't get setup for pass2.
194  if (pass_n == 1 || lang_t->tessedit_ocr_engine_mode != OEM_CUBE_ONLY) {
195  word_res->SetupForRecognition(
196  lang_t->unicharset, lang_t, BestPix(),
199  lang_t->textord_use_cjk_fp_model,
200  lang_t->poly_allow_detailed_fx, word->row, word->block);
201  }
202  }
203  }
204 }
205 
206 // Runs word recognition on all the words.
207 bool Tesseract::RecogAllWordsPassN(int pass_n, ETEXT_DESC* monitor,
208  PAGE_RES_IT* pr_it,
209  GenericVector<WordData>* words) {
210  // TODO(rays) Before this loop can be parallelized (it would yield a massive
211  // speed-up) all remaining member globals need to be converted to local/heap
212  // (eg set_pass1 and set_pass2) and an intermediate adaption pass needs to be
213  // added. The results will be significantly different with adaption on, and
214  // deterioration will need investigation.
215  pr_it->restart_page();
216  for (int w = 0; w < words->size(); ++w) {
217  WordData* word = &(*words)[w];
218  if (w > 0) word->prev_word = &(*words)[w - 1];
219  if (monitor != NULL) {
220  monitor->ocr_alive = TRUE;
221  if (pass_n == 1)
222  monitor->progress = 30 + 50 * w / words->size();
223  else
224  monitor->progress = 80 + 10 * w / words->size();
225  if (monitor->deadline_exceeded() ||
226  (monitor->cancel != NULL && (*monitor->cancel)(monitor->cancel_this,
227  words->size()))) {
228  // Timeout. Fake out the rest of the words.
229  for (; w < words->size(); ++w) {
230  (*words)[w].word->SetupFake(unicharset);
231  }
232  return false;
233  }
234  }
235  if (word->word->tess_failed) {
236  int s;
237  for (s = 0; s < word->lang_words.size() &&
238  word->lang_words[s]->tess_failed; ++s) {}
239  // If all are failed, skip it. Image words are skipped by this test.
240  if (s > word->lang_words.size()) continue;
241  }
242  // Sync pr_it with the wth WordData.
243  while (pr_it->word() != NULL && pr_it->word() != word->word)
244  pr_it->forward();
245  ASSERT_HOST(pr_it->word() != NULL);
246  bool make_next_word_fuzzy = false;
247  if (ReassignDiacritics(pass_n, pr_it, &make_next_word_fuzzy)) {
248  // Needs to be setup again to see the new outlines in the chopped_word.
249  SetupWordPassN(pass_n, word);
250  }
251 
252  classify_word_and_language(pass_n, pr_it, word);
254  tprintf("Pass%d: %s [%s]\n", pass_n,
256  word->word->best_choice->debug_string().string());
257  }
258  pr_it->forward();
259  if (make_next_word_fuzzy && pr_it->word() != NULL) {
260  pr_it->MakeCurrentWordFuzzy();
261  }
262  }
263  return true;
264 }
265 
288  ETEXT_DESC* monitor,
289  const TBOX* target_word_box,
290  const char* word_config,
291  int dopasses) {
292  PAGE_RES_IT page_res_it(page_res);
293 
295  tessedit_test_adaption.set_value (TRUE);
296  tessedit_minimal_rejection.set_value (TRUE);
297  }
298 
299  if (dopasses==0 || dopasses==1) {
300  page_res_it.restart_page();
301  // ****************** Pass 1 *******************
302 
303  // If the adaptive classifier is full switch to one we prepared earlier,
304  // ie on the previous page. If the current adaptive classifier is non-empty,
305  // prepare a backup starting at this page, in case it fills up. Do all this
306  // independently for each language.
307  if (AdaptiveClassifierIsFull()) {
309  } else if (!AdaptiveClassifierIsEmpty()) {
311  }
312  // Now check the sub-langs as well.
313  for (int i = 0; i < sub_langs_.size(); ++i) {
314  if (sub_langs_[i]->AdaptiveClassifierIsFull()) {
315  sub_langs_[i]->SwitchAdaptiveClassifier();
316  } else if (!sub_langs_[i]->AdaptiveClassifierIsEmpty()) {
317  sub_langs_[i]->StartBackupAdaptiveClassifier();
318  }
319  }
320  // Set up all words ready for recognition, so that if parallelism is on
321  // all the input and output classes are ready to run the classifier.
323  SetupAllWordsPassN(1, target_word_box, word_config, page_res, &words);
324  if (tessedit_parallelize) {
325  PrerecAllWordsPar(words);
326  }
327 
328  stats_.word_count = words.size();
329 
330  stats_.dict_words = 0;
331  stats_.doc_blob_quality = 0;
332  stats_.doc_outline_errs = 0;
333  stats_.doc_char_quality = 0;
334  stats_.good_char_count = 0;
335  stats_.doc_good_char_quality = 0;
336 
337  most_recently_used_ = this;
338  // Run pass 1 word recognition.
339  if (!RecogAllWordsPassN(1, monitor, &page_res_it, &words)) return false;
340  // Pass 1 post-processing.
341  for (page_res_it.restart_page(); page_res_it.word() != NULL;
342  page_res_it.forward()) {
343  if (page_res_it.word()->word->flag(W_REP_CHAR)) {
344  fix_rep_char(&page_res_it);
345  continue;
346  }
347 
348  // Count dict words.
349  if (page_res_it.word()->best_choice->permuter() == USER_DAWG_PERM)
350  ++(stats_.dict_words);
351 
352  // Update misadaption log (we only need to do it on pass 1, since
353  // adaption only happens on this pass).
354  if (page_res_it.word()->blamer_bundle != NULL &&
355  page_res_it.word()->blamer_bundle->misadaption_debug().length() > 0) {
356  page_res->misadaption_log.push_back(
357  page_res_it.word()->blamer_bundle->misadaption_debug());
358  }
359  }
360  }
361 
362  if (dopasses == 1) return true;
363 
364  // ****************** Pass 2 *******************
366  AnyTessLang()) {
367  page_res_it.restart_page();
369  SetupAllWordsPassN(2, target_word_box, word_config, page_res, &words);
370  if (tessedit_parallelize) {
371  PrerecAllWordsPar(words);
372  }
373  most_recently_used_ = this;
374  // Run pass 2 word recognition.
375  if (!RecogAllWordsPassN(2, monitor, &page_res_it, &words)) return false;
376  }
377 
378  // The next passes can only be run if tesseract has been used, as cube
379  // doesn't set all the necessary outputs in WERD_RES.
380  if (AnyTessLang()) {
381  // ****************** Pass 3 *******************
382  // Fix fuzzy spaces.
384 
387  fix_fuzzy_spaces(monitor, stats_.word_count, page_res);
388 
389  // ****************** Pass 4 *******************
392 
393  // ****************** Pass 5,6 *******************
394  rejection_passes(page_res, monitor, target_word_box, word_config);
395 
396 #ifndef ANDROID_BUILD
397  // ****************** Pass 7 *******************
398  // Cube combiner.
399  // If cube is loaded and its combiner is present, run it.
401  run_cube_combiner(page_res);
402  }
403 #endif
404 
405  // ****************** Pass 8 *******************
406  font_recognition_pass(page_res);
407 
408  // ****************** Pass 9 *******************
409  // Check the correctness of the final results.
410  blamer_pass(page_res);
411  script_pos_pass(page_res);
412  }
413 
414  // Write results pass.
416  // This is now redundant, but retained commented so show how to obtain
417  // bounding boxes and style information.
418 
419  // changed by jetsoft
420  // needed for dll to output memory structure
421  if ((dopasses == 0 || dopasses == 2) && (monitor || tessedit_write_unlv))
422  output_pass(page_res_it, target_word_box);
423  // end jetsoft
424  PageSegMode pageseg_mode = static_cast<PageSegMode>(
425  static_cast<int>(tessedit_pageseg_mode));
426  textord_.CleanupSingleRowResult(pageseg_mode, page_res);
427 
428  // Remove empty words, as these mess up the result iterators.
429  for (page_res_it.restart_page(); page_res_it.word() != NULL;
430  page_res_it.forward()) {
431  WERD_RES* word = page_res_it.word();
432  if (word->best_choice == NULL || word->best_choice->length() == 0)
433  page_res_it.DeleteCurrentWord();
434  }
435 
436  if (monitor != NULL) {
437  monitor->progress = 100;
438  }
439  return true;
440 }
441 
443  PAGE_RES_IT word_it(page_res);
444 
445  WERD_RES *w_prev = NULL;
446  WERD_RES *w = word_it.word();
447  while (1) {
448  w_prev = w;
449  while (word_it.forward() != NULL &&
450  (!word_it.word() || word_it.word()->part_of_combo)) {
451  // advance word_it, skipping over parts of combos
452  }
453  if (!word_it.word()) break;
454  w = word_it.word();
455  if (!w || !w_prev || w->uch_set != w_prev->uch_set) {
456  continue;
457  }
458  if (w_prev->word->flag(W_REP_CHAR) || w->word->flag(W_REP_CHAR)) {
459  if (tessedit_bigram_debug) {
460  tprintf("Skipping because one of the words is W_REP_CHAR\n");
461  }
462  continue;
463  }
464  // Two words sharing the same language model, excellent!
465  GenericVector<WERD_CHOICE *> overrides_word1;
466  GenericVector<WERD_CHOICE *> overrides_word2;
467 
468  STRING orig_w1_str = w_prev->best_choice->unichar_string();
469  STRING orig_w2_str = w->best_choice->unichar_string();
470  WERD_CHOICE prev_best(w->uch_set);
471  {
472  int w1start, w1end;
473  w_prev->best_choice->GetNonSuperscriptSpan(&w1start, &w1end);
474  prev_best = w_prev->best_choice->shallow_copy(w1start, w1end);
475  }
476  WERD_CHOICE this_best(w->uch_set);
477  {
478  int w2start, w2end;
479  w->best_choice->GetNonSuperscriptSpan(&w2start, &w2end);
480  this_best = w->best_choice->shallow_copy(w2start, w2end);
481  }
482 
483  if (w->tesseract->getDict().valid_bigram(prev_best, this_best)) {
484  if (tessedit_bigram_debug) {
485  tprintf("Top choice \"%s %s\" verified by bigram model.\n",
486  orig_w1_str.string(), orig_w2_str.string());
487  }
488  continue;
489  }
490  if (tessedit_bigram_debug > 2) {
491  tprintf("Examining alt choices for \"%s %s\".\n",
492  orig_w1_str.string(), orig_w2_str.string());
493  }
494  if (tessedit_bigram_debug > 1) {
495  if (!w_prev->best_choices.singleton()) {
496  w_prev->PrintBestChoices();
497  }
498  if (!w->best_choices.singleton()) {
499  w->PrintBestChoices();
500  }
501  }
502  float best_rating = 0.0;
503  int best_idx = 0;
504  WERD_CHOICE_IT prev_it(&w_prev->best_choices);
505  for (prev_it.mark_cycle_pt(); !prev_it.cycled_list(); prev_it.forward()) {
506  WERD_CHOICE *p1 = prev_it.data();
507  WERD_CHOICE strip1(w->uch_set);
508  {
509  int p1start, p1end;
510  p1->GetNonSuperscriptSpan(&p1start, &p1end);
511  strip1 = p1->shallow_copy(p1start, p1end);
512  }
513  WERD_CHOICE_IT w_it(&w->best_choices);
514  for (w_it.mark_cycle_pt(); !w_it.cycled_list(); w_it.forward()) {
515  WERD_CHOICE *p2 = w_it.data();
516  WERD_CHOICE strip2(w->uch_set);
517  {
518  int p2start, p2end;
519  p2->GetNonSuperscriptSpan(&p2start, &p2end);
520  strip2 = p2->shallow_copy(p2start, p2end);
521  }
522  if (w->tesseract->getDict().valid_bigram(strip1, strip2)) {
523  overrides_word1.push_back(p1);
524  overrides_word2.push_back(p2);
525  if (overrides_word1.size() == 1 ||
526  p1->rating() + p2->rating() < best_rating) {
527  best_rating = p1->rating() + p2->rating();
528  best_idx = overrides_word1.size() - 1;
529  }
530  }
531  }
532  }
533  if (overrides_word1.size() >= 1) {
534  // Excellent, we have some bigram matches.
536  *overrides_word1[best_idx]) &&
538  *overrides_word2[best_idx])) {
539  if (tessedit_bigram_debug > 1) {
540  tprintf("Top choice \"%s %s\" verified (sans case) by bigram "
541  "model.\n", orig_w1_str.string(), orig_w2_str.string());
542  }
543  continue;
544  }
545  STRING new_w1_str = overrides_word1[best_idx]->unichar_string();
546  STRING new_w2_str = overrides_word2[best_idx]->unichar_string();
547  if (new_w1_str != orig_w1_str) {
548  w_prev->ReplaceBestChoice(overrides_word1[best_idx]);
549  }
550  if (new_w2_str != orig_w2_str) {
551  w->ReplaceBestChoice(overrides_word2[best_idx]);
552  }
553  if (tessedit_bigram_debug > 0) {
554  STRING choices_description;
555  int num_bigram_choices
556  = overrides_word1.size() * overrides_word2.size();
557  if (num_bigram_choices == 1) {
558  choices_description = "This was the unique bigram choice.";
559  } else {
560  if (tessedit_bigram_debug > 1) {
561  STRING bigrams_list;
562  const int kMaxChoicesToPrint = 20;
563  for (int i = 0; i < overrides_word1.size() &&
564  i < kMaxChoicesToPrint; i++) {
565  if (i > 0) { bigrams_list += ", "; }
566  WERD_CHOICE *p1 = overrides_word1[i];
567  WERD_CHOICE *p2 = overrides_word2[i];
568  bigrams_list += p1->unichar_string() + " " + p2->unichar_string();
569  if (i == kMaxChoicesToPrint) {
570  bigrams_list += " ...";
571  }
572  }
573  choices_description = "There were many choices: {";
574  choices_description += bigrams_list;
575  choices_description += "}";
576  } else {
577  choices_description.add_str_int("There were ", num_bigram_choices);
578  choices_description += " compatible bigrams.";
579  }
580  }
581  tprintf("Replaced \"%s %s\" with \"%s %s\" with bigram model. %s\n",
582  orig_w1_str.string(), orig_w2_str.string(),
583  new_w1_str.string(), new_w2_str.string(),
584  choices_description.string());
585  }
586  }
587  }
588 }
589 
591  ETEXT_DESC* monitor,
592  const TBOX* target_word_box,
593  const char* word_config) {
594  PAGE_RES_IT page_res_it(page_res);
595  // ****************** Pass 5 *******************
596  // Gather statistics on rejects.
597  int word_index = 0;
598  while (!tessedit_test_adaption && page_res_it.word() != NULL) {
600  WERD_RES* word = page_res_it.word();
601  word_index++;
602  if (monitor != NULL) {
603  monitor->ocr_alive = TRUE;
604  monitor->progress = 95 + 5 * word_index / stats_.word_count;
605  }
606  if (word->rebuild_word == NULL) {
607  // Word was not processed by tesseract.
608  page_res_it.forward();
609  continue;
610  }
611  check_debug_pt(word, 70);
612 
613  // changed by jetsoft
614  // specific to its needs to extract one word when need
615  if (target_word_box &&
617  *target_word_box, word_config, 4)) {
618  page_res_it.forward();
619  continue;
620  }
621  // end jetsoft
622 
623  page_res_it.rej_stat_word();
624  int chars_in_word = word->reject_map.length();
625  int rejects_in_word = word->reject_map.reject_count();
626 
627  int blob_quality = word_blob_quality(word, page_res_it.row()->row);
628  stats_.doc_blob_quality += blob_quality;
629  int outline_errs = word_outline_errs(word);
630  stats_.doc_outline_errs += outline_errs;
631  inT16 all_char_quality;
632  inT16 accepted_all_char_quality;
633  word_char_quality(word, page_res_it.row()->row,
634  &all_char_quality, &accepted_all_char_quality);
635  stats_.doc_char_quality += all_char_quality;
636  uinT8 permuter_type = word->best_choice->permuter();
637  if ((permuter_type == SYSTEM_DAWG_PERM) ||
638  (permuter_type == FREQ_DAWG_PERM) ||
639  (permuter_type == USER_DAWG_PERM)) {
640  stats_.good_char_count += chars_in_word - rejects_in_word;
641  stats_.doc_good_char_quality += accepted_all_char_quality;
642  }
643  check_debug_pt(word, 80);
645  (blob_quality == 0) && (outline_errs >= chars_in_word))
647  check_debug_pt(word, 90);
648  page_res_it.forward();
649  }
650 
652  tprintf
653  ("QUALITY: num_chs= %d num_rejs= %d %5.3f blob_qual= %d %5.3f"
654  " outline_errs= %d %5.3f char_qual= %d %5.3f good_ch_qual= %d %5.3f\n",
655  page_res->char_count, page_res->rej_count,
656  page_res->rej_count / static_cast<float>(page_res->char_count),
657  stats_.doc_blob_quality,
658  stats_.doc_blob_quality / static_cast<float>(page_res->char_count),
659  stats_.doc_outline_errs,
660  stats_.doc_outline_errs / static_cast<float>(page_res->char_count),
661  stats_.doc_char_quality,
662  stats_.doc_char_quality / static_cast<float>(page_res->char_count),
663  stats_.doc_good_char_quality,
664  (stats_.good_char_count > 0) ?
665  (stats_.doc_good_char_quality /
666  static_cast<float>(stats_.good_char_count)) : 0.0);
667  }
668  BOOL8 good_quality_doc =
669  ((page_res->rej_count / static_cast<float>(page_res->char_count)) <=
670  quality_rej_pc) &&
671  (stats_.doc_blob_quality / static_cast<float>(page_res->char_count) >=
672  quality_blob_pc) &&
673  (stats_.doc_outline_errs / static_cast<float>(page_res->char_count) <=
675  (stats_.doc_char_quality / static_cast<float>(page_res->char_count) >=
677 
678  // ****************** Pass 6 *******************
679  // Do whole document or whole block rejection pass
680  if (!tessedit_test_adaption) {
682  quality_based_rejection(page_res_it, good_quality_doc);
683  }
684 }
685 
687  if (!wordrec_run_blamer) return;
688  PAGE_RES_IT page_res_it(page_res);
689  for (page_res_it.restart_page(); page_res_it.word() != NULL;
690  page_res_it.forward()) {
691  WERD_RES *word = page_res_it.word();
694  }
695  tprintf("Blame reasons:\n");
696  for (int bl = 0; bl < IRR_NUM_REASONS; ++bl) {
698  static_cast<IncorrectResultReason>(bl)),
699  page_res->blame_reasons[bl]);
700  }
701  if (page_res->misadaption_log.length() > 0) {
702  tprintf("Misadaption log:\n");
703  for (int i = 0; i < page_res->misadaption_log.length(); ++i) {
704  tprintf("%s\n", page_res->misadaption_log[i].string());
705  }
706  }
707 }
708 
709 // Sets script positions and detects smallcaps on all output words.
711  PAGE_RES_IT page_res_it(page_res);
712  for (page_res_it.restart_page(); page_res_it.word() != NULL;
713  page_res_it.forward()) {
714  WERD_RES* word = page_res_it.word();
715  if (word->word->flag(W_REP_CHAR)) {
716  page_res_it.forward();
717  continue;
718  }
719  float x_height = page_res_it.block()->block->x_height();
720  float word_x_height = word->x_height;
721  if (word_x_height < word->best_choice->min_x_height() ||
722  word_x_height > word->best_choice->max_x_height()) {
723  word_x_height = (word->best_choice->min_x_height() +
724  word->best_choice->max_x_height()) / 2.0f;
725  }
726  // Test for small caps. Word capheight must be close to block xheight,
727  // and word must contain no lower case letters, and at least one upper case.
728  double small_cap_xheight = x_height * kXHeightCapRatio;
729  double small_cap_delta = (x_height - small_cap_xheight) / 2.0;
730  if (word->uch_set->script_has_xheight() &&
731  small_cap_xheight - small_cap_delta <= word_x_height &&
732  word_x_height <= small_cap_xheight + small_cap_delta) {
733  // Scan for upper/lower.
734  int num_upper = 0;
735  int num_lower = 0;
736  for (int i = 0; i < word->best_choice->length(); ++i) {
737  if (word->uch_set->get_isupper(word->best_choice->unichar_id(i)))
738  ++num_upper;
739  else if (word->uch_set->get_islower(word->best_choice->unichar_id(i)))
740  ++num_lower;
741  }
742  if (num_upper > 0 && num_lower == 0)
743  word->small_caps = true;
744  }
745  word->SetScriptPositions();
746  }
747 }
748 
749 // Factored helper considers the indexed word and updates all the pointed
750 // values.
751 static void EvaluateWord(const PointerVector<WERD_RES>& words, int index,
752  float* rating, float* certainty, bool* bad,
753  bool* valid_permuter, int* right, int* next_left) {
754  *right = -MAX_INT32;
755  *next_left = MAX_INT32;
756  if (index < words.size()) {
757  WERD_CHOICE* choice = words[index]->best_choice;
758  if (choice == NULL) {
759  *bad = true;
760  } else {
761  *rating += choice->rating();
762  *certainty = MIN(*certainty, choice->certainty());
763  if (!Dict::valid_word_permuter(choice->permuter(), false))
764  *valid_permuter = false;
765  }
766  *right = words[index]->word->bounding_box().right();
767  if (index + 1 < words.size())
768  *next_left = words[index + 1]->word->bounding_box().left();
769  } else {
770  *valid_permuter = false;
771  *bad = true;
772  }
773 }
774 
775 // Helper chooses the best combination of words, transferring good ones from
776 // new_words to best_words. To win, a new word must have (better rating and
777 // certainty) or (better permuter status and rating within rating ratio and
778 // certainty within certainty margin) than current best.
779 // All the new_words are consumed (moved to best_words or deleted.)
780 // The return value is the number of new_words used minus the number of
781 // best_words that remain in the output.
782 static int SelectBestWords(double rating_ratio,
783  double certainty_margin,
784  bool debug,
785  PointerVector<WERD_RES>* new_words,
786  PointerVector<WERD_RES>* best_words) {
787  // Process the smallest groups of words that have an overlapping word
788  // boundary at the end.
789  GenericVector<WERD_RES*> out_words;
790  // Index into each word vector (best, new).
791  int b = 0, n = 0;
792  int num_best = 0, num_new = 0;
793  while (b < best_words->size() || n < new_words->size()) {
794  // Start of the current run in each.
795  int start_b = b, start_n = n;
796  // Rating of the current run in each.
797  float b_rating = 0.0f, n_rating = 0.0f;
798  // Certainty of the current run in each.
799  float b_certainty = 0.0f, n_certainty = 0.0f;
800  // True if any word is missing its best choice.
801  bool b_bad = false, n_bad = false;
802  // True if all words have a valid permuter.
803  bool b_valid_permuter = true, n_valid_permuter = true;
804 
805  while (b < best_words->size() || n < new_words->size()) {
806  int b_right = -MAX_INT32;
807  int next_b_left = MAX_INT32;
808  EvaluateWord(*best_words, b, &b_rating, &b_certainty, &b_bad,
809  &b_valid_permuter, &b_right, &next_b_left);
810  int n_right = -MAX_INT32;
811  int next_n_left = MAX_INT32;
812  EvaluateWord(*new_words, n, &n_rating, &n_certainty, &n_bad,
813  &n_valid_permuter, &n_right, &next_n_left);
814  if (MAX(b_right, n_right) < MIN(next_b_left, next_n_left)) {
815  // The word breaks overlap. [start_b,b] and [start_n, n] match.
816  break;
817  }
818  // Keep searching for the matching word break.
819  if ((b_right < n_right && b < best_words->size()) ||
820  n == new_words->size())
821  ++b;
822  else
823  ++n;
824  }
825  bool new_better = false;
826  if (!n_bad && (b_bad || (n_certainty > b_certainty &&
827  n_rating < b_rating) ||
828  (!b_valid_permuter && n_valid_permuter &&
829  n_rating < b_rating * rating_ratio &&
830  n_certainty > b_certainty - certainty_margin))) {
831  // New is better.
832  for (int i = start_n; i <= n; ++i) {
833  out_words.push_back((*new_words)[i]);
834  (*new_words)[i] = NULL;
835  ++num_new;
836  }
837  new_better = true;
838  } else if (!b_bad) {
839  // Current best is better.
840  for (int i = start_b; i <= b; ++i) {
841  out_words.push_back((*best_words)[i]);
842  (*best_words)[i] = NULL;
843  ++num_best;
844  }
845  }
846  int end_b = b < best_words->size() ? b + 1 : b;
847  int end_n = n < new_words->size() ? n + 1 : n;
848  if (debug) {
849  tprintf("%d new words %s than %d old words: r: %g v %g c: %g v %g"
850  " valid dict: %d v %d\n",
851  end_n - start_n, new_better ? "better" : "worse",
852  end_b - start_b, n_rating, b_rating,
853  n_certainty, b_certainty, n_valid_permuter, b_valid_permuter);
854  }
855  // Move on to the next group.
856  b = end_b;
857  n = end_n;
858  }
859  // Transfer from out_words to best_words.
860  best_words->clear();
861  for (int i = 0; i < out_words.size(); ++i)
862  best_words->push_back(out_words[i]);
863  return num_new - num_best;
864 }
865 
866 // Helper to recognize the word using the given (language-specific) tesseract.
867 // Returns positive if this recognizer found more new best words than the
868 // number kept from best_words.
870  WordRecognizer recognizer,
871  WERD_RES** in_word,
872  PointerVector<WERD_RES>* best_words) {
873  bool debug = classify_debug_level || cube_debug_level;
874  if (debug) {
875  tprintf("Trying word using lang %s, oem %d\n",
876  lang.string(), static_cast<int>(tessedit_ocr_engine_mode));
877  }
878  // Run the recognizer on the word.
879  PointerVector<WERD_RES> new_words;
880  (this->*recognizer)(word_data, in_word, &new_words);
881  if (new_words.empty()) {
882  // Transfer input word to new_words, as the classifier must have put
883  // the result back in the input.
884  new_words.push_back(*in_word);
885  *in_word = NULL;
886  }
887  if (debug) {
888  for (int i = 0; i < new_words.size(); ++i)
889  new_words[i]->DebugTopChoice("Lang result");
890  }
891  // Initial version is a bit of a hack based on better certainty and rating
892  // (to reduce false positives from cube) or a dictionary vs non-dictionary
893  // word.
894  return SelectBestWords(classify_max_rating_ratio,
896  debug, &new_words, best_words);
897 }
898 
899 // Helper returns true if all the words are acceptable.
900 static bool WordsAcceptable(const PointerVector<WERD_RES>& words) {
901  for (int w = 0; w < words.size(); ++w) {
902  if (words[w]->tess_failed || !words[w]->tess_accepted) return false;
903  }
904  return true;
905 }
906 
907 // Moves good-looking "noise"/diacritics from the reject list to the main
908 // blob list on the current word. Returns true if anything was done, and
909 // sets make_next_word_fuzzy if blob(s) were added to the end of the word.
911  bool* make_next_word_fuzzy) {
912  *make_next_word_fuzzy = false;
913  WERD* real_word = pr_it->word()->word;
914  if (real_word->rej_cblob_list()->empty() ||
915  real_word->cblob_list()->empty() ||
916  real_word->rej_cblob_list()->length() > noise_maxperword)
917  return false;
918  real_word->rej_cblob_list()->sort(&C_BLOB::SortByXMiddle);
919  // Get the noise outlines into a vector with matching bool map.
920  GenericVector<C_OUTLINE*> outlines;
921  real_word->GetNoiseOutlines(&outlines);
922  GenericVector<bool> word_wanted;
923  GenericVector<bool> overlapped_any_blob;
924  GenericVector<C_BLOB*> target_blobs;
925  AssignDiacriticsToOverlappingBlobs(outlines, pass, real_word, pr_it,
926  &word_wanted, &overlapped_any_blob,
927  &target_blobs);
928  // Filter the outlines that overlapped any blob and put them into the word
929  // now. This simplifies the remaining task and also makes it more accurate
930  // as it has more completed blobs to work on.
931  GenericVector<bool> wanted;
932  GenericVector<C_BLOB*> wanted_blobs;
933  GenericVector<C_OUTLINE*> wanted_outlines;
934  int num_overlapped = 0;
935  int num_overlapped_used = 0;
936  for (int i = 0; i < overlapped_any_blob.size(); ++i) {
937  if (overlapped_any_blob[i]) {
938  ++num_overlapped;
939  if (word_wanted[i]) ++num_overlapped_used;
940  wanted.push_back(word_wanted[i]);
941  wanted_blobs.push_back(target_blobs[i]);
942  wanted_outlines.push_back(outlines[i]);
943  outlines[i] = NULL;
944  }
945  }
946  real_word->AddSelectedOutlines(wanted, wanted_blobs, wanted_outlines, NULL);
947  AssignDiacriticsToNewBlobs(outlines, pass, real_word, pr_it, &word_wanted,
948  &target_blobs);
949  int non_overlapped = 0;
950  int non_overlapped_used = 0;
951  for (int i = 0; i < word_wanted.size(); ++i) {
952  if (word_wanted[i]) ++non_overlapped_used;
953  if (outlines[i] != NULL) ++non_overlapped_used;
954  }
955  if (debug_noise_removal) {
956  tprintf("Used %d/%d overlapped %d/%d non-overlaped diacritics on word:",
957  num_overlapped_used, num_overlapped, non_overlapped_used,
958  non_overlapped);
959  real_word->bounding_box().print();
960  }
961  // Now we have decided which outlines we want, put them into the real_word.
962  if (real_word->AddSelectedOutlines(word_wanted, target_blobs, outlines,
963  make_next_word_fuzzy)) {
964  pr_it->MakeCurrentWordFuzzy();
965  }
966  // TODO(rays) Parts of combos have a deep copy of the real word, and need
967  // to have their noise outlines moved/assigned in the same way!!
968  return num_overlapped_used != 0 || non_overlapped_used != 0;
969 }
970 
971 // Attempts to put noise/diacritic outlines into the blobs that they overlap.
972 // Input: a set of noisy outlines that probably belong to the real_word.
973 // Output: word_wanted indicates which outlines are to be assigned to a blob,
974 // target_blobs indicates which to assign to, and overlapped_any_blob is
975 // true for all outlines that overlapped a blob.
977  const GenericVector<C_OUTLINE*>& outlines, int pass, WERD* real_word,
978  PAGE_RES_IT* pr_it, GenericVector<bool>* word_wanted,
979  GenericVector<bool>* overlapped_any_blob,
980  GenericVector<C_BLOB*>* target_blobs) {
981  GenericVector<bool> blob_wanted;
982  word_wanted->init_to_size(outlines.size(), false);
983  overlapped_any_blob->init_to_size(outlines.size(), false);
984  target_blobs->init_to_size(outlines.size(), NULL);
985  // For each real blob, find the outlines that seriously overlap it.
986  // A single blob could be several merged characters, so there can be quite
987  // a few outlines overlapping, and the full engine needs to be used to chop
988  // and join to get a sensible result.
989  C_BLOB_IT blob_it(real_word->cblob_list());
990  for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
991  C_BLOB* blob = blob_it.data();
992  TBOX blob_box = blob->bounding_box();
993  blob_wanted.init_to_size(outlines.size(), false);
994  int num_blob_outlines = 0;
995  for (int i = 0; i < outlines.size(); ++i) {
996  if (blob_box.major_x_overlap(outlines[i]->bounding_box()) &&
997  !(*word_wanted)[i]) {
998  blob_wanted[i] = true;
999  (*overlapped_any_blob)[i] = true;
1000  ++num_blob_outlines;
1001  }
1002  }
1003  if (debug_noise_removal) {
1004  tprintf("%d noise outlines overlap blob at:", num_blob_outlines);
1005  blob_box.print();
1006  }
1007  // If any outlines overlap the blob, and not too many, classify the blob
1008  // (using the full engine, languages and all), and choose the maximal
1009  // combination of outlines that doesn't hurt the end-result classification
1010  // by too much. Mark them as wanted.
1011  if (0 < num_blob_outlines && num_blob_outlines < noise_maxperblob) {
1012  if (SelectGoodDiacriticOutlines(pass, noise_cert_basechar, pr_it, blob,
1013  outlines, num_blob_outlines,
1014  &blob_wanted)) {
1015  for (int i = 0; i < blob_wanted.size(); ++i) {
1016  if (blob_wanted[i]) {
1017  // Claim the outline and record where it is going.
1018  (*word_wanted)[i] = true;
1019  (*target_blobs)[i] = blob;
1020  }
1021  }
1022  }
1023  }
1024  }
1025 }
1026 
1027 // Attempts to assign non-overlapping outlines to their nearest blobs or
1028 // make new blobs out of them.
1030  const GenericVector<C_OUTLINE*>& outlines, int pass, WERD* real_word,
1031  PAGE_RES_IT* pr_it, GenericVector<bool>* word_wanted,
1032  GenericVector<C_BLOB*>* target_blobs) {
1033  GenericVector<bool> blob_wanted;
1034  word_wanted->init_to_size(outlines.size(), false);
1035  target_blobs->init_to_size(outlines.size(), NULL);
1036  // Check for outlines that need to be turned into stand-alone blobs.
1037  for (int i = 0; i < outlines.size(); ++i) {
1038  if (outlines[i] == NULL) continue;
1039  // Get a set of adjacent outlines that don't overlap any existing blob.
1040  blob_wanted.init_to_size(outlines.size(), false);
1041  int num_blob_outlines = 0;
1042  TBOX total_ol_box(outlines[i]->bounding_box());
1043  while (i < outlines.size() && outlines[i] != NULL) {
1044  blob_wanted[i] = true;
1045  total_ol_box += outlines[i]->bounding_box();
1046  ++i;
1047  ++num_blob_outlines;
1048  }
1049  // Find the insertion point.
1050  C_BLOB_IT blob_it(real_word->cblob_list());
1051  while (!blob_it.at_last() &&
1052  blob_it.data_relative(1)->bounding_box().left() <=
1053  total_ol_box.left()) {
1054  blob_it.forward();
1055  }
1056  // Choose which combination of them we actually want and where to put
1057  // them.
1058  if (debug_noise_removal)
1059  tprintf("Num blobless outlines = %d\n", num_blob_outlines);
1060  C_BLOB* left_blob = blob_it.data();
1061  TBOX left_box = left_blob->bounding_box();
1062  C_BLOB* right_blob = blob_it.at_last() ? NULL : blob_it.data_relative(1);
1063  if ((left_box.x_overlap(total_ol_box) || right_blob == NULL ||
1064  !right_blob->bounding_box().x_overlap(total_ol_box)) &&
1065  SelectGoodDiacriticOutlines(pass, noise_cert_disjoint, pr_it, left_blob,
1066  outlines, num_blob_outlines,
1067  &blob_wanted)) {
1068  if (debug_noise_removal) tprintf("Added to left blob\n");
1069  for (int j = 0; j < blob_wanted.size(); ++j) {
1070  if (blob_wanted[j]) {
1071  (*word_wanted)[j] = true;
1072  (*target_blobs)[j] = left_blob;
1073  }
1074  }
1075  } else if (right_blob != NULL &&
1076  (!left_box.x_overlap(total_ol_box) ||
1077  right_blob->bounding_box().x_overlap(total_ol_box)) &&
1079  right_blob, outlines,
1080  num_blob_outlines, &blob_wanted)) {
1081  if (debug_noise_removal) tprintf("Added to right blob\n");
1082  for (int j = 0; j < blob_wanted.size(); ++j) {
1083  if (blob_wanted[j]) {
1084  (*word_wanted)[j] = true;
1085  (*target_blobs)[j] = right_blob;
1086  }
1087  }
1088  } else if (SelectGoodDiacriticOutlines(pass, noise_cert_punc, pr_it, NULL,
1089  outlines, num_blob_outlines,
1090  &blob_wanted)) {
1091  if (debug_noise_removal) tprintf("Fitted between blobs\n");
1092  for (int j = 0; j < blob_wanted.size(); ++j) {
1093  if (blob_wanted[j]) {
1094  (*word_wanted)[j] = true;
1095  (*target_blobs)[j] = NULL;
1096  }
1097  }
1098  }
1099  }
1100 }
1101 
1102 // Starting with ok_outlines set to indicate which outlines overlap the blob,
1103 // chooses the optimal set (approximately) and returns true if any outlines
1104 // are desired, in which case ok_outlines indicates which ones.
1106  int pass, float certainty_threshold, PAGE_RES_IT* pr_it, C_BLOB* blob,
1107  const GenericVector<C_OUTLINE*>& outlines, int num_outlines,
1108  GenericVector<bool>* ok_outlines) {
1109  STRING best_str;
1110  float target_cert = certainty_threshold;
1111  if (blob != NULL) {
1112  float target_c2;
1113  target_cert = ClassifyBlobAsWord(pass, pr_it, blob, &best_str, &target_c2);
1114  if (debug_noise_removal) {
1115  tprintf("No Noise blob classified as %s=%g(%g) at:", best_str.string(),
1116  target_cert, target_c2);
1117  blob->bounding_box().print();
1118  }
1119  target_cert -= (target_cert - certainty_threshold) * noise_cert_factor;
1120  }
1121  GenericVector<bool> test_outlines = *ok_outlines;
1122  // Start with all the outlines in.
1123  STRING all_str;
1124  GenericVector<bool> best_outlines = *ok_outlines;
1125  float best_cert = ClassifyBlobPlusOutlines(test_outlines, outlines, pass,
1126  pr_it, blob, &all_str);
1127  if (debug_noise_removal) {
1128  TBOX ol_box;
1129  for (int i = 0; i < test_outlines.size(); ++i) {
1130  if (test_outlines[i]) ol_box += outlines[i]->bounding_box();
1131  }
1132  tprintf("All Noise blob classified as %s=%g, delta=%g at:",
1133  all_str.string(), best_cert, best_cert - target_cert);
1134  ol_box.print();
1135  }
1136  // Iteratively zero out the bit that improves the certainty the most, until
1137  // we get past the threshold, have zero bits, or fail to improve.
1138  int best_index = 0; // To zero out.
1139  while (num_outlines > 1 && best_index >= 0 &&
1140  (blob == NULL || best_cert < target_cert || blob != NULL)) {
1141  // Find the best bit to zero out.
1142  best_index = -1;
1143  for (int i = 0; i < outlines.size(); ++i) {
1144  if (test_outlines[i]) {
1145  test_outlines[i] = false;
1146  STRING str;
1147  float cert = ClassifyBlobPlusOutlines(test_outlines, outlines, pass,
1148  pr_it, blob, &str);
1149  if (debug_noise_removal) {
1150  TBOX ol_box;
1151  for (int j = 0; j < outlines.size(); ++j) {
1152  if (test_outlines[j]) ol_box += outlines[j]->bounding_box();
1153  tprintf("%d", test_outlines[j]);
1154  }
1155  tprintf(" blob classified as %s=%g, delta=%g) at:", str.string(),
1156  cert, cert - target_cert);
1157  ol_box.print();
1158  }
1159  if (cert > best_cert) {
1160  best_cert = cert;
1161  best_index = i;
1162  best_outlines = test_outlines;
1163  }
1164  test_outlines[i] = true;
1165  }
1166  }
1167  if (best_index >= 0) {
1168  test_outlines[best_index] = false;
1169  --num_outlines;
1170  }
1171  }
1172  if (best_cert >= target_cert) {
1173  // Save the best combination.
1174  *ok_outlines = best_outlines;
1175  if (debug_noise_removal) {
1176  tprintf("%s noise combination ", blob ? "Adding" : "New");
1177  for (int i = 0; i < best_outlines.size(); ++i) {
1178  tprintf("%d", best_outlines[i]);
1179  }
1180  tprintf(" yields certainty %g, beating target of %g\n", best_cert,
1181  target_cert);
1182  }
1183  return true;
1184  }
1185  return false;
1186 }
1187 
1188 // Classifies the given blob plus the outlines flagged by ok_outlines, undoes
1189 // the inclusion of the outlines, and returns the certainty of the raw choice.
1191  const GenericVector<bool>& ok_outlines,
1192  const GenericVector<C_OUTLINE*>& outlines, int pass_n, PAGE_RES_IT* pr_it,
1193  C_BLOB* blob, STRING* best_str) {
1194  C_OUTLINE_IT ol_it;
1195  C_OUTLINE* first_to_keep = NULL;
1196  if (blob != NULL) {
1197  // Add the required outlines to the blob.
1198  ol_it.set_to_list(blob->out_list());
1199  first_to_keep = ol_it.data();
1200  }
1201  for (int i = 0; i < ok_outlines.size(); ++i) {
1202  if (ok_outlines[i]) {
1203  // This outline is to be added.
1204  if (blob == NULL) {
1205  blob = new C_BLOB(outlines[i]);
1206  ol_it.set_to_list(blob->out_list());
1207  } else {
1208  ol_it.add_before_stay_put(outlines[i]);
1209  }
1210  }
1211  }
1212  float c2;
1213  float cert = ClassifyBlobAsWord(pass_n, pr_it, blob, best_str, &c2);
1214  ol_it.move_to_first();
1215  if (first_to_keep == NULL) {
1216  // We created blob. Empty its outlines and delete it.
1217  for (; !ol_it.empty(); ol_it.forward()) ol_it.extract();
1218  delete blob;
1219  cert = -c2;
1220  } else {
1221  // Remove the outlines that we put in.
1222  for (; ol_it.data() != first_to_keep; ol_it.forward()) {
1223  ol_it.extract();
1224  }
1225  }
1226  return cert;
1227 }
1228 
1229 // Classifies the given blob (part of word_data->word->word) as an individual
1230 // word, using languages, chopper etc, returning only the certainty of the
1231 // best raw choice, and undoing all the work done to fake out the word.
1233  C_BLOB* blob, STRING* best_str, float* c2) {
1234  WERD* real_word = pr_it->word()->word;
1235  WERD* word = real_word->ConstructFromSingleBlob(
1236  real_word->flag(W_BOL), real_word->flag(W_EOL), C_BLOB::deep_copy(blob));
1237  WERD_RES* word_res = pr_it->InsertSimpleCloneWord(*pr_it->word(), word);
1238  // Get a new iterator that points to the new word.
1239  PAGE_RES_IT it(pr_it->page_res);
1240  while (it.word() != word_res && it.word() != NULL) it.forward();
1241  ASSERT_HOST(it.word() == word_res);
1242  WordData wd(it);
1243  // Force full initialization.
1244  SetupWordPassN(1, &wd);
1245  classify_word_and_language(pass_n, &it, &wd);
1246  if (debug_noise_removal) {
1247  tprintf("word xheight=%g, row=%g, range=[%g,%g]\n", word_res->x_height,
1248  wd.row->x_height(), wd.word->raw_choice->min_x_height(),
1249  wd.word->raw_choice->max_x_height());
1250  }
1251  float cert = wd.word->raw_choice->certainty();
1252  float rat = wd.word->raw_choice->rating();
1253  *c2 = rat > 0.0f ? cert * cert / rat : 0.0f;
1254  *best_str = wd.word->raw_choice->unichar_string();
1255  it.DeleteCurrentWord();
1256  pr_it->ResetWordIterator();
1257  return cert;
1258 }
1259 
1260 // Generic function for classifying a word. Can be used either for pass1 or
1261 // pass2 according to the function passed to recognizer.
1262 // word_data holds the word to be recognized, and its block and row, and
1263 // pr_it points to the word as well, in case we are running LSTM and it wants
1264 // to output multiple words.
1265 // Recognizes in the current language, and if successful that is all.
1266 // If recognition was not successful, tries all available languages until
1267 // it gets a successful result or runs out of languages. Keeps the best result.
1269  WordData* word_data) {
1270  WordRecognizer recognizer = pass_n == 1 ? &Tesseract::classify_word_pass1
1272  // Best result so far.
1273  PointerVector<WERD_RES> best_words;
1274  // Points to the best result. May be word or in lang_words.
1275  WERD_RES* word = word_data->word;
1276  clock_t start_t = clock();
1278  tprintf("%s word with lang %s at:",
1279  word->done ? "Already done" : "Processing",
1280  most_recently_used_->lang.string());
1281  word->word->bounding_box().print();
1282  }
1283  if (word->done) {
1284  // If done on pass1, leave it as-is.
1285  if (!word->tess_failed)
1286  most_recently_used_ = word->tesseract;
1287  return;
1288  }
1289  int sub = sub_langs_.size();
1290  if (most_recently_used_ != this) {
1291  // Get the index of the most_recently_used_.
1292  for (sub = 0; sub < sub_langs_.size() &&
1293  most_recently_used_ != sub_langs_[sub]; ++sub) {}
1294  }
1295  most_recently_used_->RetryWithLanguage(
1296  *word_data, recognizer, &word_data->lang_words[sub], &best_words);
1297  Tesseract* best_lang_tess = most_recently_used_;
1298  if (!WordsAcceptable(best_words)) {
1299  // Try all the other languages to see if they are any better.
1300  if (most_recently_used_ != this &&
1301  this->RetryWithLanguage(*word_data, recognizer,
1302  &word_data->lang_words[sub_langs_.size()],
1303  &best_words) > 0) {
1304  best_lang_tess = this;
1305  }
1306  for (int i = 0; !WordsAcceptable(best_words) && i < sub_langs_.size();
1307  ++i) {
1308  if (most_recently_used_ != sub_langs_[i] &&
1309  sub_langs_[i]->RetryWithLanguage(*word_data, recognizer,
1310  &word_data->lang_words[i],
1311  &best_words) > 0) {
1312  best_lang_tess = sub_langs_[i];
1313  }
1314  }
1315  }
1316  most_recently_used_ = best_lang_tess;
1317  if (!best_words.empty()) {
1318  if (best_words.size() == 1 && !best_words[0]->combination) {
1319  // Move the best single result to the main word.
1320  word_data->word->ConsumeWordResults(best_words[0]);
1321  } else {
1322  // Words came from LSTM, and must be moved to the PAGE_RES properly.
1323  word_data->word = best_words.back();
1324  pr_it->ReplaceCurrentWord(&best_words);
1325  }
1326  ASSERT_HOST(word_data->word->box_word != NULL);
1327  } else {
1328  tprintf("no best words!!\n");
1329  }
1330  clock_t ocr_t = clock();
1331  if (tessedit_timing_debug) {
1332  tprintf("%s (ocr took %.2f sec)\n",
1333  word->best_choice->unichar_string().string(),
1334  static_cast<double>(ocr_t-start_t)/CLOCKS_PER_SEC);
1335  }
1336 }
1337 
1345  WERD_RES** in_word,
1346  PointerVector<WERD_RES>* out_words) {
1347  ROW* row = word_data.row;
1348  BLOCK* block = word_data.block;
1349  prev_word_best_choice_ = word_data.prev_word != NULL
1350  ? word_data.prev_word->word->best_choice : NULL;
1351 #ifndef ANDROID_BUILD
1352  // If we only intend to run cube - run it and return.
1354  cube_word_pass1(block, row, *in_word);
1355  return;
1356  }
1357 #endif
1358  WERD_RES* word = *in_word;
1359  match_word_pass_n(1, word, row, block);
1360  if (!word->tess_failed && !word->word->flag(W_REP_CHAR)) {
1361  word->tess_would_adapt = AdaptableWord(word);
1362  bool adapt_ok = word_adaptable(word, tessedit_tess_adaption_mode);
1363 
1364  if (adapt_ok) {
1365  // Send word to adaptive classifier for training.
1366  word->BestChoiceToCorrectText();
1367  LearnWord(NULL, word);
1368  // Mark misadaptions if running blamer.
1369  if (word->blamer_bundle != NULL) {
1372  }
1373  }
1374 
1375  if (tessedit_enable_doc_dict && !word->IsAmbiguous())
1377  }
1378 }
1379 
1380 // Helper to report the result of the xheight fix.
1381 void Tesseract::ReportXhtFixResult(bool accept_new_word, float new_x_ht,
1382  WERD_RES* word, WERD_RES* new_word) {
1383  tprintf("New XHT Match:%s = %s ",
1384  word->best_choice->unichar_string().string(),
1385  word->best_choice->debug_string().string());
1386  word->reject_map.print(debug_fp);
1387  tprintf(" -> %s = %s ",
1388  new_word->best_choice->unichar_string().string(),
1389  new_word->best_choice->debug_string().string());
1390  new_word->reject_map.print(debug_fp);
1391  tprintf(" %s->%s %s %s\n",
1392  word->guessed_x_ht ? "GUESS" : "CERT",
1393  new_word->guessed_x_ht ? "GUESS" : "CERT",
1394  new_x_ht > 0.1 ? "STILL DOUBT" : "OK",
1395  accept_new_word ? "ACCEPTED" : "");
1396 }
1397 
1398 // Run the x-height fix-up, based on min/max top/bottom information in
1399 // unicharset.
1400 // Returns true if the word was changed.
1401 // See the comment in fixxht.cpp for a description of the overall process.
1402 bool Tesseract::TrainedXheightFix(WERD_RES *word, BLOCK* block, ROW *row) {
1403  bool accept_new_x_ht = false;
1404  int original_misfits = CountMisfitTops(word);
1405  if (original_misfits == 0)
1406  return false;
1407  float baseline_shift = 0.0f;
1408  float new_x_ht = ComputeCompatibleXheight(word, &baseline_shift);
1409  if (baseline_shift != 0.0f) {
1410  // Try the shift on its own first.
1411  if (!TestNewNormalization(original_misfits, baseline_shift, word->x_height,
1412  word, block, row))
1413  return false;
1414  original_misfits = CountMisfitTops(word);
1415  if (original_misfits > 0) {
1416  float new_baseline_shift;
1417  // Now recompute the new x_height.
1418  new_x_ht = ComputeCompatibleXheight(word, &new_baseline_shift);
1419  if (new_x_ht >= kMinRefitXHeightFraction * word->x_height) {
1420  // No test of return value here, as we are definitely making a change
1421  // to the word by shifting the baseline.
1422  TestNewNormalization(original_misfits, baseline_shift, new_x_ht,
1423  word, block, row);
1424  }
1425  }
1426  return true;
1427  } else if (new_x_ht >= kMinRefitXHeightFraction * word->x_height) {
1428  return TestNewNormalization(original_misfits, 0.0f, new_x_ht,
1429  word, block, row);
1430  } else {
1431  return false;
1432  }
1433 }
1434 
1435 // Runs recognition with the test baseline shift and x-height and returns true
1436 // if there was an improvement in recognition result.
1437 bool Tesseract::TestNewNormalization(int original_misfits,
1438  float baseline_shift, float new_x_ht,
1439  WERD_RES *word, BLOCK* block, ROW *row) {
1440  bool accept_new_x_ht = false;
1441  WERD_RES new_x_ht_word(word->word);
1442  if (word->blamer_bundle != NULL) {
1443  new_x_ht_word.blamer_bundle = new BlamerBundle();
1444  new_x_ht_word.blamer_bundle->CopyTruth(*(word->blamer_bundle));
1445  }
1446  new_x_ht_word.x_height = new_x_ht;
1447  new_x_ht_word.baseline_shift = baseline_shift;
1448  new_x_ht_word.caps_height = 0.0;
1449  new_x_ht_word.SetupForRecognition(
1452  poly_allow_detailed_fx, row, block);
1453  match_word_pass_n(2, &new_x_ht_word, row, block);
1454  if (!new_x_ht_word.tess_failed) {
1455  int new_misfits = CountMisfitTops(&new_x_ht_word);
1456  if (debug_x_ht_level >= 1) {
1457  tprintf("Old misfits=%d with x-height %f, new=%d with x-height %f\n",
1458  original_misfits, word->x_height,
1459  new_misfits, new_x_ht);
1460  tprintf("Old rating= %f, certainty=%f, new=%f, %f\n",
1461  word->best_choice->rating(), word->best_choice->certainty(),
1462  new_x_ht_word.best_choice->rating(),
1463  new_x_ht_word.best_choice->certainty());
1464  }
1465  // The misfits must improve and either the rating or certainty.
1466  accept_new_x_ht = new_misfits < original_misfits &&
1467  (new_x_ht_word.best_choice->certainty() >
1468  word->best_choice->certainty() ||
1469  new_x_ht_word.best_choice->rating() <
1470  word->best_choice->rating());
1471  if (debug_x_ht_level >= 1) {
1472  ReportXhtFixResult(accept_new_x_ht, new_x_ht, word, &new_x_ht_word);
1473  }
1474  }
1475  if (accept_new_x_ht) {
1476  word->ConsumeWordResults(&new_x_ht_word);
1477  return true;
1478  }
1479  return false;
1480 }
1481 
1489  WERD_RES** in_word,
1490  PointerVector<WERD_RES>* out_words) {
1491  // Return if we do not want to run Tesseract.
1494  word_data.word->best_choice != NULL)
1495  return;
1497  return;
1498  }
1499  ROW* row = word_data.row;
1500  BLOCK* block = word_data.block;
1501  WERD_RES* word = *in_word;
1502  prev_word_best_choice_ = word_data.prev_word != NULL
1503  ? word_data.prev_word->word->best_choice : NULL;
1504 
1506  check_debug_pt(word, 30);
1507  if (!word->done) {
1508  word->caps_height = 0.0;
1509  if (word->x_height == 0.0f)
1510  word->x_height = row->x_height();
1511  match_word_pass_n(2, word, row, block);
1512  check_debug_pt(word, 40);
1513  }
1514 
1515  SubAndSuperscriptFix(word);
1516 
1517  if (!word->tess_failed && !word->word->flag(W_REP_CHAR)) {
1519  block->classify_rotation().y() == 0.0f) {
1520  // Use the tops and bottoms since they are available.
1521  TrainedXheightFix(word, block, row);
1522  }
1523 
1525  }
1526 #ifndef GRAPHICS_DISABLED
1528  if (fx_win == NULL)
1529  create_fx_win();
1530  clear_fx_win();
1531  word->rebuild_word->plot(fx_win);
1532  TBOX wbox = word->rebuild_word->bounding_box();
1533  fx_win->ZoomToRectangle(wbox.left(), wbox.top(),
1534  wbox.right(), wbox.bottom());
1536  }
1537 #endif
1539  check_debug_pt(word, 50);
1540 }
1541 
1542 
1550  ROW *row, BLOCK* block) {
1551  if (word->tess_failed) return;
1552  tess_segment_pass_n(pass_n, word);
1553 
1554  if (!word->tess_failed) {
1555  if (!word->word->flag (W_REP_CHAR)) {
1556  word->fix_quotes();
1558  word->fix_hyphens();
1559  /* Dont trust fix_quotes! - though I think I've fixed the bug */
1560  if (word->best_choice->length() != word->box_word->length()) {
1561  tprintf("POST FIX_QUOTES FAIL String:\"%s\"; Strlen=%d;"
1562  " #Blobs=%d\n",
1563  word->best_choice->debug_string().string(),
1564  word->best_choice->length(),
1565  word->box_word->length());
1566 
1567  }
1568  word->tess_accepted = tess_acceptable_word(word);
1569 
1570  // Also sets word->done flag
1571  make_reject_map(word, row, pass_n);
1572  }
1573  }
1574  set_word_fonts(word);
1575 
1576  ASSERT_HOST(word->raw_choice != NULL);
1577 }
1578 
1579 // Helper to return the best rated BLOB_CHOICE in the whole word that matches
1580 // the given char_id, or NULL if none can be found.
1581 static BLOB_CHOICE* FindBestMatchingChoice(UNICHAR_ID char_id,
1582  WERD_RES* word_res) {
1583  // Find the corresponding best BLOB_CHOICE from any position in the word_res.
1584  BLOB_CHOICE* best_choice = NULL;
1585  for (int i = 0; i < word_res->best_choice->length(); ++i) {
1586  BLOB_CHOICE* choice = FindMatchingChoice(char_id,
1587  word_res->GetBlobChoices(i));
1588  if (choice != NULL) {
1589  if (best_choice == NULL || choice->rating() < best_choice->rating())
1590  best_choice = choice;
1591  }
1592  }
1593  return best_choice;
1594 }
1595 
1596 // Helper to insert blob_choice in each location in the leader word if there is
1597 // no matching BLOB_CHOICE there already, and correct any incorrect results
1598 // in the best_choice.
1599 static void CorrectRepcharChoices(BLOB_CHOICE* blob_choice,
1600  WERD_RES* word_res) {
1601  WERD_CHOICE* word = word_res->best_choice;
1602  for (int i = 0; i < word_res->best_choice->length(); ++i) {
1603  BLOB_CHOICE* choice = FindMatchingChoice(blob_choice->unichar_id(),
1604  word_res->GetBlobChoices(i));
1605  if (choice == NULL) {
1606  BLOB_CHOICE_IT choice_it(word_res->GetBlobChoices(i));
1607  choice_it.add_before_stay_put(new BLOB_CHOICE(*blob_choice));
1608  }
1609  }
1610  // Correct any incorrect results in word.
1611  for (int i = 0; i < word->length(); ++i) {
1612  if (word->unichar_id(i) != blob_choice->unichar_id())
1613  word->set_unichar_id(blob_choice->unichar_id(), i);
1614  }
1615 }
1616 
1625  WERD_RES *word_res = page_res_it->word();
1626  const WERD_CHOICE &word = *(word_res->best_choice);
1627 
1628  // Find the frequency of each unique character in the word.
1629  SortHelper<UNICHAR_ID> rep_ch(word.length());
1630  for (int i = 0; i < word.length(); ++i) {
1631  rep_ch.Add(word.unichar_id(i), 1);
1632  }
1633 
1634  // Find the most frequent result.
1635  UNICHAR_ID maxch_id = INVALID_UNICHAR_ID; // most common char
1636  int max_count = rep_ch.MaxCount(&maxch_id);
1637  // Find the best exemplar of a classifier result for maxch_id.
1638  BLOB_CHOICE* best_choice = FindBestMatchingChoice(maxch_id, word_res);
1639  if (best_choice == NULL) {
1640  tprintf("Failed to find a choice for %s, occurring %d times\n",
1641  word_res->uch_set->debug_str(maxch_id).string(), max_count);
1642  return;
1643  }
1644  word_res->done = TRUE;
1645 
1646  // Measure the mean space.
1647  int gap_count = 0;
1648  WERD* werd = word_res->word;
1649  C_BLOB_IT blob_it(werd->cblob_list());
1650  C_BLOB* prev_blob = blob_it.data();
1651  for (blob_it.forward(); !blob_it.at_first(); blob_it.forward()) {
1652  C_BLOB* blob = blob_it.data();
1653  int gap = blob->bounding_box().left();
1654  gap -= prev_blob->bounding_box().right();
1655  ++gap_count;
1656  prev_blob = blob;
1657  }
1658  // Just correct existing classification.
1659  CorrectRepcharChoices(best_choice, word_res);
1660  word_res->reject_map.initialise(word.length());
1661 }
1662 
1664  const UNICHARSET& char_set, const char *s, const char *lengths) {
1665  int i = 0;
1666  int offset = 0;
1667  int leading_punct_count;
1668  int upper_count = 0;
1669  int hyphen_pos = -1;
1671 
1672  if (strlen (lengths) > 20)
1673  return word_type;
1674 
1675  /* Single Leading punctuation char*/
1676 
1677  if (s[offset] != '\0' && STRING(chs_leading_punct).contains(s[offset]))
1678  offset += lengths[i++];
1679  leading_punct_count = i;
1680 
1681  /* Initial cap */
1682  while (s[offset] != '\0' && char_set.get_isupper(s + offset, lengths[i])) {
1683  offset += lengths[i++];
1684  upper_count++;
1685  }
1686  if (upper_count > 1) {
1687  word_type = AC_UPPER_CASE;
1688  } else {
1689  /* Lower case word, possibly with an initial cap */
1690  while (s[offset] != '\0' && char_set.get_islower(s + offset, lengths[i])) {
1691  offset += lengths[i++];
1692  }
1693  if (i - leading_punct_count < quality_min_initial_alphas_reqd)
1694  goto not_a_word;
1695  /*
1696  Allow a single hyphen in a lower case word
1697  - dont trust upper case - I've seen several cases of "H" -> "I-I"
1698  */
1699  if (lengths[i] == 1 && s[offset] == '-') {
1700  hyphen_pos = i;
1701  offset += lengths[i++];
1702  if (s[offset] != '\0') {
1703  while ((s[offset] != '\0') &&
1704  char_set.get_islower(s + offset, lengths[i])) {
1705  offset += lengths[i++];
1706  }
1707  if (i < hyphen_pos + 3)
1708  goto not_a_word;
1709  }
1710  } else {
1711  /* Allow "'s" in NON hyphenated lower case words */
1712  if (lengths[i] == 1 && (s[offset] == '\'') &&
1713  lengths[i + 1] == 1 && (s[offset + lengths[i]] == 's')) {
1714  offset += lengths[i++];
1715  offset += lengths[i++];
1716  }
1717  }
1718  if (upper_count > 0)
1719  word_type = AC_INITIAL_CAP;
1720  else
1721  word_type = AC_LOWER_CASE;
1722  }
1723 
1724  /* Up to two different, constrained trailing punctuation chars */
1725  if (lengths[i] == 1 && s[offset] != '\0' &&
1726  STRING(chs_trailing_punct1).contains(s[offset]))
1727  offset += lengths[i++];
1728  if (lengths[i] == 1 && s[offset] != '\0' && i > 0 &&
1729  s[offset - lengths[i - 1]] != s[offset] &&
1730  STRING(chs_trailing_punct2).contains (s[offset]))
1731  offset += lengths[i++];
1732 
1733  if (s[offset] != '\0')
1734  word_type = AC_UNACCEPTABLE;
1735 
1736  not_a_word:
1737 
1738  if (word_type == AC_UNACCEPTABLE) {
1739  /* Look for abbreviation string */
1740  i = 0;
1741  offset = 0;
1742  if (s[0] != '\0' && char_set.get_isupper(s, lengths[0])) {
1743  word_type = AC_UC_ABBREV;
1744  while (s[offset] != '\0' &&
1745  char_set.get_isupper(s + offset, lengths[i]) &&
1746  lengths[i + 1] == 1 && s[offset + lengths[i]] == '.') {
1747  offset += lengths[i++];
1748  offset += lengths[i++];
1749  }
1750  }
1751  else if (s[0] != '\0' && char_set.get_islower(s, lengths[0])) {
1752  word_type = AC_LC_ABBREV;
1753  while (s[offset] != '\0' &&
1754  char_set.get_islower(s + offset, lengths[i]) &&
1755  lengths[i + 1] == 1 && s[offset + lengths[i]] == '.') {
1756  offset += lengths[i++];
1757  offset += lengths[i++];
1758  }
1759  }
1760  if (s[offset] != '\0')
1761  word_type = AC_UNACCEPTABLE;
1762  }
1763 
1764  return word_type;
1765 }
1766 
1768  BOOL8 show_map_detail = FALSE;
1769  inT16 i;
1770 
1771  if (!test_pt)
1772  return FALSE;
1773 
1774  tessedit_rejection_debug.set_value (FALSE);
1775  debug_x_ht_level.set_value(0);
1776 
1777  if (word->word->bounding_box ().contains (FCOORD (test_pt_x, test_pt_y))) {
1778  if (location < 0)
1779  return TRUE; // For breakpoint use
1780  tessedit_rejection_debug.set_value (TRUE);
1781  debug_x_ht_level.set_value(2);
1782  tprintf ("\n\nTESTWD::");
1783  switch (location) {
1784  case 0:
1785  tprintf ("classify_word_pass1 start\n");
1786  word->word->print();
1787  break;
1788  case 10:
1789  tprintf ("make_reject_map: initial map");
1790  break;
1791  case 20:
1792  tprintf ("make_reject_map: after NN");
1793  break;
1794  case 30:
1795  tprintf ("classify_word_pass2 - START");
1796  break;
1797  case 40:
1798  tprintf ("classify_word_pass2 - Pre Xht");
1799  break;
1800  case 50:
1801  tprintf ("classify_word_pass2 - END");
1802  show_map_detail = TRUE;
1803  break;
1804  case 60:
1805  tprintf ("fixspace");
1806  break;
1807  case 70:
1808  tprintf ("MM pass START");
1809  break;
1810  case 80:
1811  tprintf ("MM pass END");
1812  break;
1813  case 90:
1814  tprintf ("After Poor quality rejection");
1815  break;
1816  case 100:
1817  tprintf ("unrej_good_quality_words - START");
1818  break;
1819  case 110:
1820  tprintf ("unrej_good_quality_words - END");
1821  break;
1822  case 120:
1823  tprintf ("Write results pass");
1824  show_map_detail = TRUE;
1825  break;
1826  }
1827  if (word->best_choice != NULL) {
1828  tprintf(" \"%s\" ", word->best_choice->unichar_string().string());
1829  word->reject_map.print(debug_fp);
1830  tprintf("\n");
1831  if (show_map_detail) {
1832  tprintf("\"%s\"\n", word->best_choice->unichar_string().string());
1833  for (i = 0; word->best_choice->unichar_string()[i] != '\0'; i++) {
1834  tprintf("**** \"%c\" ****\n", word->best_choice->unichar_string()[i]);
1835  word->reject_map[i].full_print(debug_fp);
1836  }
1837  }
1838  } else {
1839  tprintf("null best choice\n");
1840  }
1841  tprintf ("Tess Accepted: %s\n", word->tess_accepted ? "TRUE" : "FALSE");
1842  tprintf ("Done flag: %s\n\n", word->done ? "TRUE" : "FALSE");
1843  return TRUE;
1844  } else {
1845  return FALSE;
1846  }
1847 }
1848 
1854 static void find_modal_font( //good chars in word
1855  STATS *fonts, //font stats
1856  inT16 *font_out, //output font
1857  inT8 *font_count //output count
1858  ) {
1859  inT16 font; //font index
1860  inT32 count; //pile couat
1861 
1862  if (fonts->get_total () > 0) {
1863  font = (inT16) fonts->mode ();
1864  *font_out = font;
1865  count = fonts->pile_count (font);
1866  *font_count = count < MAX_INT8 ? count : MAX_INT8;
1867  fonts->add (font, -*font_count);
1868  }
1869  else {
1870  *font_out = -1;
1871  *font_count = 0;
1872  }
1873 }
1874 
1881  // Don't try to set the word fonts for a cube word, as the configs
1882  // will be meaningless.
1883  if (word->chopped_word == NULL) return;
1884  ASSERT_HOST(word->best_choice != NULL);
1885 
1886  int fontinfo_size = get_fontinfo_table().size();
1887  if (fontinfo_size == 0) return;
1888  GenericVector<int> font_total_score;
1889  font_total_score.init_to_size(fontinfo_size, 0);
1890 
1891  word->italic = 0;
1892  word->bold = 0;
1893  // Compute the font scores for the word
1894  if (tessedit_debug_fonts) {
1895  tprintf("Examining fonts in %s\n",
1896  word->best_choice->debug_string().string());
1897  }
1898  for (int b = 0; b < word->best_choice->length(); ++b) {
1899  BLOB_CHOICE* choice = word->GetBlobChoice(b);
1900  if (choice == NULL) continue;
1901  const GenericVector<ScoredFont>& fonts = choice->fonts();
1902  for (int f = 0; f < fonts.size(); ++f) {
1903  int fontinfo_id = fonts[f].fontinfo_id;
1904  if (0 <= fontinfo_id && fontinfo_id < fontinfo_size) {
1905  font_total_score[fontinfo_id] += fonts[f].score;
1906  }
1907  }
1908  }
1909  // Find the top and 2nd choice for the word.
1910  int score1 = 0, score2 = 0;
1911  inT16 font_id1 = -1, font_id2 = -1;
1912  for (int f = 0; f < fontinfo_size; ++f) {
1913  if (tessedit_debug_fonts && font_total_score[f] > 0) {
1914  tprintf("Font %s, total score = %d\n",
1915  fontinfo_table_.get(f).name, font_total_score[f]);
1916  }
1917  if (font_total_score[f] > score1) {
1918  score2 = score1;
1919  font_id2 = font_id1;
1920  score1 = font_total_score[f];
1921  font_id1 = f;
1922  } else if (font_total_score[f] > score2) {
1923  score2 = font_total_score[f];
1924  font_id2 = f;
1925  }
1926  }
1927  word->fontinfo = font_id1 >= 0 ? &fontinfo_table_.get(font_id1) : NULL;
1928  word->fontinfo2 = font_id2 >= 0 ? &fontinfo_table_.get(font_id2) : NULL;
1929  // Each score has a limit of MAX_UINT16, so divide by that to get the number
1930  // of "votes" for that font, ie number of perfect scores.
1931  word->fontinfo_id_count = ClipToRange(score1 / MAX_UINT16, 1, MAX_INT8);
1932  word->fontinfo_id2_count = ClipToRange(score2 / MAX_UINT16, 0, MAX_INT8);
1933  if (score1 > 0) {
1934  FontInfo fi = fontinfo_table_.get(font_id1);
1935  if (tessedit_debug_fonts) {
1936  if (word->fontinfo_id2_count > 0) {
1937  tprintf("Word modal font=%s, score=%d, 2nd choice %s/%d\n",
1938  fi.name, word->fontinfo_id_count,
1939  fontinfo_table_.get(font_id2).name,
1940  word->fontinfo_id2_count);
1941  } else {
1942  tprintf("Word modal font=%s, score=%d. No 2nd choice\n",
1943  fi.name, word->fontinfo_id_count);
1944  }
1945  }
1946  word->italic = (fi.is_italic() ? 1 : -1) * word->fontinfo_id_count;
1947  word->bold = (fi.is_bold() ? 1 : -1) * word->fontinfo_id_count;
1948  }
1949 }
1950 
1951 
1959  PAGE_RES_IT page_res_it(page_res);
1960  WERD_RES *word; // current word
1961  STATS doc_fonts(0, font_table_size_); // font counters
1962 
1963  // Gather font id statistics.
1964  for (page_res_it.restart_page(); page_res_it.word() != NULL;
1965  page_res_it.forward()) {
1966  word = page_res_it.word();
1967  if (word->fontinfo != NULL) {
1968  doc_fonts.add(word->fontinfo->universal_id, word->fontinfo_id_count);
1969  }
1970  if (word->fontinfo2 != NULL) {
1971  doc_fonts.add(word->fontinfo2->universal_id, word->fontinfo_id2_count);
1972  }
1973  }
1974  inT16 doc_font; // modal font
1975  inT8 doc_font_count; // modal font
1976  find_modal_font(&doc_fonts, &doc_font, &doc_font_count);
1977  if (doc_font_count == 0)
1978  return;
1979  // Get the modal font pointer.
1980  const FontInfo* modal_font = NULL;
1981  for (page_res_it.restart_page(); page_res_it.word() != NULL;
1982  page_res_it.forward()) {
1983  word = page_res_it.word();
1984  if (word->fontinfo != NULL && word->fontinfo->universal_id == doc_font) {
1985  modal_font = word->fontinfo;
1986  break;
1987  }
1988  if (word->fontinfo2 != NULL && word->fontinfo2->universal_id == doc_font) {
1989  modal_font = word->fontinfo2;
1990  break;
1991  }
1992  }
1993  ASSERT_HOST(modal_font != NULL);
1994 
1995  // Assign modal font to weak words.
1996  for (page_res_it.restart_page(); page_res_it.word() != NULL;
1997  page_res_it.forward()) {
1998  word = page_res_it.word();
1999  int length = word->best_choice->length();
2000 
2001  int count = word->fontinfo_id_count;
2002  if (!(count == length || (length > 3 && count >= length * 3 / 4))) {
2003  word->fontinfo = modal_font;
2004  // Counts only get 1 as it came from the doc.
2005  word->fontinfo_id_count = 1;
2006  word->italic = modal_font->is_italic() ? 1 : -1;
2007  word->bold = modal_font->is_bold() ? 1 : -1;
2008  }
2009  }
2010 }
2011 
2012 // If a word has multiple alternates check if the best choice is in the
2013 // dictionary. If not, replace it with an alternate that exists in the
2014 // dictionary.
2016  PAGE_RES_IT word_it(page_res);
2017  for (WERD_RES* word = word_it.word(); word != NULL;
2018  word = word_it.forward()) {
2019  if (word->best_choices.singleton())
2020  continue; // There are no alternates.
2021 
2022  WERD_CHOICE* best = word->best_choice;
2023  if (word->tesseract->getDict().valid_word(*best) != 0)
2024  continue; // The best choice is in the dictionary.
2025 
2026  WERD_CHOICE_IT choice_it(&word->best_choices);
2027  for (choice_it.mark_cycle_pt(); !choice_it.cycled_list();
2028  choice_it.forward()) {
2029  WERD_CHOICE* alternate = choice_it.data();
2030  if (word->tesseract->getDict().valid_word(*alternate)) {
2031  // The alternate choice is in the dictionary.
2032  if (tessedit_bigram_debug) {
2033  tprintf("Dictionary correction replaces best choice '%s' with '%s'\n",
2034  best->unichar_string().string(),
2035  alternate->unichar_string().string());
2036  }
2037  // Replace the 'best' choice with a better choice.
2038  word->ReplaceBestChoice(alternate);
2039  break;
2040  }
2041  }
2042  }
2043 }
2044 
2045 } // namespace tesseract
bool is_bold() const
Definition: fontinfo.h:112
BOOL8 tess_accepted
Definition: pageres.h:280
void set_global_loc_code(int loc_code)
Definition: globaloc.cpp:79
void SetScriptPositions()
Definition: pageres.cpp:853
void ConsumeWordResults(WERD_RES *word)
Definition: pageres.cpp:757
void run_cube_combiner(PAGE_RES *page_res)
inT32 get_total() const
Definition: statistc.h:86
WERD * ConstructFromSingleBlob(bool bol, bool eol, C_BLOB *blob)
Definition: werd.cpp:137
STRING debug_str(UNICHAR_ID id) const
Definition: unicharset.cpp:318
bool TrainedXheightFix(WERD_RES *word, BLOCK *block, ROW *row)
Definition: control.cpp:1402
void set_unichar_id(UNICHAR_ID unichar_id, int index)
Definition: ratngs.h:356
static C_BLOB * deep_copy(const C_BLOB *src)
Definition: stepblob.h:113
a.b.c.
Definition: control.h:40
void AssignDiacriticsToNewBlobs(const GenericVector< C_OUTLINE * > &outlines, int pass, WERD *real_word, PAGE_RES_IT *pr_it, GenericVector< bool > *word_wanted, GenericVector< C_BLOB * > *target_blobs)
Definition: control.cpp:1029
void rej_stat_word()
Definition: pageres.cpp:1673
int size() const
Definition: genericvector.h:72
WERD_CHOICE_LIST best_choices
Definition: pageres.h:227
tesseract::BoxWord * box_word
Definition: pageres.h:250
const double kRepcharGapThreshold
Definition: control.cpp:55
bool right_to_left() const
void match_word_pass_n(int pass_n, WERD_RES *word, ROW *row, BLOCK *block)
Definition: control.cpp:1549
bool classify_bln_numeric_mode
Definition: classify.h:500
BOOL8 recog_interactive(PAGE_RES_IT *pr_it)
Definition: control.cpp:84
inT16 word_outline_errs(WERD_RES *word)
Definition: docqual.cpp:77
float rating() const
Definition: ratngs.h:324
int RetryWithLanguage(const WordData &word_data, WordRecognizer recognizer, WERD_RES **in_word, PointerVector< WERD_RES > *best_words)
Definition: control.cpp:869
inT32 length() const
Definition: rejctmap.h:237
int length() const
Definition: genericvector.h:79
#define MAX(x, y)
Definition: ndminx.h:24
void ReplaceBestChoice(WERD_CHOICE *choice)
Definition: pageres.cpp:787
BOOL8 word_adaptable(WERD_RES *word, uinT16 mode)
Definition: adaptions.cpp:45
void classify_word_pass2(const WordData &word_data, WERD_RES **in_word, PointerVector< WERD_RES > *out_words)
Definition: control.cpp:1488
static const double kXHeightCapRatio
Definition: ccstruct.h:37
#define MAX_UINT16
Definition: host.h:122
void make_reject_map(WERD_RES *word, ROW *row, inT16 pass)
float ClassifyBlobPlusOutlines(const GenericVector< bool > &ok_outlines, const GenericVector< C_OUTLINE * > &outlines, int pass_n, PAGE_RES_IT *pr_it, C_BLOB *blob, STRING *best_str)
Definition: control.cpp:1190
int length() const
Definition: ratngs.h:300
WERD_CHOICE * best_choice
Definition: pageres.h:219
bool SelectGoodDiacriticOutlines(int pass, float certainty_threshold, PAGE_RES_IT *pr_it, C_BLOB *blob, const GenericVector< C_OUTLINE * > &outlines, int num_outlines, GenericVector< bool > *ok_outlines)
Definition: control.cpp:1105
void ResetWordIterator()
Definition: pageres.cpp:1532
int push_back(T object)
void rejection_passes(PAGE_RES *page_res, ETEXT_DESC *monitor, const TBOX *target_word_box, const char *word_config)
Definition: control.cpp:590
bool SubAndSuperscriptFix(WERD_RES *word_res)
REJMAP reject_map
Definition: pageres.h:271
TWERD * chopped_word
Definition: pageres.h:201
void ReplaceCurrentWord(tesseract::PointerVector< WERD_RES > *words)
Definition: pageres.cpp:1321
static void Update()
Definition: scrollview.cpp:715
bool AdaptiveClassifierIsFull() const
Definition: classify.h:284
inT32 char_count
Definition: pageres.h:60
volatile inT8 ocr_alive
Definition: ocrclass.h:117
inT8 bold
Definition: pageres.h:286
#define tprintf(...)
Definition: tprintf.h:31
#define MIN(x, y)
Definition: ndminx.h:28
bool get_isupper(UNICHAR_ID unichar_id) const
Definition: unicharset.h:463
PAGE_RES_IT * make_pseudo_word(PAGE_RES *page_res, const TBOX &selection_box)
Definition: werdit.cpp:31
Definition: statistc.h:33
float ClassifyBlobAsWord(int pass_n, PAGE_RES_IT *pr_it, C_BLOB *blob, STRING *best_str, float *c2)
Definition: control.cpp:1232
T & back() const
void * cancel_this
Definition: ocrclass.h:120
UNICHARSET unicharset
Definition: ccutil.h:72
PointerVector< WERD_RES > lang_words
#define LOC_DOC_BLK_REJ
Definition: errcode.h:53
bool ReassignDiacritics(int pass, PAGE_RES_IT *pr_it, bool *make_next_word_fuzzy)
Definition: control.cpp:910
const double kMinRefitXHeightFraction
Definition: control.cpp:58
void print() const
Definition: rect.h:270
bool AddSelectedOutlines(const GenericVector< bool > &wanted, const GenericVector< C_BLOB * > &target_blobs, const GenericVector< C_OUTLINE * > &outlines, bool *make_next_word_fuzzy)
Definition: werd.cpp:548
#define LOC_MM_ADAPT
Definition: errcode.h:52
void blamer_pass(PAGE_RES *page_res)
Definition: control.cpp:686
static void PrintParams(FILE *fp, const ParamsVectors *member_params)
Definition: params.cpp:180
void clear_fx_win()
Definition: drawfx.cpp:73
A.B.C.
Definition: control.h:41
float caps_height
Definition: pageres.h:296
inT32 mode() const
Definition: statistc.cpp:118
void output_pass(PAGE_RES_IT &page_res_it, const TBOX *target_word_box)
Definition: output.cpp:68
void add(inT32 value, inT32 count)
Definition: statistc.cpp:104
PAGE_RES * page_res
Definition: pageres.h:658
unsigned char BOOL8
Definition: host.h:113
float x_height() const
Definition: ocrrow.h:61
TBOX bounding_box() const
Definition: werd.cpp:160
void InitForRetryRecognition(const WERD_RES &source)
Definition: pageres.cpp:269
inT32 length() const
Definition: strngs.cpp:188
const FontInfo * fontinfo
Definition: pageres.h:288
static int SortByXMiddle(const void *v1, const void *v2)
Definition: stepblob.h:119
float ComputeCompatibleXheight(WERD_RES *word_res, float *baseline_shift)
Definition: fixxht.cpp:101
void fix_quotes()
Definition: pageres.cpp:1012
void ReportXhtFixResult(bool accept_new_word, float new_x_ht, WERD_RES *word, WERD_RES *new_word)
Definition: control.cpp:1381
GenericVector< int > blame_reasons
Definition: pageres.h:68
bool small_caps
Definition: pageres.h:283
UnicityTable< FontInfo > fontinfo_table_
Definition: classify.h:488
void DeleteCurrentWord()
Definition: pageres.cpp:1449
inT16 right() const
Definition: rect.h:75
BLOB_CHOICE * FindMatchingChoice(UNICHAR_ID char_id, BLOB_CHOICE_LIST *bc_list)
Definition: ratngs.cpp:160
void AssignDiacriticsToOverlappingBlobs(const GenericVector< C_OUTLINE * > &outlines, int pass, WERD *real_word, PAGE_RES_IT *pr_it, GenericVector< bool > *word_wanted, GenericVector< bool > *overlapped_any_blob, GenericVector< C_BLOB * > *target_blobs)
Definition: control.cpp:976
IncorrectResultReason incorrect_result_reason() const
Definition: blamer.h:106
float x_height
Definition: pageres.h:295
bool recog_all_words(PAGE_RES *page_res, ETEXT_DESC *monitor, const TBOX *target_word_box, const char *word_config, int dopasses)
Definition: control.cpp:287
BLOCK * block
Definition: pageres.h:99
WERD_CHOICE shallow_copy(int start, int end) const
Definition: ratngs.cpp:392
float min_x_height() const
Definition: ratngs.h:333
int CountMisfitTops(WERD_RES *word_res)
Definition: fixxht.cpp:69
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:115
BOOL8 tess_would_adapt
Definition: pageres.h:281
#define ASSERT_HOST(x)
Definition: errcode.h:84
Definition: ocrrow.h:32
void script_pos_pass(PAGE_RES *page_res)
Definition: control.cpp:710
WERD_CHOICE * prev_word_best_choice_
Definition: wordrec.h:416
const STRING & unichar_string() const
Definition: ratngs.h:524
Definition: werd.h:35
ALL upper case.
Definition: control.h:38
bool wordrec_debug_blamer
Definition: wordrec.h:167
BLOCK_RES * block() const
Definition: pageres.h:739
BOOL8 part_of_combo
Definition: pageres.h:316
C_OUTLINE_LIST * out_list()
Definition: stepblob.h:64
void plot(ScrollView *window)
Definition: blobs.cpp:918
WERD_RES * forward()
Definition: pageres.h:713
float rating() const
Definition: ratngs.h:79
void GetNonSuperscriptSpan(int *start, int *end) const
Definition: ratngs.cpp:375
void quality_based_rejection(PAGE_RES_IT &page_res_it, BOOL8 good_quality_doc)
Definition: docqual.cpp:140
void full_print(FILE *fp)
Definition: rejctmap.cpp:406
bool wordrec_run_blamer
Definition: wordrec.h:168
FCOORD classify_rotation() const
Definition: ocrblock.h:144
GenericVector< STRING > misadaption_log
Definition: pageres.h:73
inT8 fontinfo_id_count
Definition: pageres.h:290
CANCEL_FUNC cancel
Definition: ocrclass.h:119
void MakeCurrentWordFuzzy()
Definition: pageres.cpp:1482
WERD_RES * restart_page()
Definition: pageres.h:680
static bool valid_word_permuter(uinT8 perm, bool numbers_ok)
Check all the DAWGs to see if this word is in any of them.
Definition: dict.h:447
Definition: werd.h:36
BOOL8 check_debug_pt(WERD_RES *word, int location)
Definition: control.cpp:1767
inT32 x_height() const
return xheight
Definition: ocrblock.h:110
void word_char_quality(WERD_RES *word, ROW *row, inT16 *match_count, inT16 *accepted_match_count)
Definition: docqual.cpp:97
inT16 left() const
Definition: rect.h:68
bool ProcessTargetWord(const TBOX &word_box, const TBOX &target_word_box, const char *word_config, int pass)
Definition: control.cpp:118
float certainty() const
Definition: ratngs.h:327
TWERD * rebuild_word
Definition: pageres.h:244
void ZoomToRectangle(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:765
#define LOC_WRITE_RESULTS
Definition: errcode.h:54
bool tessedit_enable_bigram_correction
bool script_has_xheight() const
Definition: unicharset.h:849
const UNICHAR_ID unichar_id(int index) const
Definition: ratngs.h:312
bool deadline_exceeded() const
Definition: ocrclass.h:144
Definition: ocrblock.h:30
const UNICHARSET * uch_set
Definition: pageres.h:192
ACCEPTABLE_WERD_TYPE
Definition: control.h:34
void init_to_size(int size, T t)
ROW_RES * row() const
Definition: pageres.h:736
bool is_italic() const
Definition: fontinfo.h:111
static const char * IncorrectReasonName(IncorrectResultReason irr)
Definition: blamer.cpp:56
uinT8 permuter() const
Definition: ratngs.h:343
void print()
Definition: werd.cpp:266
void classify_word_pass1(const WordData &word_data, WERD_RES **in_word, PointerVector< WERD_RES > *out_words)
Definition: control.cpp:1344
bool RecogAllWordsPassN(int pass_n, ETEXT_DESC *monitor, PAGE_RES_IT *pr_it, GenericVector< WordData > *words)
Definition: control.cpp:207
void dictionary_correction_pass(PAGE_RES *page_res)
Definition: control.cpp:2015
const STRING debug_string() const
Definition: ratngs.h:502
WERD_CHOICE * raw_choice
Definition: pageres.h:224
inT32 rej_count
Definition: pageres.h:61
tesseract::Tesseract * tesseract
Definition: pageres.h:266
Dict & getDict()
Definition: classify.h:65
void PrintBestChoices() const
Definition: pageres.cpp:709
const char *const kBackUpConfigFile
Definition: control.cpp:53
void font_recognition_pass(PAGE_RES *page_res)
Definition: control.cpp:1958
void SetMisAdaptionDebug(const WERD_CHOICE *best_choice, bool debug)
Definition: blamer.cpp:574
ACCEPTABLE_WERD_TYPE acceptable_word_string(const UNICHARSET &char_set, const char *s, const char *lengths)
Definition: control.cpp:1663
int UNICHAR_ID
Definition: unichar.h:33
float baseline_shift
Definition: pageres.h:297
BOOL8 guessed_x_ht
Definition: pageres.h:292
bool valid_bigram(const WERD_CHOICE &word1, const WERD_CHOICE &word2) const
Definition: dict.cpp:738
EXTERN ScrollView * fx_win
Definition: drawfx.cpp:51
const FontInfo * fontinfo2
Definition: pageres.h:289
#define MAX_INT32
Definition: host.h:120
void bigram_correction_pass(PAGE_RES *page_res)
Definition: control.cpp:442
void SwitchAdaptiveClassifier()
Definition: adaptmatch.cpp:628
const STRING & misadaption_debug() const
Definition: blamer.h:119
bool get_islower(UNICHAR_ID unichar_id) const
Definition: unicharset.h:456
Definition: werd.h:60
void SetupWordPassN(int pass_n, WordData *word)
Definition: control.cpp:171
Pix * BestPix() const
void tess_segment_pass_n(int pass_n, WERD_RES *word)
Definition: tessbox.cpp:39
void cube_word_pass1(BLOCK *block, ROW *row, WERD_RES *word)
void rej_word_bad_quality()
Definition: rejctmap.cpp:488
inT16 word_blob_quality(WERD_RES *word, ROW *row)
Definition: docqual.cpp:65
bool major_x_overlap(const TBOX &box) const
Definition: rect.h:402
UnicityTable< FontInfo > & get_fontinfo_table()
Definition: classify.h:345
inT16 bottom() const
Definition: rect.h:61
TBOX bounding_box() const
Definition: blobs.cpp:881
BOOL8 done
Definition: pageres.h:282
bool AdaptiveClassifierIsEmpty() const
Definition: classify.h:285
void SetupAllWordsPassN(int pass_n, const TBOX *target_word_box, const char *word_config, PAGE_RES *page_res, GenericVector< WordData > *words)
Definition: control.cpp:148
bool major_overlap(const TBOX &box) const
Definition: rect.h:358
inT8 fontinfo_id2_count
Definition: pageres.h:291
WERD * word
Definition: pageres.h:175
#define MAX_INT8
Definition: host.h:118
bool x_overlap(const TBOX &box) const
Definition: rect.h:391
bool empty() const
Definition: genericvector.h:84
ParamsVectors * params()
Definition: ccutil.h:65
double classify_max_rating_ratio
Definition: classify.h:402
bool AnyTessLang() const
ALL lower case.
Definition: control.h:37
void fix_rep_char(PAGE_RES_IT *page_res_it)
Definition: control.cpp:1624
void add_str_int(const char *str, int number)
Definition: strngs.cpp:376
float max_x_height() const
Definition: ratngs.h:336
WERD_RES * InsertSimpleCloneWord(const WERD_RES &clone_res, WERD *new_word)
Definition: pageres.cpp:1268
void GetNoiseOutlines(GenericVector< C_OUTLINE * > *outlines)
Definition: werd.cpp:530
void classify_word_and_language(int pass_n, PAGE_RES_IT *pr_it, WordData *word_data)
Definition: control.cpp:1268
void recog_pseudo_word(PAGE_RES *page_res, TBOX &selection_box)
Definition: control.cpp:68
Unacceptable word.
Definition: control.h:36
void CopyTruth(const BlamerBundle &other)
Definition: blamer.h:187
bool top_bottom_useful() const
Definition: unicharset.h:495
const int length() const
Definition: boxword.h:85
void set_word_fonts(WERD_RES *word)
Definition: control.cpp:1880
#define FALSE
Definition: capi.h:29
BOOL8 tess_failed
Definition: pageres.h:272
int count(LIST var_list)
Definition: oldlist.cpp:108
inT16 progress
Definition: ocrclass.h:115
void Add(T value, int count)
Definition: sorthelper.h:65
void fix_hyphens()
Definition: pageres.cpp:1041
bool TestNewNormalization(int original_misfits, float baseline_shift, float new_x_ht, WERD_RES *word, BLOCK *block, ROW *row)
Definition: control.cpp:1437
double classify_max_certainty_margin
Definition: classify.h:404
ROW * row
Definition: pageres.h:127
inT16 reject_count()
Definition: rejctmap.h:243
Definition: rect.h:30
#define TRUE
Definition: capi.h:28
void(Tesseract::* WordRecognizer)(const WordData &word_data, WERD_RES **in_word, PointerVector< WERD_RES > *out_words)
bool tess_acceptable_word(WERD_RES *word)
Definition: tessbox.cpp:69
float y() const
Definition: points.h:212
bool SetupForRecognition(const UNICHARSET &unicharset_in, tesseract::Tesseract *tesseract, Pix *pix, int norm_mode, const TBOX *norm_box, bool numeric_mode, bool use_body_size, bool allow_detailed_fx, ROW *row, const BLOCK *block)
Definition: pageres.cpp:294
FILE * debug_fp
Definition: tessvars.cpp:24
STRING lang
Definition: ccutil.h:69
BOOL8 flag(WERD_FLAGS mask) const
Definition: werd.h:128
void PrerecAllWordsPar(const GenericVector< WordData > &words)
Definition: par_control.cpp:36
bool contains(const FCOORD pt) const
Definition: rect.h:323
bool AdaptableWord(WERD_RES *word)
Definition: adaptmatch.cpp:850
TBOX bounding_box() const
Definition: stepblob.cpp:250
inT32 pile_count(inT32 value) const
Definition: statistc.h:78
ALL but initial lc.
Definition: control.h:39
void CleanupSingleRowResult(PageSegMode pageseg_mode, PAGE_RES *page_res)
Definition: textord.cpp:359
static bool ReadParamsFile(const char *file, SetParamConstraint constraint, ParamsVectors *member_params)
Definition: params.cpp:41
Definition: strngs.h:44
const GenericVector< tesseract::ScoredFont > & fonts() const
Definition: ratngs.h:91
void set_global_subloc_code(int loc_code)
Definition: globaloc.cpp:85
void initialise(inT16 length)
Definition: rejctmap.cpp:318
#define NULL
Definition: host.h:144
#define LOC_FUZZY_SPACE
Definition: errcode.h:50
bool IsAmbiguous()
Definition: pageres.cpp:443
SIGNED char inT8
Definition: host.h:98
BLOB_CHOICE * GetBlobChoice(int index) const
Definition: pageres.cpp:742
void LearnWord(const char *fontname, WERD_RES *word)
Definition: adaptmatch.cpp:244
inT8 italic
Definition: pageres.h:285
void print(FILE *fp)
Definition: rejctmap.cpp:394
void StartBackupAdaptiveClassifier()
Definition: adaptmatch.cpp:644
const char * string() const
Definition: strngs.cpp:193
inT16 top() const
Definition: rect.h:54
#define SUBLOC_NORM
Definition: errcode.h:59
static void LastChanceBlame(bool debug, WERD_RES *word)
Definition: blamer.cpp:547
void BestChoiceToCorrectText()
Definition: pageres.cpp:917
UNICHAR_ID unichar_id() const
Definition: ratngs.h:76
Definition: points.h:189
BLOB_CHOICE_LIST * GetBlobChoices(int index) const
Definition: pageres.cpp:751
BlamerBundle * blamer_bundle
Definition: pageres.h:230
void fix_fuzzy_spaces(ETEXT_DESC *monitor, inT32 word_count, PAGE_RES *page_res)
Definition: fixspace.cpp:48
bool EqualIgnoringCaseAndTerminalPunct(const WERD_CHOICE &word1, const WERD_CHOICE &word2)
Definition: ratngs.cpp:791
C_BLOB_LIST * rej_cblob_list()
Definition: werd.h:95
C_BLOB_LIST * cblob_list()
Definition: werd.h:100
WERD_RES * word() const
Definition: pageres.h:733
void create_fx_win()
Definition: drawfx.cpp:60
BOOL8 contains(const char c) const
Definition: strngs.cpp:184
short inT16
Definition: host.h:100
int inT32
Definition: host.h:102
void tess_add_doc_word(WERD_CHOICE *word_choice)
Definition: tessbox.cpp:79
unsigned char uinT8
Definition: host.h:99