tesseract  4.1.0
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] = { 0, 0, 0, 0 };
559 
560  int chunk_index = 0;
561  for (int blob_index = 0; blob_index < length_; ++blob_index, ++chunk_index) {
562  TBLOB* tblob = word->blobs[chunk_index];
563  int uni_id = unichar_id(blob_index);
564  TBOX blob_box = tblob->bounding_box();
565  if (state_ != nullptr) {
566  for (int i = 1; i < state_[blob_index]; ++i) {
567  ++chunk_index;
568  tblob = word->blobs[chunk_index];
569  blob_box += tblob->bounding_box();
570  }
571  }
572  script_pos_[blob_index] = ScriptPositionOf(false, *unicharset_, blob_box,
573  uni_id);
574  if (small_caps && script_pos_[blob_index] != tesseract::SP_DROPCAP) {
575  script_pos_[blob_index] = tesseract::SP_NORMAL;
576  }
577  position_counts[script_pos_[blob_index]]++;
578  }
579  // If almost everything looks like a superscript or subscript,
580  // we most likely just got the baseline wrong.
581  if (position_counts[tesseract::SP_SUBSCRIPT] > 0.75 * length_ ||
582  position_counts[tesseract::SP_SUPERSCRIPT] > 0.75 * length_) {
583  if (debug >= 2) {
584  tprintf("Most characters of %s are subscript or superscript.\n"
585  "That seems wrong, so I'll assume we got the baseline wrong\n",
586  unichar_string().string());
587  }
588  for (int i = 0; i < length_; i++) {
589  ScriptPos sp = script_pos_[i];
591  position_counts[sp]--;
592  position_counts[tesseract::SP_NORMAL]++;
593  script_pos_[i] = tesseract::SP_NORMAL;
594  }
595  }
596  }
597 
598  if ((debug >= 1 && position_counts[tesseract::SP_NORMAL] < length_) ||
599  debug >= 2) {
600  tprintf("SetScriptPosition on %s\n", unichar_string().string());
601  int chunk_index = 0;
602  for (int blob_index = 0; blob_index < length_; ++blob_index) {
603  if (debug >= 2 || script_pos_[blob_index] != tesseract::SP_NORMAL) {
604  TBLOB* tblob = word->blobs[chunk_index];
605  ScriptPositionOf(true, *unicharset_, tblob->bounding_box(),
606  unichar_id(blob_index));
607  }
608  chunk_index += state_ != nullptr ? state_[blob_index] : 1;
609  }
610  }
611 }
612 // Sets the script_pos_ member from some source positions with a given length.
614  int length) {
615  ASSERT_HOST(length == length_);
616  if (positions != script_pos_) {
617  delete [] script_pos_;
618  script_pos_ = new ScriptPos[length];
619  memcpy(script_pos_, positions, sizeof(positions[0]) * length);
620  }
621 }
622 // Sets all the script_pos_ positions to the given position.
624  for (int i = 0; i < length_; ++i)
625  script_pos_[i] = position;
626 }
627 
628 /* static */
630  const UNICHARSET& unicharset,
631  const TBOX& blob_box,
634  int top = blob_box.top();
635  int bottom = blob_box.bottom();
636  int min_bottom, max_bottom, min_top, max_top;
637  unicharset.get_top_bottom(unichar_id,
638  &min_bottom, &max_bottom,
639  &min_top, &max_top);
640 
641  int sub_thresh_top = min_top - kMinSubscriptOffset;
642  int sub_thresh_bot = kBlnBaselineOffset - kMinSubscriptOffset;
643  int sup_thresh_bot = max_bottom + kMinSuperscriptOffset;
644  if (bottom <= kMaxDropCapBottom) {
645  retval = tesseract::SP_DROPCAP;
646  } else if (top < sub_thresh_top && bottom < sub_thresh_bot) {
647  retval = tesseract::SP_SUBSCRIPT;
648  } else if (bottom > sup_thresh_bot) {
649  retval = tesseract::SP_SUPERSCRIPT;
650  }
651 
652  if (print_debug) {
653  const char *pos = ScriptPosToString(retval);
654  tprintf("%s Character %s[bot:%d top: %d] "
655  "bot_range[%d,%d] top_range[%d, %d] "
656  "sub_thresh[bot:%d top:%d] sup_thresh_bot %d\n",
657  pos, unicharset.id_to_unichar(unichar_id),
658  bottom, top,
659  min_bottom, max_bottom, min_top, max_top,
660  sub_thresh_bot, sub_thresh_top,
661  sup_thresh_bot);
662  }
663  return retval;
664 }
665 
666 // Returns the script-id (eg Han) of the dominant script in the word.
668  int max_script = unicharset_->get_script_table_size();
669  int *sid = new int[max_script];
670  int x;
671  for (x = 0; x < max_script; x++) sid[x] = 0;
672  for (x = 0; x < length_; ++x) {
673  int script_id = unicharset_->get_script(unichar_id(x));
674  sid[script_id]++;
675  }
676  if (unicharset_->han_sid() != unicharset_->null_sid()) {
677  // Add the Hiragana & Katakana counts to Han and zero them out.
678  if (unicharset_->hiragana_sid() != unicharset_->null_sid()) {
679  sid[unicharset_->han_sid()] += sid[unicharset_->hiragana_sid()];
680  sid[unicharset_->hiragana_sid()] = 0;
681  }
682  if (unicharset_->katakana_sid() != unicharset_->null_sid()) {
683  sid[unicharset_->han_sid()] += sid[unicharset_->katakana_sid()];
684  sid[unicharset_->katakana_sid()] = 0;
685  }
686  }
687  // Note that high script ID overrides lower one on a tie, thus biasing
688  // towards non-Common script (if sorted that way in unicharset file).
689  int max_sid = 0;
690  for (x = 1; x < max_script; x++)
691  if (sid[x] >= sid[max_sid]) max_sid = x;
692  if (sid[max_sid] < length_ / 2)
693  max_sid = unicharset_->null_sid();
694  delete[] sid;
695  return max_sid;
696 }
697 
698 // Fixes the state_ for a chop at the given blob_posiiton.
699 void WERD_CHOICE::UpdateStateForSplit(int blob_position) {
700  int total_chunks = 0;
701  for (int i = 0; i < length_; ++i) {
702  total_chunks += state_[i];
703  if (total_chunks > blob_position) {
704  ++state_[i];
705  return;
706  }
707  }
708 }
709 
710 // Returns the sum of all the state elements, being the total number of blobs.
712  int total_chunks = 0;
713  for (int i = 0; i < length_; ++i) {
714  total_chunks += state_[i];
715  }
716  return total_chunks;
717 }
718 
724 void WERD_CHOICE::print(const char *msg) const {
725  tprintf("%s : ", msg);
726  for (int i = 0; i < length_; ++i) {
727  tprintf("%s", unicharset_->id_to_unichar(unichar_ids_[i]));
728  }
729  tprintf(" : R=%g, C=%g, F=%g, Perm=%d, xht=[%g,%g], ambig=%d\n",
730  rating_, certainty_, adjust_factor_, permuter_,
731  min_x_height_, max_x_height_, dangerous_ambig_found_);
732  tprintf("pos");
733  for (int i = 0; i < length_; ++i) {
734  tprintf("\t%s", ScriptPosToString(script_pos_[i]));
735  }
736  tprintf("\nstr");
737  for (int i = 0; i < length_; ++i) {
738  tprintf("\t%s", unicharset_->id_to_unichar(unichar_ids_[i]));
739  }
740  tprintf("\nstate:");
741  for (int i = 0; i < length_; ++i) {
742  tprintf("\t%d ", state_[i]);
743  }
744  tprintf("\nC");
745  for (int i = 0; i < length_; ++i) {
746  tprintf("\t%.3f", certainties_[i]);
747  }
748  tprintf("\n");
749 }
750 
751 // Prints the segmentation state with an introductory message.
752 void WERD_CHOICE::print_state(const char *msg) const {
753  tprintf("%s", msg);
754  for (int i = 0; i < length_; ++i)
755  tprintf(" %d", state_[i]);
756  tprintf("\n");
757 }
758 
759 // Displays the segmentation state of *this (if not the same as the last
760 // one displayed) and waits for a click in the window.
762 #ifndef GRAPHICS_DISABLED
763  // Number of different colors to draw with.
764  const int kNumColors = 6;
765  static ScrollView *segm_window = nullptr;
766  // Check the state against the static prev_drawn_state.
767  static GenericVector<int> prev_drawn_state;
768  bool already_done = prev_drawn_state.size() == length_;
769  if (!already_done) prev_drawn_state.init_to_size(length_, 0);
770  for (int i = 0; i < length_; ++i) {
771  if (prev_drawn_state[i] != state_[i]) {
772  already_done = false;
773  }
774  prev_drawn_state[i] = state_[i];
775  }
776  if (already_done || word->blobs.empty()) return;
777 
778  // Create the window if needed.
779  if (segm_window == nullptr) {
780  segm_window = new ScrollView("Segmentation", 5, 10, 500, 256,
781  2000.0, 256.0, true);
782  } else {
783  segm_window->Clear();
784  }
785 
786  TBOX bbox;
787  int blob_index = 0;
788  for (int c = 0; c < length_; ++c) {
789  auto color =
790  static_cast<ScrollView::Color>(c % kNumColors + 3);
791  for (int i = 0; i < state_[c]; ++i, ++blob_index) {
792  TBLOB* blob = word->blobs[blob_index];
793  bbox += blob->bounding_box();
794  blob->plot(segm_window, color, color);
795  }
796  }
797  segm_window->ZoomToRectangle(bbox.left(), bbox.top(),
798  bbox.right(), bbox.bottom());
799  segm_window->Update();
800  window_wait(segm_window);
801 #endif
802 }
803 
804 
806  const WERD_CHOICE &word2) {
807  const UNICHARSET *uchset = word1.unicharset();
808  if (word2.unicharset() != uchset) return false;
809  int w1start, w1end;
810  word1.punct_stripped(&w1start, &w1end);
811  int w2start, w2end;
812  word2.punct_stripped(&w2start, &w2end);
813  if (w1end - w1start != w2end - w2start) return false;
814  for (int i = 0; i < w1end - w1start; i++) {
815  if (uchset->to_lower(word1.unichar_id(w1start + i)) !=
816  uchset->to_lower(word2.unichar_id(w2start + i))) {
817  return false;
818  }
819  }
820  return true;
821 }
822 
833 void print_ratings_list(const char *msg,
834  BLOB_CHOICE_LIST *ratings,
835  const UNICHARSET &current_unicharset) {
836  if (ratings->length() == 0) {
837  tprintf("%s:<none>\n", msg);
838  return;
839  }
840  if (*msg != '\0') {
841  tprintf("%s\n", msg);
842  }
843  BLOB_CHOICE_IT c_it;
844  c_it.set_to_list(ratings);
845  for (c_it.mark_cycle_pt(); !c_it.cycled_list(); c_it.forward()) {
846  c_it.data()->print(&current_unicharset);
847  if (!c_it.at_last()) tprintf("\n");
848  }
849  tprintf("\n");
850  fflush(stdout);
851 }
UNICHAR_ID unichar_id() const
Definition: ratngs.h:77
float max_xheight() const
Definition: ratngs.h:127
void set_blob_choice(int index, int blob_count, const BLOB_CHOICE *blob_choice)
Definition: ratngs.cpp:312
const UNICHAR_ID * unichar_ids() const
Definition: ratngs.h:312
int16_t top() const
Definition: rect.h:58
void append_unichar_id(UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)
Definition: ratngs.cpp:468
void append_unichar_id_space_allocated(UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)
Definition: ratngs.h:452
void operator=(const ELIST_LINK &)
Definition: elst.h:99
float yshift() const
Definition: ratngs.h:130
Definition: rect.h:34
void init(int reserved)
Definition: ratngs.h:409
void UpdateStateForSplit(int blob_position)
Definition: ratngs.cpp:699
Definition: blobs.h:397
float rating() const
Definition: ratngs.h:80
UNICHAR_ID get_mirror(UNICHAR_ID unichar_id) const
Definition: unicharset.h:697
Definition: strngs.h:45
GenericVector< TBLOB * > blobs
Definition: blobs.h:438
void ZoomToRectangle(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:757
WERD_CHOICE & operator+=(const WERD_CHOICE &second)
Definition: ratngs.cpp:485
TBOX bounding_box() const
Definition: blobs.cpp:472
const STRING & unichar_string() const
Definition: ratngs.h:541
UNICHAR_ID to_lower(UNICHAR_ID unichar_id) const
Definition: unicharset.h:704
bool PosAndSizeAgree(const BLOB_CHOICE &other, float x_height, bool debug) const
Definition: ratngs.cpp:152
bool contains_unichar_id(UNICHAR_ID unichar_id) const
Definition: ratngs.cpp:326
const int kMinSubscriptOffset
Definition: ratngs.cpp:43
float min_x_height() const
Definition: ratngs.h:336
MATRIX_COORD MatrixCoord(int index) const
Definition: ratngs.cpp:302
int length() const
Definition: ratngs.h:303
static std::string CleanupString(const char *utf8_str)
Definition: unicharset.h:246
Direction get_direction(UNICHAR_ID unichar_id) const
Definition: unicharset.h:690
Definition: blobs.h:263
const char * id_to_unichar(UNICHAR_ID id) const
Definition: unicharset.cpp:291
bool EqualIgnoringCaseAndTerminalPunct(const WERD_CHOICE &word1, const WERD_CHOICE &word2)
Definition: ratngs.cpp:805
int null_sid() const
Definition: unicharset.h:884
WERD_CHOICE & operator=(const WERD_CHOICE &source)
Definition: ratngs.cpp:521
const double kMaxOverlapDenominator
Definition: ratngs.cpp:49
void punct_stripped(int *start_core, int *end_core) const
Definition: ratngs.cpp:383
void init_to_size(int size, const T &t)
int hiragana_sid() const
Definition: unicharset.h:890
const double kMinXHeightMatch
Definition: ratngs.cpp:52
int GetTopScriptID() const
Definition: ratngs.cpp:667
const char * id_to_unichar_ext(UNICHAR_ID id) const
Definition: unicharset.cpp:299
T get(ICOORD pos) const
Definition: matrix.h:231
float min_xheight() const
Definition: ratngs.h:124
void SetScriptPositions(bool small_caps, TWERD *word, int debug=0)
Definition: ratngs.cpp:550
bool get_isdigit(UNICHAR_ID unichar_id) const
Definition: unicharset.h:512
void string_and_lengths(STRING *word_str, STRING *word_lengths_str) const
Definition: ratngs.cpp:449
void print() const
Definition: ratngs.h:580
BLOB_CHOICE * FindMatchingChoice(UNICHAR_ID char_id, BLOB_CHOICE_LIST *bc_list)
Definition: ratngs.cpp:180
static void Update()
Definition: scrollview.cpp:709
float certainty() const
Definition: ratngs.h:83
void DisplaySegmentation(TWERD *word)
Definition: ratngs.cpp:761
const int kBlnBaselineOffset
Definition: normalis.h:25
#define ELISTIZE(CLASSNAME)
Definition: elst.h:955
void print_ratings_list(const char *msg, BLOB_CHOICE_LIST *ratings, const UNICHARSET &current_unicharset)
Definition: ratngs.cpp:833
void plot(ScrollView *window, ScrollView::Color color, ScrollView::Color child_color)
Definition: blobs.cpp:514
int script_id() const
Definition: ratngs.h:112
int16_t fontinfo_id() const
Definition: ratngs.h:86
float rating() const
Definition: ratngs.h:327
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
Definition: unicharset.cpp:210
const int kMinSuperscriptOffset
Definition: ratngs.cpp:45
char window_wait(ScrollView *win)
Definition: callcpp.cpp:103
void put(ICOORD pos, const T &thing)
Definition: matrix.h:223
void get_top_bottom(UNICHAR_ID unichar_id, int *min_bottom, int *max_bottom, int *min_top, int *max_top) const
Definition: unicharset.h:568
~WERD_CHOICE()
Definition: ratngs.cpp:276
const char * string() const
Definition: strngs.cpp:194
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:36
void Clear()
Definition: scrollview.cpp:589
bool empty() const
Definition: genericvector.h:89
const int kMaxDropCapBottom
Definition: ratngs.cpp:47
void double_the_size()
Make more space in unichar_id_ and fragment_lengths_ arrays.
Definition: ratngs.h:387
void make_bad()
Set the fields in this choice to be default (bad) values.
Definition: ratngs.h:443
WERD_CHOICE(const UNICHARSET *unicharset)
Definition: ratngs.h:278
Definition: matrix.h:578
int16_t right() const
Definition: rect.h:79
int16_t bottom() const
Definition: rect.h:65
const char * permuter_name() const
Definition: ratngs.cpp:283
int get_script_table_size() const
Definition: unicharset.h:849
bool has_rtl_unichar_id() const
Definition: ratngs.cpp:431
void GetNonSuperscriptSpan(int *start, int *end) const
Definition: ratngs.cpp:397
int NumBlobs() const
Definition: blobs.h:427
float certainty() const
Definition: ratngs.h:330
UNICHAR_ID unichar_id(int index) const
Definition: ratngs.h:315
const char * ScriptPosToString(enum ScriptPos script_pos)
Definition: ratngs.cpp:200
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:108
#define ASSERT_HOST(x)
Definition: errcode.h:88
BLOB_CHOICE()
Definition: ratngs.h:52
int16_t left() const
Definition: rect.h:72
int16_t fontinfo_id2() const
Definition: ratngs.h:89
int UNICHAR_ID
Definition: unichar.h:34
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:259
void remove_unichar_ids(int index, int num)
Definition: ratngs.cpp:342
BLOB_CHOICE_LIST * blob_choices(int index, MATRIX *ratings) const
Definition: ratngs.cpp:290
tesseract::ScriptPos BlobPosition(int index) const
Definition: ratngs.h:322
float max_x_height() const
Definition: ratngs.h:339
void reverse_and_mirror_unichar_ids()
Definition: ratngs.cpp:365
int size() const
Definition: genericvector.h:70
int get_script(UNICHAR_ID unichar_id) const
Definition: unicharset.h:663
int TotalOfStates() const
Definition: ratngs.cpp:711
static tesseract::ScriptPos ScriptPositionOf(bool print_debug, const UNICHARSET &unicharset, const TBOX &blob_box, UNICHAR_ID unichar_id)
Definition: ratngs.cpp:629
WERD_CHOICE shallow_copy(int start, int end) const
Definition: ratngs.cpp:414
BlobChoiceClassifier
Definition: ratngs.h:41
void print_state(const char *msg) const
Definition: ratngs.cpp:752
int han_sid() const
Definition: unicharset.h:889
const double kMaxBaselineDrift
Definition: ratngs.cpp:55
const UNICHARSET * unicharset() const
Definition: ratngs.h:300
void SetAllScriptPositions(tesseract::ScriptPos position)
Definition: ratngs.cpp:623
uint8_t permuter() const
Definition: ratngs.h:346
int katakana_sid() const
Definition: unicharset.h:891