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