22 #include "config_auto.h" 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";
71 static const char *
const kPermuterTypeNames[] = {
73 kPermuterTypePuncPerm,
75 kPermuterTypeLowerPerm,
76 kPermuterTypeUpperPerm,
77 kPermuterTypeNgramPerm,
78 kPermuterTypeNumberPerm,
79 kPermuterTypeUserPatPerm,
80 kPermuterTypeSysDawgPerm,
81 kPermuterTypeDocDawgPerm,
82 kPermuterTypeUserDawgPerm,
83 kPermuterTypeFreqDawgPerm,
84 kPermuterTypeCompoundPerm
100 unichar_id_ = src_unichar_id;
101 rating_ = src_rating;
102 certainty_ = src_cert;
105 script_id_ = src_script_id;
124 matrix_cell_ = other.matrix_cell_;
125 min_xheight_ = other.min_xheight_;
126 max_xheight_ = other.max_xheight_;
128 classifier_ = other.classifier_;
129 fonts_ = other.fonts_;
141 matrix_cell_ = other.matrix_cell_;
142 min_xheight_ = other.min_xheight_;
143 max_xheight_ = other.max_xheight_;
145 classifier_ = other.classifier_;
146 fonts_ = other.fonts_;
157 tprintf(
"Baseline diff %g for %d v %d\n",
158 baseline_diff, unichar_id_, other.unichar_id_);
164 double denominator =
ClipToRange(std::min(this_range, other_range),
168 overlap /= denominator;
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);
181 BLOB_CHOICE_LIST* bc_list) {
183 BLOB_CHOICE_IT choice_it(bc_list);
184 for (choice_it.mark_cycle_pt(); !choice_it.cycled_list();
185 choice_it.forward()) {
195 return kPermuterTypeNames[
permuter];
201 switch (script_pos) {
220 : unicharset_(&unicharset){
226 lengths.push_back(
'\0');
227 STRING src_lengths = &lengths[0];
246 const char *src_lengths,
249 uint8_t src_permuter) {
250 int src_string_len = strlen(src_string);
251 if (src_string_len == 0) {
254 this->
init(src_lengths ? strlen(src_lengths): src_string_len);
257 for (
int i = 0; i < length_; ++i) {
258 int unichar_length = src_lengths ? src_lengths[i] : 1;
260 unicharset_->
unichar_to_id(src_string+offset, unichar_length);
262 certainties_[i] = src_certainty;
263 offset += unichar_length;
266 adjust_factor_ = 1.0f;
267 rating_ = src_rating;
268 certainty_ = src_certainty;
269 permuter_ = src_permuter;
270 dangerous_ambig_found_ =
false;
277 delete[] unichar_ids_;
278 delete[] script_pos_;
280 delete[] certainties_;
284 return kPermuterTypeNames[permuter_];
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);
304 for (
int i = 0; i < index; ++i)
306 int row = col + state_[index] - 1;
314 unichar_ids_[index] = blob_choice->
unichar_id();
316 state_[index] = blob_count;
317 certainties_[index] = blob_choice->
certainty();
327 for (
int i = 0; i < length_; ++i) {
345 for (
int i = 0; i < num; ++i) {
347 state_[start - 1] += state_[start + i];
348 else if (start + num < length_)
349 state_[start + num] += state_[start + i];
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];
366 for (
int i = 0; i < length_ / 2; ++i) {
368 unichar_ids_[i] = unicharset_->
get_mirror(unichar_ids_[length_-1-i]);
369 unichar_ids_[length_-1-i] = unicharset_->
get_mirror(tmp_id);
371 if (length_ % 2 != 0) {
372 unichar_ids_[length_/2] = unicharset_->
get_mirror(unichar_ids_[length_/2]);
386 while (*start <
length() &&
405 while (start < end &&
417 if (end < start) { end = start; }
419 for (
int i = start; i < end; i++) {
421 unichar_ids_[i], state_[i], 0.0f, certainties_[i]);
433 for (i = 0; i < length_; ++i) {
450 STRING *word_lengths_str)
const {
452 if (word_lengths_str !=
nullptr) *word_lengths_str =
"";
453 for (
int i = 0; i < length_; ++i) {
456 if (word_lengths_str !=
nullptr) {
457 *word_lengths_str += strlen(ch);
470 float rating,
float certainty) {
471 if (length_ == reserved_) {
487 while (reserved_ < length_ + second.
length()) {
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];
497 length_ += second.
length();
498 if (second.adjust_factor_ > adjust_factor_)
499 adjust_factor_ = second.adjust_factor_;
500 rating_ += second.
rating();
503 if (second.dangerous_ambig_found_)
504 dangerous_ambig_found_ =
true;
522 while (reserved_ < source.
length()) {
526 unicharset_ = source.unicharset_;
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];
534 length_ = source.
length();
535 adjust_factor_ = source.adjust_factor_;
536 rating_ = source.
rating();
541 dangerous_ambig_found_ = source.dangerous_ambig_found_;
552 for (
int i = 0; i < length_; ++i)
558 int position_counts[4];
559 for (
int i = 0; i < 4; i++) {
560 position_counts[i] = 0;
564 for (
int blob_index = 0; blob_index < length_; ++blob_index, ++chunk_index) {
568 if (state_ !=
nullptr) {
569 for (
int i = 1; i < state_[blob_index]; ++i) {
571 tblob = word->
blobs[chunk_index];
580 position_counts[script_pos_[blob_index]]++;
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",
591 for (
int i = 0; i < length_; i++) {
594 position_counts[sp]--;
605 for (
int blob_index = 0; blob_index < length_; ++blob_index) {
611 chunk_index += state_ !=
nullptr ? state_[blob_index] : 1;
619 if (positions != script_pos_) {
620 delete [] script_pos_;
622 memcpy(script_pos_, positions,
sizeof(positions[0]) *
length);
627 for (
int i = 0; i < length_; ++i)
628 script_pos_[i] = position;
634 const TBOX& blob_box,
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,
649 }
else if (top < sub_thresh_top && bottom < sub_thresh_bot) {
651 }
else if (bottom > sup_thresh_bot) {
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",
662 min_bottom, max_bottom, min_top, max_top,
663 sub_thresh_bot, sub_thresh_top,
672 int *sid =
new int[max_script];
674 for (x = 0; x < max_script; x++) sid[x] = 0;
675 for (x = 0; x < length_; ++x) {
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)
703 int total_chunks = 0;
704 for (
int i = 0; i < length_; ++i) {
705 total_chunks += state_[i];
706 if (total_chunks > blob_position) {
715 int total_chunks = 0;
716 for (
int i = 0; i < length_; ++i) {
717 total_chunks += state_[i];
729 for (
int i = 0; i < length_; ++i) {
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_);
736 for (
int i = 0; i < length_; ++i) {
740 for (
int i = 0; i < length_; ++i) {
744 for (
int i = 0; i < length_; ++i) {
748 for (
int i = 0; i < length_; ++i) {
749 tprintf(
"\t%.3f", certainties_[i]);
757 for (
int i = 0; i < length_; ++i)
765 #ifndef GRAPHICS_DISABLED 767 const int kNumColors = 6;
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;
777 prev_drawn_state[i] = state_[i];
779 if (already_done || word->
blobs.
empty())
return;
782 if (segm_window ==
nullptr) {
783 segm_window =
new ScrollView(
"Segmentation", 5, 10, 500, 256,
784 2000.0, 256.0,
true);
786 segm_window->
Clear();
791 for (
int c = 0; c < length_; ++c) {
794 for (
int i = 0; i < state_[c]; ++i, ++blob_index) {
797 blob->
plot(segm_window, color, color);
811 if (word2.
unicharset() != uchset)
return false;
816 if (w1end - w1start != w2end - w2start)
return false;
817 for (
int i = 0; i < w1end - w1start; i++) {
837 BLOB_CHOICE_LIST *ratings,
839 if (ratings->length() == 0) {
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(¤t_unicharset);
850 if (!c_it.at_last())
tprintf(
"\n");
static const float kBadRating
void remove_unichar_ids(int index, int num)
bool encode_string(const char *str, bool give_up_on_failure, GenericVector< UNICHAR_ID > *encoding, GenericVector< char > *lengths, int *encoded_length) const
MATRIX_COORD MatrixCoord(int index) const
void SetAllScriptPositions(tesseract::ScriptPos position)
const double kMaxOverlapDenominator
void append_unichar_id_space_allocated(UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)
void ZoomToRectangle(int x1, int y1, int x2, int y2)
const char * string() const
const UNICHARSET * unicharset() const
void plot(ScrollView *window, ScrollView::Color color, ScrollView::Color child_color)
void append_unichar_id(UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)
void UpdateStateForSplit(int blob_position)
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
int get_script_table_size() const
void make_bad()
Set the fields in this choice to be default (bad) values.
const int kBlnBaselineOffset
WERD_CHOICE shallow_copy(int start, int end) const
int16_t fontinfo_id() const
const double kMaxBaselineDrift
float max_x_height() const
Direction get_direction(UNICHAR_ID unichar_id) const
void reverse_and_mirror_unichar_ids()
void print_state(const char *msg) const
void GetNonSuperscriptSpan(int *start, int *end) const
bool PosAndSizeAgree(const BLOB_CHOICE &other, float x_height, bool debug) const
bool get_isdigit(UNICHAR_ID unichar_id) const
void get_top_bottom(UNICHAR_ID unichar_id, int *min_bottom, int *max_bottom, int *min_top, int *max_top) const
WERD_CHOICE(const UNICHARSET *unicharset)
int16_t fontinfo_id2() const
void set_blob_choice(int index, int blob_count, const BLOB_CHOICE *blob_choice)
void init_to_size(int size, const T &t)
float min_xheight() const
void DisplaySegmentation(TWERD *word)
void operator=(const ELIST_LINK &)
bool has_rtl_unichar_id() const
const double kMinXHeightMatch
void punct_stripped(int *start_core, int *end_core) const
#define ELISTIZE(CLASSNAME)
bool contains_unichar_id(UNICHAR_ID unichar_id) const
UNICHAR_ID unichar_id(int index) const
float max_xheight() const
DLLSYM void tprintf(const char *format,...)
TBOX bounding_box() const
void put(ICOORD pos, const T &thing)
int GetTopScriptID() const
bool EqualIgnoringCaseAndTerminalPunct(const WERD_CHOICE &word1, const WERD_CHOICE &word2)
const char * id_to_unichar_ext(UNICHAR_ID id) const
static std::string CleanupString(const char *utf8_str)
const int kMinSuperscriptOffset
const UNICHAR_ID * unichar_ids() const
float min_x_height() const
GenericVector< TBLOB * > blobs
const char * permuter_name() const
void string_and_lengths(STRING *word_str, STRING *word_lengths_str) const
void double_the_size()
Make more space in unichar_id_ and fragment_lengths_ arrays.
UNICHAR_ID get_mirror(UNICHAR_ID unichar_id) const
const char * id_to_unichar(UNICHAR_ID id) const
const char * ScriptPosToString(enum ScriptPos script_pos)
const int kMaxDropCapBottom
const STRING & unichar_string() const
tesseract::ScriptPos BlobPosition(int index) const
void print_ratings_list(const char *msg, BLOB_CHOICE_LIST *ratings, const UNICHARSET ¤t_unicharset)
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
BLOB_CHOICE * FindMatchingChoice(UNICHAR_ID char_id, BLOB_CHOICE_LIST *bc_list)
void SetScriptPositions(bool small_caps, TWERD *word, int debug=0)
char window_wait(ScrollView *win)
const int kMinSubscriptOffset
WERD_CHOICE & operator=(const WERD_CHOICE &source)
UNICHAR_ID to_lower(UNICHAR_ID unichar_id) const
static tesseract::ScriptPos ScriptPositionOf(bool print_debug, const UNICHARSET &unicharset, const TBOX &blob_box, UNICHAR_ID unichar_id)
UNICHAR_ID unichar_id() const
int get_script(UNICHAR_ID unichar_id) const
BLOB_CHOICE_LIST * blob_choices(int index, MATRIX *ratings) const
WERD_CHOICE & operator+=(const WERD_CHOICE &second)
int TotalOfStates() const