tesseract  4.00.00dev
ratngs.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: ratngs.cpp (Formerly ratings.c)
3  * Description: Code to manipulate the BLOB_CHOICE and WERD_CHOICE classes.
4  * Author: Ray Smith
5  * Created: Thu Apr 23 13:23:29 BST 1992
6  *
7  * (C) Copyright 1992, Hewlett-Packard Ltd.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config_auto.h"
23 #endif
24 
25 #include "ratngs.h"
26 
27 #include "blobs.h"
28 #include "callcpp.h"
29 #include "genericvector.h"
30 #include "matrix.h"
31 #include "normalis.h" // kBlnBaselineOffset.
32 #include "unicharset.h"
33 
35 
38 
39 const float WERD_CHOICE::kBadRating = 100000.0;
40 // Min offset in baseline-normalized coords to make a character a subscript.
41 const int kMinSubscriptOffset = 20;
42 // Min offset in baseline-normalized coords to make a character a superscript.
43 const int kMinSuperscriptOffset = 20;
44 // Max y of bottom of a drop-cap blob.
45 const int kMaxDropCapBottom = -128;
46 // Max fraction of x-height to use as denominator in measuring x-height overlap.
47 const double kMaxOverlapDenominator = 0.125;
48 // Min fraction of x-height range that should be in agreement for matching
49 // x-heights.
50 const double kMinXHeightMatch = 0.5;
51 // Max tolerance on baseline position as a fraction of x-height for matching
52 // baselines.
53 const double kMaxBaselineDrift = 0.0625;
54 
55 static const char kPermuterTypeNoPerm[] = "None";
56 static const char kPermuterTypePuncPerm[] = "Punctuation";
57 static const char kPermuterTypeTopPerm[] = "Top Choice";
58 static const char kPermuterTypeLowerPerm[] = "Top Lower Case";
59 static const char kPermuterTypeUpperPerm[] = "Top Upper Case";
60 static const char kPermuterTypeNgramPerm[] = "Ngram";
61 static const char kPermuterTypeNumberPerm[] = "Number";
62 static const char kPermuterTypeUserPatPerm[] = "User Pattern";
63 static const char kPermuterTypeSysDawgPerm[] = "System Dictionary";
64 static const char kPermuterTypeDocDawgPerm[] = "Document Dictionary";
65 static const char kPermuterTypeUserDawgPerm[] = "User Dictionary";
66 static const char kPermuterTypeFreqDawgPerm[] = "Frequent Words Dictionary";
67 static const char kPermuterTypeCompoundPerm[] = "Compound";
68 
69 static const char * const kPermuterTypeNames[] = {
70  kPermuterTypeNoPerm, // 0
71  kPermuterTypePuncPerm, // 1
72  kPermuterTypeTopPerm, // 2
73  kPermuterTypeLowerPerm, // 3
74  kPermuterTypeUpperPerm, // 4
75  kPermuterTypeNgramPerm, // 5
76  kPermuterTypeNumberPerm, // 6
77  kPermuterTypeUserPatPerm, // 7
78  kPermuterTypeSysDawgPerm, // 8
79  kPermuterTypeDocDawgPerm, // 9
80  kPermuterTypeUserDawgPerm, // 10
81  kPermuterTypeFreqDawgPerm, // 11
82  kPermuterTypeCompoundPerm // 12
83 };
84 
90 BLOB_CHOICE::BLOB_CHOICE(UNICHAR_ID src_unichar_id, // character id
91  float src_rating, // rating
92  float src_cert, // certainty
93  int src_script_id, // script
94  float min_xheight, // min xheight allowed
95  float max_xheight, // max xheight by this char
96  float yshift, // yshift out of position
97  BlobChoiceClassifier c) { // adapted match or other
98  unichar_id_ = src_unichar_id;
99  rating_ = src_rating;
100  certainty_ = src_cert;
101  fontinfo_id_ = -1;
102  fontinfo_id2_ = -1;
103  script_id_ = src_script_id;
104  min_xheight_ = min_xheight;
105  max_xheight_ = max_xheight;
106  yshift_ = yshift;
107  classifier_ = c;
108 }
109 
116  unichar_id_ = other.unichar_id();
117  rating_ = other.rating();
118  certainty_ = other.certainty();
119  fontinfo_id_ = other.fontinfo_id();
120  fontinfo_id2_ = other.fontinfo_id2();
121  script_id_ = other.script_id();
122  matrix_cell_ = other.matrix_cell_;
123  min_xheight_ = other.min_xheight_;
124  max_xheight_ = other.max_xheight_;
125  yshift_ = other.yshift();
126  classifier_ = other.classifier_;
127  fonts_ = other.fonts_;
128 }
129 
130 // Returns true if *this and other agree on the baseline and x-height
131 // to within some tolerance based on a given estimate of the x-height.
132 bool BLOB_CHOICE::PosAndSizeAgree(const BLOB_CHOICE& other, float x_height,
133  bool debug) const {
134  double baseline_diff = fabs(yshift() - other.yshift());
135  if (baseline_diff > kMaxBaselineDrift * x_height) {
136  if (debug) {
137  tprintf("Baseline diff %g for %d v %d\n",
138  baseline_diff, unichar_id_, other.unichar_id_);
139  }
140  return false;
141  }
142  double this_range = max_xheight() - min_xheight();
143  double other_range = other.max_xheight() - other.min_xheight();
144  double denominator = ClipToRange(MIN(this_range, other_range),
145  1.0, kMaxOverlapDenominator * x_height);
146  double overlap = MIN(max_xheight(), other.max_xheight()) -
147  MAX(min_xheight(), other.min_xheight());
148  overlap /= denominator;
149  if (debug) {
150  tprintf("PosAndSize for %d v %d: bl diff = %g, ranges %g, %g / %g ->%g\n",
151  unichar_id_, other.unichar_id_, baseline_diff,
152  this_range, other_range, denominator, overlap);
153  }
154 
155  return overlap >= kMinXHeightMatch;
156 }
157 
158 // Helper to find the BLOB_CHOICE in the bc_list that matches the given
159 // unichar_id, or NULL if there is no match.
161  BLOB_CHOICE_LIST* bc_list) {
162  // Find the corresponding best BLOB_CHOICE.
163  BLOB_CHOICE_IT choice_it(bc_list);
164  for (choice_it.mark_cycle_pt(); !choice_it.cycled_list();
165  choice_it.forward()) {
166  BLOB_CHOICE* choice = choice_it.data();
167  if (choice->unichar_id() == char_id) {
168  return choice;
169  }
170  }
171  return NULL;
172 }
173 
174 const char *WERD_CHOICE::permuter_name(uinT8 permuter) {
175  return kPermuterTypeNames[permuter];
176 }
177 
178 namespace tesseract {
179 
180 const char *ScriptPosToString(enum ScriptPos script_pos) {
181  switch (script_pos) {
182  case SP_NORMAL: return "NORM";
183  case SP_SUBSCRIPT: return "SUB";
184  case SP_SUPERSCRIPT: return "SUPER";
185  case SP_DROPCAP: return "DROPC";
186  }
187  return "SP_UNKNOWN";
188 }
189 
190 } // namespace tesseract.
191 
198 WERD_CHOICE::WERD_CHOICE(const char *src_string,
199  const UNICHARSET &unicharset)
200  : unicharset_(&unicharset){
201  GenericVector<UNICHAR_ID> encoding;
202  GenericVector<char> lengths;
203  if (unicharset.encode_string(src_string, true, &encoding, &lengths, NULL)) {
204  lengths.push_back('\0');
205  STRING src_lengths = &lengths[0];
206  this->init(src_string, src_lengths.string(), 0.0, 0.0, NO_PERM);
207  } else { // There must have been an invalid unichar in the string.
208  this->init(8);
209  this->make_bad();
210  }
211 }
212 
223 void WERD_CHOICE::init(const char *src_string,
224  const char *src_lengths,
225  float src_rating,
226  float src_certainty,
227  uinT8 src_permuter) {
228  int src_string_len = strlen(src_string);
229  if (src_string_len == 0) {
230  this->init(8);
231  } else {
232  this->init(src_lengths ? strlen(src_lengths): src_string_len);
233  length_ = reserved_;
234  int offset = 0;
235  for (int i = 0; i < length_; ++i) {
236  int unichar_length = src_lengths ? src_lengths[i] : 1;
237  unichar_ids_[i] =
238  unicharset_->unichar_to_id(src_string+offset, unichar_length);
239  state_[i] = 1;
240  certainties_[i] = src_certainty;
241  offset += unichar_length;
242  }
243  }
244  adjust_factor_ = 1.0f;
245  rating_ = src_rating;
246  certainty_ = src_certainty;
247  permuter_ = src_permuter;
248  dangerous_ambig_found_ = false;
249 }
250 
255  delete[] unichar_ids_;
256  delete[] script_pos_;
257  delete[] state_;
258  delete[] certainties_;
259 }
260 
261 const char *WERD_CHOICE::permuter_name() const {
262  return kPermuterTypeNames[permuter_];
263 }
264 
265 // Returns the BLOB_CHOICE_LIST corresponding to the given index in the word,
266 // taken from the appropriate cell in the ratings MATRIX.
267 // Borrowed pointer, so do not delete.
268 BLOB_CHOICE_LIST* WERD_CHOICE::blob_choices(int index, MATRIX* ratings) const {
269  MATRIX_COORD coord = MatrixCoord(index);
270  BLOB_CHOICE_LIST* result = ratings->get(coord.col, coord.row);
271  if (result == NULL) {
272  result = new BLOB_CHOICE_LIST;
273  ratings->put(coord.col, coord.row, result);
274  }
275  return result;
276 }
277 
278 // Returns the MATRIX_COORD corresponding to the location in the ratings
279 // MATRIX for the given index into the word.
281  int col = 0;
282  for (int i = 0; i < index; ++i)
283  col += state_[i];
284  int row = col + state_[index] - 1;
285  return MATRIX_COORD(col, row);
286 }
287 
288 // Sets the entries for the given index from the BLOB_CHOICE, assuming
289 // unit fragment lengths, but setting the state for this index to blob_count.
290 void WERD_CHOICE::set_blob_choice(int index, int blob_count,
291  const BLOB_CHOICE* blob_choice) {
292  unichar_ids_[index] = blob_choice->unichar_id();
293  script_pos_[index] = tesseract::SP_NORMAL;
294  state_[index] = blob_count;
295  certainties_[index] = blob_choice->certainty();
296 }
297 
298 
305  for (int i = 0; i < length_; ++i) {
306  if (unichar_ids_[i] == unichar_id) {
307  return true;
308  }
309  }
310  return false;
311 }
312 
320 void WERD_CHOICE::remove_unichar_ids(int start, int num) {
321  ASSERT_HOST(start >= 0 && start + num <= length_);
322  // Accumulate the states to account for the merged blobs.
323  for (int i = 0; i < num; ++i) {
324  if (start > 0)
325  state_[start - 1] += state_[start + i];
326  else if (start + num < length_)
327  state_[start + num] += state_[start + i];
328  }
329  for (int i = start; i + num < length_; ++i) {
330  unichar_ids_[i] = unichar_ids_[i + num];
331  script_pos_[i] = script_pos_[i + num];
332  state_[i] = state_[i + num];
333  certainties_[i] = certainties_[i + num];
334  }
335  length_ -= num;
336 }
337 
344  for (int i = 0; i < length_ / 2; ++i) {
345  UNICHAR_ID tmp_id = unichar_ids_[i];
346  unichar_ids_[i] = unicharset_->get_mirror(unichar_ids_[length_-1-i]);
347  unichar_ids_[length_-1-i] = unicharset_->get_mirror(tmp_id);
348  }
349  if (length_ % 2 != 0) {
350  unichar_ids_[length_/2] = unicharset_->get_mirror(unichar_ids_[length_/2]);
351  }
352 }
353 
361 void WERD_CHOICE::punct_stripped(int *start, int *end) const {
362  *start = 0;
363  *end = length() - 1;
364  while (*start < length() &&
365  unicharset()->get_ispunctuation(unichar_id(*start))) {
366  (*start)++;
367  }
368  while (*end > -1 &&
369  unicharset()->get_ispunctuation(unichar_id(*end))) {
370  (*end)--;
371  }
372  (*end)++;
373 }
374 
375 void WERD_CHOICE::GetNonSuperscriptSpan(int *pstart, int *pend) const {
376  int end = length();
377  while (end > 0 &&
378  unicharset_->get_isdigit(unichar_ids_[end - 1]) &&
380  end--;
381  }
382  int start = 0;
383  while (start < end &&
384  unicharset_->get_isdigit(unichar_ids_[start]) &&
386  start++;
387  }
388  *pstart = start;
389  *pend = end;
390 }
391 
392 WERD_CHOICE WERD_CHOICE::shallow_copy(int start, int end) const {
393  ASSERT_HOST(start >= 0 && start <= length_);
394  ASSERT_HOST(end >= 0 && end <= length_);
395  if (end < start) { end = start; }
396  WERD_CHOICE retval(unicharset_, end - start);
397  for (int i = start; i < end; i++) {
399  unichar_ids_[i], state_[i], 0.0f, certainties_[i]);
400  }
401  return retval;
402 }
403 
410  int i;
411  for (i = 0; i < length_; ++i) {
412  UNICHARSET::Direction dir = unicharset_->get_direction(unichar_ids_[i]);
413  if (dir == UNICHARSET::U_RIGHT_TO_LEFT ||
415  return true;
416  }
417  }
418  return false;
419 }
420 
428  STRING *word_lengths_str) const {
429  *word_str = "";
430  if (word_lengths_str != NULL) *word_lengths_str = "";
431  for (int i = 0; i < length_; ++i) {
432  const char *ch = unicharset_->id_to_unichar_ext(unichar_ids_[i]);
433  *word_str += ch;
434  if (word_lengths_str != NULL) {
435  *word_lengths_str += strlen(ch);
436  }
437  }
438 }
439 
447  UNICHAR_ID unichar_id, int blob_count,
448  float rating, float certainty) {
449  if (length_ == reserved_) {
450  this->double_the_size();
451  }
452  this->append_unichar_id_space_allocated(unichar_id, blob_count,
453  rating, certainty);
454 }
455 
464  ASSERT_HOST(unicharset_ == second.unicharset_);
465  while (reserved_ < length_ + second.length()) {
466  this->double_the_size();
467  }
468  const UNICHAR_ID *other_unichar_ids = second.unichar_ids();
469  for (int i = 0; i < second.length(); ++i) {
470  unichar_ids_[length_ + i] = other_unichar_ids[i];
471  state_[length_ + i] = second.state_[i];
472  certainties_[length_ + i] = second.certainties_[i];
473  script_pos_[length_ + i] = second.BlobPosition(i);
474  }
475  length_ += second.length();
476  if (second.adjust_factor_ > adjust_factor_)
477  adjust_factor_ = second.adjust_factor_;
478  rating_ += second.rating(); // add ratings
479  if (second.certainty() < certainty_) // take min
480  certainty_ = second.certainty();
481  if (second.dangerous_ambig_found_)
482  dangerous_ambig_found_ = true;
483  if (permuter_ == NO_PERM) {
484  permuter_ = second.permuter();
485  } else if (second.permuter() != NO_PERM &&
486  second.permuter() != permuter_) {
487  permuter_ = COMPOUND_PERM;
488  }
489  return *this;
490 }
491 
492 
500  while (reserved_ < source.length()) {
501  this->double_the_size();
502  }
503 
504  unicharset_ = source.unicharset_;
505  const UNICHAR_ID *other_unichar_ids = source.unichar_ids();
506  for (int i = 0; i < source.length(); ++i) {
507  unichar_ids_[i] = other_unichar_ids[i];
508  state_[i] = source.state_[i];
509  certainties_[i] = source.certainties_[i];
510  script_pos_[i] = source.BlobPosition(i);
511  }
512  length_ = source.length();
513  adjust_factor_ = source.adjust_factor_;
514  rating_ = source.rating();
515  certainty_ = source.certainty();
516  min_x_height_ = source.min_x_height();
517  max_x_height_ = source.max_x_height();
518  permuter_ = source.permuter();
519  dangerous_ambig_found_ = source.dangerous_ambig_found_;
520  return *this;
521 }
522 
523 // Sets up the script_pos_ member using the blobs_list to get the bln
524 // bounding boxes, *this to get the unichars, and this->unicharset
525 // to get the target positions. If small_caps is true, sub/super are not
526 // considered, but dropcaps are.
527 // NOTE: blobs_list should be the chopped_word blobs. (Fully segemented.)
528 void WERD_CHOICE::SetScriptPositions(bool small_caps, TWERD* word) {
529  // Since WERD_CHOICE isn't supposed to depend on a Tesseract,
530  // we don't have easy access to the flags Tesseract stores. Therefore, debug
531  // for this module is hard compiled in.
532  int debug = 0;
533 
534  // Initialize to normal.
535  for (int i = 0; i < length_; ++i)
536  script_pos_[i] = tesseract::SP_NORMAL;
537  if (word->blobs.empty() || word->NumBlobs() != TotalOfStates()) {
538  return;
539  }
540 
541  int position_counts[4];
542  for (int i = 0; i < 4; i++) {
543  position_counts[i] = 0;
544  }
545 
546  int chunk_index = 0;
547  for (int blob_index = 0; blob_index < length_; ++blob_index, ++chunk_index) {
548  TBLOB* tblob = word->blobs[chunk_index];
549  int uni_id = unichar_id(blob_index);
550  TBOX blob_box = tblob->bounding_box();
551  if (state_ != NULL) {
552  for (int i = 1; i < state_[blob_index]; ++i) {
553  ++chunk_index;
554  tblob = word->blobs[chunk_index];
555  blob_box += tblob->bounding_box();
556  }
557  }
558  script_pos_[blob_index] = ScriptPositionOf(false, *unicharset_, blob_box,
559  uni_id);
560  if (small_caps && script_pos_[blob_index] != tesseract::SP_DROPCAP) {
561  script_pos_[blob_index] = tesseract::SP_NORMAL;
562  }
563  position_counts[script_pos_[blob_index]]++;
564  }
565  // If almost everything looks like a superscript or subscript,
566  // we most likely just got the baseline wrong.
567  if (position_counts[tesseract::SP_SUBSCRIPT] > 0.75 * length_ ||
568  position_counts[tesseract::SP_SUPERSCRIPT] > 0.75 * length_) {
569  if (debug >= 2) {
570  tprintf("Most characters of %s are subscript or superscript.\n"
571  "That seems wrong, so I'll assume we got the baseline wrong\n",
572  unichar_string().string());
573  }
574  for (int i = 0; i < length_; i++) {
575  ScriptPos sp = script_pos_[i];
577  position_counts[sp]--;
578  position_counts[tesseract::SP_NORMAL]++;
579  script_pos_[i] = tesseract::SP_NORMAL;
580  }
581  }
582  }
583 
584  if ((debug >= 1 && position_counts[tesseract::SP_NORMAL] < length_) ||
585  debug >= 2) {
586  tprintf("SetScriptPosition on %s\n", unichar_string().string());
587  int chunk_index = 0;
588  for (int blob_index = 0; blob_index < length_; ++blob_index) {
589  if (debug >= 2 || script_pos_[blob_index] != tesseract::SP_NORMAL) {
590  TBLOB* tblob = word->blobs[chunk_index];
591  ScriptPositionOf(true, *unicharset_, tblob->bounding_box(),
592  unichar_id(blob_index));
593  }
594  chunk_index += state_ != NULL ? state_[blob_index] : 1;
595  }
596  }
597 }
598 // Sets the script_pos_ member from some source positions with a given length.
600  int length) {
601  ASSERT_HOST(length == length_);
602  if (positions != script_pos_) {
603  delete [] script_pos_;
604  script_pos_ = new ScriptPos[length];
605  memcpy(script_pos_, positions, sizeof(positions[0]) * length);
606  }
607 }
608 // Sets all the script_pos_ positions to the given position.
610  for (int i = 0; i < length_; ++i)
611  script_pos_[i] = position;
612 }
613 
614 /* static */
616  const UNICHARSET& unicharset,
617  const TBOX& blob_box,
620  int top = blob_box.top();
621  int bottom = blob_box.bottom();
622  int min_bottom, max_bottom, min_top, max_top;
623  unicharset.get_top_bottom(unichar_id,
624  &min_bottom, &max_bottom,
625  &min_top, &max_top);
626 
627  int sub_thresh_top = min_top - kMinSubscriptOffset;
628  int sub_thresh_bot = kBlnBaselineOffset - kMinSubscriptOffset;
629  int sup_thresh_bot = max_bottom + kMinSuperscriptOffset;
630  if (bottom <= kMaxDropCapBottom) {
631  retval = tesseract::SP_DROPCAP;
632  } else if (top < sub_thresh_top && bottom < sub_thresh_bot) {
633  retval = tesseract::SP_SUBSCRIPT;
634  } else if (bottom > sup_thresh_bot) {
635  retval = tesseract::SP_SUPERSCRIPT;
636  }
637 
638  if (print_debug) {
639  const char *pos = ScriptPosToString(retval);
640  tprintf("%s Character %s[bot:%d top: %d] "
641  "bot_range[%d,%d] top_range[%d, %d] "
642  "sub_thresh[bot:%d top:%d] sup_thresh_bot %d\n",
643  pos, unicharset.id_to_unichar(unichar_id),
644  bottom, top,
645  min_bottom, max_bottom, min_top, max_top,
646  sub_thresh_bot, sub_thresh_top,
647  sup_thresh_bot);
648  }
649  return retval;
650 }
651 
652 // Returns the script-id (eg Han) of the dominant script in the word.
654  int max_script = unicharset_->get_script_table_size();
655  int *sid = new int[max_script];
656  int x;
657  for (x = 0; x < max_script; x++) sid[x] = 0;
658  for (x = 0; x < length_; ++x) {
659  int script_id = unicharset_->get_script(unichar_id(x));
660  sid[script_id]++;
661  }
662  if (unicharset_->han_sid() != unicharset_->null_sid()) {
663  // Add the Hiragana & Katakana counts to Han and zero them out.
664  if (unicharset_->hiragana_sid() != unicharset_->null_sid()) {
665  sid[unicharset_->han_sid()] += sid[unicharset_->hiragana_sid()];
666  sid[unicharset_->hiragana_sid()] = 0;
667  }
668  if (unicharset_->katakana_sid() != unicharset_->null_sid()) {
669  sid[unicharset_->han_sid()] += sid[unicharset_->katakana_sid()];
670  sid[unicharset_->katakana_sid()] = 0;
671  }
672  }
673  // Note that high script ID overrides lower one on a tie, thus biasing
674  // towards non-Common script (if sorted that way in unicharset file).
675  int max_sid = 0;
676  for (x = 1; x < max_script; x++)
677  if (sid[x] >= sid[max_sid]) max_sid = x;
678  if (sid[max_sid] < length_ / 2)
679  max_sid = unicharset_->null_sid();
680  delete[] sid;
681  return max_sid;
682 }
683 
684 // Fixes the state_ for a chop at the given blob_posiiton.
685 void WERD_CHOICE::UpdateStateForSplit(int blob_position) {
686  int total_chunks = 0;
687  for (int i = 0; i < length_; ++i) {
688  total_chunks += state_[i];
689  if (total_chunks > blob_position) {
690  ++state_[i];
691  return;
692  }
693  }
694 }
695 
696 // Returns the sum of all the state elements, being the total number of blobs.
698  int total_chunks = 0;
699  for (int i = 0; i < length_; ++i) {
700  total_chunks += state_[i];
701  }
702  return total_chunks;
703 }
704 
710 void WERD_CHOICE::print(const char *msg) const {
711  tprintf("%s : ", msg);
712  for (int i = 0; i < length_; ++i) {
713  tprintf("%s", unicharset_->id_to_unichar(unichar_ids_[i]));
714  }
715  tprintf(" : R=%g, C=%g, F=%g, Perm=%d, xht=[%g,%g], ambig=%d\n",
716  rating_, certainty_, adjust_factor_, permuter_,
717  min_x_height_, max_x_height_, dangerous_ambig_found_);
718  tprintf("pos");
719  for (int i = 0; i < length_; ++i) {
720  tprintf("\t%s", ScriptPosToString(script_pos_[i]));
721  }
722  tprintf("\nstr");
723  for (int i = 0; i < length_; ++i) {
724  tprintf("\t%s", unicharset_->id_to_unichar(unichar_ids_[i]));
725  }
726  tprintf("\nstate:");
727  for (int i = 0; i < length_; ++i) {
728  tprintf("\t%d ", state_[i]);
729  }
730  tprintf("\nC");
731  for (int i = 0; i < length_; ++i) {
732  tprintf("\t%.3f", certainties_[i]);
733  }
734  tprintf("\n");
735 }
736 
737 // Prints the segmentation state with an introductory message.
738 void WERD_CHOICE::print_state(const char *msg) const {
739  tprintf("%s", msg);
740  for (int i = 0; i < length_; ++i)
741  tprintf(" %d", state_[i]);
742  tprintf("\n");
743 }
744 
745 // Displays the segmentation state of *this (if not the same as the last
746 // one displayed) and waits for a click in the window.
748 #ifndef GRAPHICS_DISABLED
749  // Number of different colors to draw with.
750  const int kNumColors = 6;
751  static ScrollView *segm_window = NULL;
752  // Check the state against the static prev_drawn_state.
753  static GenericVector<int> prev_drawn_state;
754  bool already_done = prev_drawn_state.size() == length_;
755  if (!already_done) prev_drawn_state.init_to_size(length_, 0);
756  for (int i = 0; i < length_; ++i) {
757  if (prev_drawn_state[i] != state_[i]) {
758  already_done = false;
759  }
760  prev_drawn_state[i] = state_[i];
761  }
762  if (already_done || word->blobs.empty()) return;
763 
764  // Create the window if needed.
765  if (segm_window == NULL) {
766  segm_window = new ScrollView("Segmentation", 5, 10, 500, 256,
767  2000.0, 256.0, true);
768  } else {
769  segm_window->Clear();
770  }
771 
772  TBOX bbox;
773  int blob_index = 0;
774  for (int c = 0; c < length_; ++c) {
775  ScrollView::Color color =
776  static_cast<ScrollView::Color>(c % kNumColors + 3);
777  for (int i = 0; i < state_[c]; ++i, ++blob_index) {
778  TBLOB* blob = word->blobs[blob_index];
779  bbox += blob->bounding_box();
780  blob->plot(segm_window, color, color);
781  }
782  }
783  segm_window->ZoomToRectangle(bbox.left(), bbox.top(),
784  bbox.right(), bbox.bottom());
785  segm_window->Update();
786  window_wait(segm_window);
787 #endif
788 }
789 
790 
792  const WERD_CHOICE &word2) {
793  const UNICHARSET *uchset = word1.unicharset();
794  if (word2.unicharset() != uchset) return false;
795  int w1start, w1end;
796  word1.punct_stripped(&w1start, &w1end);
797  int w2start, w2end;
798  word2.punct_stripped(&w2start, &w2end);
799  if (w1end - w1start != w2end - w2start) return false;
800  for (int i = 0; i < w1end - w1start; i++) {
801  if (uchset->to_lower(word1.unichar_id(w1start + i)) !=
802  uchset->to_lower(word2.unichar_id(w2start + i))) {
803  return false;
804  }
805  }
806  return true;
807 }
808 
819 void print_ratings_list(const char *msg,
820  BLOB_CHOICE_LIST *ratings,
821  const UNICHARSET &current_unicharset) {
822  if (ratings->length() == 0) {
823  tprintf("%s:<none>\n", msg);
824  return;
825  }
826  if (*msg != '\0') {
827  tprintf("%s\n", msg);
828  }
829  BLOB_CHOICE_IT c_it;
830  c_it.set_to_list(ratings);
831  for (c_it.mark_cycle_pt(); !c_it.cycled_list(); c_it.forward()) {
832  c_it.data()->print(&current_unicharset);
833  if (!c_it.at_last()) tprintf("\n");
834  }
835  tprintf("\n");
836  fflush(stdout);
837 }
static tesseract::ScriptPos ScriptPositionOf(bool print_debug, const UNICHARSET &unicharset, const TBOX &blob_box, UNICHAR_ID unichar_id)
Definition: ratngs.cpp:615
float yshift() const
Definition: ratngs.h:129
WERD_CHOICE & operator+=(const WERD_CHOICE &second)
Definition: ratngs.cpp:463
BLOB_CHOICE_LIST * blob_choices(int index, MATRIX *ratings) const
Definition: ratngs.cpp:268
UNICHAR_ID unichar_id(int index) const
Definition: ratngs.h:313
int hiragana_sid() const
Definition: unicharset.h:849
void print() const
Definition: ratngs.h:578
float max_x_height() const
Definition: ratngs.h:337
float min_x_height() const
Definition: ratngs.h:334
void init_to_size(int size, T t)
int UNICHAR_ID
Definition: unichar.h:33
void reverse_and_mirror_unichar_ids()
Definition: ratngs.cpp:343
const double kMaxOverlapDenominator
Definition: ratngs.cpp:47
inT16 fontinfo_id2() const
Definition: ratngs.h:88
int length() const
Definition: ratngs.h:301
void set_blob_choice(int index, int blob_count, const BLOB_CHOICE *blob_choice)
Definition: ratngs.cpp:290
void SetScriptPositions(bool small_caps, TWERD *word)
Definition: ratngs.cpp:528
MATRIX_COORD MatrixCoord(int index) const
Definition: ratngs.cpp:280
T get(ICOORD pos) const
Definition: matrix.h:223
void DisplaySegmentation(TWERD *word)
Definition: ratngs.cpp:747
int push_back(T object)
float rating() const
Definition: ratngs.h:79
#define tprintf(...)
Definition: tprintf.h:31
BLOB_CHOICE()
Definition: ratngs.h:51
const char * string() const
Definition: strngs.cpp:198
int TotalOfStates() const
Definition: ratngs.cpp:697
const int kBlnBaselineOffset
Definition: normalis.h:29
voidpf uLong offset
Definition: ioapi.h:42
bool empty() const
Definition: genericvector.h:90
tesseract::ScriptPos BlobPosition(int index) const
Definition: ratngs.h:320
void append_unichar_id(UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)
Definition: ratngs.cpp:446
int size() const
Definition: genericvector.h:72
float certainty() const
Definition: ratngs.h:82
const double kMaxBaselineDrift
Definition: ratngs.cpp:53
int han_sid() const
Definition: unicharset.h:848
Direction get_direction(UNICHAR_ID unichar_id) const
Definition: unicharset.h:650
#define ASSERT_HOST(x)
Definition: errcode.h:84
void string_and_lengths(STRING *word_str, STRING *word_lengths_str) const
Definition: ratngs.cpp:427
int get_script(UNICHAR_ID unichar_id) const
Definition: unicharset.h:623
const char * id_to_unichar(UNICHAR_ID id) const
Definition: unicharset.cpp:266
inT16 left() const
Definition: rect.h:68
Definition: blobs.h:395
uinT8 permuter() const
Definition: ratngs.h:344
void Clear()
Definition: scrollview.cpp:595
inT16 fontinfo_id() const
Definition: ratngs.h:85
const char * id_to_unichar_ext(UNICHAR_ID id) const
Definition: unicharset.cpp:274
bool get_isdigit(UNICHAR_ID unichar_id) const
Definition: unicharset.h:472
int get_script_table_size() const
Definition: unicharset.h:809
void get_top_bottom(UNICHAR_ID unichar_id, int *min_bottom, int *max_bottom, int *min_top, int *max_top) const
Definition: unicharset.h:528
UNICHAR_ID to_lower(UNICHAR_ID unichar_id) const
Definition: unicharset.h:664
bool contains_unichar_id(UNICHAR_ID unichar_id) const
Definition: ratngs.cpp:304
Definition: strngs.h:45
static const float kBadRating
Definition: ratngs.h:273
UNICHAR_ID get_mirror(UNICHAR_ID unichar_id) const
Definition: unicharset.h:657
static void Update()
Definition: scrollview.cpp:715
void GetNonSuperscriptSpan(int *start, int *end) const
Definition: ratngs.cpp:375
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:122
char window_wait(ScrollView *win)
Definition: callcpp.cpp:111
const double kMinXHeightMatch
Definition: ratngs.cpp:50
void ZoomToRectangle(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:765
WERD_CHOICE(const UNICHARSET *unicharset)
Definition: ratngs.h:276
float min_xheight() const
Definition: ratngs.h:123
WERD_CHOICE shallow_copy(int start, int end) const
Definition: ratngs.cpp:392
float certainty() const
Definition: ratngs.h:328
int script_id() const
Definition: ratngs.h:111
const char * ScriptPosToString(enum ScriptPos script_pos)
Definition: ratngs.cpp:180
bool has_rtl_unichar_id() const
Definition: ratngs.cpp:409
int NumBlobs() const
Definition: blobs.h:425
inT16 top() const
Definition: rect.h:54
bool EqualIgnoringCaseAndTerminalPunct(const WERD_CHOICE &word1, const WERD_CHOICE &word2)
Definition: ratngs.cpp:791
const UNICHARSET * unicharset() const
Definition: ratngs.h:298
#define MAX(x, y)
Definition: ndminx.h:24
const int kMinSuperscriptOffset
Definition: ratngs.cpp:43
int katakana_sid() const
Definition: unicharset.h:850
void init(int reserved)
Definition: ratngs.h:407
const STRING & unichar_string() const
Definition: ratngs.h:539
Definition: rect.h:30
GenericVector< TBLOB * > blobs
Definition: blobs.h:436
ELISTIZE(BLOB_CHOICE)
bool encode_string(const char *str, bool give_up_on_failure, GenericVector< UNICHAR_ID > *encoding, GenericVector< char > *lengths, int *encoded_length) const
Definition: unicharset.cpp:234
void put(ICOORD pos, const T &thing)
Definition: matrix.h:215
void plot(ScrollView *window, ScrollView::Color color, ScrollView::Color child_color)
Definition: blobs.cpp:524
#define MIN(x, y)
Definition: ndminx.h:28
Definition: matrix.h:563
Definition: blobs.h:261
void punct_stripped(int *start_core, int *end_core) const
Definition: ratngs.cpp:361
void append_unichar_id_space_allocated(UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)
Definition: ratngs.h:450
void make_bad()
Set the fields in this choice to be default (bad) values.
Definition: ratngs.h:441
uint8_t uinT8
Definition: host.h:35
void remove_unichar_ids(int index, int num)
Definition: ratngs.cpp:320
inT16 right() const
Definition: rect.h:75
void UpdateStateForSplit(int blob_position)
Definition: ratngs.cpp:685
const UNICHAR_ID * unichar_ids() const
Definition: ratngs.h:310
bool PosAndSizeAgree(const BLOB_CHOICE &other, float x_height, bool debug) const
Definition: ratngs.cpp:132
const char * permuter_name() const
Definition: ratngs.cpp:261
UNICHAR_ID unichar_id() const
Definition: ratngs.h:76
int GetTopScriptID() const
Definition: ratngs.cpp:653
const int kMaxDropCapBottom
Definition: ratngs.cpp:45
WERD_CHOICE & operator=(const WERD_CHOICE &source)
Definition: ratngs.cpp:499
void double_the_size()
Make more space in unichar_id_ and fragment_lengths_ arrays.
Definition: ratngs.h:385
int null_sid() const
Definition: unicharset.h:843
inT16 bottom() const
Definition: rect.h:61
BlobChoiceClassifier
Definition: ratngs.h:40
void print_ratings_list(const char *msg, BLOB_CHOICE_LIST *ratings, const UNICHARSET &current_unicharset)
Definition: ratngs.cpp:819
void SetAllScriptPositions(tesseract::ScriptPos position)
Definition: ratngs.cpp:609
BLOB_CHOICE * FindMatchingChoice(UNICHAR_ID char_id, BLOB_CHOICE_LIST *bc_list)
Definition: ratngs.cpp:160
const int kMinSubscriptOffset
Definition: ratngs.cpp:41
float max_xheight() const
Definition: ratngs.h:126
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
Definition: unicharset.cpp:194
TBOX bounding_box() const
Definition: blobs.cpp:482
void print_state(const char *msg) const
Definition: ratngs.cpp:738
float rating() const
Definition: ratngs.h:325
~WERD_CHOICE()
Definition: ratngs.cpp:254